modified: myjupyterlab.sh
[GalaxyCodeBases.git] / c_cpp / etc / calc / help.c
blobc6241c2757cde7acbcab11b8acf72c5d9d62070a
1 /*
2 * help - display help for calc
4 * Copyright (C) 1999-2007 Landon Curt Noll
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.3 $
21 * @(#) $Id: help.c,v 30.3 2013/08/11 01:08:32 chongo Exp $
22 * @(#) $Source: /usr/local/src/cmd/calc/RCS/help.c,v $
24 * Under source code control: 1997/09/14 10:58:30
25 * File existed as early as: 1997
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <sys/types.h>
35 #include <signal.h>
37 #include "calc.h"
38 #include "conf.h"
40 #include "have_unistd.h"
41 #if defined(HAVE_UNISTD_H)
42 #include <unistd.h>
43 #endif
45 #if defined(_WIN32)
46 # define popen _popen
47 # define pclose _pclose
48 #endif
52 * some help topics are symbols, so we alias them to nice filenames
54 STATIC struct help_alias {
55 char *topic;
56 char *filename;
57 } halias[] = {
58 {"=", "address"},
59 {"->", "arrow"},
60 {"=", "assign"},
61 {"*", "dereference"},
62 {".", "oldvalue"},
63 {"%", "mod"},
64 {"//", "quo"},
65 {"#", "pound"},
66 {"calc", "help"},
67 {"copy", "blkcpy"},
68 {"copying", "COPYING"},
69 {"copying-lgpl", "COPYING-LGPL"},
70 {"copying_lgpl", "COPYING-LGPL"},
71 {"COPYING_LGPL", "COPYING-LGPL"},
72 {"Copyright", "copyright"},
73 {"COPYRIGHT", "copyright"},
74 {"Copyleft", "copyright"},
75 {"COPYLEFT", "copyright"},
76 {"define", "command"},
77 {"read", "command"},
78 {"write", "command"},
79 {"quit", "command"},
80 {"exit", "command"},
81 {"abort", "command"},
82 {"cd", "command"},
83 {"show", "command"},
84 {"stdlib", "resource"},
85 {NULL, NULL}
90 * external values
92 EXTERN char *pager; /* $PAGER or default */
96 * page_file - output an open file thru a pager
98 * The popen() below is fairly safe. The $PAGER environment variable
99 * (supplied by the user) is the command we need to run without args.
100 * True, the user could set $PAGER is a bogus prog ... but if -m is
101 * 5 or 7 (read and exec allowed), then the calc is given ``permission''
102 * to open the help file for reading as well as to exec the pager!
104 * given:
105 * stream open file stream of the file to send to the pager
107 S_FUNC void
108 page_file(FILE *stream)
110 FILE *cmd; /* pager command */
111 char buf[BUFSIZ+1]; /* I/O buffer */
112 char *fgets_ret; /* fgets() return value */
115 * flush any pending I/O
117 fflush(stderr);
118 fflush(stdout);
119 fflush(stdin);
122 * form a write pipe to a pager
124 if (pager == NULL || pager[0] == '\0') {
125 cmd = stdout;
126 } else {
127 cmd = popen(pager, "w");
129 if (cmd == NULL) {
130 fprintf(stderr, "unable form pipe to pager: %s", pager);
131 return;
135 * read the help file and send non-## lines to the pager
136 * until EOF or error
138 buf[BUFSIZ] = '\0';
139 do {
142 * read the next line that does not begin with ##
144 buf[0] = '\0';
145 while ((fgets_ret = fgets(buf, BUFSIZ, stream)) != NULL &&
146 buf[0] == '#' && buf[1] == '#') {
150 * stop reading when we reach EOF (or error) on help file
152 if (fgets_ret == NULL) {
153 fflush(cmd);
154 break;
158 * write the line to pager, if possible
160 * We have to use fprintf() instead of fputs() because
161 * on NetBSD, fputs() function returns 0 on success and
162 # EOF on error. *sigh*
164 } while (fprintf(cmd, "%s", buf) > 0);
167 * all done, EOF or error, so just clean up
169 (void) pclose(cmd);
170 fflush(stderr);
171 fflush(stdout);
172 fflush(stdin);
173 return;
178 * givehelp - display a help file
180 * given:
181 * type the type of help to give, NULL => index
183 void
184 givehelp(char *type)
186 struct help_alias *p; /* help alias being considered */
187 FILE *stream; /* help file stream */
188 char *helppath; /* path to the help file */
189 char *c;
192 * check permissions to see if we are allowed to help
194 if (!allow_exec || !allow_read) {
195 fprintf(stderr,
196 "sorry, help is only allowed with -m mode 5 or 7\n");
197 return;
200 /* catch the case where we just print the index */
201 if (type == NULL) {
202 type = DEFAULTCALCHELP; /* the help index file */
205 /* alias the type of help, if needed */
206 for (p=halias; p->topic; ++p) {
207 if (strcmp(type, p->topic) == 0) {
208 type = p->filename;
209 break;
214 * sanity check on name
216 /* look for /. or a leading . */
217 if (strstr(type, "/.") != NULL || type[0] == '.') {
218 fprintf(stderr, "bad help name\n");
219 return;
221 /* look for chars that could be shell meta chars */
222 for (c = type; *c; ++c) {
223 switch ((int)*c) {
224 case '+':
225 case ',':
226 case '-':
227 case '.':
228 case '/':
229 case '_':
230 break;
231 default:
232 if (!isascii((int)*c) || !isalnum((int)*c)) {
233 fprintf(stderr, "bogus char in help name\n");
234 return;
236 break;
241 * special case for Copyright and Copyleft
243 if (strcmp(type, "copyright") == 0) {
244 fputs(Copyright, stdout);
245 fflush(stdout);
246 return;
250 * open the helpfile (looking in HELPDIR first)
252 #if defined(CUSTOM)
253 if (sizeof(CUSTOMHELPDIR) > sizeof(HELPDIR)) {
254 helppath = (char *)malloc(sizeof(CUSTOMHELPDIR)+1+strlen(type));
255 } else {
256 helppath = (char *)malloc(sizeof(HELPDIR)+1+strlen(type));
258 #else /* CUSTOM */
259 helppath = (char *)malloc(sizeof(HELPDIR)+1+strlen(type));
260 #endif /* CUSTOM */
261 if (helppath == NULL) {
262 fprintf(stderr, "malloc failure in givehelp()\n");
263 return;
265 sprintf(helppath, "%s/%s", HELPDIR, type);
266 stream = fopen(helppath, "r");
267 if (stream != NULL) {
270 * we have the help file open, now display it
272 page_file(stream);
273 (void) fclose(stream);
275 #if defined(CUSTOM)
277 * open the helpfile (looking in CUSTOMHELPDIR last)
279 } else {
281 sprintf(helppath, "%s/%s", CUSTOMHELPDIR, type);
282 stream = fopen(helppath, "r");
283 if (stream == NULL) {
285 /* no such help file */
286 fprintf(stderr,
287 "%s: no such help file, try: help help\n",
288 type);
289 } else {
291 /* we have the help file open, now display it */
292 page_file(stream);
293 (void) fclose(stream);
295 #endif /* CUSTOM */
299 * cleanup
301 free(helppath);
302 return;