/[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.21 by sysadm, Mon Apr 28 03:31:00 2025 UTC Revision 1.52 by sysadm, Thu May 15 14:17:53 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 "bbs.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
19    #include "str_process.h"
20    #include "log.h"
21  #include "io.h"  #include "io.h"
22    #include "screen.h"
23    #include <fcntl.h>
24    #include <string.h>
25    #include <ctype.h>
26    #include <unistd.h>
27    #include <stdlib.h>
28    #include <errno.h>
29  #include <sys/types.h>  #include <sys/types.h>
30  #include <sys/stat.h>  #include <sys/stat.h>
31  #include <unistd.h>  #include <sys/param.h>
32    #include <sys/mman.h>
33    
34  #define ACTIVE_BOARD_HEIGHT 8  #define ACTIVE_BOARD_HEIGHT 8
35    
36  int screen_lines = 24;  int screen_rows = 24;
37    int screen_cols = 80;
38    
39  void moveto(int row, int col)  void moveto(int row, int col)
40  {  {
# Line 36  void moveto(int row, int col) Line 46  void moveto(int row, int col)
46          {          {
47                  prints("\r");                  prints("\r");
48          }          }
         iflush();  
49  }  }
50    
51  void clrtoeol()  void clrtoeol()
52  {  {
53          prints("\033[K");          prints("\033[K");
         iflush();  
54  }  }
55    
56  void clrline(int line_begin, int line_end)  void clrline(int line_begin, int line_end)
# Line 54  void clrline(int line_begin, int line_en Line 62  void clrline(int line_begin, int line_en
62                  moveto(i, 0);                  moveto(i, 0);
63                  prints("\033[K");                  prints("\033[K");
64          }          }
   
         iflush();  
65  }  }
66    
67  void clrtobot(int line_begin)  void clrtobot(int line_begin)
68  {  {
69          clrline(line_begin, screen_lines);          moveto(line_begin, 0);
70            prints("\033[J");
71          moveto(line_begin, 0);          moveto(line_begin, 0);
72  }  }
73    
74  void clearscr()  void clearscr()
75  {  {
76          prints("\33[2J");          prints("\033[2J");
77          moveto(0, 0);          moveto(0, 0);
         iflush();  
78  }  }
79    
80  int press_any_key()  int press_any_key()
81  {  {
82          moveto(screen_lines, 0);          moveto(screen_rows, 0);
83          clrtoeol();          clrtoeol();
84    
85          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints("                           \033[1;33m按任意键继续...\033[0;37m");
86          iflush();          iflush();
87    
88          return igetch_t(60);          return igetch_t(MIN(MAX_DELAY_TIME, 60));
89  }  }
90    
91  void set_input_echo(int echo)  void set_input_echo(int echo)
# Line 94  void set_input_echo(int echo) Line 100  void set_input_echo(int echo)
100                  //    outc ('\x85'); // ASCII code 133                  //    outc ('\x85'); // ASCII code 133
101                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
102                  iflush();                  iflush();
103                  igetch_t(60);                  igetch(0);
104                  igetch_t(60);                  igetch_reset();
105          }          }
106  }  }
107    
108  int _str_input(char *buffer, int buffer_length, int echo_mode)  static int _str_input(char *buffer, int buf_size, int echo_mode)
109  {  {
110          char buf[256], ch;          int c;
111          int c, offset = 0, len, loop = 1, i, hz = 0;          int offset = 0;
112            int hz = 0;
113    
114          for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)          buffer[buf_size - 1] = '\0';
115          {          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)
116                  offset++;                  ;
         }  
