/[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.12 by sysadm, Mon Mar 21 17:08:21 2005 UTC Revision 1.102 by sysadm, Sat Jun 21 02:15:18 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 "editor.h"
20    #include "file_loader.h"
21  #include "io.h"  #include "io.h"
22  #include <sys/types.h>  #include "log.h"
23  #include <sys/stat.h>  #include "login.h"
24    #include "screen.h"
25    #include "str_process.h"
26    #include <ctype.h>
27    #include <errno.h>
28    #include <fcntl.h>
29    #include <string.h>
30    #include <stdlib.h>
31  #include <unistd.h>  #include <unistd.h>
32    #include <sys/param.h>
33    #include <sys/stat.h>
34    #include <sys/shm.h>
35    #include <sys/types.h>
36    
37  #define ACTIVE_BOARD_HEIGHT     8  #define ACTIVE_BOARD_HEIGHT 8
38    
39  int screen_lines = 24;  #define STR_TOP_LEFT_MAX_LEN 40
40    #define STR_TOP_MIDDLE_MAX_LEN 20
41    #define STR_TOP_RIGHT_MAX_LEN 20
42    
43  void  void moveto(int row, int col)
 moveto (int row, int col)  
44  {  {
45    if (row >= 0)          if (row >= 0)
46      {          {
47        prints ("\033[%d;%dH", row, col);                  prints("\033[%d;%dH", row, col);
48      }          }
49    else          else
50      {          {
51        prints ("\r");                  prints("\r");
52      }          }
   iflush ();  
53  }  }
54    
55  void  void clrtoeol()
 clrtoeol ()  
56  {  {
57    prints ("\033[K");          prints(CTRL_SEQ_CLR_LINE);
   iflush ();  
58  }  }
59    
60  void  void clrline(int line_begin, int line_end)
 clrline (int line_begin, int line_end)  
61  {  {
62    int i;          int i;
63    
64    for (i = line_begin; i <= line_end; i++)          for (i = line_begin; i <= line_end; i++)
65      {          {
66        moveto (i, 0);                  moveto(i, 0);
67        prints ("\033[K");                  prints(CTRL_SEQ_CLR_LINE);
68      }          }
   
   iflush ();  
69  }  }
70    
71  void  void clrtobot(int line_begin)
 clearscr ()  
72  {  {
73    prints ("\33[2J");          moveto(line_begin, 0);
74    moveto (0, 0);          prints("\033[J");
75    iflush ();          moveto(line_begin, 0);
76  }  }
77    
78  int  void clearscr()
 press_any_key ()  
79  {  {
80    moveto (screen_lines, 0);          prints("\033[2J");
81    clrtoeol ();          moveto(0, 0);
   
   prints ("                           \033[1;33m按任意键继续...\033[0;37m");  
   iflush ();  
   
   return igetch ();  
82  }  }
83    
84  void  int press_any_key()
 set_input_echo (int echo)  
85  {  {
86    char temp[256];          moveto(SCREEN_ROWS, 0);
87            clrtoeol();
88    
89            prints("                           \033[1;33m按任意键继续...\033[0;37m");
90            iflush();
91    
92    if (echo)          return igetch_t(MIN(MAX_DELAY_TIME, 60));
     {  
       outc ('\x83');            // ASCII code 131  
       iflush ();  
     }  
   else  
     {  
 //    outc ('\x85'); // ASCII code 133  
       prints ("\xff\xfb\x01\xff\xfb\x03");  
       iflush ();  
       igetch ();  
       igetch ();  
     }  
93  }  }
94    
95  int  void set_input_echo(int echo)
 str_input (char *buffer, int buffer_length, int echo_mode)  
96  {  {
97    char buf[256], ch;          if (echo)
   int c, offset = 0, len, loop = 1, i, hz = 0;  
   
   memset (buffer, '\0', buffer_length);  
   
   while (c = igetch ())  
     {  
       if (c == CR)  
         break;  
       if (c == LF)  
         continue;  
       if (c == BACKSPACE)  
         {  
           if (offset > 0)  
             {  
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
         }  
       if (c > 255 || iscntrl (c))  
         {  
           continue;  
         }  
       if (c > 127 && c <= 255)  
         {  
           hz = (!hz);  
         }  
       if (offset >= buffer_length)  
98          {          {
99            outc ('\a');                  outc('\x83'); // ASCII code 131
100            continue;                  iflush();
101          }          }
102        buffer[offset++] = (char) c;          else
       buffer[offset] = '\0';  
       switch (echo_mode)  
103          {          {
104          case 0:                  //    outc ('\x85'); // ASCII code 133
105            outc ((char) c);                  prints("\xff\xfb\x01\xff\xfb\x03");
106            break;                  iflush();
107          case 1:                  igetch(0);
108            outc ('*');                  igetch_reset();
           break;  
109          }          }
110        if (!hz)  }
111    
112    static int _str_input(char *buffer, int buf_size, int echo_mode)
113    {
114            int c;
115            int offset = 0;
116            int hz = 0;
117    
118            buffer[buf_size - 1] = '\0';
119            for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)
120                    ;
121    
122            igetch_reset();
123    
124            while (!SYS_server_exit)
125          {          {
126            iflush ();                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));
127          }  
128      }                  if (c == CR)
129                    {
130                            igetch_reset();
131                            break;
132                    }
133                    else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe
134                    {
135                            return -1;
136                    }
137                    else if (c == LF || c == '\0')
138                    {
139                            continue;
140                    }
141                    else if (c == BACKSPACE)
142                    {
143                            if (offset > 0)
144                            {
145                                    offset--;
146                                    if (buffer[offset] < 0 || buffer[offset] > 127)
147                                    {
148                                            prints("\033[D \033[D");
149                                            offset--;
150                                            if (offset < 0) // should not happen
151                                            {
152                                                    log_error("Offset of buffer is negative\n");
153                                                    offset = 0;
154                                            }
155                                    }
156                                    buffer[offset] = '\0';
157                                    prints("\033[D \033[D");
158                                    iflush();
159                            }
160                            continue;
161                    }
162                    else if (c > 255 || iscntrl(c))
163                    {
164                            continue;
165                    }
166                    else if (c > 127 && c <= 255)
167                    {
168                            if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character
169                            {
170                                    igetch(0); // Ignore 1 character
171                                    outc('\a');
172                                    iflush();
173                                    continue;
174                            }
175                            hz = (!hz);
176                    }
177    
178                    if (offset + 1 > buf_size - 1)
179                    {
180                            outc('\a');
181                            iflush();
182                            continue;
183                    }
184    
185    prints ("\r\n");                  buffer[offset++] = (char)c;
186    iflush ();                  buffer[offset] = '\0';
187    
188    return offset;                  switch (echo_mode)
189                    {
190                    case DOECHO:
191                            outc((char)c);
192                            break;
193                    case NOECHO:
194                            outc('*');
195                            break;
196                    }
197                    if (!hz)
198                    {
199                            iflush();
200                    }
201            }
202    
203            return offset;
204  }  }
205    
206  int  int str_input(char *buffer, int buf_size, int echo_mode)
 display_file (const char *filename)  
