blob: 3f3c7f534d5498dd9eccc14fd07a08e01fe63abc [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * Copyright 1987, 1988 by MIT Student Information Processing Board
3 *
4 * For copyright info, see copyright.h.
5 */
6
Theodore Ts'o50e1e101997-04-26 13:58:21 +00007#ifdef HAVE_UNISTD_H
Theodore Ts'of3db3561997-04-26 13:34:30 +00008#include <unistd.h>
9#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010#ifdef HAVE_STDLIB_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000011#include <stdlib.h>
12#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000013#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000014#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
25extern int errno;
26
27void 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{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000033 char *buffer;
Theodore Ts'o3839e651997-04-26 13:21:57 +000034 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 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000051 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 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 sprintf(buffer, "usage:\n\t%s [topic|command]\nor\t%s\n",
58 request_name, request_name);
59 ss_perror(sci_idx, 0, buffer);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000060 free(buffer);
Theodore Ts'o3839e651997-04-26 13:21:57 +000061 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 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +000072 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 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000080 (void) strcpy(buffer, info->info_dirs[idx]);
81 (void) strcat(buffer, "/");
82 (void) strcat(buffer, argv[1]);
83 (void) strcat(buffer, ".info");
Theodore Ts'o50e1e101997-04-26 13:58:21 +000084 fd = open(buffer, O_RDONLY);
85 free(buffer);
86 if (fd >= 0)
87 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +000089 if (fd < 0) {
90#define MSG "No info found for "
91 char *buf = malloc(strlen (MSG) + strlen (argv[1]) + 1);
92 strcpy(buf, MSG);
Theodore Ts'o3839e651997-04-26 13:21:57 +000093 strcat(buf, argv[1]);
94 ss_perror(sci_idx, 0, buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000095 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 return;
97 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 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? */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000107 while (wait(0) != child) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108 /* do nothing if wrong pid */
109 };
110 }
111}
112
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000113#ifndef HAVE_DIRENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114#include <sys/dir.h>
115#else
116#include <dirent.h>
117#endif
118
119void 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
156void 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}