1 /* $FreeBSD$ */ 2 /* 3 * Copyright (C) 1984-2015 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, 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 no_init; 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 extern char *__progname; 75 76 #ifdef __EMX__ 77 _response(&argc, &argv); 78 _wildcard(&argc, &argv); 79 #endif 80 81 progname = *argv++; 82 argc--; 83 84 secure = 0; 85 s = lgetenv("LESSSECURE"); 86 if (s != NULL && *s != '\0') 87 secure = 1; 88 89 #ifdef WIN32 90 if (getenv("HOME") == NULL) 91 { 92 /* 93 * If there is no HOME environment variable, 94 * try the concatenation of HOMEDRIVE + HOMEPATH. 95 */ 96 char *drive = getenv("HOMEDRIVE"); 97 char *path = getenv("HOMEPATH"); 98 if (drive != NULL && path != NULL) 99 { 100 char *env = (char *) ecalloc(strlen(drive) + 101 strlen(path) + 6, sizeof(char)); 102 strcpy(env, "HOME="); 103 strcat(env, drive); 104 strcat(env, path); 105 putenv(env); 106 } 107 } 108 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char)); 109 #endif /* WIN32 */ 110 111 /* 112 * Process command line arguments and LESS environment arguments. 113 * Command line arguments override environment arguments. 114 */ 115 is_tty = isatty(1); 116 get_term(); 117 init_cmds(); 118 init_charset(); 119 init_line(); 120 init_cmdhist(); 121 init_option(); 122 init_search(); 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) 168 no_init = 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 free(filename); 220 #endif 221 } 222 /* 223 * Set up terminal, etc. 224 */ 225 if (!is_tty) 226 { 227 /* 228 * Output is not a tty. 229 * Just copy the input file(s) to output. 230 */ 231 SET_BINARY(1); 232 if (nifile() == 0) 233 { 234 if (edit_stdin() == 0) 235 cat_file(); 236 } else if (edit_first() == 0) 237 { 238 do { 239 cat_file(); 240 } while (edit_next(1) == 0); 241 } 242 quit(QUIT_OK); 243 } 244 245 if (missing_cap && !know_dumb && !less_is_more) 246 error("WARNING: terminal is not fully functional", NULL_PARG); 247 init_mark(); 248 open_getchr(); 249 raw_mode(1); 250 init_signals(1); 251 252 /* 253 * Select the first file to examine. 254 */ 255 #if TAGS 256 if (tagoption != NULL || strcmp(tags, "-") == 0) 257 { 258 /* 259 * A -t option was given. 260 * Verify that no filenames were also given. 261 * Edit the file selected by the "tags" search, 262 * and search for the proper line in the file. 263 */ 264 if (nifile() > 0) 265 { 266 error("No filenames allowed with -t option", NULL_PARG); 267 quit(QUIT_ERROR); 268 } 269 findtag(tagoption); 270 if (edit_tagfile()) /* Edit file which contains the tag */ 271 quit(QUIT_ERROR); 272 /* 273 * Search for the line which contains the tag. 274 * Set up initial_scrpos so we display that line. 275 */ 276 initial_scrpos.pos = tagsearch(); 277 if (initial_scrpos.pos == NULL_POSITION) 278 quit(QUIT_ERROR); 279 initial_scrpos.ln = jump_sline; 280 } else 281 #endif 282 if (nifile() == 0) 283 { 284 if (edit_stdin()) /* Edit standard input */ 285 quit(QUIT_ERROR); 286 } else 287 { 288 if (edit_first()) /* Edit first valid file in cmd line */ 289 quit(QUIT_ERROR); 290 } 291 292 init(); 293 commands(); 294 quit(QUIT_OK); 295 /*NOTREACHED*/ 296 return (0); 297 } 298 299 /* 300 * Copy a string to a "safe" place 301 * (that is, to a buffer allocated by calloc). 302 */ 303 public char * 304 save(constant char *s) 305 { 306 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 318 ecalloc(int count, unsigned int size) 319 { 320 VOID_POINTER p; 321 322 p = (VOID_POINTER) calloc(count, size); 323 if (p != NULL) 324 return (p); 325 error("Cannot allocate memory", NULL_PARG); 326 quit(QUIT_ERROR); 327 /*NOTREACHED*/ 328 return (NULL); 329 } 330 331 /* 332 * Skip leading spaces in a string. 333 */ 334 public char * 335 skipsp(char *s) 336 { 337 while (*s == ' ' || *s == '\t') 338 s++; 339 return (s); 340 } 341 342 /* 343 * See how many characters of two strings are identical. 344 * If uppercase is true, the first string must begin with an uppercase 345 * character; the remainder of the first string may be either case. 346 */ 347 public int 348 sprefix(char *ps, char *s, int uppercase) 349 { 350 int c; 351 int sc; 352 int len = 0; 353 354 for ( ; *s != '\0'; s++, ps++) 355 { 356 c = *ps; 357 if (uppercase) 358 { 359 if (len == 0 && ASCII_IS_LOWER(c)) 360 return (-1); 361 if (ASCII_IS_UPPER(c)) 362 c = ASCII_TO_LOWER(c); 363 } 364 sc = *s; 365 if (len > 0 && ASCII_IS_UPPER(sc)) 366 sc = ASCII_TO_LOWER(sc); 367 if (c != sc) 368 break; 369 len++; 370 } 371 return (len); 372 } 373 374 /* 375 * Exit the program. 376 */ 377 public void 378 quit(int status) 379 { 380 static int save_status; 381 382 /* 383 * Put cursor at bottom left corner, clear the line, 384 * reset the terminal modes, and exit. 385 */ 386 if (status < 0) 387 status = save_status; 388 else 389 save_status = status; 390 quitting = 1; 391 edit((char*)NULL); 392 save_cmdhist(); 393 if (any_display && is_tty) 394 clear_bot(); 395 deinit(); 396 flush(); 397 raw_mode(0); 398 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC 399 /* 400 * If we don't close 2, we get some garbage from 401 * 2's buffer when it flushes automatically. 402 * I cannot track this one down RB 403 * The same bug shows up if we use ^C^C to abort. 404 */ 405 close(2); 406 #endif 407 #ifdef WIN32 408 SetConsoleTitle(consoleTitle); 409 #endif 410 close_getchr(); 411 exit(status); 412 } 413