/[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.10 by sysadm, Sun Mar 20 17:23:17 2005 UTC Revision 1.128 by sysadm, Sat Nov 8 09:05:59 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                            screen.c  -  description  /*
3                               -------------------   * screen
4      begin                : Mon Oct 18 2004   *   - advanced telnet-based user interactive input / output features
5      copyright            : (C) 2004 by Leaflet   *
6      email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
   
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
11    #include "editor.h"
12    #include "file_loader.h"
13  #include "io.h"  #include "io.h"
14  #include <sys/types.h>  #include "log.h"
15  #include <sys/stat.h>  #include "login.h"
16    #include "screen.h"
17    #include "str_process.h"
18    #include <ctype.h>
19    #include <errno.h>
20    #include <fcntl.h>
21    #include <string.h>
22    #include <stdlib.h>
23  #include <unistd.h>  #include <unistd.h>
24    #include <wchar.h>
25    #include <sys/param.h>
26    #include <sys/stat.h>
27    #include <sys/shm.h>
28    #include <sys/types.h>
29    
30  #define ACTIVE_BOARD_HEIGHT     8  const char CTRL_SEQ_CLR_LINE[] = "\033[K";
31    
32  int screen_lines = 24;  static const int ACTIVE_BOARD_HEIGHT = 8;
33    
34  void  static const int STR_TOP_LEFT_MAX_LEN = 80;
35  moveto (int row, int col)  static const int STR_TOP_MIDDLE_MAX_LEN = 40;
36    static const int STR_TOP_RIGHT_MAX_LEN = 80;
37    
38    static size_t get_time_str(char *s, size_t len)
39  {  {
40    if (row >= 0)          time_t curtime;
41      {          struct tm local_tm;
42        prints ("\033[%d;%dH", row, col);  
43      }          time(&curtime);
44    else          localtime_r(&curtime, &local_tm);
45      {          size_t j = strftime(s, len, "%m/%d %H:%M %Z", &local_tm);
46        prints ("\r");  
47      }          return j;
   iflush ();  
48  }  }
49    
50  void  void moveto(int row, int col)
 clrtoeol ()  
51  {  {
52    prints ("\033[K");          if (row >= 0)
53    iflush ();          {
54                    prints("\033[%d;%dH", row, col);
55            }
56            else
57            {
58                    prints("\r");
59            }
60  }  }
61    
62  void  void clrtoeol()
 clrline (int line_begin, int line_end)  
63  {  {
64    int i;          prints(CTRL_SEQ_CLR_LINE);
65    }
66    
67    for (i = line_begin; i <= line_end; i++)  void clrline(int line_begin, int line_end)
68      {  {
69        moveto (i, 0);          int i;
       prints ("\033[K");  
     }  
70    
71    iflush ();          for (i = line_begin; i <= line_end; i++)
72            {
73                    moveto(i, 0);
74                    prints(CTRL_SEQ_CLR_LINE);
75            }
76  }  }
77    
78  void  void clrtobot(int line_begin)
 clearscr ()  
79  {  {
80    prints ("\33[2J");          moveto(line_begin, 0);
81    moveto (0, 0);          prints("\033[J");
82    iflush ();          moveto(line_begin, 0);
83  }  }
84    
85  int  void clearscr()
 press_any_key ()  
86  {  {
87    moveto (screen_lines, 0);          prints("\033[2J");
88    clrtoeol ();          moveto(0, 0);
   
   prints ("                           \033[1;33m按任意键继续...\033[0;37m");  
   iflush ();  
   
   return igetch ();  
89  }  }
90    
91  void  inline int press_any_key()
 set_input_echo(int echo)  
92  {  {
93    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 ();  
   }  
94  }  }
95    
96  int  int press_any_key_ex(const char *msg, int sec)
 str_input (char *buffer, int buffer_length, int echo_mode)  
97  {  {
98    char buf[256], ch;          int ch = 0;
99    int c, offset = 0, len, loop = 1, i, hz = 0;          int duration = 0;
100            time_t t_begin = time(NULL);
101    
102    memset (buffer, '\0', buffer_length);          moveto(SCREEN_ROWS, 0);
103            clrtoeol();
104    
105    while (c = igetch ())          prints(msg);
106      {          iflush();
107        if (c == CR)  
108          break;          igetch_reset();
109        if (c == LF)  
110          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)  
111          {          {
112            hz = (!hz);                  ch = igetch_t(sec - duration);
113          }                  duration = (int)(time(NULL) - t_begin);
114        if (offset >= buffer_length)          } while (!SYS_server_exit && ch == 0 && duration < 60);
115    
116            return ch;
117    }
118    
119    void set_input_echo(int echo)
120    {
121            if (echo)
122          {          {
123            outc ('\a');                  outc('\x83'); // ASCII code 131
           continue;  
124          }          }
125        buffer[offset++] = (char) c;          else
       buffer[offset] = '\0';  
       switch (echo_mode)  
126          {          {
127          case 0:                  // outc ('\x85'); // ASCII code 133
128            outc ((char) c);                  prints("\xff\xfb\x01\xff\xfb\x03");
           break;  
         case 1:  
           outc ('*');  
           break;  
129          }          }
130        if (!hz)          iflush();
131    }
132    
133    static int _str_input(char *buffer, int buf_size, int max_display_len, enum io_echo_t echo_mode)
134    {
135            int ch;
136            int offset = 0;
137            int eol;
138            int display_len;
139            char input_str[5];
140            int str_len = 0;
141            wchar_t wcs[2];
142            char c;
143    
144            buffer[buf_size - 1] = '\0';
145            offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
146    
147            igetch_reset();
148    
149            while (!SYS_server_exit)
150          {          {
151            iflush ();                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
         }  
     }  
152    
153    prints ("\r\n");                  if (ch == CR)
154    iflush ();                  {
155                            break;
156                    }
157                    else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
158                    {
159                            return -1;
160                    }
161                    else if (ch == LF || ch == '\0')
162                    {
163                            continue;
164                    }
165                    else if (ch == BACKSPACE)
166                    {
167                            if (offset > 0)
168                            {
169                                    offset--;
170                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
171                                    {
172                                            while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
173                                            {
174                                                    offset--;
175                                            }
176                                            display_len--;
177                                            prints("\033[D \033[D");
178                                    }
179                                    buffer[offset] = '\0';
180                                    display_len--;
181                                    prints("\033[D \033[D");
182                                    iflush();
183                            }
184                            continue;
185                    }
186                    else if (ch > 255 || iscntrl(ch))
187                    {
188                            continue;
189                    }
190                    else if ((ch & 0xff80) == 0x80) // head of multi-byte character
191                    {
192                            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    return offset;                          if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
224                            {
225                                    log_error("mbstowcs() error\n");
226                            }
227                            if (offset + str_len > buf_size - 1 || display_len + (UTF8_fixed_width ? 2 : wcwidth(wcs[0])) > max_display_len) // No enough space for Chinese character
228                            {
229                                    outc('\a');
230                                    iflush();
231                                    continue;
232                            }
233    
234                            memcpy(buffer + offset, input_str, (size_t)str_len);
235                            offset += str_len;
236                            buffer[offset] = '\0';
237                            display_len += 2;
238    
239                            switch (echo_mode)
240                            {
241                            case DOECHO:
242                                    prints(input_str);
243                                    break;
244                            case NOECHO:
245                                    prints("**");
246                                    break;
247                            }
248                    }
249                    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)ch;
259                            buffer[offset] = '\0';
260                            display_len++;
261    
262                            switch (echo_mode)
263                            {
264                            case DOECHO:
265                                    outc((char)ch);
266                                    break;
267                            case NOECHO:
268                                    outc('*');
269                                    break;
270                            }
271                    }
272                    else // Invalid character
273                    {
274                            continue;
275                    }
276    
277                    iflush();
278            }
279    
280            return offset;
281  }  }
282    
283  int  int str_input(char *buffer, int buf_size, enum io_echo_t echo_mode)
 display_file (const char *filename)  
284  {  {
285    char buffer[260];          int len;
   FILE *fin;  
   int i;  
286    
287    if ((fin = fopen (filename, "r")) == NULL)          buffer[0] = '\0';
   {  
     return -1;  
   }  
288    
289    while (fgets (buffer, 255, fin))          len = _str_input(buffer, buf_size, buf_size, echo_mode);
290      {  
291        i = strlen (buffer);          prints("\r\n");
292        if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')          iflush();
         {  
           buffer[i - 1] = '\r';  
           buffer[i] = '\n';  
           buffer[i + 1] = '\0';  
         }  
       prints (buffer);  
       iflush ();  
     }  
   fclose(fin);  
293    
294    return 0;          return len;
295  }  }
296    
297  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)  
298  {  {
299    char buffer[260], temp[256];          int len = 0;
300    int i, ch, input_ok;          int col_cur = 0;
301    long int line, c_line_begin = 0, c_line_total=0;          int ch;
302    long int f_line, f_size, f_offset;          int offset = 0;
303    FILE *fin;          int eol;
304    struct stat f_stat;          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);
318            prints("%s", prompt);
319            prints("%s", buffer);
320            prints("%*s", max_display_len - display_len, "");
321            moveto(row, col_cur);
322            iflush();
323    
324    clrline (begin_line, screen_lines);          igetch_reset();
   line = begin_line;  
   moveto (line, 0);  
325    
326    if ((fin = fopen (filename, "r")) != NULL)          while (!SYS_server_exit)
     {  
       if (fstat (fileno (fin), &f_stat) != 0)  
327          {          {
328            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;  
329    
330        while (fgets (buffer, 255, fin))                  if (ch == CR)
331          c_line_total ++;                  {
332        rewind (fin);                          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        while (fgets (buffer, 255, fin))                                  moveto(row, col_cur);
444          {                                  iflush();
445            if (line >= screen_lines)                          }
446              {                          continue;
447                f_offset = ftell (fin);                  }
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                moveto (screen_lines, 0);                                  col_cur++;
479                prints                                  offset += str_len;
                 ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",  
                  (f_offset-strlen(buffer)) * 100 / f_size);  
               iflush ();  
480    
481                input_ok = 0;                                  moveto(row, col_cur);
482                while (!input_ok)                                  iflush();
483                            }
484                            continue;
485                    }
486                    else if (ch == KEY_HOME || ch == KEY_UP)
487                  {                  {
488                    ch = igetch ();                          if (offset > 0)
                   input_ok = 1;  
                   switch (ch)  
                     {  
                     case KEY_UP:  
                       c_line_begin--;  
                       if (c_line_begin >= 0)  
489                          {                          {
490                            rewind (fin);                                  offset = 0;
491                            for (f_line = 0; f_line < c_line_begin; f_line++)                                  col_cur = col + str_length(prompt, 1);
492                              {  
493                                if (fgets (buffer, 255, fin) == NULL)                                  moveto(row, col_cur);
494                                  goto exit;                                  iflush();
                             }  
495                          }                          }
496                        else                          continue;
497                    }
498                    else if (ch == KEY_END || ch == KEY_DOWN)
499                    {
500                            if (offset < len)
501                          {                          {
502                            goto exit;                                  offset = len;
503                                    col_cur = col + str_length(prompt, 1) + display_len;
504    
505                                    moveto(row, col_cur);
506                                    iflush();
507                          }                          }
508                        break;                          continue;
509                      case KEY_DOWN:                  }
510                      case CR:                  else if (ch > 255 || iscntrl(ch))
511                        c_line_begin++;                  {
512                        rewind (fin);                          continue;
513                        for (f_line = 0; f_line < c_line_begin; f_line++)                  }
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                            if (fgets (buffer, 255, fin) == NULL)                                  input_str[str_len] = (char)(ch - 256);
521                              goto exit;                                  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                        break;                          input_str[str_len] = '\0';
541                      case KEY_PGUP:  
542                      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++)  
543                          {                          {
544                            if (fgets (buffer, 255, fin) == NULL)                                  continue;
                             goto exit;  
545                          }                          }
546                        break;  
547                      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++)  
548                          {                          {
549                            if (fgets (buffer, 255, fin) == NULL)                                  log_error("mbstowcs() error\n");
                             goto exit;  
550                          }                          }
551                        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);  
552    
553                        //Refresh after display help information                          if (len + str_len > buf_size - 1 ||
554                        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++)  
555                          {                          {
556                            if (fgets (buffer, 255, fin) == NULL)                                  outc('\a');
557                              goto exit;                                  iflush();
558                                    continue;
559                          }                          }
560                        break;  
561                      default:                          memmove(buffer + offset + str_len, buffer + offset, (size_t)(len - offset));
562                        input_ok = 0;                          memcpy(buffer + offset, input_str, (size_t)str_len);
563                        break;                          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                clrline (begin_line, screen_lines);          return len;
611                line = begin_line;  }
               moveto (line, 0);  
612    
613                continue;  int display_data(const void *p_data, long display_line_total, const long *p_line_offsets, int eof_exit,
614              }                                   display_data_key_handler key_handler, const char *help_filename)
615    {
616            static int show_help = 1;
617            char buffer[LINE_BUFFER_LEN];
618            DISPLAY_CTX ctx;
619            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;
626            long int len;
627            long int percentile;
628            int loop;
629            int eol, display_len;
630    
631            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 ();  
632    
633            line++;          // update msg_ext with extended key handler
634            if (key_handler(&ch, &ctx) != 0)
635            {
636                    return ch;
637          }          }
       if (wait)  
         ch = press_any_key();  
       else  
         ch = 0;  
638    
639      exit:          loop = 1;
640        fclose (fin);          while (!SYS_server_exit && loop)
641            {
642                    if (eof_exit > 0 && line_current >= display_line_total)
643                    {
644                            if (eof_exit == 1)
645                            {
646                                    ch = press_any_key();
647                            }
648                            else // if (eof_exit == 2)
649                            {
650                                    iflush();
651                            }
652    
653        return ch;                          loop = 0;
654      }                          break;
655                    }
656    
657                    if (line_current >= display_line_total || output_current_row > output_end_row)
658                    {
659                            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 - (output_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total;
664                                    ctx.reach_end = 0;
665                            }
666                            else
667                            {
668                                    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    return -1;                          moveto(SCREEN_ROWS, 0);
691                            prints("%s", buffer);
692                            iflush();
693    
694                            input_ok = 0;
695                            while (!SYS_server_exit && !input_ok)
696                            {
697                                    ch = igetch_t(BBS_max_user_idle_time);
698                                    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)
712                                    {
713                                    case KEY_NULL:
714                                            log_error("KEY_NULL\n");
715                                            goto cleanup;
716                                    case KEY_TIMEOUT:
717                                            log_error("User input timeout\n");
718                                            goto cleanup;
719                                    case KEY_HOME:
720                                            if (line_current - output_current_row < 0) // Reach begin
721                                            {
722                                                    break;
723                                            }
724                                            line_current = 0;
725                                            output_current_row = screen_begin_row;
726                                            output_end_row = SCREEN_ROWS - 1;
727                                            clrline(output_current_row, SCREEN_ROWS);
728                                            break;
729                                    case KEY_END:
730                                            if (display_line_total < screen_row_total)
731                                            {
732                                                    break;
733                                            }
734                                            line_current = display_line_total - screen_row_total;
735                                            output_current_row = screen_begin_row;
736                                            output_end_row = SCREEN_ROWS - 1;
737                                            clrline(output_current_row, SCREEN_ROWS);
738                                            break;
739                                    case KEY_UP:
740                                            if (line_current - output_current_row < 0) // Reach begin
741                                            {
742                                                    break;
743                                            }
744                                            line_current -= output_current_row;
745                                            output_current_row = screen_begin_row;
746                                            // screen_end_line = screen_begin_line;
747                                            // prints("\033[T"); // Scroll down 1 line
748                                            output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
749                                            break;
750                                    case CR:
751                                    case KEY_DOWN:
752                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end
753                                            {
754                                                    break;
755                                            }
756                                            line_current += (screen_row_total - (output_current_row - screen_begin_row));
757                                            output_current_row = screen_row_total;
758                                            output_end_row = SCREEN_ROWS - 1;
759                                            moveto(SCREEN_ROWS, 0);
760                                            clrtoeol();
761                                            // prints("\033[S"); // Scroll up 1 line
762                                            prints("\n"); // Legacy Cterm only works with this line
763                                            break;
764                                    case KEY_PGUP:
765                                            if (line_current - output_current_row < 0) // Reach begin
766                                            {
767                                                    break;
768                                            }
769                                            line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
770                                            if (line_current < 0)
771                                            {
772                                                    line_current = 0;
773                                            }
774                                            output_current_row = screen_begin_row;
775                                            output_end_row = SCREEN_ROWS - 1;
776                                            clrline(output_current_row, SCREEN_ROWS);
777                                            break;
778                                    case KEY_SPACE:
779                                    case KEY_PGDN:
780                                            if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end
781                                            {
782                                                    break;
783                                            }
784                                            line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
785                                            if (line_current + screen_row_total > display_line_total) // No enough lines to display
786                                            {
787                                                    line_current = display_line_total - screen_row_total;
788                                            }
789                                            output_current_row = screen_begin_row;
790                                            output_end_row = SCREEN_ROWS - 1;
791                                            clrline(output_current_row, SCREEN_ROWS);
792                                            break;
793                                    case KEY_ESC:
794                                    case KEY_LEFT:
795                                            loop = 0;
796                                            break;
797                                    case 'h':
798                                            if (!show_help) // Not reentrant
799                                            {
800                                                    break;
801                                            }
802                                            // Display help information
803                                            show_help = 0;
804                                            display_file(help_filename, 1);
805                                            show_help = 1;
806                                    case KEY_F5:
807                                            // Refresh after display help information
808                                            line_current -= (output_current_row - screen_begin_row);
809                                            output_current_row = screen_begin_row;
810                                            output_end_row = SCREEN_ROWS - 1;
811                                            clrline(output_current_row, SCREEN_ROWS);
812                                            break;
813                                    case 0: // Refresh bottom line
814                                            break;
815                                    default:
816                                            input_ok = 0;
817                                            break;
818                                    }
819                            }
820    
821                            continue;
822                    }
823    
824                    len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
825                    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("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);
835                            len = 0;
836                    }
837    
838                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
839                    buffer[len] = '\0';
840    
841                    moveto(output_current_row, 0);
842                    clrtoeol();
843                    prints("%s", buffer);
844                    line_current++;
845                    output_current_row++;
846            }
847    
848    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  int display_file(const char *filename, int eof_exit)
 show_top (char *status)  
868  {  {
869    char buffer[256];          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    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)
877            {
878                    log_error("get_file_shm(%s) error\n", filename);
879                    return KEY_NULL;
880            }
881    
882    moveto (1, 0);          if (user_online_update("VIEW_FILE") < 0)
883    prints ("\033[1;44;33m%-20s \033[37m%20s"          {
884            "         %s\033[33m讨论区 [%s]\033[m",                  log_error("user_online_update(VIEW_FILE) error\n");
885            status, BBS_name, buffer, BBS_current_section_name);          }
   iflush ();  
886    
887    return 0;          ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);
888    
889            if (detach_file_shm(p_shm) < 0)
890            {
891                    log_error("detach_file_shm(%s) error\n", filename);
892            }
893    
894            return ret;
895    }
896    
897    int show_top(const char *str_left, const char *str_middle, const char *str_right)
898    {
899            char str_left_f[STR_TOP_LEFT_MAX_LEN + 1];
900            char str_middle_f[STR_TOP_MIDDLE_MAX_LEN + 1];
901            char str_right_f[STR_TOP_RIGHT_MAX_LEN + 1];
902            int str_left_len;
903            int str_middle_len;
904            int str_right_len;
905            int eol;
906            int len;
907    
908            strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
909            str_left_f[sizeof(str_left_f) - 1] = '\0';
910            len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN / 2, &eol, &str_left_len, 1);
911            str_left_f[len] = '\0';
912    
913            strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
914            str_middle_f[sizeof(str_middle_f) - 1] = '\0';
915            len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN / 2, &eol, &str_middle_len, 1);
916            str_middle_f[len] = '\0';
917    
918            strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
919            str_right_f[sizeof(str_right_f) - 1] = '\0';
920            len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN / 2, &eol, &str_right_len, 1);
921            str_right_f[len] = '\0';
922    
923            moveto(1, 0);
924            clrtoeol();
925            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_len, "",
927                       str_middle_f, 36 - str_right_len, "", str_right_f);
928    
929            return 0;
930  }  }
931    
932  int  int show_bottom(const char *msg)
 show_bottom (char *msg)  
933  {  {
934    char str_time[256], str_time_onine[20], buffer[256];          char str_time[LINE_BUFFER_LEN];
935    time_t time_online;          int len_str_time;
936    struct tm *tm_online;          time_t time_online;
937            struct tm *tm_online;
938            char msg_f[LINE_BUFFER_LEN];
939            int eol;
940            int len_msg;
941            int len;
942            int len_username;
943            char str_tm_online[LINE_BUFFER_LEN];
944            int len_str_tm_online;
945    
946            len_str_time = (int)get_time_str(str_time, sizeof(str_time));
947    
948            msg_f[0] = '\0';
949            len_msg = 0;
950            if (msg != NULL)
951            {
952                    strncpy(msg_f, msg, sizeof(msg_f) - 1);
953                    msg_f[sizeof(msg_f) - 1] = '\0';
954                    len = split_line(msg_f, 23, &eol, &len_msg, 1);
955                    msg_f[len] = '\0';
956            }
957    
958    get_time_str (str_time, 256);          len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
   str_space (buffer, 33 - strlen (BBS_username));  
959    
960    time_online = time (0) - BBS_login_tm;          time_online = time(NULL) - BBS_login_tm;
961    tm_online = gmtime (&time_online);          tm_online = gmtime(&time_online);
962            if (tm_online->tm_mday > 1)
963            {
964                    snprintf(str_tm_online, sizeof(str_tm_online),
965                                     "\033[36m%d\033[33md \033[36m%d\033[33m:\033[36m%.2d",
966                                     tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
967            }
968            else
969            {
970                    snprintf(str_tm_online, sizeof(str_tm_online),
971                                     "\033[36m%d\033[33m:\033[36m%.2d",
972                                     tm_online->tm_hour, tm_online->tm_min);
973            }
974            len_str_tm_online = str_length(str_tm_online, 1);
975    
976    moveto (screen_lines, 0);          moveto(SCREEN_ROWS, 0);
977    prints ("\033[1;44;33m[\033[36m%s\033[33m]"          clrtoeol();
978            "%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, buffer, BBS_username, tm_online->tm_mday - 1,                     "", BBS_username, str_tm_online);
           tm_online->tm_hour, tm_online->tm_min);  
   iflush ();  
981    
982    return 0;          return 0;
983  }  }
984    
985  int  int show_active_board()
 show_active_board ()  
986  {  {
987    char filename[256], buffer[260];          static int line_current = 0;
988    FILE *fin;          static const void *p_shm = NULL;
989    int i, j;          static size_t data_len;
990    static long int line;          static long line_total;
991            static const void *p_data;
992            static const long *p_line_offsets;
993    
994    sprintf (filename, "%sdata/active_board.txt", app_home_dir);          static time_t t_last_show = 0;
995            static int line_last = 0;
996    
997    clrline (3, 2 + ACTIVE_BOARD_HEIGHT);          char buffer[LINE_BUFFER_LEN];
998            long int len;
999    
1000    moveto (3, 0);          if (p_shm == NULL)
1001            {
1002                    if ((p_shm = get_file_shm_readonly(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
1003                    {
1004                            log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
1005                            return KEY_NULL;
1006                    }
1007            }
1008    
1009    if ((fin = fopen (filename, "r")) != NULL)          if (time(NULL) - t_last_show >= 10)
     {  
       for (j = 0; j < line; j++)  
1010          {          {
1011            if (fgets (buffer, 255, fin) == NULL)                  line_last = line_current;
1012              {                  t_last_show = time(NULL);
               line = 0;  
               rewind (fin);  
               break;  
             }  
1013          }          }
1014            else
1015            {
1016                    line_current = line_last;
1017            }
1018    
1019            clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
1020    
1021        for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
1022          {          {
1023            if (fgets (buffer, 255, fin) == NULL)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
1024              {                  if (len >= LINE_BUFFER_LEN)
               line = 0;  
               if (j == 0)  
1025                  {                  {
1026                    rewind (fin);                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
1027                    if (fgets (buffer, 255, fin) == NULL)                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
1028                      {                          len = LINE_BUFFER_LEN - 1;
                       break;  
                     }  
1029                  }                  }
1030                else  
1031                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
1032                    buffer[len] = '\0';
1033    
1034                    moveto(3 + i, 0);
1035                    prints("%s", buffer);
1036    
1037                    line_current++;
1038                    if (line_current >= line_total)
1039                  {                  {
1040                    break;                          line_current = 0;
1041                            break;
1042                  }                  }
             }  
           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 ();  
1043          }          }
       fclose (fin);  
     }  
1044    
1045    return 0;          return 0;
1046  }  }


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

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