/[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.117 by sysadm, Wed Oct 22 04:48:53 2025 UTC Revision 1.130 by sysadm, Wed Nov 19 03:12:58 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    screen.c  -  description  /*
3                                                           -------------------   * screen
4          Copyright            : (C) 2004-2025 by Leaflet   *   - advanced telnet-based user interactive input / output features
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
# Line 29  Line 25 
25  #include <string.h>  #include <string.h>
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <unistd.h>  #include <unistd.h>
28    #include <wchar.h>
29  #include <sys/param.h>  #include <sys/param.h>
30  #include <sys/stat.h>  #include <sys/stat.h>
31  #include <sys/shm.h>  #include <sys/shm.h>
32  #include <sys/types.h>  #include <sys/types.h>
33    
34  #define ACTIVE_BOARD_HEIGHT 8  const char CTRL_SEQ_CLR_LINE[] = "\033[K";
35    
36  #define STR_TOP_LEFT_MAX_LEN 80  static const int ACTIVE_BOARD_HEIGHT = 8;
 #define STR_TOP_MIDDLE_MAX_LEN 40  
 #define STR_TOP_RIGHT_MAX_LEN 80  
37    
38  static const char *get_time_str(char *s, size_t len)  static const int STR_TOP_LEFT_MAX_LEN = 80;
39    static const int STR_TOP_MIDDLE_MAX_LEN = 40;
40    static const int STR_TOP_RIGHT_MAX_LEN = 80;
41    
42    static size_t get_time_str(char *s, size_t len)
43  {  {
         static const char *weekday[] = {  
                 "天", "一", "二", "三", "四", "五", "六"};  
44          time_t curtime;          time_t curtime;
45          struct tm local_tm;          struct tm local_tm;
46    
47          time(&curtime);          time(&curtime);
48          localtime_r(&curtime, &local_tm);          localtime_r(&curtime, &local_tm);
49          size_t j = strftime(s, len, "%b %d %H:%M 星期", &local_tm);          size_t j = strftime(s, len, "%m/%d %H:%M %Z", &local_tm);
   
         if (j == 0 || j + strlen(weekday[local_tm.tm_wday]) + 1 > len)  
         {  
                 return NULL;  
         }  
50    
51          strncat(s, weekday[local_tm.tm_wday], len - 1 - j);          return j;
   
         return s;  
52  }  }
53    
54  void moveto(int row, int col)  void moveto(int row, int col)
# Line 144  void set_input_echo(int echo) Line 134  void set_input_echo(int echo)
134          iflush();          iflush();
135  }  }
136    
137  static int _str_input(char *buffer, int buf_size, int max_display_len, int echo_mode)  static int _str_input(char *buffer, int buf_size, int max_display_len, enum io_echo_t echo_mode)
138  {  {
139          int ch;          int ch;
140          int offset = 0;          int offset = 0;
141          int eol;          int eol;
142          int display_len;          int display_len;
143          char input_str[4];          char input_str[5];
144          int str_len = 0;          int str_len = 0;
145            wchar_t wcs[2];
146          char c;          char c;
147    
148          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
# Line 161  static int _str_input(char *buffer, int Line 152  static int _str_input(char *buffer, int
152    
153          while (!SYS_server_exit)          while (!SYS_server_exit)
154          {          {
155                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
156    
157                  if (ch == CR)                  if (ch == CR)
158                  {                  {
# Line 226  static int _str_input(char *buffer, int Line 217  static int _str_input(char *buffer, int
217                                          break;                                          break;
218                                  }                                  }
219                          }                          }
220                            input_str[str_len] = '\0';
221    
222                          if (str_len == 0) // Incomplete input                          if (str_len == 0) // Incomplete input
223                          {                          {
224                                  continue;                                  continue;
225                          }                          }
226    
227                          if (offset + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character                          if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
228                            {
229                                    log_error("mbstowcs() error\n");
230                            }
231                            if (offset + str_len > buf_size - 1 || display_len + (UTF8_fixed_width ? 2 : wcwidth(wcs[0])) > max_display_len) // No enough space for Chinese character
232                          {                          {
233                                  outc('\a');                                  outc('\a');
234                                  iflush();                                  iflush();
# Line 288  static int _str_input(char *buffer, int Line 284  static int _str_input(char *buffer, int
284          return offset;          return offset;
285  }  }
286    
287  int str_input(char *buffer, int buf_size, int echo_mode)  int str_input(char *buffer, int buf_size, enum io_echo_t echo_mode)
288  {  {
289          int len;          int len;
290    
# Line 310  int get_data(int row, int col, char *pro Line 306  int get_data(int row, int col, char *pro
306          int offset = 0;          int offset = 0;
307          int eol;          int eol;
308          int display_len;          int display_len;
309          char input_str[4];          char input_str[5];
310          int str_len = 0;          int str_len = 0;
311            wchar_t wcs[2];
312            int wc_len;
313          char c;          char c;
314    
315          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
# Line 331  int get_data(int row, int col, char *pro Line 329  int get_data(int row, int col, char *pro
329    
330          while (!SYS_server_exit)          while (!SYS_server_exit)
331          {          {
332                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
333    
334                  if (ch == CR)                  if (ch == CR)
335                  {                  {
# Line 358  int get_data(int row, int col, char *pro Line 356  int get_data(int row, int col, char *pro
356                                                  str_len++;                                                  str_len++;
357                                                  offset--;                                                  offset--;
358                                          }                                          }
359                                          display_len--;  
360                                          col_cur--;                                          if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
361                                            {
362                                                    log_error("mbstowcs() error\n");
363                                            }
364                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
365    
366                                            if (wc_len == 2)
367                                            {
368                                                    display_len--;
369                                                    col_cur--;
370                                            }
371                                  }                                  }
372    
373                                  memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));                                  memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
# Line 422  int get_data(int row, int col, char *pro Line 430  int get_data(int row, int col, char *pro
430                                                  str_len++;                                                  str_len++;
431                                                  offset--;                                                  offset--;
432                                          }                                          }
433                                          col_cur--;  
434                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
435                                            {
436                                                    log_error("mbstowcs() error\n");
437                                            }
438                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
439    
440                                            if (wc_len == 2)
441                                            {
442                                                    col_cur--;
443                                            }
444                                  }                                  }
445                                  col_cur--;                                  col_cur--;
446    
# Line 444  int get_data(int row, int col, char *pro Line 462  int get_data(int row, int col, char *pro
462                                                  str_len++;                                                  str_len++;
463                                                  c = (c & 0x7f) << 1;                                                  c = (c & 0x7f) << 1;
464                                          }                                          }
465                                          col_cur++;  
466                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
467                                            {
468                                                    log_error("mbstowcs() error\n");
469                                            }
470                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
471    
472                                            if (wc_len == 2)
473                                            {
474                                                    col_cur++;
475                                            }
476                                  }                                  }
477                                  else                                  else
478                                  {                                  {
# Line 513  int get_data(int row, int col, char *pro Line 541  int get_data(int row, int col, char *pro
541                                          break;                                          break;
542                                  }                                  }
543                          }                          }
544                            input_str[str_len] = '\0';
545    
546                          if (str_len == 0) // Incomplete input                          if (str_len == 0) // Incomplete input
547                          {                          {
548                                  continue;                                  continue;
549                          }                          }
550    
551                          if (len + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character                          if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
552                            {
553                                    log_error("mbstowcs() error\n");
554                            }
555                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
556    
557                            if (len + str_len > buf_size - 1 ||
558                                    display_len + wc_len > max_display_len) // No enough space for Chinese character
559                          {                          {
560                                  outc('\a');                                  outc('\a');
561                                  iflush();                                  iflush();
# Line 530  int get_data(int row, int col, char *pro Line 566  int get_data(int row, int col, char *pro
566                          memcpy(buffer + offset, input_str, (size_t)str_len);                          memcpy(buffer + offset, input_str, (size_t)str_len);
567                          len += str_len;                          len += str_len;
568                          buffer[len] = '\0';                          buffer[len] = '\0';
569                          display_len += 2;                          display_len += wc_len;
570    
571                          moveto(row, col_cur);                          moveto(row, col_cur);
572                          prints("%s", buffer + offset);                          prints("%s", buffer + offset);
573                          prints("%*s", max_display_len - display_len, "");                          prints("%*s", max_display_len - display_len, "");
574    
575                          col_cur += 2;                          col_cur += wc_len;
576    
577                          moveto(row, col_cur);                          moveto(row, col_cur);
578                          iflush();                          iflush();
# Line 607  int display_data(const void *p_data, lon Line 643  int display_data(const void *p_data, lon
643          loop = 1;          loop = 1;
644          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
645          {          {
646                  if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total)                  if (eof_exit > 0 && line_current >= display_line_total)
647                  {                  {
648                          if (eof_exit == 1)                          if (eof_exit == 1)
649                          {                          {
# Line 662  int display_data(const void *p_data, lon Line 698  int display_data(const void *p_data, lon
698                          input_ok = 0;                          input_ok = 0;
699                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
700                          {                          {
701                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(BBS_max_user_idle_time);
702                                  input_ok = 1;                                  input_ok = 1;
703    
704                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
# Line 817  cleanup: Line 853  cleanup:
853          return ch;          return ch;
854  }  }
855    
856  static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)  int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
857  {  {
858          switch (*p_key)          switch (*p_key)
859          {          {
# Line 835  static int display_file_key_handler(int Line 871  static int display_file_key_handler(int
871  int display_file(const char *filename, int eof_exit)  int display_file(const char *filename, int eof_exit)
872  {  {
873          int ret;          int ret;
874          const void *p_shm;          void *p_shm;
875          size_t data_len;          size_t data_len;
876          long line_total;          long line_total;
877          const void *p_data;          const void *p_data;
# Line 900  int show_top(const char *str_left, const Line 936  int show_top(const char *str_left, const
936  int show_bottom(const char *msg)  int show_bottom(const char *msg)
937  {  {
938          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
939            int len_str_time;
940          time_t time_online;          time_t time_online;
941          struct tm *tm_online;          struct tm *tm_online;
942          char msg_f[LINE_BUFFER_LEN];          char msg_f[LINE_BUFFER_LEN];
943          int eol;          int eol;
944          int msg_len;          int len_msg;
945          int len;          int len;
946          int len_username;          int len_username;
947          char str_tm_online[LINE_BUFFER_LEN];          char str_tm_online[LINE_BUFFER_LEN];
948            int len_str_tm_online;
949    
950          get_time_str(str_time, sizeof(str_time));          len_str_time = (int)get_time_str(str_time, sizeof(str_time));
951    
952          msg_f[0] = '\0';          msg_f[0] = '\0';
953          msg_len = 0;          len_msg = 0;
954          if (msg != NULL)          if (msg != NULL)
955          {          {
956                  strncpy(msg_f, msg, sizeof(msg_f) - 1);                  strncpy(msg_f, msg, sizeof(msg_f) - 1);
957                  msg_f[sizeof(msg_f) - 1] = '\0';                  msg_f[sizeof(msg_f) - 1] = '\0';
958                  len = split_line(msg_f, 23, &eol, &msg_len, 1);                  len = split_line(msg_f, 23, &eol, &len_msg, 1);
959                  msg_f[len] = '\0';                  msg_f[len] = '\0';
960          }          }
961    
# Line 925  int show_bottom(const char *msg) Line 963  int show_bottom(const char *msg)
963    
964          time_online = time(NULL) - BBS_login_tm;          time_online = time(NULL) - BBS_login_tm;
965          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
966          if (tm_online->tm_mday > 0)          if (tm_online->tm_mday > 1)
967          {          {
968                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
969                                   "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",                                   "\033[36m%d\033[33md \033[36m%d\033[33m:\033[36m%.2d",
970                                   tm_online->tm_yday, tm_online->tm_hour);                                   tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
971          }          }
972          else          else
973          {          {
974                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
975                                   "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",                                   "\033[36m%d\033[33m:\033[36m%.2d",
976                                   tm_online->tm_hour, tm_online->tm_min);                                   tm_online->tm_hour, tm_online->tm_min);
977          }          }
978            len_str_tm_online = str_length(str_tm_online, 1);
979    
980          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
981          clrtoeol();          clrtoeol();
982          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",
983                     str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);                     str_time, msg_f, 65 - len_str_time - len_msg - len_username - len_str_tm_online,
984                       "", BBS_username, str_tm_online);
985    
986          return 0;          return 0;
987  }  }


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

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