207  {  {
208    char buffer[260];          int len;
   FILE *fin;  
   int i;  
209    
210    if ((fin = fopen (filename, "r")) == NULL)          buffer[0] = '\0';
     {  
       return -1;  
     }  
211    
212    while (fgets (buffer, 255, fin))          len = _str_input(buffer, buf_size, echo_mode);
213      {  
214        i = strlen (buffer);          prints("\r\n");
215        if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')          iflush();
216          {  
217            buffer[i - 1] = '\r';          return len;
218            buffer[i] = '\n';  };
219            buffer[i + 1] = '\0';  
220          }  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int echo_mode)
221        prints (buffer);  {
222        iflush ();          int len;
223      }  
224    fclose (fin);          moveto(row, col);
225            prints("%s", prompt);
226            prints("%s", buffer);
227            iflush();
228    
229    return 0;          len = _str_input(buffer, buf_size, echo_mode);
230    
231            return len;
232  }  }
233    
234  int  int display_data(const void *p_data, long display_line_total, const long *p_line_offsets, int eof_exit,
235  display_file_ex (const char *filename, int begin_line, int wait)                                   display_data_key_handler key_handler, const char *help_filename)
236  {  {
237    char buffer[260], temp[256];          static int show_help = 1;
238    int i, ch, input_ok;          char buffer[LINE_BUFFER_LEN];
239    long int line, c_line_begin = 0, c_line_total = 0;          DISPLAY_CTX ctx;
240    long int f_line, f_size, f_offset;          int ch = 0;
241    FILE *fin;          int input_ok;
242    struct stat f_stat;          const int screen_begin_row = 1;
243            const int screen_row_total = SCREEN_ROWS - screen_begin_row;
244            int output_current_row = screen_begin_row;
245            int output_end_row = SCREEN_ROWS - 1;
246            long int line_current = 0;
247            long int len;
248            long int percentile;
249            int loop;
250            int eol, display_len;
251    
252    clrline (begin_line, screen_lines);          clrline(output_current_row, SCREEN_ROWS);
   line = begin_line;  
   moveto (line, 0);  
253    
254    if ((fin = fopen (filename, "r")) != NULL)          // update msg_ext with extended key handler
255      {          if (key_handler(&ch, &ctx) != 0)
       if (fstat (fileno (fin), &f_stat) != 0)  
256          {          {
257            log_error ("Get file stat failed\n");                  return ch;
           return -1;  
258          }          }
       f_size = f_stat.st_size;  
   
       while (fgets (buffer, 255, fin))  
         c_line_total++;  
       rewind (fin);  
259    
260        while (fgets (buffer, 255, fin))          loop = 1;
261            while (!SYS_server_exit && loop)
262          {          {
263            if (line >= screen_lines)                  if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total)
             {  
               f_offset = ftell (fin);  
   
               moveto (screen_lines, 0);  
               prints  
                 ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",  
                  (f_offset - strlen (buffer)) * 100 / f_size);  
               iflush ();  
   
               input_ok = 0;  
               while (!input_ok)  
264                  {                  {
265                    ch = igetch ();                          if (eof_exit == 1)
                   input_ok = 1;  
                   switch (ch)  
                     {  
                     case KEY_UP:  
                       c_line_begin--;  
                       if (c_line_begin >= 0)  
266                          {                          {
267                            rewind (fin);                                  ch = press_any_key();
                           for (f_line = 0; f_line < c_line_begin; f_line++)  
                             {  
                               if (fgets (buffer, 255, fin) == NULL)  
                                 goto exit;  
                             }  
268                          }                          }
269                        else                          else // if (eof_exit == 2)
270                          {                          {
271                            goto exit;                                  iflush();
272                          }                          }
273                        break;  
274                      case KEY_DOWN:                          loop = 0;
275                      case CR:                          break;
276                        c_line_begin++;                  }
277                        rewind (fin);  
278                        for (f_line = 0; f_line < c_line_begin; f_line++)                  if (line_current >= display_line_total || output_current_row > output_end_row)
279                    {
280                            ctx.reach_begin = (line_current < output_current_row ? 1 : 0);
281    
282                            if (line_current - (output_current_row - screen_begin_row) + screen_row_total < display_line_total)
283                          {                          {
284                            if (fgets (buffer, 255, fin) == NULL)                                  percentile = (line_current - (output_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total;
285                              goto exit;                                  ctx.reach_end = 0;
286                          }                          }
287                        break;                          else
                     case KEY_PGUP:  
                     case Ctrl ('B'):  
                       if (c_line_begin > 0)  
                         c_line_begin -= (screen_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++)  
288                          {                          {
289                            if (fgets (buffer, 255, fin) == NULL)                                  percentile = 100;
290                              goto exit;                                  ctx.reach_end = 1;
291                          }                          }
292                        break;  
293                      case KEY_RIGHT:                          ctx.line_top = line_current - (output_current_row - screen_begin_row) + 1;
294                      case KEY_PGDN:                          ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);
295                      case Ctrl ('F'):  
296                      case KEY_SPACE:                          snprintf(buffer, sizeof(buffer),
297                        c_line_begin += (screen_lines - begin_line - 1);                                           "\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s",
298                        if (c_line_begin + (screen_lines - begin_line) >                                           ctx.line_top,
299                            c_line_total)                                           ctx.line_bottom,
300                          c_line_begin =                                           percentile,
301                            c_line_total - (screen_lines - begin_line);                                           ctx.msg);
302                        rewind (fin);  
303                        for (f_line = 0; f_line < c_line_begin; f_line++)                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
304                            for (; display_len < SCREEN_COLS; display_len++)
305                          {                          {
306                            if (fgets (buffer, 255, fin) == NULL)                                  buffer[len++] = ' ';
                             goto exit;  
307                          }                          }
308                        break;                          buffer[len] = '\0';
309                      case KEY_LEFT:                          strncat(buffer, "\033[m", sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer)));
310                      case 'q':  
311                      case 'Q':                          moveto(SCREEN_ROWS, 0);
312                        goto exit;                          prints("%s", buffer);
313                        break;                          iflush();
314                      case '?':  
315                      case 'h':                          input_ok = 0;
316                      case 'H':                          while (!SYS_server_exit && !input_ok)
                       //Display help information  
                       strcpy (temp, app_home_dir);  
                       strcat (temp, "data/read_help.txt");  
                       display_file_ex (temp, begin_line, 1);  
   
                       //Refresh after display help information  
                       rewind (fin);  
                       for (f_line = 0; f_line < c_line_begin; f_line++)  
317                          {                          {
318                            if (fgets (buffer, 255, fin) == NULL)                                  ch = igetch_t(MAX_DELAY_TIME);
319                              goto exit;                                  input_ok = 1;
320    
321                                    // extended key handler
322                                    if (key_handler(&ch, &ctx) != 0)
323                                    {
324                                            goto cleanup;
325                                    }
326    
327                                    switch (ch)
328                                    {
329                                    case KEY_NULL:
330                                    case KEY_TIMEOUT:
331                                            goto cleanup;
332                                    case KEY_HOME:
333                                            if (line_current - output_current_row < 0) // Reach begin
334                                            {
335                                                    break;
336                                            }
337                                            line_current = 0;
338                                            output_current_row = screen_begin_row;
339                                            output_end_row = SCREEN_ROWS - 1;
340                                            clrline(output_current_row, SCREEN_ROWS);
341                                            break;
342                                    case KEY_END:
343                                            if (display_line_total < screen_row_total)
344                                            {
345                                                    break;
346                                            }
347                                            line_current = display_line_total - screen_row_total;
348                                            output_current_row = screen_begin_row;
349                                            output_end_row = SCREEN_ROWS - 1;
350                                            clrline(output_current_row, SCREEN_ROWS);
351                                            break;
352                                    case KEY_UP:
353                                            if (line_current - output_current_row < 0) // Reach begin
354                                            {
355                                                    break;
356                                            }
357                                            line_current -= output_current_row;
358                                            output_current_row = screen_begin_row;
359                                            // screen_end_line = screen_begin_line;
360                                            // prints("\033[T"); // Scroll down 1 line
361                                            output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
362                                            break;
363                                    case CR:
364                                            igetch_reset();
365                                    case KEY_SPACE:
366                                    case KEY_DOWN:
367                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end
368                                            {
369                                                    break;
370                                            }
371                                            line_current += (screen_row_total - (output_current_row - screen_begin_row));
372                                            output_current_row = screen_row_total;
373                                            output_end_row = SCREEN_ROWS - 1;
374                                            moveto(SCREEN_ROWS, 0);
375                                            clrtoeol();
376                                            prints("\033[S"); // Scroll up 1 line
377                                            break;
378                                    case KEY_PGUP:
379                                            if (line_current - output_current_row < 0) // Reach begin
380                                            {
381                                                    break;
382                                            }
383                                            line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
384                                            if (line_current < 0)
385                                            {
386                                                    line_current = 0;
387                                            }
388                                            output_current_row = screen_begin_row;
389                                            output_end_row = SCREEN_ROWS - 1;
390                                            clrline(output_current_row, SCREEN_ROWS);
391                                            break;
392                                    case KEY_PGDN:
393                                            if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end
394                                            {
395                                                    break;
396                                            }
397                                            line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
398                                            if (line_current + screen_row_total > display_line_total) // No enough lines to display
399                                            {
400                                                    line_current = display_line_total - screen_row_total;
401                                            }
402                                            output_current_row = screen_begin_row;
403                                            output_end_row = SCREEN_ROWS - 1;
404                                            clrline(output_current_row, SCREEN_ROWS);
405                                            break;
406                                    case KEY_ESC:
407                                    case KEY_LEFT:
408                                            loop = 0;
409                                            break;
410                                    case 'h':
411                                            if (!show_help) // Not reentrant
412                                            {
413                                                    break;
414                                            }
415                                            // Display help information
416                                            show_help = 0;
417                                            display_file(help_filename, 1);
418                                            show_help = 1;
419                                    case KEY_F5:
420                                            // Refresh after display help information
421                                            line_current -= (output_current_row - screen_begin_row);
422                                            output_current_row = screen_begin_row;
423                                            output_end_row = SCREEN_ROWS - 1;
424                                            clrline(output_current_row, SCREEN_ROWS);
425                                            break;
426                                    case 0: // Refresh bottom line
427                                            break;
428                                    default:
429                                            input_ok = 0;
430                                            break;
431                                    }
432    
433                                    BBS_last_access_tm = time(NULL);
434                          }                          }
435                        break;  
436                      default:                          continue;
                       input_ok = 0;  
                       break;  
                     }  
437                  }                  }
438    
439                clrline (begin_line, screen_lines);                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
440                line = begin_line;                  if (len >= sizeof(buffer))
441                moveto (line, 0);                  {
442                            log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
443                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
444                            len = sizeof(buffer) - 1;
445                    }
446                    else if (len < 0)
447                    {
448                            log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld \n",
449                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
450                            len = 0;
451                    }
452    
453                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
454                    buffer[len] = '\0';
455    
456                    moveto(output_current_row, 0);
457                    clrtoeol();
458                    prints("%s", buffer);
459                    line_current++;
460                    output_current_row++;
461            }
462    
463    cleanup:
464            return ch;
465    }
466    
467    static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
468    {
469            switch (*p_key)
470            {
471            case 0: // Set msg
472                    snprintf(p_ctx->msg, sizeof(p_ctx->msg),
473                                     "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
474                                     "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
475                                     "帮助[\033[32mh\033[33m] |");
476                    break;
477            }
478    
479            return 0;
480    }
481    
482                continue;  int display_file(const char *filename, int eof_exit)
483              }  {
484            int ret;
485            const void *p_shm;
486            size_t data_len;
487            long line_total;
488            const void *p_data;
489            const long *p_line_offsets;
490    
491            i = strlen (buffer);          if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
492            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')          {
493              {                  log_error("get_file_shm(%s) error\n", filename);
494                buffer[i - 1] = '\r';                  return KEY_NULL;
495                buffer[i] = '\n';          }
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
496    
497            line++;          if (user_online_update("VIEW_FILE") < 0)
498            {
499                    log_error("user_online_update(VIEW_FILE) error\n");
500          }          }
       if (wait)  
         ch = press_any_key ();  
       else  
         ch = 0;  
