1 /* $FreeBSD$ */ 2 /* 3 * Copyright (C) 1984-2011 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 22 public char * every_first_cmd = NULL; 23 public int new_file; 24 public int is_tty; 25 public IFILE curr_ifile = NULL_IFILE; 26 public IFILE old_ifile = NULL_IFILE; 27 public struct scrpos initial_scrpos; 28 public int any_display = FALSE; 29 public POSITION start_attnpos = NULL_POSITION; 30 public POSITION end_attnpos = NULL_POSITION; 31 public int wscroll; 32 public char * progname; 33 public int quitting; 34 public int secure; 35 public int dohelp; 36 37 #if LOGFILE 38 public int logfile = -1; 39 public int force_logfile = FALSE; 40 public char * namelogfile = NULL; 41 #endif 42 43 #if EDITOR 44 public char * editor; 45 public char * editproto; 46 #endif 47 48 #if TAGS 49 extern char * tags; 50 extern char * tagoption; 51 extern int jump_sline; 52 #endif 53 54 #ifdef WIN32 55 static char consoleTitle[256]; 56 #endif 57 58 extern int less_is_more; 59 extern int missing_cap; 60 extern int know_dumb; 61 extern int quit_if_one_screen; 62 extern int no_init; 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 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); 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 * 307 save(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 322 ecalloc(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 * 341 skipsp(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 355 sprefix(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 388 quit(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 } 424