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