/[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.100 by sysadm, Tue Jun 17 13:18:55 2025 UTC Revision 1.105 by sysadm, Wed Jul 2 06:25:02 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "screen.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
 #include "str_process.h"  
 #include "log.h"  
 #include "io.h"  
19  #include "editor.h"  #include "editor.h"
20  #include "file_loader.h"  #include "file_loader.h"
21  #include <fcntl.h>  #include "io.h"
22    #include "log.h"
23    #include "login.h"
24    #include "screen.h"
25    #include "str_process.h"
26  #include <ctype.h>  #include <ctype.h>
27    #include <errno.h>
28    #include <fcntl.h>
29  #include <string.h>  #include <string.h>
 #include <unistd.h>  
30  #include <stdlib.h>  #include <stdlib.h>
31  #include <errno.h>  #include <unistd.h>
 #include <sys/types.h>  
 #include <sys/stat.h>  
32  #include <sys/param.h>  #include <sys/param.h>
33    #include <sys/stat.h>
34  #include <sys/shm.h>  #include <sys/shm.h>
35    #include <sys/types.h>
36    
37  #define ACTIVE_BOARD_HEIGHT 8  #define ACTIVE_BOARD_HEIGHT 8
38    
39  #define STR_TOP_LEFT_MAX_LEN 40  #define STR_TOP_LEFT_MAX_LEN 80
40  #define STR_TOP_MIDDLE_MAX_LEN 20  #define STR_TOP_MIDDLE_MAX_LEN 40
41  #define STR_TOP_RIGHT_MAX_LEN 20  #define STR_TOP_RIGHT_MAX_LEN 40
42    
43  void moveto(int row, int col)  void moveto(int row, int col)
44  {  {
# Line 85  int press_any_key() Line 86  int press_any_key()
86          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
87          clrtoeol();          clrtoeol();
88    
89          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints("                           \033[1;33m鎸変换鎰忛敭缁х画...\033[0;37m");
90          iflush();          iflush();
91    
92          return igetch_t(MIN(MAX_DELAY_TIME, 60));          return igetch_t(MIN(MAX_DELAY_TIME, 60));
# Line 108  void set_input_echo(int echo) Line 109  void set_input_echo(int echo)
109          }          }
110  }  }
111    
112  static int _str_input(char *buffer, int buf_size, int echo_mode)  static int _str_input(char *buffer, int buf_size, int max_display_len, int echo_mode)
113  {  {
114          int c;          int ch;
115          int offset = 0;          int offset = 0;
116          int hz = 0;          int eol;
117            int display_len;
118            char input_str[4];
119            int str_len = 0;
120            char c;
121    
122          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
123          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)          offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
                 ;  
124    
125          igetch_reset();          igetch_reset();
126    
127          while (!SYS_server_exit)          while (!SYS_server_exit)
128          {          {
129                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));
130    
131                  if (c == CR)                  if (ch == CR)
132                  {                  {
133                          igetch_reset();                          igetch_reset();
134                          break;                          break;
135                  }                  }
136                  else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
137                  {                  {
138                          return -1;                          return -1;
139                  }                  }
140                  else if (c == LF || c == '\0')                  else if (ch == LF || ch == '\0')
141                  {                  {
142                          continue;                          continue;
143                  }                  }
144                  else if (c == BACKSPACE)                  else if (ch == BACKSPACE)
145                  {                  {
146                          if (offset > 0)                          if (offset > 0)
147                          {                          {
148                                  offset--;                                  offset--;
149                                  if (buffer[offset] < 0 || buffer[offset] > 127)                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
150                                  {                                  {
151                                          prints("\033[D \033[D");                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)
                                         offset--;  
                                         if (offset < 0) // should not happen  
152                                          {                                          {
153                                                  log_error("Offset of buffer is negative\n");                                                  offset--;
                                                 offset = 0;  
154                                          }                                          }
155                                            display_len--;
156                                            prints("\033[D \033[D");
157                                  }                                  }
158                                  buffer[offset] = '\0';                                  buffer[offset] = '\0';
159                                    display_len--;
160                                  prints("\033[D \033[D");                                  prints("\033[D \033[D");
161                                  iflush();                                  iflush();
162                          }                          }
163                          continue;                          continue;
164                  }                  }
165                  else if (c > 255 || iscntrl(c))                  else if (ch > 255 || iscntrl(ch))
166                  {                  {
167                          continue;                          continue;
168                  }                  }
169                  else if (c > 127 && c <= 255)                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
170                  {                  {
171                          if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character                          str_len = 0;
172                            c = (char)(ch & 0b11110000);
173                            while (c & 0b10000000)
174                            {
175                                    input_str[str_len] = (char)(ch - 256);
176                                    str_len++;
177                                    c = (c & 0b01111111) << 1;
178    
179                                    if ((c & 0b10000000) == 0) // Input completed
180                                    {
181                                            break;
182                                    }
183    
184                                    // Expect additional bytes of input
185                                    ch = igetch(100);                                                // 0.1 second
186                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
187                                    {
188                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
189                                            str_len = 0;
190                                            break;
191                                    }
192                            }
193    
194                            if (str_len == 0) // Incomplete input
195                            {
196                                    continue;
197                            }
198    
199                            if (offset + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character
200                          {                          {
                                 igetch(0); // Ignore 1 character  
201                                  outc('\a');                                  outc('\a');
202                                  iflush();                                  iflush();
203                                  continue;                                  continue;
204                          }                          }
                         hz = (!hz);  
                 }  
205    
206                  if (offset + 1 > buf_size - 1)                          memcpy(buffer + offset, input_str, (size_t)str_len);
207                  {                          offset += str_len;
208                          outc('\a');                          buffer[offset] = '\0';
209                          iflush();                          display_len += 2;
210                          continue;  
211                            switch (echo_mode)
212                            {
213                            case DOECHO:
214                                    prints(input_str);
215                                    break;
216                            case NOECHO:
217                                    prints("**");
218                                    break;
219                            }
220                  }                  }
221                    else if (ch >= 32 && ch < 127) // Printable character
222                    {
223                            if (offset + 1 > buf_size - 1 || display_len + 1 > max_display_len)
224                            {
225                                    outc('\a');
226                                    iflush();
227                                    continue;
228                            }
229    
230                  buffer[offset++] = (char)c;                          buffer[offset++] = (char)ch;
231                  buffer[offset] = '\0';                          buffer[offset] = '\0';
232                            display_len++;
233    
234                  switch (echo_mode)                          switch (echo_mode)
235                  {                          {
236                  case DOECHO:                          case DOECHO:
237                          outc((char)c);                                  outc((char)ch);
238                          break;                                  break;
239                  case NOECHO:                          case NOECHO:
240                          outc('*');                                  outc('*');
241                          break;                                  break;
242                            }
243                  }                  }
244                  if (!hz)                  else // Invalid character
245                  {                  {
246                          iflush();                          continue;
247                  }                  }
248    
249                    iflush();
250          }          }
251    
252          return offset;          return offset;
# Line 208  int str_input(char *buffer, int buf_size Line 258  int str_input(char *buffer, int buf_size
258    
259          buffer[0] = '\0';          buffer[0] = '\0';
260    
261          len = _str_input(buffer, buf_size, echo_mode);          len = _str_input(buffer, buf_size, buf_size, echo_mode);
262    
263          prints("\r\n");          prints("\r\n");
264          iflush();          iflush();
# Line 216  int str_input(char *buffer, int buf_size Line 266  int str_input(char *buffer, int buf_size
266          return len;          return len;
267  };  };
268    
269  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int echo_mode)  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len, int echo_mode)
270  {  {
271          int len;          int len;
272    
# Line 225  int get_data(int row, int col, char *pro Line 275  int get_data(int row, int col, char *pro
275          prints("%s", buffer);          prints("%s", buffer);
276          iflush();          iflush();
277    
278          len = _str_input(buffer, buf_size, echo_mode);          len = _str_input(buffer, buf_size, max_display_len, echo_mode);
279    
280          return len;          return len;
281  }  }
# Line 293  int display_data(const void *p_data, lon Line 343  int display_data(const void *p_data, lon
343                          ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);                          ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);
344    
345                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
346                                           "\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s",                                           "\033[1;44;33m绗琝033[32m%ld\033[33m-\033[32m%ld\033[33m琛 (\033[32m%ld%%\033[33m) %s",
347                                           ctx.line_top,                                           ctx.line_top,
348                                           ctx.line_bottom,                                           ctx.line_bottom,
349                                           percentile,                                           percentile,
# Line 372  int display_data(const void *p_data, lon Line 422  int display_data(const void *p_data, lon
422                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
423                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
424                                          clrtoeol();                                          clrtoeol();
425                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
426                                            prints("\n"); // Legacy Cterm only works with this line
427                                          break;                                          break;
428                                  case KEY_PGUP:                                  case KEY_PGUP:
429                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 469  static int display_file_key_handler(int Line 520  static int display_file_key_handler(int
520          {          {
521          case 0: // Set msg          case 0: // Set msg
522                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
523                                   "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "                                   "| 杩斿洖[\033[32m鈫怽033[33m,\033[32mESC\033[33m] | "
524                                   "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "                                   "绉诲姩[\033[32m鈫慭033[33m/\033[32m鈫揬033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
525                                   "帮助[\033[32mh\033[33m] |");                                   "甯姪[\033[32mh\033[33m] |");
526                  break;                  break;
527          }          }
528    
# Line 493  int display_file(const char *filename, i Line 544  int display_file(const char *filename, i
544                  return KEY_NULL;                  return KEY_NULL;
545          }          }
546    
547            if (user_online_update("VIEW_FILE") < 0)
548            {
549                    log_error("user_online_update(VIEW_FILE) error\n");
550            }
551    
552          ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);          ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);
553    
554          if (detach_file_shm(p_shm) < 0)          if (detach_file_shm(p_shm) < 0)
# Line 516  int show_top(const char *str_left, const Line 572  int show_top(const char *str_left, const
572    
573          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
574          str_left_f[sizeof(str_left_f) - 1] = '\0';          str_left_f[sizeof(str_left_f) - 1] = '\0';
575          len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN, &eol, &str_left_len, 1);          len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN / 2, &eol, &str_left_len, 1);
576          str_left_f[len] = '\0';          str_left_f[len] = '\0';
577    
578          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
579          str_middle_f[sizeof(str_middle_f) - 1] = '\0';          str_middle_f[sizeof(str_middle_f) - 1] = '\0';
580          len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN, &eol, &str_middle_len, 1);          len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN / 2, &eol, &str_middle_len, 1);
581          str_middle_f[len] = '\0';          str_middle_f[len] = '\0';
582    
583          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
584          str_right_f[sizeof(str_right_f) - 1] = '\0';          str_right_f[sizeof(str_right_f) - 1] = '\0';
585          len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN, &eol, &str_right_len, 1);          len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN / 2, &eol, &str_right_len, 1);
586          str_right_f[len] = '\0';          str_right_f[len] = '\0';
587    
588          moveto(1, 0);          moveto(1, 0);
589          clrtoeol();          clrtoeol();
590          prints("\033[1;44;33m%s\033[37m%*s\033[33m%*s\033[m",          prints("\033[1;44;33m%s\033[37m%*s%s\033[33m%*s%s\033[m",
591                     str_left_f, 44 - str_left_len, str_middle_f, 36, str_right_f);                     str_left_f, 44 - str_left_len - str_middle_len, "",
592                       str_middle_f, 36 - str_right_len, "", str_right_f);
593    
594          return 0;          return 0;
595  }  }
# Line 568  int show_bottom(const char *msg) Line 625  int show_bottom(const char *msg)
625          if (tm_online->tm_mday > 1)          if (tm_online->tm_mday > 1)
626          {          {
627                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
628                                   "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",                                   "\033[36m%2d\033[33m澶‐033[36m%2d\033[33m鏃",
629                                   tm_online->tm_mday - 1, tm_online->tm_hour);                                   tm_online->tm_mday - 1, tm_online->tm_hour);
630          }          }
631          else          else
632          {          {
633                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
634                                   "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",                                   "\033[36m%2d\033[33m鏃禱033[36m%2d\033[33m鍒",
635                                   tm_online->tm_hour, tm_online->tm_min);                                   tm_online->tm_hour, tm_online->tm_min);
636          }          }
637    
638          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
639          clrtoeol();          clrtoeol();
640          prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m][%s\033[33m]\033[m",          prints("\033[1;44;33m鏃堕棿[\033[36m%s\033[33m]%s%*s \033[33m甯愬彿[\033[36m%s\033[33m][%s\033[33m]\033[m",
641                     str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);                     str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);
642    
643          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