/[LeafOK_CVS]/lbbs/src/screen.c
ViewVC logotype

Contents of /lbbs/src/screen.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.113 - (show annotations)
Fri Oct 17 11:23:30 2025 UTC (4 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.112: +7 -2 lines
Content type: text/x-csrc
Add press_any_key_ex() to support customizable prompt message

1 /***************************************************************************
2 screen.c - description
3 -------------------
4 Copyright : (C) 2004-2025 by Leaflet
5 Email : leaflet@leafok.com
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * 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 *
12 * the Free Software Foundation; either version 3 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17 #include "bbs.h"
18 #include "common.h"
19 #include "editor.h"
20 #include "file_loader.h"
21 #include "io.h"
22 #include "log.h"
23 #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>
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
38
39 #define STR_TOP_LEFT_MAX_LEN 80
40 #define STR_TOP_MIDDLE_MAX_LEN 40
41 #define STR_TOP_RIGHT_MAX_LEN 80
42
43 static const char *get_time_str(char *s, size_t len)
44 {
45 static const char *weekday[] = {
46 "天", "一", "二", "三", "四", "五", "六"};
47 time_t curtime;
48 struct tm local_tm;
49
50 time(&curtime);
51 localtime_r(&curtime, &local_tm);
52 size_t j = strftime(s, len, "%b %d %H:%M 星期", &local_tm);
53
54 if (j == 0 || j + strlen(weekday[local_tm.tm_wday]) + 1 > len)
55 {
56 return NULL;
57 }
58
59 strncat(s, weekday[local_tm.tm_wday], len - 1 - j);
60
61 return s;
62 }
63
64 void moveto(int row, int col)
65 {
66 if (row >= 0)
67 {
68 prints("\033[%d;%dH", row, col);
69 }
70 else
71 {
72 prints("\r");
73 }
74 }
75
76 void clrtoeol()
77 {
78 prints(CTRL_SEQ_CLR_LINE);
79 }
80
81 void clrline(int line_begin, int line_end)
82 {
83 int i;
84
85 for (i = line_begin; i <= line_end; i++)
86 {
87 moveto(i, 0);
88 prints(CTRL_SEQ_CLR_LINE);
89 }
90 }
91
92 void clrtobot(int line_begin)
93 {
94 moveto(line_begin, 0);
95 prints("\033[J");
96 moveto(line_begin, 0);
97 }
98
99 void clearscr()
100 {
101 prints("\033[2J");
102 moveto(0, 0);
103 }
104
105 inline int press_any_key()
106 {
107 return press_any_key_ex(" \033[1;33m按任意键继续...\033[m");
108 }
109
110 int press_any_key_ex(const char *msg)
111 {
112 int ch = 0;
113 int wait_seconds = 60;
114 int duration = 0;
115 time_t t_begin = time(NULL);
116
117 moveto(SCREEN_ROWS, 0);
118 clrtoeol();
119
120 prints(msg);
121 iflush();
122
123 do
124 {
125 ch = igetch_t(wait_seconds - duration);
126 duration = (int)(time(NULL) - t_begin);
127 } while (!SYS_server_exit && ch == 0 && duration < 60);
128
129 return ch;
130 }
131
132 void set_input_echo(int echo)
133 {
134 if (echo)
135 {
136 outc('\x83'); // ASCII code 131
137 }
138 else
139 {
140 // outc ('\x85'); // ASCII code 133
141 prints("\xff\xfb\x01\xff\xfb\x03");
142 }
143 iflush();
144 }
145
146 static int _str_input(char *buffer, int buf_size, int max_display_len, int echo_mode)
147 {
148 int ch;
149 int offset = 0;
150 int eol;
151 int display_len;
152 char input_str[4];
153 int str_len = 0;
154 char c;
155
156 buffer[buf_size - 1] = '\0';
157 offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
158
159 igetch_reset();
160
161 while (!SYS_server_exit)
162 {
163 ch = igetch_t(MIN(MAX_DELAY_TIME, 60));
164
165 if (ch == CR)
166 {
167 break;
168 }
169 else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
170 {
171 return -1;
172 }
173 else if (ch == LF || ch == '\0')
174 {
175 continue;
176 }
177 else if (ch == BACKSPACE)
178 {
179 if (offset > 0)
180 {
181 offset--;
182 if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
183 {
184 while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)
185 {
186 offset--;
187 }
188 display_len--;
189 prints("\033[D \033[D");
190 }
191 buffer[offset] = '\0';
192 display_len--;
193 prints("\033[D \033[D");
194 iflush();
195 }
196 continue;
197 }
198 else if (ch > 255 || iscntrl(ch))
199 {
200 continue;
201 }
202 else if ((ch & 0xff80) == 0x80) // head of multi-byte character
203 {
204 str_len = 0;
205 c = (char)(ch & 0b11110000);
206 while (c & 0b10000000)
207 {
208 input_str[str_len] = (char)(ch - 256);
209 str_len++;
210 c = (c & 0b01111111) << 1;
211
212 if ((c & 0b10000000) == 0) // Input completed
213 {
214 break;
215 }
216
217 // Expect additional bytes of input
218 ch = igetch(100); // 0.1 second
219 if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
220 {
221 #ifdef _DEBUG
222 log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
223 #endif
224 str_len = 0;
225 break;
226 }
227 }
228
229 if (str_len == 0) // Incomplete input
230 {
231 continue;
232 }
233
234 if (offset + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character
235 {
236 outc('\a');
237 iflush();
238 continue;
239 }
240
241 memcpy(buffer + offset, input_str, (size_t)str_len);
242 offset += str_len;
243 buffer[offset] = '\0';
244 display_len += 2;
245
246 switch (echo_mode)
247 {
248 case DOECHO:
249 prints(input_str);
250 break;
251 case NOECHO:
252 prints("**");
253 break;
254 }
255 }
256 else if (ch >= 32 && ch < 127) // Printable character
257 {
258 if (offset + 1 > buf_size - 1 || display_len + 1 > max_display_len)
259 {
260 outc('\a');
261 iflush();
262 continue;
263 }
264
265 buffer[offset++] = (char)ch;
266 buffer[offset] = '\0';
267 display_len++;
268
269 switch (echo_mode)
270 {
271 case DOECHO:
272 outc((char)ch);
273 break;
274 case NOECHO:
275 outc('*');
276 break;
277 }
278 }
279 else // Invalid character
280 {
281 continue;
282 }
283
284 iflush();
285 }
286
287 return offset;
288 }
289
290 int str_input(char *buffer, int buf_size, int echo_mode)
291 {
292 int len;
293
294 buffer[0] = '\0';
295
296 len = _str_input(buffer, buf_size, buf_size, echo_mode);
297
298 prints("\r\n");
299 iflush();
300
301 return len;
302 };
303
304 int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len)
305 {
306 int len = 0;
307 int col_cur = 0;
308 int ch;
309 int offset = 0;
310 int eol;
311 int display_len;
312 char input_str[4];
313 int str_len = 0;
314 char c;
315
316 buffer[buf_size - 1] = '\0';
317 offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
318 buffer[offset] = '\0';
319 len = offset;
320 col_cur = col + str_length(prompt, 1) + display_len;
321
322 moveto(row, col);
323 prints("%s", prompt);
324 prints("%s", buffer);
325 prints("%*s", max_display_len - display_len, "");
326 moveto(row, col_cur);
327 iflush();
328
329 igetch_reset();
330
331 while (!SYS_server_exit)
332 {
333 ch = igetch_t(MIN(MAX_DELAY_TIME, 60));
334
335 if (ch == CR)
336 {
337 break;
338 }
339 else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
340 {
341 return -1;
342 }
343 else if (ch == LF || ch == '\0')
344 {
345 continue;
346 }
347 else if (ch == BACKSPACE)
348 {
349 if (offset > 0)
350 {
351 str_len = 1;
352 offset--;
353 if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
354 {
355 while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)
356 {
357 str_len++;
358 offset--;
359 }
360 display_len--;
361 col_cur--;
362 }
363
364 memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
365 len -= str_len;
366 buffer[len] = '\0';
367 display_len--;
368 col_cur--;
369
370 moveto(row, col_cur);
371 prints("%s", buffer + offset);
372 prints("%*s", max_display_len - display_len, "");
373 moveto(row, col_cur);
374 iflush();
375 }
376 continue;
377 }
378 else if (ch == KEY_DEL)
379 {
380 if (offset < len)
381 {
382 if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
383 {
384 str_len = 0;
385 c = (char)(buffer[offset] & 0b11110000);
386 while (c & 0b10000000)
387 {
388 str_len++;
389 c = (c & 0b01111111) << 1;
390 }
391 display_len--;
392 }
393 else
394 {
395 str_len = 1;
396 }
397
398 memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
399 len -= str_len;
400 buffer[len] = '\0';
401 display_len--;
402
403 moveto(row, col_cur);
404 prints("%s", buffer + offset);
405 prints("%*s", max_display_len - display_len, "");
406 moveto(row, col_cur);
407 iflush();
408 }
409 continue;
410 }
411 else if (ch == KEY_LEFT)
412 {
413 if (offset > 0)
414 {
415 str_len = 1;
416 offset--;
417 if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
418 {
419 while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)
420 {
421 str_len++;
422 offset--;
423 }
424 col_cur--;
425 }
426 col_cur--;
427
428 moveto(row, col_cur);
429 iflush();
430 }
431 continue;
432 }
433 else if (ch == KEY_RIGHT)
434 {
435 if (offset < len)
436 {
437 str_len = 0;
438 if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
439 {
440 c = (char)(buffer[offset] & 0b11110000);
441 while (c & 0b10000000)
442 {
443 str_len++;
444 c = (c & 0b01111111) << 1;
445 }
446 col_cur++;
447 }
448 else
449 {
450 str_len = 1;
451 }
452
453 col_cur++;
454 offset += str_len;
455
456 moveto(row, col_cur);
457 iflush();
458 }
459 continue;
460 }
461 else if (ch == KEY_HOME || ch == KEY_UP)
462 {
463 if (offset > 0)
464 {
465 offset = 0;
466 col_cur = col + str_length(prompt, 1);
467
468 moveto(row, col_cur);
469 iflush();
470 }
471 continue;
472 }
473 else if (ch == KEY_END || ch == KEY_DOWN)
474 {
475 if (offset < len)
476 {
477 offset = len;
478 col_cur = col + str_length(prompt, 1) + display_len;
479
480 moveto(row, col_cur);
481 iflush();
482 }
483 continue;
484 }
485 else if (ch > 255 || iscntrl(ch))
486 {
487 continue;
488 }
489 else if ((ch & 0xff80) == 0x80) // head of multi-byte character
490 {
491 str_len = 0;
492 c = (char)(ch & 0b11110000);
493 while (c & 0b10000000)
494 {
495 input_str[str_len] = (char)(ch - 256);
496 str_len++;
497 c = (c & 0b01111111) << 1;
498
499 if ((c & 0b10000000) == 0) // Input completed
500 {
501 break;
502 }
503
504 // Expect additional bytes of input
505 ch = igetch(100); // 0.1 second
506 if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
507 {
508 #ifdef _DEBUG
509 log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
510 #endif
511 str_len = 0;
512 break;
513 }
514 }
515
516 if (str_len == 0) // Incomplete input
517 {
518 continue;
519 }
520
521 if (len + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character
522 {
523 outc('\a');
524 iflush();
525 continue;
526 }
527
528 memmove(buffer + offset + str_len, buffer + offset, (size_t)(len - offset));
529 memcpy(buffer + offset, input_str, (size_t)str_len);
530 len += str_len;
531 buffer[len] = '\0';
532 display_len += 2;
533
534 moveto(row, col_cur);
535 prints("%s", buffer + offset);
536 prints("%*s", max_display_len - display_len, "");
537
538 col_cur += 2;
539
540 moveto(row, col_cur);
541 iflush();
542
543 offset += str_len;
544 }
545 else if (ch >= 32 && ch < 127) // Printable character
546 {
547 if (len + 1 > buf_size - 1 || display_len + 1 > max_display_len)
548 {
549 outc('\a');
550 iflush();
551 continue;
552 }
553
554 memmove(buffer + offset + 1, buffer + offset, (size_t)(len - offset));
555 buffer[offset] = (char)ch;
556 len++;
557 buffer[len] = '\0';
558 display_len++;
559
560 moveto(row, col_cur);
561 prints("%s", buffer + offset);
562 prints("%*s", max_display_len - display_len, "");
563
564 col_cur++;
565
566 moveto(row, col_cur);
567 iflush();
568
569 offset++;
570 }
571 else // Invalid character
572 {
573 continue;
574 }
575 }
576
577 return len;
578 }
579
580 int display_data(const void *p_data, long display_line_total, const long *p_line_offsets, int eof_exit,
581 display_data_key_handler key_handler, const char *help_filename)
582 {
583 static int show_help = 1;
584 char buffer[LINE_BUFFER_LEN];
585 DISPLAY_CTX ctx;
586 int ch = 0;
587 int input_ok;
588 const int screen_begin_row = 1;
589 const int screen_row_total = SCREEN_ROWS - screen_begin_row;
590 int output_current_row = screen_begin_row;
591 int output_end_row = SCREEN_ROWS - 1;
592 long int line_current = 0;
593 long int len;
594 long int percentile;
595 int loop;
596 int eol, display_len;
597
598 clrline(output_current_row, SCREEN_ROWS);
599
600 // update msg_ext with extended key handler
601 if (key_handler(&ch, &ctx) != 0)
602 {
603 return ch;
604 }
605
606 loop = 1;
607 while (!SYS_server_exit && loop)
608 {
609 if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total)
610 {
611 if (eof_exit == 1)
612 {
613 ch = press_any_key();
614 }
615 else // if (eof_exit == 2)
616 {
617 iflush();
618 }
619
620 loop = 0;
621 break;
622 }
623
624 if (line_current >= display_line_total || output_current_row > output_end_row)
625 {
626 ctx.reach_begin = (line_current < output_current_row ? 1 : 0);
627
628 if (line_current - (output_current_row - screen_begin_row) + screen_row_total < display_line_total)
629 {
630 percentile = (line_current - (output_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total;
631 ctx.reach_end = 0;
632 }
633 else
634 {
635 percentile = 100;
636 ctx.reach_end = 1;
637 }
638
639 ctx.line_top = line_current - (output_current_row - screen_begin_row) + 1;
640 ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);
641
642 snprintf(buffer, sizeof(buffer),
643 "\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s",
644 ctx.line_top,
645 ctx.line_bottom,
646 percentile,
647 ctx.msg);
648
649 len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
650 for (; display_len < SCREEN_COLS; display_len++)
651 {
652 buffer[len++] = ' ';
653 }
654 buffer[len] = '\0';
655 strncat(buffer, "\033[m", sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer)));
656
657 moveto(SCREEN_ROWS, 0);
658 prints("%s", buffer);
659 iflush();
660
661 input_ok = 0;
662 while (!SYS_server_exit && !input_ok)
663 {
664 ch = igetch_t(MAX_DELAY_TIME);
665 input_ok = 1;
666
667 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
668 {
669 BBS_last_access_tm = time(NULL);
670 }
671
672 // extended key handler
673 if (key_handler(&ch, &ctx) != 0)
674 {
675 goto cleanup;
676 }
677
678 switch (ch)
679 {
680 case KEY_NULL:
681 log_error("KEY_NULL\n");
682 goto cleanup;
683 case KEY_TIMEOUT:
684 log_error("User input timeout\n");
685 goto cleanup;
686 case KEY_HOME:
687 if (line_current - output_current_row < 0) // Reach begin
688 {
689 break;
690 }
691 line_current = 0;
692 output_current_row = screen_begin_row;
693 output_end_row = SCREEN_ROWS - 1;
694 clrline(output_current_row, SCREEN_ROWS);
695 break;
696 case KEY_END:
697 if (display_line_total < screen_row_total)
698 {
699 break;
700 }
701 line_current = display_line_total - screen_row_total;
702 output_current_row = screen_begin_row;
703 output_end_row = SCREEN_ROWS - 1;
704 clrline(output_current_row, SCREEN_ROWS);
705 break;
706 case KEY_UP:
707 if (line_current - output_current_row < 0) // Reach begin
708 {
709 break;
710 }
711 line_current -= output_current_row;
712 output_current_row = screen_begin_row;
713 // screen_end_line = screen_begin_line;
714 // prints("\033[T"); // Scroll down 1 line
715 output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
716 break;
717 case CR:
718 case KEY_SPACE:
719 case KEY_DOWN:
720 if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end
721 {
722 break;
723 }
724 line_current += (screen_row_total - (output_current_row - screen_begin_row));
725 output_current_row = screen_row_total;
726 output_end_row = SCREEN_ROWS - 1;
727 moveto(SCREEN_ROWS, 0);
728 clrtoeol();
729 // prints("\033[S"); // Scroll up 1 line
730 prints("\n"); // Legacy Cterm only works with this line
731 break;
732 case KEY_PGUP:
733 if (line_current - output_current_row < 0) // Reach begin
734 {
735 break;
736 }
737 line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
738 if (line_current < 0)
739 {
740 line_current = 0;
741 }
742 output_current_row = screen_begin_row;
743 output_end_row = SCREEN_ROWS - 1;
744 clrline(output_current_row, SCREEN_ROWS);
745 break;
746 case KEY_PGDN:
747 if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end
748 {
749 break;
750 }
751 line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
752 if (line_current + screen_row_total > display_line_total) // No enough lines to display
753 {
754 line_current = display_line_total - screen_row_total;
755 }
756 output_current_row = screen_begin_row;
757 output_end_row = SCREEN_ROWS - 1;
758 clrline(output_current_row, SCREEN_ROWS);
759 break;
760 case KEY_ESC:
761 case KEY_LEFT:
762 loop = 0;
763 break;
764 case 'h':
765 if (!show_help) // Not reentrant
766 {
767 break;
768 }
769 // Display help information
770 show_help = 0;
771 display_file(help_filename, 1);
772 show_help = 1;
773 case KEY_F5:
774 // Refresh after display help information
775 line_current -= (output_current_row - screen_begin_row);
776 output_current_row = screen_begin_row;
777 output_end_row = SCREEN_ROWS - 1;
778 clrline(output_current_row, SCREEN_ROWS);
779 break;
780 case 0: // Refresh bottom line
781 break;
782 default:
783 input_ok = 0;
784 break;
785 }
786 }
787
788 continue;
789 }
790
791 len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
792 if (len >= sizeof(buffer))
793 {
794 log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
795 len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
796 len = sizeof(buffer) - 1;
797 }
798 else if (len < 0)
799 {
800 log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld \n",
801 len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
802 len = 0;
803 }
804
805 memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
806 buffer[len] = '\0';
807
808 moveto(output_current_row, 0);
809 clrtoeol();
810 prints("%s", buffer);
811 line_current++;
812 output_current_row++;
813 }
814
815 cleanup:
816 return ch;
817 }
818
819 static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
820 {
821 switch (*p_key)
822 {
823 case 0: // Set msg
824 snprintf(p_ctx->msg, sizeof(p_ctx->msg),
825 "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
826 "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
827 "帮助[\033[32mh\033[33m] |");
828 break;
829 }
830
831 return 0;
832 }
833
834 int display_file(const char *filename, int eof_exit)
835 {
836 int ret;
837 const void *p_shm;
838 size_t data_len;
839 long line_total;
840 const void *p_data;
841 const long *p_line_offsets;
842
843 if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
844 {
845 log_error("get_file_shm(%s) error\n", filename);
846 return KEY_NULL;
847 }
848
849 if (user_online_update("VIEW_FILE") < 0)
850 {
851 log_error("user_online_update(VIEW_FILE) error\n");
852 }
853
854 ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);
855
856 if (detach_file_shm(p_shm) < 0)
857 {
858 log_error("detach_file_shm(%s) error\n", filename);
859 }
860
861 return ret;
862 }
863
864 int show_top(const char *str_left, const char *str_middle, const char *str_right)
865 {
866 char str_left_f[STR_TOP_LEFT_MAX_LEN + 1];
867 char str_middle_f[STR_TOP_MIDDLE_MAX_LEN + 1];
868 char str_right_f[STR_TOP_RIGHT_MAX_LEN + 1];
869 int str_left_len;
870 int str_middle_len;
871 int str_right_len;
872 int eol;
873 int len;
874
875 strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
876 str_left_f[sizeof(str_left_f) - 1] = '\0';
877 len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN / 2, &eol, &str_left_len, 1);
878 str_left_f[len] = '\0';
879
880 strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
881 str_middle_f[sizeof(str_middle_f) - 1] = '\0';
882 len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN / 2, &eol, &str_middle_len, 1);
883 str_middle_f[len] = '\0';
884
885 strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
886 str_right_f[sizeof(str_right_f) - 1] = '\0';
887 len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN / 2, &eol, &str_right_len, 1);
888 str_right_f[len] = '\0';
889
890 moveto(1, 0);
891 clrtoeol();
892 prints("\033[1;44;33m%s\033[37m%*s%s\033[33m%*s%s\033[m",
893 str_left_f, 44 - str_left_len - str_middle_len, "",
894 str_middle_f, 36 - str_right_len, "", str_right_f);
895
896 return 0;
897 }
898
899 int show_bottom(const char *msg)
900 {
901 char str_time[LINE_BUFFER_LEN];
902 time_t time_online;
903 struct tm *tm_online;
904 char msg_f[LINE_BUFFER_LEN];
905 int eol;
906 int msg_len;
907 int len;
908 int len_username;
909 char str_tm_online[LINE_BUFFER_LEN];
910
911 get_time_str(str_time, sizeof(str_time));
912
913 msg_f[0] = '\0';
914 msg_len = 0;
915 if (msg != NULL)
916 {
917 strncpy(msg_f, msg, sizeof(msg_f) - 1);
918 msg_f[sizeof(msg_f) - 1] = '\0';
919 len = split_line(msg_f, 23, &eol, &msg_len, 1);
920 msg_f[len] = '\0';
921 }
922
923 len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
924
925 time_online = time(NULL) - BBS_login_tm;
926 tm_online = gmtime(&time_online);
927 if (tm_online->tm_mday > 1)
928 {
929 snprintf(str_tm_online, sizeof(str_tm_online),
930 "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",
931 tm_online->tm_mday - 1, tm_online->tm_hour);
932 }
933 else
934 {
935 snprintf(str_tm_online, sizeof(str_tm_online),
936 "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",
937 tm_online->tm_hour, tm_online->tm_min);
938 }
939
940 moveto(SCREEN_ROWS, 0);
941 clrtoeol();
942 prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m][%s\033[33m]\033[m",
943 str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);
944
945 return 0;
946 }
947
948 int show_active_board()
949 {
950 static int line_current = 0;
951 static const void *p_shm = NULL;
952 static size_t data_len;
953 static long line_total;
954 static const void *p_data;
955 static const long *p_line_offsets;
956
957 static time_t t_last_show = 0;
958 static int line_last = 0;
959
960 char buffer[LINE_BUFFER_LEN];
961 long int len;
962
963 if (p_shm == NULL)
964 {
965 if ((p_shm = get_file_shm_readonly(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
966 {
967 log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
968 return KEY_NULL;
969 }
970 }
971
972 if (time(NULL) - t_last_show >= 10)
973 {
974 line_last = line_current;
975 t_last_show = time(NULL);
976 }
977 else
978 {
979 line_current = line_last;
980 }
981
982 clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
983
984 for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
985 {
986 len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
987 if (len >= LINE_BUFFER_LEN)
988 {
989 log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
990 len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
991 len = LINE_BUFFER_LEN - 1;
992 }
993
994 memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
995 buffer[len] = '\0';
996
997 moveto(3 + i, 0);
998 prints("%s", buffer);
999
1000 line_current++;
1001 if (line_current >= line_total)
1002 {
1003 line_current = 0;
1004 break;
1005 }
1006 }
1007
1008 return 0;
1009 }

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