/[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.23 by sysadm, Tue Apr 29 14:04:10 2025 UTC Revision 1.65 by sysadm, Mon May 19 06:55:06 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    screen.c  -  description                                                    screen.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 18 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
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"
21    #include "log.h"
22  #include "io.h"  #include "io.h"
23    #include "file_loader.h"
24    #include <fcntl.h>
25    #include <string.h>
26    #include <ctype.h>
27    #include <unistd.h>
28    #include <stdlib.h>
29    #include <errno.h>
30  #include <sys/types.h>  #include <sys/types.h>
31  #include <sys/stat.h>  #include <sys/stat.h>
32  #include <unistd.h>  #include <sys/param.h>
33    #include <sys/shm.h>
34    
35  #define ACTIVE_BOARD_HEIGHT 8  #define ACTIVE_BOARD_HEIGHT 8
36    
 int screen_lines = 24;  
   
37  void moveto(int row, int col)  void moveto(int row, int col)
38  {  {
39          if (row >= 0)          if (row >= 0)
# Line 36  void moveto(int row, int col) Line 44  void moveto(int row, int col)
44          {          {
45                  prints("\r");                  prints("\r");
46          }          }
         iflush();  
47  }  }
48    
49  void clrtoeol()  void clrtoeol()
50  {  {
51          prints("\033[K");          prints(CTRL_SEQ_CLR_LINE);
         iflush();  
52  }  }
53    
54  void clrline(int line_begin, int line_end)  void clrline(int line_begin, int line_end)
# Line 52  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          }          }
   
         iflush();  
63  }  }
64    
65  void clrtobot(int line_begin)  void clrtobot(int line_begin)
66  {  {
67          clrline(line_begin, screen_lines);          moveto(line_begin, 0);
68            prints("\033[J");
69          moveto(line_begin, 0);          moveto(line_begin, 0);
70  }  }
71    
# Line 68  void clearscr() Line 73  void clearscr()
73  {  {
74          prints("\033[2J");          prints("\033[2J");
75          moveto(0, 0);          moveto(0, 0);
         iflush();  
76  }  }
77    
78  int press_any_key()  int press_any_key()
79  {  {
80          moveto(screen_lines, 0);          moveto(SCREEN_ROWS, 0);
81          clrtoeol();          clrtoeol();
82    
83          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints("                           \033[1;33m按任意键继续...\033[0;37m");
84          iflush();          iflush();
85    
86          return igetch_t(60);          return igetch_t(MIN(MAX_DELAY_TIME, 60));
87  }  }
88    
89  void set_input_echo(int echo)  void set_input_echo(int echo)
# Line 94  void set_input_echo(int echo) Line 98  void set_input_echo(int echo)
98                  //    outc ('\x85'); // ASCII code 133                  //    outc ('\x85'); // ASCII code 133
99                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
100                  iflush();                  iflush();
101                  igetch_t(60);                  igetch(0);
102                  igetch_t(60);                  igetch_reset();
103          }          }
104  }  }
105    
106  int _str_input(char *buffer, int buffer_length, int echo_mode)  static int _str_input(char *buffer, int buf_size, int echo_mode)
107  {  {
108          char buf[256], ch;          int c;
109          int c, offset = 0, len, loop = 1, i, hz = 0;          int offset = 0;
110            int hz = 0;
111    
112          for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)          buffer[buf_size - 1] = '\0';
113          {          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)
114                  offset++;                  ;
115          }  
116            igetch_reset();
117    
118          while (c = igetch_t(60))          while (!SYS_server_exit)
119          {          {
120                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));
121    
122                    if (c == CR)
123                  {                  {
124                            igetch_reset();
125                          break;                          break;
126                  }                  }
127                  if (c == LF)                  else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe
128                    {
129                            return -1;
130                    }
131                    else if (c == LF || c == '\0')
132                  {                  {
133                          continue;                          continue;
134                  }                  }
135                  if (c == BACKSPACE)                  else if (c == BACKSPACE)
136                  {                  {
137                          if (offset > 0)                          if (offset > 0)
138                          {                          {
139                                  buffer[--offset] = '\0';                                  offset--;
140                                  prints("\b \b");                                  if (buffer[offset] < 0 || buffer[offset] > 127)
141                                  //            clrtoeol ();                                  {
142                                            prints("\033[D \033[D");
143                                            offset--;
144                                            if (offset < 0) // should not happen
145                                            {
146                                                    log_error("Offset of buffer is negative\n");
147                                                    offset = 0;
148                                            }
149                                    }
150                                    buffer[offset] = '\0';
151                                    prints("\033[D \033[D");
152                                  iflush();                                  iflush();
153                          }                          }
154                          continue;                          continue;
155                  }                  }
156                  if (c > 255 || iscntrl(c))                  else if (c > 255 || iscntrl(c))
157                  {                  {
158                          continue;                          continue;
159                  }                  }
160                  if (c > 127 && c <= 255)                  else if (c > 127 && c <= 255)
161                  {                  {
162                            if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character
163                            {
164                                    igetch(0); // Ignore 1 character
165                                    outc('\a');
166                                    iflush();
167                                    continue;
168                            }
169                          hz = (!hz);                          hz = (!hz);
170                  }                  }
171                  if (offset >= buffer_length)  
172                    if (offset + 1 > buf_size - 1)
173                  {                  {
174                          outc('\a');                          outc('\a');
175                            iflush();
176                          continue;                          continue;
177                  }                  }
178    
179                  buffer[offset++] = (char)c;                  buffer[offset++] = (char)c;
180                  buffer[offset] = '\0';                  buffer[offset] = '\0';
181    
182                  switch (echo_mode)                  switch (echo_mode)
183                  {                  {
184                  case DOECHO:                  case DOECHO:
# Line 160  int _str_input(char *buffer, int buffer_ Line 194  int _str_input(char *buffer, int buffer_
194                  }                  }
195          }          }
196    
         prints("\r\n");  
         iflush();  
   
