1 /* 2 * Copyright (C) 1984-2011 Mark Nudelman 3 * 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. 6 * 7 * For more information about less, or for information on how to 8 * contact the author, see the README file. 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 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 36 #if LOGFILE 37 public int logfile = -1; 38 public int force_logfile = FALSE; 39 public char * namelogfile = NULL; 40 #endif 41 42 #if EDITOR 43 public char * editor; 44 public char * editproto; 45 #endif 46 47 #if TAGS 48 extern char * tags; 49 extern char * tagoption; 50 extern int jump_sline; 51 #endif 52 53 #ifdef WIN32 54 static char consoleTitle[256]; 55 #endif 56 57 extern int less_is_more; 58 extern int missing_cap; 59 extern int know_dumb; 60 extern int quit_if_one_screen; 61 extern int pr_type; 62 63 64 /* 65 * Entry point. 66 */ 67 int 68 main(argc, argv) 69 int argc; 70 char *argv[]; 71 { 72 IFILE ifile; 73 char *s; 74 75 #ifdef __EMX__ 76 _response(&argc, &argv); 77 _wildcard(&argc, &argv); 78 #endif 79 80 progname = *argv++; 81 argc--; 82 83 secure = 0; 84 s = lgetenv("LESSSECURE"); 85 if (s != NULL && *s != '\0') 86 secure = 1; 87 88 #ifdef WIN32 89 if (getenv("HOME") == NULL) 90 { 91 /* 92 * If there is no HOME environment variable, 93 * try the concatenation of HOMEDRIVE + HOMEPATH. 94 */ 95 char *drive = getenv("HOMEDRIVE"); 96 char *path = getenv("HOMEPATH"); 97 if (drive != NULL && path != NULL) 98 { 99 size_t len = strlen(drive) + strlen(path) + 6; 100 char *env = (char *) ecalloc(len, sizeof(char)); 101 strlcpy(env, "HOME=", len); 102 strlcat(env, drive, len); 103 strlcat(env, path, len); 104 putenv(env); 105 } 106 } 107 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char)); 108 #endif /* WIN32 */ 109 110 /* 111 * Process command line arguments and LESS environment arguments. 112 * Command line arguments override environment arguments. 113 */ 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(); 122 123 /* 124 * If the name of the executable program is "more", 125 * act like LESS_IS_MORE is set. 126 */ 127 for (s = progname + strlen(progname); s > progname; s--) 128 { 129 if (s[-1] == PATHNAME_SEP[0]) 130 break; 131 } 132 if (strcmp(s, "more") == 0) 133 less_is_more = 1; 134 135 init_prompt(); 136 137 if (less_is_more) { 138 scan_option("-G"); 139 scan_option("-L"); 140 scan_option("-X"); 141 } 142 143 s = lgetenv(less_is_more ? "MORE" : "LESS"); 144 if (s != NULL) 145 scan_option(save(s)); 146 147 #define isoptstring(s) (((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 && get_quit_at_eof()) 169 quit_if_one_screen = TRUE; 170 171 #if EDITOR 172 editor = lgetenv("VISUAL"); 173 if (editor == NULL || *editor == '\0') 174 { 175 editor = lgetenv("EDITOR"); 176 if (editor == NULL || *editor == '\0') 177 editor = EDIT_PGM; 178 } 179 editproto = lgetenv("LESSEDIT"); 180 if (editproto == NULL || *editproto == '\0') 181 editproto = "%E ?lm+%lm. %f"; 182 #endif 183 184 /* 185 * Call get_ifile with all the command line filenames 186 * to "register" them with the ifile system. 187 */ 188 ifile = NULL_IFILE; 189 #if !SMALL 190 if (dohelp) 191 ifile = get_ifile(HELPFILE, ifile); 192 #endif /* !SMALL */ 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 #endif 223 } 224 /* 225 * Set up terminal, etc. 226 */ 227 if (!is_tty) 228 { 229 /* 230 * Output is not a tty. 231 * Just copy the input file(s) to output. 232 */ 233 SET_BINARY(1); 234 if (nifile() == 0) 235 { 236 if (edit_stdin() == 0) 237 cat_file(); 238 } else if (edit_first() == 0) 239 { 240 do { 241 cat_file(); 242 } while (edit_next(1) == 0); 243 } 244 quit(QUIT_OK); 245 } 246 247 if (missing_cap && !know_dumb && !less_is_more) 248 error("WARNING: terminal is not fully functional", NULL_PARG); 249 init_mark(); 250 open_getchr(); 251 raw_mode(1); 252 init_signals(1); 253 254 /* 255 * Select the first file to examine. 256 */ 257 #if TAGS 258 if (tagoption != NULL || strcmp(tags, "-") == 0) 259 { 260 /* 261 * A -t option was given. 262 * Verify that no filenames were also given. 263 * Edit the file selected by the "tags" search, 264 * and search for the proper line in the file. 265 */ 266 if (nifile() > 0) 267 { 268 error("No filenames allowed with -t option", NULL_PARG); 269 quit(QUIT_ERROR); 270 } 271 findtag(tagoption); 272 if (edit_tagfile()) /* Edit file which contains the tag */ 273 quit(QUIT_ERROR); 274 /* 275 * Search for the line which contains the tag. 276 * Set up initial_scrpos so we display that line. 277 */ 278 initial_scrpos.pos = tagsearch(); 279 if (initial_scrpos.pos == NULL_POSITION) 280 quit(QUIT_ERROR); 281 initial_scrpos.ln = jump_sline; 282 } else 283 #endif 284 if (nifile() == 0) 285 { 286 if (edit_stdin()) /* Edit standard input */ 287 quit(QUIT_ERROR); 288 } else 289 { 290 if (edit_first()) /* Edit first valid file in cmd line */ 291 quit(QUIT_ERROR); 292 } 293 294 init(); 295 commands(); 296 quit(QUIT_OK); 297 /*NOTREACHED*/ 298 return (0); 299 } 300 301 /* 302 * Copy a string to a "safe" place 303 * (that is, to a buffer allocated by calloc). 304 */ 305 public char * 306 save(s) 307 char *s; 308 { 309 register char *p; 310 size_t len = strlen(s) + 1; 311 312 p = (char *) ecalloc(len, sizeof(char)); 313 strlcpy(p, s, len); 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 #if GNU_OPTIONS 350 /* 351 * See how many characters of two strings are identical. 352 * If uppercase is true, the first string must begin with an uppercase 353 * character; the remainder of the first string may be either case. 354 */ 355 public int 356 sprefix(ps, s, uppercase) 357 char *ps; 358 char *s; 359 int uppercase; 360 { 361 register int c; 362 register int sc; 363 register int len = 0; 364 365 for ( ; *s != '\0'; s++, ps++) 366 { 367 c = *ps; 368 if (uppercase) 369 { 370 if (len == 0 && ASCII_IS_LOWER(c)) 371 return (-1); 372 if (ASCII_IS_UPPER(c)) 373 c = ASCII_TO_LOWER(c); 374 } 375 sc = *s; 376 if (len > 0 && ASCII_IS_UPPER(sc)) 377 sc = ASCII_TO_LOWER(sc); 378 if (c != sc) 379 break; 380 len++; 381 } 382 return (len); 383 } 384 #endif /* GNU_OPTIONS */ 385 386 /* 387 * Exit the program. 388 */ 389 public void 390 quit(status) 391 int status; 392 { 393 static int save_status; 394 395 /* 396 * Put cursor at bottom left corner, clear the line, 397 * reset the terminal modes, and exit. 398 */ 399 if (status < 0) 400 status = save_status; 401 else 402 save_status = status; 403 quitting = 1; 404 edit((char*)NULL); 405 save_cmdhist(); 406 if (any_display && is_tty) 407 clear_bot(); 408 deinit(); 409 flush(); 410 raw_mode(0); 411 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC 412 /* 413 * If we don't close 2, we get some garbage from 414 * 2's buffer when it flushes automatically. 415 * I cannot track this one down RB 416 * The same bug shows up if we use ^C^C to abort. 417 */ 418 close(2); 419 #endif 420 #ifdef WIN32 421 SetConsoleTitle(consoleTitle); 422 #endif 423 close_getchr(); 424 exit(status); 425 } 426