117    
118          while (c = igetch_t(60))          igetch_reset();
119    
120            while (!SYS_server_exit)
121          {          {
122                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));
123    
124                    if (c == CR)
125                    {
126                            igetch_reset();
127                          break;                          break;
128                  if (c == LF)                  }
129                    else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe
130                    {
131                            return -1;
132                    }
133                    else if (c == LF || c == '\0')
134                    {
135                          continue;                          continue;
136                  if (c == BACKSPACE)                  }
137                    else if (c == BACKSPACE)
138                  {                  {
139                          if (offset > 0)                          if (offset > 0)
140                          {                          {
141                                  buffer[--offset] = '\0';                                  offset--;
142                                  prints("\b \b");                                  if (buffer[offset] < 0 || buffer[offset] > 127)
143                                  //            clrtoeol ();                                  {
144                                            prints("\033[D \033[D");
145                                            offset--;
146                                            if (offset < 0) // should not happen
147                                            {
148                                                    log_error("Offset of buffer is negative\n");
149                                                    offset = 0;
150                                            }
151                                    }
152                                    buffer[offset] = '\0';
153                                    prints("\033[D \033[D");
154                                  iflush();                                  iflush();
155                          }                          }
156                          continue;                          continue;
157                  }                  }
158                  if (c > 255 || iscntrl(c))                  else if (c > 255 || iscntrl(c))
159                  {                  {
160                          continue;                          continue;
161                  }                  }
162                  if (c > 127 && c <= 255)                  else if (c > 127 && c <= 255)
163                  {                  {
164                            if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character
165                            {
166                                    igetch(0); // Ignore 1 character
167                                    outc('\a');
168                                    iflush();
169                                    continue;
170                            }
171                          hz = (!hz);                          hz = (!hz);
172                  }                  }
173                  if (offset >= buffer_length)  
174                    if (offset + 1 > buf_size - 1)
175                  {                  {
176                          outc('\a');                          outc('\a');
177                            iflush();
178                          continue;                          continue;
179                  }                  }
180    
181                  buffer[offset++] = (char)c;                  buffer[offset++] = (char)c;
182                  buffer[offset] = '\0';                  buffer[offset] = '\0';
183    
184                  switch (echo_mode)                  switch (echo_mode)
185                  {                  {
186                  case DOECHO:                  case DOECHO:
# Line 156  int _str_input(char *buffer, int buffer_ Line 196  int _str_input(char *buffer, int buffer_
196                  }                  }
197          }          }
198    
         prints("\r\n");  
         iflush();  
   
