kernel - swapcache - vm_object_page_remove()
[dragonfly.git] / gnu / lib / libdialog / help.c
blobde49c6ab576f2e7438974fbafaf3f3eddb90c130
1 /***************************************************************
3 * Program: help.c
4 * Author: Marc van Kempen
5 * Desc: get help
8 * Copyright (c) 1995, Marc van Kempen
10 * All rights reserved.
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.
20 ***************************************************************/
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>
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;
34 /******************************************************************
36 * helpfile routines
38 ******************************************************************/
40 void
41 use_helpfile(char *hfile)
43 * desc: set the helpfile to be opened on pressing F1 to <helpfile>
46 if (hfile != NULL) {
47 _helpfile = _helpfilebuf;
48 strcpy(_helpfile, hfile);
49 } else {
50 _helpfile = NULL;
53 return;
54 } /* use_helpfile() */
56 void
57 display_helpfile(void)
59 * desc: display the current helpfile in a window
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;
69 if (in_help) return; /* dont call help when you're in help */
71 if (_helpfile != NULL) {
72 if ((w = dupwin(newscr)) == NULL) {
73 dialog_notify("No memory to dup previous screen\n");
74 return;
76 if ((f = fopen(_helpfile, "r")) == NULL) {
77 sprintf(msg, "Can't open helpfile : %s\n", _helpfile);
78 dialog_notify(msg);
79 return;
81 if (fstat(fileno(f), &sb)) {
82 sprintf(msg, "Can't stat helpfile : %s\n", _helpfile);
83 dialog_notify(msg);
84 return;
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;
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;
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 */
112 return;
113 } /* display_helpfile() */
116 /******************************************************************
118 * helpline routines
120 ******************************************************************/
122 void
123 use_helpline(char *hline)
125 * desc: set the helpline to printed in dialogs
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);
137 } else {
138 _helpline = NULL;
141 return;
142 } /* use_helpline() */
144 void
145 display_helpline(WINDOW *w, int y, int width)
147 * desc: display the helpline at the given coordinates <y, x> in the window <w>
150 if (_helpline != NULL) {
151 if (strlen(_helpline) > width - 6) {
152 _helpline[width - 6] = 0;
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 */
163 return;
166 char *
167 get_helpline(void)
169 * desc: allocate new space, copy the helpline to it and return a pointer to it
172 char *hlp;
174 if (_helpline) {
175 hlp = (char *) malloc( strlen(_helpline) + 1 );
176 strcpy(hlp, _helpline);
177 } else {
178 hlp = NULL;
181 return(hlp);
182 } /* get_helpline() */
184 void
185 restore_helpline(char *helpline)
187 * Desc: set the helpline to <helpline> and free the space allocated to it
190 use_helpline(helpline);
191 free(helpline);
193 return;
194 } /* restore_helpline() */