Annotation of src/usr.bin/less/main.c, Revision 1.12
1.1 etheisen 1: /*
1.11 shadchin 2: * Copyright (C) 1984-2011 Mark Nudelman
1.1 etheisen 3: *
1.7 millert 4: * You may distribute under the terms of either the GNU General Public
5: * License or the Less License, as specified in the README file.
1.1 etheisen 6: *
1.7 millert 7: * For more information about less, or for information on how to
8: * contact the author, see the README file.
1.1 etheisen 9: */
10:
11:
12: /*
13: * Entry point, initialization, miscellaneous routines.
14: */
15:
16: #include "less.h"
1.7 millert 17: #if MSDOS_COMPILER==WIN32C
18: #include <windows.h>
19: #endif
1.1 etheisen 20:
21: public char * every_first_cmd = NULL;
22: public int new_file;
23: public int is_tty;
24: public IFILE curr_ifile = NULL_IFILE;
25: public IFILE old_ifile = NULL_IFILE;
26: public struct scrpos initial_scrpos;
27: public int any_display = FALSE;
1.7 millert 28: public POSITION start_attnpos = NULL_POSITION;
29: public POSITION end_attnpos = NULL_POSITION;
1.1 etheisen 30: public int wscroll;
31: public char * progname;
32: public int quitting;
1.7 millert 33: public int secure;
1.11 shadchin 34: public int dohelp;
1.1 etheisen 35:
36: #if LOGFILE
37: public int logfile = -1;
38: public int force_logfile = FALSE;
39: public char * namelogfile = NULL;
40: #endif
41:
42: #if EDITOR
43: public char * editor;
44: public char * editproto;
45: #endif
46:
47: #if TAGS
1.7 millert 48: extern char * tags;
1.1 etheisen 49: extern char * tagoption;
50: extern int jump_sline;
51: #endif
52:
1.7 millert 53: #ifdef WIN32
54: static char consoleTitle[256];
55: #endif
1.1 etheisen 56:
1.11 shadchin 57: extern int less_is_more;
1.7 millert 58: extern int missing_cap;
59: extern int know_dumb;
1.11 shadchin 60: extern int quit_if_one_screen;
61: extern int pr_type;
1.7 millert 62:
1.1 etheisen 63:
64: /*
65: * Entry point.
66: */
67: int
68: main(argc, argv)
69: int argc;
70: char *argv[];
71: {
72: IFILE ifile;
1.7 millert 73: char *s;
1.1 etheisen 74:
75: #ifdef __EMX__
76: _response(&argc, &argv);
77: _wildcard(&argc, &argv);
78: #endif
79:
80: progname = *argv++;
1.7 millert 81: argc--;
82:
83: secure = 0;
84: s = lgetenv("LESSSECURE");
85: if (s != NULL && *s != '\0')
86: secure = 1;
87:
88: #ifdef WIN32
89: if (getenv("HOME") == NULL)
90: {
91: /*
92: * If there is no HOME environment variable,
93: * try the concatenation of HOMEDRIVE + HOMEPATH.
94: */
95: char *drive = getenv("HOMEDRIVE");
96: char *path = getenv("HOMEPATH");
97: if (drive != NULL && path != NULL)
98: {
99: size_t len = strlen(drive) + strlen(path) + 6;
100: char *env = (char *) ecalloc(len, sizeof(char));
101: strlcpy(env, "HOME=", len);
102: strlcat(env, drive, len);
103: strlcat(env, path, len);
104: putenv(env);
105: }
106: }
107: GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
108: #endif /* WIN32 */
1.1 etheisen 109:
110: /*
111: * Process command line arguments and LESS environment arguments.
112: * Command line arguments override environment arguments.
113: */
1.7 millert 114: is_tty = isatty(1);
1.1 etheisen 115: get_term();
116: init_cmds();
117: init_charset();
1.7 millert 118: init_line();
1.11 shadchin 119: init_cmdhist();
1.1 etheisen 120: init_option();
1.11 shadchin 121: init_search();
122:
123: /*
124: * If the name of the executable program is "more",
125: * act like LESS_IS_MORE is set.
126: */
127: for (s = progname + strlen(progname); s > progname; s--)
128: {
129: if (s[-1] == PATHNAME_SEP[0])
130: break;
131: }
132: if (strcmp(s, "more") == 0)
133: less_is_more = 1;
134:
135: init_prompt();
136:
137: if (less_is_more) {
1.2 etheisen 138: scan_option("-G");
1.7 millert 139: scan_option("-L");
1.11 shadchin 140: scan_option("-X");
1.12 ! nicm 141: scan_option("-c");
1.11 shadchin 142: }
143:
144: s = lgetenv(less_is_more ? "MORE" : "LESS");
1.7 millert 145: if (s != NULL)
146: scan_option(save(s));
1.1 etheisen 147:
1.7 millert 148: #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
149: while (argc > 0 && (isoptstring(*argv) || isoptpending()))
1.1 etheisen 150: {
1.7 millert 151: s = *argv++;
152: argc--;
153: if (strcmp(s, "--") == 0)
1.3 deraadt 154: break;
1.7 millert 155: scan_option(s);
1.3 deraadt 156: }
1.1 etheisen 157: #undef isoptstring
158:
159: if (isoptpending())
160: {
161: /*
162: * Last command line option was a flag requiring a
163: * following string, but there was no following string.
164: */
165: nopendopt();
166: quit(QUIT_OK);
167: }
168:
1.11 shadchin 169: if (less_is_more && get_quit_at_eof())
170: quit_if_one_screen = TRUE;
171:
1.1 etheisen 172: #if EDITOR
1.7 millert 173: editor = lgetenv("VISUAL");
1.1 etheisen 174: if (editor == NULL || *editor == '\0')
175: {
1.7 millert 176: editor = lgetenv("EDITOR");
1.1 etheisen 177: if (editor == NULL || *editor == '\0')
178: editor = EDIT_PGM;
179: }
1.7 millert 180: editproto = lgetenv("LESSEDIT");
1.1 etheisen 181: if (editproto == NULL || *editproto == '\0')
182: editproto = "%E ?lm+%lm. %f";
183: #endif
184:
185: /*
186: * Call get_ifile with all the command line filenames
187: * to "register" them with the ifile system.
188: */
189: ifile = NULL_IFILE;
1.11 shadchin 190: #if !SMALL
191: if (dohelp)
192: ifile = get_ifile(HELPFILE, ifile);
193: #endif /* !SMALL */
1.7 millert 194: while (argc-- > 0)
1.1 etheisen 195: {
1.7 millert 196: char *filename;
197: #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
1.1 etheisen 198: /*
199: * Because the "shell" doesn't expand filename patterns,
200: * treat each argument as a filename pattern rather than
201: * a single filename.
202: * Expand the pattern and iterate over the expanded list.
203: */
204: struct textlist tlist;
205: char *gfilename;
206:
1.7 millert 207: gfilename = lglob(*argv++);
1.1 etheisen 208: init_textlist(&tlist, gfilename);
209: filename = NULL;
210: while ((filename = forw_textlist(&tlist, filename)) != NULL)
1.7 millert 211: {
212: (void) get_ifile(filename, ifile);
213: ifile = prev_ifile(NULL_IFILE);
214: }
1.1 etheisen 215: free(gfilename);
216: #else
1.7 millert 217: filename = shell_quote(*argv);
218: if (filename == NULL)
219: filename = *argv;
220: argv++;
221: (void) get_ifile(filename, ifile);
222: ifile = prev_ifile(NULL_IFILE);
1.1 etheisen 223: #endif
224: }
225: /*
226: * Set up terminal, etc.
227: */
228: if (!is_tty)
229: {
230: /*
231: * Output is not a tty.
232: * Just copy the input file(s) to output.
233: */
1.7 millert 234: SET_BINARY(1);
1.1 etheisen 235: if (nifile() == 0)
236: {
237: if (edit_stdin() == 0)
238: cat_file();
239: } else if (edit_first() == 0)
240: {
241: do {
242: cat_file();
243: } while (edit_next(1) == 0);
244: }
245: quit(QUIT_OK);
246: }
247:
1.11 shadchin 248: if (missing_cap && !know_dumb && !less_is_more)
1.7 millert 249: error("WARNING: terminal is not fully functional", NULL_PARG);
1.1 etheisen 250: init_mark();
1.7 millert 251: open_getchr();
1.1 etheisen 252: raw_mode(1);
253: init_signals(1);
254:
255: /*
256: * Select the first file to examine.
257: */
258: #if TAGS
1.7 millert 259: if (tagoption != NULL || strcmp(tags, "-") == 0)
1.1 etheisen 260: {
261: /*
262: * A -t option was given.
263: * Verify that no filenames were also given.
264: * Edit the file selected by the "tags" search,
265: * and search for the proper line in the file.
266: */
267: if (nifile() > 0)
268: {
269: error("No filenames allowed with -t option", NULL_PARG);
270: quit(QUIT_ERROR);
271: }
272: findtag(tagoption);
1.7 millert 273: if (edit_tagfile()) /* Edit file which contains the tag */
1.1 etheisen 274: quit(QUIT_ERROR);
275: /*
276: * Search for the line which contains the tag.
277: * Set up initial_scrpos so we display that line.
278: */
279: initial_scrpos.pos = tagsearch();
280: if (initial_scrpos.pos == NULL_POSITION)
281: quit(QUIT_ERROR);
282: initial_scrpos.ln = jump_sline;
283: } else
284: #endif
285: if (nifile() == 0)
286: {
287: if (edit_stdin()) /* Edit standard input */
288: quit(QUIT_ERROR);
289: } else
290: {
291: if (edit_first()) /* Edit first valid file in cmd line */
292: quit(QUIT_ERROR);
293: }
294:
295: init();
296: commands();
297: quit(QUIT_OK);
298: /*NOTREACHED*/
1.7 millert 299: return (0);
1.1 etheisen 300: }
301:
302: /*
303: * Copy a string to a "safe" place
304: * (that is, to a buffer allocated by calloc).
305: */
306: public char *
307: save(s)
308: char *s;
309: {
1.7 millert 310: register char *p;
1.11 shadchin 311: size_t len = strlen(s) + 1;
1.1 etheisen 312:
1.6 deraadt 313: p = (char *) ecalloc(len, sizeof(char));
314: strlcpy(p, s, len);
1.1 etheisen 315: return (p);
316: }
317:
318: /*
319: * Allocate memory.
320: * Like calloc(), but never returns an error (NULL).
321: */
322: public VOID_POINTER
323: ecalloc(count, size)
324: int count;
325: unsigned int size;
326: {
1.7 millert 327: register VOID_POINTER p;
1.1 etheisen 328:
329: p = (VOID_POINTER) calloc(count, size);
330: if (p != NULL)
331: return (p);
332: error("Cannot allocate memory", NULL_PARG);
333: quit(QUIT_ERROR);
334: /*NOTREACHED*/
1.7 millert 335: return (NULL);
1.1 etheisen 336: }
337:
338: /*
339: * Skip leading spaces in a string.
340: */
341: public char *
342: skipsp(s)
1.7 millert 343: register char *s;
1.1 etheisen 344: {
345: while (*s == ' ' || *s == '\t')
346: s++;
347: return (s);
348: }
349:
1.11 shadchin 350: #if GNU_OPTIONS
1.1 etheisen 351: /*
1.7 millert 352: * See how many characters of two strings are identical.
353: * If uppercase is true, the first string must begin with an uppercase
354: * character; the remainder of the first string may be either case.
355: */
356: public int
357: sprefix(ps, s, uppercase)
358: char *ps;
359: char *s;
360: int uppercase;
361: {
362: register int c;
363: register int sc;
364: register int len = 0;
365:
366: for ( ; *s != '\0'; s++, ps++)
367: {
368: c = *ps;
369: if (uppercase)
370: {
1.11 shadchin 371: if (len == 0 && ASCII_IS_LOWER(c))
1.7 millert 372: return (-1);
1.11 shadchin 373: if (ASCII_IS_UPPER(c))
374: c = ASCII_TO_LOWER(c);
1.7 millert 375: }
376: sc = *s;
1.11 shadchin 377: if (len > 0 && ASCII_IS_UPPER(sc))
378: sc = ASCII_TO_LOWER(sc);
1.7 millert 379: if (c != sc)
380: break;
381: len++;
382: }
383: return (len);
384: }
1.11 shadchin 385: #endif /* GNU_OPTIONS */
1.7 millert 386:
387: /*
1.1 etheisen 388: * Exit the program.
389: */
390: public void
391: quit(status)
392: int status;
393: {
394: static int save_status;
395:
396: /*
397: * Put cursor at bottom left corner, clear the line,
398: * reset the terminal modes, and exit.
399: */
400: if (status < 0)
401: status = save_status;
402: else
403: save_status = status;
404: quitting = 1;
405: edit((char*)NULL);
1.11 shadchin 406: save_cmdhist();
1.7 millert 407: if (any_display && is_tty)
1.1 etheisen 408: clear_bot();
409: deinit();
410: flush();
411: raw_mode(0);
1.7 millert 412: #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
1.1 etheisen 413: /*
414: * If we don't close 2, we get some garbage from
415: * 2's buffer when it flushes automatically.
416: * I cannot track this one down RB
417: * The same bug shows up if we use ^C^C to abort.
418: */
419: close(2);
420: #endif
1.11 shadchin 421: #ifdef WIN32
1.7 millert 422: SetConsoleTitle(consoleTitle);
423: #endif
424: close_getchr();
1.1 etheisen 425: exit(status);
426: }