Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1987, 1988 by MIT Student Information Processing Board |
| 3 | * |
Theodore Ts'o | 06cefee | 1999-10-23 01:16:22 +0000 | [diff] [blame] | 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. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 14 | #ifdef HAVE_UNISTD_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 17 | #ifdef HAVE_STDLIB_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 20 | #ifdef HAVE_ERRNO_H |
| 21 | #include <errno.h> |
| 22 | #else |
| 23 | extern int errno; |
| 24 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 36 | void 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'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 42 | char *buffer; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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 */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 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 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 69 | free(buffer); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 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 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 89 | (void) strcpy(buffer, info->info_dirs[idx]); |
| 90 | (void) strcat(buffer, "/"); |
| 91 | (void) strcat(buffer, argv[1]); |
| 92 | (void) strcat(buffer, ".info"); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 93 | fd = open(buffer, O_RDONLY); |
| 94 | free(buffer); |
| 95 | if (fd >= 0) |
| 96 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 97 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 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); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 102 | strcat(buf, argv[1]); |
| 103 | ss_perror(sci_idx, 0, buf); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 104 | free(buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | return; |
| 106 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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? */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 116 | while (wait(0) != child) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 117 | /* do nothing if wrong pid */ |
| 118 | }; |
| 119 | } |
| 120 | } |
| 121 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 122 | #ifndef HAVE_DIRENT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 123 | #include <sys/dir.h> |
| 124 | #else |
| 125 | #include <dirent.h> |
| 126 | #endif |
| 127 | |
| 128 | void 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 | |
| 165 | void 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 | } |