/[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.102 by sysadm, Sat Jun 21 02:15:18 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"
18    #include "common.h"
19    #include "editor.h"
20    #include "file_loader.h"
21  #include "io.h"  #include "io.h"
22    #include "log.h"
23    #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>
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
38    
39    #define STR_TOP_LEFT_MAX_LEN 40
40    #define STR_TOP_MIDDLE_MAX_LEN 20
41    #define STR_TOP_RIGHT_MAX_LEN 20
42    
43  void  void moveto(int row, int col)
 moveto (int row, int col)  
44  {  {
45    if (row >= 0)          if (row >= 0)
46      {          {
47        prints ("\033[%d;%dH", row, col);                  prints("\033[%d;%dH", row, col);
48      }          }
49    else          else
50      {          {
51        prints ("\r");                  prints("\r");
52      }          }
   iflush();  
53  }  }
54    
55  void  void clrtoeol()
 clrtoeol ()  
56  {  {
57    prints ("\033[K");          prints(CTRL_SEQ_CLR_LINE);
   iflush();  
58  }  }
59    
60  void  void clrline(int line_begin, int line_end)
 clearscr()  
61  {  {
62    prints ("\33[2J");          int i;
63    moveto (0,0);  
64    iflush();          for (i = line_begin; i <= line_end; i++)
65            {
66                    moveto(i, 0);
67                    prints(CTRL_SEQ_CLR_LINE);
68            }
69  }  }
70    
71  int  void clrtobot(int line_begin)
 str_input (char *buffer, int buffer_length, int echo_mode)  
72  {  {
73    char buf[256], ch;          moveto(line_begin, 0);
74    int c, offset = 0, len, loop = 1, i, hz = 0;          prints("\033[J");
75            moveto(line_begin, 0);
76    }
77    
78    memset (buffer, '\0', buffer_length);  void clearscr()
79    {
80            prints("\033[2J");
81            moveto(0, 0);
82    }
83    
84    while (c = igetch ())  int press_any_key()
85      {  {
86        if (c == CR || c == LF)          moveto(SCREEN_ROWS, 0);
87          break;          clrtoeol();
88        if (c == BACKSPACE)  
89            prints("                           \033[1;33m按任意键继续...\033[0;37m");
90            iflush();
91    
92            return igetch_t(MIN(MAX_DELAY_TIME, 60));
93    }
94    
95    void set_input_echo(int echo)
96    {
97            if (echo)
98          {          {
99            if (offset > 0)                  outc('\x83'); // ASCII code 131
100              {                  iflush();
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
101          }          }
102        if (c > 255 || iscntrl (c))          else
103            {
104                    //    outc ('\x85'); // ASCII code 133
105                    prints("\xff\xfb\x01\xff\xfb\x03");
106                    iflush();
107                    igetch(0);
108                    igetch_reset();
109            }
110    }
111    
112    static int _str_input(char *buffer, int buf_size, int echo_mode)
113    {
114            int c;
115            int offset = 0;
116            int hz = 0;
117    
118            buffer[buf_size - 1] = '\0';
119            for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)
120                    ;
121    
122            igetch_reset();
123    
124            while (!SYS_server_exit)
125          {          {
126            continue;                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));
127    
128                    if (c == CR)
129                    {
130                            igetch_reset();
131                            break;
132                    }
133                    else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe
134                    {
135                            return -1;
136                    }
137                    else if (c == LF || c == '\0')
138                    {
139                            continue;
140                    }
141                    else if (c == BACKSPACE)
142                    {
143                            if (offset > 0)
144                            {
145                                    offset--;
146                                    if (buffer[offset] < 0 || buffer[offset] > 127)
147                                    {
148                                            prints("\033[D \033[D");
149                                            offset--;
150                                            if (offset < 0) // should not happen
151                                            {
152                                                    log_error("Offset of buffer is negative\n");
153                                                    offset = 0;
154                                            }
155                                    }
156                                    buffer[offset] = '\0';
157                                    prints("\033[D \033[D");
158                                    iflush();
159                            }
160                            continue;
161                    }
162                    else if (c > 255 || iscntrl(c))
163                    {
164                            continue;
165                    }
166                    else if (c > 127 && c <= 255)
167                    {
168                            if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character
169                            {
170                                    igetch(0); // Ignore 1 character
171                                    outc('\a');
172                                    iflush();
173                                    continue;
174                            }
175                            hz = (!hz);
176                    }
177    
178                    if (offset + 1 > buf_size - 1)
179                    {
180                            outc('\a');
181                            iflush();
182                            continue;
183                    }
184    
185                    buffer[offset++] = (char)c;
186                    buffer[offset] = '\0';
187    
188                    switch (echo_mode)
189                    {
190                    case DOECHO:
191                            outc((char)c);
192                            break;
193                    case NOECHO:
194                            outc('*');
195                            break;
196                    }
197                    if (!hz)
198                    {
199                            iflush();
200                    }
201          }          }
202        if (c > 127 && c <= 255)  
203          {          return offset;
204            hz = (!hz);  }
205          }  
206        if (offset >= buffer_length)  int str_input(char *buffer, int buf_size, int echo_mode)
207    {
208            int len;
209    
210            buffer[0] = '\0';
211    
212            len = _str_input(buffer, buf_size, echo_mode);
213    
214            prints("\r\n");
215            iflush();
216    
217            return len;
218    };
219    
220    int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int echo_mode)
221    {
222            int len;
223    
224            moveto(row, col);
225            prints("%s", prompt);
226            prints("%s", buffer);
227            iflush();
228    
229            len = _str_input(buffer, buf_size, echo_mode);
230    
231            return len;
232    }
233    
234    int display_data(const void *p_data, long display_line_total, const long *p_line_offsets, int eof_exit,
235                                     display_data_key_handler key_handler, const char *help_filename)
236    {
237            static int show_help = 1;
238            char buffer[LINE_BUFFER_LEN];
239            DISPLAY_CTX ctx;
240            int ch = 0;
241            int input_ok;
242            const int screen_begin_row = 1;
243            const int screen_row_total = SCREEN_ROWS - screen_begin_row;
244            int output_current_row = screen_begin_row;
245            int output_end_row = SCREEN_ROWS - 1;
246            long int line_current = 0;
247            long int len;
248            long int percentile;
249            int loop;
250            int eol, display_len;
251    
252            clrline(output_current_row, SCREEN_ROWS);
253    
254            // update msg_ext with extended key handler
255            if (key_handler(&ch, &ctx) != 0)
256          {          {
257            outc ('\a');                  return ch;
           continue;  
258          }          }
259        buffer[offset++] = (char) c;  
260        buffer[offset] = '\0';          loop = 1;
261        switch (echo_mode)          while (!SYS_server_exit && loop)
262          {          {
263          case 0:                  if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total)
264            outc ((char) c);                  {
265            break;                          if (eof_exit == 1)
266          case 1:                          {
267            outc ('*');                                  ch = press_any_key();
268            break;                          }
269                            else // if (eof_exit == 2)
270                            {
271                                    iflush();
272                            }
273    
274                            loop = 0;
275                            break;
276                    }
277    
278                    if (line_current >= display_line_total || output_current_row > output_end_row)
279                    {
280                            ctx.reach_begin = (line_current < output_current_row ? 1 : 0);
281    
282                            if (line_current - (output_current_row - screen_begin_row) + screen_row_total < display_line_total)
283                            {
284                                    percentile = (line_current - (output_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total;
285                                    ctx.reach_end = 0;
286                            }
287                            else
288                            {
289                                    percentile = 100;
290                                    ctx.reach_end = 1;
291                            }
292    
293                            ctx.line_top = line_current - (output_current_row - screen_begin_row) + 1;
294                            ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);
295    
296                            snprintf(buffer, sizeof(buffer),
297                                             "\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s",
298                                             ctx.line_top,
299                                             ctx.line_bottom,
300                                             percentile,
301                                             ctx.msg);
302    
303                            len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
304                            for (; display_len < SCREEN_COLS; display_len++)
305                            {
306                                    buffer[len++] = ' ';
307                            }
308                            buffer[len] = '\0';
309                            strncat(buffer, "\033[m", sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer)));
310    
311                            moveto(SCREEN_ROWS, 0);
312                            prints("%s", buffer);
313                            iflush();
314    
315                            input_ok = 0;
316                            while (!SYS_server_exit && !input_ok)
317                            {
318                                    ch = igetch_t(MAX_DELAY_TIME);
319                                    input_ok = 1;
320    
321                                    // extended key handler
322                                    if (key_handler(&ch, &ctx) != 0)
323                                    {
324                                            goto cleanup;
325                                    }
326    
327                                    switch (ch)
328                                    {
329                                    case KEY_NULL:
330                                    case KEY_TIMEOUT:
331                                            goto cleanup;
332                                    case KEY_HOME:
333                                            if (line_current - output_current_row < 0) // Reach begin
334                                            {
335                                                    break;
336                                            }
337                                            line_current = 0;
338                                            output_current_row = screen_begin_row;
339                                            output_end_row = SCREEN_ROWS - 1;
340                                            clrline(output_current_row, SCREEN_ROWS);
341                                            break;
342                                    case KEY_END:
343                                            if (display_line_total < screen_row_total)
344                                            {
345                                                    break;
346                                            }
347                                            line_current = display_line_total - screen_row_total;
348                                            output_current_row = screen_begin_row;
349                                            output_end_row = SCREEN_ROWS - 1;
350                                            clrline(output_current_row, SCREEN_ROWS);
351                                            break;
352                                    case KEY_UP:
353                                            if (line_current - output_current_row < 0) // Reach begin
354                                            {
355                                                    break;
356                                            }
357                                            line_current -= output_current_row;
358                                            output_current_row = screen_begin_row;
359                                            // screen_end_line = screen_begin_line;
360                                            // prints("\033[T"); // Scroll down 1 line
361                                            output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
362                                            break;
363                                    case CR:
364                                            igetch_reset();
365                                    case KEY_SPACE:
366                                    case KEY_DOWN:
367                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end
368                                            {
369                                                    break;
370                                            }
371                                            line_current += (screen_row_total - (output_current_row - screen_begin_row));
372                                            output_current_row = screen_row_total;
373                                            output_end_row = SCREEN_ROWS - 1;
374                                            moveto(SCREEN_ROWS, 0);
375                                            clrtoeol();
376                                            prints("\033[S"); // Scroll up 1 line
377                                            break;
378                                    case KEY_PGUP:
379                                            if (line_current - output_current_row < 0) // Reach begin
380                                            {
381                                                    break;
382                                            }
383                                            line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
384                                            if (line_current < 0)
385                                            {
386                                                    line_current = 0;
387                                            }
388                                            output_current_row = screen_begin_row;
389                                            output_end_row = SCREEN_ROWS - 1;
390                                            clrline(output_current_row, SCREEN_ROWS);
391                                            break;
392                                    case KEY_PGDN:
393                                            if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end
394                                            {
395                                                    break;
396                                            }
397                                            line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
398                                            if (line_current + screen_row_total > display_line_total) // No enough lines to display
399                                            {
400                                                    line_current = display_line_total - screen_row_total;
401                                            }
402                                            output_current_row = screen_begin_row;
403                                            output_end_row = SCREEN_ROWS - 1;
404                                            clrline(output_current_row, SCREEN_ROWS);
405                                            break;
406                                    case KEY_ESC:
407                                    case KEY_LEFT:
408                                            loop = 0;
409                                            break;
410                                    case 'h':
411                                            if (!show_help) // Not reentrant
412                                            {
413                                                    break;
414                                            }
415                                            // Display help information
416                                            show_help = 0;
417                                            display_file(help_filename, 1);
418                                            show_help = 1;
419                                    case KEY_F5:
420                                            // Refresh after display help information
421                                            line_current -= (output_current_row - screen_begin_row);
422                                            output_current_row = screen_begin_row;
423                                            output_end_row = SCREEN_ROWS - 1;
424                                            clrline(output_current_row, SCREEN_ROWS);
425                                            break;
426                                    case 0: // Refresh bottom line
427                                            break;
428                                    default:
429                                            input_ok = 0;
430                                            break;
431                                    }
432    
433                                    BBS_last_access_tm = time(NULL);
434                            }
435    
436                            continue;
437                    }
438    
439                    len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
440                    if (len >= sizeof(buffer))
441                    {
442                            log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
443                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
444                            len = sizeof(buffer) - 1;
445                    }
446                    else if (len < 0)
447                    {
448                            log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld \n",
449                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
450                            len = 0;
451                    }
452    
453                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
454                    buffer[len] = '\0';
455    
456                    moveto(output_current_row, 0);
457                    clrtoeol();
458                    prints("%s", buffer);
459                    line_current++;
460                    output_current_row++;
461          }          }
       if (!hz)  
         {  
           iflush ();  
         }  
     }  
