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 <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
)
27 if (count
< 1) return buffer
;
28 for (ctr
=0; ctr
< count
; ctr
++) {
30 if (p
==NULL
) return NULL
;
35 // return the number of lines in buffer
36 int countlines(char*buffer
)
43 while(p
) {p
= strchr(p
+1,'\n'); ans
++; }
48 // Print numlines of text starting from buf
49 void printtext(char*buf
, int from
)
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
)
74 union { char *buffer
; void *vbuf
; } buf
; // This is to avoild type-punning issues
79 int rv
,numlines
,curr_line
;
83 ph
= nr
- HELP_BOTTOM_MARGIN
- HELP_BODY_ROW
- 1;
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
);
93 if (scan
== ESCAPE
) break;
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
);
106 if (scan
== ESCAPE
) break;
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
);
119 numlines
= countlines(text
);
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
);
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
141 curr_line
= numlines
;
158 if (curr_line
> numlines
- ph
) curr_line
= numlines
-ph
;
159 if (curr_line
< 0) curr_line
= 0;
165 void runhelp(const char *filename
)
168 char fullname
[HELPDIRLEN
+16];
171 if (dp
!= HELPPAGE
) setdisppage(HELPPAGE
);
173 if (helpbasedir
[0] != 0) {
174 strcpy(fullname
,helpbasedir
);
175 strcat(fullname
,"/");
176 strcat(fullname
,filename
);
179 else showhelp (filename
); // Assume filename is absolute
180 if (dp
!= HELPPAGE
) setdisppage(dp
);
183 void runhelpsystem(unsigned int helpid
)
187 sprintf(filename
,"hlp%5d.txt",helpid
);
191 void init_help(const char *helpdir
)
194 strcpy(helpbasedir
,helpdir
);
195 else helpbasedir
[0] = 0;
198 void close_help(void)