Home | History | Annotate | Line # | Download | only in sed
main.c revision 1.25
      1  1.25  christos /*	$NetBSD: main.c,v 1.25 2014/06/17 16:39:02 christos Exp $	*/
      2   1.9       tls 
      3   1.1       alm /*-
      4  1.22  christos  * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson.
      5  1.22  christos  * Copyright (c) 1992 Diomidis Spinellis.
      6   1.5       cgd  * Copyright (c) 1992, 1993
      7   1.5       cgd  *	The Regents of the University of California.  All rights reserved.
      8   1.1       alm  *
      9   1.1       alm  * This code is derived from software contributed to Berkeley by
     10   1.1       alm  * Diomidis Spinellis of Imperial College, University of London.
     11   1.1       alm  *
     12   1.1       alm  * Redistribution and use in source and binary forms, with or without
     13   1.1       alm  * modification, are permitted provided that the following conditions
     14   1.1       alm  * are met:
     15   1.1       alm  * 1. Redistributions of source code must retain the above copyright
     16   1.1       alm  *    notice, this list of conditions and the following disclaimer.
     17   1.1       alm  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1       alm  *    notice, this list of conditions and the following disclaimer in the
     19   1.1       alm  *    documentation and/or other materials provided with the distribution.
     20  1.14       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.14       agc  *    may be used to endorse or promote products derived from this software
     22  1.14       agc  *    without specific prior written permission.
     23  1.14       agc  *
     24  1.14       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.14       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.14       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.14       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.14       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.14       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.14       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.14       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.14       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.14       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.14       agc  * SUCH DAMAGE.
     35  1.14       agc  */
     36  1.14       agc 
     37  1.17   gdamore #if HAVE_NBTOOL_CONFIG_H
     38  1.17   gdamore #include "nbtool_config.h"
     39  1.17   gdamore #endif
     40  1.17   gdamore 
     41  1.10     lukem #include <sys/cdefs.h>
     42  1.25  christos __RCSID("$NetBSD: main.c,v 1.25 2014/06/17 16:39:02 christos Exp $");
     43  1.22  christos #ifdef __FBSDID
     44  1.22  christos __FBSDID("$FreeBSD: head/usr.bin/sed/main.c 252231 2013-06-26 04:14:19Z pfg $");
     45  1.22  christos #endif
     46  1.22  christos 
     47   1.1       alm #ifndef lint
     48  1.18     lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
     49  1.22  christos 	The Regents of the University of California.  All rights reserved.");
     50  1.22  christos #endif
     51   1.1       alm 
     52   1.1       alm #include <sys/types.h>
     53  1.22  christos #include <sys/mman.h>
     54  1.22  christos #include <sys/param.h>
     55  1.22  christos #include <sys/stat.h>
     56   1.1       alm 
     57  1.22  christos #include <err.h>
     58   1.1       alm #include <errno.h>
     59   1.1       alm #include <fcntl.h>
     60  1.22  christos #include <libgen.h>
     61  1.21       tnn #include <limits.h>
     62  1.22  christos #include <locale.h>
     63   1.1       alm #include <regex.h>
     64   1.1       alm #include <stddef.h>
     65  1.22  christos #define _WITH_GETLINE
     66   1.1       alm #include <stdio.h>
     67   1.1       alm #include <stdlib.h>
     68   1.1       alm #include <string.h>
     69   1.1       alm #include <unistd.h>
     70   1.1       alm 
     71   1.1       alm #include "defs.h"
     72   1.1       alm #include "extern.h"
     73   1.1       alm 
     74   1.1       alm /*
     75   1.1       alm  * Linked list of units (strings and files) to be compiled
     76   1.1       alm  */
     77   1.1       alm struct s_compunit {
     78   1.1       alm 	struct s_compunit *next;
     79   1.1       alm 	enum e_cut {CU_FILE, CU_STRING} type;
     80   1.1       alm 	char *s;			/* Pointer to string or fname */
     81   1.1       alm };
     82   1.1       alm 
     83   1.1       alm /*
     84   1.1       alm  * Linked list pointer to compilation units and pointer to current
     85   1.1       alm  * next pointer.
     86   1.1       alm  */
     87   1.1       alm static struct s_compunit *script, **cu_nextp = &script;
     88   1.1       alm 
     89   1.1       alm /*
     90   1.1       alm  * Linked list of files to be processed
     91   1.1       alm  */
     92   1.1       alm struct s_flist {
     93   1.1       alm 	char *fname;
     94   1.1       alm 	struct s_flist *next;
     95   1.1       alm };
     96   1.1       alm 
     97   1.1       alm /*
     98   1.1       alm  * Linked list pointer to files and pointer to current
     99   1.1       alm  * next pointer.
    100   1.1       alm  */
    101   1.1       alm static struct s_flist *files, **fl_nextp = &files;
    102   1.1       alm 
    103  1.22  christos FILE *infile;			/* Current input file */
    104  1.22  christos FILE *outfile;			/* Current output file */
    105  1.22  christos 
    106  1.22  christos int aflag, eflag, nflag;
    107  1.22  christos int rflags = 0;
    108  1.22  christos static int rval;		/* Exit status */
    109  1.22  christos 
    110  1.22  christos static int ispan;		/* Whether inplace editing spans across files */
    111   1.1       alm 
    112   1.1       alm /*
    113   1.1       alm  * Current file and line number; line numbers restart across compilation
    114  1.22  christos  * units, but span across input files.  The latter is optional if editing
    115  1.22  christos  * in place.
    116   1.1       alm  */
    117  1.22  christos const char *fname;		/* File name. */
    118  1.22  christos const char *outfname;		/* Output file name */
    119  1.22  christos static char oldfname[PATH_MAX];	/* Old file name (for in-place editing) */
    120  1.22  christos static char tmpfname[PATH_MAX];	/* Temporary file name (for in-place editing) */
    121  1.22  christos static const char *inplace;	/* Inplace edit file extension. */
    122   1.1       alm u_long linenum;
    123   1.1       alm 
    124  1.13       wiz static void add_compunit(enum e_cut, char *);
    125  1.13       wiz static void add_file(char *);
    126  1.24     joerg static void usage(void) __dead;
    127   1.1       alm 
    128   1.1       alm int
    129  1.13       wiz main(int argc, char *argv[])
    130   1.1       alm {
    131   1.1       alm 	int c, fflag;
    132  1.22  christos 	char *temp_arg;
    133  1.22  christos 
    134  1.22  christos 	setprogname(argv[0]);
    135  1.22  christos 	(void) setlocale(LC_ALL, "");
    136   1.1       alm 
    137   1.1       alm 	fflag = 0;
    138  1.22  christos 	inplace = NULL;
    139  1.22  christos 
    140  1.25  christos 	while ((c = getopt(argc, argv, "EI::ae:f:i::lnru")) != -1)
    141   1.1       alm 		switch (c) {
    142  1.22  christos 		case 'r':		/* Gnu sed compat */
    143  1.22  christos 		case 'E':
    144  1.22  christos 			rflags = REG_EXTENDED;
    145  1.22  christos 			break;
    146  1.22  christos 		case 'I':
    147  1.22  christos 			inplace = optarg ? optarg : __UNCONST("");
    148  1.22  christos 			ispan = 1;	/* span across input files */
    149  1.22  christos 			break;
    150   1.1       alm 		case 'a':
    151   1.1       alm 			aflag = 1;
    152   1.1       alm 			break;
    153   1.1       alm 		case 'e':
    154   1.1       alm 			eflag = 1;
    155  1.22  christos 			temp_arg = xmalloc(strlen(optarg) + 2);
    156  1.22  christos 			strcpy(temp_arg, optarg);
    157  1.22  christos 			strcat(temp_arg, "\n");
    158  1.22  christos 			add_compunit(CU_STRING, temp_arg);
    159   1.1       alm 			break;
    160   1.1       alm 		case 'f':
    161   1.1       alm 			fflag = 1;
    162   1.1       alm 			add_compunit(CU_FILE, optarg);
    163   1.1       alm 			break;
    164  1.22  christos 		case 'i':
    165  1.22  christos 			inplace = optarg ? optarg : __UNCONST("");
    166  1.22  christos 			ispan = 0;	/* don't span across input files */
    167  1.22  christos 			break;
    168  1.22  christos 		case 'l':
    169  1.23  christos #ifdef _IOLBF
    170  1.23  christos 			c = setvbuf(stdout, NULL, _IOLBF, 0);
    171  1.23  christos #else
    172  1.23  christos 			c = setlinebuf(stdout);
    173  1.23  christos #endif
    174  1.23  christos 			if (c)
    175  1.23  christos 				warn("setting line buffered output failed");
    176  1.22  christos 			break;
    177   1.1       alm 		case 'n':
    178   1.1       alm 			nflag = 1;
    179   1.1       alm 			break;
    180  1.25  christos 		case 'u':
    181  1.25  christos #ifdef _IONBF
    182  1.25  christos 			c = setvbuf(stdout, NULL, _IONBF, 0);
    183  1.25  christos #else
    184  1.25  christos 			c = -1;
    185  1.25  christos 			errno = EOPNOTSUPP;
    186  1.25  christos #endif
    187  1.25  christos 			if (c)
    188  1.25  christos 				warn("setting unbuffered output failed");
    189  1.25  christos 			break;
    190   1.1       alm 		default:
    191   1.1       alm 		case '?':
    192  1.22  christos 			usage();
    193   1.1       alm 		}
    194   1.1       alm 	argc -= optind;
    195   1.1       alm 	argv += optind;
    196   1.1       alm 
    197   1.1       alm 	/* First usage case; script is the first arg */
    198   1.1       alm 	if (!eflag && !fflag && *argv) {
    199   1.1       alm 		add_compunit(CU_STRING, *argv);
    200   1.1       alm 		argv++;
    201   1.1       alm 	}
    202   1.1       alm 
    203   1.1       alm 	compile();
    204   1.1       alm 
    205   1.1       alm 	/* Continue with first and start second usage */
    206   1.1       alm 	if (*argv)
    207   1.1       alm 		for (; *argv; argv++)
    208   1.1       alm 			add_file(*argv);
    209   1.1       alm 	else
    210   1.1       alm 		add_file(NULL);
    211   1.1       alm 	process();
    212   1.1       alm 	cfclose(prog, NULL);
    213   1.1       alm 	if (fclose(stdout))
    214  1.22  christos 		err(1, "stdout");
    215  1.22  christos 	exit(rval);
    216  1.22  christos }
    217  1.22  christos 
    218  1.22  christos static void
    219  1.22  christos usage(void)
    220  1.22  christos {
    221  1.25  christos 	(void)fprintf(stderr,
    222  1.25  christos 	    "Usage: %s script [-Ealnu] [-i[<extension>]] [file ...]\n"
    223  1.25  christos 	    "\t%s [-Ealnu] [-i[<extension>]] [-e script] ... [-f script_file]"
    224  1.25  christos 	    " ... [file ...]\n", getprogname(), getprogname());
    225  1.22  christos 	exit(1);
    226   1.1       alm }
    227   1.1       alm 
    228   1.1       alm /*
    229   1.1       alm  * Like fgets, but go through the chain of compilation units chaining them
    230   1.1       alm  * together.  Empty strings and files are ignored.
    231   1.1       alm  */
    232   1.1       alm char *
    233  1.22  christos cu_fgets(char *buf, int n, int *more)
    234   1.1       alm {
    235   1.1       alm 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
    236   1.1       alm 	static FILE *f;		/* Current open file */
    237   1.1       alm 	static char *s;		/* Current pointer inside string */
    238   1.1       alm 	static char string_ident[30];
    239   1.1       alm 	char *p;
    240   1.1       alm 
    241   1.1       alm again:
    242   1.1       alm 	switch (state) {
    243   1.1       alm 	case ST_EOF:
    244  1.22  christos 		if (script == NULL) {
    245  1.22  christos 			if (more != NULL)
    246  1.22  christos 				*more = 0;
    247   1.1       alm 			return (NULL);
    248  1.22  christos 		}
    249   1.1       alm 		linenum = 0;
    250   1.1       alm 		switch (script->type) {
    251   1.1       alm 		case CU_FILE:
    252   1.1       alm 			if ((f = fopen(script->s, "r")) == NULL)
    253  1.22  christos 				err(1, "%s", script->s);
    254   1.1       alm 			fname = script->s;
    255   1.1       alm 			state = ST_FILE;
    256   1.1       alm 			goto again;
    257   1.1       alm 		case CU_STRING:
    258  1.22  christos 			if (((size_t)snprintf(string_ident,
    259   1.1       alm 			    sizeof(string_ident), "\"%s\"", script->s)) >=
    260  1.22  christos 			    sizeof(string_ident) - 1)
    261   1.1       alm 				(void)strcpy(string_ident +
    262   1.1       alm 				    sizeof(string_ident) - 6, " ...\"");
    263   1.1       alm 			fname = string_ident;
    264   1.1       alm 			s = script->s;
    265   1.1       alm 			state = ST_STRING;
    266   1.1       alm 			goto again;
    267   1.1       alm 		}
    268   1.1       alm 	case ST_FILE:
    269  1.22  christos 		if ((p = fgets(buf, n, f)) != NULL) {
    270   1.1       alm 			linenum++;
    271  1.22  christos 			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
    272   1.1       alm 				nflag = 1;
    273  1.22  christos 			if (more != NULL)
    274  1.22  christos 				*more = !feof(f);
    275  1.22  christos 			return (p);
    276   1.1       alm 		}
    277   1.1       alm 		script = script->next;
    278   1.1       alm 		(void)fclose(f);
    279   1.1       alm 		state = ST_EOF;
    280   1.1       alm 		goto again;
    281   1.1       alm 	case ST_STRING:
    282   1.1       alm 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
    283   1.1       alm 			nflag = 1;
    284  1.22  christos 		p = buf;
    285   1.1       alm 		for (;;) {
    286  1.22  christos 			if (n-- <= 1) {
    287  1.22  christos 				*p = '\0';
    288  1.22  christos 				linenum++;
    289  1.22  christos 				if (more != NULL)
    290  1.22  christos 					*more = 1;
    291  1.22  christos 				return (buf);
    292   1.1       alm 			}
    293   1.1       alm 			switch (*s) {
    294   1.1       alm 			case '\0':
    295   1.1       alm 				state = ST_EOF;
    296   1.1       alm 				if (s == script->s) {
    297   1.1       alm 					script = script->next;
    298   1.1       alm 					goto again;
    299   1.1       alm 				} else {
    300   1.1       alm 					script = script->next;
    301   1.1       alm 					*p = '\0';
    302   1.1       alm 					linenum++;
    303  1.22  christos 					if (more != NULL)
    304  1.22  christos 						*more = 0;
    305  1.22  christos 					return (buf);
    306   1.1       alm 				}
    307   1.1       alm 			case '\n':
    308   1.1       alm 				*p++ = '\n';
    309   1.1       alm 				*p = '\0';
    310   1.1       alm 				s++;
    311   1.1       alm 				linenum++;
    312  1.22  christos 				if (more != NULL)
    313  1.22  christos 					*more = 0;
    314  1.22  christos 				return (buf);
    315   1.1       alm 			default:
    316   1.1       alm 				*p++ = *s++;
    317   1.1       alm 			}
    318   1.1       alm 		}
    319   1.1       alm 	}
    320   1.1       alm 	/* NOTREACHED */
    321  1.10     lukem 	return (NULL);
    322   1.1       alm }
    323   1.1       alm 
    324   1.1       alm /*
    325   1.1       alm  * Like fgets, but go through the list of files chaining them together.
    326   1.1       alm  * Set len to the length of the line.
    327   1.1       alm  */
    328   1.1       alm int
    329  1.13       wiz mf_fgets(SPACE *sp, enum e_spflag spflag)
    330   1.1       alm {
    331  1.22  christos 	struct stat sb;
    332   1.1       alm 	size_t len;
    333  1.22  christos 	static char *p = NULL;
    334  1.22  christos 	static size_t plen = 0;
    335   1.8      mark 	int c;
    336  1.22  christos 	static int firstfile;
    337  1.22  christos 
    338  1.22  christos 	if (infile == NULL) {
    339  1.22  christos 		/* stdin? */
    340  1.22  christos 		if (files->fname == NULL) {
    341  1.22  christos 			if (inplace != NULL)
    342  1.22  christos 				errx(1, "-I or -i may not be used with stdin");
    343  1.22  christos 			infile = stdin;
    344  1.22  christos 			fname = "stdin";
    345  1.22  christos 			outfile = stdout;
    346  1.22  christos 			outfname = "stdout";
    347  1.22  christos 		}
    348  1.22  christos 		firstfile = 1;
    349  1.22  christos 	}
    350   1.1       alm 
    351  1.22  christos 	for (;;) {
    352  1.22  christos 		if (infile != NULL && (c = getc(infile)) != EOF) {
    353  1.22  christos 			(void)ungetc(c, infile);
    354  1.22  christos 			break;
    355  1.22  christos 		}
    356  1.22  christos 		/* If we are here then either eof or no files are open yet */
    357  1.22  christos 		if (infile == stdin) {
    358  1.22  christos 			sp->len = 0;
    359  1.22  christos 			return (0);
    360  1.22  christos 		}
    361  1.22  christos 		if (infile != NULL) {
    362  1.22  christos 			fclose(infile);
    363  1.22  christos 			if (*oldfname != '\0') {
    364  1.22  christos 				/* if there was a backup file, remove it */
    365  1.22  christos 				unlink(oldfname);
    366  1.22  christos 				/*
    367  1.22  christos 				 * Backup the original.  Note that hard links
    368  1.22  christos 				 * are not supported on all filesystems.
    369  1.22  christos 				 */
    370  1.22  christos 				if ((link(fname, oldfname) != 0) &&
    371  1.22  christos 				   (rename(fname, oldfname) != 0)) {
    372  1.22  christos 					warn("rename()");
    373  1.22  christos 					if (*tmpfname)
    374  1.22  christos 						unlink(tmpfname);
    375  1.22  christos 					exit(1);
    376  1.22  christos 				}
    377  1.22  christos 				*oldfname = '\0';
    378   1.1       alm 			}
    379  1.22  christos 			if (*tmpfname != '\0') {
    380  1.22  christos 				if (outfile != NULL && outfile != stdout)
    381  1.22  christos 					if (fclose(outfile) != 0) {
    382  1.22  christos 						warn("fclose()");
    383  1.22  christos 						unlink(tmpfname);
    384  1.22  christos 						exit(1);
    385  1.22  christos 					}
    386  1.22  christos 				outfile = NULL;
    387  1.22  christos 				if (rename(tmpfname, fname) != 0) {
    388  1.22  christos 					/* this should not happen really! */
    389  1.22  christos 					warn("rename()");
    390  1.22  christos 					unlink(tmpfname);
    391  1.22  christos 					exit(1);
    392  1.22  christos 				}
    393  1.22  christos 				*tmpfname = '\0';
    394   1.1       alm 			}
    395  1.22  christos 			outfname = NULL;
    396  1.22  christos 		}
    397  1.22  christos 		if (firstfile == 0)
    398  1.22  christos 			files = files->next;
    399  1.22  christos 		else
    400  1.22  christos 			firstfile = 0;
    401  1.22  christos 		if (files == NULL) {
    402  1.22  christos 			sp->len = 0;
    403  1.22  christos 			return (0);
    404  1.22  christos 		}
    405  1.22  christos 		fname = files->fname;
    406  1.22  christos 		if (inplace != NULL) {
    407  1.22  christos 			if (lstat(fname, &sb) != 0)
    408  1.22  christos 				err(1, "%s", fname);
    409  1.22  christos 			if (!(sb.st_mode & S_IFREG))
    410  1.22  christos 				errx(1, "%s: %s %s", fname,
    411  1.22  christos 				    "in-place editing only",
    412  1.22  christos 				    "works for regular files");
    413  1.22  christos 			if (*inplace != '\0') {
    414  1.22  christos 				strlcpy(oldfname, fname,
    415  1.22  christos 				    sizeof(oldfname));
    416  1.22  christos 				len = strlcat(oldfname, inplace,
    417  1.22  christos 				    sizeof(oldfname));
    418  1.22  christos 				if (len > sizeof(oldfname))
    419  1.22  christos 					errx(1, "%s: name too long", fname);
    420  1.22  christos 			}
    421  1.22  christos 			char d_name[PATH_MAX], f_name[PATH_MAX];
    422  1.22  christos 			(void)strlcpy(d_name, fname, sizeof(d_name));
    423  1.22  christos 			(void)strlcpy(f_name, fname, sizeof(f_name));
    424  1.22  christos 			len = (size_t)snprintf(tmpfname, sizeof(tmpfname),
    425  1.22  christos 			    "%s/.!%ld!%s", dirname(d_name), (long)getpid(),
    426  1.22  christos 			    basename(f_name));
    427  1.22  christos 			if (len >= sizeof(tmpfname))
    428  1.22  christos 				errx(1, "%s: name too long", fname);
    429  1.22  christos 			unlink(tmpfname);
    430  1.22  christos 			if ((outfile = fopen(tmpfname, "w")) == NULL)
    431  1.22  christos 				err(1, "%s", fname);
    432  1.22  christos 			fchown(fileno(outfile), sb.st_uid, sb.st_gid);
    433  1.22  christos 			fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
    434  1.22  christos 			outfname = tmpfname;
    435  1.22  christos 			if (!ispan) {
    436  1.22  christos 				linenum = 0;
    437  1.22  christos 				resetstate();
    438   1.1       alm 			}
    439  1.22  christos 		} else {
    440  1.22  christos 			outfile = stdout;
    441  1.22  christos 			outfname = "stdout";
    442  1.22  christos 		}
    443  1.22  christos 		if ((infile = fopen(fname, "r")) == NULL) {
    444  1.22  christos 			warn("%s", fname);
    445  1.22  christos 			rval = 1;
    446  1.22  christos 			continue;
    447   1.1       alm 		}
    448   1.1       alm 	}
    449   1.1       alm 	/*
    450  1.22  christos 	 * We are here only when infile is open and we still have something
    451  1.22  christos 	 * to read from it.
    452  1.22  christos 	 *
    453  1.22  christos 	 * Use getline() so that we can handle essentially infinite input
    454  1.22  christos 	 * data.  The p and plen are static so each invocation gives
    455  1.22  christos 	 * getline() the same buffer which is expanded as needed.
    456   1.1       alm 	 */
    457  1.22  christos 	ssize_t slen = getline(&p, &plen, infile);
    458  1.22  christos 	if (slen == -1)
    459  1.22  christos 		err(1, "%s", fname);
    460  1.22  christos 	if (slen != 0 && p[slen - 1] == '\n')
    461  1.22  christos 		slen--;
    462  1.22  christos 	cspace(sp, p, (size_t)slen, spflag);
    463   1.1       alm 
    464   1.1       alm 	linenum++;
    465  1.22  christos 
    466   1.1       alm 	return (1);
    467   1.1       alm }
    468   1.1       alm 
    469   1.1       alm /*
    470   1.1       alm  * Add a compilation unit to the linked list
    471   1.1       alm  */
    472   1.1       alm static void
    473  1.13       wiz add_compunit(enum e_cut type, char *s)
    474   1.1       alm {
    475   1.1       alm 	struct s_compunit *cu;
    476   1.1       alm 
    477   1.1       alm 	cu = xmalloc(sizeof(struct s_compunit));
    478   1.1       alm 	cu->type = type;
    479   1.1       alm 	cu->s = s;
    480   1.1       alm 	cu->next = NULL;
    481   1.1       alm 	*cu_nextp = cu;
    482   1.1       alm 	cu_nextp = &cu->next;
    483   1.1       alm }
    484   1.1       alm 
    485   1.1       alm /*
    486   1.1       alm  * Add a file to the linked list
    487   1.1       alm  */
    488   1.1       alm static void
    489  1.13       wiz add_file(char *s)
    490   1.1       alm {
    491   1.1       alm 	struct s_flist *fp;
    492   1.1       alm 
    493   1.1       alm 	fp = xmalloc(sizeof(struct s_flist));
    494   1.1       alm 	fp->next = NULL;
    495   1.1       alm 	*fl_nextp = fp;
    496   1.1       alm 	fp->fname = s;
    497   1.1       alm 	fl_nextp = &fp->next;
    498   1.1       alm }
    499  1.22  christos 
    500  1.22  christos int
    501  1.22  christos lastline(void)
    502  1.22  christos {
    503  1.22  christos 	int ch;
    504  1.22  christos 
    505  1.22  christos 	if (files->next != NULL && (inplace == NULL || ispan))
    506  1.22  christos 		return (0);
    507  1.22  christos 	if ((ch = getc(infile)) == EOF)
    508  1.22  christos 		return (1);
    509  1.22  christos 	ungetc(ch, infile);
    510  1.22  christos 	return (0);
    511  1.22  christos }
    512