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