/[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.7 by sysadm, Tue Nov 30 07:10:32 2004 UTC Revision 1.45 by sysadm, Sun May 11 11:33:44 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"  #include "common.h"
19    #include "str_process.h"
20    #include "log.h"
21  #include "io.h"  #include "io.h"
22    #include "screen.h"
23    #include <string.h>
24    #include <ctype.h>
25    #include <unistd.h>
26    #include <stdlib.h>
27  #include <sys/types.h>  #include <sys/types.h>
28  #include <sys/stat.h>  #include <sys/stat.h>
29  #include <unistd.h>  #include <sys/param.h>
30    
31  int screen_lines = 24;  #define ACTIVE_BOARD_HEIGHT 8
32    
33  void  int screen_rows = 24;
34  moveto (int row, int col)  int screen_cols = 80;
35    
36    void moveto(int row, int col)
37  {  {
38    if (row >= 0)          if (row >= 0)
39      {          {
40        prints ("\033[%d;%dH", row, col);                  prints("\033[%d;%dH", row, col);
41      }          }
42    else          else
43      {          {
44        prints ("\r");                  prints("\r");
45      }          }
   iflush ();  
46  }  }
47    
48  void  void clrtoeol()
 clrtoeol ()  
49  {  {
50    prints ("\033[K");          prints("\033[K");
   iflush ();  
51  }  }
52    
53  void  void clrline(int line_begin, int line_end)
 clrline (int line_begin, int line_end)  
54  {  {
55    int i;          int i;
   
   for (i = line_begin; i <= line_end; i++)  
     {  
       moveto (i, 0);  
       prints ("\033[K");  
     }  
56    
57    iflush ();          for (i = line_begin; i <= line_end; i++)
58            {
59                    moveto(i, 0);
60                    prints("\033[K");
61            }
62  }  }
63    
64  void  void clrtobot(int line_begin)
 clearscr ()  
65  {  {
66    prints ("\33[2J");          moveto(line_begin, 0);
67    moveto (0, 0);          prints("\033[J");
68    iflush ();          moveto(line_begin, 0);
69  }  }
70    
71  int  void clearscr()
 press_any_key ()  
72  {  {
73    moveto (screen_lines, 0);          prints("\033[2J");
74    clrtoeol ();          moveto(0, 0);
   
   prints ("                           \033[1;33m按任意键继续...\033[0;37m");  
   iflush ();  
   
   return igetch ();  
75  }  }
76    
77  void  int press_any_key()
 set_input_echo(int echo)  
78  {  {
79    char temp[256];          moveto(screen_rows, 0);
80            clrtoeol();
81    
82    if (echo)          prints("                           \033[1;33m按任意键继续...\033[0;37m");
83    {          iflush();
84      outc ('\x83'); // ASCII code 131  
85      iflush ();          return igetch_t(MIN(MAX_DELAY_TIME, 60));
   }  
   else  
   {  
 //    outc ('\x85'); // ASCII code 133  
     prints ("\xff\xfb\x01\xff\xfb\x03");  
     iflush ();  
     igetch ();  
     igetch ();  
   }  
86  }  }
87    
88  int  void set_input_echo(int echo)
 str_input (char *buffer, int buffer_length, int echo_mode)  
89  {  {
90    char buf[256], ch;          if (echo)
   int c, offset = 0, len, loop = 1, i, hz = 0;  
   
   memset (buffer, '\0', buffer_length);  
   
   while (c = igetch ())  
     {  
       if (c == CR)  
         break;  
       if (c == LF)  
         continue;  
       if (c == BACKSPACE)  
91          {          {
92            if (offset > 0)                  outc('\x83'); // ASCII code 131
93              {                  iflush();
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
94          }          }
95        if (c > 255 || iscntrl (c))          else
96          {          {
97            continue;                  //    outc ('\x85'); // ASCII code 133
98                    prints("\xff\xfb\x01\xff\xfb\x03");
99                    iflush();
100                    igetch(0);
101                    igetch(1);
102          }          }
103        if (c > 127 && c <= 255)  }
104          {  
105            hz = (!hz);  static int _str_input(char *buffer, int buf_size, int echo_mode)
106          }  {
107        if (offset >= buffer_length)          int c;
108          {          int offset = 0;
109            outc ('\a');          int hz = 0;
110            continue;  
111          }          buffer[buf_size - 1] = '\0';
112        buffer[offset++] = (char) c;          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)
113        buffer[offset] = '\0';                  ;
114        switch (echo_mode)  
115          {          while (!SYS_server_exit && (c = igetch_t(MIN(MAX_DELAY_TIME, 60))))
         case 0:  
           outc ((char) c);  
           break;  
         case 1:  
           outc ('*');  
           break;  
         }  
       if (!hz)  
116          {          {
117            iflush ();                  if (c == CR)
118          }                  {
119      }                          igetch(1); // Cleanup remaining '\n' in the buffer
120                            break;
121                    }
122                    else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe
123                    {
124                            return -1;
125                    }
126                    else if (c == LF || c == '\0')
127                    {
128                            continue;
129                    }
130                    else if (c == BACKSPACE)
131                    {
132                            if (offset > 0)
133                            {
134                                    offset--;
135                                    if (buffer[offset] < 0 || buffer[offset] > 127)
136                                    {
137                                            prints("\033[D \033[D");
138                                            offset--;
139                                            if (offset < 0) // should not happen
140                                            {
141                                                    log_error("Offset of buffer is negative\n");
142                                                    offset = 0;
143                                            }
144                                    }
145                                    buffer[offset] = '\0';
146                                    prints("\033[D \033[D");
147                                    iflush();
148                            }
149                            continue;
150                    }
151                    else if (c > 255 || iscntrl(c))
152                    {
153                            continue;
154                    }
155                    else if (c > 127 && c <= 255)
156                    {
157                            if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character
158                            {
159                                    igetch(1); // Cleanup remaining input
160                                    outc('\a');
161                                    iflush();
162                                    continue;
163                            }
164                            hz = (!hz);
165                    }
166    
167    prints ("\r\n");                  if (offset + 1 > buf_size - 1)
168    iflush ();                  {
169                            outc('\a');
170                            iflush();
171                            continue;
172                    }
173    
174                    buffer[offset++] = (char)c;
175                    buffer[offset] = '\0';
176    
177                    switch (echo_mode)
178                    {
179                    case DOECHO:
180                            outc((char)c);
181                            break;
182                    case NOECHO:
183                            outc('*');
184                            break;
185                    }
186                    if (!hz)
187                    {
188                            iflush();
189                    }
190            }
191    
192    return offset;          return offset;
193  }  }
194    
195  int  int str_input(char *buffer, int buf_size, int echo_mode)
196  display_file (const char *filename)  {
197            int len;
198    
199            buffer[0] = '\0';
200    
201            len = _str_input(buffer, buf_size, echo_mode);
202    
203            prints("\r\n");
204            iflush();
205    
206            return len;
207    };
208    
209    int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int echo_mode)
210  {  {
211    char buffer[260];          int len;
212    FILE *fin;  
213    int i;          igetch(1); // Cleanup input buffer
214    
215            moveto(row, col);
216            prints(prompt);
217            prints(buffer);
218            iflush();
219    
220            len = _str_input(buffer, buf_size, echo_mode);
221    
222    if ((fin = fopen (filename, "r")) != NULL)          return len;
   {  
     while (fgets (buffer, 255, fin))  
       {  
         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 ();  
       }  
     fclose(fin);  
     return 0;  
   }  
     
   return -1;  
223  }  }
224    
225  int  int display_file(const char *filename)
 display_file_ex (const char *filename, int begin_line, int wait)  
