/[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.63 by sysadm, Sun May 18 08:53:21 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(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 298  int display_file_ex(const char *filename Line 293  int display_file_ex(const char *filename
293                                  input_ok = 1;                                  input_ok = 1;
294                                  switch (ch)                                  switch (ch)
295                                  {                                  {
296                                  case KEY_NULL:                                  // case KEY_NULL:
297                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
298                                          goto cleanup;                                          goto cleanup;
299                                  case KEY_HOME:                                  case KEY_HOME:
# 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:
         if (shmdt(p_file_shm) == -1)  
         {  
                 log_error("shmdt() error (%d)\n", errno);  
         }  
   
422          return ch;          return ch;
423  }  }
424    
425  int show_top(char *status)  int show_top(const char *status)
426  {  {
427            char status_f[21];
428          int truncate;          int truncate;
429          int status_len;          int status_len;
430          int section_name_len;          int section_name_len;
431          int len;          int len;
         char status_f[21];  
432    
433          strncpy(status_f, status, sizeof(status_f) - 1);          strncpy(status_f, status, sizeof(status_f) - 1);
434          status_f[sizeof(status_f) - 1] = '\0';          status_f[sizeof(status_f) - 1] = '\0';
# Line 458  int show_top(char *status) Line 448  int show_top(char *status)
448    
449          moveto(1, 0);          moveto(1, 0);
450          clrtoeol();          clrtoeol();
451          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",
452                     status_f, (39 - status_len), BBS_name, (30 - section_name_len), "", BBS_current_section_name);                     status_f, 32, BBS_name, 26, "", BBS_current_section_name);
453          iflush();          iflush();
454    
455          return 0;          return 0;
456  }  }
457    
458  int show_bottom(char *msg)  int show_bottom(const char *msg)
459  {  {
460          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
         char space[LINE_BUFFER_LEN];  
461          time_t time_online;          time_t time_online;
462          struct tm *tm_online;          struct tm *tm_online;
463            char msg_f[21];
464            int truncate;
465            int msg_len;
466            int len;
467            int len_username;
468    
469          get_time_str(str_time, sizeof(str_time));          get_time_str(str_time, sizeof(str_time));
470          str_space(space, 34 - (int)strnlen(BBS_username, sizeof(BBS_username)));  
471            strncpy(msg_f, msg, sizeof(msg_f) - 1);
472            msg_f[sizeof(msg_f) - 1] = '\0';
473    
474            len = split_line(msg_f, 20, &truncate, &msg_len);
475            if (truncate)
476            {
477                    log_error("Status string is truncated\n");
478                    msg_f[len] = '\0';
479            }
480    
481            len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
482    
483          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
484          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
485    
486          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
487          clrtoeol();          clrtoeol();
488          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]"
489                     "[\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",
490                     str_time, space, BBS_username, tm_online->tm_mday - 1,                     str_time, 21, msg_f, 13 - len_username, "", BBS_username,
491                     tm_online->tm_hour, tm_online->tm_min);                     tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
492          iflush();          iflush();
493    
494          return 0;          return 0;
# Line 491  int show_bottom(char *msg) Line 496  int show_bottom(char *msg)
496    
497  int show_active_board()  int show_active_board()
498  {  {
499          char filename[FILE_PATH_LEN];          static int line_current = 0;
500          char buffer[LINE_BUFFER_LEN];          static const void *p_shm = NULL;
501          FILE *fin;          static size_t data_len;
502          static int line;          static long line_total;
503          int len;          static const void *p_data;
504          int end_of_line;          static const long *p_line_offsets;
         int display_len;  
505    
506          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          char buffer[LINE_BUFFER_LEN];
507            long int len;
         if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)  
         {  
                 log_error("Unable to open file %s\n", filename);  
                 return -1;  
         }  
508    
509          for (int i = 0; i < line; i++)          if (p_shm == NULL)
510          {          {
511                  if (fgets(buffer, sizeof(buffer), fin) == NULL)                  if ((p_shm = get_file_shm(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
512                  {                  {
513                          line = 0;                          log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
514                          rewind(fin);                          return KEY_NULL;
                         break;  
515                  }                  }
516          }          }
517    
518            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
519    
520          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
521          {          {
522                  if (fgets(buffer, sizeof(buffer), fin) == NULL)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
523                    if (len >= LINE_BUFFER_LEN)
524                  {                  {
525                          line = 0;                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
526                          break;                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
527                            len = LINE_BUFFER_LEN - 1;
528                  }                  }
529                  line++;  
530                  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);
531                  buffer[len] = '\0'; // Truncate over-length line                  buffer[len] = '\0';
532    
533                  moveto(3 + i, 0);                  moveto(3 + i, 0);
534                  prints("%s", buffer);                  prints("%s", buffer);
535    
536                    line_current++;
537                    if (line_current >= line_total)
538                    {
539                            line_current = 0;
540                            break;
541                    }
542          }          }
543          iflush();          iflush();
544    
         fclose(fin);  
   
545          return 0;          return 0;
546  }  }


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

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