/[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.53 by sysadm, Fri May 16 12:23:57 2025 UTC Revision 1.56 by sysadm, Sat May 17 06:14:22 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #include "screen.h"
18  #include "bbs.h"  #include "bbs.h"
19  #include "common.h"  #include "common.h"
20  #include "str_process.h"  #include "str_process.h"
21  #include "log.h"  #include "log.h"
22  #include "io.h"  #include "io.h"
23  #include "screen.h"  #include "file_loader.h"
24  #include <fcntl.h>  #include <fcntl.h>
25  #include <string.h>  #include <string.h>
26  #include <ctype.h>  #include <ctype.h>
# Line 29  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 228  int display_file_ex(const char *filename Line 229  int display_file_ex(const char *filename
229  {  {
230          static int show_help = 1;          static int show_help = 1;
231          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
232          void *p_data;          int ch = KEY_NULL;
         int ch = 0;  
233          int input_ok, line, max_lines;          int input_ok, line, max_lines;
234          long int c_line_current = 0;          long int line_current = 0;
235          long int c_line_total = 0;          const void *p_file_shm;
236          int fd;          const void *p_data;
237          struct stat sb;          size_t data_len;
238          long *p_line_offsets;          long line_total;
239            const long *p_line_offsets;
240          long int len;          long int len;
241          long int percentile;          long int percentile;
242          int loop = 1;          int loop;
243    
244          if ((fd = open(filename, O_RDONLY)) < 0)          if ((p_file_shm = get_file_shm(filename)) == NULL)
245          {          {
246                  log_error("open(%s) error (%d)\n", filename, errno);                  log_error("get_file_shm(%s) error\n", filename);
247                  return -1;                  return KEY_NULL;
248          }          }
249    
250          if (fstat(fd, &sb) < 0)          data_len = *((size_t *)p_file_shm);
251          {          line_total = *((long *)(p_file_shm + sizeof(size_t)));
252                  log_error("fstat(fd) error (%d)\n", errno);          p_data = p_file_shm + sizeof(data_len) + sizeof(line_total);
253                  return -1;          p_line_offsets = p_data + data_len + 1;
         }  
   
         p_data = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0L);  
         if (p_data == MAP_FAILED)  
         {  
                 log_error("mmap() error (%d)\n", errno);  
                 return -2;  
         }  
   
         if (close(fd) < 0)  
         {  
                 log_error("close(fd) error (%d)\n", errno);  
                 return -1;  
         }  
   
         p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);  
   
         c_line_total = split_data_lines(p_data, SCREEN_COLS, p_line_offsets, MAX_FILE_LINES);  
254    
255          clrline(begin_line, SCREEN_ROWS);          clrline(begin_line, SCREEN_ROWS);
256          line = begin_line;          line = begin_line;
257          max_lines = SCREEN_ROWS - 1;          max_lines = SCREEN_ROWS - 1;
258    
259            loop = 1;
260          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
261          {          {
262                  if (c_line_current >= c_line_total && c_line_total <= SCREEN_ROWS - 2)                  if (line_current >= line_total && line_total <= SCREEN_ROWS - 2)
263                  {                  {
264                          if (wait)                          if (wait)
265                          {                          {
# Line 290  int display_file_ex(const char *filename Line 274  int display_file_ex(const char *filename
274                          break;                          break;
275                  }                  }
276    
277                  if (c_line_current >= c_line_total || line >= max_lines)                  if (line_current >= line_total || line >= max_lines)
278                  {                  {
279                          if (c_line_current - (line - 1) + (SCREEN_ROWS - 2) < c_line_total)                          if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total)
280                          {                          {
281                                  percentile = (c_line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / c_line_total;                                  percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total;
282                          }                          }
283                          else                          else
284                          {                          {
# Line 318  int display_file_ex(const char *filename Line 302  int display_file_ex(const char *filename
302                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
303                                          goto cleanup;                                          goto cleanup;
304                                  case KEY_HOME:                                  case KEY_HOME:
305                                          c_line_current = 0;                                          line_current = 0;
306                                          line = begin_line;                                          line = begin_line;
307                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
308                                          clrline(begin_line, SCREEN_ROWS);                                          clrline(begin_line, SCREEN_ROWS);
309                                          break;                                          break;
310                                  case KEY_END:                                  case KEY_END:
311                                          c_line_current = c_line_total - (SCREEN_ROWS - 2);                                          line_current = line_total - (SCREEN_ROWS - 2);
312                                          line = begin_line;                                          line = begin_line;
313                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
314                                          clrline(begin_line, SCREEN_ROWS);                                          clrline(begin_line, SCREEN_ROWS);
315                                          break;                                          break;
316                                  case KEY_UP:                                  case KEY_UP:
317                                          if (c_line_current - line < 0) // Reach top                                          if (line_current - line < 0) // Reach top
318                                          {                                          {
319                                                  break;                                                  break;
320                                          }                                          }
321                                          c_line_current -= line;                                          line_current -= line;
322                                          line = begin_line;                                          line = begin_line;
323                                          // max_lines = begin_line + 1;                                          // max_lines = begin_line + 1;
324                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
# Line 343  int display_file_ex(const char *filename Line 327  int display_file_ex(const char *filename
327                                  case CR:                                  case CR:
328                                          igetch_reset();                                          igetch_reset();
329                                  case KEY_DOWN:                                  case KEY_DOWN:
330                                          if (c_line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= c_line_total) // Reach bottom                                          if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom
331                                          {                                          {
332                                                  break;                                                  break;
333                                          }                                          }
334                                          c_line_current += ((SCREEN_ROWS - 2) - (line - 1));                                          line_current += ((SCREEN_ROWS - 2) - (line - 1));
335                                          line = SCREEN_ROWS - 2;                                          line = SCREEN_ROWS - 2;
336                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
337                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
# Line 356  int display_file_ex(const char *filename Line 340  int display_file_ex(const char *filename
340                                          break;                                          break;
341                                  case KEY_PGUP:                                  case KEY_PGUP:
342                                  case Ctrl('B'):                                  case Ctrl('B'):
343                                          if (c_line_current - line < 0) // Reach top                                          if (line_current - line < 0) // Reach top
344                                          {                                          {
345                                                  break;                                                  break;
346                                          }                                          }
347                                          c_line_current -= ((SCREEN_ROWS - 3) + (line - 1));                                          line_current -= ((SCREEN_ROWS - 3) + (line - 1));
348                                          if (c_line_current < 0)                                          if (line_current < 0)
349                                          {                                          {
350                                                  c_line_current = 0;                                                  line_current = 0;
351                                          }                                          }
352                                          line = begin_line;                                          line = begin_line;
353                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
# Line 373  int display_file_ex(const char *filename Line 357  int display_file_ex(const char *filename
357                                  case KEY_PGDN:                                  case KEY_PGDN:
358                                  case Ctrl('F'):                                  case Ctrl('F'):
359                                  case KEY_SPACE:                                  case KEY_SPACE:
360                                          if (c_line_current + (SCREEN_ROWS - 2) - (line - 1) >= c_line_total) // Reach bottom                                          if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach bottom
361                                          {                                          {
362                                                  break;                                                  break;
363                                          }                                          }
364                                          c_line_current += (SCREEN_ROWS - 3) - (line - 1);                                          line_current += (SCREEN_ROWS - 3) - (line - 1);
365                                          if (c_line_current + SCREEN_ROWS - 2 > c_line_total) // No enough lines to display                                          if (line_current + SCREEN_ROWS - 2 > line_total) // No enough lines to display
366                                          {                                          {
367                                                  c_line_current = c_line_total - (SCREEN_ROWS - 2);                                                  line_current = line_total - (SCREEN_ROWS - 2);
368                                          }                                          }
369                                          line = begin_line;                                          line = begin_line;
370                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
# Line 405  int display_file_ex(const char *filename Line 389  int display_file_ex(const char *filename
389                                          show_help = 1;                                          show_help = 1;
390    
391                                          // Refresh after display help information                                          // Refresh after display help information
392                                          c_line_current -= (line - 1);                                          line_current -= (line - 1);
393                                          line = begin_line;                                          line = begin_line;
394                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
395                                          clrline(begin_line, SCREEN_ROWS);                                          clrline(begin_line, SCREEN_ROWS);
# Line 422  int display_file_ex(const char *filename Line 406  int display_file_ex(const char *filename
406                          continue;                          continue;
407                  }                  }
408    
409                  len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
410                  if (len >= LINE_BUFFER_LEN)                  if (len >= LINE_BUFFER_LEN)
411                  {                  {
412                          log_error("Error length exceeds buffer size: %d\n", len);                          log_error("Error length exceeds buffer size: %d\n", len);
413                          len = LINE_BUFFER_LEN - 1;                          len = LINE_BUFFER_LEN - 1;
414                  }                  }
415    
416                  memcpy(buffer, (const char *)p_data + p_line_offsets[c_line_current], (size_t)len);                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
417                  buffer[len] = '\0';                  buffer[len] = '\0';
418    
419                  moveto(line, 0);                  moveto(line, 0);
420                  clrtoeol();                  clrtoeol();
421                  prints("%s", buffer);                  prints("%s", buffer);
422                  c_line_current++;                  line_current++;
423                  line++;                  line++;
424          }          }
425    
426  cleanup:  cleanup:
427          if (munmap(p_data, (size_t)sb.st_size) < 0)          if (shmdt(p_file_shm) == -1)
428          {          {
429                  log_error("munmap() error (%d)\n", errno);                  log_error("shmdt() error (%d)\n", errno);
430          }          }
431    
         free(p_line_offsets);  
   
432          return ch;          return ch;
433  }  }
434    
435  int show_top(char *status)  int show_top(char *status)
436  {  {
437          int end_of_line;          int truncate;
438          int display_len;          int status_len;
439            int section_name_len;
440          int len;          int len;
441            char status_f[21];
442    
443          char space1[LINE_BUFFER_LEN];          strncpy(status_f, status, sizeof(status_f) - 1);
444          char space2[LINE_BUFFER_LEN];          status_f[sizeof(status_f) - 1] = '\0';
445    
446          len = split_line(status, 20, &end_of_line, &display_len);          len = split_line(status_f, 20, &truncate, &status_len);
447          if (end_of_line)          if (truncate)
448          {          {
449                  status[len] = '\0';                  log_error("Status string is truncated\n");
450                    status_f[len] = '\0';
451          }          }
         str_space(space1, 31 - display_len);  
452    
453          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);
454          if (end_of_line)          if (truncate)
455          {          {
456                  status[len] = '\0';                  log_error("Section name is truncated\n");
457          }          }
         str_space(space2, 30 - display_len);  
458    
459          moveto(1, 0);          moveto(1, 0);
460          clrtoeol();          clrtoeol();
461          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",
462                     status, space1, BBS_name, space2, BBS_current_section_name);                     status_f, (39 - status_len), BBS_name, (30 - section_name_len), "", BBS_current_section_name);
463          iflush();          iflush();
464    
465          return 0;          return 0;


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

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