blob: 1c9b3a25d5b65c3fae2d6b41defa2fb4cd500547 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * Copyright 1987, 1988 by MIT Student Information Processing Board
3 *
Theodore Ts'o06cefee1999-10-23 01:16:22 +00004 * 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.
Theodore Ts'o3839e651997-04-26 13:21:57 +000012 */
13
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#ifdef HAVE_UNISTD_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000015#include <unistd.h>
16#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#ifdef HAVE_STDLIB_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000018#include <stdlib.h>
19#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000020#ifdef HAVE_ERRNO_H
21#include <errno.h>
22#else
23extern int errno;
24#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000025#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <sys/param.h>
27#include <sys/types.h>
28#include <sys/file.h>
29#ifdef NEED_SYS_FCNTL_H
30/* just for O_* */
31#include <sys/fcntl.h>
32#endif
33#include <sys/wait.h>
34#include "ss_internal.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000035
Theodore Ts'o3839e651997-04-26 13:21:57 +000036void ss_help (argc, argv, sci_idx, info_ptr)
37 int argc;
38 char const * const *argv;
39 int sci_idx;
40 pointer info_ptr;
41{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000042 char *buffer;
Theodore Ts'o3839e651997-04-26 13:21:57 +000043 char const *request_name;
44 int code;
45 int fd, child;
46 register int idx;
47 register ss_data *info;
48
49 request_name = ss_current_request(sci_idx, &code);
50 if (code != 0) {
51 ss_perror(sci_idx, code, "");
52 return; /* no ss_abort_line, if invalid invocation */
53 }
54 if (argc == 1) {
55 ss_list_requests(argc, argv, sci_idx, info_ptr);
56 return;
57 }
58 else if (argc != 2) {
59 /* should do something better than this */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000060 buffer = malloc(80+2*strlen(request_name));
61 if (!buffer) {
62 ss_perror(sci_idx, 0,
63 "couldn't allocate memory to print usage message");
64 return;
65 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000066 sprintf(buffer, "usage:\n\t%s [topic|command]\nor\t%s\n",
67 request_name, request_name);
68 ss_perror(sci_idx, 0, buffer);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000069 free(buffer);
Theodore Ts'o3839e651997-04-26 13:21:57 +000070 return;
71 }
72 info = ss_info(sci_idx);
73 if (info->info_dirs == (char **)NULL) {
74 ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
75 return;
76 }
77 if (info->info_dirs[0] == (char *)NULL) {
78 ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
79 return;
80 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +000081 for (fd = -1, idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) {
82 buffer = malloc(strlen (info->info_dirs[idx]) + 1 +
83 strlen (argv[1]) + 6);
84 if (!buffer) {
85 ss_perror(sci_idx, 0,
86 "couldn't allocate memory for help filename");
87 return;
88 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000089 (void) strcpy(buffer, info->info_dirs[idx]);
90 (void) strcat(buffer, "/");
91 (void) strcat(buffer, argv[1]);
92 (void) strcat(buffer, ".info");
Theodore Ts'o50e1e101997-04-26 13:58:21 +000093 fd = open(buffer, O_RDONLY);
94 free(buffer);
95 if (fd >= 0)
96 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +000098 if (fd < 0) {
99#define MSG "No info found for "
100 char *buf = malloc(strlen (MSG) + strlen (argv[1]) + 1);
101 strcpy(buf, MSG);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 strcat(buf, argv[1]);
103 ss_perror(sci_idx, 0, buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000104 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 return;
106 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107 switch (child = fork()) {
108 case -1:
109 ss_perror(sci_idx, errno, "Can't fork for pager");
110 return;
111 case 0:
112 (void) dup2(fd, 0); /* put file on stdin */
113 ss_page_stdin();
114 default:
115 (void) close(fd); /* what can we do if it fails? */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000116 while (wait(0) != child) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117 /* do nothing if wrong pid */
118 };
119 }
120}
121
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000122#ifndef HAVE_DIRENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123#include <sys/dir.h>
124#else
125#include <dirent.h>
126#endif
127
128void ss_add_info_dir(sci_idx, info_dir, code_ptr)
129 int sci_idx;
130 char *info_dir;
131 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) {
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 info->info_dirs = (char **)NULL;
155 *code_ptr = errno;
156 return;
157 }
158 info->info_dirs = dirs;
159 dirs[n_dirs + 1] = (char *)NULL;
160 dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
161 strcpy(dirs[n_dirs], info_dir);
162 *code_ptr = 0;
163}
164
165void ss_delete_info_dir(sci_idx, info_dir, code_ptr)
166 int sci_idx;
167 char *info_dir;
168 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}