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