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