Actually hook powernow.4 into the build.
[dragonfly.git] / contrib / less / main.c
blobed6a61612305024a1678d3bae201bccf8c8af699
1 /*
2 * Copyright (C) 1984-2009 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();
121 init_search();
124 * If the name of the executable program is "more",
125 * act like LESS_IS_MORE is set.
127 for (s = progname + strlen(progname); s > progname; s--)
129 if (s[-1] == PATHNAME_SEP[0])
130 break;
132 if (strcmp(s, "more") == 0)
133 less_is_more = 1;
135 init_prompt();
137 s = lgetenv(less_is_more ? "MORE" : "LESS");
138 if (s != NULL)
139 scan_option(save(s));
141 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
142 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
144 s = *argv++;
145 argc--;
146 if (strcmp(s, "--") == 0)
147 break;
148 scan_option(s);
150 #undef isoptstring
152 if (isoptpending())
155 * Last command line option was a flag requiring a
156 * following string, but there was no following string.
158 nopendopt();
159 quit(QUIT_OK);
162 if (less_is_more && get_quit_at_eof())
163 quit_if_one_screen = TRUE;
165 #if EDITOR
166 editor = lgetenv("VISUAL");
167 if (editor == NULL || *editor == '\0')
169 editor = lgetenv("EDITOR");
170 if (editor == NULL || *editor == '\0')
171 editor = EDIT_PGM;
173 editproto = lgetenv("LESSEDIT");
174 if (editproto == NULL || *editproto == '\0')
175 editproto = "%E ?lm+%lm. %f";
176 #endif
179 * Call get_ifile with all the command line filenames
180 * to "register" them with the ifile system.
182 ifile = NULL_IFILE;
183 if (dohelp)
184 ifile = get_ifile(FAKE_HELPFILE, ifile);
185 while (argc-- > 0)
187 char *filename;
188 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
190 * Because the "shell" doesn't expand filename patterns,
191 * treat each argument as a filename pattern rather than
192 * a single filename.
193 * Expand the pattern and iterate over the expanded list.
195 struct textlist tlist;
196 char *gfilename;
198 gfilename = lglob(*argv++);
199 init_textlist(&tlist, gfilename);
200 filename = NULL;
201 while ((filename = forw_textlist(&tlist, filename)) != NULL)
203 (void) get_ifile(filename, ifile);
204 ifile = prev_ifile(NULL_IFILE);
206 free(gfilename);
207 #else
208 filename = shell_quote(*argv);
209 if (filename == NULL)
210 filename = *argv;
211 argv++;
212 (void) get_ifile(filename, ifile);
213 ifile = prev_ifile(NULL_IFILE);
214 #endif
217 * Set up terminal, etc.
219 if (!is_tty)
222 * Output is not a tty.
223 * Just copy the input file(s) to output.
225 SET_BINARY(1);
226 if (nifile() == 0)
228 if (edit_stdin() == 0)
229 cat_file();
230 } else if (edit_first() == 0)
232 do {
233 cat_file();
234 } while (edit_next(1) == 0);
236 quit(QUIT_OK);
239 if (missing_cap && !know_dumb)
240 error("WARNING: terminal is not fully functional", NULL_PARG);
241 init_mark();
242 open_getchr();
243 raw_mode(1);
244 init_signals(1);
247 * Select the first file to examine.
249 #if TAGS
250 if (tagoption != NULL || strcmp(tags, "-") == 0)
253 * A -t option was given.
254 * Verify that no filenames were also given.
255 * Edit the file selected by the "tags" search,
256 * and search for the proper line in the file.
258 if (nifile() > 0)
260 error("No filenames allowed with -t option", NULL_PARG);
261 quit(QUIT_ERROR);
263 findtag(tagoption);
264 if (edit_tagfile()) /* Edit file which contains the tag */
265 quit(QUIT_ERROR);
267 * Search for the line which contains the tag.
268 * Set up initial_scrpos so we display that line.
270 initial_scrpos.pos = tagsearch();
271 if (initial_scrpos.pos == NULL_POSITION)
272 quit(QUIT_ERROR);
273 initial_scrpos.ln = jump_sline;
274 } else
275 #endif
276 if (nifile() == 0)
278 if (edit_stdin()) /* Edit standard input */
279 quit(QUIT_ERROR);
280 } else
282 if (edit_first()) /* Edit first valid file in cmd line */
283 quit(QUIT_ERROR);
286 init();
287 commands();
288 quit(QUIT_OK);
289 /*NOTREACHED*/
290 return (0);
294 * Copy a string to a "safe" place
295 * (that is, to a buffer allocated by calloc).
297 public char *
298 save(s)
299 char *s;
301 register char *p;
303 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
304 strcpy(p, s);
305 return (p);
309 * Allocate memory.
310 * Like calloc(), but never returns an error (NULL).
312 public VOID_POINTER
313 ecalloc(count, size)
314 int count;
315 unsigned int size;
317 register VOID_POINTER p;
319 p = (VOID_POINTER) calloc(count, size);
320 if (p != NULL)
321 return (p);
322 error("Cannot allocate memory", NULL_PARG);
323 quit(QUIT_ERROR);
324 /*NOTREACHED*/
325 return (NULL);
329 * Skip leading spaces in a string.
331 public char *
332 skipsp(s)
333 register char *s;
335 while (*s == ' ' || *s == '\t')
336 s++;
337 return (s);
341 * See how many characters of two strings are identical.
342 * If uppercase is true, the first string must begin with an uppercase
343 * character; the remainder of the first string may be either case.
345 public int
346 sprefix(ps, s, uppercase)
347 char *ps;
348 char *s;
349 int uppercase;
351 register int c;
352 register int sc;
353 register int len = 0;
355 for ( ; *s != '\0'; s++, ps++)
357 c = *ps;
358 if (uppercase)
360 if (len == 0 && ASCII_IS_LOWER(c))
361 return (-1);
362 if (ASCII_IS_UPPER(c))
363 c = ASCII_TO_LOWER(c);
365 sc = *s;
366 if (len > 0 && ASCII_IS_UPPER(sc))
367 sc = ASCII_TO_LOWER(sc);
368 if (c != sc)
369 break;
370 len++;
372 return (len);
376 * Exit the program.
378 public void
379 quit(status)
380 int status;
382 static int save_status;
385 * Put cursor at bottom left corner, clear the line,
386 * reset the terminal modes, and exit.
388 if (status < 0)
389 status = save_status;
390 else
391 save_status = status;
392 quitting = 1;
393 edit((char*)NULL);
394 save_cmdhist();
395 if (any_display && is_tty)
396 clear_bot();
397 deinit();
398 flush();
399 raw_mode(0);
400 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
402 * If we don't close 2, we get some garbage from
403 * 2's buffer when it flushes automatically.
404 * I cannot track this one down RB
405 * The same bug shows up if we use ^C^C to abort.
407 close(2);
408 #endif
409 #if WIN32
410 SetConsoleTitle(consoleTitle);
411 #endif
412 close_getchr();
413 exit(status);