462    
463    prints ("\r\n");  cleanup:
464    iflush ();          return ch;
465    }
466    
467    return offset;  static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
468    {
469            switch (*p_key)
470            {
471            case 0: // Set msg
472                    snprintf(p_ctx->msg, sizeof(p_ctx->msg),
473                                     "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
474                                     "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
475                                     "帮助[\033[32mh\033[33m] |");
476                    break;
477            }
478    
479            return 0;
480  }  }
481    
482  int  int display_file(const char *filename, int eof_exit)
 display_file(const char* filename)  
483  {  {
484    char buffer[256];          int ret;
485    FILE *fin;          const void *p_shm;
486              size_t data_len;
487    if ((fin = fopen(filename, "r")) != NULL)          long line_total;
488    {          const void *p_data;
489      while (fgets(buffer, 255, fin))          const long *p_line_offsets;
     {  
       prints (buffer);  
       iflush ();  
     }  
     fclose (fin);  
490    
491      return 0;          if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
492    }          {
493                      log_error("get_file_shm(%s) error\n", filename);
494    return -1;                  return KEY_NULL;
495            }
496    
497            if (user_online_update("VIEW_FILE") < 0)
498            {
499                    log_error("user_online_update(VIEW_FILE) error\n");
500            }
501    
502            ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);
503    
504            if (detach_file_shm(p_shm) < 0)
505            {
506                    log_error("detach_file_shm(%s) error\n", filename);
507            }
508    
509            return ret;
510  }  }
511    
512  int  int show_top(const char *str_left, const char *str_middle, const char *str_right)
 show_top()  
