/[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.61 by sysadm, Sun May 18 06:57:56 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 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(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 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 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("Error length exceeds buffer size: %d\n", len);
407                          len = LINE_BUFFER_LEN - 1;                          len = LINE_BUFFER_LEN - 1;
408                  }                  }
409    
410                  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);
411                  buffer[len] = '\0';                  buffer[len] = '\0';
412    
413                  moveto(line, 0);                  moveto(line, 0);
# Line 427  cleanup: Line 421  cleanup:
421          return ch;          return ch;
422  }  }
423    
424  int show_top(char *status)  int show_top(const char *status)
425  {  {
426          int end_of_line;          char status_f[21];
427          int display_len;          int truncate;
428            int status_len;
429            int section_name_len;
430          int len;          int len;
431    
432          char space1[LINE_BUFFER_LEN];          strncpy(status_f, status, sizeof(status_f) - 1);
433          char space2[LINE_BUFFER_LEN];          status_f[sizeof(status_f) - 1] = '\0';
434    
435          len = split_line(status, 20, &end_of_line, &display_len);          len = split_line(status_f, 20, &truncate, &status_len);
436          if (end_of_line)          if (truncate)
437          {          {
438                  status[len] = '\0';                  log_error("Status string is truncated\n");
439                    status_f[len] = '\0';
440          }          }
         str_space(space1, 31 - display_len);  
441    
442          len = split_line(BBS_current_section_name, 20, &end_of_line, &display_len);          len = split_line(BBS_current_section_name, 20, &truncate, &section_name_len);
443          if (end_of_line)          if (truncate)
444          {          {
445                  status[len] = '\0';                  log_error("Section name is truncated\n");
446          }          }
         str_space(space2, 30 - display_len);  
447    
448          moveto(1, 0);          moveto(1, 0);
449          clrtoeol();          clrtoeol();
450          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",
451                     status, space1, BBS_name, space2, BBS_current_section_name);                     status_f, 32, BBS_name, 26, "", BBS_current_section_name);
452          iflush();          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);
491          iflush();          iflush();
492    
493          return 0;          return 0;
# Line 485  int show_bottom(char *msg) Line 495  int show_bottom(char *msg)
495    
496  int show_active_board()  int show_active_board()
497  {  {
498          char filename[FILE_PATH_LEN];          static int line_current = 0;
499          char buffer[LINE_BUFFER_LEN];          static const void *p_shm = NULL;
500          FILE *fin;          static size_t data_len;
501          static int line;          static long line_total;
502          int len;          static const void *p_data;
503          int end_of_line;          static const long *p_line_offsets;
         int display_len;  
504    
505          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          char buffer[LINE_BUFFER_LEN];
506            long int len;
         if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)  
         {  
                 log_error("Unable to open file %s\n", filename);  
                 return -1;  
         }  
507    
508          for (int i = 0; i < line; i++)          if (p_shm == NULL)
509          {          {
510                  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)
511                  {                  {
512                          line = 0;                          log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
513                          rewind(fin);                          return KEY_NULL;
                         break;  
514                  }                  }
515          }          }
516    
517            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
518    
519          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
520          {          {
521                  if (fgets(buffer, sizeof(buffer), fin) == NULL)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
522                    if (len >= LINE_BUFFER_LEN)
523                  {                  {
524                          line = 0;                          log_error("Error length exceeds buffer size: %d\n", len);
525                          break;                          len = LINE_BUFFER_LEN - 1;
526                  }                  }
527                  line++;  
528                  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);
529                  buffer[len] = '\0'; // Truncate over-length line                  buffer[len] = '\0';
530    
531                  moveto(3 + i, 0);                  moveto(3 + i, 0);
532                  prints("%s", buffer);                  prints("%s", buffer);
533    
534                    line_current++;
535                    if (line_current >= line_total)
536                    {
537                            line_current = 0;
538                            break;
539                    }
540          }          }
541          iflush();          iflush();
542    
         fclose(fin);  
   
543          return 0;          return 0;
544  }  }


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

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