1 /* 2 * Copyright (C) 1984-2012 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, see the README file. 8 */ 9 10 11 /* 12 * Entry point, initialization, miscellaneous routines. 13 */ 14 15 #include "less.h" 16 #if MSDOS_COMPILER==WIN32C 17 #include <windows.h> 18 #endif 19 20 public char * every_first_cmd = NULL; 21 public int new_file; 22 public int is_tty; 23 public IFILE curr_ifile = NULL_IFILE; 24 public IFILE old_ifile = NULL_IFILE; 25 public struct scrpos initial_scrpos; 26 public int any_display = FALSE; 27 public POSITION start_attnpos = NULL_POSITION; 28 public POSITION end_attnpos = NULL_POSITION; 29 public int wscroll; 30 public char * progname; 31 public int quitting; 32 public int secure; 33 public int dohelp; 34 35 #if LOGFILE 36 public int logfile = -1; 37 public int force_logfile = FALSE; 38 public char * namelogfile = NULL; 39 #endif 40 41 #if EDITOR 42 public char * editor; 43 public char * editproto; 44 #endif 45 46 #if TAGS 47 extern char * tags; 48 extern char * tagoption; 49 extern int jump_sline; 50 #endif 51 52 #ifdef WIN32 53 static char consoleTitle[256]; 54 #endif 55 56 extern int less_is_more; 57 extern int missing_cap; 58 extern int know_dumb; 59 extern int quit_if_one_screen; 60 extern int pr_type; 61 62 63 /* 64 * Entry point. 65 */ 66 int 67 main(argc, argv) 68 int argc; 69 char *argv[]; 70 { 71 IFILE ifile; 72 char *s; 73 74 #ifdef __EMX__ 75 _response(&argc, &argv); 76 _wildcard(&argc, &argv); 77 #endif 78 79 progname = *argv++; 80 argc--; 81 82 secure = 0; 83 s = lgetenv("LESSSECURE"); 84 if (s != NULL && *s != '\0') 85 secure = 1; 86 87 #ifdef WIN32 88 if (getenv("HOME") == NULL) 89 { 90 /* 91 * If there is no HOME environment variable, 92 * try the concatenation of HOMEDRIVE + HOMEPATH. 93 */ 94 char *drive = getenv("HOMEDRIVE"); 95 char *path = getenv("HOMEPATH"); 96 if (drive != NULL && path != NULL) 97 { 98 char *env = (char *) ecalloc(strlen(drive) + 99 strlen(path) + 6, sizeof(char)); 100 strcpy(env, "HOME="); 101 strcat(env, drive); 102 strcat(env, path); 103 putenv(env); 104 } 105 } 106 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char)); 107 #endif /* WIN32 */ 108 109 /* 110 * Process command line arguments and LESS environment arguments. 111 * Command line arguments override environment arguments. 112 */ 113 is_tty = isatty(1); 114 get_term(); 115 init_cmds(); 116 init_charset(); 117 init_line(); 118 init_cmdhist(); 119 init_option(); 120 init_search(); 121 122 /* 123 * If the name of the executable program is "more", 124 * act like LESS_IS_MORE is set. 125 */ 126 for (s = progname + strlen(progname); s > progname; s--) 127 { 128 if (s[-1] == PATHNAME_SEP[0]) 129 break; 130 } 131 if (strcmp(s, "more") == 0) 132 less_is_more = 1; 133 134 init_prompt(); 135 136 s = lgetenv(less_is_more ? "MORE" : "LESS"); 137 if (s != NULL) 138 scan_option(save(s)); 139 140 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0') 141 while (argc > 0 && (isoptstring(*argv) || isoptpending())) 142 { 143 s = *argv++; 144 argc--; 145 if (strcmp(s, "--") == 0) 146 break; 147 scan_option(s); 148 } 149 #undef isoptstring 150 151 if (isoptpending()) 152 { 153 /* 154 * Last command line option was a flag requiring a 155 * following string, but there was no following string. 156 */ 157 nopendopt(); 158 quit(QUIT_OK); 159 } 160 161 if (less_is_more && get_quit_at_eof()) 162 quit_if_one_screen = TRUE; 163 164 #if EDITOR 165 editor = lgetenv("VISUAL"); 166 if (editor == NULL || *editor == '\0') 167 { 168 editor = lgetenv("EDITOR"); 169 if (editor == NULL || *editor == '\0') 170 editor = EDIT_PGM; 171 } 172 editproto = lgetenv("LESSEDIT"); 173 if (editproto == NULL || *editproto == '\0') 174 editproto = "%E ?lm+%lm. %f"; 175 #endif 176 177 /* 178 * Call get_ifile with all the command line filenames 179 * to "register" them with the ifile system. 180 */ 181 ifile = NULL_IFILE; 182 if (dohelp) 183 ifile = get_ifile(FAKE_HELPFILE, ifile); 184 while (argc-- > 0) 185 { 186 char *filename; 187 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC) 188 /* 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. 193 */ 194 struct textlist tlist; 195 char *gfilename; 196 197 gfilename = lglob(*argv++); 198 init_textlist(&tlist, gfilename); 199 filename = NULL; 200 while ((filename = forw_textlist(&tlist, filename)) != NULL) 201 { 202 (void) get_ifile(filename, ifile); 203 ifile = prev_ifile(NULL_IFILE); 204 } 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 free(filename); 214 #endif 215 } 216 /* 217 * Set up terminal, etc. 218 */ 219 if (!is_tty) 220 { 221 /* 222 * Output is not a tty. 223 * Just copy the input file(s) to output. 224 */ 225 SET_BINARY(1); 226 if (nifile() == 0) 227 { 228 if (edit_stdin() == 0) 229 cat_file(); 230 } else if (edit_first() == 0) 231 { 232 do { 233 cat_file(); 234 } while (edit_next(1) == 0); 235 } 236 quit(QUIT_OK); 237 } 238 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); 245 246 /* 247 * Select the first file to examine. 248 */ 249 #if TAGS 250 if (tagoption != NULL || strcmp(tags, "-") == 0) 251 { 252 /* 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. 257 */ 258 if (nifile() > 0) 259 { 260 error("No filenames allowed with -t option", NULL_PARG); 261 quit(QUIT_ERROR); 262 } 263 findtag(tagoption); 264 if (edit_tagfile()) /* Edit file which contains the tag */ 265 quit(QUIT_ERROR); 266 /* 267 * Search for the line which contains the tag. 268 * Set up initial_scrpos so we display that line. 269 */ 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) 277 { 278 if (edit_stdin()) /* Edit standard input */ 279 quit(QUIT_ERROR); 280 } else 281 { 282 if (edit_first()) /* Edit first valid file in cmd line */ 283 quit(QUIT_ERROR); 284 } 285 286 init(); 287 commands(); 288 quit(QUIT_OK); 289 /*NOTREACHED*/ 290 return (0); 291 } 292 293 /* 294 * Copy a string to a "safe" place 295 * (that is, to a buffer allocated by calloc). 296 */ 297 public char * 298 save(s) 299 char *s; 300 { 301 register char *p; 302 303 p = (char *) ecalloc(strlen(s)+1, sizeof(char)); 304 strcpy(p, s); 305 return (p); 306 } 307 308 /* 309 * Allocate memory. 310 * Like calloc(), but never returns an error (NULL). 311 */ 312 public VOID_POINTER 313 ecalloc(count, size) 314 int count; 315 unsigned int size; 316 { 317 register VOID_POINTER p; 318 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); 326 } 327 328 /* 329 * Skip leading spaces in a string. 330 */ 331 public char * 332 skipsp(s) 333 register char *s; 334 { 335 while (*s == ' ' || *s == '\t') 336 s++; 337 return (s); 338 } 339 340 /* 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. 344 */ 345 public int 346 sprefix(ps, s, uppercase) 347 char *ps; 348 char *s; 349 int uppercase; 350 { 351 register int c; 352 register int sc; 353 register int len = 0; 354 355 for ( ; *s != '\0'; s++, ps++) 356 { 357 c = *ps; 358 if (uppercase) 359 { 360 if (len == 0 && ASCII_IS_LOWER(c)) 361 return (-1); 362 if (ASCII_IS_UPPER(c)) 363 c = ASCII_TO_LOWER(c); 364 } 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++; 371 } 372 return (len); 373 } 374 375 /* 376 * Exit the program. 377 */ 378 public void 379 quit(status) 380 int status; 381 { 382 static int save_status; 383 384 /* 385 * Put cursor at bottom left corner, clear the line, 386 * reset the terminal modes, and exit. 387 */ 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 401 /* 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. 406 */ 407 close(2); 408 #endif 409 #ifdef WIN32 410 SetConsoleTitle(consoleTitle); 411 #endif 412 close_getchr(); 413 exit(status); 414 } 415