/[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.56 by sysadm, Sat May 17 06:14:22 2025 UTC Revision 1.69 by sysadm, Sun May 25 06:55:23 2025 UTC
# 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 void *p_file_shm;          const void *p_shm;
         const void *p_data;  
236          size_t data_len;          size_t data_len;
237          long line_total;          long line_total;
238            const void *p_data;
239          const long *p_line_offsets;          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_shm = get_file_shm(filename)) == NULL)          if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
245          {          {
246                  log_error("get_file_shm(%s) error\n", filename);                  log_error("get_file_shm(%s) error\n", filename);
247                  return KEY_NULL;                  return KEY_NULL;
248          }          }
249    
         data_len = *((size_t *)p_file_shm);  
         line_total = *((long *)(p_file_shm + sizeof(size_t)));  
         p_data = p_file_shm + sizeof(data_len) + sizeof(line_total);  
         p_line_offsets = p_data + data_len + 1;  
   
250          clrline(begin_line, SCREEN_ROWS);          clrline(begin_line, SCREEN_ROWS);
251          line = begin_line;          line = begin_line;
252          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 409  int display_file_ex(const char *filename Line 403  int display_file_ex(const char *filename
403                  len = p_line_offsets[line_current + 1] - p_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    
# 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 (shmdt(p_file_shm) == -1)          if (detach_file_shm(p_shm) < 0)
423          {          {
424                  log_error("shmdt() error (%d)\n", errno);                  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            char status_f[21];
433          int truncate;          int truncate;
434          int status_len;          int status_len;
435          int section_name_len;          int section_name_len;
436          int len;          int len;
         char status_f[21];  
437    
438          strncpy(status_f, status, sizeof(status_f) - 1);          strncpy(status_f, status, sizeof(status_f) - 1);
439          status_f[sizeof(status_f) - 1] = '\0';          status_f[sizeof(status_f) - 1] = '\0';
# Line 450  int show_top(char *status) Line 445  int show_top(char *status)
445                  status_f[len] = '\0';                  status_f[len] = '\0';
446          }          }
447    
448          len = split_line(BBS_current_section_name, 20, &truncate, &section_name_len);          len = split_line(BBS_current_section_name, BBS_section_name_max_len, &truncate, &section_name_len);
449          if (truncate)          if (truncate)
450          {          {
451                  log_error("Section name is truncated\n");                  log_error("Section name is truncated\n");
# Line 458  int show_top(char *status) Line 453  int show_top(char *status)
453    
454          moveto(1, 0);          moveto(1, 0);
455          clrtoeol();          clrtoeol();
456          prints("\033[1;44;33m%s \033[37m%*s%*s\033[33m 讨论区 [%s]\033[m",          prints("\033[1;44;33m%s\033[37m%*s%*s\033[33m 讨论区 [%s]\033[m",
457                     status_f, (39 - status_len), BBS_name, (30 - section_name_len), "", 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