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


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

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