Annotation of src/usr.bin/less/main.c, Revision 1.20
1.1 etheisen 1: /*
1.13 shadchin 2: * Copyright (C) 1984-2012 Mark Nudelman
1.19 nicm 3: * Modified for use with illumos by Garrett D'Amore.
4: * Copyright 2014 Garrett D'Amore <garrett@damore.org>
1.1 etheisen 5: *
1.7 millert 6: * You may distribute under the terms of either the GNU General Public
7: * License or the Less License, as specified in the README file.
1.1 etheisen 8: *
1.13 shadchin 9: * For more information, see the README file.
1.17 nicm 10: */
1.1 etheisen 11:
12: /*
13: * Entry point, initialization, miscellaneous routines.
14: */
15:
1.17 nicm 16: #include <libgen.h>
17: #include <stdarg.h>
18: #include <sys/types.h>
1.1 etheisen 19: #include "less.h"
20:
1.17 nicm 21: char *every_first_cmd = NULL;
22: int new_file;
23: int is_tty;
24: IFILE curr_ifile = NULL_IFILE;
25: IFILE old_ifile = NULL_IFILE;
26: struct scrpos initial_scrpos;
27: int any_display = FALSE;
28: off_t start_attnpos = -1;
29: off_t end_attnpos = -1;
30: int wscroll;
31:
1.18 deraadt 32: static char *progname;
1.17 nicm 33:
34: int quitting;
35: int secure;
36: int dohelp;
37:
38: int logfile = -1;
39: int force_logfile = FALSE;
40: char *namelogfile = NULL;
41: char *editor;
42: char *editproto;
1.1 etheisen 43:
1.18 deraadt 44: extern char *tags;
45: extern char *tagoption;
1.17 nicm 46: extern int jump_sline;
1.11 shadchin 47: extern int less_is_more;
1.7 millert 48: extern int missing_cap;
49: extern int know_dumb;
1.17 nicm 50: extern int quit_if_one_screen;
51: extern int quit_at_eof;
1.11 shadchin 52: extern int pr_type;
1.17 nicm 53: extern int hilite_search;
54: extern int use_lessopen;
55: extern int no_init;
56: extern int top_scroll;
57: extern int errmsgs;
1.7 millert 58:
1.1 etheisen 59:
60: /*
61: * Entry point.
62: */
63: int
1.17 nicm 64: main(int argc, char *argv[])
1.1 etheisen 65: {
66: IFILE ifile;
1.7 millert 67: char *s;
1.1 etheisen 68:
1.17 nicm 69: progname = basename(argv[0]);
70: argv++;
71: argc--;
1.1 etheisen 72:
1.17 nicm 73: /*
74: * If the name of the executable program is "more",
75: * act like LESS_IS_MORE is set. We have to set this as early
76: * as possible for POSIX.
77: */
78: if ((strcmp(progname, "more") == 0) ||
79: (strcmp(progname, "page") == 0)) {
80: less_is_more = 1;
81: } else {
82: s = lgetenv("LESS_IS_MORE");
83: if (s != NULL && *s != '\0')
84: less_is_more = 1;
85: }
1.7 millert 86:
87: secure = 0;
88: s = lgetenv("LESSSECURE");
89: if (s != NULL && *s != '\0')
90: secure = 1;
91:
1.1 etheisen 92: /*
93: * Process command line arguments and LESS environment arguments.
94: * Command line arguments override environment arguments.
95: */
1.7 millert 96: is_tty = isatty(1);
1.1 etheisen 97: get_term();
98: init_cmds();
99: init_charset();
1.7 millert 100: init_line();
1.11 shadchin 101: init_cmdhist();
1.1 etheisen 102: init_option();
1.11 shadchin 103: init_search();
104:
105:
106: init_prompt();
107:
108: s = lgetenv(less_is_more ? "MORE" : "LESS");
1.7 millert 109: if (s != NULL)
1.20 ! tedu 110: scan_option(estrdup(s));
1.1 etheisen 111:
1.17 nicm 112: if (less_is_more) {
113: /* this is specified by XPG */
114: quit_at_eof = OPT_ON;
115:
116: /* more users don't like the warning */
117: know_dumb = OPT_ON;
118:
119: /* default prompt is medium */
120: pr_type = OPT_ON;
121:
122: /* do not hilight search terms */
123: hilite_search = OPT_OFF;
124:
125: /* do not use LESSOPEN */
126: use_lessopen = OPT_OFF;
127:
128: /* do not set init strings to terminal */
129: no_init = OPT_ON;
130:
131: /* repaint from top of screen */
132: top_scroll = OPT_OFF;
133: }
134:
1.7 millert 135: #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
1.17 nicm 136: while (argc > 0 && (isoptstring(*argv) || isoptpending())) {
1.7 millert 137: s = *argv++;
138: argc--;
139: if (strcmp(s, "--") == 0)
1.3 deraadt 140: break;
1.7 millert 141: scan_option(s);
1.3 deraadt 142: }
1.1 etheisen 143: #undef isoptstring
144:
1.17 nicm 145: if (isoptpending()) {
1.1 etheisen 146: /*
147: * Last command line option was a flag requiring a
148: * following string, but there was no following string.
149: */
150: nopendopt();
151: quit(QUIT_OK);
152: }
1.11 shadchin 153:
1.17 nicm 154: if (errmsgs) {
155: quit(QUIT_ERROR);
156: }
157: if (less_is_more && quit_at_eof == OPT_ONPLUS) {
158: extern int no_init;
159: no_init = OPT_ON;
160: }
161: if (less_is_more && pr_type == OPT_ONPLUS) {
162: extern int quiet;
163: quiet = VERY_QUIET;
164: }
165:
1.7 millert 166: editor = lgetenv("VISUAL");
1.17 nicm 167: if (editor == NULL || *editor == '\0') {
1.7 millert 168: editor = lgetenv("EDITOR");
1.1 etheisen 169: if (editor == NULL || *editor == '\0')
170: editor = EDIT_PGM;
171: }
1.7 millert 172: editproto = lgetenv("LESSEDIT");
1.1 etheisen 173: if (editproto == NULL || *editproto == '\0')
174: editproto = "%E ?lm+%lm. %f";
175:
176: /*
177: * Call get_ifile with all the command line filenames
178: * to "register" them with the ifile system.
179: */
180: ifile = NULL_IFILE;
1.11 shadchin 181: if (dohelp)
1.15 schwarze 182: ifile = get_ifile(helpfile(), ifile);
1.17 nicm 183: while (argc-- > 0) {
1.7 millert 184: char *filename;
185: filename = shell_quote(*argv);
186: if (filename == NULL)
187: filename = *argv;
188: argv++;
189: (void) get_ifile(filename, ifile);
190: ifile = prev_ifile(NULL_IFILE);
1.13 shadchin 191: free(filename);
1.1 etheisen 192: }
193: /*
194: * Set up terminal, etc.
195: */
1.17 nicm 196: if (!is_tty) {
1.1 etheisen 197: /*
198: * Output is not a tty.
199: * Just copy the input file(s) to output.
200: */
1.17 nicm 201: if (nifile() == 0) {
1.1 etheisen 202: if (edit_stdin() == 0)
203: cat_file();
1.17 nicm 204: } else if (edit_first() == 0) {
1.1 etheisen 205: do {
206: cat_file();
207: } while (edit_next(1) == 0);
208: }
209: quit(QUIT_OK);
210: }
211:
1.17 nicm 212: if (missing_cap && !know_dumb)
1.7 millert 213: error("WARNING: terminal is not fully functional", NULL_PARG);
1.1 etheisen 214: init_mark();
1.7 millert 215: open_getchr();
1.1 etheisen 216: raw_mode(1);
217: init_signals(1);
218:
219: /*
220: * Select the first file to examine.
221: */
1.17 nicm 222: if (tagoption != NULL || strcmp(tags, "-") == 0) {
1.1 etheisen 223: /*
224: * A -t option was given.
225: * Verify that no filenames were also given.
226: * Edit the file selected by the "tags" search,
227: * and search for the proper line in the file.
228: */
1.17 nicm 229: if (nifile() > 0) {
1.1 etheisen 230: error("No filenames allowed with -t option", NULL_PARG);
231: quit(QUIT_ERROR);
232: }
233: findtag(tagoption);
1.7 millert 234: if (edit_tagfile()) /* Edit file which contains the tag */
1.1 etheisen 235: quit(QUIT_ERROR);
236: /*
237: * Search for the line which contains the tag.
238: * Set up initial_scrpos so we display that line.
239: */
240: initial_scrpos.pos = tagsearch();
1.17 nicm 241: if (initial_scrpos.pos == -1)
1.1 etheisen 242: quit(QUIT_ERROR);
243: initial_scrpos.ln = jump_sline;
1.17 nicm 244: } else if (nifile() == 0) {
1.1 etheisen 245: if (edit_stdin()) /* Edit standard input */
246: quit(QUIT_ERROR);
1.17 nicm 247: } else {
1.1 etheisen 248: if (edit_first()) /* Edit first valid file in cmd line */
249: quit(QUIT_ERROR);
250: }
251:
252: init();
253: commands();
254: quit(QUIT_OK);
255: /*NOTREACHED*/
1.7 millert 256: return (0);
1.1 etheisen 257: }
258:
259: /*
260: * Allocate memory.
261: * Like calloc(), but never returns an error (NULL).
262: */
1.17 nicm 263: void *
264: ecalloc(int count, unsigned int size)
1.1 etheisen 265: {
1.17 nicm 266: void *p;
1.1 etheisen 267:
1.17 nicm 268: p = calloc(count, size);
1.1 etheisen 269: if (p != NULL)
270: return (p);
271: error("Cannot allocate memory", NULL_PARG);
272: quit(QUIT_ERROR);
273: /*NOTREACHED*/
1.7 millert 274: return (NULL);
1.1 etheisen 275: }
276:
1.17 nicm 277: char *
278: easprintf(const char *fmt, ...)
279: {
280: char *p = NULL;
281: int rv;
282: va_list ap;
283:
284: va_start(ap, fmt);
285: rv = vasprintf(&p, fmt, ap);
286: va_end(ap);
287:
288: if (p == NULL || rv < 0) {
289: error("Cannot allocate memory", NULL_PARG);
290: quit(QUIT_ERROR);
291: /*NOTREACHED*/
292: }
293: return (p);
294: }
295:
296: char *
297: estrdup(const char *str)
298: {
1.20 ! tedu 299: char *n;
! 300:
! 301: n = strdup(str);
! 302: if (n == NULL) {
1.17 nicm 303: error("Cannot allocate memory", NULL_PARG);
1.20 ! tedu 304: quit(QUIT_ERROR);
1.17 nicm 305: }
306: return (n);
307: }
308:
1.1 etheisen 309: /*
310: * Skip leading spaces in a string.
311: */
1.17 nicm 312: char *
313: skipsp(char *s)
1.1 etheisen 314: {
1.17 nicm 315: while (*s == ' ' || *s == '\t')
1.1 etheisen 316: s++;
317: return (s);
318: }
319:
320: /*
1.7 millert 321: * See how many characters of two strings are identical.
322: * If uppercase is true, the first string must begin with an uppercase
323: * character; the remainder of the first string may be either case.
324: */
1.17 nicm 325: int
326: sprefix(char *ps, char *s, int uppercase)
1.7 millert 327: {
1.17 nicm 328: int c;
329: int sc;
330: int len = 0;
1.7 millert 331:
1.17 nicm 332: for (; *s != '\0'; s++, ps++) {
1.7 millert 333: c = *ps;
1.17 nicm 334: if (uppercase) {
335: if (len == 0 && isupper(c))
1.7 millert 336: return (-1);
1.17 nicm 337: if (isupper(c))
338: c = tolower(c);
1.7 millert 339: }
340: sc = *s;
1.17 nicm 341: if (len > 0 && isupper(sc))
342: sc = tolower(sc);
1.7 millert 343: if (c != sc)
344: break;
345: len++;
346: }
347: return (len);
348: }
349:
350: /*
1.1 etheisen 351: * Exit the program.
352: */
1.17 nicm 353: void
354: quit(int status)
1.1 etheisen 355: {
356: static int save_status;
357:
358: /*
359: * Put cursor at bottom left corner, clear the line,
360: * reset the terminal modes, and exit.
361: */
362: if (status < 0)
363: status = save_status;
364: else
365: save_status = status;
366: quitting = 1;
1.17 nicm 367: edit(NULL);
1.11 shadchin 368: save_cmdhist();
1.7 millert 369: if (any_display && is_tty)
1.1 etheisen 370: clear_bot();
371: deinit();
372: flush();
373: raw_mode(0);
1.7 millert 374: close_getchr();
1.1 etheisen 375: exit(status);
1.15 schwarze 376: }
377:
1.17 nicm 378: char *
1.15 schwarze 379: helpfile(void)
380: {
381: return (less_is_more ? HELPDIR "/more.help" : HELPDIR "/less.help");
1.1 etheisen 382: }