Adding upstream version 4.02+dfsg.
[syslinux-debian/hramrach.git] / com32 / cmenu / libmenu / help.c
blob0df1e10359cbaa5864aa6b0b69580c4c81185d34
1 /* -*- c -*- ------------------------------------------------------------- *
3 * Copyright 2004-2005 Murali Krishnan Ganapathy - 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., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
13 #include "help.h"
14 #include <stdio.h>
15 #include "string.h"
16 #include "com32io.h"
17 #include <syslinux/loadfile.h> // to read entire file into memory
19 int nc, nr; // Number of columns/rows of the screen
20 char helpbasedir[HELPDIRLEN]; // name of help directory limited to HELPDIRLEN
22 // Find the occurence of the count'th \n in buffer (or NULL) if not found
23 static char *findline(char *buffer, int count)
25 int ctr;
26 char *p = buffer - 1;
28 if (count < 1)
29 return buffer;
30 for (ctr = 0; ctr < count; ctr++) {
31 p = strchr(p + 1, '\n');
32 if (p == NULL)
33 return NULL;
35 return p;
38 // return the number of lines in buffer
39 static int countlines(char *buffer)
41 int ans;
42 const char *p;
44 p = buffer - 1;
45 ans = 1;
46 while (p) {
47 p = strchr(p + 1, '\n');
48 ans++;
50 return ans;
53 // Print numlines of text starting from buf
54 static void printtext(char *buf, int from)
56 char *f, *t;
57 int right, nlines, i;
59 // clear window to print
60 right = nc - HELP_RIGHT_MARGIN;
61 nlines = nr - HELP_BODY_ROW - HELP_BOTTOM_MARGIN - 1;
63 f = findline(buf, from);
64 if (!f)
65 return; // nothing to print
66 if (*f == '\n')
67 f++; // start of from+1st line
68 t = f;
69 while (i < nlines) {
70 gotoxy(HELP_BODY_ROW + i, HELP_LEFT_MARGIN);
71 clear_end_of_line();
72 putchar(SO);
73 gotoxy(HELP_BODY_ROW + i, nc - 1);
74 putch(LEFT_BORDER, 0x07);
75 putchar(SI);
77 gotoxy(HELP_BODY_ROW + i, HELP_LEFT_MARGIN);
78 while (*t != '\n') {
79 if (*t == '\0')
80 return;
81 putchar(*t);
82 t++;
84 putchar('\n');
85 t++;
86 i++;
90 void showhelp(const char *filename)
92 char ph;
93 char *title, *text;
94 union {
95 char *buffer;
96 void *vbuf;
97 } buf; // This is to avoild type-punning issues
99 char line[512];
100 size_t size;
101 int scan;
102 int rv, numlines, curr_line;
104 if (getscreensize(1, &nr, &nc)) {
105 /* Unknown screen size? */
106 nc = 80;
107 nr = 24;
109 ph = nr - HELP_BODY_ROW;
110 cls();
112 /* Turn autowrap off, to avoid scrolling the menu */
113 printf(CSI "?7l");
115 if (filename == NULL) { // print file contents
116 strcpy(line, "Filename not given");
117 goto puke;
120 rv = loadfile(filename, (void **)&buf.vbuf, &size); // load entire file into memory
121 if (rv < 0) { // Error reading file or no such file
122 sprintf(line, "Error reading file or file not found\n file=%s", filename);
123 goto puke;
126 title = buf.buffer;
127 text = findline(title, 1); // end of first line
128 *text++ = '\0'; // end the title string and increment text
130 // Now we have a file just print it.
131 numlines = countlines(text);
132 curr_line = 0;
133 scan = KEY_ESC + 1; // anything except ESCAPE
135 /* top, left, bottom, right, attr */
136 drawbox(0, 0, nr - 1, nc - 1, 0x07);
137 while (scan != KEY_ESC) {
138 /* Title */
139 gotoxy(1, (nc - strlen(title)) / 2);
140 fputs(title, stdout);
141 drawhorizline(2, HELP_LEFT_MARGIN - 1, nc - HELP_RIGHT_MARGIN, 0x07, 0); // dumb==0
142 /* Text */
143 printtext(text, curr_line);
144 gotoxy(HELP_BODY_ROW - 1, nc - HELP_RIGHT_MARGIN);
145 if (curr_line > 0)
146 putchar(HELP_MORE_ABOVE);
147 else
148 putchar(' ');
149 gotoxy(nr - HELP_BOTTOM_MARGIN - 1, nc - HELP_RIGHT_MARGIN);
150 if (curr_line < numlines - ph)
151 putchar(HELP_MORE_BELOW);
152 else
153 putchar(' ');
155 scan = get_key(stdout, 0); // wait for user keypress
157 switch (scan) {
158 case KEY_HOME:
159 curr_line = 0;
160 break;
161 case KEY_END:
162 curr_line = numlines;
163 break;
164 case KEY_UP:
165 curr_line--;
166 break;
167 case KEY_DOWN:
168 curr_line++;
169 break;
170 case KEY_PGUP:
171 curr_line -= ph;
172 break;
173 case KEY_PGDN:
174 curr_line += ph;
175 break;
176 default:
177 break;
179 if (curr_line > numlines - ph)
180 curr_line = numlines - ph;
181 if (curr_line < 0)
182 curr_line = 0;
184 out:
185 cls();
186 return;
188 puke:
189 gotoxy(HELP_BODY_ROW, HELP_LEFT_MARGIN);
190 fputs(line, stdout);
191 while (1) {
192 scan = get_key(stdin, 0);
193 if (scan == KEY_ESC)
194 break;
196 goto out;
199 void runhelp(const char *filename)
201 char fullname[HELPDIRLEN + 16];
203 cls();
204 cursoroff();
205 if (helpbasedir[0] != 0) {
206 strcpy(fullname, helpbasedir);
207 strcat(fullname, "/");
208 strcat(fullname, filename);
209 showhelp(fullname);
210 } else
211 showhelp(filename); // Assume filename is absolute
214 void runhelpsystem(unsigned int helpid)
216 char filename[15];
218 sprintf(filename, "hlp%05d.txt", helpid);
219 runhelp(filename);
222 void init_help(const char *helpdir)
224 if (helpdir != NULL)
225 strcpy(helpbasedir, helpdir);
226 else
227 helpbasedir[0] = 0;
230 void close_help(void)