]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/less/main.c
zfs: merge openzfs/zfs@c4c162c1e (master) into main
[FreeBSD/FreeBSD.git] / contrib / less / main.c
1 /* $FreeBSD$ */
2 /*
3  * Copyright (C) 1984-2021  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 #define WIN32_LEAN_AND_MEAN
19 #include <windows.h>
20 #endif
21
22 public char *   every_first_cmd = NULL;
23 public int      new_file;
24 public int      is_tty;
25 public IFILE    curr_ifile = NULL_IFILE;
26 public IFILE    old_ifile = NULL_IFILE;
27 public struct scrpos initial_scrpos;
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      one_screen;
58 extern int      less_is_more;
59 extern int      missing_cap;
60 extern int      know_dumb;
61 extern int      pr_type;
62 extern int      quit_if_one_screen;
63 extern int      no_init;
64 extern int errmsgs;
65
66
67 /*
68  * Entry point.
69  */
70 int
71 main(argc, argv)
72         int argc;
73         char *argv[];
74 {
75         IFILE ifile;
76         char *s;
77
78 #ifdef __EMX__
79         _response(&argc, &argv);
80         _wildcard(&argc, &argv);
81 #endif
82
83         progname = *argv++;
84         argc--;
85
86 #if SECURE
87         secure = 1;
88 #else
89         secure = 0;
90         s = lgetenv("LESSSECURE");
91         if (!isnullenv(s))
92                 secure = 1;
93 #endif
94
95 #ifdef WIN32
96         if (getenv("HOME") == NULL)
97         {
98                 /*
99                  * If there is no HOME environment variable,
100                  * try the concatenation of HOMEDRIVE + HOMEPATH.
101                  */
102                 char *drive = getenv("HOMEDRIVE");
103                 char *path  = getenv("HOMEPATH");
104                 if (drive != NULL && path != NULL)
105                 {
106                         char *env = (char *) ecalloc(strlen(drive) + 
107                                         strlen(path) + 6, sizeof(char));
108                         strcpy(env, "HOME=");
109                         strcat(env, drive);
110                         strcat(env, path);
111                         putenv(env);
112                 }
113         }
114         GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
115 #endif /* WIN32 */
116
117         /*
118          * Process command line arguments and LESS environment arguments.
119          * Command line arguments override environment arguments.
120          */
121         is_tty = isatty(1);
122         init_mark();
123         init_cmds();
124         get_term();
125         init_charset();
126         init_line();
127         init_cmdhist();
128         init_option();
129         init_search();
130
131         /*
132          * If the name of the executable program is "more",
133          * act like LESS_IS_MORE is set.
134          */
135         s = last_component(progname);
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         expand_cmd_tables();
174
175 #if EDITOR
176         editor = lgetenv("VISUAL");
177         if (editor == NULL || *editor == '\0')
178         {
179                 editor = lgetenv("EDITOR");
180                 if (isnullenv(editor))
181                         editor = EDIT_PGM;
182         }
183         editproto = lgetenv("LESSEDIT");
184         if (isnullenv(editproto))
185                 editproto = "%E ?lm+%lm. %g";
186 #endif
187
188         /*
189          * Call get_ifile with all the command line filenames
190          * to "register" them with the ifile system.
191          */
192         ifile = NULL_IFILE;
193         if (dohelp)
194                 ifile = get_ifile(FAKE_HELPFILE, ifile);
195         while (argc-- > 0)
196         {
197 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
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 *filename;
206                 char *gfilename;
207                 char *qfilename;
208                 
209                 gfilename = lglob(*argv++);
210                 init_textlist(&tlist, gfilename);
211                 filename = NULL;
212                 while ((filename = forw_textlist(&tlist, filename)) != NULL)
213                 {
214                         qfilename = shell_unquote(filename);
215                         (void) get_ifile(qfilename, ifile);
216                         free(qfilename);
217                         ifile = prev_ifile(NULL_IFILE);
218                 }
219                 free(gfilename);
220 #else
221                 (void) get_ifile(*argv++, ifile);
222                 ifile = prev_ifile(NULL_IFILE);
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                  */
234                 set_output(1); /* write to stdout */
235                 SET_BINARY(1);
236                 if (edit_first() == 0)
237                 {
238                         do {
239                                 cat_file();
240                         } while (edit_next(1) == 0);
241                 }
242                 quit(QUIT_OK);
243         }
244
245         if (missing_cap && !know_dumb && !less_is_more)
246                 error("WARNING: terminal is not fully functional", NULL_PARG);
247         open_getchr();
248         raw_mode(1);
249         init_signals(1);
250
251         /*
252          * Select the first file to examine.
253          */
254 #if TAGS
255         if (tagoption != NULL || strcmp(tags, "-") == 0)
256         {
257                 /*
258                  * A -t option was given.
259                  * Verify that no filenames were also given.
260                  * Edit the file selected by the "tags" search,
261                  * and search for the proper line in the file.
262                  */
263                 if (nifile() > 0)
264                 {
265                         error("No filenames allowed with -t option", NULL_PARG);
266                         quit(QUIT_ERROR);
267                 }
268                 findtag(tagoption);
269                 if (edit_tagfile())  /* Edit file which contains the tag */
270                         quit(QUIT_ERROR);
271                 /*
272                  * Search for the line which contains the tag.
273                  * Set up initial_scrpos so we display that line.
274                  */
275                 initial_scrpos.pos = tagsearch();
276                 if (initial_scrpos.pos == NULL_POSITION)
277                         quit(QUIT_ERROR);
278                 initial_scrpos.ln = jump_sline;
279         } else
280 #endif
281         {
282                 if (edit_first())
283                         quit(QUIT_ERROR);
284                 /*
285                  * See if file fits on one screen to decide whether 
286                  * to send terminal init. But don't need this 
287                  * if -X (no_init) overrides this (see init()).
288                  */
289                 if (quit_if_one_screen)
290                 {
291                         if (nifile() > 1) /* If more than one file, -F cannot be used */
292                                 quit_if_one_screen = FALSE;
293                         else if (!no_init)
294                                 one_screen = get_one_screen();
295                 }
296         }
297
298         if (errmsgs > 0)
299         {
300                 /*
301                  * We displayed some messages on error output
302                  * (file descriptor 2; see flush()).
303                  * Before erasing the screen contents, wait for a keystroke.
304                  */
305                 less_printf("Press RETURN to continue ", NULL_PARG);
306                 get_return();
307                 putchr('\n');
308         }
309         set_output(1);
310         init();
311         commands();
312         quit(QUIT_OK);
313         /*NOTREACHED*/
314         return (0);
315 }
316
317 /*
318  * Copy a string to a "safe" place
319  * (that is, to a buffer allocated by calloc).
320  */
321         public char *
322 save(s)
323         constant char *s;
324 {
325         char *p;
326
327         p = (char *) ecalloc(strlen(s)+1, sizeof(char));
328         strcpy(p, s);
329         return (p);
330 }
331
332 /*
333  * Allocate memory.
334  * Like calloc(), but never returns an error (NULL).
335  */
336         public VOID_POINTER
337 ecalloc(count, size)
338         int count;
339         unsigned int size;
340 {
341         VOID_POINTER p;
342
343         p = (VOID_POINTER) calloc(count, size);
344         if (p != NULL)
345                 return (p);
346         error("Cannot allocate memory", NULL_PARG);
347         quit(QUIT_ERROR);
348         /*NOTREACHED*/
349         return (NULL);
350 }
351
352 /*
353  * Skip leading spaces in a string.
354  */
355         public char *
356 skipsp(s)
357         char *s;
358 {
359         while (*s == ' ' || *s == '\t')
360                 s++;
361         return (s);
362 }
363
364 /*
365  * See how many characters of two strings are identical.
366  * If uppercase is true, the first string must begin with an uppercase
367  * character; the remainder of the first string may be either case.
368  */
369         public int
370 sprefix(ps, s, uppercase)
371         char *ps;
372         char *s;
373         int uppercase;
374 {
375         int c;
376         int sc;
377         int len = 0;
378
379         for ( ;  *s != '\0';  s++, ps++)
380         {
381                 c = *ps;
382                 if (uppercase)
383                 {
384                         if (len == 0 && ASCII_IS_LOWER(c))
385                                 return (-1);
386                         if (ASCII_IS_UPPER(c))
387                                 c = ASCII_TO_LOWER(c);
388                 }
389                 sc = *s;
390                 if (len > 0 && ASCII_IS_UPPER(sc))
391                         sc = ASCII_TO_LOWER(sc);
392                 if (c != sc)
393                         break;
394                 len++;
395         }
396         return (len);
397 }
398
399 /*
400  * Exit the program.
401  */
402         public void
403 quit(status)
404         int status;
405 {
406         static int save_status;
407
408         /*
409          * Put cursor at bottom left corner, clear the line,
410          * reset the terminal modes, and exit.
411          */
412         if (status < 0)
413                 status = save_status;
414         else
415                 save_status = status;
416 #if LESSTEST
417         rstat('Q');
418 #endif /*LESSTEST*/
419         quitting = 1;
420         edit((char*)NULL);
421         save_cmdhist();
422         if (interactive())
423                 clear_bot();
424         deinit();
425         flush();
426         raw_mode(0);
427 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
428         /* 
429          * If we don't close 2, we get some garbage from
430          * 2's buffer when it flushes automatically.
431          * I cannot track this one down  RB
432          * The same bug shows up if we use ^C^C to abort.
433          */
434         close(2);
435 #endif
436 #ifdef WIN32
437         SetConsoleTitle(consoleTitle);
438 #endif
439         close_getchr();
440         exit(status);
441 }