Adding upstream version 4.00~pre61+dfsg.
[syslinux-debian/hramrach.git] / com32 / menu / printmsg.c
blobcfceac39e8f67b9a241df4860130b510d00e2d31
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
8 * Boston MA 02110-1301, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
13 #include <ctype.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <consoles.h>
18 #include <getkey.h>
19 #include <minmax.h>
20 #include <setjmp.h>
21 #include <limits.h>
22 #include <sha1.h>
23 #include <base64.h>
24 #include <colortbl.h>
25 #ifdef __COM32__
26 #include <com32.h>
27 #endif
29 #include "menu.h"
31 static int draw_message_file(const char *filename)
33 FILE *f;
34 int ch;
35 enum msgname_state {
36 st_init, /* Base state */
37 st_si_1, /* <SI> digit 1 */
38 st_si_2, /* <SI> digit 2 */
39 st_skipline, /* Skip until NL */
40 } state = st_init;
41 int eof = 0;
42 int attr = 0;
44 f = fopen(filename, "r");
45 if (!f)
46 return -1;
48 /* Clear screen, hide cursor, default attribute */
49 printf("\033e\033%%@\033)0\033(B\3#%03d\033[?25l\033[2J\033[H",
50 message_base_color + 0x07);
52 while (!eof && (ch = getc(f)) != EOF) {
53 switch (state) {
54 case st_init:
55 switch (ch) {
56 case '\f':
57 fputs("\033[2J\033[H", stdout);
58 break;
59 case 15: /* SI */
60 state = st_si_1;
61 break;
62 case 24:
63 state = st_skipline;
64 break;
65 case 26:
66 eof = 1;
67 break;
68 case '\a':
69 case '\n':
70 case '\r':
71 putchar(ch);
72 break;
73 default:
74 if (ch >= 32)
75 putchar(ch);
76 break;
78 break;
80 case st_si_1:
81 attr = hexval(ch) << 4;
82 state = st_si_2;
83 break;
85 case st_si_2:
86 attr |= hexval(ch);
87 printf("\3#%03d", attr + message_base_color);
88 state = st_init;
89 break;
91 case st_skipline:
92 if (ch == '\n')
93 state = st_init;
94 break;
98 fclose(f);
99 return 0;
102 int show_message_file(const char *filename, const char *background)
104 int rv = KEY_NONE;
105 const char *old_background = NULL;
107 if (background) {
108 old_background = current_background;
109 set_background(background);
112 if (!(rv = draw_message_file(filename)))
113 rv = mygetkey(0); /* Wait for keypress */
115 if (old_background)
116 set_background(old_background);
118 return rv;