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