1 /* $FreeBSD$ */ 2 /* 3 * Copyright (C) 1984-2020 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 #define WIN32_LEAN_AND_MEAN 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 public int one_screen; 59 extern int less_is_more; 60 extern int missing_cap; 61 extern int know_dumb; 62 extern int no_init; 63 extern int pr_type; 64 extern int quit_if_one_screen; 65 extern int no_init; 66 67 68 /* 69 * Entry point. 70 */ 71 int 72 main(argc, argv) 73 int argc; 74 char *argv[]; 75 { 76 IFILE ifile; 77 char *s; 78 79 #ifdef __EMX__ 80 _response(&argc, &argv); 81 _wildcard(&argc, &argv); 82 #endif 83 84 progname = *argv++; 85 argc--; 86 87 secure = 0; 88 s = lgetenv("LESSSECURE"); 89 if (!isnullenv(s)) 90 secure = 1; 91 92 #ifdef WIN32 93 if (getenv("HOME") == NULL) 94 { 95 /* 96 * If there is no HOME environment variable, 97 * try the concatenation of HOMEDRIVE + HOMEPATH. 98 */ 99 char *drive = getenv("HOMEDRIVE"); 100 char *path = getenv("HOMEPATH"); 101 if (drive != NULL && path != NULL) 102 { 103 char *env = (char *) ecalloc(strlen(drive) + 104 strlen(path) + 6, sizeof(char)); 105 strcpy(env, "HOME="); 106 strcat(env, drive); 107 strcat(env, path); 108 putenv(env); 109 } 110 } 111 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char)); 112 #endif /* WIN32 */ 113 114 /* 115 * Process command line arguments and LESS environment arguments. 116 * Command line arguments override environment arguments. 117 */ 118 is_tty = isatty(1); 119 init_mark(); 120 init_cmds(); 121 get_term(); 122 init_charset(); 123 init_line(); 124 init_cmdhist(); 125 init_option(); 126 init_search(); 127 128 /* 129 * If the name of the executable program is "more", 130 * act like LESS_IS_MORE is set. 131 */ 132 s = last_component(progname); 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 expand_cmd_tables(); 171 172 #if EDITOR 173 editor = lgetenv("VISUAL"); 174 if (editor == NULL || *editor == '\0') 175 { 176 editor = lgetenv("EDITOR"); 177 if (isnullenv(editor)) 178 editor = EDIT_PGM; 179 } 180 editproto = lgetenv("LESSEDIT"); 181 if (isnullenv(editproto)) 182 editproto = "%E ?lm+%lm. %g"; 183 #endif 184 185 /* 186 * Call get_ifile with all the command line filenames 187 * to "register" them with the ifile system. 188 */ 189 ifile = NULL_IFILE; 190 if (dohelp) 191 ifile = get_ifile(FAKE_HELPFILE, ifile); 192 while (argc-- > 0) 193 { 194 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC) 195 /* 196 * Because the "shell" doesn't expand filename patterns, 197 * treat each argument as a filename pattern rather than 198 * a single filename. 199 * Expand the pattern and iterate over the expanded list. 200 */ 201 struct textlist tlist; 202 char *filename; 203 char *gfilename; 204 char *qfilename; 205 206 gfilename = lglob(*argv++); 207 init_textlist(&tlist, gfilename); 208 filename = NULL; 209 while ((filename = forw_textlist(&tlist, filename)) != NULL) 210 { 211 qfilename = shell_unquote(filename); 212 (void) get_ifile(qfilename, ifile); 213 free(qfilename); 214 ifile = prev_ifile(NULL_IFILE); 215 } 216 free(gfilename); 217 #else 218 (void) get_ifile(*argv++, ifile); 219 ifile = prev_ifile(NULL_IFILE); 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 (edit_first() == 0) 233 { 234 do { 235 cat_file(); 236 } while (edit_next(1) == 0); 237 } 238 quit(QUIT_OK); 239 } 240 241 if (missing_cap && !know_dumb && !less_is_more) 242 error("WARNING: terminal is not fully functional", NULL_PARG); 243 open_getchr(); 244 raw_mode(1); 245 init_signals(1); 246 247 /* 248 * Select the first file to examine. 249 */ 250 #if TAGS 251 if (tagoption != NULL || strcmp(tags, "-") == 0) 252 { 253 /* 254 * A -t option was given. 255 * Verify that no filenames were also given. 256 * Edit the file selected by the "tags" search, 257 * and search for the proper line in the file. 258 */ 259 if (nifile() > 0) 260 { 261 error("No filenames allowed with -t option", NULL_PARG); 262 quit(QUIT_ERROR); 263 } 264 findtag(tagoption); 265 if (edit_tagfile()) /* Edit file which contains the tag */ 266 quit(QUIT_ERROR); 267 /* 268 * Search for the line which contains the tag. 269 * Set up initial_scrpos so we display that line. 270 */ 271 initial_scrpos.pos = tagsearch(); 272 if (initial_scrpos.pos == NULL_POSITION) 273 quit(QUIT_ERROR); 274 initial_scrpos.ln = jump_sline; 275 } else 276 #endif 277 { 278 if (edit_first()) 279 quit(QUIT_ERROR); 280 /* 281 * See if file fits on one screen to decide whether 282 * to send terminal init. But don't need this 283 * if -X (no_init) overrides this (see init()). 284 */ 285 if (quit_if_one_screen) 286 { 287 if (nifile() > 1) /* If more than one file, -F cannot be used */ 288 quit_if_one_screen = FALSE; 289 else if (!no_init) 290 one_screen = get_one_screen(); 291 } 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 constant char *s; 308 { 309 char *p; 310 311 p = (char *) ecalloc(strlen(s)+1, sizeof(char)); 312 strcpy(p, s); 313 return (p); 314 } 315 316 /* 317 * Allocate memory. 318 * Like calloc(), but never returns an error (NULL). 319 */ 320 public VOID_POINTER 321 ecalloc(count, size) 322 int count; 323 unsigned int size; 324 { 325 VOID_POINTER p; 326 327 p = (VOID_POINTER) calloc(count, size); 328 if (p != NULL) 329 return (p); 330 error("Cannot allocate memory", NULL_PARG); 331 quit(QUIT_ERROR); 332 /*NOTREACHED*/ 333 return (NULL); 334 } 335 336 /* 337 * Skip leading spaces in a string. 338 */ 339 public char * 340 skipsp(s) 341 char *s; 342 { 343 while (*s == ' ' || *s == '\t') 344 s++; 345 return (s); 346 } 347 348 /* 349 * See how many characters of two strings are identical. 350 * If uppercase is true, the first string must begin with an uppercase 351 * character; the remainder of the first string may be either case. 352 */ 353 public int 354 sprefix(ps, s, uppercase) 355 char *ps; 356 char *s; 357 int uppercase; 358 { 359 int c; 360 int sc; 361 int len = 0; 362 363 for ( ; *s != '\0'; s++, ps++) 364 { 365 c = *ps; 366 if (uppercase) 367 { 368 if (len == 0 && ASCII_IS_LOWER(c)) 369 return (-1); 370 if (ASCII_IS_UPPER(c)) 371 c = ASCII_TO_LOWER(c); 372 } 373 sc = *s; 374 if (len > 0 && ASCII_IS_UPPER(sc)) 375 sc = ASCII_TO_LOWER(sc); 376 if (c != sc) 377 break; 378 len++; 379 } 380 return (len); 381 } 382 383 /* 384 * Exit the program. 385 */ 386 public void 387 quit(status) 388 int status; 389 { 390 static int save_status; 391 392 /* 393 * Put cursor at bottom left corner, clear the line, 394 * reset the terminal modes, and exit. 395 */ 396 if (status < 0) 397 status = save_status; 398 else 399 save_status = status; 400 quitting = 1; 401 edit((char*)NULL); 402 save_cmdhist(); 403 if (any_display && is_tty) 404 clear_bot(); 405 deinit(); 406 flush(); 407 raw_mode(0); 408 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC 409 /* 410 * If we don't close 2, we get some garbage from 411 * 2's buffer when it flushes automatically. 412 * I cannot track this one down RB 413 * The same bug shows up if we use ^C^C to abort. 414 */ 415 close(2); 416 #endif 417 #ifdef WIN32 418 SetConsoleTitle(consoleTitle); 419 #endif 420 close_getchr(); 421 exit(status); 422 } 423