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


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

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