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


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

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