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