Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ss / help.c
1 /*
2  * Copyright 1987, 1988 by MIT Student Information Processing Board
3  *
4  * For copyright info, see copyright.h.
5  */
6
7 #ifdef HAVE_UNISTD_H
8 #include <unistd.h>
9 #endif
10 #ifdef HAVE_STDLIB_H
11 #include <stdlib.h>
12 #endif
13 #include <fcntl.h>
14 #include <sys/param.h>
15 #include <sys/types.h>
16 #include <sys/file.h>
17 #ifdef NEED_SYS_FCNTL_H
18 /* just for O_* */
19 #include <sys/fcntl.h>
20 #endif
21 #include <sys/wait.h>
22 #include "ss_internal.h"
23 #include "copyright.h"
24
25 extern int errno;
26
27 void ss_help (argc, argv, sci_idx, info_ptr)
28     int argc;
29     char const * const *argv;
30     int sci_idx;
31     pointer info_ptr;
32 {
33     char *buffer;
34     char const *request_name;
35     int code;
36     int fd, child;
37     register int idx;
38     register ss_data *info;
39
40     request_name = ss_current_request(sci_idx, &code);
41     if (code != 0) {
42         ss_perror(sci_idx, code, "");
43         return;         /* no ss_abort_line, if invalid invocation */
44     }
45     if (argc == 1) {
46         ss_list_requests(argc, argv, sci_idx, info_ptr);
47         return;
48     }
49     else if (argc != 2) {
50         /* should do something better than this */
51         buffer = malloc(80+2*strlen(request_name));
52         if (!buffer) {
53                 ss_perror(sci_idx, 0,
54                           "couldn't allocate memory to print usage message");
55                 return;
56         }
57         sprintf(buffer, "usage:\n\t%s [topic|command]\nor\t%s\n",
58                 request_name, request_name);
59         ss_perror(sci_idx, 0, buffer);
60         free(buffer);
61         return;
62     }
63     info = ss_info(sci_idx);
64     if (info->info_dirs == (char **)NULL) {
65         ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
66         return;
67     }
68     if (info->info_dirs[0] == (char *)NULL) {
69         ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
70         return;
71     }
72     for (fd = -1, idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) {
73         buffer = malloc(strlen (info->info_dirs[idx]) + 1 +
74                         strlen (argv[1]) + 6);
75         if (!buffer) {
76             ss_perror(sci_idx, 0,
77                       "couldn't allocate memory for help filename");
78             return;
79         }
80         (void) strcpy(buffer, info->info_dirs[idx]);
81         (void) strcat(buffer, "/");
82         (void) strcat(buffer, argv[1]);
83         (void) strcat(buffer, ".info");
84         fd = open(buffer, O_RDONLY);
85         free(buffer);
86         if (fd >= 0)
87             break;
88     }
89     if (fd < 0) {
90 #define MSG "No info found for "
91         char *buf = malloc(strlen (MSG) + strlen (argv[1]) + 1);
92         strcpy(buf, MSG);
93         strcat(buf, argv[1]);
94         ss_perror(sci_idx, 0, buf);
95         free(buf);
96         return;
97     }
98     switch (child = fork()) {
99     case -1:
100         ss_perror(sci_idx, errno, "Can't fork for pager");
101         return;
102     case 0:
103         (void) dup2(fd, 0); /* put file on stdin */
104         ss_page_stdin();
105     default:
106         (void) close(fd); /* what can we do if it fails? */
107         while (wait(0) != child) {
108             /* do nothing if wrong pid */
109         };
110     }
111 }
112
113 #ifndef HAVE_DIRENT_H
114 #include <sys/dir.h>
115 #else
116 #include <dirent.h>
117 #endif
118
119 void ss_add_info_dir(sci_idx, info_dir, code_ptr)
120     int sci_idx;
121     char *info_dir;
122     int *code_ptr;
123 {
124     register ss_data *info;
125     DIR *d;
126     int n_dirs;
127     register char **dirs;
128
129     info = ss_info(sci_idx);
130     if (info_dir == NULL && *info_dir) {
131         *code_ptr = SS_ET_NO_INFO_DIR;
132         return;
133     }
134     if ((d = opendir(info_dir)) == (DIR *)NULL) {
135         *code_ptr = errno;
136         return;
137     }
138     closedir(d);
139     dirs = info->info_dirs;
140     for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++)
141         ;               /* get number of non-NULL dir entries */
142     dirs = (char **)realloc((char *)dirs,
143                             (unsigned)(n_dirs + 2)*sizeof(char *));
144     if (dirs == (char **)NULL) {
145         info->info_dirs = (char **)NULL;
146         *code_ptr = errno;
147         return;
148     }
149     info->info_dirs = dirs;
150     dirs[n_dirs + 1] = (char *)NULL;
151     dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
152     strcpy(dirs[n_dirs], info_dir);
153     *code_ptr = 0;
154 }
155
156 void ss_delete_info_dir(sci_idx, info_dir, code_ptr)
157     int sci_idx;
158     char *info_dir;
159     int *code_ptr;
160 {
161     register char **i_d;
162     register char **info_dirs;
163
164     info_dirs = ss_info(sci_idx)->info_dirs;
165     for (i_d = info_dirs; *i_d; i_d++) {
166         if (!strcmp(*i_d, info_dir)) {
167             while (*i_d) {
168                 *i_d = *(i_d+1);
169                 i_d++;
170             }
171             *code_ptr = 0;
172             return;
173         }
174     }
175     *code_ptr = SS_ET_NO_INFO_DIR;
176 }