199          return offset;          return offset;
200  }  }
201    
202  int str_input(char *buffer, int buffer_length, int echo_mode)  int str_input(char *buffer, int buf_size, int echo_mode)
203  {  {
204          int offset;          int len;
205    
206          memset(buffer, '\0', buffer_length);          buffer[0] = '\0';
207    
208          offset = _str_input(buffer, buffer_length, echo_mode);          len = _str_input(buffer, buf_size, echo_mode);
209    
210          return offset;          prints("\r\n");
211            iflush();
212    
213            return len;
214  };  };
215    
216  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)
217  {  {
218          int len;          int len;
219    
220          moveto(row, col);          moveto(row, col);
         iflush();  
221          prints(prompt);          prints(prompt);
222          prints(buffer);          prints(buffer);
223          iflush();          iflush();
224    
225          len = _str_input(buffer, buffer_length, echo_mode);          len = _str_input(buffer, buf_size, echo_mode);
226    
227          return len;          return len;
228  }  }
229    
230  int display_file(const char *filename)  int display_file_ex(const char *filename, int begin_line, int wait)
231  {  {
232          char buffer[260];          static int show_help = 1;
233          FILE *fin;          char buffer[LINE_BUFFER_LEN];
234          int i;          void *p_data;
235            int ch = 0;
236            int input_ok, line, max_lines;
237            long int c_line_current = 0;
238            long int c_line_total = 0;
239            int fd;
240            struct stat sb;
241            long *p_line_offsets;
242            long int len;
243            long int percentile;
244            int loop = 1;
245    
246          if ((fin = fopen(filename, "r")) == NULL)          if ((fd = open(filename, O_RDONLY)) < 0)
247          {          {
248                    log_error("open(%s) error (%d)\n", filename, errno);
249                  return -1;                  return -1;
250          }          }
251    
252          while (fgets(buffer, 255, fin))          if (fstat(fd, &sb) < 0)
253          {          {
254                  i = strlen(buffer);                  log_error("fstat(fd) error (%d)\n", errno);
255                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  return -1;
                 {  
                         buffer[i - 1] = '\r';  
                         buffer[i] = '\n';  
                         buffer[i + 1] = '\0';  
                 }  
                 prints(buffer);  
                 iflush();  
256          }          }
         fclose(fin);  
257    
258          return 0;          p_data = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0L);
259  }          if (p_data == MAP_FAILED)
260            {
261                    log_error("mmap() error (%d)\n", errno);
262                    return -2;
263            }
264    
265  int display_file_ex(const char *filename, int begin_line, int wait)          if (close(fd) < 0)
266  {          {
267          char buffer[260], temp[256];                  log_error("close(fd) error (%d)\n", errno);
268          int i, ch, input_ok, max_lines;                  return -1;
269          long int line, c_line_begin = 0, c_line_total = 0;          }
270          long int f_line, f_size, f_offset;  
271          FILE *fin;          p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
272          struct stat f_stat;  
273            c_line_total = split_data_lines(p_data, screen_cols, p_line_offsets, MAX_FILE_LINES);
274    
275          max_lines = screen_lines - 1;          clrline(begin_line, screen_rows);
         clrline(begin_line, screen_lines);  
276          line = begin_line;          line = begin_line;
277          moveto(line, 0);          max_lines = screen_rows - 1;
278    
279          if ((fin = fopen(filename, "r")) != NULL)          while (!SYS_server_exit && loop)
280          {          {
281                  if (fstat(fileno(fin), &f_stat) != 0)                  if (c_line_current >= c_line_total && c_line_total <= screen_rows - 2)
282                  {                  {
283                          log_error("Get file stat failed\n");                          if (wait)
284                          return -1;                          {
285                  }                                  ch = press_any_key();
286                  f_size = f_stat.st_size;                          }
287                            else
288                            {
289                                    iflush();
290                            }
291    
292                  while (fgets(buffer, 255, fin))                          loop = 0;
293                          c_line_total++;                          break;
294                  rewind(fin);                  }
295    
296                  while (fgets(buffer, 255, fin))                  if (c_line_current >= c_line_total || line >= max_lines)
297                  {                  {
298                          if (line >= max_lines)                          if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total)
299                          {                          {
300                                  f_offset = ftell(fin);                                  percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
301                            }
302                            else
303                            {
304                                    percentile = 100;
305                            }
306    
307                                  moveto(screen_lines, 0);                          moveto(screen_rows, 0);
308                                  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",
309                                             (f_offset - strlen(buffer)) * 100 / f_size);                                     (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
310                                  iflush();                                     (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
311                            iflush();
312    
313                                  input_ok = 0;                          input_ok = 0;
314                                  while (!input_ok)                          while (!SYS_server_exit && !input_ok)
315                            {
316                                    ch = igetch_t(MAX_DELAY_TIME);
317                                    input_ok = 1;
318                                    switch (ch)
319                                  {                                  {
320                                          ch = igetch_t(MAX_DELAY_TIME);                                  case KEY_NULL:
321                                          input_ok = 1;                                  case KEY_TIMEOUT:
322                                          switch (ch)                                          goto cleanup;
323                                    case KEY_HOME:
324                                            c_line_current = 0;
325                                            line = begin_line;
326                                            max_lines = screen_rows - 1;
327                                            clrline(begin_line, screen_rows);
328                                            break;
329                                    case KEY_END:
330                                            c_line_current = c_line_total - (screen_rows - 2);
331                                            line = begin_line;
332                                            max_lines = screen_rows - 1;
333                                            clrline(begin_line, screen_rows);
334                                            break;
335                                    case KEY_UP:
336                                            if (c_line_current - line < 0) // Reach top
337                                          {                                          {
                                         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;  
                                                 }  
                                                 break;  
                                         case KEY_DOWN:  
                                         case CR:  
                                                 c_line_begin++;  
                                                 rewind(fin);  
                                                 for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                 {  
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
338                                                  break;                                                  break;
339                                          case KEY_PGUP:                                          }
340                                          case Ctrl('B'):                                          c_line_current -= line;
341                                                  if (c_line_begin > 0)                                          line = begin_line;
342                                                          c_line_begin -= (max_lines - begin_line - 1);                                          // max_lines = begin_line + 1;
343                                                  else                                          // prints("\033[T"); // Scroll down 1 line
344                                                          goto exit;                                          max_lines = screen_rows - 1; // Legacy Fterm only works with this line
345                                                  if (c_line_begin < 0)                                          break;
346                                                          c_line_begin = 0;                                  case CR:
347                                                  rewind(fin);                                          igetch_reset();
348                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                  case KEY_DOWN:
349                                                  {                                          if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
350                                                          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;  
                                                 }  
351                                                  break;                                                  break;
352                                          case KEY_NULL:                                          }
353                                          case KEY_TIMEOUT:                                          c_line_current += ((screen_rows - 2) - (line - 1));
354                                          case KEY_LEFT:                                          line = screen_rows - 2;
355                                          case 'q':                                          max_lines = screen_rows - 1;
356                                          case 'Q':                                          moveto(screen_rows, 0);
357                                                  goto exit;                                          clrtoeol();
358                                            prints("\033[S"); // Scroll up 1 line
359                                            break;
360                                    case KEY_PGUP:
361                                    case Ctrl('B'):
362                                            if (c_line_current - line < 0) // Reach top
363                                            {
364                                                  break;                                                  break;
365                                          case '?':                                          }
366                                          case 'h':                                          c_line_current -= ((screen_rows - 3) + (line - 1));
367                                          case 'H':                                          if (c_line_current < 0)
368                                                  // Display help information                                          {
369                                                  strcpy(temp, app_home_dir);                                                  c_line_current = 0;
370                                                  strcat(temp, "data/read_help.txt");                                          }
371                                                  display_file_ex(temp, begin_line, 1);                                          line = begin_line;
372                                            max_lines = screen_rows - 1;
373                                                  // Refresh after display help information                                          clrline(begin_line, screen_rows);
374                                                  rewind(fin);                                          break;
375                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                  case KEY_RIGHT:
376                                                  {                                  case KEY_PGDN:
377                                                          if (fgets(buffer, 255, fin) == NULL)                                  case Ctrl('F'):
378                                                                  goto exit;                                  case KEY_SPACE:
379                                                  }                                          if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom
380                                            {
381                                                  break;                                                  break;
382                                          default:                                          }
383                                                  input_ok = 0;                                          c_line_current += (screen_rows - 3) - (line - 1);
384                                            if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display
385                                            {
386                                                    c_line_current = c_line_total - (screen_rows - 2);
387                                            }
388                                            line = begin_line;
389                                            max_lines = screen_rows - 1;
390                                            clrline(begin_line, screen_rows);
391                                            break;
392                                    case KEY_LEFT:
393                                    case 'q':
394                                    case 'Q':
395                                            loop = 0;
396                                            break;
397                                    case '?':
398                                    case 'h':
399                                    case 'H':
400                                            if (!show_help)
401                                            {
402                                                  break;                                                  break;
403                                          }                                          }
                                 }  
404    
405                                  clrline(begin_line, screen_lines);                                          // Display help information
406                                  line = begin_line;                                          show_help = 0;
407                                  moveto(line, 0);                                          display_file_ex(DATA_READ_HELP, begin_line, 1);
408                                            show_help = 1;
409    
410                                            // Refresh after display help information
411                                            c_line_current -= (line - 1);
412                                            line = begin_line;
413                                            max_lines = screen_rows - 1;
414                                            clrline(begin_line, screen_rows);
415                                            break;
416                                    default:
417                                            log_std("Input: %d\n", ch);
418                                            input_ok = 0;
419                                            break;
420                                    }
421    
422                                  continue;                                  BBS_last_access_tm = time(0);
423                          }                          }
424    
425                          i = strlen(buffer);                          continue;
426                          if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  }
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
                         }  
                         prints(buffer);  
                         iflush();  
427    
428                          line++;                  len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
429                    if (len >= LINE_BUFFER_LEN)
430                    {
431                            log_error("Error length exceeds buffer size: %d\n", len);
432                            len = LINE_BUFFER_LEN - 1;
433                  }                  }
                 if (wait)  
                         ch = press_any_key();  
                 else  
                         ch = 0;  
