]> www.wagner.pp.ru Git - oss/less.git/blob - main.c
e4f35b9ac72eb34b2c6557999b940c9927d78887
[oss/less.git] / main.c
1 /*
2  * Copyright (C) 1984-2015  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 static int  savedConsoleCodePage;
55 #endif
56
57 extern int      less_is_more;
58 extern int      missing_cap;
59 extern int      know_dumb;
60 extern int      pr_type;
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
74 #ifdef __EMX__
75         _response(&argc, &argv);
76         _wildcard(&argc, &argv);
77 #endif
78
79         progname = *argv++;
80         argc--;
81
82         secure = 0;
83         s = lgetenv("LESSSECURE");
84         if (s != NULL && *s != '\0')
85                 secure = 1;
86
87 #ifdef WIN32
88         if (getenv("HOME") == NULL)
89         {
90                 /*
91                  * If there is no HOME environment variable,
92                  * try the concatenation of HOMEDRIVE + HOMEPATH.
93                  */
94                 char *drive = getenv("HOMEDRIVE");
95                 char *path  = getenv("HOMEPATH");
96                 if (drive != NULL && path != NULL)
97                 {
98                         char *env = (char *) ecalloc(strlen(drive) + 
99                                         strlen(path) + 6, sizeof(char));
100                         strcpy(env, "HOME=");
101                         strcat(env, drive);
102                         strcat(env, path);
103                         putenv(env);
104                 }
105         }
106         GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
107         savedConsoleCodePage = GetConsoleOutputCP();    
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         } else 
279         {
280                 if (edit_first())  /* Edit first valid file in cmd line */
281                         quit(QUIT_ERROR);
282         }
283
284         init();
285         commands();
286         quit(QUIT_OK);
287         /*NOTREACHED*/
288         return (0);
289 }
290
291 /*
292  * Copy a string to a "safe" place
293  * (that is, to a buffer allocated by calloc).
294  */
295         public char *
296 save(s)
297         char *s;
298 {
299         register char *p;
300
301         p = (char *) ecalloc(strlen(s)+1, sizeof(char));
302         strcpy(p, s);
303         return (p);
304 }
305
306 /*
307  * Allocate memory.
308  * Like calloc(), but never returns an error (NULL).
309  */
310         public VOID_POINTER
311 ecalloc(count, size)
312         int count;
313         unsigned int size;
314 {
315         register VOID_POINTER p;
316
317         p = (VOID_POINTER) calloc(count, size);
318         if (p != NULL)
319                 return (p);
320         error("Cannot allocate memory", NULL_PARG);
321         quit(QUIT_ERROR);
322         /*NOTREACHED*/
323         return (NULL);
324 }
325
326 /*
327  * Skip leading spaces in a string.
328  */
329         public char *
330 skipsp(s)
331         register char *s;
332 {
333         while (*s == ' ' || *s == '\t') 
334                 s++;
335         return (s);
336 }
337
338 /*
339  * See how many characters of two strings are identical.
340  * If uppercase is true, the first string must begin with an uppercase
341  * character; the remainder of the first string may be either case.
342  */
343         public int
344 sprefix(ps, s, uppercase)
345         char *ps;
346         char *s;
347         int uppercase;
348 {
349         register int c;
350         register int sc;
351         register int len = 0;
352
353         for ( ;  *s != '\0';  s++, ps++)
354         {
355                 c = *ps;
356                 if (uppercase)
357                 {
358                         if (len == 0 && ASCII_IS_LOWER(c))
359                                 return (-1);
360                         if (ASCII_IS_UPPER(c))
361                                 c = ASCII_TO_LOWER(c);
362                 }
363                 sc = *s;
364                 if (len > 0 && ASCII_IS_UPPER(sc))
365                         sc = ASCII_TO_LOWER(sc);
366                 if (c != sc)
367                         break;
368                 len++;
369         }
370         return (len);
371 }
372
373 /*
374  * Exit the program.
375  */
376         public void
377 quit(status)
378         int status;
379 {
380         static int save_status;
381
382         /*
383          * Put cursor at bottom left corner, clear the line,
384          * reset the terminal modes, and exit.
385          */
386         if (status < 0)
387                 status = save_status;
388         else
389                 save_status = status;
390         quitting = 1;
391         edit((char*)NULL);
392         save_cmdhist();
393         if (any_display && is_tty)
394                 clear_bot();
395         deinit();
396         flush();
397         raw_mode(0);
398 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
399         /* 
400          * If we don't close 2, we get some garbage from
401          * 2's buffer when it flushes automatically.
402          * I cannot track this one down  RB
403          * The same bug shows up if we use ^C^C to abort.
404          */
405         close(2);
406 #endif
407 #ifdef WIN32
408         SetConsoleTitle(consoleTitle);
409         SetConsoleOutputCP(savedConsoleCodePage);
410 #endif
411         close_getchr();
412         exit(status);
413 }