Get rid of varargs.h.
[dragonfly.git] / gnu / lib / libdialog / help.c
1 /***************************************************************
2  *
3  * Program:     help.c
4  * Author:      Marc van Kempen
5  * Desc:        get help
6  *
7  *
8  * Copyright (c) 1995, Marc van Kempen
9  *
10  * All rights reserved.
11  *
12  * This software may be used, modified, copied, distributed, and
13  * sold, in both source and binary form provided that the above
14  * copyright and these terms are retained, verbatim, as the first
15  * lines of this file.  Under no circumstances is the author
16  * responsible for the proper functioning of this software, nor does
17  * the author assume any responsibility for damages incurred with
18  * its use.
19  *
20  ***************************************************************/
21
22 #include <stdlib.h>
23 #include <sys/param.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <string.h>
27 #include <dialog.h>
28
29 static char     _helpfilebuf[MAXPATHLEN];
30 static char     _helplinebuf[77];         /* limit the helpline to 76 characters */
31 static char     *_helpfile = NULL;
32 static char     *_helpline = NULL;
33
34 /******************************************************************
35  *
36  *      helpfile routines
37  *
38  ******************************************************************/
39
40 void
41 use_helpfile(char *hfile)
42 /*
43  * desc: set the helpfile to be opened on pressing F1 to <helpfile>
44  */
45 {
46     if (hfile != NULL) {
47         _helpfile = _helpfilebuf;
48         strcpy(_helpfile, hfile);
49     } else {
50         _helpfile = NULL;
51     }
52
53     return;
54 } /* use_helpfile() */
55
56 void
57 display_helpfile(void)
58 /*
59  * desc: display the current helpfile in a window
60  */
61 {
62     WINDOW      *w;
63     FILE        *f;
64     struct stat sb;
65     char        msg[80], *buf;
66     static int  in_help = FALSE;
67     char        *savehline = NULL;
68
69     if (in_help) return;        /* dont call help when you're in help */
70
71     if (_helpfile != NULL) {
72         if ((w = dupwin(newscr)) == NULL) {
73             dialog_notify("No memory to dup previous screen\n");
74             return;
75         }
76         if ((f = fopen(_helpfile, "r")) == NULL) {
77             sprintf(msg, "Can't open helpfile : %s\n", _helpfile);
78             dialog_notify(msg);
79             return;
80         }
81         if (fstat(fileno(f), &sb)) {
82             sprintf(msg, "Can't stat helpfile : %s\n", _helpfile);
83             dialog_notify(msg);
84             return;
85         }
86         if ((buf = (char *) malloc( sb.st_size )) == NULL) {
87             sprintf(msg, "Could not malloc space for helpfile : %s\n", _helpfile);
88             dialog_notify(msg);
89             return;
90         }
91         if (fread(buf, 1, sb.st_size, f) != sb.st_size) {
92             sprintf(msg, "Could not read entire help file : %s", _helpfile);
93             dialog_notify(msg);
94             free(buf);
95             return;
96         }
97         buf[sb.st_size] = 0;
98         in_help = TRUE;
99         savehline = get_helpline();
100         use_helpline("Use arrowkeys, PgUp, PgDn, Home and End to move through text");
101         dialog_mesgbox("Online help", buf, LINES-4, COLS-4);
102         restore_helpline(savehline);
103         in_help = FALSE;
104         touchwin(w);
105         wrefresh(w);
106         delwin(w);
107         free(buf);
108     } else {
109         /* do nothing */
110     }
111
112     return;
113 } /* display_helpfile() */
114
115
116 /******************************************************************
117  *
118  *      helpline routines
119  *
120  ******************************************************************/
121
122 void
123 use_helpline(char *hline)
124 /*
125  * desc: set the helpline to printed in dialogs
126  */
127 {
128     if (hline) {
129         _helpline = _helplinebuf;
130         if (strlen(hline) > 76) {
131             /* only display the first 76 characters in the helpline */
132             strncpy(_helpline, hline, 76);
133             _helpline[76] = 0;
134         } else {
135             strcpy(_helpline, hline);
136         }
137     } else {
138         _helpline = NULL;
139     }
140
141     return;
142 } /* use_helpline() */
143
144 void
145 display_helpline(WINDOW *w, int y, int width)
146 /*
147  * desc: display the helpline at the given coordinates <y, x> in the window <w>
148  */
149 {
150     if (_helpline != NULL) {
151         if (strlen(_helpline) > width - 6) {
152             _helpline[width - 6] = 0;
153         }
154         wmove(w, y, (int) (width - strlen(_helpline)- 4) / 2);
155         wattrset(w, title_attr);
156         waddstr(w, "[ ");
157         waddstr(w, _helpline);
158         waddstr(w, " ]");
159     } else {
160         /* do nothing */
161     }
162
163     return;
164 }
165
166 char *
167 get_helpline(void)
168 /*
169  * desc: allocate new space, copy the helpline to it and return a pointer to it
170  */
171 {
172     char *hlp;
173
174     if (_helpline) {
175         hlp = (char *) malloc( strlen(_helpline) + 1 );
176         strcpy(hlp, _helpline);
177     } else {
178         hlp = NULL;
179     }
180
181     return(hlp);
182 } /* get_helpline() */
183
184 void
185 restore_helpline(char *helpline)
186 /*
187  * Desc: set the helpline to <helpline> and free the space allocated to it
188  */
189 {
190     use_helpline(helpline);
191     free(helpline);
192
193     return;
194 } /* restore_helpline() */