513  {  {
514    return 0;          char str_left_f[STR_TOP_LEFT_MAX_LEN + 1];
515            char str_middle_f[STR_TOP_MIDDLE_MAX_LEN + 1];
516            char str_right_f[STR_TOP_RIGHT_MAX_LEN + 1];
517            int str_left_len;
518            int str_middle_len;
519            int str_right_len;
520            int eol;
521            int len;
522    
523            strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
524            str_left_f[sizeof(str_left_f) - 1] = '\0';
525            len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN, &eol, &str_left_len, 1);
526            str_left_f[len] = '\0';
527    
528            strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
529            str_middle_f[sizeof(str_middle_f) - 1] = '\0';
530            len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN, &eol, &str_middle_len, 1);
531            str_middle_f[len] = '\0';
532    
533            strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
534            str_right_f[sizeof(str_right_f) - 1] = '\0';
535            len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN, &eol, &str_right_len, 1);
536            str_right_f[len] = '\0';
537    
538            moveto(1, 0);
539            clrtoeol();
540            prints("\033[1;44;33m%s\033[37m%*s\033[33m%*s\033[m",
541                       str_left_f, 44 - str_left_len, str_middle_f, 36, str_right_f);
542    
543            return 0;
544  }  }
545    
546  int  int show_bottom(const char *msg)
 show_bottom()  
