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


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

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