Imported Upstream version 530
[platform/upstream/less.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         init_cmds();
116         get_term();
117         expand_cmd_tables();
118         init_charset();
119         init_line();
120         init_cmdhist();
121         init_option();
122         init_search();
123
124         /*
125          * If the name of the executable program is "more",
126          * act like LESS_IS_MORE is set.
127          */
128         for (s = progname + strlen(progname);  s > progname;  s--)
129         {
130                 if (s[-1] == PATHNAME_SEP[0])
131                         break;
132         }
133         if (strcmp(s, "more") == 0)
134                 less_is_more = 1;
135
136         init_prompt();
137
138         s = lgetenv(less_is_more ? "MORE" : "LESS");
139         if (s != NULL)
140                 scan_option(save(s));
141
142 #define isoptstring(s)  (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
143         while (argc > 0 && (isoptstring(*argv) || isoptpending()))
144         {
145                 s = *argv++;
146                 argc--;
147                 if (strcmp(s, "--") == 0)
148                         break;
149                 scan_option(s);
150         }
151 #undef isoptstring
152
153         if (isoptpending())
154         {
155                 /*
156                  * Last command line option was a flag requiring a
157                  * following string, but there was no following string.
158                  */
159                 nopendopt();
160                 quit(QUIT_OK);
161         }
162
163 #if EDITOR
164         editor = lgetenv("VISUAL");
165         if (editor == NULL || *editor == '\0')
166         {
167                 editor = lgetenv("EDITOR");
168                 if (editor == NULL || *editor == '\0')
169                         editor = EDIT_PGM;
170         }
171         editproto = lgetenv("LESSEDIT");
172         if (editproto == NULL || *editproto == '\0')
173                 editproto = "%E ?lm+%lm. %f";
174 #endif
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;
181         if (dohelp)
182                 ifile = get_ifile(FAKE_HELPFILE, ifile);
183         while (argc-- > 0)
184         {
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 *filename;
194                 char *gfilename;
195                 char *qfilename;
196                 
197                 gfilename = lglob(*argv++);
198                 init_textlist(&tlist, gfilename);
199                 filename = NULL;
200                 while ((filename = forw_textlist(&tlist, filename)) != NULL)
201                 {
202                         qfilename = shell_unquote(filename);
203                         (void) get_ifile(qfilename, ifile);
204                         free(qfilename);
205                         ifile = prev_ifile(NULL_IFILE);
206                 }
207                 free(gfilename);
208 #else
209                 (void) get_ifile(*argv++, ifile);
210                 ifile = prev_ifile(NULL_IFILE);
211 #endif
212         }
213         /*
214          * Set up terminal, etc.
215          */
216         if (!is_tty)
217         {
218                 /*
219                  * Output is not a tty.
220                  * Just copy the input file(s) to output.
221                  */
222                 SET_BINARY(1);
223                 if (nifile() == 0)
224                 {
225                         if (edit_stdin() == 0)
226                                 cat_file();
227                 } else if (edit_first() == 0)
228                 {
229                         do {
230                                 cat_file();
231                         } while (edit_next(1) == 0);
232                 }
233                 quit(QUIT_OK);
234         }
235
236         if (missing_cap && !know_dumb)
237                 error("WARNING: terminal is not fully functional", NULL_PARG);
238         init_mark();
239         open_getchr();
240         raw_mode(1);
241         init_signals(1);
242
243         /*
244          * Select the first file to examine.
245          */
246 #if TAGS
247         if (tagoption != NULL || strcmp(tags, "-") == 0)
248         {
249                 /*
250                  * A -t option was given.
251                  * Verify that no filenames were also given.
252                  * Edit the file selected by the "tags" search,
253                  * and search for the proper line in the file.
254                  */
255                 if (nifile() > 0)
256                 {
257                         error("No filenames allowed with -t option", NULL_PARG);
258                         quit(QUIT_ERROR);
259                 }
260                 findtag(tagoption);
261                 if (edit_tagfile())  /* Edit file which contains the tag */
262                         quit(QUIT_ERROR);
263                 /*
264                  * Search for the line which contains the tag.
265                  * Set up initial_scrpos so we display that line.
266                  */
267                 initial_scrpos.pos = tagsearch();
268                 if (initial_scrpos.pos == NULL_POSITION)
269                         quit(QUIT_ERROR);
270                 initial_scrpos.ln = jump_sline;
271         } else
272 #endif
273         if (nifile() == 0)
274         {
275                 if (edit_stdin())  /* Edit standard input */
276                         quit(QUIT_ERROR);
277                 if (quit_if_one_screen)
278                         line_count = get_line_count();
279         } else 
280         {
281                 if (edit_first())  /* Edit first valid file in cmd line */
282                         quit(QUIT_ERROR);
283                 if (quit_if_one_screen)
284                 {
285                         if (nifile() == 1)
286                                 line_count = get_line_count();
287                         else /* If more than one file, -F can not be used */
288                                 quit_if_one_screen = FALSE;
289                 }
290         }
291
292         init();
293         commands();
294         quit(QUIT_OK);
295         /*NOTREACHED*/
296         return (0);
297 }
298
299 /*
300  * Copy a string to a "safe" place
301  * (that is, to a buffer allocated by calloc).
302  */
303         public char *
304 save(s)
305         constant char *s;
306 {
307         char *p;
308
309         p = (char *) ecalloc(strlen(s)+1, sizeof(char));
310         strcpy(p, s);
311         return (p);
312 }
313
314 /*
315  * Allocate memory.
316  * Like calloc(), but never returns an error (NULL).
317  */
318         public VOID_POINTER
319 ecalloc(count, size)
320         int count;
321         unsigned int size;
322 {
323         VOID_POINTER p;
324
325         p = (VOID_POINTER) calloc(count, size);
326         if (p != NULL)
327                 return (p);
328         error("Cannot allocate memory", NULL_PARG);
329         quit(QUIT_ERROR);
330         /*NOTREACHED*/
331         return (NULL);
332 }
333
334 /*
335  * Skip leading spaces in a string.
336  */
337         public char *
338 skipsp(s)
339         char *s;
340 {
341         while (*s == ' ' || *s == '\t') 
342                 s++;
343         return (s);
344 }
345
346 /*
347  * See how many characters of two strings are identical.
348  * If uppercase is true, the first string must begin with an uppercase
349  * character; the remainder of the first string may be either case.
350  */
351         public int
352 sprefix(ps, s, uppercase)
353         char *ps;
354         char *s;
355         int uppercase;
356 {
357         int c;
358         int sc;
359         int len = 0;
360
361         for ( ;  *s != '\0';  s++, ps++)
362         {
363                 c = *ps;
364                 if (uppercase)
365                 {
366                         if (len == 0 && ASCII_IS_LOWER(c))
367                                 return (-1);
368                         if (ASCII_IS_UPPER(c))
369                                 c = ASCII_TO_LOWER(c);
370                 }
371                 sc = *s;
372                 if (len > 0 && ASCII_IS_UPPER(sc))
373                         sc = ASCII_TO_LOWER(sc);
374                 if (c != sc)
375                         break;
376                 len++;
377         }
378         return (len);
379 }
380
381 /*
382  * Exit the program.
383  */
384         public void
385 quit(status)
386         int status;
387 {
388         static int save_status;
389
390         /*
391          * Put cursor at bottom left corner, clear the line,
392          * reset the terminal modes, and exit.
393          */
394         if (status < 0)
395                 status = save_status;
396         else
397                 save_status = status;
398         quitting = 1;
399         edit((char*)NULL);
400         save_cmdhist();
401         if (any_display && is_tty)
402                 clear_bot();
403         deinit();
404         flush();
405         raw_mode(0);
406 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
407         /* 
408          * If we don't close 2, we get some garbage from
409          * 2's buffer when it flushes automatically.
410          * I cannot track this one down  RB
411          * The same bug shows up if we use ^C^C to abort.
412          */
413         close(2);
414 #endif
415 #ifdef WIN32
416         SetConsoleTitle(consoleTitle);
417 #endif
418         close_getchr();
419         exit(status);
420 }