blob: fbc040f8ce45fa909bda332fd8d7903a44e8cbed [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
Theodore Ts'offf45482003-04-13 00:44:19 -040033#ifdef HAVE_SYS_WAIT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <sys/wait.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040035#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include "ss_internal.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000037
JP Abgralle0ed7402014-03-19 19:08:39 -070038void ss_help(int argc, char const * const *argv, int sci_idx, pointer info_ptr)
Theodore Ts'o3839e651997-04-26 13:21:57 +000039{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000040 char *buffer;
Theodore Ts'o3839e651997-04-26 13:21:57 +000041 char const *request_name;
42 int code;
43 int fd, child;
44 register int idx;
45 register ss_data *info;
46
47 request_name = ss_current_request(sci_idx, &code);
48 if (code != 0) {
49 ss_perror(sci_idx, code, "");
50 return; /* no ss_abort_line, if invalid invocation */
51 }
52 if (argc == 1) {
53 ss_list_requests(argc, argv, sci_idx, info_ptr);
54 return;
55 }
56 else if (argc != 2) {
57 /* should do something better than this */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000058 buffer = malloc(80+2*strlen(request_name));
59 if (!buffer) {
60 ss_perror(sci_idx, 0,
61 "couldn't allocate memory to print usage message");
62 return;
63 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000064 sprintf(buffer, "usage:\n\t%s [topic|command]\nor\t%s\n",
65 request_name, request_name);
66 ss_perror(sci_idx, 0, buffer);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000067 free(buffer);
Theodore Ts'o3839e651997-04-26 13:21:57 +000068 return;
69 }
70 info = ss_info(sci_idx);
71 if (info->info_dirs == (char **)NULL) {
72 ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
73 return;
74 }
75 if (info->info_dirs[0] == (char *)NULL) {
76 ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
77 return;
78 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +000079 for (fd = -1, idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) {
80 buffer = malloc(strlen (info->info_dirs[idx]) + 1 +
81 strlen (argv[1]) + 6);
82 if (!buffer) {
83 ss_perror(sci_idx, 0,
84 "couldn't allocate memory for help filename");
85 return;
86 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000087 (void) strcpy(buffer, info->info_dirs[idx]);
88 (void) strcat(buffer, "/");
89 (void) strcat(buffer, argv[1]);
90 (void) strcat(buffer, ".info");
Theodore Ts'o50e1e101997-04-26 13:58:21 +000091 fd = open(buffer, O_RDONLY);
92 free(buffer);
93 if (fd >= 0)
94 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +000096 if (fd < 0) {
97#define MSG "No info found for "
98 char *buf = malloc(strlen (MSG) + strlen (argv[1]) + 1);
99 strcpy(buf, MSG);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100 strcat(buf, argv[1]);
101 ss_perror(sci_idx, 0, buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000102 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 return;
104 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 switch (child = fork()) {
106 case -1:
107 ss_perror(sci_idx, errno, "Can't fork for pager");
JP Abgralle0ed7402014-03-19 19:08:39 -0700108 (void) close(fd);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 return;
110 case 0:
111 (void) dup2(fd, 0); /* put file on stdin */
112 ss_page_stdin();
113 default:
114 (void) close(fd); /* what can we do if it fails? */
JP Abgralle0ed7402014-03-19 19:08:39 -0700115 while (wait(NULL) != child) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 /* do nothing if wrong pid */
117 };
118 }
119}
120
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000121#ifndef HAVE_DIRENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122#include <sys/dir.h>
123#else
124#include <dirent.h>
125#endif
126
JP Abgralle0ed7402014-03-19 19:08:39 -0700127void ss_add_info_dir(int sci_idx, char *info_dir, int *code_ptr)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128{
129 register ss_data *info;
130 DIR *d;
131 int n_dirs;
132 register char **dirs;
133
134 info = ss_info(sci_idx);
Brian Behlendorfd2021de2007-03-19 08:39:32 -0400135 if (info_dir == NULL || *info_dir == '\0') {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 *code_ptr = SS_ET_NO_INFO_DIR;
137 return;
138 }
139 if ((d = opendir(info_dir)) == (DIR *)NULL) {
140 *code_ptr = errno;
141 return;
142 }
143 closedir(d);
144 dirs = info->info_dirs;
145 for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++)
146 ; /* get number of non-NULL dir entries */
147 dirs = (char **)realloc((char *)dirs,
148 (unsigned)(n_dirs + 2)*sizeof(char *));
149 if (dirs == (char **)NULL) {
150 info->info_dirs = (char **)NULL;
151 *code_ptr = errno;
152 return;
153 }
154 info->info_dirs = dirs;
155 dirs[n_dirs + 1] = (char *)NULL;
156 dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
157 strcpy(dirs[n_dirs], info_dir);
158 *code_ptr = 0;
159}
160
JP Abgralle0ed7402014-03-19 19:08:39 -0700161void ss_delete_info_dir(int sci_idx, char *info_dir, int *code_ptr)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162{
163 register char **i_d;
164 register char **info_dirs;
165
166 info_dirs = ss_info(sci_idx)->info_dirs;
167 for (i_d = info_dirs; *i_d; i_d++) {
168 if (!strcmp(*i_d, info_dir)) {
169 while (*i_d) {
170 *i_d = *(i_d+1);
171 i_d++;
172 }
173 *code_ptr = 0;
174 return;
175 }
176 }
177 *code_ptr = SS_ET_NO_INFO_DIR;
178}