1 /* 2 * Copyright (C) 1984-2019 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 #define WIN32_LEAN_AND_MEAN 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 public int one_screen; 58 extern int less_is_more; 59 extern int missing_cap; 60 extern int know_dumb; 61 extern int pr_type; 62 extern int quit_if_one_screen; 63 extern int no_init; 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 77 #ifdef __EMX__ 78 _response(&argc, &argv); 79 _wildcard(&argc, &argv); 80 #endif 81 82 progname = *argv++; 83 argc--; 84 85 secure = 0; 86 s = lgetenv("LESSSECURE"); 87 if (!isnullenv(s)) 88 secure = 1; 89 90 #ifdef WIN32 91 if (getenv("HOME") == NULL) 92 { 93 /* 94 * If there is no HOME environment variable, 95 * try the concatenation of HOMEDRIVE + HOMEPATH. 96 */ 97 char *drive = getenv("HOMEDRIVE"); 98 char *path = getenv("HOMEPATH"); 99 if (drive != NULL && path != NULL) 100 { 101 char *env = (char *) ecalloc(strlen(drive) + 102 strlen(path) + 6, sizeof(char)); 103 strcpy(env, "HOME="); 104 strcat(env, drive); 105 strcat(env, path); 106 putenv(env); 107 } 108 } 109 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char)); 110 #endif /* WIN32 */ 111 112 /* 113 * Process command line arguments and LESS environment arguments. 114 * Command line arguments override environment arguments. 115 */ 116 is_tty = isatty(1); 117 init_mark(); 118 init_cmds(); 119 get_term(); 120 expand_cmd_tables(); 121 init_charset(); 122 init_line(); 123 init_cmdhist(); 124 init_option(); 125 init_search(); 126 127 /* 128 * If the name of the executable program is "more", 129 * act like LESS_IS_MORE is set. 130 */ 131 s = last_component(progname); 132 if (strcmp(s, "more") == 0) 133 less_is_more = 1; 134 135 init_prompt(); 136 137 s = lgetenv(less_is_more ? "MORE" : "LESS"); 138 if (s != NULL) 139 scan_option(save(s)); 140 141 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0') 142 while (argc > 0 && (isoptstring(*argv) || isoptpending())) 143 { 144 s = *argv++; 145 argc--; 146 if (strcmp(s, "--") == 0) 147 break; 148 scan_option(s); 149 } 150 #undef isoptstring 151 152 if (isoptpending()) 153 { 154 /* 155 * Last command line option was a flag requiring a 156 * following string, but there was no following string. 157 */ 158 nopendopt(); 159 quit(QUIT_OK); 160 } 161 162 #if EDITOR 163 editor = lgetenv("VISUAL"); 164 if (editor == NULL || *editor == '\0') 165 { 166 editor = lgetenv("EDITOR"); 167 if (isnullenv(editor)) 168 editor = EDIT_PGM; 169 } 170 editproto = lgetenv("LESSEDIT"); 171 if (isnullenv(editproto)) 172 editproto = "%E ?lm+%lm. %g"; 173 #endif 174 175 /* 176 * Call get_ifile with all the command line filenames 177 * to "register" them with the ifile system. 178 */ 179 ifile = NULL_IFILE; 180 if (dohelp) 181 ifile = get_ifile(FAKE_HELPFILE, ifile); 182 while (argc-- > 0) 183 { 184 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC) 185 /* 186 * Because the "shell" doesn't expand filename patterns, 187 * treat each argument as a filename pattern rather than 188 * a single filename. 189 * Expand the pattern and iterate over the expanded list. 190 */ 191 struct textlist tlist; 192 char *filename; 193 char *gfilename; 194 char *qfilename; 195 196 gfilename = lglob(*argv++); 197 init_textlist(&tlist, gfilename); 198 filename = NULL; 199 while ((filename = forw_textlist(&tlist, filename)) != NULL) 200 { 201 qfilename = shell_unquote(filename); 202 (void) get_ifile(qfilename, ifile); 203 free(qfilename); 204 ifile = prev_ifile(NULL_IFILE); 205 } 206 free(gfilename); 207 #else 208 (void) get_ifile(*argv++, ifile); 209 ifile = prev_ifile(NULL_IFILE); 210 #endif 211 } 212 /* 213 * Set up terminal, etc. 214 */ 215 if (!is_tty) 216 { 217 /* 218 * Output is not a tty. 219 * Just copy the input file(s) to output. 220 */ 221 SET_BINARY(1); 222 if (edit_first() == 0) 223 { 224 do { 225 cat_file(); 226 } while (edit_next(1) == 0); 227 } 228 quit(QUIT_OK); 229 } 230 231 if (missing_cap && !know_dumb) 232 error("WARNING: terminal is not fully functional", NULL_PARG); 233 open_getchr(); 234 raw_mode(1); 235 init_signals(1); 236 237 /* 238 * Select the first file to examine. 239 */ 240 #if TAGS 241 if (tagoption != NULL || strcmp(tags, "-") == 0) 242 { 243 /* 244 * A -t option was given. 245 * Verify that no filenames were also given. 246 * Edit the file selected by the "tags" search, 247 * and search for the proper line in the file. 248 */ 249 if (nifile() > 0) 250 { 251 error("No filenames allowed with -t option", NULL_PARG); 252 quit(QUIT_ERROR); 253 } 254 findtag(tagoption); 255 if (edit_tagfile()) /* Edit file which contains the tag */ 256 quit(QUIT_ERROR); 257 /* 258 * Search for the line which contains the tag. 259 * Set up initial_scrpos so we display that line. 260 */ 261 initial_scrpos.pos = tagsearch(); 262 if (initial_scrpos.pos == NULL_POSITION) 263 quit(QUIT_ERROR); 264 initial_scrpos.ln = jump_sline; 265 } else 266 #endif 267 { 268 if (edit_first()) 269 quit(QUIT_ERROR); 270 /* 271 * See if file fits on one screen to decide whether 272 * to send terminal init. But don't need this 273 * if -X (no_init) overrides this (see init()). 274 */ 275 if (quit_if_one_screen) 276 { 277 if (nifile() > 1) /* If more than one file, -F cannot be used */ 278 quit_if_one_screen = FALSE; 279 else if (!no_init) 280 one_screen = get_one_screen(); 281 } 282 } 283 284 init(); 285 commands(); 286 quit(QUIT_OK); 287 /*NOTREACHED*/ 288 return (0); 289 } 290 291 /* 292 * Copy a string to a "safe" place 293 * (that is, to a buffer allocated by calloc). 294 */ 295 public char * 296 save(s) 297 constant char *s; 298 { 299 char *p; 300 301 p = (char *) ecalloc(strlen(s)+1, sizeof(char)); 302 strcpy(p, s); 303 return (p); 304 } 305 306 /* 307 * Allocate memory. 308 * Like calloc(), but never returns an error (NULL). 309 */ 310 public VOID_POINTER 311 ecalloc(count, size) 312 int count; 313 unsigned int size; 314 { 315 VOID_POINTER p; 316 317 p = (VOID_POINTER) calloc(count, size); 318 if (p != NULL) 319 return (p); 320 error("Cannot allocate memory", NULL_PARG); 321 quit(QUIT_ERROR); 322 /*NOTREACHED*/ 323 return (NULL); 324 } 325 326 /* 327 * Skip leading spaces in a string. 328 */ 329 public char * 330 skipsp(s) 331 char *s; 332 { 333 while (*s == ' ' || *s == '\t') 334 s++; 335 return (s); 336 } 337 338 /* 339 * See how many characters of two strings are identical. 340 * If uppercase is true, the first string must begin with an uppercase 341 * character; the remainder of the first string may be either case. 342 */ 343 public int 344 sprefix(ps, s, uppercase) 345 char *ps; 346 char *s; 347 int uppercase; 348 { 349 int c; 350 int sc; 351 int len = 0; 352 353 for ( ; *s != '\0'; s++, ps++) 354 { 355 c = *ps; 356 if (uppercase) 357 { 358 if (len == 0 && ASCII_IS_LOWER(c)) 359 return (-1); 360 if (ASCII_IS_UPPER(c)) 361 c = ASCII_TO_LOWER(c); 362 } 363 sc = *s; 364 if (len > 0 && ASCII_IS_UPPER(sc)) 365 sc = ASCII_TO_LOWER(sc); 366 if (c != sc) 367 break; 368 len++; 369 } 370 return (len); 371 } 372 373 /* 374 * Exit the program. 375 */ 376 public void 377 quit(status) 378 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