1*838f5788Ssimonb /* $NetBSD: main.c,v 1.5 2023/10/06 05:49:49 simonb Exp $ */
220006a0bStron
320006a0bStron /*
4*838f5788Ssimonb * Copyright (C) 1984-2023 Mark Nudelman
520006a0bStron *
620006a0bStron * You may distribute under the terms of either the GNU General Public
720006a0bStron * License or the Less License, as specified in the README file.
820006a0bStron *
9ec18bca0Stron * For more information, see the README file.
1020006a0bStron */
1120006a0bStron
1220006a0bStron
1320006a0bStron /*
1420006a0bStron * Entry point, initialization, miscellaneous routines.
1520006a0bStron */
1620006a0bStron
1720006a0bStron #include "less.h"
1820006a0bStron #if MSDOS_COMPILER==WIN32C
19*838f5788Ssimonb #define WIN32_LEAN_AND_MEAN
2020006a0bStron #include <windows.h>
2120006a0bStron #endif
2220006a0bStron
2320006a0bStron public char * every_first_cmd = NULL;
2420006a0bStron public int new_file;
2520006a0bStron public int is_tty;
2620006a0bStron public IFILE curr_ifile = NULL_IFILE;
2720006a0bStron public IFILE old_ifile = NULL_IFILE;
2820006a0bStron public struct scrpos initial_scrpos;
2920006a0bStron public POSITION start_attnpos = NULL_POSITION;
3020006a0bStron public POSITION end_attnpos = NULL_POSITION;
3120006a0bStron public int wscroll;
3220006a0bStron public char * progname;
3320006a0bStron public int quitting;
3420006a0bStron public int secure;
3520006a0bStron public int dohelp;
36824a88bbStron public int more_mode = 0;
3720006a0bStron
3820006a0bStron #if LOGFILE
3920006a0bStron public int logfile = -1;
4020006a0bStron public int force_logfile = FALSE;
4120006a0bStron public char * namelogfile = NULL;
4220006a0bStron #endif
4320006a0bStron
4420006a0bStron #if EDITOR
4520006a0bStron public char * editor;
4620006a0bStron public char * editproto;
4720006a0bStron #endif
4820006a0bStron
4920006a0bStron #if TAGS
5020006a0bStron extern char * tags;
5120006a0bStron extern char * tagoption;
5220006a0bStron extern int jump_sline;
5320006a0bStron #endif
5420006a0bStron
5520006a0bStron #ifdef WIN32
5620006a0bStron static char consoleTitle[256];
5720006a0bStron #endif
5820006a0bStron
59*838f5788Ssimonb public int one_screen;
6020006a0bStron extern int less_is_more;
6120006a0bStron extern int missing_cap;
6220006a0bStron extern int know_dumb;
6320006a0bStron extern int pr_type;
64*838f5788Ssimonb extern int quit_if_one_screen;
65*838f5788Ssimonb extern int no_init;
66*838f5788Ssimonb extern int errmsgs;
67*838f5788Ssimonb extern int redraw_on_quit;
68*838f5788Ssimonb extern int term_init_done;
69*838f5788Ssimonb extern int first_time;
7020006a0bStron
7120006a0bStron /*
7220006a0bStron * Entry point.
7320006a0bStron */
main(int argc,char * argv[])74*838f5788Ssimonb int main(int argc, char *argv[])
7520006a0bStron {
7620006a0bStron IFILE ifile;
7720006a0bStron char *s;
7820006a0bStron
7920006a0bStron #ifdef __EMX__
8020006a0bStron _response(&argc, &argv);
8120006a0bStron _wildcard(&argc, &argv);
8220006a0bStron #endif
8320006a0bStron
8420006a0bStron progname = *argv++;
8520006a0bStron argc--;
8620006a0bStron
87*838f5788Ssimonb #if SECURE
88*838f5788Ssimonb secure = 1;
89*838f5788Ssimonb #else
9020006a0bStron secure = 0;
9120006a0bStron s = lgetenv("LESSSECURE");
92*838f5788Ssimonb if (!isnullenv(s))
9320006a0bStron secure = 1;
94*838f5788Ssimonb #endif
9520006a0bStron
9620006a0bStron #ifdef WIN32
9720006a0bStron if (getenv("HOME") == NULL)
9820006a0bStron {
9920006a0bStron /*
10020006a0bStron * If there is no HOME environment variable,
10120006a0bStron * try the concatenation of HOMEDRIVE + HOMEPATH.
10220006a0bStron */
10320006a0bStron char *drive = getenv("HOMEDRIVE");
10420006a0bStron char *path = getenv("HOMEPATH");
10520006a0bStron if (drive != NULL && path != NULL)
10620006a0bStron {
10720006a0bStron char *env = (char *) ecalloc(strlen(drive) +
10820006a0bStron strlen(path) + 6, sizeof(char));
10920006a0bStron strcpy(env, "HOME=");
11020006a0bStron strcat(env, drive);
11120006a0bStron strcat(env, path);
11220006a0bStron putenv(env);
11320006a0bStron }
11420006a0bStron }
11520006a0bStron GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
11620006a0bStron #endif /* WIN32 */
11720006a0bStron
11820006a0bStron /*
11920006a0bStron * Process command line arguments and LESS environment arguments.
12020006a0bStron * Command line arguments override environment arguments.
12120006a0bStron */
122824a88bbStron if (strcmp(getprogname(), "more") == 0)
123824a88bbStron more_mode = 1;
124824a88bbStron
12520006a0bStron is_tty = isatty(1);
126*838f5788Ssimonb init_mark();
12720006a0bStron init_cmds();
128*838f5788Ssimonb init_poll();
129*838f5788Ssimonb get_term();
13020006a0bStron init_charset();
13120006a0bStron init_line();
13220006a0bStron init_cmdhist();
13320006a0bStron init_option();
13420006a0bStron init_search();
13520006a0bStron
13620006a0bStron /*
13720006a0bStron * If the name of the executable program is "more",
13820006a0bStron * act like LESS_IS_MORE is set.
13920006a0bStron */
140*838f5788Ssimonb s = last_component(progname);
14120006a0bStron if (strcmp(s, "more") == 0)
14220006a0bStron less_is_more = 1;
14320006a0bStron
14420006a0bStron init_prompt();
14520006a0bStron
14620006a0bStron s = lgetenv(less_is_more ? "MORE" : "LESS");
14720006a0bStron if (s != NULL)
148*838f5788Ssimonb scan_option(s);
14920006a0bStron
15020006a0bStron #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
15120006a0bStron while (argc > 0 && (isoptstring(*argv) || isoptpending()))
15220006a0bStron {
15320006a0bStron s = *argv++;
15420006a0bStron argc--;
15520006a0bStron if (strcmp(s, "--") == 0)
15620006a0bStron break;
15720006a0bStron scan_option(s);
15820006a0bStron }
15920006a0bStron #undef isoptstring
16020006a0bStron
16120006a0bStron if (isoptpending())
16220006a0bStron {
16320006a0bStron /*
16420006a0bStron * Last command line option was a flag requiring a
16520006a0bStron * following string, but there was no following string.
16620006a0bStron */
16720006a0bStron nopendopt();
16820006a0bStron quit(QUIT_OK);
16920006a0bStron }
17020006a0bStron
171*838f5788Ssimonb expand_cmd_tables();
17220006a0bStron
17320006a0bStron #if EDITOR
17420006a0bStron editor = lgetenv("VISUAL");
17520006a0bStron if (editor == NULL || *editor == '\0')
17620006a0bStron {
17720006a0bStron editor = lgetenv("EDITOR");
178*838f5788Ssimonb if (isnullenv(editor))
17920006a0bStron editor = EDIT_PGM;
18020006a0bStron }
18120006a0bStron editproto = lgetenv("LESSEDIT");
182*838f5788Ssimonb if (isnullenv(editproto))
183*838f5788Ssimonb editproto = "%E ?lm+%lm. %g";
18420006a0bStron #endif
18520006a0bStron
18620006a0bStron /*
18720006a0bStron * Call get_ifile with all the command line filenames
18820006a0bStron * to "register" them with the ifile system.
18920006a0bStron */
19020006a0bStron ifile = NULL_IFILE;
19120006a0bStron if (dohelp)
19220006a0bStron ifile = get_ifile(FAKE_HELPFILE, ifile);
19320006a0bStron while (argc-- > 0)
19420006a0bStron {
19520006a0bStron #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
19620006a0bStron /*
19720006a0bStron * Because the "shell" doesn't expand filename patterns,
19820006a0bStron * treat each argument as a filename pattern rather than
19920006a0bStron * a single filename.
20020006a0bStron * Expand the pattern and iterate over the expanded list.
20120006a0bStron */
20220006a0bStron struct textlist tlist;
203*838f5788Ssimonb char *filename;
20420006a0bStron char *gfilename;
205*838f5788Ssimonb char *qfilename;
20620006a0bStron
20720006a0bStron gfilename = lglob(*argv++);
20820006a0bStron init_textlist(&tlist, gfilename);
20920006a0bStron filename = NULL;
21020006a0bStron while ((filename = forw_textlist(&tlist, filename)) != NULL)
21120006a0bStron {
212*838f5788Ssimonb qfilename = shell_unquote(filename);
213*838f5788Ssimonb (void) get_ifile(qfilename, ifile);
214*838f5788Ssimonb free(qfilename);
21520006a0bStron ifile = prev_ifile(NULL_IFILE);
21620006a0bStron }
21720006a0bStron free(gfilename);
21820006a0bStron #else
219*838f5788Ssimonb (void) get_ifile(*argv++, ifile);
22020006a0bStron ifile = prev_ifile(NULL_IFILE);
22120006a0bStron #endif
22220006a0bStron }
22320006a0bStron /*
22420006a0bStron * Set up terminal, etc.
22520006a0bStron */
22620006a0bStron if (!is_tty)
22720006a0bStron {
22820006a0bStron /*
22920006a0bStron * Output is not a tty.
23020006a0bStron * Just copy the input file(s) to output.
23120006a0bStron */
232*838f5788Ssimonb set_output(1); /* write to stdout */
23320006a0bStron SET_BINARY(1);
234*838f5788Ssimonb if (edit_first() == 0)
23520006a0bStron {
23620006a0bStron do {
23720006a0bStron cat_file();
23820006a0bStron } while (edit_next(1) == 0);
23920006a0bStron }
24020006a0bStron quit(QUIT_OK);
24120006a0bStron }
24220006a0bStron
243824a88bbStron if (missing_cap && !know_dumb && !more_mode)
24420006a0bStron error("WARNING: terminal is not fully functional", NULL_PARG);
24520006a0bStron open_getchr();
24620006a0bStron raw_mode(1);
24720006a0bStron init_signals(1);
24820006a0bStron
24920006a0bStron /*
25020006a0bStron * Select the first file to examine.
25120006a0bStron */
25220006a0bStron #if TAGS
25320006a0bStron if (tagoption != NULL || strcmp(tags, "-") == 0)
25420006a0bStron {
25520006a0bStron /*
25620006a0bStron * A -t option was given.
25720006a0bStron * Verify that no filenames were also given.
25820006a0bStron * Edit the file selected by the "tags" search,
25920006a0bStron * and search for the proper line in the file.
26020006a0bStron */
26120006a0bStron if (nifile() > 0)
26220006a0bStron {
26320006a0bStron error("No filenames allowed with -t option", NULL_PARG);
26420006a0bStron quit(QUIT_ERROR);
26520006a0bStron }
26620006a0bStron findtag(tagoption);
26720006a0bStron if (edit_tagfile()) /* Edit file which contains the tag */
26820006a0bStron quit(QUIT_ERROR);
26920006a0bStron /*
27020006a0bStron * Search for the line which contains the tag.
27120006a0bStron * Set up initial_scrpos so we display that line.
27220006a0bStron */
27320006a0bStron initial_scrpos.pos = tagsearch();
27420006a0bStron if (initial_scrpos.pos == NULL_POSITION)
27520006a0bStron quit(QUIT_ERROR);
27620006a0bStron initial_scrpos.ln = jump_sline;
27720006a0bStron } else
27820006a0bStron #endif
27920006a0bStron {
280*838f5788Ssimonb if (edit_first())
28120006a0bStron quit(QUIT_ERROR);
282*838f5788Ssimonb /*
283*838f5788Ssimonb * See if file fits on one screen to decide whether
284*838f5788Ssimonb * to send terminal init. But don't need this
285*838f5788Ssimonb * if -X (no_init) overrides this (see init()).
286*838f5788Ssimonb */
287*838f5788Ssimonb if (quit_if_one_screen)
28820006a0bStron {
289*838f5788Ssimonb if (nifile() > 1) /* If more than one file, -F cannot be used */
290*838f5788Ssimonb quit_if_one_screen = FALSE;
291*838f5788Ssimonb else if (!no_init)
292*838f5788Ssimonb one_screen = get_one_screen();
293*838f5788Ssimonb }
29420006a0bStron }
29520006a0bStron
296*838f5788Ssimonb if (errmsgs > 0)
297*838f5788Ssimonb {
298*838f5788Ssimonb /*
299*838f5788Ssimonb * We displayed some messages on error output
300*838f5788Ssimonb * (file descriptor 2; see flush()).
301*838f5788Ssimonb * Before erasing the screen contents, wait for a keystroke.
302*838f5788Ssimonb */
303*838f5788Ssimonb less_printf("Press RETURN to continue ", NULL_PARG);
304*838f5788Ssimonb get_return();
305*838f5788Ssimonb putchr('\n');
306*838f5788Ssimonb }
307*838f5788Ssimonb set_output(1);
30820006a0bStron init();
30920006a0bStron commands();
31020006a0bStron quit(QUIT_OK);
31120006a0bStron /*NOTREACHED*/
31220006a0bStron return (0);
31320006a0bStron }
31420006a0bStron
31520006a0bStron /*
31620006a0bStron * Copy a string to a "safe" place
31720006a0bStron * (that is, to a buffer allocated by calloc).
31820006a0bStron */
save(constant char * s)319*838f5788Ssimonb public char * save(constant char *s)
32020006a0bStron {
321*838f5788Ssimonb char *p;
32220006a0bStron
32320006a0bStron p = (char *) ecalloc(strlen(s)+1, sizeof(char));
32420006a0bStron strcpy(p, s);
32520006a0bStron return (p);
32620006a0bStron }
32720006a0bStron
out_of_memory(void)328*838f5788Ssimonb public void out_of_memory(void)
329*838f5788Ssimonb {
330*838f5788Ssimonb error("Cannot allocate memory", NULL_PARG);
331*838f5788Ssimonb quit(QUIT_ERROR);
332*838f5788Ssimonb }
333*838f5788Ssimonb
33420006a0bStron /*
33520006a0bStron * Allocate memory.
33620006a0bStron * Like calloc(), but never returns an error (NULL).
33720006a0bStron */
ecalloc(int count,unsigned int size)338*838f5788Ssimonb public void * ecalloc(int count, unsigned int size)
33920006a0bStron {
340*838f5788Ssimonb void * p;
34120006a0bStron
342*838f5788Ssimonb p = (void *) calloc(count, size);
343*838f5788Ssimonb if (p == NULL)
344*838f5788Ssimonb out_of_memory();
345*838f5788Ssimonb return p;
34620006a0bStron }
34720006a0bStron
34820006a0bStron /*
34920006a0bStron * Skip leading spaces in a string.
35020006a0bStron */
skipsp(char * s)351*838f5788Ssimonb public char * skipsp(char *s)
35220006a0bStron {
35320006a0bStron while (*s == ' ' || *s == '\t')
35420006a0bStron s++;
35520006a0bStron return (s);
35620006a0bStron }
35720006a0bStron
35820006a0bStron /*
35920006a0bStron * See how many characters of two strings are identical.
36020006a0bStron * If uppercase is true, the first string must begin with an uppercase
36120006a0bStron * character; the remainder of the first string may be either case.
36220006a0bStron */
sprefix(char * ps,char * s,int uppercase)363*838f5788Ssimonb public int sprefix(char *ps, char *s, int uppercase)
36420006a0bStron {
365*838f5788Ssimonb int c;
366*838f5788Ssimonb int sc;
367*838f5788Ssimonb int len = 0;
36820006a0bStron
36920006a0bStron for ( ; *s != '\0'; s++, ps++)
37020006a0bStron {
37120006a0bStron c = *ps;
37220006a0bStron if (uppercase)
37320006a0bStron {
37420006a0bStron if (len == 0 && ASCII_IS_LOWER(c))
37520006a0bStron return (-1);
37620006a0bStron if (ASCII_IS_UPPER(c))
37720006a0bStron c = ASCII_TO_LOWER(c);
37820006a0bStron }
37920006a0bStron sc = *s;
38020006a0bStron if (len > 0 && ASCII_IS_UPPER(sc))
38120006a0bStron sc = ASCII_TO_LOWER(sc);
38220006a0bStron if (c != sc)
38320006a0bStron break;
38420006a0bStron len++;
38520006a0bStron }
38620006a0bStron return (len);
38720006a0bStron }
38820006a0bStron
38920006a0bStron /*
39020006a0bStron * Exit the program.
39120006a0bStron */
quit(int status)392*838f5788Ssimonb public void quit(int status)
39320006a0bStron {
39420006a0bStron static int save_status;
39520006a0bStron
39620006a0bStron /*
39720006a0bStron * Put cursor at bottom left corner, clear the line,
39820006a0bStron * reset the terminal modes, and exit.
39920006a0bStron */
40020006a0bStron if (status < 0)
40120006a0bStron status = save_status;
40220006a0bStron else
40320006a0bStron save_status = status;
40420006a0bStron quitting = 1;
405*838f5788Ssimonb check_altpipe_error();
406*838f5788Ssimonb if (interactive())
40720006a0bStron clear_bot();
40820006a0bStron deinit();
40920006a0bStron flush();
410*838f5788Ssimonb if (redraw_on_quit && term_init_done)
411*838f5788Ssimonb {
412*838f5788Ssimonb /*
413*838f5788Ssimonb * The last file text displayed might have been on an
414*838f5788Ssimonb * alternate screen, which now (since deinit) cannot be seen.
415*838f5788Ssimonb * redraw_on_quit tells us to redraw it on the main screen.
416*838f5788Ssimonb */
417*838f5788Ssimonb first_time = 1; /* Don't print "skipping" or tildes */
418*838f5788Ssimonb repaint();
419*838f5788Ssimonb flush();
420*838f5788Ssimonb }
421*838f5788Ssimonb edit((char*)NULL);
422*838f5788Ssimonb save_cmdhist();
42320006a0bStron raw_mode(0);
42420006a0bStron #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
42520006a0bStron /*
42620006a0bStron * If we don't close 2, we get some garbage from
42720006a0bStron * 2's buffer when it flushes automatically.
42820006a0bStron * I cannot track this one down RB
42920006a0bStron * The same bug shows up if we use ^C^C to abort.
43020006a0bStron */
43120006a0bStron close(2);
43220006a0bStron #endif
43320006a0bStron #ifdef WIN32
43420006a0bStron SetConsoleTitle(consoleTitle);
43520006a0bStron #endif
43620006a0bStron close_getchr();
43720006a0bStron exit(status);
43820006a0bStron }
439