/[LeafOK_CVS]/lbbs/src/screen.c
ViewVC logotype

Diff of /lbbs/src/screen.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.14 by sysadm, Tue Mar 22 13:36:13 2005 UTC Revision 1.141 by sysadm, Sun Feb 8 15:43:19 2026 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                            screen.c  -  description  /*
3                               -------------------   * screen
4      begin                : Mon Oct 18 2004   *   - advanced telnet-based user interactive input / output features
5      copyright            : (C) 2004 by Leaflet   *
6      email                : leaflet@leafok.com   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
8    
9  /***************************************************************************  #ifdef HAVE_CONFIG_H
10   *                                                                         *  #include "config.h"
11   *   This program is free software; you can redistribute it and/or modify  *  #endif
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
15    #include "editor.h"
16    #include "file_loader.h"
17  #include "io.h"  #include "io.h"
18  #include <sys/types.h>  #include "log.h"
19  #include <sys/stat.h>  #include "login.h"
20    #include "screen.h"
21    #include "str_process.h"
22    #include <ctype.h>
23    #include <errno.h>
24    #include <fcntl.h>
25    #include <string.h>
26    #include <stdlib.h>
27  #include <unistd.h>  #include <unistd.h>
28    #include <wchar.h>
29    #include <sys/param.h>
30    #include <sys/stat.h>
31    #include <sys/types.h>
32    
33  #define ACTIVE_BOARD_HEIGHT     8  const char CTRL_SEQ_CLR_LINE[] = "\033[K";
34    
35  int screen_lines = 24;  static const int ACTIVE_BOARD_HEIGHT = 8;
36    
37  void  static const int STR_TOP_LEFT_MAX_LEN = 80;
38  moveto (int row, int col)  static const int STR_TOP_MIDDLE_MAX_LEN = 40;
39    static const int STR_TOP_RIGHT_MAX_LEN = 80;
40    
41    static size_t get_time_str(char *s, size_t len)
42  {  {
43    if (row >= 0)          time_t curtime;
44      {          struct tm local_tm;
45        prints ("\033[%d;%dH", row, col);  
46      }          time(&curtime);
47    else          localtime_r(&curtime, &local_tm);
48      {          size_t j = strftime(s, len, "%m/%d %H:%M %Z", &local_tm);
49        prints ("\r");  
50      }          return j;
   iflush ();  
51  }  }
52    
53  void  void moveto(int row, int col)
 clrtoeol ()  
54  {  {
55    prints ("\033[K");          if (row >= 0)
56    iflush ();          {
57                    prints("\033[%d;%dH", row, col);
58            }
59            else
60            {
61                    prints("\r");
62            }
63  }  }
64    
65  void  void clrtoeol()
 clrline (int line_begin, int line_end)  
66  {  {
67    int i;          prints(CTRL_SEQ_CLR_LINE);
68    }
69    
70    for (i = line_begin; i <= line_end; i++)  void clrline(int line_begin, int line_end)
71      {  {
72        moveto (i, 0);          int i;
       prints ("\033[K");  
     }  
73    
74    iflush ();          for (i = line_begin; i <= line_end; i++)
75            {
76                    moveto(i, 0);
77                    prints(CTRL_SEQ_CLR_LINE);
78            }
79  }  }
80    
81  void  void clrtobot(int line_begin)
 clearscr ()  
82  {  {
83    prints ("\33[2J");          moveto(line_begin, 0);
84    moveto (0, 0);          prints("\033[J");
85    iflush ();          moveto(line_begin, 0);
86  }  }
87    
88  int  void clearscr()
 press_any_key ()  
89  {  {
90    moveto (screen_lines, 0);          prints("\033[2J");
91    clrtoeol ();          moveto(1, 1);
92    }
   prints ("                           \033[1;33m按任意键继续...\033[0;37m");  
   iflush ();  
93    
94    return igetch_t (60);  inline int press_any_key()
95    {
96            return press_any_key_ex("                           \033[1;33m鎸変换鎰忛敭缁х画...\033[m", 60);
97  }  }
98    
99  void  int press_any_key_ex(const char *msg, int sec)
 set_input_echo (int echo)  
100  {  {
101    if (echo)          moveto(SCREEN_ROWS, 0);
102      {          clrtoeol();
103        outc ('\x83');            // ASCII code 131  
104        iflush ();          prints(msg);
105      }          iflush();
106    else  
107      {          return press_any_key_no_prompt(sec);
 //    outc ('\x85'); // ASCII code 133  
       prints ("\xff\xfb\x01\xff\xfb\x03");  
       iflush ();  
       igetch_t (60);  
       igetch_t (60);  
     }  
108  }  }
109    
110  int  inline int press_any_key_no_prompt(int sec)
 str_input (char *buffer, int buffer_length, int echo_mode)  
111  {  {
112    char buf[256], ch;          int ch = 0;
113    int c, offset = 0, len, loop = 1, i, hz = 0;          int duration = 0;
114            time_t t_begin = time(NULL);
115    
116    memset (buffer, '\0', buffer_length);          igetch_reset();
117    
118    while (c = igetch_t (60))          do
     {  
       if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)  
         break;  
       if (c == LF)  
         continue;  
       if (c == BACKSPACE)  
119          {          {
120            if (offset > 0)                  ch = igetch_t(sec - duration);
121              {                  duration = (int)(time(NULL) - t_begin);
122                buffer[--offset] = '\0';          } while (!SYS_server_exit && ch == 0 && duration < 60);
123                prints ("\b \b");  
124  //            clrtoeol ();          return ch;
125                iflush ();  }
126              }  
127            continue;  void set_input_echo(int echo)
128          }  {
129        if (c > 255 || iscntrl (c))          if (echo)
         {  
           continue;  
         }  
       if (c > 127 && c <= 255)  
         {  
           hz = (!hz);  
         }  
       if (offset >= buffer_length)  
130          {          {
131            outc ('\a');                  outc('\x83'); // ASCII code 131
           continue;  
132          }          }
133        buffer[offset++] = (char) c;          else
       buffer[offset] = '\0';  
       switch (echo_mode)  
134          {          {
135          case 0:                  // outc ('\x85'); // ASCII code 133
136            outc ((char) c);                  prints("\xff\xfb\x01\xff\xfb\x03");
           break;  
         case 1:  
           outc ('*');  
           break;  
137          }          }
138        if (!hz)          iflush();
139    }
140    
141    static int _str_input(char *buffer, int buf_size, int max_display_len, enum io_echo_t echo_mode)
142    {
143            int ch;
144            int offset = 0;
145            int eol;
146            int display_len;
147            char input_str[5];
148            int str_len = 0;
149            wchar_t wcs[2];
150            char c;
151    
152            buffer[buf_size - 1] = '\0';
153            offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
154    
155            igetch_reset();
156    
157            while (!SYS_server_exit)
158          {          {
159            iflush ();                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
160    
161                    if (ch == CR)
162                    {
163                            break;
164                    }
165                    else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
166                    {
167                            return -1;
168                    }
169                    else if (ch == LF || ch == '\0')
170                    {
171                            continue;
172                    }
173                    else if (ch == KEY_ESC)
174                    {
175                            buffer[0] = '\0';
176                            offset = 0;
177                            break;
178                    }
179                    else if (ch == BACKSPACE || ch == KEY_DEL)
180                    {
181                            if (offset > 0)
182                            {
183                                    offset--;
184                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
185                                    {
186                                            while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
187                                            {
188                                                    offset--;
189                                            }
190                                            display_len--;
191                                            prints("\033[D \033[D");
192                                    }
193                                    buffer[offset] = '\0';
194                                    display_len--;
195                                    prints("\033[D \033[D");
196                                    iflush();
197                            }
198                            continue;
199                    }
200                    else if (ch > 255 || iscntrl(ch))
201                    {
202                            continue;
203                    }
204                    else if ((ch & 0xff80) == 0x80) // head of multi-byte character
205                    {
206                            str_len = 0;
207                            c = (char)(ch & 0xf0);
208                            while (c & 0x80)
209                            {
210                                    input_str[str_len] = (char)(ch - 256);
211                                    str_len++;
212                                    c = (c & 0x7f) << 1;
213    
214                                    if ((c & 0x80) == 0) // Input completed
215                                    {
216                                            break;
217                                    }
218    
219                                    // Expect additional bytes of input
220                                    ch = igetch(100);                                                // 0.1 second
221                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
222                                    {
223                                            log_debug("Ignore %d bytes of incomplete UTF8 character", str_len);
224                                            str_len = 0;
225                                            break;
226                                    }
227                            }
228                            input_str[str_len] = '\0';
229    
230                            if (str_len == 0) // Incomplete input
231                            {
232                                    continue;
233                            }
234    
235                            if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
236                            {
237                                    log_error("mbstowcs() error");
238                            }
239                            if (offset + str_len > buf_size - 1 || display_len + (UTF8_fixed_width ? 2 : wcwidth(wcs[0])) > max_display_len) // No enough space for Chinese character
240                            {
241                                    outc('\a');
242                                    iflush();
243                                    continue;
244                            }
245    
246                            memcpy(buffer + offset, input_str, (size_t)str_len);
247                            offset += str_len;
248                            buffer[offset] = '\0';
249                            display_len += 2;
250    
251                            switch (echo_mode)
252                            {
253                            case DOECHO:
254                                    prints(input_str);
255                                    break;
256                            case NOECHO:
257                                    prints("**");
258                                    break;
259                            }
260                    }
261                    else if (ch >= 32 && ch < 127) // Printable character
262                    {
263                            if (offset + 1 > buf_size - 1 || display_len + 1 > max_display_len)
264                            {
265                                    outc('\a');
266                                    iflush();
267                                    continue;
268                            }
269    
270                            buffer[offset++] = (char)ch;
271                            buffer[offset] = '\0';
272                            display_len++;
273    
274                            switch (echo_mode)
275                            {
276                            case DOECHO:
277                                    outc((char)ch);
278                                    break;
279                            case NOECHO:
280                                    outc('*');
281                                    break;
282                            }
283                    }
284                    else // Invalid character
285                    {
286                            continue;
287                    }
288    
289                    iflush();
290          }          }
     }  
291    
292    prints ("\r\n");          return offset;
293    iflush ();  }
294    
295    int str_input(char *buffer, int buf_size, enum io_echo_t echo_mode)
296    {
297            int len;
298    
299            buffer[0] = '\0';
300    
301            len = _str_input(buffer, buf_size, buf_size, echo_mode);
302    
303            prints("\r\n");
304            iflush();
305    
306    return offset;          return len;
307  }  }
308    
309  int  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len)
 display_file (const char *filename)  
310  {  {
311    char buffer[260];          int len = 0;
312    FILE *fin;          int col_cur = 0;
313    int i;          int ch;
314            int offset = 0;
315            int eol;
316            int display_len;
317            char input_str[5];
318            int str_len = 0;
319            wchar_t wcs[2];
320            int wc_len;
321            char c;
322    
323            buffer[buf_size - 1] = '\0';
324            offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
325            buffer[offset] = '\0';
326            len = offset;
327            col_cur = col + str_length(prompt, 1) + display_len;
328    
329            moveto(row, col);
330            prints("%s", prompt);
331            prints("%s", buffer);
332            prints("%*s", max_display_len - display_len, "");
333            moveto(row, col_cur);
334            iflush();
335    
336    if ((fin = fopen (filename, "r")) == NULL)          igetch_reset();
     {  
       return -1;  
     }  
337    
338    while (fgets (buffer, 255, fin))          while (!SYS_server_exit)
     {  
       i = strlen (buffer);  
       if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
339          {          {
340            buffer[i - 1] = '\r';                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
341            buffer[i] = '\n';  
342            buffer[i + 1] = '\0';                  if (ch == CR)
343                    {
344                            break;
345                    }
346                    else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
347                    {
348                            return -1;
349                    }
350                    else if (ch == LF || ch == '\0')
351                    {
352                            continue;
353                    }
354                    else if (ch == KEY_ESC)
355                    {
356                            buffer[0] = '\0';
357                            len = 0;
358                            break;
359                    }
360                    else if (ch == BACKSPACE)
361                    {
362                            if (offset > 0)
363                            {
364                                    str_len = 1;
365                                    offset--;
366                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
367                                    {
368                                            while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
369                                            {
370                                                    str_len++;
371                                                    offset--;
372                                            }
373    
374                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
375                                            {
376                                                    log_error("mbstowcs() error");
377                                            }
378                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
379    
380                                            if (wc_len == 2)
381                                            {
382                                                    display_len--;
383                                                    col_cur--;
384                                            }
385                                    }
386    
387                                    memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
388                                    len -= str_len;
389                                    buffer[len] = '\0';
390                                    display_len--;
391                                    col_cur--;
392    
393                                    moveto(row, col_cur);
394                                    prints("%s", buffer + offset);
395                                    prints("%*s", max_display_len - display_len, "");
396                                    moveto(row, col_cur);
397                                    iflush();
398                            }
399                            continue;
400                    }
401                    else if (ch == KEY_DEL)
402                    {
403                            if (offset < len)
404                            {
405                                    if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
406                                    {
407                                            str_len = 0;
408                                            c = (char)(buffer[offset] & 0xf0);
409                                            while (c & 0x80)
410                                            {
411                                                    str_len++;
412                                                    c = (c & 0x7f) << 1;
413                                            }
414                                            display_len--;
415                                    }
416                                    else
417                                    {
418                                            str_len = 1;
419                                    }
420    
421                                    memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
422                                    len -= str_len;
423                                    buffer[len] = '\0';
424                                    display_len--;
425    
426                                    moveto(row, col_cur);
427                                    prints("%s", buffer + offset);
428                                    prints("%*s", max_display_len - display_len, "");
429                                    moveto(row, col_cur);
430                                    iflush();
431                            }
432                            continue;
433                    }
434                    else if (ch == KEY_LEFT)
435                    {
436                            if (offset > 0)
437                            {
438                                    str_len = 1;
439                                    offset--;
440                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
441                                    {
442                                            while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
443                                            {
444                                                    str_len++;
445                                                    offset--;
446                                            }
447    
448                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
449                                            {
450                                                    log_error("mbstowcs() error");
451                                            }
452                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
453    
454                                            if (wc_len == 2)
455                                            {
456                                                    col_cur--;
457                                            }
458                                    }
459                                    col_cur--;
460    
461                                    moveto(row, col_cur);
462                                    iflush();
463                            }
464                            continue;
465                    }
466                    else if (ch == KEY_RIGHT)
467                    {
468                            if (offset < len)
469                            {
470                                    str_len = 0;
471                                    if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
472                                    {
473                                            c = (char)(buffer[offset] & 0xf0);
474                                            while (c & 0x80)
475                                            {
476                                                    str_len++;
477                                                    c = (c & 0x7f) << 1;
478                                            }
479    
480                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
481                                            {
482                                                    log_error("mbstowcs() error");
483                                            }
484                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
485    
486                                            if (wc_len == 2)
487                                            {
488                                                    col_cur++;
489                                            }
490                                    }
491                                    else
492                                    {
493                                            str_len = 1;
494                                    }
495    
496                                    col_cur++;
497                                    offset += str_len;
498    
499                                    moveto(row, col_cur);
500                                    iflush();
501                            }
502                            continue;
503                    }
504                    else if (ch == KEY_HOME || ch == KEY_UP)
505                    {
506                            if (offset > 0)
507                            {
508                                    offset = 0;
509                                    col_cur = col + str_length(prompt, 1);
510    
511                                    moveto(row, col_cur);
512                                    iflush();
513                            }
514                            continue;
515                    }
516                    else if (ch == KEY_END || ch == KEY_DOWN)
517                    {
518                            if (offset < len)
519                            {
520                                    offset = len;
521                                    col_cur = col + str_length(prompt, 1) + display_len;
522    
523                                    moveto(row, col_cur);
524                                    iflush();
525                            }
526                            continue;
527                    }
528                    else if (ch > 255 || iscntrl(ch))
529                    {
530                            continue;
531                    }
532                    else if ((ch & 0xff80) == 0x80) // head of multi-byte character
533                    {
534                            str_len = 0;
535                            c = (char)(ch & 0xf0);
536                            while (c & 0x80)
537                            {
538                                    input_str[str_len] = (char)(ch - 256);
539                                    str_len++;
540                                    c = (c & 0x7f) << 1;
541    
542                                    if ((c & 0x80) == 0) // Input completed
543                                    {
544                                            break;
545                                    }
546    
547                                    // Expect additional bytes of input
548                                    ch = igetch(100);                                                // 0.1 second
549                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
550                                    {
551                                            log_debug("Ignore %d bytes of incomplete UTF8 character", str_len);
552                                            str_len = 0;
553                                            break;
554                                    }
555                            }
556                            input_str[str_len] = '\0';
557    
558                            if (str_len == 0) // Incomplete input
559                            {
560                                    continue;
561                            }
562    
563                            if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
564                            {
565                                    log_error("mbstowcs() error");
566                            }
567                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
568    
569                            if (len + str_len > buf_size - 1 ||
570                                    display_len + wc_len > max_display_len) // No enough space for Chinese character
571                            {
572                                    outc('\a');
573                                    iflush();
574                                    continue;
575                            }
576    
577                            memmove(buffer + offset + str_len, buffer + offset, (size_t)(len - offset));
578                            memcpy(buffer + offset, input_str, (size_t)str_len);
579                            len += str_len;
580                            buffer[len] = '\0';
581                            display_len += wc_len;
582    
583                            moveto(row, col_cur);
584                            prints("%s", buffer + offset);
585                            prints("%*s", max_display_len - display_len, "");
586    
587                            col_cur += wc_len;
588    
589                            moveto(row, col_cur);
590                            iflush();
591    
592                            offset += str_len;
593                    }
594                    else if (ch >= 32 && ch < 127) // Printable character
595                    {
596                            if (len + 1 > buf_size - 1 || display_len + 1 > max_display_len)
597                            {
598                                    outc('\a');
599                                    iflush();
600                                    continue;
601                            }
602    
603                            memmove(buffer + offset + 1, buffer + offset, (size_t)(len - offset));
604                            buffer[offset] = (char)ch;
605                            len++;
606                            buffer[len] = '\0';
607                            display_len++;
608    
609                            moveto(row, col_cur);
610                            prints("%s", buffer + offset);
611                            prints("%*s", max_display_len - display_len, "");
612    
613                            col_cur++;
614    
615                            moveto(row, col_cur);
616                            iflush();
617    
618                            offset++;
619                    }
620                    else // Invalid character
621                    {
622                            continue;
623                    }
624          }          }
       prints (buffer);  
       iflush ();  
     }  
   fclose (fin);  
625    
626    return 0;          return len;
627  }  }
628    
629  int  int display_data(const void *p_data, long display_line_total, const long *p_line_offsets, int eof_exit,
630  display_file_ex (const char *filename, int begin_line, int wait)                                   display_data_key_handler key_handler, const char *help_filename)
631  {  {
632    char buffer[260], temp[256];          static int show_help = 1;
633    int i, ch, input_ok;          char buffer[LINE_BUFFER_LEN];
634    long int line, c_line_begin = 0, c_line_total = 0;          DISPLAY_CTX ctx;
635    long int f_line, f_size, f_offset;          int ch = 0;
636    FILE *fin;          int input_ok;
637    struct stat f_stat;          const int screen_begin_row = 1;
638            const int screen_row_total = SCREEN_ROWS - screen_begin_row;
639            int output_current_row = screen_begin_row;
640            int output_end_row = SCREEN_ROWS - 1;
641            long int line_current = 0;
642            long int len;
643            long int percentile;
644            int loop;
645            int eol, display_len;
646    
647    clrline (begin_line, screen_lines);          clrline(output_current_row, SCREEN_ROWS);
   line = begin_line;  
   moveto (line, 0);  
648    
649    if ((fin = fopen (filename, "r")) != NULL)          // update msg_ext with extended key handler
650      {          if (key_handler(&ch, &ctx) != 0)
       if (fstat (fileno (fin), &f_stat) != 0)  
651          {          {
652            log_error ("Get file stat failed\n");                  return ch;
           return -1;  
653          }          }
       f_size = f_stat.st_size;  
   
       while (fgets (buffer, 255, fin))  
         c_line_total++;  
       rewind (fin);  
654    
655        while (fgets (buffer, 255, fin))          loop = 1;
656            while (!SYS_server_exit && loop)
657          {          {
658            if (line >= screen_lines)                  if (eof_exit > 0 && line_current >= display_line_total)
             {  
               f_offset = ftell (fin);  
   
               moveto (screen_lines, 0);  
               prints  
                 ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",  
                  (f_offset - strlen (buffer)) * 100 / f_size);  
               iflush ();  
   
               input_ok = 0;  
               while (!input_ok)  
659                  {                  {
660                    ch = igetch_t (MAX_DELAY_TIME);                          if (eof_exit == 1)
                   input_ok = 1;  
                   switch (ch)  
                     {  
                     case KEY_UP:  
                       c_line_begin--;  
                       if (c_line_begin >= 0)  
661                          {                          {
662                            rewind (fin);                                  ch = press_any_key();
                           for (f_line = 0; f_line < c_line_begin; f_line++)  
                             {  
                               if (fgets (buffer, 255, fin) == NULL)  
                                 goto exit;  
                             }  
663                          }                          }
664                        else                          else // if (eof_exit == 2)
665                          {                          {
666                            goto exit;                                  iflush();
667                          }                          }
668                        break;  
669                      case KEY_DOWN:                          loop = 0;
670                      case CR:                          break;
671                        c_line_begin++;                  }
672                        rewind (fin);  
673                        for (f_line = 0; f_line < c_line_begin; f_line++)                  if (line_current >= display_line_total || output_current_row > output_end_row)
674                    {
675                            ctx.reach_begin = (line_current < output_current_row ? 1 : 0);
676    
677                            if (line_current - (output_current_row - screen_begin_row) + screen_row_total < display_line_total)
678                          {                          {
679                            if (fgets (buffer, 255, fin) == NULL)                                  percentile = (line_current - (output_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total;
680                              goto exit;                                  ctx.reach_end = 0;
681                          }                          }
682                        break;                          else
                     case KEY_PGUP:  
                     case Ctrl ('B'):  
                       if (c_line_begin > 0)  
                         c_line_begin -= (screen_lines - begin_line - 1);  
                       else  
                         goto exit;  
                       if (c_line_begin < 0)  
                         c_line_begin = 0;  
                       rewind (fin);  
                       for (f_line = 0; f_line < c_line_begin; f_line++)  
683                          {                          {
684                            if (fgets (buffer, 255, fin) == NULL)                                  percentile = 100;
685                              goto exit;                                  ctx.reach_end = 1;
686                          }                          }
687                        break;  
688                      case KEY_RIGHT:                          ctx.line_top = line_current - (output_current_row - screen_begin_row) + 1;
689                      case KEY_PGDN:                          ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);
690                      case Ctrl ('F'):  
691                      case KEY_SPACE:                          snprintf(buffer, sizeof(buffer),
692                        c_line_begin += (screen_lines - begin_line - 1);                                           "\033[1;44;33m绗琝033[32m%ld\033[33m-\033[32m%ld\033[33m琛 (\033[32m%ld%%\033[33m) %s",
693                        if (c_line_begin + (screen_lines - begin_line) >                                           ctx.line_top,
694                            c_line_total)                                           ctx.line_bottom,
695                          c_line_begin =                                           percentile,
696                            c_line_total - (screen_lines - begin_line);                                           ctx.msg);
697                        rewind (fin);  
698                        for (f_line = 0; f_line < c_line_begin; f_line++)                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
699                            for (; display_len < SCREEN_COLS; display_len++)
700                          {                          {
701                            if (fgets (buffer, 255, fin) == NULL)                                  buffer[len++] = ' ';
                             goto exit;  
702                          }                          }
703                        break;                          buffer[len] = '\0';
704                      case KEY_NULL:                          strncat(buffer, "\033[m", sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer)));
                     case KEY_TIMEOUT:  
                     case KEY_LEFT:  
                     case 'q':  
                     case 'Q':  
                       goto exit;  
                       break;  
                     case '?':  
                     case 'h':  
                     case 'H':  
                       //Display help information  
                       strcpy (temp, app_home_dir);  
                       strcat (temp, "data/read_help.txt");  
                       display_file_ex (temp, begin_line, 1);  
705    
706                        //Refresh after display help information                          moveto(SCREEN_ROWS, 0);
707                        rewind (fin);                          prints("%s", buffer);
708                        for (f_line = 0; f_line < c_line_begin; f_line++)                          iflush();
709    
710                            input_ok = 0;
711                            while (!SYS_server_exit && !input_ok)
712                          {                          {
713                            if (fgets (buffer, 255, fin) == NULL)                                  ch = igetch_t(BBS_max_user_idle_time);
714                              goto exit;                                  input_ok = 1;
715    
716                                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
717                                    {
718                                            BBS_last_access_tm = time(NULL);
719    
720                                            // Refresh current action
721                                            if (user_online_update(NULL) < 0)
722                                            {
723                                                    log_error("user_online_update(NULL) error");
724                                            }
725                                    }
726    
727                                    // extended key handler
728                                    if (key_handler(&ch, &ctx) != 0)
729                                    {
730                                            goto cleanup;
731                                    }
732    
733                                    switch (ch)
734                                    {
735                                    case KEY_NULL:
736                                            log_debug("KEY_NULL");
737                                            goto cleanup;
738                                    case KEY_TIMEOUT:
739                                            log_debug("User input timeout");
740                                            goto cleanup;
741                                    case KEY_HOME:
742                                            if (line_current - output_current_row < 0) // Reach begin
743                                            {
744                                                    break;
745                                            }
746                                            line_current = 0;
747                                            output_current_row = screen_begin_row;
748                                            output_end_row = SCREEN_ROWS - 1;
749                                            clrline(output_current_row, SCREEN_ROWS);
750                                            break;
751                                    case KEY_END:
752                                            if (display_line_total < screen_row_total)
753                                            {
754                                                    break;
755                                            }
756                                            line_current = display_line_total - screen_row_total;
757                                            output_current_row = screen_begin_row;
758                                            output_end_row = SCREEN_ROWS - 1;
759                                            clrline(output_current_row, SCREEN_ROWS);
760                                            break;
761                                    case KEY_UP:
762                                            if (line_current - output_current_row < 0) // Reach begin
763                                            {
764                                                    break;
765                                            }
766                                            line_current -= output_current_row;
767                                            output_current_row = screen_begin_row;
768                                            // screen_end_line = screen_begin_line;
769                                            // prints("\033[T"); // Scroll down 1 line
770                                            output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
771                                            break;
772                                    case CR:
773                                    case KEY_DOWN:
774                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end
775                                            {
776                                                    break;
777                                            }
778                                            line_current += (screen_row_total - (output_current_row - screen_begin_row));
779                                            output_current_row = screen_row_total;
780                                            output_end_row = SCREEN_ROWS - 1;
781                                            moveto(SCREEN_ROWS, 0);
782                                            clrtoeol();
783                                            // prints("\033[S"); // Scroll up 1 line
784                                            prints("\n"); // Legacy Cterm only works with this line
785                                            break;
786                                    case KEY_PGUP:
787                                            if (line_current - output_current_row < 0) // Reach begin
788                                            {
789                                                    break;
790                                            }
791                                            line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
792                                            if (line_current < 0)
793                                            {
794                                                    line_current = 0;
795                                            }
796                                            output_current_row = screen_begin_row;
797                                            output_end_row = SCREEN_ROWS - 1;
798                                            clrline(output_current_row, SCREEN_ROWS);
799                                            break;
800                                    case KEY_SPACE:
801                                    case KEY_PGDN:
802                                            if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end
803                                            {
804                                                    break;
805                                            }
806                                            line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
807                                            if (line_current + screen_row_total > display_line_total) // No enough lines to display
808                                            {
809                                                    line_current = display_line_total - screen_row_total;
810                                            }
811                                            output_current_row = screen_begin_row;
812                                            output_end_row = SCREEN_ROWS - 1;
813                                            clrline(output_current_row, SCREEN_ROWS);
814                                            break;
815                                    case KEY_ESC:
816                                    case KEY_LEFT:
817                                            loop = 0;
818                                            break;
819                                    case 'h':
820                                            if (!show_help) // Not reentrant
821                                            {
822                                                    break;
823                                            }
824                                            // Display help information
825                                            show_help = 0;
826                                            display_file(help_filename, 1);
827                                            show_help = 1;
828                                    case KEY_F5:
829                                            // Refresh after display help information
830                                            line_current -= (output_current_row - screen_begin_row);
831                                            output_current_row = screen_begin_row;
832                                            output_end_row = SCREEN_ROWS - 1;
833                                            clrline(output_current_row, SCREEN_ROWS);
834                                            break;
835                                    case 0: // Refresh bottom line
836                                            break;
837                                    default:
838                                            input_ok = 0;
839                                            break;
840                                    }
841                          }                          }
                       break;  
                     default:  
                       input_ok = 0;  
                       break;  
                     }  
                 }  
842    
843                clrline (begin_line, screen_lines);                          continue;
844                line = begin_line;                  }
               moveto (line, 0);  
845    
846                continue;                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
847              }                  if (len >= sizeof(buffer))
848                    {
849                            log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld ",
850                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
851                            len = sizeof(buffer) - 1;
852                    }
853                    else if (len < 0)
854                    {
855                            log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld ",
856                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
857                            len = 0;
858                    }
859    
860            i = strlen (buffer);                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
861            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  buffer[len] = '\0';
             {  
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
862    
863            line++;                  moveto(output_current_row, 0);
864                    clrtoeol();
865                    prints("%s", buffer);
866                    line_current++;
867                    output_current_row++;
868          }          }
       if (wait)  
         ch = press_any_key ();  
       else  
         ch = 0;  
869    
870      exit:  cleanup:
871        fclose (fin);          return ch;
872    }
873    
874        return ch;  int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
875      }  {
876            switch (*p_key)
877            {
878            case 0: // Set msg
879                    snprintf(p_ctx->msg, sizeof(p_ctx->msg),
880                                     "| 杩斿洖[\033[32m鈫怽033[33m,\033[32mESC\033[33m] | "
881                                     "绉诲姩[\033[32m鈫慭033[33m/\033[32m鈫揬033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
882                                     "甯姪[\033[32mh\033[33m] |");
883                    break;
884            }
885    
886    return -1;          return 0;
887  }  }
888    
889  int  int display_file(const char *filename, int eof_exit)
 show_top (char *status)  
890  {  {
891    char buffer[256];          int ret;
892            void *p_shm;
893            size_t data_len;
894            long line_total;
895            const void *p_data;
896            const long *p_line_offsets;
897    
898            if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
899            {
900                    log_error("get_file_shm(%s) error", filename);
901                    return KEY_NULL;
902            }
903    
904    str_space (buffer, 20 - strlen (BBS_current_section_name));          if (user_online_update("VIEW_FILE") < 0)
905            {
906                    log_error("user_online_update(VIEW_FILE) error");
907            }
908    
909    moveto (1, 0);          ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);
   prints ("\033[1;44;33m%-20s \033[37m%20s"  
           "         %s\033[33m讨论区 [%s]\033[m",  
           status, BBS_name, buffer, BBS_current_section_name);  
   iflush ();  
910    
911    return 0;          if (detach_file_shm(p_shm) < 0)
912            {
913                    log_error("detach_file_shm(%s) error", filename);
914            }
915    
916            return ret;
917  }  }
918    
919  int  int show_top(const char *str_left, const char *str_middle, const char *str_right)
 show_bottom (char *msg)  
920  {  {
921    char str_time[256], str_time_onine[20], buffer[256];          char str_left_f[STR_TOP_LEFT_MAX_LEN + 1];
922    time_t time_online;          char str_middle_f[STR_TOP_MIDDLE_MAX_LEN + 1];
923    struct tm *tm_online;          char str_right_f[STR_TOP_RIGHT_MAX_LEN + 1];
924            int str_left_len;
925            int str_middle_len;
926            int str_right_len;
927            int eol;
928            int len;
929    
930            strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
931            str_left_f[sizeof(str_left_f) - 1] = '\0';
932            len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN / 2, &eol, &str_left_len, 1);
933            str_left_f[len] = '\0';
934    
935            strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
936            str_middle_f[sizeof(str_middle_f) - 1] = '\0';
937            len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN / 2, &eol, &str_middle_len, 1);
938            str_middle_f[len] = '\0';
939    
940            strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
941            str_right_f[sizeof(str_right_f) - 1] = '\0';
942            len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN / 2, &eol, &str_right_len, 1);
943            str_right_f[len] = '\0';
944    
945            moveto(1, 0);
946            clrtoeol();
947            prints("\033[1;44;33m%s\033[37m%*s%s\033[33m%*s%s\033[m",
948                       str_left_f, 44 - str_left_len - str_middle_len, "",
949                       str_middle_f, 36 - str_right_len, "", str_right_f);
950    
951    get_time_str (str_time, 256);          return 0;
952    str_space (buffer, 33 - strlen (BBS_username));  }
953    
954    time_online = time (0) - BBS_login_tm;  int show_bottom(const char *msg)
955    tm_online = gmtime (&time_online);  {
956            char str_time[LINE_BUFFER_LEN];
957            int len_str_time;
958            time_t time_online;
959            struct tm *tm_online;
960            char msg_f[LINE_BUFFER_LEN];
961            int eol;
962            int len_msg;
963            int len;
964            int len_username;
965            char str_tm_online[LINE_BUFFER_LEN];
966            int len_str_tm_online;
967    
968            len_str_time = (int)get_time_str(str_time, sizeof(str_time));
969    
970            msg_f[0] = '\0';
971            len_msg = 0;
972            if (msg != NULL)
973            {
974                    strncpy(msg_f, msg, sizeof(msg_f) - 1);
975                    msg_f[sizeof(msg_f) - 1] = '\0';
976                    len = split_line(msg_f, 23, &eol, &len_msg, 1);
977                    msg_f[len] = '\0';
978            }
979    
980    moveto (screen_lines, 0);          len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
   prints ("\033[1;44;33m[\033[36m%s\033[33m]"  
           "%s帐号[\033[36m%s\033[33m]"  
           "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",  
           str_time, buffer, BBS_username, tm_online->tm_mday - 1,  
           tm_online->tm_hour, tm_online->tm_min);  
   iflush ();  
981    
982    return 0;          time_online = time(NULL) - BBS_login_tm;
983            tm_online = gmtime(&time_online);
984            if (tm_online->tm_mday > 1)
985            {
986                    snprintf(str_tm_online, sizeof(str_tm_online),
987                                     "\033[36m%d\033[33md \033[36m%d\033[33m:\033[36m%.2d",
988                                     tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
989            }
990            else
991            {
992                    snprintf(str_tm_online, sizeof(str_tm_online),
993                                     "\033[36m%d\033[33m:\033[36m%.2d",
994                                     tm_online->tm_hour, tm_online->tm_min);
995            }
996            len_str_tm_online = str_length(str_tm_online, 1);
997    
998            moveto(SCREEN_ROWS, 0);
999            clrtoeol();
1000            prints("\033[1;44;33m鏃堕棿[\033[36m%s\033[33m]%s%*s \033[33m鐢ㄦ埛[\033[36m%s\033[33m][%s\033[33m]\033[m",
1001                       str_time, msg_f, 65 - len_str_time - len_msg - len_username - len_str_tm_online,
1002                       "", BBS_username, str_tm_online);
1003    
1004            return 0;
1005  }  }
1006    
1007  int  int show_active_board()
 show_active_board ()  
1008  {  {
1009    char filename[256], buffer[260];          static int line_current = 0;
1010    FILE *fin;          static const void *p_shm = NULL;
1011    int i, j;          static size_t data_len;
1012    static long int line;          static long line_total;
1013            static const void *p_data;
1014            static const long *p_line_offsets;
1015    
1016    sprintf (filename, "%sdata/active_board.txt", app_home_dir);          static time_t t_last_show = 0;
1017            static int line_last = 0;
1018    
1019    clrline (3, 2 + ACTIVE_BOARD_HEIGHT);          char buffer[LINE_BUFFER_LEN];
1020            long int len;
1021    
1022    moveto (3, 0);          if (p_shm == NULL)
1023            {
1024                    if ((p_shm = get_file_shm_readonly(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
1025                    {
1026                            log_error("get_file_shm(%s) error", DATA_ACTIVE_BOARD);
1027                            return KEY_NULL;
1028                    }
1029            }
1030    
1031    if ((fin = fopen (filename, "r")) != NULL)          if (time(NULL) - t_last_show >= 10)
     {  
       for (j = 0; j < line; j++)  
1032          {          {
1033            if (fgets (buffer, 255, fin) == NULL)                  line_last = line_current;
1034              {                  t_last_show = time(NULL);
               line = 0;  
               rewind (fin);  
               break;  
             }  
1035          }          }
1036            else
1037            {
1038                    line_current = line_last;
1039            }
1040    
1041            clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
1042    
1043        for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
1044          {          {
1045            if (fgets (buffer, 255, fin) == NULL)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
1046              {                  if (len >= LINE_BUFFER_LEN)
               line = 0;  
               if (j == 0)  
1047                  {                  {
1048                    rewind (fin);                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld ",
1049                    if (fgets (buffer, 255, fin) == NULL)                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
1050                      {                          len = LINE_BUFFER_LEN - 1;
                       break;  
                     }  
1051                  }                  }
1052                else  
1053                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
1054                    buffer[len] = '\0';
1055    
1056                    moveto(3 + i, 0);
1057                    prints("%s", buffer);
1058    
1059                    line_current++;
1060                    if (line_current >= line_total)
1061                  {                  {
1062                    break;                          line_current = 0;
1063                            break;
1064                  }                  }
             }  
           line++;  
           i = strlen (buffer);  
           if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
             {  
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
1065          }          }
       fclose (fin);  
     }  
1066    
1067    return 0;          return 0;
1068  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1