226  {  {
227    char buffer[260], temp[256];          char buffer[LINE_BUFFER_LEN];
228    int i, ch, input_ok;          FILE *fin;
229    long int line, c_line_begin = 0, c_line_total=0;          size_t i;
   long int f_line, f_size, f_offset;  
   FILE *fin;  
   struct stat f_stat;  
230    
231    clrline (begin_line, screen_lines);          if ((fin = fopen(filename, "r")) == NULL)
232    line = begin_line;          {
233    moveto (line, 0);                  return -1;
234            }
235    
236    if ((fin = fopen (filename, "r")) != NULL)          while (fgets(buffer, sizeof(buffer) - 1, fin))
     {  
       if (fstat (fileno (fin), &f_stat) != 0)  
237          {          {
238            log_error ("Get file stat failed\n");                  i = strnlen(buffer, sizeof(buffer) - 1);
239            return -1;                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
240                    {
241                            buffer[i - 1] = '\r';
242                            buffer[i] = '\n';
243                            buffer[i + 1] = '\0';
244                    }
245                    prints(buffer);
246                    iflush();
247          }          }
248        f_size = f_stat.st_size;          fclose(fin);
249    
250        while (fgets (buffer, 255, fin))          return 0;
251          c_line_total ++;  }
252        rewind (fin);  
253    int display_file_ex(const char *filename, int begin_line, int wait)
254    {
255            static int show_help = 1;
256            char buffer[LINE_BUFFER_LEN];
257            int ch = 0;
258            int input_ok, line, max_lines;
259            long int c_line_current = 0;
260            long int c_line_total = 0;
261            FILE *fin;
262            long *p_line_offsets;
263            long int len;
264            long int percentile;
265            int loop = 1;
266    
267        while (fgets (buffer, 255, fin))          if ((fin = fopen(filename, "r")) == NULL)
268          {          {
269            if (line >= screen_lines)                  log_error("Unable to open file %s\n", filename);
270              {                  return -1;
271                f_offset = ftell (fin);          }
272    
273                moveto (screen_lines, 0);          p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
               prints  
                 ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",  
                  (f_offset-strlen(buffer)) * 100 / f_size);  
               iflush ();  
274    
275                input_ok = 0;          c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
276                while (!input_ok)  
277            clrline(begin_line, screen_rows);
278            line = begin_line;
279            max_lines = screen_rows - 1;
280    
281            while (!SYS_server_exit && loop)
282            {
283                    if (c_line_current >= c_line_total && c_line_total <= screen_rows - 2)
284                  {                  {
285                    ch = igetch ();                          if (wait)
                   input_ok = 1;  
                   switch (ch)  
                     {  
                     case KEY_UP:  
                       c_line_begin--;  
                       if (c_line_begin >= 0)  
286                          {                          {
287                            rewind (fin);                                  ch = press_any_key();
                           for (f_line = 0; f_line < c_line_begin; f_line++)  
                             {  
                               if (fgets (buffer, 255, fin) == NULL)  
                                 goto exit;  
                             }  
288                          }                          }
289                        else                          else
290                          {                          {
291                            goto exit;                                  iflush();
292                          }                          }
293                        break;  
294                      case KEY_DOWN:                          loop = 0;
295                      case CR:                          break;
296                        c_line_begin++;                  }
297                        rewind (fin);  
298                        for (f_line = 0; f_line < c_line_begin; f_line++)                  if (c_line_current >= c_line_total || line >= max_lines)
299                          {                  {
300                            if (fgets (buffer, 255, fin) == NULL)                          if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total)
                             goto exit;  
                         }  
                       break;  
                     case KEY_PGUP:  
                     case Ctrl ('B'):  
                       if (c_line_begin > 0)  
                         c_line_begin -= (screen_lines - begin_line - 1);  
                       else  
                         goto exit;  
                       if (c_line_begin < 0)  
                         c_line_begin = 0;  
                       rewind (fin);  
                       for (f_line = 0; f_line < c_line_begin; f_line++)  
301                          {                          {
302                            if (fgets (buffer, 255, fin) == NULL)                                  percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
                             goto exit;  
303                          }                          }
304                        break;                          else
                     case KEY_RIGHT:  
                     case KEY_PGDOWN:  
                     case Ctrl ('F'):  
                     case KEY_SPACE:  
                       c_line_begin += (screen_lines - begin_line - 1);  
                       if (c_line_begin + (screen_lines - begin_line) > c_line_total)  
                         c_line_begin = c_line_total - (screen_lines - begin_line);  
                       rewind (fin);  
                       for (f_line = 0; f_line < c_line_begin; f_line++)  
305                          {                          {
306                            if (fgets (buffer, 255, fin) == NULL)                                  percentile = 100;
                             goto exit;  
307                          }                          }
308                        break;  
309                      case KEY_LEFT:                          moveto(screen_rows, 0);
310                      case 'q':                          prints("\033[1;44;32m%s (%d%%)%s\033[33m          │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
311                      case 'Q':                                     (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
312                        goto exit;                                     (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
313                        break;                          iflush();
314                      case '?':  
315                      case 'h':                          input_ok = 0;
316                      case 'H':                          while (!SYS_server_exit && !input_ok)
                       //Display help information  
                       strcpy (temp, app_home_dir);  
                       strcat (temp, "data/read_help.txt");  
                       display_file_ex (temp, begin_line, 1);  
   
                       //Refresh after display help information  
                       rewind (fin);  
                       for (f_line = 0; f_line < c_line_begin; f_line++)  
317                          {                          {
318                            if (fgets (buffer, 255, fin) == NULL)                                  ch = igetch_t(MAX_DELAY_TIME);
319                              goto exit;                                  input_ok = 1;
320                                    switch (ch)
321                                    {
322                                    case KEY_TIMEOUT:
323                                            goto cleanup;
324                                    case KEY_UP:
325                                            if (c_line_current - line < 0) // Reach top
326                                            {
327                                                    break;
328                                            }
329                                            c_line_current -= line;
330                                            line = begin_line;
331                                            // max_lines = begin_line + 1;
332                                            // prints("\033[T"); // Scroll down 1 line
333                                            max_lines = screen_rows - 1; // Legacy Fterm only works with this line
334                                            break;
335                                    case KEY_DOWN:
336                                    case CR:
337                                            if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
338                                            {
339                                                    break;
340                                            }
341                                            c_line_current += ((screen_rows - 2) - (line - 1));
342                                            line = screen_rows - 2;
343                                            max_lines = screen_rows - 1;
344                                            moveto(screen_rows, 0);
345                                            clrtoeol();
346                                            prints("\033[S"); // Scroll up 1 line
347                                            break;
348                                    case KEY_PGUP:
349                                    case Ctrl('B'):
350                                            if (c_line_current - line < 0) // Reach top
351                                            {
352                                                    break;
353                                            }
354                                            c_line_current -= ((screen_rows - 3) + (line - 1));
355                                            if (c_line_current < 0)
356                                            {
357                                                    c_line_current = 0;
358                                            }
359                                            line = begin_line;
360                                            max_lines = screen_rows - 1;
361                                            clrline(begin_line, screen_rows);
362                                            break;
363                                    case KEY_RIGHT:
364                                    case KEY_PGDN:
365                                    case Ctrl('F'):
366                                    case KEY_SPACE:
367                                            if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom
368                                            {
369                                                    break;
370                                            }
371                                            c_line_current += (screen_rows - 3) - (line - 1);
372                                            if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display
373                                            {
374                                                    c_line_current = c_line_total - (screen_rows - 2);
375                                            }
376                                            line = begin_line;
377                                            max_lines = screen_rows - 1;
378                                            clrline(begin_line, screen_rows);
379                                            break;
380                                    case KEY_LEFT:
381                                    case 'q':
382                                    case 'Q':
383                                            loop = 0;
384                                            break;
385                                    case '?':
386                                    case 'h':
387                                    case 'H':
388                                            if (!show_help)
389                                            {
390                                                    break;
391                                            }
392    
393                                            // Display help information
394                                            show_help = 0;
395                                            display_file_ex(DATA_READ_HELP, begin_line, 1);
396                                            show_help = 1;
397    
398                                            // Refresh after display help information
399                                            c_line_current -= (line - 1);
400                                            line = begin_line;
401                                            max_lines = screen_rows - 1;
402                                            clrline(begin_line, screen_rows);
403                                            break;
404                                    default:
405                                            input_ok = 0;
406                                            break;
407                                    }
408    
409                                    BBS_last_access_tm = time(0);
410                          }                          }
411                        break;  
412                      default:                          continue;
                       input_ok = 0;  
                       break;  
                     }  
413                  }                  }
414    
415                clrline (begin_line, screen_lines);                  fseek(fin, p_line_offsets[c_line_current], SEEK_SET);
416                line = begin_line;                  len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
417                moveto (line, 0);                  if (len >= LINE_BUFFER_LEN)
418                    {
419                            log_error("Error length exceeds buffer size: %d\n", len);
420                            len = LINE_BUFFER_LEN - 1;
421                    }
422                    if (fgets(buffer, (int)len + 1, fin) == NULL)
423                    {
424                            log_error("Reach EOF\n");
425                            break;
426                    }
427                    moveto(line, 0);
428                    clrtoeol();
429                    prints("%s", buffer);
430                    c_line_current++;
431                    line++;
432            }
433    
434    cleanup:
435            free(p_line_offsets);
436            fclose(fin);
437    
438                continue;          return ch;
439              }  }
440    
441            i = strlen (buffer);  int show_top(char *status)
442            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  {
443              {          int end_of_line;
444                buffer[i - 1] = '\r';          int display_len;
445                buffer[i] = '\n';          unsigned int len;
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
446    
447            line++;          char space1[LINE_BUFFER_LEN];
448            char space2[LINE_BUFFER_LEN];
449    
450            len = split_line(status, 20, &end_of_line, &display_len);
451            if (end_of_line)
452            {
453                    status[len] = '\0';
454          }          }
455        if (wait)          str_space(space1, 31 - display_len);
         ch = press_any_key();  
       else  
         ch = 0;  
456    
457      exit:          len = split_line(BBS_current_section_name, 20, &end_of_line, &display_len);
458        fclose (fin);          if (end_of_line)
459            {
460                    status[len] = '\0';
461            }
462            str_space(space2, 30 - display_len);
463    
464        return ch;          moveto(1, 0);
465      }          clrtoeol();
466            prints("\033[1;44;33m%s \033[37m%s%s%s\033[33m 讨论区 [%s]\033[m",
467                       status, space1, BBS_name, space2, BBS_current_section_name);
468            iflush();
469    
470    return -1;          return 0;
471  }  }
472    
473  int  int show_bottom(char *msg)
 show_top ()  
474  {  {
475    return 0;          char str_time[LINE_BUFFER_LEN];
476            char space[LINE_BUFFER_LEN];
477            time_t time_online;
478            struct tm *tm_online;
479    
480            get_time_str(str_time, sizeof(str_time));
481            str_space(space, 34 - (int)strnlen(BBS_username, sizeof(BBS_username)));
482    
483            time_online = time(0) - BBS_login_tm;
484            tm_online = gmtime(&time_online);
485    
486            moveto(screen_rows, 0);
487            clrtoeol();
488            prints("\033[1;44;33m[\033[36m%s\033[33m]%s帐号[\033[36m%s\033[33m]"
489                       "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
490                       str_time, space, BBS_username, tm_online->tm_mday - 1,
491                       tm_online->tm_hour, tm_online->tm_min);
492            iflush();
493    
494            return 0;
495  }  }
496    
497  int  int show_active_board()
 show_bottom ()  
498  {  {
499    return 0;          char filename[FILE_PATH_LEN];
500            char buffer[LINE_BUFFER_LEN];
501            FILE *fin;
502            static int line;
503            unsigned int len;
504            int end_of_line;
505            int display_len;
506    
507            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
508    
509            if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)
510            {
511                    log_error("Unable to open file %s\n", filename);
512                    return -1;
513            }
514    
515            for (int i = 0; i < line; i++)
516            {
517                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
518                    {
519                            line = 0;
520                            rewind(fin);
521                            break;
522                    }
523            }
524    
525            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
526            {
527                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
528                    {
529                            line = 0;
530                            break;
531                    }
532                    line++;
533                    len = split_line(buffer, screen_cols, &end_of_line, &display_len);
534                    buffer[len] = '\0'; // Truncate over-length line
535                    moveto(3 + i, 0);
536                    prints("%s", buffer);
537            }
538            iflush();
539    
540            fclose(fin);
541    
542            return 0;
543  }  }


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

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