501    
502      exit:          ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);
       fclose (fin);  
503    
504        return ch;          if (detach_file_shm(p_shm) < 0)
505      }          {
506                    log_error("detach_file_shm(%s) error\n", filename);
507            }
508    
509    return -1;          return ret;
510  }  }
511    
512  int  int show_top(const char *str_left, const char *str_middle, const char *str_right)
 show_top (char *status)  
513  {  {
514    char buffer[256];          char str_left_f[STR_TOP_LEFT_MAX_LEN + 1];
515            char str_middle_f[STR_TOP_MIDDLE_MAX_LEN + 1];
516            char str_right_f[STR_TOP_RIGHT_MAX_LEN + 1];
517            int str_left_len;
518            int str_middle_len;
519            int str_right_len;
520            int eol;
521            int len;
522    
523            strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
524            str_left_f[sizeof(str_left_f) - 1] = '\0';
525            len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN, &eol, &str_left_len, 1);
526            str_left_f[len] = '\0';
527    
528            strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
529            str_middle_f[sizeof(str_middle_f) - 1] = '\0';
530            len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN, &eol, &str_middle_len, 1);
531            str_middle_f[len] = '\0';
532    
533    str_space (buffer, 20 - strlen (BBS_current_section_name));          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
534            str_right_f[sizeof(str_right_f) - 1] = '\0';
535            len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN, &eol, &str_right_len, 1);
536            str_right_f[len] = '\0';
537    
538    moveto (1, 0);          moveto(1, 0);
539    prints ("\033[1;44;33m%-20s \033[37m%20s"          clrtoeol();
540            "         %s\033[33m讨论区 [%s]\033[m",          prints("\033[1;44;33m%s\033[37m%*s\033[33m%*s\033[m",
541            status, BBS_name, buffer, BBS_current_section_name);                     str_left_f, 44 - str_left_len, str_middle_f, 36, str_right_f);
   iflush ();  
