|
|
1.1 ! root 1: /* ! 2: * Entry point, initialization, miscellaneous routines. ! 3: */ ! 4: ! 5: #include "less.h" ! 6: #include "position.h" ! 7: #include <setjmp.h> ! 8: ! 9: public int ispipe; ! 10: public jmp_buf main_loop; ! 11: public char * first_cmd; ! 12: public char * every_first_cmd; ! 13: public int new_file; ! 14: public int is_tty; ! 15: public char current_file[128]; ! 16: public int ac; ! 17: public char **av; ! 18: public int curr_ac; ! 19: #if EDITOR ! 20: public char * editor; ! 21: #endif ! 22: ! 23: extern int file; ! 24: extern int nbufs; ! 25: extern int sigs; ! 26: extern int quit_at_eof; ! 27: extern int p_nbufs, f_nbufs; ! 28: extern int back_scroll; ! 29: extern int top_scroll; ! 30: extern int sc_height; ! 31: ! 32: ! 33: /* ! 34: * Edit a new file. ! 35: * Filename "-" means standard input. ! 36: * No filename means the "current" file, from the command line. ! 37: */ ! 38: public void ! 39: edit(filename) ! 40: char *filename; ! 41: { ! 42: register int f; ! 43: char message[100]; ! 44: static int any_edited = 0; ! 45: static int hold_scroll = 0; ! 46: ! 47: if (filename == NULL || *filename == '\0') ! 48: { ! 49: if (curr_ac >= ac) ! 50: { ! 51: error("No current file"); ! 52: return; ! 53: } ! 54: filename = av[curr_ac]; ! 55: } ! 56: if (strcmp(filename, "-") == 0) ! 57: f = 0; /* Standard input */ ! 58: else if ((f = open(filename, 0)) < 0) ! 59: { ! 60: sprintf(message, "Cannot open %.*s", ! 61: error_width()-13, filename); ! 62: if (any_edited) ! 63: error(message); ! 64: else ! 65: { ! 66: puts(message); ! 67: hold_scroll = 1; ! 68: } ! 69: return; ! 70: } ! 71: ! 72: if (isatty(f)) ! 73: { ! 74: /* ! 75: * Not really necessary to call this an error, ! 76: * but if the control terminal (for commands) ! 77: * and the input file (for data) are the same, ! 78: * we get weird results at best. ! 79: */ ! 80: error("Can't take input from a terminal"); ! 81: if (f > 0) ! 82: close(f); ! 83: return; ! 84: } ! 85: ! 86: /* ! 87: * Close the current input file and set up to use the new one. ! 88: */ ! 89: if (file > 0) ! 90: close(file); ! 91: new_file = 1; ! 92: strcpy(current_file, filename); ! 93: ispipe = (f == 0); ! 94: file = f; ! 95: ch_init( (ispipe) ? p_nbufs : f_nbufs ); ! 96: init_mark(); ! 97: if (every_first_cmd != NULL) ! 98: first_cmd = every_first_cmd; ! 99: if (is_tty) ! 100: { ! 101: any_edited = 1; ! 102: if (hold_scroll) ! 103: { ! 104: /* ! 105: * Before erasing the screen contents, ! 106: * display the file name and ask for a keystroke. ! 107: */ ! 108: error(filename); ! 109: hold_scroll = 0; ! 110: } ! 111: if (first_cmd == NULL || *first_cmd == '\0') ! 112: { ! 113: /* ! 114: * Display the first screen. ! 115: */ ! 116: jump_back(1); ! 117: } else ! 118: { ! 119: /* ! 120: * The first_cmd will hopefully redisplay the ! 121: * screen, so we need not display anything yet. ! 122: * Indicate there is nothing yet on the screen. ! 123: */ ! 124: pos_clear(); ! 125: } ! 126: } ! 127: } ! 128: ! 129: /* ! 130: * Edit the next file in the command line list. ! 131: */ ! 132: public void ! 133: next_file(n) ! 134: int n; ! 135: { ! 136: if (curr_ac + n >= ac) ! 137: { ! 138: if (quit_at_eof) ! 139: quit(); ! 140: error("No (N-th) next file"); ! 141: } else ! 142: edit(av[curr_ac += n]); ! 143: } ! 144: ! 145: /* ! 146: * Edit the previous file in the command line list. ! 147: */ ! 148: public void ! 149: prev_file(n) ! 150: int n; ! 151: { ! 152: if (curr_ac - n < 0) ! 153: error("No (N-th) previous file"); ! 154: else ! 155: edit(av[curr_ac -= n]); ! 156: } ! 157: ! 158: /* ! 159: * Copy a file directly to standard output. ! 160: * Used if standard output is not a tty. ! 161: */ ! 162: static void ! 163: cat_file() ! 164: { ! 165: register int c; ! 166: ! 167: while ((c = ch_forw_get()) != EOF) ! 168: putc(c); ! 169: flush(); ! 170: } ! 171: ! 172: /* ! 173: * Entry point. ! 174: */ ! 175: main(argc, argv) ! 176: int argc; ! 177: char *argv[]; ! 178: { ! 179: char *getenv(); ! 180: ! 181: ! 182: /* ! 183: * Process command line arguments and LESS environment arguments. ! 184: * Command line arguments override environment arguments. ! 185: */ ! 186: init_option(); ! 187: scan_option(getenv("LESS")); ! 188: argv++; ! 189: while ( (--argc > 0) && ! 190: (argv[0][0] == '-' || argv[0][0] == '+') && ! 191: argv[0][1] != '\0') ! 192: scan_option(*argv++); ! 193: ! 194: #if EDITOR ! 195: editor = getenv("EDITOR"); ! 196: if (editor == NULL || *editor == '\0') ! 197: editor = EDIT_PGM; ! 198: #endif ! 199: ! 200: /* ! 201: * Set up list of files to be examined. ! 202: */ ! 203: ac = argc; ! 204: av = argv; ! 205: curr_ac = 0; ! 206: ! 207: /* ! 208: * Set up terminal, etc. ! 209: */ ! 210: is_tty = isatty(1); ! 211: if (!is_tty) ! 212: { ! 213: /* ! 214: * Output is not a tty. ! 215: * Just copy the input file(s) to output. ! 216: */ ! 217: if (ac < 1) ! 218: { ! 219: edit("-"); ! 220: cat_file(); ! 221: } else ! 222: { ! 223: do ! 224: { ! 225: edit((char *)NULL); ! 226: if (file >= 0) ! 227: cat_file(); ! 228: } while (++curr_ac < ac); ! 229: } ! 230: exit(0); ! 231: } ! 232: ! 233: raw_mode(1); ! 234: get_term(); ! 235: open_getc(); ! 236: init(); ! 237: ! 238: if (back_scroll < 0) ! 239: { ! 240: /* {{ KLUDGE }} */ ! 241: back_scroll = sc_height-1; ! 242: if (top_scroll) ! 243: back_scroll--; ! 244: } ! 245: ! 246: if (setjmp(main_loop)) ! 247: quit(); ! 248: init_signals(); ! 249: ! 250: /* ! 251: * Select the first file to examine. ! 252: */ ! 253: if (ac < 1) ! 254: edit("-"); /* Standard input */ ! 255: else ! 256: { ! 257: /* ! 258: * Try all the files named as command arguments. ! 259: * We are simply looking for one which can be ! 260: * opened without error. ! 261: */ ! 262: do ! 263: { ! 264: edit((char *)NULL); ! 265: if (file >= 0) ! 266: /* We can open this file. */ ! 267: break; ! 268: putc('\n'); flush(); ! 269: } while (++curr_ac < ac); ! 270: } ! 271: ! 272: if (file >= 0) ! 273: commands(); ! 274: quit(); ! 275: } ! 276: ! 277: /* ! 278: * Exit the program. ! 279: */ ! 280: public void ! 281: quit() ! 282: { ! 283: /* ! 284: * Put cursor at bottom left corner, clear the line, ! 285: * reset the terminal modes, and exit. ! 286: */ ! 287: lower_left(); ! 288: clear_eol(); ! 289: deinit(); ! 290: flush(); ! 291: raw_mode(0); ! 292: exit(0); ! 293: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.