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