If boot verbose, print asicrev, chiprev and bus type.
[dragonfly.git] / contrib / less-4 / main.c
blob36a230e36ac8eb788bda02769cbe82030ab2f620
1 /*
2 * Copyright (C) 1984-2007 Mark Nudelman
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.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * Entry point, initialization, miscellaneous routines.
16 #include "less.h"
17 #if MSDOS_COMPILER==WIN32C
18 #include <windows.h>
19 #endif
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 dohelp;
35 public int less_is_more;
37 #if LOGFILE
38 public int logfile = -1;
39 public int force_logfile = FALSE;
40 public char * namelogfile = NULL;
41 #endif
43 #if EDITOR
44 public char * editor;
45 public char * editproto;
46 #endif
48 #if TAGS
49 extern char * tags;
50 extern char * tagoption;
51 extern int jump_sline;
52 #endif
54 #ifdef WIN32
55 static char consoleTitle[256];
56 #endif
58 extern int missing_cap;
59 extern int know_dumb;
60 extern int quit_if_one_screen;
61 extern int pr_type;
65 * Entry point.
67 int
68 main(argc, argv)
69 int argc;
70 char *argv[];
72 IFILE ifile;
73 char *s;
75 #ifdef __EMX__
76 _response(&argc, &argv);
77 _wildcard(&argc, &argv);
78 #endif
80 progname = *argv++;
81 argc--;
83 secure = 0;
84 s = lgetenv("LESSSECURE");
85 if (s != NULL && *s != '\0')
86 secure = 1;
88 #ifdef WIN32
89 if (getenv("HOME") == NULL)
92 * If there is no HOME environment variable,
93 * try the concatenation of HOMEDRIVE + HOMEPATH.
95 char *drive = getenv("HOMEDRIVE");
96 char *path = getenv("HOMEPATH");
97 if (drive != NULL && path != NULL)
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);
107 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
108 #endif /* WIN32 */
111 * Process command line arguments and LESS environment arguments.
112 * Command line arguments override environment arguments.
114 is_tty = isatty(1);
115 get_term();
116 init_cmds();
117 init_charset();
118 init_line();
119 init_cmdhist();
120 init_option();
123 * If the name of the executable program is "more",
124 * act like LESS_IS_MORE is set.
126 for (s = progname + strlen(progname); s > progname; s--)
128 if (s[-1] == PATHNAME_SEP[0])
129 break;
131 if (strcmp(s, "more") == 0)
132 less_is_more = 1;
134 init_prompt();
136 s = lgetenv(less_is_more ? "MORE" : "LESS");
137 if (s != NULL)
138 scan_option(save(s));
140 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
141 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
143 s = *argv++;
144 argc--;
145 if (strcmp(s, "--") == 0)
146 break;
147 scan_option(s);
149 #undef isoptstring
151 if (isoptpending())
154 * Last command line option was a flag requiring a
155 * following string, but there was no following string.
157 nopendopt();
158 quit(QUIT_OK);
161 if (less_is_more && get_quit_at_eof())
162 quit_if_one_screen = TRUE;
164 #if EDITOR
165 editor = lgetenv("VISUAL");
166 if (editor == NULL || *editor == '\0')
168 editor = lgetenv("EDITOR");
169 if (editor == NULL || *editor == '\0')
170 editor = EDIT_PGM;
172 editproto = lgetenv("LESSEDIT");
173 if (editproto == NULL || *editproto == '\0')
174 editproto = "%E ?lm+%lm. %f";
175 #endif
178 * Call get_ifile with all the command line filenames
179 * to "register" them with the ifile system.
181 ifile = NULL_IFILE;
182 if (dohelp)
183 ifile = get_ifile(FAKE_HELPFILE, ifile);
184 while (argc-- > 0)
186 char *filename;
187 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
189 * Because the "shell" doesn't expand filename patterns,
190 * treat each argument as a filename pattern rather than
191 * a single filename.
192 * Expand the pattern and iterate over the expanded list.
194 struct textlist tlist;
195 char *gfilename;
197 gfilename = lglob(*argv++);
198 init_textlist(&tlist, gfilename);
199 filename = NULL;
200 while ((filename = forw_textlist(&tlist, filename)) != NULL)
202 (void) get_ifile(filename, ifile);
203 ifile = prev_ifile(NULL_IFILE);
205 free(gfilename);
206 #else
207 filename = shell_quote(*argv);
208 if (filename == NULL)
209 filename = *argv;
210 argv++;
211 (void) get_ifile(filename, ifile);
212 ifile = prev_ifile(NULL_IFILE);
213 #endif
216 * Set up terminal, etc.
218 if (!is_tty)
221 * Output is not a tty.
222 * Just copy the input file(s) to output.
224 SET_BINARY(1);
225 if (nifile() == 0)
227 if (edit_stdin() == 0)
228 cat_file();
229 } else if (edit_first() == 0)
231 do {
232 cat_file();
233 } while (edit_next(1) == 0);
235 quit(QUIT_OK);
238 if (missing_cap && !know_dumb)
239 error("WARNING: terminal is not fully functional", NULL_PARG);
240 init_mark();
241 open_getchr();
242 raw_mode(1);
243 init_signals(1);
246 * Select the first file to examine.
248 #if TAGS
249 if (tagoption != NULL || strcmp(tags, "-") == 0)
252 * A -t option was given.
253 * Verify that no filenames were also given.
254 * Edit the file selected by the "tags" search,
255 * and search for the proper line in the file.
257 if (nifile() > 0)
259 error("No filenames allowed with -t option", NULL_PARG);
260 quit(QUIT_ERROR);
262 findtag(tagoption);
263 if (edit_tagfile()) /* Edit file which contains the tag */
264 quit(QUIT_ERROR);
266 * Search for the line which contains the tag.
267 * Set up initial_scrpos so we display that line.
269 initial_scrpos.pos = tagsearch();
270 if (initial_scrpos.pos == NULL_POSITION)
271 quit(QUIT_ERROR);
272 initial_scrpos.ln = jump_sline;
273 } else
274 #endif
275 if (nifile() == 0)
277 if (edit_stdin()) /* Edit standard input */
278 quit(QUIT_ERROR);
279 } else
281 if (edit_first()) /* Edit first valid file in cmd line */
282 quit(QUIT_ERROR);
285 init();
286 commands();
287 quit(QUIT_OK);
288 /*NOTREACHED*/
289 return (0);
293 * Copy a string to a "safe" place
294 * (that is, to a buffer allocated by calloc).
296 public char *
297 save(s)
298 char *s;
300 register char *p;
302 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
303 strcpy(p, s);
304 return (p);
308 * Allocate memory.
309 * Like calloc(), but never returns an error (NULL).
311 public VOID_POINTER
312 ecalloc(count, size)
313 int count;
314 unsigned int size;
316 register VOID_POINTER p;
318 p = (VOID_POINTER) calloc(count, size);
319 if (p != NULL)
320 return (p);
321 error("Cannot allocate memory", NULL_PARG);
322 quit(QUIT_ERROR);
323 /*NOTREACHED*/
324 return (NULL);
328 * Skip leading spaces in a string.
330 public char *
331 skipsp(s)
332 register char *s;
334 while (*s == ' ' || *s == '\t')
335 s++;
336 return (s);
340 * See how many characters of two strings are identical.
341 * If uppercase is true, the first string must begin with an uppercase
342 * character; the remainder of the first string may be either case.
344 public int
345 sprefix(ps, s, uppercase)
346 char *ps;
347 char *s;
348 int uppercase;
350 register int c;
351 register int sc;
352 register int len = 0;
354 for ( ; *s != '\0'; s++, ps++)
356 c = *ps;
357 if (uppercase)
359 if (len == 0 && ASCII_IS_LOWER(c))
360 return (-1);
361 if (ASCII_IS_UPPER(c))
362 c = ASCII_TO_LOWER(c);
364 sc = *s;
365 if (len > 0 && ASCII_IS_UPPER(sc))
366 sc = ASCII_TO_LOWER(sc);
367 if (c != sc)
368 break;
369 len++;
371 return (len);
375 * Exit the program.
377 public void
378 quit(status)
379 int status;
381 static int save_status;
384 * Put cursor at bottom left corner, clear the line,
385 * reset the terminal modes, and exit.
387 if (status < 0)
388 status = save_status;
389 else
390 save_status = status;
391 quitting = 1;
392 edit((char*)NULL);
393 save_cmdhist();
394 if (any_display && is_tty)
395 clear_bot();
396 deinit();
397 flush();
398 raw_mode(0);
399 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
401 * If we don't close 2, we get some garbage from
402 * 2's buffer when it flushes automatically.
403 * I cannot track this one down RB
404 * The same bug shows up if we use ^C^C to abort.
406 close(2);
407 #endif
408 #if WIN32
409 SetConsoleTitle(consoleTitle);
410 #endif
411 close_getchr();
412 exit(status);