/[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.54 by sysadm, Fri May 16 14:09:31 2025 UTC Revision 1.69 by sysadm, Sun May 25 06:55:23 2025 UTC
# Line 30  Line 30 
30  #include <sys/types.h>  #include <sys/types.h>
31  #include <sys/stat.h>  #include <sys/stat.h>
32  #include <sys/param.h>  #include <sys/param.h>
33  #include <sys/mman.h>  #include <sys/shm.h>
34    
35  #define ACTIVE_BOARD_HEIGHT 8  #define ACTIVE_BOARD_HEIGHT 8
36    
# Line 48  void moveto(int row, int col) Line 48  void moveto(int row, int col)
48    
49  void clrtoeol()  void clrtoeol()
50  {  {
51          prints("\033[K");          prints(CTRL_SEQ_CLR_LINE);
52  }  }
53    
54  void clrline(int line_begin, int line_end)  void clrline(int line_begin, int line_end)
# Line 58  void clrline(int line_begin, int line_en Line 58  void clrline(int line_begin, int line_en
58          for (i = line_begin; i <= line_end; i++)          for (i = line_begin; i <= line_end; i++)
59          {          {
60                  moveto(i, 0);                  moveto(i, 0);
61                  prints("\033[K");                  prints(CTRL_SEQ_CLR_LINE);
62          }          }
63  }  }
64    
# Line 232  int display_file_ex(const char *filename Line 232  int display_file_ex(const char *filename
232          int ch = KEY_NULL;          int ch = KEY_NULL;
233          int input_ok, line, max_lines;          int input_ok, line, max_lines;
234          long int line_current = 0;          long int line_current = 0;
235          const FILE_MMAP *p_file_mmap;          const void *p_shm;
236            size_t data_len;
237            long line_total;
238            const void *p_data;
239            const long *p_line_offsets;
240          long int len;          long int len;
241          long int percentile;          long int percentile;
242          int loop;          int loop;
243    
244          if ((p_file_mmap = get_file_mmap(filename)) == NULL)          if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
245          {          {
246                  if (load_file_mmap(filename) < 0)                  log_error("get_file_shm(%s) error\n", filename);
247                  {                  return KEY_NULL;
                         log_error("load_file_mmap(%s) error\n", filename);  
                         return KEY_NULL;  
                 }  
   
                 if ((p_file_mmap = get_file_mmap(filename)) == NULL)  
                 {  
                         log_error("get_file_mmap(%s) error\n", filename);  
                         return KEY_NULL;  
                 }  
248          }          }
249    
250          clrline(begin_line, SCREEN_ROWS);          clrline(begin_line, SCREEN_ROWS);
# Line 259  int display_file_ex(const char *filename Line 254  int display_file_ex(const char *filename
254          loop = 1;          loop = 1;
255          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
256          {          {
257                  if (line_current >= p_file_mmap->line_total && p_file_mmap->line_total <= SCREEN_ROWS - 2)                  if (line_current >= line_total && line_total <= SCREEN_ROWS - 2)
258                  {                  {
259                          if (wait)                          if (wait)
260                          {                          {
# Line 274  int display_file_ex(const char *filename Line 269  int display_file_ex(const char *filename
269                          break;                          break;
270                  }                  }
271    
272                  if (line_current >= p_file_mmap->line_total || line >= max_lines)                  if (line_current >= line_total || line >= max_lines)
273                  {                  {
274                          if (line_current - (line - 1) + (SCREEN_ROWS - 2) < p_file_mmap->line_total)                          if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total)
275                          {                          {
276                                  percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / p_file_mmap->line_total;                                  percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total;
277                          }                          }
278                          else                          else
279                          {                          {
# Line 308  int display_file_ex(const char *filename Line 303  int display_file_ex(const char *filename
303                                          clrline(begin_line, SCREEN_ROWS);                                          clrline(begin_line, SCREEN_ROWS);
304                                          break;                                          break;
305                                  case KEY_END:                                  case KEY_END:
306                                          line_current = p_file_mmap->line_total - (SCREEN_ROWS - 2);                                          line_current = line_total - (SCREEN_ROWS - 2);
307                                          line = begin_line;                                          line = begin_line;
308                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
309                                          clrline(begin_line, SCREEN_ROWS);                                          clrline(begin_line, SCREEN_ROWS);
# Line 327  int display_file_ex(const char *filename Line 322  int display_file_ex(const char *filename
322                                  case CR:                                  case CR:
323                                          igetch_reset();                                          igetch_reset();
324                                  case KEY_DOWN:                                  case KEY_DOWN:
325                                          if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= p_file_mmap->line_total) // Reach bottom                                          if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom
326                                          {                                          {
327                                                  break;                                                  break;
328                                          }                                          }
# Line 357  int display_file_ex(const char *filename Line 352  int display_file_ex(const char *filename
352                                  case KEY_PGDN:                                  case KEY_PGDN:
353                                  case Ctrl('F'):                                  case Ctrl('F'):
354                                  case KEY_SPACE:                                  case KEY_SPACE:
355                                          if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= p_file_mmap->line_total) // Reach bottom                                          if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach bottom
356                                          {                                          {
357                                                  break;                                                  break;
358                                          }                                          }
359                                          line_current += (SCREEN_ROWS - 3) - (line - 1);                                          line_current += (SCREEN_ROWS - 3) - (line - 1);
360                                          if (line_current + SCREEN_ROWS - 2 > p_file_mmap->line_total) // No enough lines to display                                          if (line_current + SCREEN_ROWS - 2 > line_total) // No enough lines to display
361                                          {                                          {
362                                                  line_current = p_file_mmap->line_total - (SCREEN_ROWS - 2);                                                  line_current = line_total - (SCREEN_ROWS - 2);
363                                          }                                          }
364                                          line = begin_line;                                          line = begin_line;
365                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
# Line 395  int display_file_ex(const char *filename Line 390  int display_file_ex(const char *filename
390                                          clrline(begin_line, SCREEN_ROWS);                                          clrline(begin_line, SCREEN_ROWS);
391                                          break;                                          break;
392                                  default:                                  default:
                                         log_std("Input: %d\n", ch);  
393                                          input_ok = 0;                                          input_ok = 0;
394                                          break;                                          break;
395                                  }                                  }
# Line 406  int display_file_ex(const char *filename Line 400  int display_file_ex(const char *filename
400                          continue;                          continue;
401                  }                  }
402    
403                  len = p_file_mmap->line_offsets[line_current + 1] - p_file_mmap->line_offsets[line_current];                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
404                  if (len >= LINE_BUFFER_LEN)                  if (len >= LINE_BUFFER_LEN)
405                  {                  {
406                          log_error("Error length exceeds buffer size: %d\n", len);                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
407                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
408                          len = LINE_BUFFER_LEN - 1;                          len = LINE_BUFFER_LEN - 1;
409                  }                  }
410    
411                  memcpy(buffer, (const char *)p_file_mmap->p_data + p_file_mmap->line_offsets[line_current], (size_t)len);                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
412                  buffer[len] = '\0';                  buffer[len] = '\0';
413    
414                  moveto(line, 0);                  moveto(line, 0);
# Line 424  int display_file_ex(const char *filename Line 419  int display_file_ex(const char *filename
419          }          }
420    
421  cleanup:  cleanup:
422            if (detach_file_shm(p_shm) < 0)
423            {
424                    log_error("detach_file_shm(%s) error\n", filename);
425            }
426    
427          return ch;          return ch;
428  }  }
429    
430  int show_top(char *status)  int show_top(const char *status)
431  {  {
432          int end_of_line;          char status_f[21];
433          int display_len;          int truncate;
434            int status_len;
435            int section_name_len;
436          int len;          int len;
437    
438          char space1[LINE_BUFFER_LEN];          strncpy(status_f, status, sizeof(status_f) - 1);
439          char space2[LINE_BUFFER_LEN];          status_f[sizeof(status_f) - 1] = '\0';
440    
441          len = split_line(status, 20, &end_of_line, &display_len);          len = split_line(status_f, 20, &truncate, &status_len);
442          if (end_of_line)          if (truncate)
443          {          {
444                  status[len] = '\0';                  log_error("Status string is truncated\n");
445                    status_f[len] = '\0';
446          }          }
         str_space(space1, 31 - display_len);  
447    
448          len = split_line(BBS_current_section_name, 20, &end_of_line, &display_len);          len = split_line(BBS_current_section_name, BBS_section_name_max_len, &truncate, &section_name_len);
449          if (end_of_line)          if (truncate)
450          {          {
451                  status[len] = '\0';                  log_error("Section name is truncated\n");
452          }          }
         str_space(space2, 30 - display_len);  
453    
454          moveto(1, 0);          moveto(1, 0);
455          clrtoeol();          clrtoeol();
456          prints("\033[1;44;33m%s \033[37m%s%s%s\033[33m 讨论区 [%s]\033[m",          prints("\033[1;44;33m%s\033[37m%*s%*s\033[33m 讨论区 [%s]\033[m",
457                     status, space1, BBS_name, space2, BBS_current_section_name);                     status_f, 44 - status_len, BBS_name, 26 - section_name_len, "", BBS_current_section_name);
         iflush();  
458    
459          return 0;          return 0;
460  }  }
461    
462  int show_bottom(char *msg)  int show_bottom(const char *msg)
463  {  {
464          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
         char space[LINE_BUFFER_LEN];  
465          time_t time_online;          time_t time_online;
466          struct tm *tm_online;          struct tm *tm_online;
467            char msg_f[21];
468            int truncate;
469            int msg_len;
470            int len;
471            int len_username;
472    
473          get_time_str(str_time, sizeof(str_time));          get_time_str(str_time, sizeof(str_time));
474          str_space(space, 34 - (int)strnlen(BBS_username, sizeof(BBS_username)));  
475            strncpy(msg_f, msg, sizeof(msg_f) - 1);
476            msg_f[sizeof(msg_f) - 1] = '\0';
477    
478            len = split_line(msg_f, 20, &truncate, &msg_len);
479            if (truncate)
480            {
481                    log_error("Status string is truncated\n");
482                    msg_f[len] = '\0';
483            }
484    
485            len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
486    
487          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
488          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
489    
490          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
491          clrtoeol();          clrtoeol();
492          prints("\033[1;44;33m[\033[36m%s\033[33m]%s帐号[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]%*s%*s帐号[\033[36m%s\033[33m]"
493                     "[\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",
494                     str_time, space, BBS_username, tm_online->tm_mday - 1,                     str_time, 21, msg_f, 9 - len_username, "", BBS_username,
495                     tm_online->tm_hour, tm_online->tm_min);                     tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
         iflush();  
496    
497          return 0;          return 0;
498  }  }
499    
500  int show_active_board()  int show_active_board()
501  {  {
502          char filename[FILE_PATH_LEN];          static int line_current = 0;
503          char buffer[LINE_BUFFER_LEN];          static const void *p_shm = NULL;
504          FILE *fin;          static size_t data_len;
505          static int line;          static long line_total;
506          int len;          static const void *p_data;
507          int end_of_line;          static const long *p_line_offsets;
         int display_len;  
508    
509          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          static time_t t_last_show = 0;
510            static int line_last = 0;
511    
512          if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)          char buffer[LINE_BUFFER_LEN];
513          {          long int len;
                 log_error("Unable to open file %s\n", filename);  
                 return -1;  
         }  
514    
515          for (int i = 0; i < line; i++)          if (p_shm == NULL)
516          {          {
517                  if (fgets(buffer, sizeof(buffer), fin) == NULL)                  if ((p_shm = get_file_shm_readonly(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
518                  {                  {
519                          line = 0;                          log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
520                          rewind(fin);                          return KEY_NULL;
                         break;  
521                  }                  }
522          }          }
523    
524            if (time(0) - t_last_show >= 10)
525            {
526                    line_last = line_current;
527                    t_last_show = time(0);
528            }
529            else
530            {
531                    line_current = line_last;
532            }
533    
534            clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
535    
536          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
537          {          {
538                  if (fgets(buffer, sizeof(buffer), fin) == NULL)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
539                    if (len >= LINE_BUFFER_LEN)
540                  {                  {
541                          line = 0;                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
542                          break;                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
543                            len = LINE_BUFFER_LEN - 1;
544                  }                  }
545                  line++;  
546                  len = split_line(buffer, SCREEN_COLS, &end_of_line, &display_len);                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
547                  buffer[len] = '\0'; // Truncate over-length line                  buffer[len] = '\0';
548    
549                  moveto(3 + i, 0);                  moveto(3 + i, 0);
550                  prints("%s", buffer);                  prints("%s", buffer);
         }  
         iflush();  
551    
552          fclose(fin);                  line_current++;
553                    if (line_current >= line_total)
554                    {
555                            line_current = 0;
556                            break;
557                    }
558            }
559    
560          return 0;          return 0;
561  }  }


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

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