| 1 | /* |
| 2 | * Copyright (c) 1988 Mark Nudleman |
| 3 | * Copyright (c) 1988 Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms are permitted |
| 7 | * provided that the above copyright notice and this paragraph are |
| 8 | * duplicated in all such forms and that any documentation, |
| 9 | * advertising materials, and other materials related to such |
| 10 | * distribution and use acknowledge that the software was developed |
| 11 | * by Mark Nudleman and the University of California, Berkeley. The |
| 12 | * name of Mark Nudleman or the |
| 13 | * University may not be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 17 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 18 | */ |
| 19 | |
| 20 | #ifndef lint |
| 21 | char copyright[] = |
| 22 | "@(#) Copyright (c) 1988 Mark Nudleman.\n\ |
| 23 | @(#) Copyright (c) 1988 Regents of the University of California.\n\ |
| 24 | All rights reserved.\n"; |
| 25 | #endif /* not lint */ |
| 26 | |
| 27 | #ifndef lint |
| 28 | static char sccsid[] = "@(#)main.c 5.9 (Berkeley) %G%"; |
| 29 | #endif /* not lint */ |
| 30 | |
| 31 | /* |
| 32 | * Entry point, initialization, miscellaneous routines. |
| 33 | */ |
| 34 | |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/file.h> |
| 37 | #include <stdio.h> |
| 38 | #include <less.h> |
| 39 | |
| 40 | int ispipe; |
| 41 | int new_file; |
| 42 | int is_tty; |
| 43 | char *current_file, *previous_file, *current_name; |
| 44 | off_t prev_pos; |
| 45 | int any_display; |
| 46 | int scroll; |
| 47 | int ac; |
| 48 | char **av; |
| 49 | int curr_ac; |
| 50 | int quitting; |
| 51 | |
| 52 | extern int file; |
| 53 | extern int cbufs; |
| 54 | extern int errmsgs; |
| 55 | |
| 56 | extern char *tagfile; |
| 57 | extern int tagoption; |
| 58 | |
| 59 | /* |
| 60 | * Edit a new file. |
| 61 | * Filename "-" means standard input. |
| 62 | * No filename means the "current" file, from the command line. |
| 63 | */ |
| 64 | edit(filename) |
| 65 | register char *filename; |
| 66 | { |
| 67 | extern int errno; |
| 68 | register int f; |
| 69 | register char *m; |
| 70 | off_t initial_pos, position(); |
| 71 | static int didpipe; |
| 72 | char message[100], *p; |
| 73 | char *rindex(), *strerror(), *save(), *bad_file(); |
| 74 | |
| 75 | initial_pos = NULL_POSITION; |
| 76 | if (filename == NULL || *filename == '\0') { |
| 77 | if (curr_ac >= ac) { |
| 78 | error("No current file"); |
| 79 | return(0); |
| 80 | } |
| 81 | filename = save(av[curr_ac]); |
| 82 | } |
| 83 | else if (strcmp(filename, "#") == 0) { |
| 84 | if (*previous_file == '\0') { |
| 85 | error("no previous file"); |
| 86 | return(0); |
| 87 | } |
| 88 | filename = save(previous_file); |
| 89 | initial_pos = prev_pos; |
| 90 | } else |
| 91 | filename = save(filename); |
| 92 | |
| 93 | /* use standard input. */ |
| 94 | if (!strcmp(filename, "-")) { |
| 95 | if (didpipe) { |
| 96 | error("Can view standard input only once"); |
| 97 | return(0); |
| 98 | } |
| 99 | f = 0; |
| 100 | } |
| 101 | else if ((m = bad_file(filename, message, sizeof(message))) != NULL) { |
| 102 | error(m); |
| 103 | free(filename); |
| 104 | return(0); |
| 105 | } |
| 106 | else if ((f = open(filename, O_RDONLY, 0)) < 0) { |
| 107 | (void)sprintf(message, "%s: %s", filename, strerror(errno)); |
| 108 | error(message); |
| 109 | free(filename); |
| 110 | return(0); |
| 111 | } |
| 112 | |
| 113 | if (isatty(f)) { |
| 114 | /* |
| 115 | * Not really necessary to call this an error, |
| 116 | * but if the control terminal (for commands) |
| 117 | * and the input file (for data) are the same, |
| 118 | * we get weird results at best. |
| 119 | */ |
| 120 | error("Can't take input from a terminal"); |
| 121 | if (f > 0) |
| 122 | (void)close(f); |
| 123 | (void)free(filename); |
| 124 | return(0); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * We are now committed to using the new file. |
| 129 | * Close the current input file and set up to use the new one. |
| 130 | */ |
| 131 | if (file > 0) |
| 132 | (void)close(file); |
| 133 | new_file = 1; |
| 134 | if (previous_file != NULL) |
| 135 | free(previous_file); |
| 136 | previous_file = current_file; |
| 137 | current_file = filename; |
| 138 | prev_pos = position(TOP); |
| 139 | ispipe = (f == 0); |
| 140 | if (ispipe) { |
| 141 | didpipe = 1; |
| 142 | current_name = "stdin"; |
| 143 | } else |
| 144 | current_name = (p = rindex(filename, '/')) ? p + 1 : filename; |
| 145 | file = f; |
| 146 | ch_init(cbufs, 0); |
| 147 | init_mark(); |
| 148 | |
| 149 | if (is_tty) { |
| 150 | int no_display = !any_display; |
| 151 | any_display = 1; |
| 152 | if (no_display && errmsgs > 0) { |
| 153 | /* |
| 154 | * We displayed some messages on error output |
| 155 | * (file descriptor 2; see error() function). |
| 156 | * Before erasing the screen contents, |
| 157 | * display the file name and wait for a keystroke. |
| 158 | */ |
| 159 | error(filename); |
| 160 | } |
| 161 | /* |
| 162 | * Indicate there is nothing displayed yet. |
| 163 | */ |
| 164 | pos_clear(); |
| 165 | if (initial_pos != NULL_POSITION) |
| 166 | jump_loc(initial_pos); |
| 167 | clr_linenum(); |
| 168 | } |
| 169 | return(1); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Edit the next file in the command line list. |
| 174 | */ |
| 175 | next_file(n) |
| 176 | int n; |
| 177 | { |
| 178 | extern int quit_at_eof; |
| 179 | off_t position(); |
| 180 | |
| 181 | if (curr_ac + n >= ac) { |
| 182 | if (quit_at_eof || position(TOP) == NULL_POSITION) |
| 183 | quit(); |
| 184 | error("No (N-th) next file"); |
| 185 | } |
| 186 | else |
| 187 | (void)edit(av[curr_ac += n]); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Edit the previous file in the command line list. |
| 192 | */ |
| 193 | prev_file(n) |
| 194 | int n; |
| 195 | { |
| 196 | if (curr_ac - n < 0) |
| 197 | error("No (N-th) previous file"); |
| 198 | else |
| 199 | (void)edit(av[curr_ac -= n]); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Copy a file directly to standard output. |
| 204 | * Used if standard output is not a tty. |
| 205 | */ |
| 206 | static |
| 207 | cat_file() |
| 208 | { |
| 209 | register int c; |
| 210 | |
| 211 | while ((c = ch_forw_get()) != EOI) |
| 212 | putchr(c); |
| 213 | flush(); |
| 214 | } |
| 215 | |
| 216 | main(argc, argv) |
| 217 | int argc; |
| 218 | char **argv; |
| 219 | { |
| 220 | int envargc, argcnt; |
| 221 | char *envargv[2], *getenv(); |
| 222 | |
| 223 | /* |
| 224 | * Process command line arguments and MORE environment arguments. |
| 225 | * Command line arguments override environment arguments. |
| 226 | */ |
| 227 | if (envargv[1] = getenv("MORE")) { |
| 228 | envargc = 2; |
| 229 | envargv[0] = "more"; |
| 230 | envargv[2] = NULL; |
| 231 | (void)option(envargc, envargv); |
| 232 | } |
| 233 | argcnt = option(argc, argv); |
| 234 | argv += argcnt; |
| 235 | argc -= argcnt; |
| 236 | |
| 237 | /* |
| 238 | * Set up list of files to be examined. |
| 239 | */ |
| 240 | ac = argc; |
| 241 | av = argv; |
| 242 | curr_ac = 0; |
| 243 | |
| 244 | /* |
| 245 | * Set up terminal, etc. |
| 246 | */ |
| 247 | is_tty = isatty(1); |
| 248 | if (!is_tty) { |
| 249 | /* |
| 250 | * Output is not a tty. |
| 251 | * Just copy the input file(s) to output. |
| 252 | */ |
| 253 | if (ac < 1) { |
| 254 | (void)edit("-"); |
| 255 | cat_file(); |
| 256 | } else { |
| 257 | do { |
| 258 | (void)edit((char *)NULL); |
| 259 | if (file >= 0) |
| 260 | cat_file(); |
| 261 | } while (++curr_ac < ac); |
| 262 | } |
| 263 | exit(0); |
| 264 | } |
| 265 | |
| 266 | raw_mode(1); |
| 267 | get_term(); |
| 268 | open_getchr(); |
| 269 | init(); |
| 270 | init_signals(1); |
| 271 | |
| 272 | /* select the first file to examine. */ |
| 273 | if (tagoption) { |
| 274 | /* |
| 275 | * A -t option was given; edit the file selected by the |
| 276 | * "tags" search, and search for the proper line in the file. |
| 277 | */ |
| 278 | if (!tagfile || !edit(tagfile) || tagsearch()) |
| 279 | quit(); |
| 280 | } |
| 281 | else if (ac < 1) |
| 282 | (void)edit("-"); /* Standard input */ |
| 283 | else { |
| 284 | /* |
| 285 | * Try all the files named as command arguments. |
| 286 | * We are simply looking for one which can be |
| 287 | * opened without error. |
| 288 | */ |
| 289 | do { |
| 290 | (void)edit((char *)NULL); |
| 291 | } while (file < 0 && ++curr_ac < ac); |
| 292 | } |
| 293 | |
| 294 | if (file >= 0) |
| 295 | commands(); |
| 296 | quit(); |
| 297 | /*NOTREACHED*/ |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Copy a string to a "safe" place |
| 302 | * (that is, to a buffer allocated by malloc). |
| 303 | */ |
| 304 | char * |
| 305 | save(s) |
| 306 | char *s; |
| 307 | { |
| 308 | char *p, *strcpy(), *malloc(); |
| 309 | |
| 310 | p = malloc((u_int)strlen(s)+1); |
| 311 | if (p == NULL) |
| 312 | { |
| 313 | error("cannot allocate memory"); |
| 314 | quit(); |
| 315 | } |
| 316 | return(strcpy(p, s)); |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Exit the program. |
| 321 | */ |
| 322 | quit() |
| 323 | { |
| 324 | /* |
| 325 | * Put cursor at bottom left corner, clear the line, |
| 326 | * reset the terminal modes, and exit. |
| 327 | */ |
| 328 | quitting = 1; |
| 329 | lower_left(); |
| 330 | clear_eol(); |
| 331 | deinit(); |
| 332 | flush(); |
| 333 | raw_mode(0); |
| 334 | exit(0); |
| 335 | } |