Adding upstream version 3.51.
[syslinux-debian/hramrach.git] / com32 / modules / printmsg.c
blob128bbc03ee69e758b7acb663ac626812ee939f8d
1 #define _GNU_SOURCE /* Needed for asprintf() on Linux */
2 #include <ctype.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <consoles.h>
7 #include <getkey.h>
8 #include <minmax.h>
9 #include <setjmp.h>
10 #include <limits.h>
11 #include <sha1.h>
12 #include <base64.h>
13 #include <colortbl.h>
14 #ifdef __COM32__
15 #include <com32.h>
16 #endif
18 #include "menu.h"
20 static int draw_message_file(const char *filename)
22 FILE *f;
23 int ch;
24 enum msgname_state {
25 st_init, /* Base state */
26 st_si_1, /* <SI> digit 1 */
27 st_si_2, /* <SI> digit 2 */
28 st_skipline, /* Skip until NL */
29 } state = st_init;
30 int eof = 0;
31 int attr = 0;
33 f = fopen(filename, "r");
34 if (!f)
35 return -1;
37 /* Clear screen, hide cursor, default attribute */
38 printf("\033e\033%%@\033)0\033(B\3#%03d\033[?25l\033[2J\033[H",
39 message_base_color+0x07);
41 while (!eof && (ch = getc(f)) != EOF) {
42 switch (state) {
43 case st_init:
44 switch (ch) {
45 case '\f':
46 fputs("\033[2J\033[H", stdout);
47 break;
48 case 15: /* SI */
49 state = st_si_1;
50 break;
51 case 24:
52 state = st_skipline;
53 break;
54 case 26:
55 eof = 1;
56 break;
57 case '\a':
58 case '\n':
59 case '\r':
60 putchar(ch);
61 break;
62 default:
63 if (ch >= 32)
64 putchar(ch);
65 break;
67 break;
69 case st_si_1:
70 attr = hexval(ch) << 4;
71 state = st_si_2;
72 break;
74 case st_si_2:
75 attr |= hexval(ch);
76 printf("\3#%03d", attr+message_base_color);
77 state = st_init;
78 break;
80 case st_skipline:
81 if (ch == '\n')
82 state = st_init;
83 break;
87 fclose(f);
88 return 0;
91 int show_message_file(const char *filename, const char *background)
93 int rv = KEY_NONE;
95 if (background && (!menu_background || strcmp(background, menu_background)))
96 draw_background(background);
97 else
98 background = NULL;
100 if ( !(rv = draw_message_file(filename)) )
101 rv = mygetkey(0); /* Wait for keypress */
103 if (background)
104 draw_background(menu_background);
106 return rv;