547  {  {
548    return 0;          char str_time[LINE_BUFFER_LEN];
549            time_t time_online;
550            struct tm *tm_online;
551            char msg_f[LINE_BUFFER_LEN];
552            int eol;
553            int msg_len;
554            int len;
555            int len_username;
556            char str_tm_online[LINE_BUFFER_LEN];
557    
558            get_time_str(str_time, sizeof(str_time));
559    
560            msg_f[0] = '\0';
561            msg_len = 0;
562            if (msg != NULL)
563            {
564                    strncpy(msg_f, msg, sizeof(msg_f) - 1);
565                    msg_f[sizeof(msg_f) - 1] = '\0';
566                    len = split_line(msg_f, 23, &eol, &msg_len, 1);
567                    msg_f[len] = '\0';
568            }
569    
570            len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
571    
572            time_online = time(NULL) - BBS_login_tm;
573            tm_online = gmtime(&time_online);
574            if (tm_online->tm_mday > 1)
575            {
576                    snprintf(str_tm_online, sizeof(str_tm_online),
577                                     "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",
578                                     tm_online->tm_mday - 1, tm_online->tm_hour);
579            }
580            else
581            {
582                    snprintf(str_tm_online, sizeof(str_tm_online),
583                                     "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",
584                                     tm_online->tm_hour, tm_online->tm_min);
585            }
586    
587            moveto(SCREEN_ROWS, 0);
588            clrtoeol();
589            prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m][%s\033[33m]\033[m",
590                       str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);
591    
592            return 0;
593  }  }
594    
595  int  int show_active_board()
 press_any_key()  
596  {  {
597    prints ("                       \033[1;33m按任意键盘继续...\033[0;37m                       \r\n");          static int line_current = 0;
598    iflush();          static const void *p_shm = NULL;
599            static size_t data_len;
600            static long line_total;
601            static const void *p_data;
602            static const long *p_line_offsets;
603    
604            static time_t t_last_show = 0;
605            static int line_last = 0;
606    
607            char buffer[LINE_BUFFER_LEN];
608            long int len;
609    
610            if (p_shm == NULL)
611            {
612                    if ((p_shm = get_file_shm_readonly(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
613                    {
614                            log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
615                            return KEY_NULL;
616                    }
617            }
618    
619            if (time(NULL) - t_last_show >= 10)
620            {
621                    line_last = line_current;
622                    t_last_show = time(NULL);
623            }
624            else
625            {
626                    line_current = line_last;
627            }
628    
629            clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
630    
631            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
632            {
633                    len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
634                    if (len >= LINE_BUFFER_LEN)
635                    {
636                            log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
637                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
638                            len = LINE_BUFFER_LEN - 1;
639                    }
640    
641                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
642                    buffer[len] = '\0';
643    
644                    moveto(3 + i, 0);
645                    prints("%s", buffer);
646    
647                    line_current++;
648                    if (line_current >= line_total)
649                    {
650                            line_current = 0;
651                            break;
652                    }
653            }
654    
655    return igetch ();          return 0;
656  }  }


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

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