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 right
= nc
- HELP_RIGHT_MARGIN
;
61 nlines
= nr
- HELP_BODY_ROW
- HELP_BOTTOM_MARGIN
- 1;
63 f
= findline(buf
, from
);
65 return; // nothing to print
67 f
++; // start of from+1st line
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
);
90 void showhelp(const char *filename
)
97 } buf
; // This is to avoild type-punning issues
102 int rv
, numlines
, curr_line
;
104 if (getscreensize(1, &nr
, &nc
)) {
105 /* Unknown screen size? */
109 ph
= nr
- HELP_BODY_ROW
;
112 /* Turn autowrap off, to avoid scrolling the menu */
115 if (filename
== NULL
) { // print file contents
116 strcpy(line
, "Filename not given");
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
);
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
);
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
) {
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
143 printtext(text
, curr_line
);
144 gotoxy(HELP_BODY_ROW
- 1, nc
- HELP_RIGHT_MARGIN
);
146 putchar(HELP_MORE_ABOVE
);
149 gotoxy(nr
- HELP_BOTTOM_MARGIN
- 1, nc
- HELP_RIGHT_MARGIN
);
150 if (curr_line
< numlines
- ph
)
151 putchar(HELP_MORE_BELOW
);
155 scan
= get_key(stdout
, 0); // wait for user keypress
162 curr_line
= numlines
;
179 if (curr_line
> numlines
- ph
)
180 curr_line
= numlines
- ph
;
189 gotoxy(HELP_BODY_ROW
, HELP_LEFT_MARGIN
);
192 scan
= get_key(stdin
, 0);
199 void runhelp(const char *filename
)
201 char fullname
[HELPDIRLEN
+ 16];
205 if (helpbasedir
[0] != 0) {
206 strcpy(fullname
, helpbasedir
);
207 strcat(fullname
, "/");
208 strcat(fullname
, filename
);
211 showhelp(filename
); // Assume filename is absolute
214 void runhelpsystem(unsigned int helpid
)
218 sprintf(filename
, "hlp%05d.txt", helpid
);
222 void init_help(const char *helpdir
)
225 strcpy(helpbasedir
, helpdir
);
230 void close_help(void)