Deleted Added
full compact
main.c (221715) main.c (237613)
1/* $FreeBSD: head/contrib/less/main.c 221715 2011-05-09 21:51:59Z delphij $ */
1/* $FreeBSD: head/contrib/less/main.c 237613 2012-06-26 23:17:33Z delphij $ */
2/*
2/*
3 * Copyright (C) 1984-2011 Mark Nudelman
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 *
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.
8 * For more information, 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
22public char * every_first_cmd = NULL;
23public int new_file;
24public int is_tty;
25public IFILE curr_ifile = NULL_IFILE;
26public IFILE old_ifile = NULL_IFILE;
27public struct scrpos initial_scrpos;
28public int any_display = FALSE;
29public POSITION start_attnpos = NULL_POSITION;
30public POSITION end_attnpos = NULL_POSITION;
31public int wscroll;
32public char * progname;
33public int quitting;
34public int secure;
35public int dohelp;
36
37#if LOGFILE
38public int logfile = -1;
39public int force_logfile = FALSE;
40public char * namelogfile = NULL;
41#endif
42
43#if EDITOR
44public char * editor;
45public char * editproto;
46#endif
47
48#if TAGS
49extern char * tags;
50extern char * tagoption;
51extern int jump_sline;
52#endif
53
54#ifdef WIN32
55static char consoleTitle[256];
56#endif
57
58extern int less_is_more;
59extern int missing_cap;
60extern int know_dumb;
61extern int quit_if_one_screen;
62extern int no_init;
63extern int pr_type;
64
65
66/*
67 * Entry point.
68 */
69int
70main(argc, argv)
71 int argc;
72 char *argv[];
73{
74 IFILE ifile;
75 char *s;
76 extern char *__progname;
77
78#ifdef __EMX__
79 _response(&argc, &argv);
80 _wildcard(&argc, &argv);
81#endif
82
83 progname = *argv++;
84 argc--;
85
86 secure = 0;
87 s = lgetenv("LESSSECURE");
88 if (s != NULL && *s != '\0')
89 secure = 1;
90
91#ifdef WIN32
92 if (getenv("HOME") == NULL)
93 {
94 /*
95 * If there is no HOME environment variable,
96 * try the concatenation of HOMEDRIVE + HOMEPATH.
97 */
98 char *drive = getenv("HOMEDRIVE");
99 char *path = getenv("HOMEPATH");
100 if (drive != NULL && path != NULL)
101 {
102 char *env = (char *) ecalloc(strlen(drive) +
103 strlen(path) + 6, sizeof(char));
104 strcpy(env, "HOME=");
105 strcat(env, drive);
106 strcat(env, path);
107 putenv(env);
108 }
109 }
110 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
111#endif /* WIN32 */
112
113 /*
114 * Process command line arguments and LESS environment arguments.
115 * Command line arguments override environment arguments.
116 */
117 is_tty = isatty(1);
118 get_term();
119 init_cmds();
120 init_charset();
121 init_line();
122 init_cmdhist();
123 init_option();
124 init_search();
125
126 /*
127 * If the name of the executable program is "more",
128 * act like LESS_IS_MORE is set.
129 */
130 for (s = progname + strlen(progname); s > progname; s--)
131 {
132 if (s[-1] == PATHNAME_SEP[0])
133 break;
134 }
135 if (strcmp(s, "more") == 0)
136 less_is_more = 1;
137
138 init_prompt();
139
140 if (less_is_more)
141 scan_option("-fG");
142
143 s = lgetenv(less_is_more ? "MORE" : "LESS");
144 if (s != NULL)
145 scan_option(save(s));
146
147#define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
148 (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
149 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
150 {
151 s = *argv++;
152 argc--;
153 if (strcmp(s, "--") == 0)
154 break;
155 scan_option(s);
156 }
157#undef isoptstring
158
159 if (isoptpending())
160 {
161 /*
162 * Last command line option was a flag requiring a
163 * following string, but there was no following string.
164 */
165 nopendopt();
166 quit(QUIT_OK);
167 }
168
169 if (less_is_more)
170 no_init = TRUE;
171 if (less_is_more && get_quit_at_eof())
172 quit_if_one_screen = TRUE;
173
174#if EDITOR
175 editor = lgetenv("VISUAL");
176 if (editor == NULL || *editor == '\0')
177 {
178 editor = lgetenv("EDITOR");
179 if (editor == NULL || *editor == '\0')
180 editor = EDIT_PGM;
181 }
182 editproto = lgetenv("LESSEDIT");
183 if (editproto == NULL || *editproto == '\0')
184 editproto = "%E ?lm+%lm. %f";
185#endif
186
187 /*
188 * Call get_ifile with all the command line filenames
189 * to "register" them with the ifile system.
190 */
191 ifile = NULL_IFILE;
192 if (dohelp)
193 ifile = get_ifile(FAKE_HELPFILE, ifile);
194 while (argc-- > 0)
195 {
196 char *filename;
197#if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
198 /*
199 * Because the "shell" doesn't expand filename patterns,
200 * treat each argument as a filename pattern rather than
201 * a single filename.
202 * Expand the pattern and iterate over the expanded list.
203 */
204 struct textlist tlist;
205 char *gfilename;
206
207 gfilename = lglob(*argv++);
208 init_textlist(&tlist, gfilename);
209 filename = NULL;
210 while ((filename = forw_textlist(&tlist, filename)) != NULL)
211 {
212 (void) get_ifile(filename, ifile);
213 ifile = prev_ifile(NULL_IFILE);
214 }
215 free(gfilename);
216#else
217 filename = shell_quote(*argv);
218 if (filename == NULL)
219 filename = *argv;
220 argv++;
221 (void) get_ifile(filename, ifile);
222 ifile = prev_ifile(NULL_IFILE);
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
21public char * every_first_cmd = NULL;
22public int new_file;
23public int is_tty;
24public IFILE curr_ifile = NULL_IFILE;
25public IFILE old_ifile = NULL_IFILE;
26public struct scrpos initial_scrpos;
27public int any_display = FALSE;
28public POSITION start_attnpos = NULL_POSITION;
29public POSITION end_attnpos = NULL_POSITION;
30public int wscroll;
31public char * progname;
32public int quitting;
33public int secure;
34public int dohelp;
35
36#if LOGFILE
37public int logfile = -1;
38public int force_logfile = FALSE;
39public char * namelogfile = NULL;
40#endif
41
42#if EDITOR
43public char * editor;
44public char * editproto;
45#endif
46
47#if TAGS
48extern char * tags;
49extern char * tagoption;
50extern int jump_sline;
51#endif
52
53#ifdef WIN32
54static char consoleTitle[256];
55#endif
56
57extern int less_is_more;
58extern int missing_cap;
59extern int know_dumb;
60extern int quit_if_one_screen;
61extern int no_init;
62extern int pr_type;
63
64
65/*
66 * Entry point.
67 */
68int
69main(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 *
307save(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
322ecalloc(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 *
341skipsp(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
355sprefix(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
388quit(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}
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 *
307save(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
322ecalloc(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 *
341skipsp(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
355sprefix(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
388quit(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}