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


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

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