542    
543    return 0;          return 0;
544  }  }
545    
546  int  int show_bottom(const char *msg)
 show_bottom (char *msg)  
547  {  {
548    char str_time[256], str_time_onine[20], buffer[256];          char str_time[LINE_BUFFER_LEN];
549    time_t time_online;          time_t time_online;
550    struct tm *tm_online;          struct tm *tm_online;
551            char msg_f[LINE_BUFFER_LEN];
552            int eol;
553            int msg_len;
554            int len;
555            int len_username;
556            char str_tm_online[LINE_BUFFER_LEN];
557    
558            get_time_str(str_time, sizeof(str_time));
559    
560            msg_f[0] = '\0';
561            msg_len = 0;
562            if (msg != NULL)
563            {
564                    strncpy(msg_f, msg, sizeof(msg_f) - 1);
565                    msg_f[sizeof(msg_f) - 1] = '\0';
566                    len = split_line(msg_f, 23, &eol, &msg_len, 1);
567                    msg_f[len] = '\0';
568            }
569    
570    get_time_str (str_time, 256);          len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
   str_space (buffer, 33 - strlen (BBS_username));  
571    
572    time_online = time (0) - BBS_login_tm;          time_online = time(NULL) - BBS_login_tm;
573    tm_online = gmtime (&time_online);          tm_online = gmtime(&time_online);
574            if (tm_online->tm_mday > 1)
575            {
576                    snprintf(str_tm_online, sizeof(str_tm_online),
577                                     "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",
578                                     tm_online->tm_mday - 1, tm_online->tm_hour);
579            }
580            else
581            {
582                    snprintf(str_tm_online, sizeof(str_tm_online),
583                                     "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",
584                                     tm_online->tm_hour, tm_online->tm_min);
585            }
586    
587    moveto (screen_lines, 0);          moveto(SCREEN_ROWS, 0);
588    prints ("\033[1;44;33m[\033[36m%s\033[33m]"          clrtoeol();
589            "%s帐号[\033[36m%s\033[33m]"          prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m][%s\033[33m]\033[m",
590            "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",                     str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);
           str_time, buffer, BBS_username, tm_online->tm_mday - 1,  
           tm_online->tm_hour, tm_online->tm_min);  
   iflush ();  
591    
592    return 0;          return 0;
593  }  }
594    
595  int  int show_active_board()
 show_active_board ()  
