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