Adding debian version 3.35-1.
[syslinux-debian/hramrach.git] / menu / libmenu / help.c
blobc71cd26695ce8777b330bc4a7b1522fdbd23789f
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 <loadfile.h> // to read entire file into memory
19 char helpbasedir[HELPDIRLEN]; // name of help directory limited to HELPDIRLEN
21 // Find the occurence of the count'th \n in buffer (or NULL) if not found
22 char * findline(char*buffer,int count)
24 int ctr;
25 char *p= buffer-1;
27 if (count < 1) return buffer;
28 for (ctr=0; ctr < count; ctr++) {
29 p = strchr(p+1,'\n');
30 if (p==NULL) return NULL;
32 return p;
35 // return the number of lines in buffer
36 int countlines(char*buffer)
38 int ans;
39 const char *p;
41 p = buffer-1;
42 ans = 1;
43 while(p) {p = strchr(p+1,'\n'); ans++; }
44 return ans;
48 // Print numlines of text starting from buf
49 void printtext(char*buf, int from)
51 char *p,*f;
52 char right,bot,nlines;
54 // clear window to print
55 right = getnumcols() - HELP_RIGHT_MARGIN;
56 bot = getnumrows() - HELP_BOTTOM_MARGIN;
57 nlines = bot-HELP_BODY_ROW+1;
58 scrollupwindow(HELP_BODY_ROW,HELP_LEFT_MARGIN,bot,right,0x07,nlines);
60 f = findline(buf,from);
61 if (!f) return; // nothing to print
62 if (*f=='\n') f++; // start of from+1st line
63 p = findline(f,nlines);
64 if (p && (*p=='\n')) *p = '\0'; // change to NUL
65 gotoxy(HELP_BODY_ROW,HELP_LEFT_MARGIN,HELPPAGE);
66 cswprint(f,0x07,HELP_LEFT_MARGIN);
67 if (p) *p = '\n'; // set it back
70 void showhelp(const char *filename)
72 char nc,nr,ph;
73 char *title,*text;
74 union { char *buffer; void *vbuf; } buf; // This is to avoild type-punning issues
76 char line[512];
77 size_t size;
78 char scan;
79 int rv,numlines,curr_line;
81 nc = getnumcols();
82 nr = getnumrows();
83 ph = nr - HELP_BOTTOM_MARGIN - HELP_BODY_ROW - 1;
84 cls();
85 drawbox(0,0,nr,nc-1,HELPPAGE,0x07,HELPBOX);
87 drawhorizline(2,0,nc-1,HELPPAGE,0x07,HELPBOX,0); // dumb==0
88 if (filename == NULL) { // print file contents
89 gotoxy(HELP_BODY_ROW,HELP_LEFT_MARGIN,HELPPAGE);
90 cswprint("Filename not given",0x07,HELP_LEFT_MARGIN);
91 while (1) {
92 inputc(&scan);
93 if (scan == ESCAPE) break;
95 cls();
96 return;
99 rv = loadfile(filename,(void **)&buf.vbuf, &size); // load entire file into memory
100 if (rv < 0) { // Error reading file or no such file
101 sprintf(line, "Error reading file or file not found\n file=%s",filename);
102 gotoxy(HELP_BODY_ROW,HELP_LEFT_MARGIN,HELPPAGE);
103 cswprint(line,0x07,HELP_LEFT_MARGIN);
104 while (1) {
105 inputc(&scan);
106 if (scan == ESCAPE) break;
108 cls();
109 return;
112 title = buf.buffer;
113 text = findline(title,1); // end of first line
114 *text++='\0'; // end the title string and increment text
116 // Now we have a file just print it.
117 gotoxy(1,(nc-strlen(title))/2,HELPPAGE);
118 csprint(title,0x07);
119 numlines = countlines(text);
120 curr_line = 0;
121 scan = ESCAPE+1; // anything except ESCAPE
123 while(scan != ESCAPE) {
124 printtext(text,curr_line);
125 gotoxy(HELP_BODY_ROW-1,nc-HELP_RIGHT_MARGIN,HELPPAGE);
126 if (curr_line > 0)
127 putch(HELP_MORE_ABOVE,0x07,HELPPAGE);
128 else putch(' ',0x07,HELPPAGE);
129 gotoxy(nr-HELP_BOTTOM_MARGIN+1,nc-HELP_RIGHT_MARGIN,HELPPAGE);
130 if (curr_line < numlines - ph)
131 putch(HELP_MORE_BELOW,0x07,HELPPAGE);
132 else putch(' ',0x07,HELPPAGE);
134 inputc(&scan); // wait for user keypress
136 switch(scan) {
137 case HOMEKEY:
138 curr_line = 0;
139 break;
140 case ENDKEY:
141 curr_line = numlines;
142 break;
143 case UPARROW:
144 curr_line--;
145 break;
146 case DNARROW:
147 curr_line++;
148 break;
149 case PAGEUP:
150 curr_line -= ph;
151 break;
152 case PAGEDN:
153 curr_line += ph;
154 break;
155 default:
156 break;
158 if (curr_line > numlines - ph) curr_line = numlines-ph;
159 if (curr_line < 0) curr_line = 0;
161 cls();
162 return;
165 void runhelp(const char *filename)
167 char dp;
168 char fullname[HELPDIRLEN+16];
170 dp = getdisppage();
171 if (dp != HELPPAGE) setdisppage(HELPPAGE);
172 cursoroff();
173 if (helpbasedir[0] != 0) {
174 strcpy(fullname,helpbasedir);
175 strcat(fullname,"/");
176 strcat(fullname,filename);
177 showhelp(fullname);
179 else showhelp (filename); // Assume filename is absolute
180 if (dp != HELPPAGE) setdisppage(dp);
183 void runhelpsystem(unsigned int helpid)
185 char filename[15];
187 sprintf(filename,"hlp%5d.txt",helpid);
188 runhelp(filename);
191 void init_help(const char *helpdir)
193 if (helpdir != NULL)
194 strcpy(helpbasedir,helpdir);
195 else helpbasedir[0] = 0;
198 void close_help(void)