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


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

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