197          return offset;          return offset;
198  }  }
199    
200  int str_input(char *buffer, int buffer_length, int echo_mode)  int str_input(char *buffer, int buf_size, int echo_mode)
201  {  {
202          int offset;          int len;
203    
204          memset(buffer, '\0', buffer_length);          buffer[0] = '\0';
205    
206          offset = _str_input(buffer, buffer_length, echo_mode);          len = _str_input(buffer, buf_size, echo_mode);
207    
208          return offset;          prints("\r\n");
209            iflush();
210    
211            return len;
212  };  };
213    
214  int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int echo_mode)
215  {  {
216          int len;          int len;
217    
218          moveto(row, col);          moveto(row, col);
         iflush();  
219          prints(prompt);          prints(prompt);
220          prints(buffer);          prints(buffer);
221          iflush();          iflush();
222    
223          len = _str_input(buffer, buffer_length, echo_mode);          len = _str_input(buffer, buf_size, echo_mode);
224    
225          return len;          return len;
226  }  }
227    
228  int display_file(const char *filename)  int display_file_ex(const char *filename, int begin_line, int wait)
229  {  {
230          char buffer[260];          static int show_help = 1;
231          FILE *fin;          char buffer[LINE_BUFFER_LEN];
232          int i;          int ch = KEY_NULL;
233            int input_ok, line, max_lines;
234            long int line_current = 0;
235            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;
241            long int percentile;
242            int loop;
243    
244          if ((fin = fopen(filename, "r")) == NULL)          if ((p_shm = get_file_shm(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
245          {          {
246                  return -1;                  log_error("get_file_shm(%s) error\n", filename);
247          }                  return KEY_NULL;
   
         while (fgets(buffer, 255, fin))  
         {  
                 i = strlen(buffer);  
                 if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
                 {  
                         buffer[i - 1] = '\r';  
                         buffer[i] = '\n';  
                         buffer[i + 1] = '\0';  
                 }  
                 prints(buffer);  
                 iflush();  
248          }          }
         fclose(fin);  
249    
250          return 0;          clrline(begin_line, SCREEN_ROWS);
 }  
   
 int display_file_ex(const char *filename, int begin_line, int wait)  
 {  
         char buffer[260], temp[256];  
         int i, ch, input_ok, max_lines;  
         long int line, c_line_begin = 0, c_line_total = 0;  
         long int f_line, f_size, f_offset;  
         FILE *fin;  
         struct stat f_stat;  
   
         max_lines = screen_lines - 1;  
         clrline(begin_line, screen_lines);  
251          line = begin_line;          line = begin_line;
252          moveto(line, 0);          max_lines = SCREEN_ROWS - 1;
253    
254          if ((fin = fopen(filename, "r")) != NULL)          loop = 1;
255            while (!SYS_server_exit && loop)
256          {          {
257                  if (fstat(fileno(fin), &f_stat) != 0)                  if (line_current >= line_total && line_total <= SCREEN_ROWS - 2)
258                  {                  {
259                          log_error("Get file stat failed\n");                          if (wait)
260                          return -1;                          {
261                  }                                  ch = press_any_key();
262                  f_size = f_stat.st_size;                          }
263                            else
264                            {
265                                    iflush();
266                            }
267    
268                  while (fgets(buffer, 255, fin))                          loop = 0;
269                          c_line_total++;                          break;
270                  rewind(fin);                  }
271    
272                  while (fgets(buffer, 255, fin))                  if (line_current >= line_total || line >= max_lines)
273                  {                  {
274                          if (line >= max_lines)                          if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total)
275                            {
276                                    percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total;
277                            }
278                            else
279                          {                          {
280                                  f_offset = ftell(fin);                                  percentile = 100;
281                            }
282    
283                                  moveto(screen_lines, 0);                          moveto(SCREEN_ROWS, 0);
284                                  prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",                          prints("\033[1;44;32m%s (%d%%)%s\033[33m          │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
285                                             (f_offset - strlen(buffer)) * 100 / f_size);                                     (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
286                                  iflush();                                     (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
287                            iflush();
288    
289                                  input_ok = 0;                          input_ok = 0;
290                                  while (!input_ok)                          while (!SYS_server_exit && !input_ok)
291                            {
292                                    ch = igetch_t(MAX_DELAY_TIME);
293                                    input_ok = 1;
294                                    switch (ch)
295                                  {                                  {
296                                          ch = igetch_t(MAX_DELAY_TIME);                                  // case KEY_NULL:
297                                          input_ok = 1;                                  case KEY_TIMEOUT:
298                                          switch (ch)                                          goto cleanup;
299                                    case KEY_HOME:
300                                            line_current = 0;
301                                            line = begin_line;
302                                            max_lines = SCREEN_ROWS - 1;
303                                            clrline(begin_line, SCREEN_ROWS);
304                                            break;
305                                    case KEY_END:
306                                            line_current = line_total - (SCREEN_ROWS - 2);
307                                            line = begin_line;
308                                            max_lines = SCREEN_ROWS - 1;
309                                            clrline(begin_line, SCREEN_ROWS);
310                                            break;
311                                    case KEY_UP:
312                                            if (line_current - line < 0) // Reach top
313                                          {                                          {
                                         case KEY_UP:  
                                                 c_line_begin--;  
                                                 if (c_line_begin >= 0)  
                                                 {  
                                                         rewind(fin);  
                                                         for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                         {  
                                                                 if (fgets(buffer, 255, fin) == NULL)  
                                                                         goto exit;  
                                                         }  
                                                 }  
                                                 else  
                                                 {  
                                                         goto exit;  
                                                 }  
314                                                  break;                                                  break;
315                                          case KEY_DOWN:                                          }
316                                          case CR:                                          line_current -= line;
317                                                  c_line_begin++;                                          line = begin_line;
318                                                  rewind(fin);                                          // max_lines = begin_line + 1;
319                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                          // prints("\033[T"); // Scroll down 1 line
320                                                  {                                          max_lines = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
321                                                          if (fgets(buffer, 255, fin) == NULL)                                          break;
322                                                                  goto exit;                                  case CR:
323                                                  }                                          igetch_reset();
324                                                  break;                                  case KEY_DOWN:
325                                          case KEY_PGUP:                                          if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom
326                                          case Ctrl('B'):                                          {
                                                 if (c_line_begin > 0)  
                                                         c_line_begin -= (max_lines - begin_line - 1);  
                                                 else  
                                                         goto exit;  
                                                 if (c_line_begin < 0)  
                                                         c_line_begin = 0;  
                                                 rewind(fin);  
                                                 for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                 {  
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
                                                 break;  
                                         case KEY_RIGHT:  
                                         case KEY_PGDN:  
                                         case Ctrl('F'):  
                                         case KEY_SPACE:  
                                                 c_line_begin += (max_lines - begin_line - 1);  
                                                 if (c_line_begin + (max_lines - begin_line) >  
                                                         c_line_total)  
                                                         c_line_begin =  
                                                                 c_line_total - (max_lines - begin_line);  
                                                 rewind(fin);  
                                                 for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                 {  
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
327                                                  break;                                                  break;
328                                          case KEY_NULL:                                          }
329                                          case KEY_TIMEOUT:                                          line_current += ((SCREEN_ROWS - 2) - (line - 1));
330                                          case KEY_LEFT:                                          line = SCREEN_ROWS - 2;
331                                          case 'q':                                          max_lines = SCREEN_ROWS - 1;
332                                          case 'Q':                                          moveto(SCREEN_ROWS, 0);
333                                                  goto exit;                                          clrtoeol();
334                                            prints("\033[S"); // Scroll up 1 line
335                                            break;
336                                    case KEY_PGUP:
337                                    case Ctrl('B'):
338                                            if (line_current - line < 0) // Reach top
339                                            {
340                                                  break;                                                  break;
341                                          case '?':                                          }
342                                          case 'h':                                          line_current -= ((SCREEN_ROWS - 3) + (line - 1));
343                                          case 'H':                                          if (line_current < 0)
344                                                  // Display help information                                          {
345                                                  strcpy(temp, app_home_dir);                                                  line_current = 0;
346                                                  strcat(temp, "data/read_help.txt");                                          }
347                                                  display_file_ex(temp, begin_line, 1);                                          line = begin_line;
348                                            max_lines = SCREEN_ROWS - 1;
349                                                  // Refresh after display help information                                          clrline(begin_line, SCREEN_ROWS);
350                                                  rewind(fin);                                          break;
351                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                  case KEY_RIGHT:
352                                                  {                                  case KEY_PGDN:
353                                                          if (fgets(buffer, 255, fin) == NULL)                                  case Ctrl('F'):
354                                                                  goto exit;                                  case KEY_SPACE:
355                                                  }                                          if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach bottom
356                                            {
357                                                  break;                                                  break;
358                                          default:                                          }
359                                                  input_ok = 0;                                          line_current += (SCREEN_ROWS - 3) - (line - 1);
360                                            if (line_current + SCREEN_ROWS - 2 > line_total) // No enough lines to display
361                                            {
362                                                    line_current = line_total - (SCREEN_ROWS - 2);
363                                            }
364                                            line = begin_line;
365                                            max_lines = SCREEN_ROWS - 1;
366                                            clrline(begin_line, SCREEN_ROWS);
367                                            break;
368                                    case KEY_LEFT:
369                                    case 'q':
370                                    case 'Q':
371                                            loop = 0;
372                                            break;
373                                    case '?':
374                                    case 'h':
375                                    case 'H':
376                                            if (!show_help)
377                                            {
378                                                  break;                                                  break;
379                                          }                                          }
                                 }  
380    
381                                  clrline(begin_line, screen_lines);                                          // Display help information
382                                  line = begin_line;                                          show_help = 0;
383                                  moveto(line, 0);                                          display_file_ex(DATA_READ_HELP, begin_line, 1);
384                                            show_help = 1;
385    
386                                            // Refresh after display help information
387                                            line_current -= (line - 1);
388                                            line = begin_line;
389                                            max_lines = SCREEN_ROWS - 1;
390                                            clrline(begin_line, SCREEN_ROWS);
391                                            break;
392                                    default:
393                                            input_ok = 0;
394                                            break;
395                                    }
396    
397                                  continue;                                  BBS_last_access_tm = time(0);
398                          }                          }
399    
400                          i = strlen(buffer);                          continue;
401                          if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  }
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
                         }  
                         prints(buffer);  
                         iflush();  
402    
403                          line++;                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
404                    if (len >= LINE_BUFFER_LEN)
405                    {
406                            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;
409                  }                  }
                 if (wait)  
                         ch = press_any_key();  
                 else  
                         ch = 0;  
410    
411          exit:                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
412                  fclose(fin);                  buffer[len] = '\0';
413    
414                  return ch;                  moveto(line, 0);
415                    clrtoeol();
416                    prints("%s", buffer);
417                    line_current++;
418                    line++;
419          }          }
420    
421          return -1;  cleanup:
422            return ch;
423  }  }
424    
425  int show_top(char *status)  int show_top(const char *status)
426  {  {
427          char buffer[256];          char status_f[21];
428            int truncate;
429            int status_len;
430            int section_name_len;
431            int len;
432    
433          str_space(buffer, 20 - strlen(BBS_current_section_name));          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)
438            {
439                    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);
444            if (truncate)
445            {
446                    log_error("Section name is truncated\n");
447            }
448    
449          moveto(1, 0);          moveto(1, 0);
450          prints("\033[1;44;33m%-20s \033[37m%20s"          clrtoeol();
451                     "         %s\033[33m讨论区 [%s]\033[m",          prints("\033[1;44;33m%s\033[37m%*s%*s\033[33m 讨论区 [%s]\033[m",
452                     status, BBS_name, buffer, 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[256], str_time_onine[20], buffer[256];          char str_time[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, 256);          get_time_str(str_time, sizeof(str_time));
469          str_space(buffer, 33 - strlen(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_lines, 0);          moveto(SCREEN_ROWS, 0);
486          prints("\033[1;44;33m[\033[36m%s\033[33m]"          clrtoeol();
487                     "%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, buffer, 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[256], buffer[260];          static int line_current = 0;
498          FILE *fin;          static const void *p_shm = NULL;
499          int i, j;          static size_t data_len;
500          static long int line;          static long line_total;
501            static const void *p_data;
502            static const long *p_line_offsets;
503    
504            static time_t t_last_show = 0;
505            static int line_last = 0;
506    
507            char buffer[LINE_BUFFER_LEN];
508            long int len;
509    
510          sprintf(filename, "%sdata/active_board.txt", app_home_dir);          if (p_shm == NULL)
511            {
512                    if ((p_shm = get_file_shm(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
513                    {
514                            log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
515                            return KEY_NULL;
516                    }
517            }
518    
519          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          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          moveto(3, 0);          clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
530    
531          if ((fin = fopen(filename, "r")) != NULL)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
532          {          {
533                  for (j = 0; j < line; j++)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
534                    if (len >= LINE_BUFFER_LEN)
535                  {                  {
536                          if (fgets(buffer, 255, fin) == NULL)                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
537                          {                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
538                                  line = 0;                          len = LINE_BUFFER_LEN - 1;
                                 rewind(fin);  
                                 break;  
                         }  
539                  }                  }
540    
541                  for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
542                    buffer[len] = '\0';
543    
544                    moveto(3 + i, 0);
545                    prints("%s", buffer);
546    
547                    line_current++;
548                    if (line_current >= line_total)
549                  {                  {
550                          if (fgets(buffer, 255, fin) == NULL)                          line_current = 0;
551                          {                          break;
                                 line = 0;  
                                 if (j == 0)  
                                 {  
                                         rewind(fin);  
                                         if (fgets(buffer, 255, fin) == NULL)  
                                         {  
                                                 break;  
                                         }  
                                 }  
                                 else  
                                 {  
                                         break;  
                                 }  
                         }  
                         line++;  
                         i = strlen(buffer);  
                         if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
                         }  
                         prints(buffer);  
                         iflush();  
552                  }                  }
                 fclose(fin);  
553          }          }
554    
555          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