xref: /titanic_44/usr/src/cmd/exstr/exstr.c (revision 884db8ab1a530d69f98f3ed409af0155ed5ae8ea)
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  */
22*884db8abSpetede /*
23*884db8abSpetede  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*884db8abSpetede  * Use is subject to license terms.
25*884db8abSpetede  */
26*884db8abSpetede 
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*884db8abSpetede #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
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 
727c478bd9Sstevel@tonic-gate main(argc, argv)
737c478bd9Sstevel@tonic-gate int	argc;
747c478bd9Sstevel@tonic-gate char	*argv[];
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	int	ch;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "erd")) != -1)
797c478bd9Sstevel@tonic-gate 		switch (ch) {
807c478bd9Sstevel@tonic-gate 		    case 'e':
817c478bd9Sstevel@tonic-gate 			if (rflg)
827c478bd9Sstevel@tonic-gate 				errflg++;
837c478bd9Sstevel@tonic-gate 			else
847c478bd9Sstevel@tonic-gate 				eflg++;
857c478bd9Sstevel@tonic-gate 			continue;
867c478bd9Sstevel@tonic-gate 		    case 'r':
877c478bd9Sstevel@tonic-gate 			if (eflg)
887c478bd9Sstevel@tonic-gate 				errflg++;
897c478bd9Sstevel@tonic-gate 			else
907c478bd9Sstevel@tonic-gate 				rflg++;
917c478bd9Sstevel@tonic-gate 			continue;
927c478bd9Sstevel@tonic-gate 		    case 'd':
937c478bd9Sstevel@tonic-gate 			if (eflg)
947c478bd9Sstevel@tonic-gate 				errflg++;
957c478bd9Sstevel@tonic-gate 			else
967c478bd9Sstevel@tonic-gate 				dflg++;
977c478bd9Sstevel@tonic-gate 			continue;
987c478bd9Sstevel@tonic-gate 		    default:
997c478bd9Sstevel@tonic-gate 			errflg++;
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 	if (optind ==  argc || errflg)
1027c478bd9Sstevel@tonic-gate 		usage();
1037c478bd9Sstevel@tonic-gate 	if (!rflg)
1047c478bd9Sstevel@tonic-gate 		for (; optind < argc; optind++)
1057c478bd9Sstevel@tonic-gate 			extract(argv[optind]);
1067c478bd9Sstevel@tonic-gate 	else  {
1077c478bd9Sstevel@tonic-gate 		if (optind+1 != argc)
1087c478bd9Sstevel@tonic-gate 			usage();
1097c478bd9Sstevel@tonic-gate 		replace(argv[optind]);
1107c478bd9Sstevel@tonic-gate 	}
111*884db8abSpetede 	return (0);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static	void
1157c478bd9Sstevel@tonic-gate extract(name)
1167c478bd9Sstevel@tonic-gate char	*name;
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	if (freopen(name, "r", stdin) == NULL) {
119*884db8abSpetede 		(void) fprintf(
120*884db8abSpetede 		    stderr, "exstr: ERROR: couldn't open file '%s'\n", name);
1217c478bd9Sstevel@tonic-gate 		exit(1);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	Fname = name;
1247c478bd9Sstevel@tonic-gate 	flag = 1;
1257c478bd9Sstevel@tonic-gate 	Lineno = 0;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (setjmp(to_eof) != 0)
1287c478bd9Sstevel@tonic-gate 		return;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	for (;;) {
1317c478bd9Sstevel@tonic-gate 		char ch;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		ch = getachar();
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		switch (ch) {
1367c478bd9Sstevel@tonic-gate 		case '#':
1377c478bd9Sstevel@tonic-gate 			if (Posno != 0)
1387c478bd9Sstevel@tonic-gate 				continue;
1397c478bd9Sstevel@tonic-gate 			do {
1407c478bd9Sstevel@tonic-gate 				ch = getachar();
1417c478bd9Sstevel@tonic-gate 			} while (isspace(ch));
1427c478bd9Sstevel@tonic-gate 			if (ch == 'd')
1437c478bd9Sstevel@tonic-gate 				continue;
1447c478bd9Sstevel@tonic-gate 			while (getachar() != '\n');
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		case '"':
1477c478bd9Sstevel@tonic-gate 			yankstr();
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 		case '\'':
1507c478bd9Sstevel@tonic-gate 			while ((ch = getachar()) != '\'')
1517c478bd9Sstevel@tonic-gate 				if (ch == '\\')
1527c478bd9Sstevel@tonic-gate 					ch = getachar();
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		case '/':
1567c478bd9Sstevel@tonic-gate 			ch = getachar();
1577c478bd9Sstevel@tonic-gate 			if (ch == '*') {
1587c478bd9Sstevel@tonic-gate 				int level = 0;
1597c478bd9Sstevel@tonic-gate 				while (level != 2) {
1607c478bd9Sstevel@tonic-gate 					ch = getachar();
1617c478bd9Sstevel@tonic-gate 					if (level == 0 && ch == '*')
1627c478bd9Sstevel@tonic-gate 						level++;
1637c478bd9Sstevel@tonic-gate 					else if (level == 1 && ch == '/')
1647c478bd9Sstevel@tonic-gate 						level++;
1657c478bd9Sstevel@tonic-gate 					else
1667c478bd9Sstevel@tonic-gate 						level = 0;
1677c478bd9Sstevel@tonic-gate 				}
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static	void
1757c478bd9Sstevel@tonic-gate yankstr()
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	char cc;
1787c478bd9Sstevel@tonic-gate 	char dbuf[BUFSIZ];
1797c478bd9Sstevel@tonic-gate 	register char *dp = dbuf;
1807c478bd9Sstevel@tonic-gate 	int saved_posno;
1817c478bd9Sstevel@tonic-gate 	int saved_lineno;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	saved_posno = Posno;
1847c478bd9Sstevel@tonic-gate 	saved_lineno = Lineno;
1857c478bd9Sstevel@tonic-gate 	while ((cc = getachar()) != '"') {
1867c478bd9Sstevel@tonic-gate 		if (cc == '\\') {
1877c478bd9Sstevel@tonic-gate 			*dp++ = cc;
1887c478bd9Sstevel@tonic-gate 			cc = getachar();
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 		if (cc == '\n') {
1917c478bd9Sstevel@tonic-gate 			dp--;
1927c478bd9Sstevel@tonic-gate 			continue;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		*dp++ = cc;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	*dp = 0;
1977c478bd9Sstevel@tonic-gate 	prstr(dbuf, saved_lineno, saved_posno);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static	void
2017c478bd9Sstevel@tonic-gate prstr(cp, lineno, posno)
2027c478bd9Sstevel@tonic-gate register char *cp;
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	if (eflg)
205*884db8abSpetede 		(void) fprintf(stdout, "%s:%d:%d:::%s\n", Fname, lineno, posno,
206*884db8abSpetede 		    cp);
2077c478bd9Sstevel@tonic-gate 	else
2087c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:%s\n", Fname, cp);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static	void
2137c478bd9Sstevel@tonic-gate usage()
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: exstr [-e] files\n");
2167c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "or   : exstr -r [-d] file\n");
2177c478bd9Sstevel@tonic-gate 	exit(1);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static	int
2217c478bd9Sstevel@tonic-gate getachar()
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	int cc;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	cc = getchar();
2267c478bd9Sstevel@tonic-gate 	if (flag) {
2277c478bd9Sstevel@tonic-gate 		Lineno++;
2287c478bd9Sstevel@tonic-gate 		Posno = 0;
2297c478bd9Sstevel@tonic-gate 		flag = 0;
2307c478bd9Sstevel@tonic-gate 	} else
2317c478bd9Sstevel@tonic-gate 		Posno++;
2327c478bd9Sstevel@tonic-gate 	if (cc == EOF)
2337c478bd9Sstevel@tonic-gate 		longjmp(to_eof, 1);
2347c478bd9Sstevel@tonic-gate 	if (cc == '\n')
2357c478bd9Sstevel@tonic-gate 		flag = 1;
2367c478bd9Sstevel@tonic-gate 	return (cc);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static void
2417c478bd9Sstevel@tonic-gate replace(name)
2427c478bd9Sstevel@tonic-gate char	*name;
2437c478bd9Sstevel@tonic-gate {
244*884db8abSpetede 	char	linebuf[BUFSIZ];
2457c478bd9Sstevel@tonic-gate 	char	*cp;
2467c478bd9Sstevel@tonic-gate 	int	curlineno;		/* line number in strings file */
2477c478bd9Sstevel@tonic-gate 	int	curposno;		/* character position in string file */
2487c478bd9Sstevel@tonic-gate 	int	savelineno = 0;
2497c478bd9Sstevel@tonic-gate 	int	curmsgno;		/* message number in strings file */
2507c478bd9Sstevel@tonic-gate 	int	wrong_msg;		/* invalid message number */
2517c478bd9Sstevel@tonic-gate 	int	cont_str = 0;		/* string continues in the next line */
2527c478bd9Sstevel@tonic-gate 	char	*repstr;
2537c478bd9Sstevel@tonic-gate 	char	repbuf[BUFSIZ], *repbufp;
2547c478bd9Sstevel@tonic-gate 	char	curline[BUFSIZ];
2557c478bd9Sstevel@tonic-gate 	char	outbuf[BUFSIZ];
256*884db8abSpetede 	/* keeps track of character position within input file */
257*884db8abSpetede 	char	*inp;
258*884db8abSpetede 	/* keeps track of character position within output buffer */
259*884db8abSpetede 	char	*outp;
2607c478bd9Sstevel@tonic-gate 	char	*msgfile;
2617c478bd9Sstevel@tonic-gate 	FILE	*fi;			/* input source file pointer */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	inp = linebuf;
2647c478bd9Sstevel@tonic-gate 	outp = outbuf;
2657c478bd9Sstevel@tonic-gate 	linebuf[0] = '\0';
2667c478bd9Sstevel@tonic-gate 	/* open input C source file */
2677c478bd9Sstevel@tonic-gate 	if ((fi = fopen(name, "r")) == (FILE *)NULL) {
268*884db8abSpetede 		(void) fprintf(stderr,
269*884db8abSpetede 		    "exstr: ERROR: couldn't open file '%s'\n", name);
2707c478bd9Sstevel@tonic-gate 		exit(1);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	Fname = name;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "extern char *gettxt();\n");
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* process file containing the list of strings */
277*884db8abSpetede 	while (fgets(repbuf, sizeof (repbuf), stdin) != (char *)NULL) {
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		wrong_msg = 0;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		/* save a copy of the current line */
2827c478bd9Sstevel@tonic-gate 		(void) strcpy(curline, repbuf);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		/* take apart the input string */
2857c478bd9Sstevel@tonic-gate 		repbufp = strchr(repbuf, ':');
2867c478bd9Sstevel@tonic-gate 		if (repbufp == (char *)NULL)
2877c478bd9Sstevel@tonic-gate 			badformat(curline);
2887c478bd9Sstevel@tonic-gate 		*repbufp++ = '\0';
2897c478bd9Sstevel@tonic-gate 		/* verify that string belongs to the input C source file */
2907c478bd9Sstevel@tonic-gate 		if (strcmp(repbuf, name) != NULL)
2917c478bd9Sstevel@tonic-gate 			continue;
2927c478bd9Sstevel@tonic-gate 		repstr = strchr(repbufp, ':');
2937c478bd9Sstevel@tonic-gate 		if (repstr == (char *)NULL)
2947c478bd9Sstevel@tonic-gate 			badformat(curline);
2957c478bd9Sstevel@tonic-gate 		*repstr++ = '\0';
2967c478bd9Sstevel@tonic-gate 		curlineno = atoi(repbufp);
2977c478bd9Sstevel@tonic-gate 		if (curlineno < savelineno) {
298*884db8abSpetede 			(void) fprintf(stderr,
299*884db8abSpetede 			    "exstr: ERROR: stdin: line out of order\n");
3007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s", curline);
3017c478bd9Sstevel@tonic-gate 			exit(1);
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 		savelineno = curlineno;
3047c478bd9Sstevel@tonic-gate 		repbufp = repstr;
3057c478bd9Sstevel@tonic-gate 		repstr = strchr(repbufp, ':');
3067c478bd9Sstevel@tonic-gate 		if (repstr == (char *)NULL)
3077c478bd9Sstevel@tonic-gate 			badformat(curline);
3087c478bd9Sstevel@tonic-gate 		repstr[strlen(repstr) - 1 ] = '\0';
3097c478bd9Sstevel@tonic-gate 		*repstr++ = '\0';
3107c478bd9Sstevel@tonic-gate 		curposno = atoi(repbufp);
3117c478bd9Sstevel@tonic-gate 		repbufp = repstr;
3127c478bd9Sstevel@tonic-gate 		repstr = strchr(repbufp, ':');
3137c478bd9Sstevel@tonic-gate 		if (repstr == (char *)NULL)
3147c478bd9Sstevel@tonic-gate 			badformat(curline);
3157c478bd9Sstevel@tonic-gate 		*repstr++ = '\0';
3167c478bd9Sstevel@tonic-gate 		msgfile = repbufp;
3177c478bd9Sstevel@tonic-gate 		if (strlen(msgfile) > (size_t)14 || *msgfile == '\0') {
318*884db8abSpetede 			(void) fprintf(stderr,
319*884db8abSpetede 			    "exstr: ERROR: stdin: invalid message file name "
320*884db8abSpetede 			    "'%s'\n", msgfile);
3217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s", curline);
3227c478bd9Sstevel@tonic-gate 			exit(1);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		repbufp = repstr;
3257c478bd9Sstevel@tonic-gate 		repstr = strchr(repbufp, ':');
3267c478bd9Sstevel@tonic-gate 		if (repstr == (char *)NULL)
3277c478bd9Sstevel@tonic-gate 			badformat(curline);
3287c478bd9Sstevel@tonic-gate 		*repstr++ = '\0';
3297c478bd9Sstevel@tonic-gate 		cp = repbufp;
3307c478bd9Sstevel@tonic-gate 		while (*cp)
3317c478bd9Sstevel@tonic-gate 			if (!isdigit(*cp++)) {
3327c478bd9Sstevel@tonic-gate 				wrong_msg++;
3337c478bd9Sstevel@tonic-gate 				break;
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 		if (*repbufp == '\0' || wrong_msg) {
336*884db8abSpetede 			(void) fprintf(stderr, "exstr: ERROR: stdin: invalid "
337*884db8abSpetede 			    "message number '%s'\n", repbufp);
3387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s", curline);
3397c478bd9Sstevel@tonic-gate 			exit(1);
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 		curmsgno = atoi(repbufp);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		/* move up to this line */
3447c478bd9Sstevel@tonic-gate 		while (Lineno != curlineno) {
3457c478bd9Sstevel@tonic-gate 			if (outp != outbuf) {
3467c478bd9Sstevel@tonic-gate 				while (*inp != '\0')
3477c478bd9Sstevel@tonic-gate 					*outp++ = *inp++;
3487c478bd9Sstevel@tonic-gate 				*outp = '\0';
3497c478bd9Sstevel@tonic-gate 				(void) fputs(outbuf, stdout);
3507c478bd9Sstevel@tonic-gate 			} else if (*linebuf != '\0')
3517c478bd9Sstevel@tonic-gate 				(void) fputs(linebuf, stdout);
3527c478bd9Sstevel@tonic-gate 			outp = outbuf;
3537c478bd9Sstevel@tonic-gate 			inp = linebuf;
354*884db8abSpetede 			if (fgets(linebuf,
355*884db8abSpetede 			    sizeof (linebuf), fi) == (char *)NULL) {
3567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "read error\n");
3577c478bd9Sstevel@tonic-gate 				exit(1);
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 			Lineno++;
3607c478bd9Sstevel@tonic-gate 			Posno = 0;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		if (Posno > curposno) {
363*884db8abSpetede 			(void) fprintf(stderr,
364*884db8abSpetede 			    "Bad input record line number %d\n", Lineno);
3657c478bd9Sstevel@tonic-gate 			exit(1);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		while (Posno != curposno) {
3687c478bd9Sstevel@tonic-gate 			*outp++ = *inp++;
3697c478bd9Sstevel@tonic-gate 			Posno++;
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 		if (*inp != '"') {
372*884db8abSpetede 			(void) fprintf(stderr, "exstr: ERROR: cannot replace "
373*884db8abSpetede 			    "string '%s' in line (%d) of file (%s)\n", repstr,
374*884db8abSpetede 			    Lineno, Fname);
3757c478bd9Sstevel@tonic-gate 			exit(1);
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 		/* check if string continues in next line */
378*884db8abSpetede 		while (inp[strlen(inp)-2] == '\\' &&
379*884db8abSpetede 		    inp[strlen(inp)-1] == '\n') {
380*884db8abSpetede 			if (fgets(linebuf,
381*884db8abSpetede 			    sizeof (linebuf), fi) == (char *)NULL) {
382*884db8abSpetede 				(void) fprintf(stderr, "exstr: ERROR: read "
383*884db8abSpetede 				    "error in file (%s)\n", Fname);
3847c478bd9Sstevel@tonic-gate 				exit(1);
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 			cont_str++;
3877c478bd9Sstevel@tonic-gate 			Lineno++;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		if (cont_str) {
3907c478bd9Sstevel@tonic-gate 			cp = linebuf;
391*884db8abSpetede 			while (*cp != '\0' && *cp++ != '"')
392*884db8abSpetede 				;
3937c478bd9Sstevel@tonic-gate 			if (*cp == '\0') {
394*884db8abSpetede 				(void) fprintf(stderr, "exstr: ERROR: cannot "
395*884db8abSpetede 				    "replace string '%s' in line (%d) of file "
396*884db8abSpetede 				    "(%s)\n", repstr, Lineno, Fname);
3977c478bd9Sstevel@tonic-gate 				exit(1);
3987c478bd9Sstevel@tonic-gate 			}
3997c478bd9Sstevel@tonic-gate 			inp = cp;
4007c478bd9Sstevel@tonic-gate 			Posno = cp - linebuf;
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		if (dflg)
403*884db8abSpetede 			outp += snprintf(outp, BUFSIZ - (outp - outbuf),
404*884db8abSpetede 			    "gettxt(\"%s:%d\", \"%s\")", msgfile, curmsgno,
405*884db8abSpetede 			    repstr);
4067c478bd9Sstevel@tonic-gate 		else
407*884db8abSpetede 			outp += snprintf(outp, BUFSIZ - (outp - outbuf),
408*884db8abSpetede 			    "gettxt(\"%s:%d\", \"\")", msgfile, curmsgno);
4097c478bd9Sstevel@tonic-gate 		if (!cont_str) {
4107c478bd9Sstevel@tonic-gate 			inp += strlen(repstr)+2;
4117c478bd9Sstevel@tonic-gate 			Posno += strlen(repstr)+2;
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		else
4147c478bd9Sstevel@tonic-gate 			cont_str = 0;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	if (outp != outbuf) {
4177c478bd9Sstevel@tonic-gate 		while (*inp != '\0')
4187c478bd9Sstevel@tonic-gate 			*outp++ = *inp++;
4197c478bd9Sstevel@tonic-gate 		*outp = '\0';
4207c478bd9Sstevel@tonic-gate 		(void) fputs(outbuf, stdout);
4217c478bd9Sstevel@tonic-gate 	}
422*884db8abSpetede 	while (fgets(linebuf, sizeof (linebuf), fi) != (char *)NULL)
4237c478bd9Sstevel@tonic-gate 		(void) fputs(linebuf, stdout);
4247c478bd9Sstevel@tonic-gate 
425*884db8abSpetede 	(void) fclose(fi);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate static	void
4297c478bd9Sstevel@tonic-gate badformat(line)
4307c478bd9Sstevel@tonic-gate char  	*line;
4317c478bd9Sstevel@tonic-gate {
432*884db8abSpetede 	(void) fprintf(stderr, "exstr: ERROR: stdin: Badly formatted "
433*884db8abSpetede 	    "replacement string\n%s", line);
4347c478bd9Sstevel@tonic-gate 	exit(1);
4357c478bd9Sstevel@tonic-gate }
436