/[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.76 by sysadm, Thu May 29 02:21:31 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          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     */
 /***************************************************************************  
  *                                                                         *  
  *   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 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #include "screen.h"  
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
11  #include "str_process.h"  #include "editor.h"
 #include "log.h"  
 #include "io.h"  
12  #include "file_loader.h"  #include "file_loader.h"
13  #include <fcntl.h>  #include "io.h"
14    #include "log.h"
15    #include "login.h"
16    #include "screen.h"
17    #include "str_process.h"
18  #include <ctype.h>  #include <ctype.h>
 #include <unistd.h>  
 #include <stdlib.h>  
19  #include <errno.h>  #include <errno.h>
20  #include <sys/types.h>  #include <fcntl.h>
21  #include <sys/stat.h>  #include <string.h>
22    #include <stdlib.h>
23    #include <unistd.h>
24    #include <wchar.h>
25  #include <sys/param.h>  #include <sys/param.h>
26    #include <sys/stat.h>
27  #include <sys/shm.h>  #include <sys/shm.h>
28    #include <sys/types.h>
29    
30  #define _POSIX_C_SOURCE 200809L  const char CTRL_SEQ_CLR_LINE[] = "\033[K";
 #include <string.h>  
31    
32  #define ACTIVE_BOARD_HEIGHT 8  static const int ACTIVE_BOARD_HEIGHT = 8;
33    
34  #define STR_TOP_LEFT_MAX_LEN 40  static const int STR_TOP_LEFT_MAX_LEN = 80;
35  #define STR_TOP_MIDDLE_MAX_LEN 20  static const int STR_TOP_MIDDLE_MAX_LEN = 40;
36  #define STR_TOP_RIGHT_MAX_LEN 20  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            time(&curtime);
44            localtime_r(&curtime, &local_tm);
45            size_t j = strftime(s, len, "%m/%d %H:%M %Z", &local_tm);
46    
47            return j;
48    }
49    
50  void moveto(int row, int col)  void moveto(int row, int col)
51  {  {
# Line 81  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            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(MIN(MAX_DELAY_TIME, 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 97  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_reset();  
129          }          }
130            iflush();
131  }  }
132    
133  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)
134  {  {
135          int c;          int ch;
136          int offset = 0;          int offset = 0;
137          int hz = 0;          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          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
145          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)          offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
                 ;  
146    
147          igetch_reset();          igetch_reset();
148    
149          while (!SYS_server_exit)          while (!SYS_server_exit)
150          {          {
151                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
152    
153                  if (c == CR)                  if (ch == CR)
154                  {                  {
                         igetch_reset();  
155                          break;                          break;
156                  }                  }
157                  else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
158                  {                  {
159                          return -1;                          return -1;
160                  }                  }
161                  else if (c == LF || c == '\0')                  else if (ch == LF || ch == '\0')
162                  {                  {
163                          continue;                          continue;
164                  }                  }
165                  else if (c == BACKSPACE)                  else if (ch == BACKSPACE)
166                  {                  {
167                          if (offset > 0)                          if (offset > 0)
168                          {                          {
169                                  offset--;                                  offset--;
170                                  if (buffer[offset] < 0 || buffer[offset] > 127)                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
171                                  {                                  {
172                                          prints("\033[D \033[D");                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
                                         offset--;  
                                         if (offset < 0) // should not happen  
173                                          {                                          {
174                                                  log_error("Offset of buffer is negative\n");                                                  offset--;
                                                 offset = 0;  
175                                          }                                          }
176                                            display_len--;
177                                            prints("\033[D \033[D");
178                                  }                                  }
179                                  buffer[offset] = '\0';                                  buffer[offset] = '\0';
180                                    display_len--;
181                                  prints("\033[D \033[D");                                  prints("\033[D \033[D");
182                                  iflush();                                  iflush();
183                          }                          }
184                          continue;                          continue;
185                  }                  }
186                  else if (c > 255 || iscntrl(c))                  else if (ch > 255 || iscntrl(ch))
187                  {                  {
188                          continue;                          continue;
189                  }                  }
190                  else if (c > 127 && c <= 255)                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
191                  {                  {
192                          if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character                          str_len = 0;
193                            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                          {                          {
                                 igetch(0); // Ignore 1 character  
229                                  outc('\a');                                  outc('\a');
230                                  iflush();                                  iflush();
231                                  continue;                                  continue;
232                          }                          }
                         hz = (!hz);  
                 }  
233    
234                  if (offset + 1 > buf_size - 1)                          memcpy(buffer + offset, input_str, (size_t)str_len);
235                  {                          offset += str_len;
236                          outc('\a');                          buffer[offset] = '\0';
237                          iflush();                          display_len += 2;
238                          continue;  
239                            switch (echo_mode)
240                            {
241                            case DOECHO:
242                                    prints(input_str);
243                                    break;
244                            case NOECHO:
245                                    prints("**");
246                                    break;
247                            }
248                  }                  }
249                    else if (ch >= 32 && ch < 127) // Printable character
250                    {
251                            if (offset + 1 > buf_size - 1 || display_len + 1 > max_display_len)
252                            {
253                                    outc('\a');
254                                    iflush();
255                                    continue;
256                            }
257    
258                  buffer[offset++] = (char)c;                          buffer[offset++] = (char)ch;
259                  buffer[offset] = '\0';                          buffer[offset] = '\0';
260                            display_len++;
261    
262                  switch (echo_mode)                          switch (echo_mode)
263                  {                          {
264                  case DOECHO:                          case DOECHO:
265                          outc((char)c);                                  outc((char)ch);
266                          break;                                  break;
267                  case NOECHO:                          case NOECHO:
268                          outc('*');                                  outc('*');
269                          break;                                  break;
270                            }
271                  }                  }
272                  if (!hz)                  else // Invalid character
273                  {                  {
274                          iflush();                          continue;
275                  }                  }
276    
277                    iflush();
278          }          }
279    
280          return offset;          return offset;
281  }  }
282    
283  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)
284  {  {
285          int len;          int len;
286    
287          buffer[0] = '\0';          buffer[0] = '\0';
288    
289          len = _str_input(buffer, buf_size, echo_mode);          len = _str_input(buffer, buf_size, buf_size, echo_mode);
290    
291          prints("\r\n");          prints("\r\n");
292          iflush();          iflush();
293    
294          return len;          return len;
295  };  }
296    
297  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)
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, buf_size, echo_mode);          igetch_reset();
325    
326            while (!SYS_server_exit)
327            {
328                    ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
329    
330                    if (ch == CR)
331                    {
332                            break;
333                    }
334                    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 (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
357                                            {
358                                                    log_error("mbstowcs() error\n");
359                                            }
360                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
361    
362                                            if (wc_len == 2)
363                                            {
364                                                    display_len--;
365                                                    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                            if (offset < len)
386                            {
387                                    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                    }
608            }
609    
610          return len;          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;          static int show_help = 1;
617          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
618          int ch = KEY_NULL;          DISPLAY_CTX ctx;
619          int input_ok, line, max_lines;          int ch = 0;
620            int input_ok;
621            const int screen_begin_row = 1;
622            const int screen_row_total = SCREEN_ROWS - screen_begin_row;
623            int output_current_row = screen_begin_row;
624            int output_end_row = SCREEN_ROWS - 1;
625          long int line_current = 0;          long int line_current = 0;
         const void *p_shm;  
         size_t data_len;  
         long line_total;  
         const void *p_data;  
         const long *p_line_offsets;  
626          long int len;          long int len;
627          long int percentile;          long int percentile;
628          int loop;          int loop;
629            int eol, display_len;
630    
631          if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)          clrline(output_current_row, SCREEN_ROWS);
632    
633            // update msg_ext with extended key handler
634            if (key_handler(&ch, &ctx) != 0)
635          {          {
636                  log_error("get_file_shm(%s) error\n", filename);                  return ch;
                 return KEY_NULL;  
637          }          }
638    
         clrline(begin_line, SCREEN_ROWS);  
         line = begin_line;  
         max_lines = SCREEN_ROWS - 1;  
   
639          loop = 1;          loop = 1;
640          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
641          {          {
642                  if (line_current >= line_total && line_total <= SCREEN_ROWS - 2)                  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 275  int display_file_ex(const char *filename Line 654  int display_file_ex(const char *filename
654                          break;                          break;
655                  }                  }
656    
657                  if (line_current >= line_total || line >= max_lines)                  if (line_current >= display_line_total || output_current_row > output_end_row)
658                  {                  {
659                          if (line_current - (line - 1) + (SCREEN_ROWS - 2) < 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 = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total;                                  percentile = (line_current - (output_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total;
664                                    ctx.reach_end = 0;
665                          }                          }
666                          else                          else
667                          {                          {
668                                  percentile = 100;                                  percentile = 100;
669                                    ctx.reach_end = 1;
670                            }
671    
672                            ctx.line_top = line_current - (output_current_row - screen_begin_row) + 1;
673                            ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);
674    
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);                          moveto(SCREEN_ROWS, 0);
691                          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 ? " " : "")));  
692                          iflush();                          iflush();
693    
694                          input_ok = 0;                          input_ok = 0;
695                          while (!SYS_server_exit && !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:                                  case KEY_NULL:
714                                            log_error("KEY_NULL\n");
715                                            goto cleanup;
716                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
717                                            log_error("User input timeout\n");
718                                          goto cleanup;                                          goto cleanup;
719                                  case KEY_HOME:                                  case KEY_HOME:
720                                            if (line_current - output_current_row < 0) // Reach begin
721                                            {
722                                                    break;
723                                            }
724                                          line_current = 0;                                          line_current = 0;
725                                          line = begin_line;                                          output_current_row = screen_begin_row;
726                                          max_lines = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
727                                          clrline(begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
728                                          break;                                          break;
729                                  case KEY_END:                                  case KEY_END:
730                                          line_current = line_total - (SCREEN_ROWS - 2);                                          if (display_line_total < screen_row_total)
731                                          line = begin_line;                                          {
732                                          max_lines = SCREEN_ROWS - 1;                                                  break;
733                                          clrline(begin_line, SCREEN_ROWS);                                          }
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;                                          break;
739                                  case KEY_UP:                                  case KEY_UP:
740                                          if (line_current - line < 0) // Reach top                                          if (line_current - output_current_row < 0) // Reach begin
741                                          {                                          {
742                                                  break;                                                  break;
743                                          }                                          }
744                                          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[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
748                                          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
749                                          break;                                          break;
750                                  case CR:                                  case CR:
                                         igetch_reset();  
751                                  case KEY_DOWN:                                  case KEY_DOWN:
752                                          if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end
753                                          {                                          {
754                                                  break;                                                  break;
755                                          }                                          }
756                                          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[S"); // 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 (line_current - line < 0) // Reach top  
766                                          {                                          {
767                                                  break;                                                  break;
768                                          }                                          }
769                                          line_current -= ((SCREEN_ROWS - 3) + (line - 1));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
770                                          if (line_current < 0)                                          if (line_current < 0)
771                                          {                                          {
772                                                  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 (line_current + (SCREEN_ROWS - 2) - (line - 1) >= 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                                          line_current += (SCREEN_ROWS - 3) - (line - 1);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
785                                          if (line_current + SCREEN_ROWS - 2 > line_total) // No enough lines to display                                          if (line_current + screen_row_total > display_line_total) // No enough lines to display
786                                          {                                          {
787                                                  line_current = 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_ESC:                                  case KEY_ESC:
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
                                         if (!show_help)  
799                                          {                                          {
800                                                  break;                                                  break;
801                                          }                                          }
   
802                                          // Display help information                                          // Display help information
803                                          show_help = 0;                                          show_help = 0;
804                                          display_file_ex(DATA_READ_HELP, begin_line, 1);                                          display_file(help_filename, 1);
805                                          show_help = 1;                                          show_help = 1;
806                                    case KEY_F5:
807                                          // Refresh after display help information                                          // Refresh after display help information
808                                          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;
817                                          break;                                          break;
818                                  }                                  }
   
                                 BBS_last_access_tm = time(0);  
819                          }                          }
820    
821                          continue;                          continue;
822                  }                  }
823    
824                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
825                  if (len >= LINE_BUFFER_LEN)                  if (len >= sizeof(buffer))
826                    {
827                            log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
828                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
829                            len = sizeof(buffer) - 1;
830                    }
831                    else if (len < 0)
832                  {                  {
833                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",                          log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld \n",
834                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
835                          len = LINE_BUFFER_LEN - 1;                          len = 0;
836                  }                  }
837    
838                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
839                  buffer[len] = '\0';                  buffer[len] = '\0';
840    
841                  moveto(line, 0);                  moveto(output_current_row, 0);
842                  clrtoeol();                  clrtoeol();
843                  prints("%s", buffer);                  prints("%s", buffer);
844                  line_current++;                  line_current++;
845                  line++;                  output_current_row++;
846          }          }
847    
848  cleanup:  cleanup:
849            return ch;
850    }
851    
852    int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
853    {
854            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            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)          if (detach_file_shm(p_shm) < 0)
890          {          {
891                  log_error("detach_file_shm(%s) error\n", filename);                  log_error("detach_file_shm(%s) error\n", filename);
892          }          }
893    
894          return ch;          return ret;
895  }  }
896    
897  int show_top(const char *str_left, const char *str_middle, const char *str_right)  int show_top(const char *str_left, const char *str_middle, const char *str_right)
# Line 447  int show_top(const char *str_left, const Line 907  int show_top(const char *str_left, const
907    
908          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
909          str_left_f[sizeof(str_left_f) - 1] = '\0';          str_left_f[sizeof(str_left_f) - 1] = '\0';
910          len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN, &eol, &str_left_len);          len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN / 2, &eol, &str_left_len, 1);
911          str_left_f[len] = '\0';          str_left_f[len] = '\0';
912    
913          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
914          str_middle_f[sizeof(str_middle_f) - 1] = '\0';          str_middle_f[sizeof(str_middle_f) - 1] = '\0';
915          len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN, &eol, &str_middle_len);          len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN / 2, &eol, &str_middle_len, 1);
916          str_middle_f[len] = '\0';          str_middle_f[len] = '\0';
917    
918          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
919          str_right_f[sizeof(str_right_f) - 1] = '\0';          str_right_f[sizeof(str_right_f) - 1] = '\0';
920          len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN, &eol, &str_right_len);          len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN / 2, &eol, &str_right_len, 1);
921          str_right_f[len] = '\0';          str_right_f[len] = '\0';
922    
923          moveto(1, 0);          moveto(1, 0);
924          clrtoeol();          clrtoeol();
925          prints("\033[1;44;33m%s\033[37m%*s\033[33m%*s\033[m",          prints("\033[1;44;33m%s\033[37m%*s%s\033[33m%*s%s\033[m",
926                     str_left_f, 44 - str_left_len, str_middle_f, 36, str_right_f);                     str_left_f, 44 - str_left_len - str_middle_len, "",
927                       str_middle_f, 36 - str_right_len, "", str_right_f);
928    
929          return 0;          return 0;
930  }  }
# Line 471  int show_top(const char *str_left, const Line 932  int show_top(const char *str_left, const
932  int show_bottom(const char *msg)  int show_bottom(const char *msg)
933  {  {
934          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
935            int len_str_time;
936          time_t time_online;          time_t time_online;
937          struct tm *tm_online;          struct tm *tm_online;
938          char msg_f[23];          char msg_f[LINE_BUFFER_LEN];
939          int eol;          int eol;
940          int msg_len;          int len_msg;
941          int len;          int len;
942          int len_username;          int len_username;
943            char str_tm_online[LINE_BUFFER_LEN];
944            int len_str_tm_online;
945    
946          get_time_str(str_time, sizeof(str_time));          len_str_time = (int)get_time_str(str_time, sizeof(str_time));
947    
948          msg_f[0] = '\0';          msg_f[0] = '\0';
949          msg_len = 0;          len_msg = 0;
950          if (msg != NULL && msg[0] != '\0')          if (msg != NULL)
951          {          {
952                  msg_f[0] = '[';                  strncpy(msg_f, msg, sizeof(msg_f) - 1);
                 strncpy(msg_f + 1, msg, sizeof(msg_f) - 2);  
953                  msg_f[sizeof(msg_f) - 1] = '\0';                  msg_f[sizeof(msg_f) - 1] = '\0';
954                  len = split_line(msg_f + 1, 20, &eol, &msg_len);                  len = split_line(msg_f, 23, &eol, &len_msg, 1);
955                  msg_f[len + 1] = ']';                  msg_f[len] = '\0';
                 msg_f[len + 2] = '\0';  
                 msg_len += 2;  
956          }          }
957    
958          len_username = (int)strnlen(BBS_username, sizeof(BBS_username));          len_username = (int)strnlen(BBS_username, sizeof(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] %s%*s ʺ[\033[36m%s\033[33m]"          prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m用户[\033[36m%s\033[33m][%s\033[33m]\033[m",
979                     "[\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,
980                     str_time, msg_f, 34 - msg_len - len_username, "", BBS_username,                     "", BBS_username, str_tm_online);
                    tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);  
981    
982          return 0;          return 0;
983  }  }
# Line 533  int show_active_board() Line 1006  int show_active_board()
1006                  }                  }
1007          }          }
1008    
1009          if (time(0) - t_last_show >= 10)          if (time(NULL) - t_last_show >= 10)
1010          {          {
1011                  line_last = line_current;                  line_last = line_current;
1012                  t_last_show = time(0);                  t_last_show = time(NULL);
1013          }          }
1014          else          else
1015          {          {


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

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