/[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.44 by sysadm, Sat May 10 15:23:42 2025 UTC Revision 1.130 by sysadm, Wed Nov 19 03:12:58 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    
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 <unistd.h>  #include <errno.h>
24    #include <fcntl.h>
25    #include <string.h>
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <sys/types.h>  #include <unistd.h>
28  #include <sys/stat.h>  #include <wchar.h>
29  #include <sys/param.h>  #include <sys/param.h>
30    #include <sys/stat.h>
31    #include <sys/shm.h>
32    #include <sys/types.h>
33    
34    const char CTRL_SEQ_CLR_LINE[] = "\033[K";
35    
36    static const int ACTIVE_BOARD_HEIGHT = 8;
37    
38    static const int STR_TOP_LEFT_MAX_LEN = 80;
39    static const int STR_TOP_MIDDLE_MAX_LEN = 40;
40    static const int STR_TOP_RIGHT_MAX_LEN = 80;
41    
42    static size_t get_time_str(char *s, size_t len)
43    {
44            time_t curtime;
45            struct tm local_tm;
46    
47  #define ACTIVE_BOARD_HEIGHT 8          time(&curtime);
48            localtime_r(&curtime, &local_tm);
49            size_t j = strftime(s, len, "%m/%d %H:%M %Z", &local_tm);
50    
51  int screen_rows = 24;          return j;
52  int screen_cols = 80;  }
53    
54  void moveto(int row, int col)  void moveto(int row, int col)
55  {  {
# Line 47  void moveto(int row, int col) Line 65  void moveto(int row, int col)
65    
66  void clrtoeol()  void clrtoeol()
67  {  {
68          prints("\033[K");          prints(CTRL_SEQ_CLR_LINE);
69  }  }
70    
71  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 75  void clrline(int line_begin, int line_en
75          for (i = line_begin; i <= line_end; i++)          for (i = line_begin; i <= line_end; i++)
76          {          {
77                  moveto(i, 0);                  moveto(i, 0);
78                  prints("\033[K");                  prints(CTRL_SEQ_CLR_LINE);
79          }          }
80  }  }
81    
# Line 74  void clearscr() Line 92  void clearscr()
92          moveto(0, 0);          moveto(0, 0);
93  }  }
94    
95  int press_any_key()  inline int press_any_key()
96    {
97            return press_any_key_ex("                           \033[1;33m按任意键继续...\033[m", 60);
98    }
99    
100    int press_any_key_ex(const char *msg, int sec)
101  {  {
102          moveto(screen_rows, 0);          int ch = 0;
103            int duration = 0;
104            time_t t_begin = time(NULL);
105    
106            moveto(SCREEN_ROWS, 0);
107          clrtoeol();          clrtoeol();
108    
109          prints("                           \033[1;33m...\033[0;37m");          prints(msg);
110          iflush();          iflush();
111    
112          return igetch_t(MIN(MAX_DELAY_TIME, 60));          igetch_reset();
113    
114            do
115            {
116                    ch = igetch_t(sec - duration);
117                    duration = (int)(time(NULL) - t_begin);
118            } while (!SYS_server_exit && ch == 0 && duration < 60);
119    
120            return ch;
121  }  }
122    
123  void set_input_echo(int echo)  void set_input_echo(int echo)
# Line 90  void set_input_echo(int echo) Line 125  void set_input_echo(int echo)
125          if (echo)          if (echo)
126          {          {
127                  outc('\x83'); // ASCII code 131                  outc('\x83'); // ASCII code 131
                 iflush();  
128          }          }
129          else          else
130          {          {
131                  //    outc ('\x85'); // ASCII code 133                  // outc ('\x85'); // ASCII code 133
132                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
                 iflush();  
                 igetch(0);  
                 igetch(1);  
133          }          }
134            iflush();
135  }  }
136    
137  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)
138  {  {
139          int c;          int ch;
140          int offset = 0;          int offset = 0;
141          int hz = 0;          int eol;
142            int display_len;
143            char input_str[5];
144            int str_len = 0;
145            wchar_t wcs[2];
146            char c;
147    
148          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
149          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)          offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
                 ;  
150    
151          while (!SYS_server_exit && (c = igetch_t(MIN(MAX_DELAY_TIME, 60))))          igetch_reset();
152    
153            while (!SYS_server_exit)
154          {          {
155                  if (c == CR)                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
156    
157                    if (ch == CR)
158                  {                  {
                         igetch(1); // Cleanup remaining '\n' in the buffer  
159                          break;                          break;
160                  }                  }
161                  else if (c == KEY_TIMEOUT)                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
162                  {                  {
163                          return -1;                          return -1;
164                  }                  }
165                  else if (c == KEY_NULL || c == LF)                  else if (ch == LF || ch == '\0')
166                  {                  {
167                          continue;                          continue;
168                  }                  }
169                  else if (c == BACKSPACE)                  else if (ch == BACKSPACE)
170                  {                  {
171                          if (offset > 0)                          if (offset > 0)
172                          {                          {
173                                  offset--;                                  offset--;
174                                  if (buffer[offset] < 0 || buffer[offset] > 127)                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
175                                  {                                  {
176                                          prints("\033[D \033[D");                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
                                         offset--;  
                                         if (offset < 0) // should not happen  
177                                          {                                          {
178                                                  log_error("Offset of buffer is negative\n");                                                  offset--;
                                                 offset = 0;  
179                                          }                                          }
180                                            display_len--;
181                                            prints("\033[D \033[D");
182                                  }                                  }
183                                  buffer[offset] = '\0';                                  buffer[offset] = '\0';
184                                    display_len--;
185                                  prints("\033[D \033[D");                                  prints("\033[D \033[D");
186                                  iflush();                                  iflush();
187                          }                          }
188                          continue;                          continue;
189                  }                  }
190                  else if (c > 255 || iscntrl(c))                  else if (ch > 255 || iscntrl(ch))
191                  {                  {
192                          continue;                          continue;
193                  }                  }
194                  else if (c > 127 && c <= 255)                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
195                  {                  {
196                          if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character                          str_len = 0;
197                            c = (char)(ch & 0xf0);
198                            while (c & 0x80)
199                            {
200                                    input_str[str_len] = (char)(ch - 256);
201                                    str_len++;
202                                    c = (c & 0x7f) << 1;
203    
204                                    if ((c & 0x80) == 0) // Input completed
205                                    {
206                                            break;
207                                    }
208    
209                                    // Expect additional bytes of input
210                                    ch = igetch(100);                                                // 0.1 second
211                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
212                                    {
213    #ifdef _DEBUG
214                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
215    #endif
216                                            str_len = 0;
217                                            break;
218                                    }
219                            }
220                            input_str[str_len] = '\0';
221    
222                            if (str_len == 0) // Incomplete input
223                            {
224                                    continue;
225                            }
226    
227                            if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
228                            {
229                                    log_error("mbstowcs() error\n");
230                            }
231                            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
232                          {                          {
                                 igetch(1); // Cleanup remaining input  
233                                  outc('\a');                                  outc('\a');
234                                  iflush();                                  iflush();
235                                  continue;                                  continue;
236                          }                          }
                         hz = (!hz);  
                 }  
237    
238                  if (offset + 1 > buf_size - 1)                          memcpy(buffer + offset, input_str, (size_t)str_len);
239                  {                          offset += str_len;
240                          outc('\a');                          buffer[offset] = '\0';
241                          iflush();                          display_len += 2;
242                          continue;  
243                            switch (echo_mode)
244                            {
245                            case DOECHO:
246                                    prints(input_str);
247                                    break;
248                            case NOECHO:
249                                    prints("**");
250                                    break;
251                            }
252                  }                  }
253                    else if (ch >= 32 && ch < 127) // Printable character
254                    {
255                            if (offset + 1 > buf_size - 1 || display_len + 1 > max_display_len)
256                            {
257                                    outc('\a');
258                                    iflush();
259                                    continue;
260                            }
261    
262                  buffer[offset++] = (char)c;                          buffer[offset++] = (char)ch;
263                  buffer[offset] = '\0';                          buffer[offset] = '\0';
264                            display_len++;
265    
266                  switch (echo_mode)                          switch (echo_mode)
267                  {                          {
268                  case DOECHO:                          case DOECHO:
269                          outc((char)c);                                  outc((char)ch);
270                          break;                                  break;
271                  case NOECHO:                          case NOECHO:
272                          outc('*');                                  outc('*');
273                          break;                                  break;
274                            }
275                  }                  }
276                  if (!hz)                  else // Invalid character
277                  {                  {
278                          iflush();                          continue;
279                  }                  }
280    
281                    iflush();
282          }          }
283    
284          return offset;          return offset;
285  }  }
286    
287  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)
288  {  {
289          int len;          int len;
290    
291          buffer[0] = '\0';          buffer[0] = '\0';
292    
293          len = _str_input(buffer, buf_size, echo_mode);          len = _str_input(buffer, buf_size, buf_size, echo_mode);
294    
295          prints("\r\n");          prints("\r\n");
296          iflush();          iflush();
297    
298          return len;          return len;
299  };  }
300    
301  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)
302  {  {
303          int len;          int len = 0;
304            int col_cur = 0;
305            int ch;
306            int offset = 0;
307            int eol;
308            int display_len;
309            char input_str[5];
310            int str_len = 0;
311            wchar_t wcs[2];
312            int wc_len;
313            char c;
314    
315          igetch(1); // Cleanup input buffer          buffer[buf_size - 1] = '\0';
316            offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
317            buffer[offset] = '\0';
318            len = offset;
319            col_cur = col + str_length(prompt, 1) + display_len;
320    
321          moveto(row, col);          moveto(row, col);
322          prints(prompt);          prints("%s", prompt);
323          prints(buffer);          prints("%s", buffer);
324            prints("%*s", max_display_len - display_len, "");
325            moveto(row, col_cur);
326          iflush();          iflush();
327    
328          len = _str_input(buffer, buf_size, echo_mode);          igetch_reset();
329    
330          return len;          while (!SYS_server_exit)
331  }          {
332                    ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
333    
334  int display_file(const char *filename)                  if (ch == CR)
335  {                  {
336          char buffer[LINE_BUFFER_LEN];                          break;
337          FILE *fin;                  }
338          size_t i;                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
339                    {
340                            return -1;
341                    }
342                    else if (ch == LF || ch == '\0')
343                    {
344                            continue;
345                    }
346                    else if (ch == BACKSPACE)
347                    {
348                            if (offset > 0)
349                            {
350                                    str_len = 1;
351                                    offset--;
352                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
353                                    {
354                                            while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
355                                            {
356                                                    str_len++;
357                                                    offset--;
358                                            }
359    
360          if ((fin = fopen(filename, "r")) == NULL)                                          if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
361          {                                          {
362                  return -1;                                                  log_error("mbstowcs() error\n");
363          }                                          }
364                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
365    
366          while (fgets(buffer, sizeof(buffer) - 1, fin))                                          if (wc_len == 2)
367          {                                          {
368                  i = strnlen(buffer, sizeof(buffer) - 1);                                                  display_len--;
369                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                                                  col_cur--;
370                                            }
371                                    }
372    
373                                    memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
374                                    len -= str_len;
375                                    buffer[len] = '\0';
376                                    display_len--;
377                                    col_cur--;
378    
379                                    moveto(row, col_cur);
380                                    prints("%s", buffer + offset);
381                                    prints("%*s", max_display_len - display_len, "");
382                                    moveto(row, col_cur);
383                                    iflush();
384                            }
385                            continue;
386                    }
387                    else if (ch == KEY_DEL)
388                  {                  {
389                          buffer[i - 1] = '\r';                          if (offset < len)
390                          buffer[i] = '\n';                          {
391                          buffer[i + 1] = '\0';                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
392                                    {
393                                            str_len = 0;
394                                            c = (char)(buffer[offset] & 0xf0);
395                                            while (c & 0x80)
396                                            {
397                                                    str_len++;
398                                                    c = (c & 0x7f) << 1;
399                                            }
400                                            display_len--;
401                                    }
402                                    else
403                                    {
404                                            str_len = 1;
405                                    }
406    
407                                    memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
408                                    len -= str_len;
409                                    buffer[len] = '\0';
410                                    display_len--;
411    
412                                    moveto(row, col_cur);
413                                    prints("%s", buffer + offset);
414                                    prints("%*s", max_display_len - display_len, "");
415                                    moveto(row, col_cur);
416                                    iflush();
417                            }
418                            continue;
419                    }
420                    else if (ch == KEY_LEFT)
421                    {
422                            if (offset > 0)
423                            {
424                                    str_len = 1;
425                                    offset--;
426                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
427                                    {
428                                            while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
429                                            {
430                                                    str_len++;
431                                                    offset--;
432                                            }
433    
434                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
435                                            {
436                                                    log_error("mbstowcs() error\n");
437                                            }
438                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
439    
440                                            if (wc_len == 2)
441                                            {
442                                                    col_cur--;
443                                            }
444                                    }
445                                    col_cur--;
446    
447                                    moveto(row, col_cur);
448                                    iflush();
449                            }
450                            continue;
451                    }
452                    else if (ch == KEY_RIGHT)
453                    {
454                            if (offset < len)
455                            {
456                                    str_len = 0;
457                                    if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
458                                    {
459                                            c = (char)(buffer[offset] & 0xf0);
460                                            while (c & 0x80)
461                                            {
462                                                    str_len++;
463                                                    c = (c & 0x7f) << 1;
464                                            }
465    
466                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
467                                            {
468                                                    log_error("mbstowcs() error\n");
469                                            }
470                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
471    
472                                            if (wc_len == 2)
473                                            {
474                                                    col_cur++;
475                                            }
476                                    }
477                                    else
478                                    {
479                                            str_len = 1;
480                                    }
481    
482                                    col_cur++;
483                                    offset += str_len;
484    
485                                    moveto(row, col_cur);
486                                    iflush();
487                            }
488                            continue;
489                    }
490                    else if (ch == KEY_HOME || ch == KEY_UP)
491                    {
492                            if (offset > 0)
493                            {
494                                    offset = 0;
495                                    col_cur = col + str_length(prompt, 1);
496    
497                                    moveto(row, col_cur);
498                                    iflush();
499                            }
500                            continue;
501                    }
502                    else if (ch == KEY_END || ch == KEY_DOWN)
503                    {
504                            if (offset < len)
505                            {
506                                    offset = len;
507                                    col_cur = col + str_length(prompt, 1) + display_len;
508    
509                                    moveto(row, col_cur);
510                                    iflush();
511                            }
512                            continue;
513                    }
514                    else if (ch > 255 || iscntrl(ch))
515                    {
516                            continue;
517                    }
518                    else if ((ch & 0xff80) == 0x80) // head of multi-byte character
519                    {
520                            str_len = 0;
521                            c = (char)(ch & 0xf0);
522                            while (c & 0x80)
523                            {
524                                    input_str[str_len] = (char)(ch - 256);
525                                    str_len++;
526                                    c = (c & 0x7f) << 1;
527    
528                                    if ((c & 0x80) == 0) // Input completed
529                                    {
530                                            break;
531                                    }
532    
533                                    // Expect additional bytes of input
534                                    ch = igetch(100);                                                // 0.1 second
535                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
536                                    {
537    #ifdef _DEBUG
538                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
539    #endif
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\n");
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;          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;          const int screen_begin_row = 1;
626          long int c_line_total = 0;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
627          FILE *fin;          int output_current_row = screen_begin_row;
628          long *p_line_offsets;          int output_end_row = SCREEN_ROWS - 1;
629            long int line_current = 0;
630          long int len;          long int len;
631          long int percentile;          long int percentile;
632          int loop = 1;          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;
   
         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;  
   
644          while (!SYS_server_exit && loop)          while (!SYS_server_exit && 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 295  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                            ctx.line_top = line_current - (output_current_row - screen_begin_row) + 1;
677                            ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);
678    
679                            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);                          moveto(SCREEN_ROWS, 0);
695                          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 ? " " : "")));  
696                          iflush();                          iflush();
697    
698                          input_ok = 0;                          input_ok = 0;
699                          while (!SYS_server_exit && !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    
709                                    // extended key handler
710                                    if (key_handler(&ch, &ctx) != 0)
711                                    {
712                                            goto cleanup;
713                                    }
714    
715                                  switch (ch)                                  switch (ch)
716                                  {                                  {
717                                    case KEY_NULL:
718                                            log_error("KEY_NULL\n");
719                                            goto cleanup;
720                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
721                                            log_error("User input timeout\n");
722                                          goto cleanup;                                          goto cleanup;
723                                    case KEY_HOME:
724                                            if (line_current - output_current_row < 0) // Reach begin
725                                            {
726                                                    break;
727                                            }
728                                            line_current = 0;
729                                            output_current_row = screen_begin_row;
730                                            output_end_row = SCREEN_ROWS - 1;
731                                            clrline(output_current_row, SCREEN_ROWS);
732                                            break;
733                                    case KEY_END:
734                                            if (display_line_total < screen_row_total)
735                                            {
736                                                    break;
737                                            }
738                                            line_current = display_line_total - screen_row_total;
739                                            output_current_row = screen_begin_row;
740                                            output_end_row = SCREEN_ROWS - 1;
741                                            clrline(output_current_row, SCREEN_ROWS);
742                                            break;
743                                  case KEY_UP:                                  case KEY_UP:
744                                          if (c_line_current - line < 0) // Reach top                                          if (line_current - output_current_row < 0) // Reach begin
745                                          {                                          {
746                                                  break;                                                  break;
747                                          }                                          }
748                                          c_line_current -= line;                                          line_current -= output_current_row;
749                                          line = begin_line;                                          output_current_row = screen_begin_row;
750                                          // max_lines = begin_line + 1;                                          // screen_end_line = screen_begin_line;
751                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
752                                          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
753                                          break;                                          break;
                                 case KEY_DOWN:  
754                                  case CR:                                  case CR:
755                                          if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom                                  case KEY_DOWN:
756                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end
757                                          {                                          {
758                                                  break;                                                  break;
759                                          }                                          }
760                                          c_line_current += ((screen_rows - 2) - (line - 1));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
761                                          line = screen_rows - 2;                                          output_current_row = screen_row_total;
762                                          max_lines = screen_rows - 1;                                          output_end_row = SCREEN_ROWS - 1;
763                                          moveto(screen_rows, 0);                                          moveto(SCREEN_ROWS, 0);
764                                          clrtoeol();                                          clrtoeol();
765                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
766                                            prints("\n"); // Legacy Cterm only works with this line
767                                          break;                                          break;
768                                  case KEY_PGUP:                                  case KEY_PGUP:
769                                  case Ctrl('B'):                                          if (line_current - output_current_row < 0) // Reach begin
                                         if (c_line_current - line < 0) // Reach top  
770                                          {                                          {
771                                                  break;                                                  break;
772                                          }                                          }
773                                          c_line_current -= ((screen_rows - 3) + (line - 1));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
774                                          if (c_line_current < 0)                                          if (line_current < 0)
775                                          {                                          {
776                                                  c_line_current = 0;                                                  line_current = 0;
777                                          }                                          }
778                                          line = begin_line;                                          output_current_row = screen_begin_row;
779                                          max_lines = screen_rows - 1;                                          output_end_row = SCREEN_ROWS - 1;
780                                          clrline(begin_line, screen_rows);                                          clrline(output_current_row, SCREEN_ROWS);
781                                          break;                                          break;
                                 case KEY_RIGHT:  
                                 case KEY_PGDN:  
                                 case Ctrl('F'):  
782                                  case KEY_SPACE:                                  case KEY_SPACE:
783                                          if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom                                  case KEY_PGDN:
784                                            if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end
785                                          {                                          {
786                                                  break;                                                  break;
787                                          }                                          }
788                                          c_line_current += (screen_rows - 3) - (line - 1);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
789                                          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
790                                          {                                          {
791                                                  c_line_current = c_line_total - (screen_rows - 2);                                                  line_current = display_line_total - screen_row_total;
792                                          }                                          }
793                                          line = begin_line;                                          output_current_row = screen_begin_row;
794                                          max_lines = screen_rows - 1;                                          output_end_row = SCREEN_ROWS - 1;
795                                          clrline(begin_line, screen_rows);                                          clrline(output_current_row, SCREEN_ROWS);
796                                          break;                                          break;
797                                    case KEY_ESC:
798                                  case KEY_LEFT:                                  case KEY_LEFT:
                                 case 'q':  
                                 case 'Q':  
799                                          loop = 0;                                          loop = 0;
800                                          break;                                          break;
                                 case '?':  
801                                  case 'h':                                  case 'h':
802                                  case 'H':                                          if (!show_help) // Not reentrant
                                         if (!show_help)  
803                                          {                                          {
804                                                  break;                                                  break;
805                                          }                                          }
   
806                                          // Display help information                                          // Display help information
807                                          show_help = 0;                                          show_help = 0;
808                                          display_file_ex(DATA_READ_HELP, begin_line, 1);                                          display_file(help_filename, 1);
809                                          show_help = 1;                                          show_help = 1;
810                                    case KEY_F5:
811                                          // Refresh after display help information                                          // Refresh after display help information
812                                          c_line_current -= (line - 1);                                          line_current -= (output_current_row - screen_begin_row);
813                                          line = begin_line;                                          output_current_row = screen_begin_row;
814                                          max_lines = screen_rows - 1;                                          output_end_row = SCREEN_ROWS - 1;
815                                          clrline(begin_line, screen_rows);                                          clrline(output_current_row, SCREEN_ROWS);
816                                            break;
817                                    case 0: // Refresh bottom line
818                                          break;                                          break;
819                                  default:                                  default:
820                                          input_ok = 0;                                          input_ok = 0;
821                                          break;                                          break;
822                                  }                                  }
   
                                 BBS_last_access_tm = time(0);  
823                          }                          }
824    
825                          continue;                          continue;
826                  }                  }
827    
828                  fseek(fin, p_line_offsets[c_line_current], SEEK_SET);                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
829                  len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];                  if (len >= sizeof(buffer))
                 if (len >= LINE_BUFFER_LEN)  
