xref: /illumos-gate/usr/src/cmd/sdiff/sdiff.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 	/*
327c478bd9Sstevel@tonic-gate 	 *	sdiff [-l] [-s] [-w #] [-o output] file1 file2
337c478bd9Sstevel@tonic-gate 	 *	does side by side diff listing
347c478bd9Sstevel@tonic-gate 	 *	-l leftside only for identical lines
357c478bd9Sstevel@tonic-gate 	 *	-s silent; only print differences
367c478bd9Sstevel@tonic-gate 	 *	-w # width of output
377c478bd9Sstevel@tonic-gate 	 *	-o output  interactive creation of new output commands:
387c478bd9Sstevel@tonic-gate 	 *		s	silent; do not print identical lines
397c478bd9Sstevel@tonic-gate 	 *		v	turn off silent
407c478bd9Sstevel@tonic-gate 	 *		l	copy left side to output
417c478bd9Sstevel@tonic-gate 	 *		r	copy right side to output
427c478bd9Sstevel@tonic-gate 	 *		e l	call ed with left side
437c478bd9Sstevel@tonic-gate 	 *		e r	call ed with right side
447c478bd9Sstevel@tonic-gate 	 *		e b	call ed with cat of left and right
457c478bd9Sstevel@tonic-gate 	 *		e	call ed with empty file
467c478bd9Sstevel@tonic-gate 	 *		q	exit from program
477c478bd9Sstevel@tonic-gate 	 *
487c478bd9Sstevel@tonic-gate 	 *	functions:
497c478bd9Sstevel@tonic-gate 	 *	cmd	decode diff commands
507c478bd9Sstevel@tonic-gate 	 *	put1	output left side
517c478bd9Sstevel@tonic-gate 	 *	put2	output right side
527c478bd9Sstevel@tonic-gate 	 *	putmid	output gutter
537c478bd9Sstevel@tonic-gate 	 *	putline	output n chars to indicated file
547c478bd9Sstevel@tonic-gate 	 *	getlen	calculate length of strings with tabs
557c478bd9Sstevel@tonic-gate 	 *	cmdin	read and process interactive cmds
567c478bd9Sstevel@tonic-gate 	 *	cpp	copy from file to file
577c478bd9Sstevel@tonic-gate 	 *	edit	call ed with file
587c478bd9Sstevel@tonic-gate 	 */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <stdio.h>
617c478bd9Sstevel@tonic-gate #include <ctype.h>
627c478bd9Sstevel@tonic-gate #include <signal.h>
637c478bd9Sstevel@tonic-gate #include <sys/types.h>
647c478bd9Sstevel@tonic-gate #include <sys/stat.h>
657c478bd9Sstevel@tonic-gate #include <sys/wait.h>
667c478bd9Sstevel@tonic-gate #include <unistd.h>
677c478bd9Sstevel@tonic-gate #include <stdlib.h>
687c478bd9Sstevel@tonic-gate #include <locale.h>
697c478bd9Sstevel@tonic-gate #include <limits.h>
707c478bd9Sstevel@tonic-gate #include <string.h>
717c478bd9Sstevel@tonic-gate #include <wchar.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	LMAX	BUFSIZ
747c478bd9Sstevel@tonic-gate #define	BMAX	BUFSIZ
757c478bd9Sstevel@tonic-gate #define	STDOUT	1
767c478bd9Sstevel@tonic-gate #define	WGUTTER	6
777c478bd9Sstevel@tonic-gate #define	WLEN	(WGUTTER * 2 + WGUTTER + 2)
787c478bd9Sstevel@tonic-gate #define	PROMPT	'%'
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static const char	twoblanks[3] = "  ";
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static const char	*DIFF	= "diff -b ";
837c478bd9Sstevel@tonic-gate static char	diffcmd[BMAX];
847c478bd9Sstevel@tonic-gate static char	inbuf[10];
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static int	llen = 130;	/* Default maximum line length written out */
877c478bd9Sstevel@tonic-gate static int	hlen;		/* Half line length with space for gutter */
887c478bd9Sstevel@tonic-gate static int	len1;		/* Calculated length of left side */
897c478bd9Sstevel@tonic-gate static int	nchars;		/* Number of characters in left side - */
907c478bd9Sstevel@tonic-gate 					/* used for tab expansion */
917c478bd9Sstevel@tonic-gate static char	change = ' ';
927c478bd9Sstevel@tonic-gate static int	leftonly = 0;	/* if set print left side only for */
937c478bd9Sstevel@tonic-gate 					/* identical lines */
947c478bd9Sstevel@tonic-gate static int	silent = 0;	/* if set do not print identical lines */
957c478bd9Sstevel@tonic-gate static int	midflg = 0;	/* set after middle was output */
967c478bd9Sstevel@tonic-gate static int	rcode = 0;	/* return code */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static char	*file1;
1007c478bd9Sstevel@tonic-gate static FILE	*fdes1;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static char	*file2;
1037c478bd9Sstevel@tonic-gate static FILE	*fdes2;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static FILE	*diffdes;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int oflag;
1087c478bd9Sstevel@tonic-gate static char	*ofile;
1097c478bd9Sstevel@tonic-gate static FILE	*odes;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static char	*ltemp;
1127c478bd9Sstevel@tonic-gate static FILE	*left;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static char	*rtemp;
1157c478bd9Sstevel@tonic-gate static FILE	*right;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static FILE *tempdes;
1187c478bd9Sstevel@tonic-gate static char *temp;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /* decoded diff cmd- left side from to; right side from, to */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static int from1, to1, from2, to2;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int num1, num2;		/* line count for left side file and right */
1257c478bd9Sstevel@tonic-gate static int tempfd = -1;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate static char	*filename(char *, char *);
1287c478bd9Sstevel@tonic-gate static char	*fgetline(FILE *);
1297c478bd9Sstevel@tonic-gate static int	put1(void);
1307c478bd9Sstevel@tonic-gate static int	put2(void);
1317c478bd9Sstevel@tonic-gate static void	putline(FILE *, char *, int);
1327c478bd9Sstevel@tonic-gate static int	cmd(char *);
1337c478bd9Sstevel@tonic-gate static int	getlen(int, char *);
1347c478bd9Sstevel@tonic-gate static void	putmid(int);
1357c478bd9Sstevel@tonic-gate static void	error(char *, char *);
1367c478bd9Sstevel@tonic-gate static void	onintr(void);
1377c478bd9Sstevel@tonic-gate static void	sremove(void);
1387c478bd9Sstevel@tonic-gate static void	cmdin(void);
1397c478bd9Sstevel@tonic-gate static void	cpp(char *, FILE *, FILE *);
1407c478bd9Sstevel@tonic-gate static void	edit(char *);
1417c478bd9Sstevel@tonic-gate 
142*a85fbef1Sakaplan int
main(int argc,char ** argv)1437c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	int	com;
1467c478bd9Sstevel@tonic-gate 	int	n1, n2, n;
1477c478bd9Sstevel@tonic-gate 	char	*bp;
1487c478bd9Sstevel@tonic-gate 	int	lfd = -1;
1497c478bd9Sstevel@tonic-gate 	int	rfd = -1;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1527c478bd9Sstevel@tonic-gate 		(void) signal((int)SIGHUP, (void (*)(int))onintr);
1537c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1547c478bd9Sstevel@tonic-gate 		(void) signal((int)SIGINT, (void (*)(int))onintr);
1557c478bd9Sstevel@tonic-gate 	if (signal(SIGPIPE, SIG_IGN) != SIG_IGN)
1567c478bd9Sstevel@tonic-gate 		(void) signal((int)SIGPIPE, (void (*)(int))onintr);
1577c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1587c478bd9Sstevel@tonic-gate 		(void) signal((int)SIGTERM, (void (*)(int))onintr);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1617c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
1627c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1637c478bd9Sstevel@tonic-gate #endif
1647c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	while (--argc > 1 && **++argv == '-') {
1677c478bd9Sstevel@tonic-gate 		switch (*++*argv) {
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		case 'w':
1707c478bd9Sstevel@tonic-gate 			/* -w# instead of -w # */
1717c478bd9Sstevel@tonic-gate 			if (*++*argv)
1727c478bd9Sstevel@tonic-gate 				llen = atoi(*argv);
1737c478bd9Sstevel@tonic-gate 			else {
1747c478bd9Sstevel@tonic-gate 				argc--;
1757c478bd9Sstevel@tonic-gate 				llen = atoi(*++argv);
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 			if (llen < WLEN)
1787c478bd9Sstevel@tonic-gate 				error(gettext("Wrong line length %s"), *argv);
1797c478bd9Sstevel@tonic-gate 			if (llen > LMAX)
1807c478bd9Sstevel@tonic-gate 				llen = LMAX;
1817c478bd9Sstevel@tonic-gate 			break;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		case 'l':
1847c478bd9Sstevel@tonic-gate 			leftonly++;
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		case 's':
1887c478bd9Sstevel@tonic-gate 			silent++;
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		case 'o':
1917c478bd9Sstevel@tonic-gate 			oflag++;
1927c478bd9Sstevel@tonic-gate 			argc--;
1937c478bd9Sstevel@tonic-gate 			ofile = *++argv;
1947c478bd9Sstevel@tonic-gate 			break;
1957c478bd9Sstevel@tonic-gate 		default:
1967c478bd9Sstevel@tonic-gate 			error(gettext("Illegal argument: %s"), *argv);
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	if (argc != 2) {
2007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2017c478bd9Sstevel@tonic-gate 		"Usage: sdiff [-l] [-s] [-o output] [-w #] file1 file2\n"));
202*a85fbef1Sakaplan 		return (2);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	file1 = *argv++;
2067c478bd9Sstevel@tonic-gate 	file2 = *argv;
2077c478bd9Sstevel@tonic-gate 	file1 = filename(file1, file2);
2087c478bd9Sstevel@tonic-gate 	file2 = filename(file2, file1);
2097c478bd9Sstevel@tonic-gate 	hlen = (llen - WGUTTER +1)/2;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if ((fdes1 = fopen(file1, "r")) == NULL)
2127c478bd9Sstevel@tonic-gate 		error(gettext("Cannot open: %s"), file1);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if ((fdes2 = fopen(file2, "r")) == NULL)
2157c478bd9Sstevel@tonic-gate 		error(gettext("Cannot open: %s"), file2);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (oflag) {
2187c478bd9Sstevel@tonic-gate 		if (tempfd == -1) {
2197c478bd9Sstevel@tonic-gate 			temp = strdup("/tmp/sdiffXXXXXX");
2207c478bd9Sstevel@tonic-gate 			tempfd = mkstemp(temp);
2217c478bd9Sstevel@tonic-gate 			if (tempfd == -1) {
2227c478bd9Sstevel@tonic-gate 				error(gettext(
2237c478bd9Sstevel@tonic-gate 					"Cannot open/create temp %s"), temp);
2247c478bd9Sstevel@tonic-gate 				free(temp);
2257c478bd9Sstevel@tonic-gate 				temp = 0;
2267c478bd9Sstevel@tonic-gate 			}
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		ltemp = strdup("/tmp/sdifflXXXXXX");
2297c478bd9Sstevel@tonic-gate 		if ((lfd = mkstemp(ltemp)) == -1 ||
2307c478bd9Sstevel@tonic-gate 			(left = fdopen(lfd, "w")) == NULL)
2317c478bd9Sstevel@tonic-gate 				error(gettext(
2327c478bd9Sstevel@tonic-gate 					"Cannot open/create temp %s"),
2337c478bd9Sstevel@tonic-gate 					ltemp);
2347c478bd9Sstevel@tonic-gate 		rtemp = strdup("/tmp/sdiffrXXXXXX");
2357c478bd9Sstevel@tonic-gate 		if ((rfd = mkstemp(rtemp)) == -1 ||
2367c478bd9Sstevel@tonic-gate 			(right = fdopen(rfd, "w")) == NULL)
2377c478bd9Sstevel@tonic-gate 				error(gettext(
2387c478bd9Sstevel@tonic-gate 					"Cannot open/create temp file %s"),
2397c478bd9Sstevel@tonic-gate 					rtemp);
2407c478bd9Sstevel@tonic-gate 		if ((odes = fopen(ofile, "w")) == NULL)
2417c478bd9Sstevel@tonic-gate 			error(gettext("Cannot open output %s"), ofile);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	/* Call DIFF command */
2447c478bd9Sstevel@tonic-gate 	(void) strcpy(diffcmd, DIFF);
2457c478bd9Sstevel@tonic-gate 	(void) strcat(diffcmd, file1);
2467c478bd9Sstevel@tonic-gate 	(void) strcat(diffcmd, " ");
2477c478bd9Sstevel@tonic-gate 	(void) strcat(diffcmd, file2);
2487c478bd9Sstevel@tonic-gate 	diffdes = popen(diffcmd, "r");
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	num1 = num2 = 0;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Read in diff output and decode commands
2547c478bd9Sstevel@tonic-gate 	 * "change" is used to determine character to put in gutter
2557c478bd9Sstevel@tonic-gate 	 *  num1 and num2 counts the number of lines in file1 and 2
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	n = 0;
2597c478bd9Sstevel@tonic-gate 	while ((bp = fgetline(diffdes)) != NULL) {
2607c478bd9Sstevel@tonic-gate 		change = ' ';
2617c478bd9Sstevel@tonic-gate 		com = cmd(bp);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * handles all diff output that is not cmd
2657c478bd9Sstevel@tonic-gate 	 * lines starting with <, >, ., ---
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 		if (com == 0)
2687c478bd9Sstevel@tonic-gate 			continue;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/* Catch up to from1 and from2 */
2717c478bd9Sstevel@tonic-gate 		rcode = 1;
2727c478bd9Sstevel@tonic-gate 		n1 = from1 - num1;
2737c478bd9Sstevel@tonic-gate 		n2 = from2 - num2;
2747c478bd9Sstevel@tonic-gate 		n = n1 > n2 ? n2 : n1;
2757c478bd9Sstevel@tonic-gate 		if (com == 'c' && n > 0)
2767c478bd9Sstevel@tonic-gate 			n--;
2777c478bd9Sstevel@tonic-gate 		if (silent)
2787c478bd9Sstevel@tonic-gate 			(void) fputs(bp, stdout);
2797c478bd9Sstevel@tonic-gate 		while (n-- > 0) {
2807c478bd9Sstevel@tonic-gate 			(void) put1();
2817c478bd9Sstevel@tonic-gate 			(void) put2();
2827c478bd9Sstevel@tonic-gate 			if (!silent)
2837c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
2847c478bd9Sstevel@tonic-gate 			midflg = 0;
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/* Process diff cmd */
2887c478bd9Sstevel@tonic-gate 		switch (com) {
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		case 'a':
2917c478bd9Sstevel@tonic-gate 			change = '>';
2927c478bd9Sstevel@tonic-gate 			while (num2 < to2) {
2937c478bd9Sstevel@tonic-gate 				(void) put2();
2947c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
2957c478bd9Sstevel@tonic-gate 				midflg = 0;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		case 'd':
3007c478bd9Sstevel@tonic-gate 			change = '<';
3017c478bd9Sstevel@tonic-gate 			while (num1 < to1) {
3027c478bd9Sstevel@tonic-gate 				(void) put1();
3037c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
3047c478bd9Sstevel@tonic-gate 				midflg = 0;
3057c478bd9Sstevel@tonic-gate 			}
3067c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		case 'c':
3097c478bd9Sstevel@tonic-gate 			n1 = to1 - from1;
3107c478bd9Sstevel@tonic-gate 			n2 = to2 - from2;
3117c478bd9Sstevel@tonic-gate 			n = n1 > n2 ? n2 : n1;
3127c478bd9Sstevel@tonic-gate 			change = '|';
3137c478bd9Sstevel@tonic-gate 			do {
3147c478bd9Sstevel@tonic-gate 				(void) put1();
3157c478bd9Sstevel@tonic-gate 				(void) put2();
3167c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
3177c478bd9Sstevel@tonic-gate 				midflg = 0;
3187c478bd9Sstevel@tonic-gate 			} while (n--);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 			change = '<';
3217c478bd9Sstevel@tonic-gate 			while (num1 < to1) {
3227c478bd9Sstevel@tonic-gate 				(void) put1();
3237c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
3247c478bd9Sstevel@tonic-gate 				midflg = 0;
3257c478bd9Sstevel@tonic-gate 			}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			change = '>';
3287c478bd9Sstevel@tonic-gate 			while (num2 < to2) {
3297c478bd9Sstevel@tonic-gate 				(void) put2();
3307c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
3317c478bd9Sstevel@tonic-gate 				midflg = 0;
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		default:
3367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3377c478bd9Sstevel@tonic-gate 				"%c: cmd not found\n"), cmd);
3387c478bd9Sstevel@tonic-gate 			break;
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		if (oflag == 1 && com != 0) {
3427c478bd9Sstevel@tonic-gate 			cmdin();
3437c478bd9Sstevel@tonic-gate 			if ((left = fopen(ltemp, "w")) == NULL)
3447c478bd9Sstevel@tonic-gate 				error(gettext(
3457c478bd9Sstevel@tonic-gate 					"main: Cannot open temp %s"), ltemp);
3467c478bd9Sstevel@tonic-gate 			if ((right = fopen(rtemp, "w")) == NULL)
3477c478bd9Sstevel@tonic-gate 				error(gettext(
3487c478bd9Sstevel@tonic-gate 					"main: Cannot open temp %s"), rtemp);
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 	/* put out remainder of input files */
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	while (put1()) {
3547c478bd9Sstevel@tonic-gate 		(void) put2();
3557c478bd9Sstevel@tonic-gate 		if (!silent)
3567c478bd9Sstevel@tonic-gate 			(void) putc('\n', stdout);
3577c478bd9Sstevel@tonic-gate 		midflg = 0;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	if (odes)
3607c478bd9Sstevel@tonic-gate 		(void) fclose(odes);
3617c478bd9Sstevel@tonic-gate 	sremove();
362*a85fbef1Sakaplan 	return (rcode);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate static int
put1(void)3667c478bd9Sstevel@tonic-gate put1(void)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	/* len1 = length of left side */
3697c478bd9Sstevel@tonic-gate 	/* nchars = num of chars including tabs */
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	char	*bp;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if ((bp = fgetline(fdes1)) != NULL) {
3757c478bd9Sstevel@tonic-gate 		len1 = getlen(0, bp);
3767c478bd9Sstevel@tonic-gate 		if ((!silent || change != ' ') && len1 != 0)
3777c478bd9Sstevel@tonic-gate 			putline(stdout, bp, nchars);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		if (oflag) {
3807c478bd9Sstevel@tonic-gate 		/*
3817c478bd9Sstevel@tonic-gate 		 * put left side either to output file
3827c478bd9Sstevel@tonic-gate 		 * if identical to right
3837c478bd9Sstevel@tonic-gate 		 * or left temp file if not
3847c478bd9Sstevel@tonic-gate 		 */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 			if (change == ' ')
3877c478bd9Sstevel@tonic-gate 				putline(odes, bp, strlen(bp));
3887c478bd9Sstevel@tonic-gate 			else
3897c478bd9Sstevel@tonic-gate 				putline(left, bp, strlen(bp));
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		if (change != ' ')
3927c478bd9Sstevel@tonic-gate 			putmid(1);
3937c478bd9Sstevel@tonic-gate 		num1++;
3947c478bd9Sstevel@tonic-gate 		return (1);
3957c478bd9Sstevel@tonic-gate 	} else
3967c478bd9Sstevel@tonic-gate 		return (0);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static int
put2(void)4007c478bd9Sstevel@tonic-gate put2(void)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	char	*bp;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if ((bp = fgetline(fdes2)) != NULL) {
4057c478bd9Sstevel@tonic-gate 		(void) getlen((hlen + WGUTTER) % 8, bp);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		/*
4087c478bd9Sstevel@tonic-gate 		 * if the left and right are different they are always
4097c478bd9Sstevel@tonic-gate 		 * printed.
4107c478bd9Sstevel@tonic-gate 		 * If the left and right are identical
4117c478bd9Sstevel@tonic-gate 		 * right is only printed if leftonly is not specified
4127c478bd9Sstevel@tonic-gate 		 * or silent mode is not specified
4137c478bd9Sstevel@tonic-gate 		 * or the right contains other than white space (len1 !=0)
4147c478bd9Sstevel@tonic-gate 		 */
4157c478bd9Sstevel@tonic-gate 		if (change != ' ') {
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		/*
4187c478bd9Sstevel@tonic-gate 		 * put right side to right temp file only
4197c478bd9Sstevel@tonic-gate 		 * because left side was written to output for
4207c478bd9Sstevel@tonic-gate 		 * identical lines
4217c478bd9Sstevel@tonic-gate 		 */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 			if (oflag)
4247c478bd9Sstevel@tonic-gate 				putline(right, bp, strlen(bp));
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 			if (midflg == 0)
4277c478bd9Sstevel@tonic-gate 				putmid(1);
4287c478bd9Sstevel@tonic-gate 			putline(stdout, bp, nchars);
4297c478bd9Sstevel@tonic-gate 		} else
4307c478bd9Sstevel@tonic-gate 			if (!silent && !leftonly && len1 != 0) {
4317c478bd9Sstevel@tonic-gate 				if (midflg == 0)
4327c478bd9Sstevel@tonic-gate 					putmid(1);
4337c478bd9Sstevel@tonic-gate 				putline(stdout, bp, nchars);
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 		num2++;
4367c478bd9Sstevel@tonic-gate 		len1 = 0;
4377c478bd9Sstevel@tonic-gate 		return (1);
4387c478bd9Sstevel@tonic-gate 	} else {
4397c478bd9Sstevel@tonic-gate 		len1 = 0;
4407c478bd9Sstevel@tonic-gate 		return (0);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate static void
putline(FILE * file,char * start,int num)4457c478bd9Sstevel@tonic-gate putline(FILE *file, char *start, int num)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	char	*cp, *end;
4487c478bd9Sstevel@tonic-gate 	int	i, len, d_col;
4497c478bd9Sstevel@tonic-gate 	wchar_t	wc;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	cp = start;
4527c478bd9Sstevel@tonic-gate 	end = cp + num;
4537c478bd9Sstevel@tonic-gate 	while (cp < end) {
4547c478bd9Sstevel@tonic-gate 		if (isascii(*cp)) {
4557c478bd9Sstevel@tonic-gate 			(void) putc(*cp++, file);
4567c478bd9Sstevel@tonic-gate 			continue;
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		if ((len = end - cp) > MB_LEN_MAX)
4607c478bd9Sstevel@tonic-gate 			len = MB_LEN_MAX;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, cp, len)) <= 0) {
4637c478bd9Sstevel@tonic-gate 			(void) putc(*cp++, file);
4647c478bd9Sstevel@tonic-gate 			continue;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		if ((d_col = wcwidth(wc)) <= 0)
4687c478bd9Sstevel@tonic-gate 			d_col = len;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		if ((cp + d_col) > end)
4717c478bd9Sstevel@tonic-gate 			return;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++)
4747c478bd9Sstevel@tonic-gate 			(void) putc(*cp++, file);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static int
cmd(char * start)4797c478bd9Sstevel@tonic-gate cmd(char *start)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	unsigned char	*cp;
4827c478bd9Sstevel@tonic-gate 	char	*cps;
4837c478bd9Sstevel@tonic-gate 	int	com;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (*start == '>' || *start == '<' || *start == '-' || *start == '.')
4867c478bd9Sstevel@tonic-gate 		return (0);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	cp = (unsigned char *)start;
4897c478bd9Sstevel@tonic-gate 	cps = start;
4907c478bd9Sstevel@tonic-gate 	while (isdigit(*cp))
4917c478bd9Sstevel@tonic-gate 		cp++;
4927c478bd9Sstevel@tonic-gate 	from1 = atoi(cps);
4937c478bd9Sstevel@tonic-gate 	to1 = from1;
4947c478bd9Sstevel@tonic-gate 	if (*cp == ',') {
4957c478bd9Sstevel@tonic-gate 		cp++;
4967c478bd9Sstevel@tonic-gate 		cps = (char *)cp;
4977c478bd9Sstevel@tonic-gate 		while (isdigit(*cp))
4987c478bd9Sstevel@tonic-gate 			cp++;
4997c478bd9Sstevel@tonic-gate 		to1 = atoi(cps);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	com = *cp++;
5037c478bd9Sstevel@tonic-gate 	cps = (char *)cp;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	while (isdigit(*cp))
5067c478bd9Sstevel@tonic-gate 		cp++;
5077c478bd9Sstevel@tonic-gate 	from2 = atoi(cps);
5087c478bd9Sstevel@tonic-gate 	to2 = from2;
5097c478bd9Sstevel@tonic-gate 	if (*cp == ',') {
5107c478bd9Sstevel@tonic-gate 		cp++;
5117c478bd9Sstevel@tonic-gate 		cps = (char *)cp;
5127c478bd9Sstevel@tonic-gate 		while (isdigit(*cp))
5137c478bd9Sstevel@tonic-gate 			cp++;
5147c478bd9Sstevel@tonic-gate 		to2 = atoi(cps);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	return (com);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate static int
getlen(int startpos,char * buffer)5207c478bd9Sstevel@tonic-gate getlen(int startpos, char *buffer)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * get the length of the string in buffer
5247c478bd9Sstevel@tonic-gate 	 *  expand tabs to next multiple of 8
5257c478bd9Sstevel@tonic-gate 	 */
5267c478bd9Sstevel@tonic-gate 	unsigned char	*cp;
5277c478bd9Sstevel@tonic-gate 	int	slen, tlen, len, d_col;
5287c478bd9Sstevel@tonic-gate 	int	notspace;
5297c478bd9Sstevel@tonic-gate 	wchar_t	wc;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	nchars = 0;
5327c478bd9Sstevel@tonic-gate 	notspace = 0;
5337c478bd9Sstevel@tonic-gate 	tlen = startpos;
5347c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *)buffer; (*cp != '\n') && (*cp); cp++) {
5357c478bd9Sstevel@tonic-gate 		if (*cp == '\t') {
5367c478bd9Sstevel@tonic-gate 			slen = tlen;
5377c478bd9Sstevel@tonic-gate 			tlen += 8 - (tlen % 8);
5387c478bd9Sstevel@tonic-gate 			if (tlen >= hlen) {
5397c478bd9Sstevel@tonic-gate 				tlen = slen;
5407c478bd9Sstevel@tonic-gate 				break;
5417c478bd9Sstevel@tonic-gate 			}
5427c478bd9Sstevel@tonic-gate 			nchars++;
5437c478bd9Sstevel@tonic-gate 			continue;
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		if (isascii(*cp)) {
5477c478bd9Sstevel@tonic-gate 			slen = tlen;
5487c478bd9Sstevel@tonic-gate 			tlen++;
5497c478bd9Sstevel@tonic-gate 			if (tlen >= hlen) {
5507c478bd9Sstevel@tonic-gate 				tlen = slen;
5517c478bd9Sstevel@tonic-gate 				break;
5527c478bd9Sstevel@tonic-gate 			}
5537c478bd9Sstevel@tonic-gate 			if (!isspace(*cp))
5547c478bd9Sstevel@tonic-gate 				notspace = 1;
5557c478bd9Sstevel@tonic-gate 			nchars++;
5567c478bd9Sstevel@tonic-gate 			continue;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)cp, MB_LEN_MAX)) <= 0) {
5607c478bd9Sstevel@tonic-gate 			slen = tlen;
5617c478bd9Sstevel@tonic-gate 			tlen++;
5627c478bd9Sstevel@tonic-gate 			if (tlen >= hlen) {
5637c478bd9Sstevel@tonic-gate 				tlen = slen;
5647c478bd9Sstevel@tonic-gate 				break;
5657c478bd9Sstevel@tonic-gate 			}
5667c478bd9Sstevel@tonic-gate 			notspace = 1;
5677c478bd9Sstevel@tonic-gate 			nchars++;
5687c478bd9Sstevel@tonic-gate 			continue;
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		if ((d_col = wcwidth(wc)) <= 0)
5727c478bd9Sstevel@tonic-gate 			d_col = len;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		slen = tlen;
5757c478bd9Sstevel@tonic-gate 		tlen += d_col;
5767c478bd9Sstevel@tonic-gate 		if (tlen > hlen) {
5777c478bd9Sstevel@tonic-gate 			tlen = slen;
5787c478bd9Sstevel@tonic-gate 			break;
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 		notspace = 1;
5817c478bd9Sstevel@tonic-gate 		cp += len - 1;
5827c478bd9Sstevel@tonic-gate 		nchars += len;
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	return (notspace ? tlen : 0);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate static void
putmid(int bflag)5887c478bd9Sstevel@tonic-gate putmid(int bflag)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	int	i;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * len1 set by getlen to the possibly truncated
5947c478bd9Sstevel@tonic-gate 	 *  length of left side
5957c478bd9Sstevel@tonic-gate 	 *  hlen is length of half line
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	midflg = 1;
5997c478bd9Sstevel@tonic-gate 	if (bflag) {
6007c478bd9Sstevel@tonic-gate 		for (i = 0; i < hlen - len1; i++)
6017c478bd9Sstevel@tonic-gate 			(void) putc(' ', stdout);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	(void) fputs(twoblanks, stdout);
6047c478bd9Sstevel@tonic-gate 	(void) putc((int)change, stdout);
6057c478bd9Sstevel@tonic-gate 	(void) fputs(twoblanks, stdout);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate static void
error(char * s1,char * s2)6097c478bd9Sstevel@tonic-gate error(char *s1, char *s2)
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "sdiff: ");
6127c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, s1, s2);
6137c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
6147c478bd9Sstevel@tonic-gate 	sremove();
6157c478bd9Sstevel@tonic-gate 	exit(2);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static void
onintr(void)6197c478bd9Sstevel@tonic-gate onintr(void)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	sremove();
6227c478bd9Sstevel@tonic-gate 	exit(rcode);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate static void
sremove(void)6267c478bd9Sstevel@tonic-gate sremove(void)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	if (ltemp) {
6297c478bd9Sstevel@tonic-gate 		(void) unlink(ltemp);
6307c478bd9Sstevel@tonic-gate 		free(ltemp);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	if (rtemp) {
6337c478bd9Sstevel@tonic-gate 		(void) unlink(rtemp);
6347c478bd9Sstevel@tonic-gate 		free(rtemp);
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 	if (temp) {
6377c478bd9Sstevel@tonic-gate 		(void) unlink(temp);
6387c478bd9Sstevel@tonic-gate 		free(temp);
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate static void
cmdin(void)6437c478bd9Sstevel@tonic-gate cmdin(void)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	char	*cp, *ename;
6467c478bd9Sstevel@tonic-gate 	int	notacc;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	(void) fclose(left);
6497c478bd9Sstevel@tonic-gate 	(void) fclose(right);
6507c478bd9Sstevel@tonic-gate 	notacc = 1;
6517c478bd9Sstevel@tonic-gate 	while (notacc) {
6527c478bd9Sstevel@tonic-gate 		(void) putc(PROMPT, stdout);
6537c478bd9Sstevel@tonic-gate 		if ((cp = fgets(inbuf, 10, stdin)) == NULL) {
6547c478bd9Sstevel@tonic-gate 			(void) putc('\n', stdout);
6557c478bd9Sstevel@tonic-gate 			break;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 		switch (*cp) {
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		case 's':
6607c478bd9Sstevel@tonic-gate 			silent = 1;
6617c478bd9Sstevel@tonic-gate 			break;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		case 'v':
6647c478bd9Sstevel@tonic-gate 			silent = 0;
6657c478bd9Sstevel@tonic-gate 			break;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		case 'q':
6687c478bd9Sstevel@tonic-gate 			sremove();
6697c478bd9Sstevel@tonic-gate 			exit(rcode);
6707c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
6717c478bd9Sstevel@tonic-gate 			break;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		case 'l':
6747c478bd9Sstevel@tonic-gate 			cpp(ltemp, left, odes);
6757c478bd9Sstevel@tonic-gate 			notacc = 0;
6767c478bd9Sstevel@tonic-gate 			break;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		case 'r':
6797c478bd9Sstevel@tonic-gate 			cpp(rtemp, right, odes);
6807c478bd9Sstevel@tonic-gate 			notacc = 0;
6817c478bd9Sstevel@tonic-gate 			break;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 		case 'e':
6847c478bd9Sstevel@tonic-gate 			while (*++cp == ' ')
6857c478bd9Sstevel@tonic-gate 				;
6867c478bd9Sstevel@tonic-gate 			switch (*cp) {
6877c478bd9Sstevel@tonic-gate 			case 'l':
6887c478bd9Sstevel@tonic-gate 			case '<':
6897c478bd9Sstevel@tonic-gate 				notacc = 0;
6907c478bd9Sstevel@tonic-gate 				ename = ltemp;
6917c478bd9Sstevel@tonic-gate 				edit(ename);
6927c478bd9Sstevel@tonic-gate 				break;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 			case 'r':
6957c478bd9Sstevel@tonic-gate 			case '>':
6967c478bd9Sstevel@tonic-gate 				notacc = 0;
6977c478bd9Sstevel@tonic-gate 				ename = rtemp;
6987c478bd9Sstevel@tonic-gate 				edit(ename);
6997c478bd9Sstevel@tonic-gate 				break;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			case 'b':
7027c478bd9Sstevel@tonic-gate 			case '|':
7037c478bd9Sstevel@tonic-gate 				if ((tempdes = fopen(temp, "w")) == NULL)
7047c478bd9Sstevel@tonic-gate 					error(gettext(
7057c478bd9Sstevel@tonic-gate 						"Cannot open temp file %s"),
7067c478bd9Sstevel@tonic-gate 						temp);
7077c478bd9Sstevel@tonic-gate 				cpp(ltemp, left, tempdes);
7087c478bd9Sstevel@tonic-gate 				cpp(rtemp, right, tempdes);
7097c478bd9Sstevel@tonic-gate 				(void) fclose(tempdes);
7107c478bd9Sstevel@tonic-gate 				notacc = 0;
7117c478bd9Sstevel@tonic-gate 				ename = temp;
7127c478bd9Sstevel@tonic-gate 				edit(ename);
7137c478bd9Sstevel@tonic-gate 				break;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 			case '\n':
7167c478bd9Sstevel@tonic-gate 				if ((tempdes = fopen(temp, "w")) == NULL)
7177c478bd9Sstevel@tonic-gate 					error(gettext(
7187c478bd9Sstevel@tonic-gate 						"Cannot open temp file %s"),
7197c478bd9Sstevel@tonic-gate 						temp);
7207c478bd9Sstevel@tonic-gate 				(void) fclose(tempdes);
7217c478bd9Sstevel@tonic-gate 				notacc = 0;
7227c478bd9Sstevel@tonic-gate 				ename = temp;
7237c478bd9Sstevel@tonic-gate 				edit(ename);
7247c478bd9Sstevel@tonic-gate 				break;
7257c478bd9Sstevel@tonic-gate 			default:
7267c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
7277c478bd9Sstevel@tonic-gate 					"Illegal command %s reenter\n"),
7287c478bd9Sstevel@tonic-gate 					cp);
7297c478bd9Sstevel@tonic-gate 				break;
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 			if (notacc == 0)
7327c478bd9Sstevel@tonic-gate 				cpp(ename, tempdes, odes);
7337c478bd9Sstevel@tonic-gate 			break;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		default:
7367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
7377c478bd9Sstevel@tonic-gate 				"Illegal command reenter\n"));
7387c478bd9Sstevel@tonic-gate 			break;
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate static void
cpp(char * from,FILE * fromdes,FILE * todes)7447c478bd9Sstevel@tonic-gate cpp(char *from, FILE *fromdes, FILE *todes)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	char	tempbuf[BMAX + 1];
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	if ((fromdes = fopen(from, "r")) == NULL)
7497c478bd9Sstevel@tonic-gate 		error(gettext(
7507c478bd9Sstevel@tonic-gate 			"cpp: Cannot open %s"), from);
7517c478bd9Sstevel@tonic-gate 	while ((fgets(tempbuf, BMAX, fromdes) != NULL))
7527c478bd9Sstevel@tonic-gate 		(void) fputs(tempbuf, todes);
7537c478bd9Sstevel@tonic-gate 	(void) fclose(fromdes);
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate static void
edit(char * file)7577c478bd9Sstevel@tonic-gate edit(char *file)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	int	i;
7607c478bd9Sstevel@tonic-gate 	pid_t	pid;
7617c478bd9Sstevel@tonic-gate 	void (*oldintr)(int);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	switch (pid = fork()) {
7647c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
7657c478bd9Sstevel@tonic-gate 		error(gettext("Cannot fork"), NULL);
7667c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
7677c478bd9Sstevel@tonic-gate 		break;
7687c478bd9Sstevel@tonic-gate 	case (pid_t)0:
7697c478bd9Sstevel@tonic-gate 		(void) execl("/usr/bin/ed", "ed", file, NULL);
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, SIG_IGN);	/* ignore interrupts in ed */
7737c478bd9Sstevel@tonic-gate 	while (pid != wait(&i))
7747c478bd9Sstevel@tonic-gate 		;
7757c478bd9Sstevel@tonic-gate 	/* restore previous interrupt proc */
7767c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate static char *
filename(char * pa1,char * pa2)7807c478bd9Sstevel@tonic-gate filename(char *pa1, char *pa2)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	int	c;
7837c478bd9Sstevel@tonic-gate 	char 	*a1, *b1, *a2;
7847c478bd9Sstevel@tonic-gate 	struct stat	stbuf;
7857c478bd9Sstevel@tonic-gate 	a1 = pa1;
7867c478bd9Sstevel@tonic-gate 	a2 = pa2;
7877c478bd9Sstevel@tonic-gate 	if (stat(a1, &stbuf) != -1 && ((stbuf.st_mode&S_IFMT) == S_IFDIR)) {
7887c478bd9Sstevel@tonic-gate 		b1 = pa1 = (char *)malloc(strlen(a1) + strlen(a2) + 2);
7897c478bd9Sstevel@tonic-gate 		while (*b1++ = *a1++);
7907c478bd9Sstevel@tonic-gate 		b1[-1] = '/';
7917c478bd9Sstevel@tonic-gate 		a1 = b1;
7927c478bd9Sstevel@tonic-gate 		while (*a1++ = *a2++)
7937c478bd9Sstevel@tonic-gate 			if (*a2 && *a2 != '/' && a2[-1] == '/')
7947c478bd9Sstevel@tonic-gate 				a1 = b1;
7957c478bd9Sstevel@tonic-gate 	} else if (a1[0] == '-' && a1[1] == 0 && temp == 0) {
7967c478bd9Sstevel@tonic-gate 		if (fstat(fileno(stdin), &stbuf) == -1)
7977c478bd9Sstevel@tonic-gate 			error(gettext("Cannot process stdin"), NULL);
7987c478bd9Sstevel@tonic-gate 		pa1 = temp = strdup("/tmp/sdiffXXXXXX");
7997c478bd9Sstevel@tonic-gate 		if ((tempfd = mkstemp(temp)) == -1 ||
8007c478bd9Sstevel@tonic-gate 			(tempdes = fdopen(tempfd, "w")) == NULL)
8017c478bd9Sstevel@tonic-gate 				error(gettext("Cannot open/create temp %s"),
8027c478bd9Sstevel@tonic-gate 					temp);
8037c478bd9Sstevel@tonic-gate 		while ((c = getc(stdin)) != EOF)
8047c478bd9Sstevel@tonic-gate 			(void) putc(c, tempdes);
8057c478bd9Sstevel@tonic-gate 		(void) fclose(tempdes);
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 	return (pa1);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  * like fgets, but reads upto and including a newline,
8127c478bd9Sstevel@tonic-gate  * the data is stored in a reusable dynamic buffer that grows to fit
8137c478bd9Sstevel@tonic-gate  * the largest line in the file, the buffer is NULL terminated
8147c478bd9Sstevel@tonic-gate  * returns a pointer to the dynamic buffer.
8157c478bd9Sstevel@tonic-gate  */
8167c478bd9Sstevel@tonic-gate static char *
fgetline(FILE * fp)8177c478bd9Sstevel@tonic-gate fgetline(FILE *fp)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	static char	*bp = NULL;
8207c478bd9Sstevel@tonic-gate 	static int	blen = 0;
8217c478bd9Sstevel@tonic-gate 	int	sl;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
8247c478bd9Sstevel@tonic-gate 		/* allocate it for the first time */
8257c478bd9Sstevel@tonic-gate 		bp = (char *)malloc(BUFSIZ);
8267c478bd9Sstevel@tonic-gate 		if (bp == NULL)
8277c478bd9Sstevel@tonic-gate 			error(gettext("fgetline: malloc failed"), NULL);
8287c478bd9Sstevel@tonic-gate 		blen = BUFSIZ;
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/* check for error or nothing read */
8327c478bd9Sstevel@tonic-gate 	if (fgets(bp, blen, fp) == NULL)
8337c478bd9Sstevel@tonic-gate 		return (NULL);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	if (feof(fp))
8367c478bd9Sstevel@tonic-gate 		return (bp);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	while ((sl = strlen(bp)) == blen-1 && *(bp+blen-2) != '\n') {
8397c478bd9Sstevel@tonic-gate 		/* still more data, grow the buffer */
8407c478bd9Sstevel@tonic-gate 		blen *= 2;
8417c478bd9Sstevel@tonic-gate 		bp = (char *)realloc(bp, blen);
8427c478bd9Sstevel@tonic-gate 		if (bp == NULL)
8437c478bd9Sstevel@tonic-gate 			error(gettext("fgetline: realloc failed"), NULL);
8447c478bd9Sstevel@tonic-gate 		/* continue reading and add to end of buffer */
8457c478bd9Sstevel@tonic-gate 		(void) fgets(bp+sl, blen-sl, fp);
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	return (bp);
8487c478bd9Sstevel@tonic-gate }
849