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


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

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