830                  {                  {
831                          log_error("Error length exceeds buffer size: %d\n", len);                          log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
832                          len = LINE_BUFFER_LEN - 1;                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
833                            len = sizeof(buffer) - 1;
834                  }                  }
835                  if (fgets(buffer, (int)len + 1, fin) == NULL)                  else if (len < 0)
836                  {                  {
837                          log_error("Reach EOF\n");                          log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld \n",
838                          break;                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
839                            len = 0;
840                  }                  }
841                  moveto(line, 0);  
842                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
843                    buffer[len] = '\0';
844    
845                    moveto(output_current_row, 0);
846                  clrtoeol();                  clrtoeol();
847                  prints("%s", buffer);                  prints("%s", buffer);
848                  c_line_current++;                  line_current++;
849                  line++;                  output_current_row++;
850          }          }
851    
852  cleanup:  cleanup:
         free(p_line_offsets);  
         fclose(fin);  
   
853          return ch;          return ch;
854  }  }
855    
856  int show_top(char *status)  int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
857  {  {
858          int end_of_line;          switch (*p_key)
859          int display_len;          {
860          unsigned int len;          case 0: // Set msg
861                    snprintf(p_ctx->msg, sizeof(p_ctx->msg),
862                                     "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
863                                     "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
864                                     "帮助[\033[32mh\033[33m] |");
865                    break;
866            }
867    
868            return 0;
869    }
870    
871    int display_file(const char *filename, int eof_exit)
872    {
873            int ret;
874            void *p_shm;
875            size_t data_len;
876            long line_total;
877            const void *p_data;
878            const long *p_line_offsets;
879    
880          char space1[LINE_BUFFER_LEN];          if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
881          char space2[LINE_BUFFER_LEN];          {
882                    log_error("get_file_shm(%s) error\n", filename);
883                    return KEY_NULL;
884            }
885    
886          len = split_line(status, 20, &end_of_line, &display_len);          if (user_online_update("VIEW_FILE") < 0)
         if (end_of_line)  
887          {          {
888                  status[len] = '\0';                  log_error("user_online_update(VIEW_FILE) error\n");
889          }          }
         str_space(space1, 31 - display_len);  
890    
891          len = split_line(BBS_current_section_name, 20, &end_of_line, &display_len);          ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);
892          if (end_of_line)  
893            if (detach_file_shm(p_shm) < 0)
894          {          {
895                  status[len] = '\0';                  log_error("detach_file_shm(%s) error\n", filename);
896          }          }
897          str_space(space2, 30 - display_len);  
898            return ret;
899    }
900    
901    int show_top(const char *str_left, const char *str_middle, const char *str_right)
902    {
903            char str_left_f[STR_TOP_LEFT_MAX_LEN + 1];
904            char str_middle_f[STR_TOP_MIDDLE_MAX_LEN + 1];
905            char str_right_f[STR_TOP_RIGHT_MAX_LEN + 1];
906            int str_left_len;
907            int str_middle_len;
908            int str_right_len;
909            int eol;
910            int len;
911    
912            strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
913            str_left_f[sizeof(str_left_f) - 1] = '\0';
914            len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN / 2, &eol, &str_left_len, 1);
915            str_left_f[len] = '\0';
916    
917            strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
918            str_middle_f[sizeof(str_middle_f) - 1] = '\0';
919            len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN / 2, &eol, &str_middle_len, 1);
920            str_middle_f[len] = '\0';
921    
922            strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
923            str_right_f[sizeof(str_right_f) - 1] = '\0';
924            len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN / 2, &eol, &str_right_len, 1);
925            str_right_f[len] = '\0';
926    
927          moveto(1, 0);          moveto(1, 0);
928          clrtoeol();          clrtoeol();
929          prints("\033[1;44;33m%s \033[37m%s%s%s\033[33m [%s]\033[m",          prints("\033[1;44;33m%s\033[37m%*s%s\033[33m%*s%s\033[m",
930                     status, space1, BBS_name, space2, BBS_current_section_name);                     str_left_f, 44 - str_left_len - str_middle_len, "",
931          iflush();                     str_middle_f, 36 - str_right_len, "", str_right_f);
932    
933          return 0;          return 0;
934  }  }
935    
936  int show_bottom(char *msg)  int show_bottom(const char *msg)
937  {  {
938          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
939          char space[LINE_BUFFER_LEN];          int len_str_time;
940          time_t time_online;          time_t time_online;
941          struct tm *tm_online;          struct tm *tm_online;
942            char msg_f[LINE_BUFFER_LEN];
943            int eol;
944            int len_msg;
945            int len;
946            int len_username;
947            char str_tm_online[LINE_BUFFER_LEN];
948            int len_str_tm_online;
949    
950            len_str_time = (int)get_time_str(str_time, sizeof(str_time));
951    
952            msg_f[0] = '\0';
953            len_msg = 0;
954            if (msg != NULL)
955            {
956                    strncpy(msg_f, msg, sizeof(msg_f) - 1);
957                    msg_f[sizeof(msg_f) - 1] = '\0';
958                    len = split_line(msg_f, 23, &eol, &len_msg, 1);
959                    msg_f[len] = '\0';
960            }
961    
962          get_time_str(str_time, sizeof(str_time));          len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
         str_space(space, 34 - (int)strnlen(BBS_username, sizeof(BBS_username)));  
963    
964          time_online = time(0) - BBS_login_tm;          time_online = time(NULL) - BBS_login_tm;
965          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
966            if (tm_online->tm_mday > 1)
967            {
968                    snprintf(str_tm_online, sizeof(str_tm_online),
969                                     "\033[36m%d\033[33md \033[36m%d\033[33m:\033[36m%.2d",
970                                     tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
971            }
972            else
973            {
974                    snprintf(str_tm_online, sizeof(str_tm_online),
975                                     "\033[36m%d\033[33m:\033[36m%.2d",
976                                     tm_online->tm_hour, tm_online->tm_min);
977            }
978            len_str_tm_online = str_length(str_tm_online, 1);
979    
980          moveto(screen_rows, 0);          moveto(SCREEN_ROWS, 0);
981          clrtoeol();          clrtoeol();
982          prints("\033[1;44;33m[\033[36m%s\033[33m]%sʺ[\033[36m%s\033[33m]"          prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m用户[\033[36m%s\033[33m][%s\033[33m]\033[m",
983                     "[\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,
984                     str_time, space, BBS_username, tm_online->tm_mday - 1,                     "", BBS_username, str_tm_online);
                    tm_online->tm_hour, tm_online->tm_min);  
         iflush();  
985    
986          return 0;          return 0;
987  }  }
988    
989  int show_active_board()  int show_active_board()
990  {  {
991          char filename[FILE_PATH_LEN];          static int line_current = 0;
992          char buffer[LINE_BUFFER_LEN];          static const void *p_shm = NULL;
993          FILE *fin;          static size_t data_len;
994          static int line;          static long line_total;
995          unsigned int len;          static const void *p_data;
996          int end_of_line;          static const long *p_line_offsets;
         int display_len;  
997    
998          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          static time_t t_last_show = 0;
999            static int line_last = 0;
1000    
1001          if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)          char buffer[LINE_BUFFER_LEN];
1002          {          long int len;
                 log_error("Unable to open file %s\n", filename);  
                 return -1;  
         }  
1003    
1004          for (int i = 0; i < line; i++)          if (p_shm == NULL)
1005          {          {
1006                  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)
1007                  {                  {
1008                          line = 0;                          log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
1009                          rewind(fin);                          return KEY_NULL;
                         break;  
1010                  }                  }
1011          }          }
1012    
1013            if (time(NULL) - t_last_show >= 10)
1014            {
1015                    line_last = line_current;
1016                    t_last_show = time(NULL);
1017            }
1018            else
1019            {
1020                    line_current = line_last;
1021            }
1022    
1023            clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
1024    
1025          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
1026          {          {
1027                  if (fgets(buffer, sizeof(buffer), fin) == NULL)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
1028                    if (len >= LINE_BUFFER_LEN)
1029                  {                  {
1030                          line = 0;                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
1031                          break;                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
1032                            len = LINE_BUFFER_LEN - 1;
1033                  }                  }
1034                  line++;  
1035                  len = split_line(buffer, screen_cols, &end_of_line, &display_len);                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
1036                  buffer[len] = '\0'; // Truncate over-length line                  buffer[len] = '\0';
1037    
1038                  moveto(3 + i, 0);                  moveto(3 + i, 0);
1039                  prints("%s", buffer);                  prints("%s", buffer);
         }  
         iflush();  
1040    
1041          fclose(fin);                  line_current++;
1042                    if (line_current >= line_total)
1043                    {
1044                            line_current = 0;
1045                            break;
1046                    }
1047            }
1048    
1049          return 0;          return 0;
1050  }  }


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

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