434    
435          exit:                  memcpy(buffer, (const char *)p_data + p_line_offsets[c_line_current], (size_t)len);
436                  fclose(fin);                  buffer[len] = '\0';
437    
438                    moveto(line, 0);
439                    clrtoeol();
440                    prints("%s", buffer);
441                    c_line_current++;
442                    line++;
443            }
444    
445                  return ch;  cleanup:
446            if (munmap(p_data, (size_t)sb.st_size) < 0)
447            {
448                    log_error("munmap() error (%d)\n", errno);
449          }          }
450    
451          return -1;          free(p_line_offsets);
452    
453            return ch;
454  }  }
455    
456  int show_top(char *status)  int show_top(char *status)
457  {  {
458          char buffer[256];          int end_of_line;
459            int display_len;
460            int len;
461    
462            char space1[LINE_BUFFER_LEN];
463            char space2[LINE_BUFFER_LEN];
464    
465            len = split_line(status, 20, &end_of_line, &display_len);
466            if (end_of_line)
467            {
468                    status[len] = '\0';
469            }
470            str_space(space1, 31 - display_len);
471    
472          str_space(buffer, 20 - strlen(BBS_current_section_name));          len = split_line(BBS_current_section_name, 20, &end_of_line, &display_len);
473            if (end_of_line)
474            {
475                    status[len] = '\0';
476            }
477            str_space(space2, 30 - display_len);
478    
479          moveto(1, 0);          moveto(1, 0);
480          prints("\033[1;44;33m%-20s \033[37m%20s"          clrtoeol();
481                     "         %s\033[33m讨论区 [%s]\033[m",          prints("\033[1;44;33m%s \033[37m%s%s%s\033[33m 讨论区 [%s]\033[m",
482                     status, BBS_name, buffer, BBS_current_section_name);                     status, space1, BBS_name, space2, BBS_current_section_name);
483          iflush();          iflush();
484    
485          return 0;          return 0;
# Line 397  int show_top(char *status) Line 487  int show_top(char *status)
487    
488  int show_bottom(char *msg)  int show_bottom(char *msg)
489  {  {
490          char str_time[256], str_time_onine[20], buffer[256];          char str_time[LINE_BUFFER_LEN];
491            char space[LINE_BUFFER_LEN];
492          time_t time_online;          time_t time_online;
493          struct tm *tm_online;          struct tm *tm_online;
494    
495          get_time_str(str_time, 256);          get_time_str(str_time, sizeof(str_time));
496          str_space(buffer, 33 - strlen(BBS_username));          str_space(space, 34 - (int)strnlen(BBS_username, sizeof(BBS_username)));
497    
498          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
499          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
500    
501          moveto(screen_lines, 0);          moveto(screen_rows, 0);
502          prints("\033[1;44;33m[\033[36m%s\033[33m]"          clrtoeol();
503                     "%s帐号[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]%s帐号[\033[36m%s\033[33m]"
504                     "[\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",
505                     str_time, buffer, BBS_username, tm_online->tm_mday - 1,                     str_time, space, BBS_username, tm_online->tm_mday - 1,
506                     tm_online->tm_hour, tm_online->tm_min);                     tm_online->tm_hour, tm_online->tm_min);
507          iflush();          iflush();
508    
# Line 420  int show_bottom(char *msg) Line 511  int show_bottom(char *msg)
511    
512  int show_active_board()  int show_active_board()
513  {  {
514          char filename[256], buffer[260];          char filename[FILE_PATH_LEN];
515            char buffer[LINE_BUFFER_LEN];
516          FILE *fin;          FILE *fin;
517          int i, j;          static int line;
518          static long int line;          int len;
519            int end_of_line;
520          sprintf(filename, "%sdata/active_board.txt", app_home_dir);          int display_len;
521    
522          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
523    
524          moveto(3, 0);          if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)
525            {
526                    log_error("Unable to open file %s\n", filename);
527                    return -1;
528            }
529    
530          if ((fin = fopen(filename, "r")) != NULL)          for (int i = 0; i < line; i++)
531          {          {
532                  for (j = 0; j < line; j++)                  if (fgets(buffer, sizeof(buffer), fin) == NULL)
533                  {                  {
534                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
535                          {                          rewind(fin);
536                                  line = 0;                          break;
                                 rewind(fin);  
                                 break;  
                         }  
537                  }                  }
538            }
539    
540                  for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
541            {
542                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
543                  {                  {
544                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
545                          {                          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();  
546                  }                  }
547                  fclose(fin);                  line++;
548                    len = split_line(buffer, screen_cols, &end_of_line, &display_len);
549                    buffer[len] = '\0'; // Truncate over-length line
550                    moveto(3 + i, 0);
551                    prints("%s", buffer);
552          }          }
553            iflush();
554    
555            fclose(fin);
556    
557          return 0;          return 0;
558  }  }


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

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