xref: /titanic_41/usr/src/cmd/exstr/exstr.c (revision 0d8b53344490faab75b127936edb53a2f1e0dc01)
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  */
22884db8abSpetede /*
23884db8abSpetede  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24884db8abSpetede  * Use is subject to license terms.
25884db8abSpetede  */
26884db8abSpetede 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
31*0d8b5334Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <setjmp.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* external functions */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate extern	int	getopt();
437c478bd9Sstevel@tonic-gate extern	void	exit();
447c478bd9Sstevel@tonic-gate extern	int	atoi();
457c478bd9Sstevel@tonic-gate extern	int	_filbuf();
467c478bd9Sstevel@tonic-gate extern	char	*optarg;
477c478bd9Sstevel@tonic-gate extern	int	optind, opterr;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* static functions */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static	void	extract();
527c478bd9Sstevel@tonic-gate static	void	replace();
537c478bd9Sstevel@tonic-gate static	void	yankstr();
547c478bd9Sstevel@tonic-gate static	void	badformat();
557c478bd9Sstevel@tonic-gate static	void	prstr();
567c478bd9Sstevel@tonic-gate static	int	getachar();
577c478bd9Sstevel@tonic-gate static  void	usage();
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* static variables */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static	int	eflg;		/* find strings in source file(s) */
627c478bd9Sstevel@tonic-gate static	int	dflg;		/* use replaced string a second argument */
637c478bd9Sstevel@tonic-gate static  int	rflg;		/* replace strings by function calls */
647c478bd9Sstevel@tonic-gate static  int	errflg;		/* syntax error on command line */
657c478bd9Sstevel@tonic-gate static  char	*Fname;		/* name of source file */
667c478bd9Sstevel@tonic-gate static  int	Lineno;		/* line number in source file */
677c478bd9Sstevel@tonic-gate static	int	Posno;		/* character position within line */
687c478bd9Sstevel@tonic-gate static  int	flag;		/* sets when newline is encountered */
697c478bd9Sstevel@tonic-gate static  jmp_buf	to_eof;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
72*0d8b5334Sceastha int
main(int argc,char * argv[])73*0d8b5334Sceastha main(int argc, char *argv[])
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	int	ch;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "erd")) != -1)
787c478bd9Sstevel@tonic-gate 		switch (ch) {
797c478bd9Sstevel@tonic-gate 		    case 'e':
807c478bd9Sstevel@tonic-gate 			if (rflg)
817c478bd9Sstevel@tonic-gate 				errflg++;
827c478bd9Sstevel@tonic-gate 			else
837c478bd9Sstevel@tonic-gate 				eflg++;
847c478bd9Sstevel@tonic-gate 			continue;
857c478bd9Sstevel@tonic-gate 		    case 'r':
867c478bd9Sstevel@tonic-gate 			if (eflg)
877c478bd9Sstevel@tonic-gate 				errflg++;
887c478bd9Sstevel@tonic-gate 			else
897c478bd9Sstevel@tonic-gate 				rflg++;
907c478bd9Sstevel@tonic-gate 			continue;
917c478bd9Sstevel@tonic-gate 		    case 'd':
927c478bd9Sstevel@tonic-gate 			if (eflg)
937c478bd9Sstevel@tonic-gate 				errflg++;
947c478bd9Sstevel@tonic-gate 			else
957c478bd9Sstevel@tonic-gate 				dflg++;
967c478bd9Sstevel@tonic-gate 			continue;
977c478bd9Sstevel@tonic-gate 		    default:
987c478bd9Sstevel@tonic-gate 			errflg++;
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 	if (optind ==  argc || errflg)
1017c478bd9Sstevel@tonic-gate 		usage();
1027c478bd9Sstevel@tonic-gate 	if (!rflg)
1037c478bd9Sstevel@tonic-gate 		for (; optind < argc; optind++)
1047c478bd9Sstevel@tonic-gate 			extract(argv[optind]);
1057c478bd9Sstevel@tonic-gate 	else  {
1067c478bd9Sstevel@tonic-gate 		if (optind+1 != argc)
1077c478bd9Sstevel@tonic-gate 			usage();
1087c478bd9Sstevel@tonic-gate 		replace(argv[optind]);
1097c478bd9Sstevel@tonic-gate 	}
110884db8abSpetede 	return (0);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static	void
extract(name)1147c478bd9Sstevel@tonic-gate extract(name)
1157c478bd9Sstevel@tonic-gate char	*name;
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	if (freopen(name, "r", stdin) == NULL) {
118884db8abSpetede 		(void) fprintf(
119884db8abSpetede 		    stderr, "exstr: ERROR: couldn't open file '%s'\n", name);
1207c478bd9Sstevel@tonic-gate 		exit(1);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	Fname = name;
1237c478bd9Sstevel@tonic-gate 	flag = 1;
1247c478bd9Sstevel@tonic-gate 	Lineno = 0;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (setjmp(to_eof) != 0)
1277c478bd9Sstevel@tonic-gate 		return;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	for (;;) {
1307c478bd9Sstevel@tonic-gate 		char ch;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		ch = getachar();
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		switch (ch) {
1357c478bd9Sstevel@tonic-gate 		case '#':
1367c478bd9Sstevel@tonic-gate 			if (Posno != 0)
1377c478bd9Sstevel@tonic-gate 				continue;
1387c478bd9Sstevel@tonic-gate 			do {
1397c478bd9Sstevel@tonic-gate 				ch = getachar();
1407c478bd9Sstevel@tonic-gate 			} while (isspace(ch));
1417c478bd9Sstevel@tonic-gate 			if (ch == 'd')
1427c478bd9Sstevel@tonic-gate 				continue;
1437c478bd9Sstevel@tonic-gate 			while (getachar() != '\n');
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case '"':
1467c478bd9Sstevel@tonic-gate 			yankstr();
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case '\'':
1497c478bd9Sstevel@tonic-gate 			while ((ch = getachar()) != '\'')
1507c478bd9Sstevel@tonic-gate 				if (ch == '\\')
1517c478bd9Sstevel@tonic-gate 					ch = getachar();
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		case '/':
1557c478bd9Sstevel@tonic-gate 			ch = getachar();
1567c478bd9Sstevel@tonic-gate 			if (ch == '*') {
1577c478bd9Sstevel@tonic-gate 				int level = 0;
1587c478bd9Sstevel@tonic-gate 				while (level != 2) {
1597c478bd9Sstevel@tonic-gate 					ch = getachar();
1607c478bd9Sstevel@tonic-gate 					if (level == 0 && ch == '*')
1617c478bd9Sstevel@tonic-gate 						level++;
1627c478bd9Sstevel@tonic-gate 					else if (level == 1 && ch == '/')
1637c478bd9Sstevel@tonic-gate 						level++;
1647c478bd9Sstevel@tonic-gate 					else
1657c478bd9Sstevel@tonic-gate 						level = 0;
1667c478bd9Sstevel@tonic-gate 				}
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static	void
yankstr()1747c478bd9Sstevel@tonic-gate yankstr()
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	char cc;
1777c478bd9Sstevel@tonic-gate 	char dbuf[BUFSIZ];
1787c478bd9Sstevel@tonic-gate 	register char *dp = dbuf;
1797c478bd9Sstevel@tonic-gate 	int saved_posno;
1807c478bd9Sstevel@tonic-gate 	int saved_lineno;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	saved_posno = Posno;
1837c478bd9Sstevel@tonic-gate 	saved_lineno = Lineno;
1847c478bd9Sstevel@tonic-gate 	while ((cc = getachar()) != '"') {
1857c478bd9Sstevel@tonic-gate 		if (cc == '\\') {
1867c478bd9Sstevel@tonic-gate 			*dp++ = cc;
1877c478bd9Sstevel@tonic-gate 			cc = getachar();
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		if (cc == '\n') {
1907c478bd9Sstevel@tonic-gate 			dp--;
1917c478bd9Sstevel@tonic-gate 			continue;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 		*dp++ = cc;
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	*dp = 0;
1967c478bd9Sstevel@tonic-gate 	prstr(dbuf, saved_lineno, saved_posno);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static	void
prstr(cp,lineno,posno)2007c478bd9Sstevel@tonic-gate prstr(cp, lineno, posno)
2017c478bd9Sstevel@tonic-gate register char *cp;
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	if (eflg)
204884db8abSpetede 		(void) fprintf(stdout, "%s:%d:%d:::%s\n", Fname, lineno, posno,
205884db8abSpetede 		    cp);
2067c478bd9Sstevel@tonic-gate 	else
2077c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:%s\n", Fname, cp);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static	void
usage()2127c478bd9Sstevel@tonic-gate usage()
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: exstr [-e] files\n");
2157c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "or   : exstr -r [-d] file\n");
2167c478bd9Sstevel@tonic-gate 	exit(1);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static	int
getachar()2207c478bd9Sstevel@tonic-gate getachar()
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	int cc;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	cc = getchar();
2257c478bd9Sstevel@tonic-gate 	if (flag) {
2267c478bd9Sstevel@tonic-gate 		Lineno++;
2277c478bd9Sstevel@tonic-gate 		Posno = 0;
2287c478bd9Sstevel@tonic-gate 		flag = 0;
2297c478bd9Sstevel@tonic-gate 	} else
2307c478bd9Sstevel@tonic-gate 		Posno++;
2317c478bd9Sstevel@tonic-gate 	if (cc == EOF)
2327c478bd9Sstevel@tonic-gate 		longjmp(to_eof, 1);
2337c478bd9Sstevel@tonic-gate 	if (cc == '\n')
2347c478bd9Sstevel@tonic-gate 		flag = 1;
2357c478bd9Sstevel@tonic-gate 	return (cc);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static void
replace(name)2407c478bd9Sstevel@tonic-gate replace(name)
2417c478bd9Sstevel@tonic-gate char	*name;
2427c478bd9Sstevel@tonic-gate {
243884db8abSpetede 	char	linebuf[BUFSIZ];
2447c478bd9Sstevel@tonic-gate 	char	*cp;
2457c478bd9Sstevel@tonic-gate 	int	curlineno;		/* line number in strings file */
2467c478bd9Sstevel@tonic-gate 	int	curposno;		/* character position in string file */
2477c478bd9Sstevel@tonic-gate 	int	savelineno = 0;
2487c478bd9Sstevel@tonic-gate 	int	curmsgno;		/* message number in strings file */
2497c478bd9Sstevel@tonic-gate 	int	wrong_msg;		/* invalid message number */
2507c478bd9Sstevel@tonic-gate 	int	cont_str = 0;		/* string continues in the next line */
2517c478bd9Sstevel@tonic-gate 	char	*repstr;
2527c478bd9Sstevel@tonic-gate 	char	repbuf[BUFSIZ], *repbufp;
2537c478bd9Sstevel@tonic-gate 	char	curline[BUFSIZ];
2547c478bd9Sstevel@tonic-gate 	char	outbuf[BUFSIZ];
255884db8abSpetede 	/* keeps track of character position within input file */
256884db8abSpetede 	char	*inp;
257884db8abSpetede 	/* keeps track of character position within output buffer */
258884db8abSpetede 	char	*outp;
2597c478bd9Sstevel@tonic-gate 	char	*msgfile;
2607c478bd9Sstevel@tonic-gate 	FILE	*fi;			/* input source file pointer */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	inp = linebuf;
2637c478bd9Sstevel@tonic-gate 	outp = outbuf;
2647c478bd9Sstevel@tonic-gate 	linebuf[0] = '\0';
2657c478bd9Sstevel@tonic-gate 	/* open input C source file */
2667c478bd9Sstevel@tonic-gate 	if ((fi = fopen(name, "r")) == (FILE *)NULL) {
267884db8abSpetede 		(void) fprintf(stderr,
268884db8abSpetede 		    "exstr: ERROR: couldn't open file '%s'\n", name);
2697c478bd9Sstevel@tonic-gate 		exit(1);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	Fname = name;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "extern char *gettxt();\n");
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/* process file containing the list of strings */
276884db8abSpetede 	while (fgets(repbuf, sizeof (repbuf), stdin) != (char *)NULL) {
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		wrong_msg = 0;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		/* save a copy of the current line */
2817c478bd9Sstevel@tonic-gate 		(void) strcpy(curline, repbuf);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		/* take apart the input string */
2847c478bd9Sstevel@tonic-gate 		repbufp = strchr(repbuf, ':');
2857c478bd9Sstevel@tonic-gate 		if (repbufp == (char *)NULL)
2867c478bd9Sstevel@tonic-gate 			badformat(curline);
2877c478bd9Sstevel@tonic-gate 		*repbufp++ = '\0';
2887c478bd9Sstevel@tonic-gate 		/* verify that string belongs to the input C source file */
2897c478bd9Sstevel@tonic-gate 		if (strcmp(repbuf, name) != NULL)
2907c478bd9Sstevel@tonic-gate 			continue;
2917c478bd9Sstevel@tonic-gate 		repstr = strchr(repbufp, ':');
2927c478bd9Sstevel@tonic-gate 		if (repstr == (char *)NULL)
2937c478bd9Sstevel@tonic-gate 			badformat(curline);
2947c478bd9Sstevel@tonic-gate 		*repstr++ = '\0';
2957c478bd9Sstevel@tonic-gate 		curlineno = atoi(repbufp);
2967c478bd9Sstevel@tonic-gate 		if (curlineno < savelineno) {
297884db8abSpetede 			(void) fprintf(stderr,
298884db8abSpetede 			    "exstr: ERROR: stdin: line out of order\n");
2997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s", curline);
3007c478bd9Sstevel@tonic-gate 			exit(1);
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 		savelineno = curlineno;
3037c478bd9Sstevel@tonic-gate 		repbufp = repstr;
3047c478bd9Sstevel@tonic-gate 		repstr = strchr(repbufp, ':');
3057c478bd9Sstevel@tonic-gate 		if (repstr == (char *)NULL)
3067c478bd9Sstevel@tonic-gate 			badformat(curline);
3077c478bd9Sstevel@tonic-gate 		repstr[strlen(repstr) - 1 ] = '\0';
3087c478bd9Sstevel@tonic-gate 		*repstr++ = '\0';
3097c478bd9Sstevel@tonic-gate 		curposno = atoi(repbufp);
3107c478bd9Sstevel@tonic-gate 		repbufp = repstr;
3117c478bd9Sstevel@tonic-gate 		repstr = strchr(repbufp, ':');
3127c478bd9Sstevel@tonic-gate 		if (repstr == (char *)NULL)
3137c478bd9Sstevel@tonic-gate 			badformat(curline);
3147c478bd9Sstevel@tonic-gate 		*repstr++ = '\0';
3157c478bd9Sstevel@tonic-gate 		msgfile = repbufp;
3167c478bd9Sstevel@tonic-gate 		if (strlen(msgfile) > (size_t)14 || *msgfile == '\0') {
317884db8abSpetede 			(void) fprintf(stderr,
318884db8abSpetede 			    "exstr: ERROR: stdin: invalid message file name "
319884db8abSpetede 			    "'%s'\n", msgfile);
3207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s", curline);
3217c478bd9Sstevel@tonic-gate 			exit(1);
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		repbufp = repstr;
3247c478bd9Sstevel@tonic-gate 		repstr = strchr(repbufp, ':');
3257c478bd9Sstevel@tonic-gate 		if (repstr == (char *)NULL)
3267c478bd9Sstevel@tonic-gate 			badformat(curline);
3277c478bd9Sstevel@tonic-gate 		*repstr++ = '\0';
3287c478bd9Sstevel@tonic-gate 		cp = repbufp;
3297c478bd9Sstevel@tonic-gate 		while (*cp)
3307c478bd9Sstevel@tonic-gate 			if (!isdigit(*cp++)) {
3317c478bd9Sstevel@tonic-gate 				wrong_msg++;
3327c478bd9Sstevel@tonic-gate 				break;
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 		if (*repbufp == '\0' || wrong_msg) {
335884db8abSpetede 			(void) fprintf(stderr, "exstr: ERROR: stdin: invalid "
336884db8abSpetede 			    "message number '%s'\n", repbufp);
3377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s", curline);
3387c478bd9Sstevel@tonic-gate 			exit(1);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		curmsgno = atoi(repbufp);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		/* move up to this line */
3437c478bd9Sstevel@tonic-gate 		while (Lineno != curlineno) {
3447c478bd9Sstevel@tonic-gate 			if (outp != outbuf) {
3457c478bd9Sstevel@tonic-gate 				while (*inp != '\0')
3467c478bd9Sstevel@tonic-gate 					*outp++ = *inp++;
3477c478bd9Sstevel@tonic-gate 				*outp = '\0';
3487c478bd9Sstevel@tonic-gate 				(void) fputs(outbuf, stdout);
3497c478bd9Sstevel@tonic-gate 			} else if (*linebuf != '\0')
3507c478bd9Sstevel@tonic-gate 				(void) fputs(linebuf, stdout);
3517c478bd9Sstevel@tonic-gate 			outp = outbuf;
3527c478bd9Sstevel@tonic-gate 			inp = linebuf;
353884db8abSpetede 			if (fgets(linebuf,
354884db8abSpetede 			    sizeof (linebuf), fi) == (char *)NULL) {
3557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "read error\n");
3567c478bd9Sstevel@tonic-gate 				exit(1);
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			Lineno++;
3597c478bd9Sstevel@tonic-gate 			Posno = 0;
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 		if (Posno > curposno) {
362884db8abSpetede 			(void) fprintf(stderr,
363884db8abSpetede 			    "Bad input record line number %d\n", Lineno);
3647c478bd9Sstevel@tonic-gate 			exit(1);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		while (Posno != curposno) {
3677c478bd9Sstevel@tonic-gate 			*outp++ = *inp++;
3687c478bd9Sstevel@tonic-gate 			Posno++;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		if (*inp != '"') {
371884db8abSpetede 			(void) fprintf(stderr, "exstr: ERROR: cannot replace "
372884db8abSpetede 			    "string '%s' in line (%d) of file (%s)\n", repstr,
373884db8abSpetede 			    Lineno, Fname);
3747c478bd9Sstevel@tonic-gate 			exit(1);
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		/* check if string continues in next line */
377884db8abSpetede 		while (inp[strlen(inp)-2] == '\\' &&
378884db8abSpetede 		    inp[strlen(inp)-1] == '\n') {
379884db8abSpetede 			if (fgets(linebuf,
380884db8abSpetede 			    sizeof (linebuf), fi) == (char *)NULL) {
381884db8abSpetede 				(void) fprintf(stderr, "exstr: ERROR: read "
382884db8abSpetede 				    "error in file (%s)\n", Fname);
3837c478bd9Sstevel@tonic-gate 				exit(1);
3847c478bd9Sstevel@tonic-gate 			}
3857c478bd9Sstevel@tonic-gate 			cont_str++;
3867c478bd9Sstevel@tonic-gate 			Lineno++;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 		if (cont_str) {
3897c478bd9Sstevel@tonic-gate 			cp = linebuf;
390884db8abSpetede 			while (*cp != '\0' && *cp++ != '"')
391884db8abSpetede 				;
3927c478bd9Sstevel@tonic-gate 			if (*cp == '\0') {
393884db8abSpetede 				(void) fprintf(stderr, "exstr: ERROR: cannot "
394884db8abSpetede 				    "replace string '%s' in line (%d) of file "
395884db8abSpetede 				    "(%s)\n", repstr, Lineno, Fname);
3967c478bd9Sstevel@tonic-gate 				exit(1);
3977c478bd9Sstevel@tonic-gate 			}
3987c478bd9Sstevel@tonic-gate 			inp = cp;
3997c478bd9Sstevel@tonic-gate 			Posno = cp - linebuf;
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 		if (dflg)
402884db8abSpetede 			outp += snprintf(outp, BUFSIZ - (outp - outbuf),
403884db8abSpetede 			    "gettxt(\"%s:%d\", \"%s\")", msgfile, curmsgno,
404884db8abSpetede 			    repstr);
4057c478bd9Sstevel@tonic-gate 		else
406884db8abSpetede 			outp += snprintf(outp, BUFSIZ - (outp - outbuf),
407884db8abSpetede 			    "gettxt(\"%s:%d\", \"\")", msgfile, curmsgno);
4087c478bd9Sstevel@tonic-gate 		if (!cont_str) {
4097c478bd9Sstevel@tonic-gate 			inp += strlen(repstr)+2;
4107c478bd9Sstevel@tonic-gate 			Posno += strlen(repstr)+2;
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 		else
4137c478bd9Sstevel@tonic-gate 			cont_str = 0;
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 	if (outp != outbuf) {
4167c478bd9Sstevel@tonic-gate 		while (*inp != '\0')
4177c478bd9Sstevel@tonic-gate 			*outp++ = *inp++;
4187c478bd9Sstevel@tonic-gate 		*outp = '\0';
4197c478bd9Sstevel@tonic-gate 		(void) fputs(outbuf, stdout);
4207c478bd9Sstevel@tonic-gate 	}
421884db8abSpetede 	while (fgets(linebuf, sizeof (linebuf), fi) != (char *)NULL)
4227c478bd9Sstevel@tonic-gate 		(void) fputs(linebuf, stdout);
4237c478bd9Sstevel@tonic-gate 
424884db8abSpetede 	(void) fclose(fi);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate static	void
badformat(line)4287c478bd9Sstevel@tonic-gate badformat(line)
4297c478bd9Sstevel@tonic-gate char  	*line;
4307c478bd9Sstevel@tonic-gate {
431884db8abSpetede 	(void) fprintf(stderr, "exstr: ERROR: stdin: Badly formatted "
432884db8abSpetede 	    "replacement string\n%s", line);
4337c478bd9Sstevel@tonic-gate 	exit(1);
4347c478bd9Sstevel@tonic-gate }
435