/[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.39 by sysadm, Tue May 6 13:34:28 2025 UTC Revision 1.52 by sysadm, Thu May 15 14:17:53 2025 UTC
# Line 20  Line 20 
20  #include "log.h"  #include "log.h"
21  #include "io.h"  #include "io.h"
22  #include "screen.h"  #include "screen.h"
23    #include <fcntl.h>
24  #include <string.h>  #include <string.h>
25  #include <ctype.h>  #include <ctype.h>
 #include <sys/types.h>  
 #include <sys/stat.h>  
26  #include <unistd.h>  #include <unistd.h>
27  #include <stdlib.h>  #include <stdlib.h>
28    #include <errno.h>
29    #include <sys/types.h>
30    #include <sys/stat.h>
31    #include <sys/param.h>
32    #include <sys/mman.h>
33    
34  #define ACTIVE_BOARD_HEIGHT 8  #define ACTIVE_BOARD_HEIGHT 8
35    
# Line 81  int press_any_key() Line 85  int press_any_key()
85          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints("                           \033[1;33m按任意键继续...\033[0;37m");
86          iflush();          iflush();
87    
88          return igetch_t(60);          return igetch_t(MIN(MAX_DELAY_TIME, 60));
89  }  }
90    
91  void set_input_echo(int echo)  void set_input_echo(int echo)
# Line 97  void set_input_echo(int echo) Line 101  void set_input_echo(int echo)
101                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
102                  iflush();                  iflush();
103                  igetch(0);                  igetch(0);
104                  igetch(1);                  igetch_reset();
105          }          }
106  }  }
107    
# Line 108  static int _str_input(char *buffer, int Line 112  static int _str_input(char *buffer, int
112          int hz = 0;          int hz = 0;
113    
114          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
115          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++);          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)
116                    ;
117    
118            igetch_reset();
119    
120          while ((c = igetch_t(60)))          while (!SYS_server_exit)
121          {          {
122                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));
123    
124                    if (c == CR)
125                  {                  {
126                          igetch(1); // Cleanup remaining '\n' in the buffer                          igetch_reset();
127                          break;                          break;
128                  }                  }
129                  if (c == LF)                  else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe
130                    {
131                            return -1;
132                    }
133                    else if (c == LF || c == '\0')
134                  {                  {
135                          continue;                          continue;
136                  }                  }
137                  if (c == BACKSPACE)                  else if (c == BACKSPACE)
138                  {                  {
139                          if (offset > 0)                          if (offset > 0)
140                          {                          {
# Line 142  static int _str_input(char *buffer, int Line 155  static int _str_input(char *buffer, int
155                          }                          }
156                          continue;                          continue;
157                  }                  }
158                  if (c > 255 || iscntrl(c))                  else if (c > 255 || iscntrl(c))
159                  {                  {
160                          continue;                          continue;
161                  }                  }
162                  if (c > 127 && c <= 255)                  else if (c > 127 && c <= 255)
163                  {                  {
164                          if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character                          if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character
165                          {                          {
166                                  igetch(1); // Cleanup remaining input                                  igetch(0); // Ignore 1 character
167                                  outc('\a');                                  outc('\a');
168                                  iflush();                                  iflush();
169                                  continue;                                  continue;
170                          }                          }
171                          hz = (!hz);                          hz = (!hz);
172                  }                  }
173    
174                  if (offset + 1 > buf_size - 1)                  if (offset + 1 > buf_size - 1)
175                  {                  {
176                          outc('\a');                          outc('\a');
177                          iflush();                          iflush();
178                          continue;                          continue;
179                  }                  }
180    
181                  buffer[offset++] = (char)c;                  buffer[offset++] = (char)c;
182                  buffer[offset] = '\0';                  buffer[offset] = '\0';
183    
184                  switch (echo_mode)                  switch (echo_mode)
185                  {                  {
186                  case DOECHO:                  case DOECHO:
# Line 201  int get_data(int row, int col, char *pro Line 217  int get_data(int row, int col, char *pro
217  {  {
218          int len;          int len;
219    
         igetch(1); // Cleanup input buffer  
   
220          moveto(row, col);          moveto(row, col);
221          prints(prompt);          prints(prompt);
222          prints(buffer);          prints(buffer);
# Line 213  int get_data(int row, int col, char *pro Line 227  int get_data(int row, int col, char *pro
227          return len;          return len;
228  }  }
229    
 int display_file(const char *filename)  
 {  
         char buffer[LINE_BUFFER_LEN];  
         FILE *fin;  
         size_t i;  
   
         if ((fin = fopen(filename, "r")) == NULL)  
         {  
                 return -1;  
         }  
   
         while (fgets(buffer, sizeof(buffer) - 1, fin))  
         {  
                 i = strnlen(buffer, sizeof(buffer) - 1);  
                 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;  
 }  
   
230  int display_file_ex(const char *filename, int begin_line, int wait)  int display_file_ex(const char *filename, int begin_line, int wait)
231  {  {
232          static int show_help = 1;          static int show_help = 1;
233          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
234            void *p_data;
235          int ch = 0;          int ch = 0;
236          int input_ok, line, max_lines;          int input_ok, line, max_lines;
237          long int c_line_current = 0;          long int c_line_current = 0;
238          long int c_line_total = 0;          long int c_line_total = 0;
239          FILE *fin;          int fd;
240            struct stat sb;
241          long *p_line_offsets;          long *p_line_offsets;
242          long int len;          long int len;
243          long int percentile;          long int percentile;
244          int loop = 1;          int loop = 1;
245    
246          if ((fin = fopen(filename, "r")) == NULL)          if ((fd = open(filename, O_RDONLY)) < 0)
247          {          {
248                  log_error("Unable to open file %s\n", filename);                  log_error("open(%s) error (%d)\n", filename, errno);
249                    return -1;
250            }
251    
252            if (fstat(fd, &sb) < 0)
253            {
254                    log_error("fstat(fd) error (%d)\n", errno);
255                    return -1;
256            }
257    
258            p_data = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0L);
259            if (p_data == MAP_FAILED)
260            {
261                    log_error("mmap() error (%d)\n", errno);
262                    return -2;
263            }
264    
265            if (close(fd) < 0)
266            {
267                    log_error("close(fd) error (%d)\n", errno);
268                  return -1;                  return -1;
269          }          }
270    
271          p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);          p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
272    
273          c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);          c_line_total = split_data_lines(p_data, screen_cols, p_line_offsets, MAX_FILE_LINES);
274    
275          clrline(begin_line, screen_rows);          clrline(begin_line, screen_rows);
276          line = begin_line;          line = begin_line;
277          max_lines = screen_rows - 1;          max_lines = screen_rows - 1;
278    
279          while (loop)          while (!SYS_server_exit && loop)
280          {          {
281                  if (c_line_current >= c_line_total && c_line_total <= screen_rows - 2)                  if (c_line_current >= c_line_total && c_line_total <= screen_rows - 2)
282                  {                  {
# Line 298  int display_file_ex(const char *filename Line 305  int display_file_ex(const char *filename
305                          }                          }
306    
307                          moveto(screen_rows, 0);                          moveto(screen_rows, 0);
308                          prints("\033[1;44;32m%s (%d%%)%s\033[33m  │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",                          prints("\033[1;44;32m%s (%d%%)%s\033[33m          │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
309                                     (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,                                     (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
310                                     (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));                                     (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
311                          iflush();                          iflush();
312    
313                          input_ok = 0;                          input_ok = 0;
314                          while (!input_ok)                          while (!SYS_server_exit && !input_ok)
315                          {                          {
316                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(MAX_DELAY_TIME);
317                                  input_ok = 1;                                  input_ok = 1;
318                                  switch (ch)                                  switch (ch)
319                                  {                                  {
320                                    case KEY_NULL:
321                                    case KEY_TIMEOUT:
322                                            goto cleanup;
323                                    case KEY_HOME:
324                                            c_line_current = 0;
325                                            line = begin_line;
326                                            max_lines = screen_rows - 1;
327                                            clrline(begin_line, screen_rows);
328                                            break;
329                                    case KEY_END:
330                                            c_line_current = c_line_total - (screen_rows - 2);
331                                            line = begin_line;
332                                            max_lines = screen_rows - 1;
333                                            clrline(begin_line, screen_rows);
334                                            break;
335                                  case KEY_UP:                                  case KEY_UP:
336                                          if (c_line_current - line < 0) // Reach top                                          if (c_line_current - line < 0) // Reach top
337                                          {                                          {
# Line 321  int display_file_ex(const char *filename Line 343  int display_file_ex(const char *filename
343                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
344                                          max_lines = screen_rows - 1; // Legacy Fterm only works with this line                                          max_lines = screen_rows - 1; // Legacy Fterm only works with this line
345                                          break;                                          break;
                                 case KEY_DOWN:  
346                                  case CR:                                  case CR:
347                                            igetch_reset();
348                                    case KEY_DOWN:
349                                          if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom                                          if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
350                                          {                                          {
351                                                  break;                                                  break;
# Line 366  int display_file_ex(const char *filename Line 389  int display_file_ex(const char *filename
389                                          max_lines = screen_rows - 1;                                          max_lines = screen_rows - 1;
390                                          clrline(begin_line, screen_rows);                                          clrline(begin_line, screen_rows);
391                                          break;                                          break;
                                 case KEY_NULL:  
                                 case KEY_TIMEOUT:  
392                                  case KEY_LEFT:                                  case KEY_LEFT:
393                                  case 'q':                                  case 'q':
394                                  case 'Q':                                  case 'Q':
# Line 393  int display_file_ex(const char *filename Line 414  int display_file_ex(const char *filename
414                                          clrline(begin_line, screen_rows);                                          clrline(begin_line, screen_rows);
415                                          break;                                          break;
416                                  default:                                  default:
417                                            log_std("Input: %d\n", ch);
418                                          input_ok = 0;                                          input_ok = 0;
419                                          break;                                          break;
420                                  }                                  }
421    
422                                    BBS_last_access_tm = time(0);
423                          }                          }
424    
425                          continue;                          continue;
426                  }                  }
427    
                 fseek(fin, p_line_offsets[c_line_current], SEEK_SET);  
428                  len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];                  len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
429                  if (len >= LINE_BUFFER_LEN)                  if (len >= LINE_BUFFER_LEN)
430                  {                  {
431                          log_error("Error length exceeds buffer size: %d\n", len);                          log_error("Error length exceeds buffer size: %d\n", len);
432                          len = LINE_BUFFER_LEN - 1;                          len = LINE_BUFFER_LEN - 1;
433                  }                  }
434                  if (fgets(buffer, (int)len + 1, fin) == NULL)  
435                  {                  memcpy(buffer, (const char *)p_data + p_line_offsets[c_line_current], (size_t)len);
436                          log_error("Reach EOF\n");                  buffer[len] = '\0';
437                          break;  
                 }  
438                  moveto(line, 0);                  moveto(line, 0);
439                  clrtoeol();                  clrtoeol();
440                  prints("%s", buffer);                  prints("%s", buffer);
# Line 420  int display_file_ex(const char *filename Line 442  int display_file_ex(const char *filename
442                  line++;                  line++;
443          }          }
444    
445    cleanup:
446            if (munmap(p_data, (size_t)sb.st_size) < 0)
447            {
448                    log_error("munmap() error (%d)\n", errno);
449            }
450    
451          free(p_line_offsets);          free(p_line_offsets);
         fclose(fin);  
452    
453          return ch;          return ch;
454  }  }
455    
456  int show_top(char *status)  int show_top(char *status)
457  {  {
458          char buffer[LINE_BUFFER_LEN];          int end_of_line;
459            int display_len;
460            int len;
461    
462          str_space(buffer, 20 - (int)strnlen(BBS_current_section_name, sizeof(BBS_current_section_name)));          char space1[LINE_BUFFER_LEN];
463            char space2[LINE_BUFFER_LEN];
464    
465            len = split_line(status, 20, &end_of_line, &display_len);
466            if (end_of_line)
467            {
468                    status[len] = '\0';
469            }
470            str_space(space1, 31 - display_len);
471    
472            len = split_line(BBS_current_section_name, 20, &end_of_line, &display_len);
473            if (end_of_line)
474            {
475                    status[len] = '\0';
476            }
477            str_space(space2, 30 - display_len);
478    
479          moveto(1, 0);          moveto(1, 0);
480          clrtoeol();          clrtoeol();
481          prints("\033[1;44;33m%-20s \033[37m%20s"          prints("\033[1;44;33m%s \033[37m%s%s%s\033[33m 讨论区 [%s]\033[m",
482                     "         %s\033[33m讨论区 [%s]\033[m",                     status, space1, BBS_name, space2, BBS_current_section_name);
                    status, BBS_name, buffer, BBS_current_section_name);  
483          iflush();          iflush();
484    
485          return 0;          return 0;
# Line 445  int show_top(char *status) Line 488  int show_top(char *status)
488  int show_bottom(char *msg)  int show_bottom(char *msg)
489  {  {
490          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
491          char buffer[LINE_BUFFER_LEN];          char space[LINE_BUFFER_LEN];
492          time_t time_online;          time_t time_online;
493          struct tm *tm_online;          struct tm *tm_online;
494    
495          get_time_str(str_time, sizeof(str_time));          get_time_str(str_time, sizeof(str_time));
496          str_space(buffer, 33 - (int)strnlen(BBS_username, sizeof(BBS_username)));          str_space(space, 34 - (int)strnlen(BBS_username, sizeof(BBS_username)));
497    
498          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
499          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
500    
501          moveto(screen_rows, 0);          moveto(screen_rows, 0);
502          clrtoeol();          clrtoeol();
503          prints("\033[1;44;33m[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]%s帐号[\033[36m%s\033[33m]"
                    "%s帐号[\033[36m%s\033[33m]"  
504                     "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",                     "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
505                     str_time, buffer, BBS_username, tm_online->tm_mday - 1,                     str_time, space, BBS_username, tm_online->tm_mday - 1,
506                     tm_online->tm_hour, tm_online->tm_min);                     tm_online->tm_hour, tm_online->tm_min);
507          iflush();          iflush();
508    
# Line 473  int show_active_board() Line 515  int show_active_board()
515          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
516          FILE *fin;          FILE *fin;
517          static int line;          static int line;
518          unsigned int len;          int len;
519          int end_of_line;          int end_of_line;
520            int display_len;
521    
522          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
523    
# Line 502  int show_active_board() Line 545  int show_active_board()
545                          break;                          break;
546                  }                  }
547                  line++;                  line++;
548                  len = split_line(buffer, screen_cols, &end_of_line);                  len = split_line(buffer, screen_cols, &end_of_line, &display_len);
549                  buffer[len] = '\0'; // Truncate over-length line                  buffer[len] = '\0'; // Truncate over-length line
550                  moveto(3 + i, 0);                  moveto(3 + i, 0);
551                  prints("%s", buffer);                  prints("%s", buffer);


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

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