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


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

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