596  {  {
597    char filename[256], buffer[260];          static int line_current = 0;
598    FILE *fin;          static const void *p_shm = NULL;
599    int i, j;          static size_t data_len;
600    static long int line;          static long line_total;
601            static const void *p_data;
602            static const long *p_line_offsets;
603    
604    sprintf (filename, "%sdata/active_board.txt", app_home_dir);          static time_t t_last_show = 0;
605            static int line_last = 0;
606    
607    clrline (3, 2 + ACTIVE_BOARD_HEIGHT);          char buffer[LINE_BUFFER_LEN];
608            long int len;
609    
610    moveto (3, 0);          if (p_shm == NULL)
611            {
612                    if ((p_shm = get_file_shm_readonly(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
613                    {
614                            log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
615                            return KEY_NULL;
616                    }
617            }
618    
619    if ((fin = fopen (filename, "r")) != NULL)          if (time(NULL) - t_last_show >= 10)
620      {          {
621        for (j = 0; j < line; j++)                  line_last = line_current;
622                    t_last_show = time(NULL);
623            }
624            else
625          {          {
626            if (fgets (buffer, 255, fin) == NULL)                  line_current = line_last;
             {  
               line = 0;  
               rewind (fin);  
               break;  
             }  
627          }          }
628    
629        for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
630    
631            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
632          {          {
633            if (fgets (buffer, 255, fin) == NULL)                  len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
634              {                  if (len >= LINE_BUFFER_LEN)
               line = 0;  
               if (j == 0)  
635                  {                  {
636                    rewind (fin);                          log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
637                    if (fgets (buffer, 255, fin) == NULL)                                            len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
638                      {                          len = LINE_BUFFER_LEN - 1;
                       break;  
                     }  
639                  }                  }
640                else  
641                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
642                    buffer[len] = '\0';
643    
644                    moveto(3 + i, 0);
645                    prints("%s", buffer);
646    
647                    line_current++;
648                    if (line_current >= line_total)
649                  {                  {
650                    break;                          line_current = 0;
651                            break;
652                  }                  }
             }  
           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 ();  
653          }          }
       fclose (fin);  
     }  
654    
655    return 0;          return 0;
656  }  }


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

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