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


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

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