Home | History | Annotate | Line # | Download | only in more
main.c revision 1.3
      1  1.3  perry /*	$NetBSD: main.c,v 1.3 1998/01/09 08:03:30 perry Exp $	*/
      2  1.3  perry 
      3  1.1    cjs /*
      4  1.1    cjs  * Copyright (c) 1988 Mark Nudleman
      5  1.1    cjs  * Copyright (c) 1988, 1993
      6  1.1    cjs  *	Regents of the University of California.  All rights reserved.
      7  1.1    cjs  *
      8  1.1    cjs  * Redistribution and use in source and binary forms, with or without
      9  1.1    cjs  * modification, are permitted provided that the following conditions
     10  1.1    cjs  * are met:
     11  1.1    cjs  * 1. Redistributions of source code must retain the above copyright
     12  1.1    cjs  *    notice, this list of conditions and the following disclaimer.
     13  1.1    cjs  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1    cjs  *    notice, this list of conditions and the following disclaimer in the
     15  1.1    cjs  *    documentation and/or other materials provided with the distribution.
     16  1.1    cjs  * 3. All advertising materials mentioning features or use of this software
     17  1.1    cjs  *    must display the following acknowledgement:
     18  1.1    cjs  *	This product includes software developed by the University of
     19  1.1    cjs  *	California, Berkeley and its contributors.
     20  1.1    cjs  * 4. Neither the name of the University nor the names of its contributors
     21  1.1    cjs  *    may be used to endorse or promote products derived from this software
     22  1.1    cjs  *    without specific prior written permission.
     23  1.1    cjs  *
     24  1.1    cjs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1    cjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1    cjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1    cjs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1    cjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1    cjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1    cjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1    cjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1    cjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1    cjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1    cjs  * SUCH DAMAGE.
     35  1.1    cjs  */
     36  1.1    cjs 
     37  1.1    cjs #ifndef lint
     38  1.1    cjs char copyright[] =
     39  1.1    cjs "@(#) Copyright (c) 1988 Mark Nudleman.\n\
     40  1.1    cjs @(#) Copyright (c) 1988, 1993
     41  1.1    cjs 	Regents of the University of California.  All rights reserved.\n";
     42  1.1    cjs #endif /* not lint */
     43  1.1    cjs 
     44  1.1    cjs #ifndef lint
     45  1.1    cjs static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/7/93";
     46  1.1    cjs #endif /* not lint */
     47  1.1    cjs 
     48  1.1    cjs /*
     49  1.1    cjs  * Entry point, initialization, miscellaneous routines.
     50  1.1    cjs  */
     51  1.1    cjs 
     52  1.1    cjs #include <sys/types.h>
     53  1.1    cjs #include <sys/file.h>
     54  1.1    cjs #include <stdio.h>
     55  1.2    cjs #include <string.h>
     56  1.1    cjs #include <less.h>
     57  1.1    cjs 
     58  1.1    cjs int	ispipe;
     59  1.1    cjs int	new_file;
     60  1.1    cjs int	is_tty;
     61  1.1    cjs char	*current_file, *previous_file, *current_name, *next_name;
     62  1.1    cjs off_t	prev_pos;
     63  1.1    cjs int	any_display;
     64  1.1    cjs int	scroll;
     65  1.1    cjs int	ac;
     66  1.1    cjs char	**av;
     67  1.1    cjs int	curr_ac;
     68  1.1    cjs int	quitting;
     69  1.1    cjs 
     70  1.1    cjs extern int	file;
     71  1.1    cjs extern int	cbufs;
     72  1.1    cjs extern int	errmsgs;
     73  1.1    cjs 
     74  1.1    cjs /*
     75  1.1    cjs  * Edit a new file.
     76  1.1    cjs  * Filename "-" means standard input.
     77  1.1    cjs  * No filename means the "current" file, from the command line.
     78  1.1    cjs  */
     79  1.1    cjs edit(filename)
     80  1.1    cjs 	register char *filename;
     81  1.1    cjs {
     82  1.1    cjs 	extern int errno;
     83  1.1    cjs 	register int f;
     84  1.1    cjs 	register char *m;
     85  1.1    cjs 	off_t initial_pos, position();
     86  1.1    cjs 	static int didpipe;
     87  1.1    cjs 	char message[100], *p;
     88  1.1    cjs 	char *rindex(), *strerror(), *save(), *bad_file();
     89  1.1    cjs 
     90  1.1    cjs 	initial_pos = NULL_POSITION;
     91  1.1    cjs 	if (filename == NULL || *filename == '\0') {
     92  1.1    cjs 		if (curr_ac >= ac) {
     93  1.1    cjs 			error("No current file");
     94  1.1    cjs 			return(0);
     95  1.1    cjs 		}
     96  1.1    cjs 		filename = save(av[curr_ac]);
     97  1.1    cjs 	}
     98  1.1    cjs 	else if (strcmp(filename, "#") == 0) {
     99  1.1    cjs 		if (*previous_file == '\0') {
    100  1.1    cjs 			error("no previous file");
    101  1.1    cjs 			return(0);
    102  1.1    cjs 		}
    103  1.1    cjs 		filename = save(previous_file);
    104  1.1    cjs 		initial_pos = prev_pos;
    105  1.1    cjs 	} else
    106  1.1    cjs 		filename = save(filename);
    107  1.1    cjs 
    108  1.1    cjs 	/* use standard input. */
    109  1.1    cjs 	if (!strcmp(filename, "-")) {
    110  1.1    cjs 		if (didpipe) {
    111  1.1    cjs 			error("Can view standard input only once");
    112  1.1    cjs 			return(0);
    113  1.1    cjs 		}
    114  1.1    cjs 		f = 0;
    115  1.1    cjs 	}
    116  1.1    cjs 	else if ((m = bad_file(filename, message, sizeof(message))) != NULL) {
    117  1.1    cjs 		error(m);
    118  1.1    cjs 		free(filename);
    119  1.1    cjs 		return(0);
    120  1.1    cjs 	}
    121  1.1    cjs 	else if ((f = open(filename, O_RDONLY, 0)) < 0) {
    122  1.1    cjs 		(void)sprintf(message, "%s: %s", filename, strerror(errno));
    123  1.1    cjs 		error(message);
    124  1.1    cjs 		free(filename);
    125  1.1    cjs 		return(0);
    126  1.1    cjs 	}
    127  1.1    cjs 
    128  1.1    cjs 	if (isatty(f)) {
    129  1.1    cjs 		/*
    130  1.1    cjs 		 * Not really necessary to call this an error,
    131  1.1    cjs 		 * but if the control terminal (for commands)
    132  1.1    cjs 		 * and the input file (for data) are the same,
    133  1.1    cjs 		 * we get weird results at best.
    134  1.1    cjs 		 */
    135  1.1    cjs 		error("Can't take input from a terminal");
    136  1.1    cjs 		if (f > 0)
    137  1.1    cjs 			(void)close(f);
    138  1.1    cjs 		(void)free(filename);
    139  1.1    cjs 		return(0);
    140  1.1    cjs 	}
    141  1.1    cjs 
    142  1.1    cjs 	/*
    143  1.1    cjs 	 * We are now committed to using the new file.
    144  1.1    cjs 	 * Close the current input file and set up to use the new one.
    145  1.1    cjs 	 */
    146  1.1    cjs 	if (file > 0)
    147  1.1    cjs 		(void)close(file);
    148  1.1    cjs 	new_file = 1;
    149  1.1    cjs 	if (previous_file != NULL)
    150  1.1    cjs 		free(previous_file);
    151  1.1    cjs 	previous_file = current_file;
    152  1.1    cjs 	current_file = filename;
    153  1.1    cjs 	pos_clear();
    154  1.1    cjs 	prev_pos = position(TOP);
    155  1.1    cjs 	ispipe = (f == 0);
    156  1.1    cjs 	if (ispipe) {
    157  1.1    cjs 		didpipe = 1;
    158  1.1    cjs 		current_name = "stdin";
    159  1.1    cjs 	} else
    160  1.1    cjs 		current_name = (p = rindex(filename, '/')) ? p + 1 : filename;
    161  1.1    cjs 	if (curr_ac >= ac)
    162  1.1    cjs 		next_name = NULL;
    163  1.1    cjs 	else
    164  1.1    cjs 		next_name = av[curr_ac + 1];
    165  1.1    cjs 	file = f;
    166  1.1    cjs 	ch_init(cbufs, 0);
    167  1.1    cjs 	init_mark();
    168  1.1    cjs 
    169  1.1    cjs 	if (is_tty) {
    170  1.1    cjs 		int no_display = !any_display;
    171  1.1    cjs 		any_display = 1;
    172  1.1    cjs 		if (no_display && errmsgs > 0) {
    173  1.1    cjs 			/*
    174  1.1    cjs 			 * We displayed some messages on error output
    175  1.1    cjs 			 * (file descriptor 2; see error() function).
    176  1.1    cjs 			 * Before erasing the screen contents,
    177  1.1    cjs 			 * display the file name and wait for a keystroke.
    178  1.1    cjs 			 */
    179  1.1    cjs 			error(filename);
    180  1.1    cjs 		}
    181  1.1    cjs 		/*
    182  1.1    cjs 		 * Indicate there is nothing displayed yet.
    183  1.1    cjs 		 */
    184  1.1    cjs 		if (initial_pos != NULL_POSITION)
    185  1.1    cjs 			jump_loc(initial_pos);
    186  1.1    cjs 		clr_linenum();
    187  1.1    cjs 	}
    188  1.1    cjs 	return(1);
    189  1.1    cjs }
    190  1.1    cjs 
    191  1.1    cjs /*
    192  1.1    cjs  * Edit the next file in the command line list.
    193  1.1    cjs  */
    194  1.1    cjs next_file(n)
    195  1.1    cjs 	int n;
    196  1.1    cjs {
    197  1.1    cjs 	extern int quit_at_eof;
    198  1.1    cjs 	off_t position();
    199  1.1    cjs 
    200  1.1    cjs 	if (curr_ac + n >= ac) {
    201  1.1    cjs 		if (quit_at_eof || position(TOP) == NULL_POSITION)
    202  1.1    cjs 			quit();
    203  1.1    cjs 		error("No (N-th) next file");
    204  1.1    cjs 	}
    205  1.1    cjs 	else
    206  1.1    cjs 		(void)edit(av[curr_ac += n]);
    207  1.1    cjs }
    208  1.1    cjs 
    209  1.1    cjs /*
    210  1.1    cjs  * Edit the previous file in the command line list.
    211  1.1    cjs  */
    212  1.1    cjs prev_file(n)
    213  1.1    cjs 	int n;
    214  1.1    cjs {
    215  1.1    cjs 	if (curr_ac - n < 0)
    216  1.1    cjs 		error("No (N-th) previous file");
    217  1.1    cjs 	else
    218  1.1    cjs 		(void)edit(av[curr_ac -= n]);
    219  1.1    cjs }
    220  1.1    cjs 
    221  1.1    cjs /*
    222  1.1    cjs  * copy a file directly to standard output; used if stdout is not a tty.
    223  1.1    cjs  * the only processing is to squeeze multiple blank input lines.
    224  1.1    cjs  */
    225  1.1    cjs static
    226  1.1    cjs cat_file()
    227  1.1    cjs {
    228  1.1    cjs 	extern int squeeze;
    229  1.1    cjs 	register int c, empty;
    230  1.1    cjs 
    231  1.1    cjs 	if (squeeze) {
    232  1.1    cjs 		empty = 0;
    233  1.1    cjs 		while ((c = ch_forw_get()) != EOI)
    234  1.1    cjs 			if (c != '\n') {
    235  1.1    cjs 				putchr(c);
    236  1.1    cjs 				empty = 0;
    237  1.1    cjs 			}
    238  1.1    cjs 			else if (empty < 2) {
    239  1.1    cjs 				putchr(c);
    240  1.1    cjs 				++empty;
    241  1.1    cjs 			}
    242  1.1    cjs 	}
    243  1.1    cjs 	else while ((c = ch_forw_get()) != EOI)
    244  1.1    cjs 		putchr(c);
    245  1.1    cjs 	flush();
    246  1.1    cjs }
    247  1.1    cjs 
    248  1.1    cjs main(argc, argv)
    249  1.1    cjs 	int argc;
    250  1.1    cjs 	char **argv;
    251  1.1    cjs {
    252  1.1    cjs 	int envargc, argcnt;
    253  1.1    cjs 	char *envargv[2], *getenv();
    254  1.1    cjs 
    255  1.1    cjs 	/*
    256  1.1    cjs 	 * Process command line arguments and MORE environment arguments.
    257  1.1    cjs 	 * Command line arguments override environment arguments.
    258  1.1    cjs 	 */
    259  1.1    cjs 	if (envargv[1] = getenv("MORE")) {
    260  1.1    cjs 		envargc = 2;
    261  1.1    cjs 		envargv[0] = "more";
    262  1.1    cjs 		envargv[2] = NULL;
    263  1.1    cjs 		(void)option(envargc, envargv);
    264  1.1    cjs 	}
    265  1.1    cjs 	argcnt = option(argc, argv);
    266  1.1    cjs 	argv += argcnt;
    267  1.1    cjs 	argc -= argcnt;
    268  1.1    cjs 
    269  1.1    cjs 	/*
    270  1.1    cjs 	 * Set up list of files to be examined.
    271  1.1    cjs 	 */
    272  1.1    cjs 	ac = argc;
    273  1.1    cjs 	av = argv;
    274  1.1    cjs 	curr_ac = 0;
    275  1.1    cjs 
    276  1.1    cjs 	/*
    277  1.1    cjs 	 * Set up terminal, etc.
    278  1.1    cjs 	 */
    279  1.1    cjs 	is_tty = isatty(1);
    280  1.1    cjs 	if (!is_tty) {
    281  1.1    cjs 		/*
    282  1.1    cjs 		 * Output is not a tty.
    283  1.1    cjs 		 * Just copy the input file(s) to output.
    284  1.1    cjs 		 */
    285  1.1    cjs 		if (ac < 1) {
    286  1.1    cjs 			(void)edit("-");
    287  1.1    cjs 			cat_file();
    288  1.1    cjs 		} else {
    289  1.1    cjs 			do {
    290  1.1    cjs 				(void)edit((char *)NULL);
    291  1.1    cjs 				if (file >= 0)
    292  1.1    cjs 					cat_file();
    293  1.1    cjs 			} while (++curr_ac < ac);
    294  1.1    cjs 		}
    295  1.1    cjs 		exit(0);
    296  1.1    cjs 	}
    297  1.1    cjs 
    298  1.1    cjs 	raw_mode(1);
    299  1.1    cjs 	get_term();
    300  1.1    cjs 	open_getchr();
    301  1.1    cjs 	init();
    302  1.1    cjs 	init_signals(1);
    303  1.1    cjs 
    304  1.1    cjs 	/* select the first file to examine. */
    305  1.2    cjs 	if (ac < 1)
    306  1.1    cjs 		(void)edit("-");	/* Standard input */
    307  1.1    cjs 	else {
    308  1.1    cjs 		/*
    309  1.1    cjs 		 * Try all the files named as command arguments.
    310  1.1    cjs 		 * We are simply looking for one which can be
    311  1.1    cjs 		 * opened without error.
    312  1.1    cjs 		 */
    313  1.1    cjs 		do {
    314  1.1    cjs 			(void)edit((char *)NULL);
    315  1.1    cjs 		} while (file < 0 && ++curr_ac < ac);
    316  1.1    cjs 	}
    317  1.1    cjs 
    318  1.1    cjs 	if (file >= 0)
    319  1.1    cjs 		commands();
    320  1.1    cjs 	quit();
    321  1.1    cjs 	/*NOTREACHED*/
    322  1.1    cjs }
    323  1.1    cjs 
    324  1.1    cjs /*
    325  1.1    cjs  * Copy a string to a "safe" place
    326  1.1    cjs  * (that is, to a buffer allocated by malloc).
    327  1.1    cjs  */
    328  1.1    cjs char *
    329  1.1    cjs save(s)
    330  1.1    cjs 	char *s;
    331  1.1    cjs {
    332  1.1    cjs 	char *p, *strcpy(), *malloc();
    333  1.1    cjs 
    334  1.1    cjs 	p = malloc((u_int)strlen(s)+1);
    335  1.1    cjs 	if (p == NULL)
    336  1.1    cjs 	{
    337  1.1    cjs 		error("cannot allocate memory");
    338  1.1    cjs 		quit();
    339  1.1    cjs 	}
    340  1.1    cjs 	return(strcpy(p, s));
    341  1.1    cjs }
    342  1.1    cjs 
    343  1.1    cjs /*
    344  1.1    cjs  * Exit the program.
    345  1.1    cjs  */
    346  1.1    cjs quit()
    347  1.1    cjs {
    348  1.1    cjs 	/*
    349  1.1    cjs 	 * Put cursor at bottom left corner, clear the line,
    350  1.1    cjs 	 * reset the terminal modes, and exit.
    351  1.1    cjs 	 */
    352  1.1    cjs 	quitting = 1;
    353  1.1    cjs 	lower_left();
    354  1.1    cjs 	clear_eol();
    355  1.1    cjs 	deinit();
    356  1.1    cjs 	flush();
    357  1.1    cjs 	raw_mode(0);
    358  1.1    cjs 	exit(0);
    359  1.1    cjs }
    360