comparison rvinterf/asyncshell/help.c @ 31:5b4e345095c4

fc-shell help command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Oct 2016 18:38:07 +0000
parents
children 27d7d7e2d9bd
comparison
equal deleted inserted replaced
30:43e4c95d148a 31:5b4e345095c4
1 /*
2 * This module implements the fc-shell help facility.
3 */
4
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <stdlib.h>
10
11 char help_file_pathname[] = "/usr/local/share/freecalypso/fc-shell.help";
12
13 show_help_topic(topic)
14 char *topic;
15 {
16 FILE *f;
17 char linebuf[256];
18 char *cp, *np;
19 int flag;
20
21 f = fopen(help_file_pathname, "r");
22 if (!f) {
23 perror(help_file_pathname);
24 return(-1);
25 }
26 for (;;) {
27 if (!fgets(linebuf, sizeof linebuf, f)) {
28 printf("error: help topic \"%s\" not found\n", topic);
29 fclose(f);
30 return(-1);
31 }
32 if (linebuf[0] != '=' || linebuf[1] != '=' || linebuf[2] != '=')
33 continue;
34 for (cp = linebuf + 3; isspace(*cp); cp++)
35 ;
36 for (np = cp; *cp && !isspace(*cp); cp++)
37 ;
38 if (*cp)
39 *cp++ = '\0';
40 if (!strcmp(np, topic))
41 break;
42 }
43 for (flag = 0; fgets(linebuf, sizeof linebuf, f); ) {
44 if (linebuf[0] == '=' && linebuf[1] == '=' &&
45 linebuf[2] == '=') {
46 if (flag)
47 break;
48 else
49 continue;
50 }
51 fputs(linebuf, stdout);
52 flag = 1;
53 }
54 fclose(f);
55 return(0);
56 }
57
58 void
59 cmd_help(argstr)
60 char *argstr;
61 {
62 char *argv[3], dbltopic[34];
63 int argc, rc;
64
65 rc = parse_interactive_command_into_argv(argstr, argv, 0, 2, &argc);
66 if (rc < 0)
67 return;
68 switch (argc) {
69 case 0:
70 show_help_topic("main");
71 break;
72 case 1:
73 show_help_topic(argv[0]);
74 break;
75 case 2:
76 if (strlen(argv[0]) <= 16 && strlen(argv[1]) <= 16) {
77 sprintf(dbltopic, "%s:%s", argv[0], argv[1]);
78 show_help_topic(dbltopic);
79 } else
80 printf("error: no such help topic\n");
81 break;
82 default:
83 fprintf(stderr, "internal error in cmd_help(): bad argc\n");
84 abort();
85 }
86 }