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


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

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