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 * ----------------------------------------------------------------------- */
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
)
30 for (ctr
= 0; ctr
< count
; ctr
++) {
31 p
= strchr(p
+ 1, '\n');
38 // return the number of lines in buffer
39 static int countlines(char *buffer
)
47 p
= strchr(p
+ 1, '\n');
53 // Print numlines of text starting from buf
54 static void printtext(char *buf
, int from
)
59 // clear window to print
60 nlines
= nr
- HELP_BODY_ROW
- HELP_BOTTOM_MARGIN
- 1;
62 f
= findline(buf
, from
);
64 return; // nothing to print
66 f
++; // start of from+1st line
69 for (i
= 0; i
< nlines
; i
++) {
70 gotoxy(HELP_BODY_ROW
+ i
, HELP_LEFT_MARGIN
);
73 gotoxy(HELP_BODY_ROW
+ i
, nc
- 1);
74 putch(LEFT_BORDER
, 0x07);
77 gotoxy(HELP_BODY_ROW
+ i
, HELP_LEFT_MARGIN
);
89 void showhelp(const char *filename
)
96 } buf
; // This is to avoild type-punning issues
101 int rv
, numlines
, curr_line
;
103 if (getscreensize(1, &nr
, &nc
)) {
104 /* Unknown screen size? */
108 ph
= nr
- HELP_BODY_ROW
;
111 /* Turn autowrap off, to avoid scrolling the menu */
114 if (filename
== NULL
) { // print file contents
115 strcpy(line
, "Filename not given");
119 rv
= loadfile(filename
, (void **)&buf
.vbuf
, &size
); // load entire file into memory
120 if (rv
< 0) { // Error reading file or no such file
121 sprintf(line
, "Error reading file or file not found\n file=%s", filename
);
126 text
= findline(title
, 1); // end of first line
127 *text
++ = '\0'; // end the title string and increment text
129 // Now we have a file just print it.
130 numlines
= countlines(text
);
132 scan
= KEY_ESC
+ 1; // anything except ESCAPE
134 /* top, left, bottom, right, attr */
135 drawbox(0, 0, nr
- 1, nc
- 1, 0x07);
136 while (scan
!= KEY_ESC
) {
138 gotoxy(1, (nc
- strlen(title
)) / 2);
139 fputs(title
, stdout
);
140 drawhorizline(2, HELP_LEFT_MARGIN
- 1, nc
- HELP_RIGHT_MARGIN
, 0x07, 0); // dumb==0
142 printtext(text
, curr_line
);
143 gotoxy(HELP_BODY_ROW
- 1, nc
- HELP_RIGHT_MARGIN
);
145 putchar(HELP_MORE_ABOVE
);
148 gotoxy(nr
- HELP_BOTTOM_MARGIN
- 1, nc
- HELP_RIGHT_MARGIN
);
149 if (curr_line
< numlines
- ph
)
150 putchar(HELP_MORE_BELOW
);
154 scan
= get_key(stdout
, 0); // wait for user keypress
161 curr_line
= numlines
;
178 if (curr_line
> numlines
- ph
)
179 curr_line
= numlines
- ph
;
188 gotoxy(HELP_BODY_ROW
, HELP_LEFT_MARGIN
);
191 scan
= get_key(stdin
, 0);
198 void runhelp(const char *filename
)
200 char fullname
[HELPDIRLEN
+ 16];
204 if (helpbasedir
[0] != 0) {
205 strcpy(fullname
, helpbasedir
);
206 strcat(fullname
, "/");
207 strcat(fullname
, filename
);
210 showhelp(filename
); // Assume filename is absolute
213 void runhelpsystem(unsigned int helpid
)
217 sprintf(filename
, "hlp%05d.txt", helpid
);
221 void init_help(const char *helpdir
)
224 strcpy(helpbasedir
, helpdir
);
229 void close_help(void)