help.c revision 06cefee50dc681838454fcd079ba5b2760969f1b
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#ifdef HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#ifdef HAVE_STDLIB_H
18#include <stdlib.h>
19#endif
20#ifdef HAVE_ERRNO_H
21#include <errno.h>
22#else
23extern int errno;
24#endif
25#include <fcntl.h>
26#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"
35
36void 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{
42    char *buffer;
43    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 */
60	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	}
66	sprintf(buffer, "usage:\n\t%s [topic|command]\nor\t%s\n",
67		request_name, request_name);
68	ss_perror(sci_idx, 0, buffer);
69	free(buffer);
70	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    }
81    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	}
89	(void) strcpy(buffer, info->info_dirs[idx]);
90	(void) strcat(buffer, "/");
91	(void) strcat(buffer, argv[1]);
92	(void) strcat(buffer, ".info");
93	fd = open(buffer, O_RDONLY);
94	free(buffer);
95	if (fd >= 0)
96	    break;
97    }
98    if (fd < 0) {
99#define MSG "No info found for "
100        char *buf = malloc(strlen (MSG) + strlen (argv[1]) + 1);
101	strcpy(buf, MSG);
102	strcat(buf, argv[1]);
103	ss_perror(sci_idx, 0, buf);
104	free(buf);
105	return;
106    }
107    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? */
116	while (wait(0) != child) {
117	    /* do nothing if wrong pid */
118	};
119    }
120}
121
122#ifndef HAVE_DIRENT_H
123#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}
186