/[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.55 by sysadm, Sat May 17 05:56:17 2025 UTC Revision 1.65 by sysadm, Mon May 19 06:55:06 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;
432    
433          len = split_line(status, 20, &truncate, &status_len);          strncpy(status_f, status, sizeof(status_f) - 1);
434            status_f[sizeof(status_f) - 1] = '\0';
435    
436            len = split_line(status_f, 20, &truncate, &status_len);
437          if (truncate)          if (truncate)
438          {          {
439                  status[len] = '\0';                  log_error("Status string is truncated\n");
440                    status_f[len] = '\0';
441          }          }
442    
443          len = split_line(BBS_current_section_name, 20, &truncate, &section_name_len);          len = split_line(BBS_current_section_name, 20, &truncate, &section_name_len);
444          if (truncate)          if (truncate)
445          {          {
446                  status[len] = '\0';                  log_error("Section name is truncated\n");
447          }          }
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, (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();  
453    
454          return 0;          return 0;
455  }  }
456    
457  int show_bottom(char *msg)  int show_bottom(const char *msg)
458  {  {
459          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
         char space[LINE_BUFFER_LEN];  
460          time_t time_online;          time_t time_online;
461          struct tm *tm_online;          struct tm *tm_online;
462            char msg_f[21];
463            int truncate;
464            int msg_len;
465            int len;
466            int len_username;
467    
468          get_time_str(str_time, sizeof(str_time));          get_time_str(str_time, sizeof(str_time));
469          str_space(space, 34 - (int)strnlen(BBS_username, sizeof(BBS_username)));  
470            strncpy(msg_f, msg, sizeof(msg_f) - 1);
471            msg_f[sizeof(msg_f) - 1] = '\0';
472    
473            len = split_line(msg_f, 20, &truncate, &msg_len);
474            if (truncate)
475            {
476                    log_error("Status string is truncated\n");
477                    msg_f[len] = '\0';
478            }
479    
480            len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
481    
482          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
483          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
484    
485          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
486          clrtoeol();          clrtoeol();
487          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]"
488                     "[\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",
489                     str_time, space, BBS_username, tm_online->tm_mday - 1,                     str_time, 21, msg_f, 13 - len_username, "", BBS_username,
490                     tm_online->tm_hour, tm_online->tm_min);                     tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
         iflush();  
491    
492          return 0;          return 0;
493  }  }
494    
495  int show_active_board()  int show_active_board()
496  {  {
497          char filename[FILE_PATH_LEN];          static int line_current = 0;
498          char buffer[LINE_BUFFER_LEN];          static const void *p_shm = NULL;
499          FILE *fin;          static size_t data_len;
500          static int line;          static long line_total;
501          int len;          static const void *p_data;
502          int end_of_line;          static const long *p_line_offsets;
         int display_len;  
503    
504          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          static time_t t_last_show = 0;
505            static int line_last = 0;
506    
507          if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)          char buffer[LINE_BUFFER_LEN];
508          {          long int len;
                 log_error("Unable to open file %s\n", filename);  
                 return -1;  
         }  
509    
510          for (int i = 0; i < line; i++)          if (p_shm == NULL)
511          {          {
512                  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)
513                  {                  {
514                          line = 0;                          log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
515                          rewind(fin);                          return KEY_NULL;
                         break;  
516                  }                  }
517          }          }
518    
519            if (time(0) - t_last_show >= 10)
520            {
521                    line_last = line_current;
522                    t_last_show = time(0);
523            }
524            else
525            {
526                    line_current = line_last;
527            }
528    
529            clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
530    
531          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
532          {          {
533                  if (fgets(buffer, sizeof(buffer), fin) == NULL)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
534                    if (len >= LINE_BUFFER_LEN)
535                  {                  {
536                          line = 0;                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
537                          break;                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
538                            len = LINE_BUFFER_LEN - 1;
539                  }                  }
540                  line++;  
541                  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);
542                  buffer[len] = '\0'; // Truncate over-length line                  buffer[len] = '\0';
543    
544                  moveto(3 + i, 0);                  moveto(3 + i, 0);
545                  prints("%s", buffer);                  prints("%s", buffer);
         }  
         iflush();  
546    
547          fclose(fin);                  line_current++;
548                    if (line_current >= line_total)
549                    {
550                            line_current = 0;
551                            break;
552                    }
553            }
554    
555          return 0;          return 0;
556  }  }


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

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