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


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

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