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