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