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