xref: /illumos-gate/usr/src/cmd/genmsg/util.c (revision 6e54a631)
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
5*6e54a631Smuffin  * Common Development and Distribution License (the "License").
6*6e54a631Smuffin  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*6e54a631Smuffin  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*6e54a631Smuffin  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <limits.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <locale.h>
357c478bd9Sstevel@tonic-gate #include <libgen.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "genmsg.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	SET_TOKEN	"$set"
457c478bd9Sstevel@tonic-gate #define	DELSET_TOKEN	"$delset"
467c478bd9Sstevel@tonic-gate #define	QUOTE_TOKEN	"$quote"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	SkipSpace(s)	while (*(s) == ' ' || *(s) == '\t') s++
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate extern char *program;		/* from main.c */
517c478bd9Sstevel@tonic-gate extern char *mctag;		/* from main.c */
527c478bd9Sstevel@tonic-gate extern char *sctag;		/* from main.c */
537c478bd9Sstevel@tonic-gate extern char *premsg;		/* from main.c */
547c478bd9Sstevel@tonic-gate extern char *sufmsg;		/* from main.c */
557c478bd9Sstevel@tonic-gate extern int suppress_error;	/* from main.c */
567c478bd9Sstevel@tonic-gate extern void warning(char *);	/* from genmsg.l */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate typedef struct _SetID *SetID;
597c478bd9Sstevel@tonic-gate typedef struct _MsgID *MsgID;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate typedef struct _SetID SetIDRec;
627c478bd9Sstevel@tonic-gate struct _SetID {
637c478bd9Sstevel@tonic-gate 	int id;
647c478bd9Sstevel@tonic-gate 	char *comment;
657c478bd9Sstevel@tonic-gate 	MsgID top;
667c478bd9Sstevel@tonic-gate 	SetID next;
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate typedef struct _MsgID MsgIDRec;
707c478bd9Sstevel@tonic-gate struct _MsgID {
717c478bd9Sstevel@tonic-gate 	int no_write;
727c478bd9Sstevel@tonic-gate 	int id;
737c478bd9Sstevel@tonic-gate 	char *msg;
747c478bd9Sstevel@tonic-gate 	int line;
757c478bd9Sstevel@tonic-gate 	char *file;
767c478bd9Sstevel@tonic-gate 	char *comment;
777c478bd9Sstevel@tonic-gate 	MsgID next;
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /* Top pointer of the setid list. */
827c478bd9Sstevel@tonic-gate static SetID setid_top;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* comment for messages. */
857c478bd9Sstevel@tonic-gate static char *msg_comment;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* comment for set numbers. */
887c478bd9Sstevel@tonic-gate static char *set_comment;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* List of set number's maximum message numbers. */
917c478bd9Sstevel@tonic-gate static int msgid_table[NL_SETMAX+1];
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* Quote character to surround messages. */
947c478bd9Sstevel@tonic-gate static char quote = QUOTE;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* Internal functions. */
977c478bd9Sstevel@tonic-gate static void add_msgid(SetID, int, char *, char *, int, int);
987c478bd9Sstevel@tonic-gate static void add_setid(int, int, char *, char *, int, int);
997c478bd9Sstevel@tonic-gate static SetID lookup_setid(int);
1007c478bd9Sstevel@tonic-gate static MsgID lookup_msgid(SetID, int, char *, char *, int);
1017c478bd9Sstevel@tonic-gate static void print_prefix(FILE *, char *, int, char *);
1027c478bd9Sstevel@tonic-gate static int is_bs_terminated(char *);
1037c478bd9Sstevel@tonic-gate static char *ustrdup(char *);
1047c478bd9Sstevel@tonic-gate static void makeup_msg(char **);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate void
1077c478bd9Sstevel@tonic-gate add_msg(int setid, int msgid, char *msg, char *file, int line, int no_write)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	SetID si;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (si = lookup_setid(setid)) {
1127c478bd9Sstevel@tonic-gate 		if (lookup_msgid(si, msgid, msg, file, line)) {
1137c478bd9Sstevel@tonic-gate 			return; /* we already have the one. */
1147c478bd9Sstevel@tonic-gate 		} else {
1157c478bd9Sstevel@tonic-gate 			add_msgid(si, msgid, msg, file, line, no_write);
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 	} else {
1187c478bd9Sstevel@tonic-gate 		add_setid(setid, msgid, msg, file, line, no_write);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate int
1237c478bd9Sstevel@tonic-gate is_writable(char *file)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	struct stat buf;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (stat(file, &buf) == -1)
1287c478bd9Sstevel@tonic-gate 		return (TRUE);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (access(file, W_OK) == 0)
1317c478bd9Sstevel@tonic-gate 		return (TRUE);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (FALSE);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate void
1377c478bd9Sstevel@tonic-gate write_msgfile(char *file)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	FILE *fp;
1407c478bd9Sstevel@tonic-gate 	SetID si = setid_top;
1417c478bd9Sstevel@tonic-gate 	char *mode = "w";
1427c478bd9Sstevel@tonic-gate 	char pquote[2];
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (is_writable(file) == FALSE) {
1457c478bd9Sstevel@tonic-gate 		prg_err(gettext("cannot create \"%s\": permission denied"),
146*6e54a631Smuffin 		    file);
1477c478bd9Sstevel@tonic-gate 		return;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (IsActiveMode(AppendMode)) {
1517c478bd9Sstevel@tonic-gate 		mode = "a";
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, mode)) == NULL) {
1557c478bd9Sstevel@tonic-gate 		prg_err(gettext("cannot create \"%s\""), file);
1567c478bd9Sstevel@tonic-gate 		return;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (quote) {
160*6e54a631Smuffin 		pquote[0] = quote;
1617c478bd9Sstevel@tonic-gate 	} else {
162*6e54a631Smuffin 		pquote[0] = '\0';
1637c478bd9Sstevel@tonic-gate 	}
164*6e54a631Smuffin 	pquote[1] = '\0';
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* AppendMode is already turned off if the file doesn't exist. */
1677c478bd9Sstevel@tonic-gate 	if (!IsActiveMode(AppendMode)) {
1687c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\n$quote %s\n\n", pquote);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	while (si) {
1727c478bd9Sstevel@tonic-gate 		int is_set = FALSE;
1737c478bd9Sstevel@tonic-gate 		MsgID mi = si->top;
1747c478bd9Sstevel@tonic-gate 		while (mi) {
1757c478bd9Sstevel@tonic-gate 			char msg[NL_TEXTMAX+32]; /* 32 is some other stuff. */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 			if (mi->no_write) {
1787c478bd9Sstevel@tonic-gate 				mi = mi->next;
1797c478bd9Sstevel@tonic-gate 				continue;
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 			if (is_set == FALSE) {
1827c478bd9Sstevel@tonic-gate 				if (si->comment &&
183*6e54a631Smuffin 				    !IsActiveMode(BackCommentMode)) {
1847c478bd9Sstevel@tonic-gate 					(void) fprintf(fp, "\n");
1857c478bd9Sstevel@tonic-gate 					print_prefix(fp, "$ ", TRUE,
186*6e54a631Smuffin 					    si->comment);
1877c478bd9Sstevel@tonic-gate 					(void) fprintf(fp, "$set\t%d\n",
188*6e54a631Smuffin 					    si->id);
1897c478bd9Sstevel@tonic-gate 				} else {
1907c478bd9Sstevel@tonic-gate 					(void) fprintf(fp, "\n$set\t%d\n",
191*6e54a631Smuffin 					    si->id);
1927c478bd9Sstevel@tonic-gate 				}
1937c478bd9Sstevel@tonic-gate 				if (si->comment &&
194*6e54a631Smuffin 				    IsActiveMode(BackCommentMode)) {
1957c478bd9Sstevel@tonic-gate 					print_prefix(fp, "$ ", TRUE,
196*6e54a631Smuffin 					    si->comment);
1977c478bd9Sstevel@tonic-gate 				}
1987c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, "\n");
1997c478bd9Sstevel@tonic-gate 				is_set = TRUE;
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 			makeup_msg(&(mi->msg));
2037c478bd9Sstevel@tonic-gate 
204*6e54a631Smuffin 			(void) snprintf(msg, sizeof (msg), "%d\t%s%s%s\n",
205*6e54a631Smuffin 			    mi->id, pquote, mi->msg, pquote);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 			if (!IsActiveMode(BackCommentMode)) {
2087c478bd9Sstevel@tonic-gate 				if (mi->line && mi->file &&
209*6e54a631Smuffin 				    IsActiveMode(LineInfoMode)) {
2107c478bd9Sstevel@tonic-gate 					(void) fprintf(fp,
211*6e54a631Smuffin 					    "$ File:%s, line:%d\n",
212*6e54a631Smuffin 					    basename(mi->file), mi->line);
2137c478bd9Sstevel@tonic-gate 				}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 				if (mi->comment) {
2167c478bd9Sstevel@tonic-gate 					print_prefix(fp, "$ ", TRUE,
217*6e54a631Smuffin 					    mi->comment);
2187c478bd9Sstevel@tonic-gate 				}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 				if (IsActiveMode(DoubleLineMode)) {
2217c478bd9Sstevel@tonic-gate 					print_prefix(fp, "$ ", FALSE, msg);
2227c478bd9Sstevel@tonic-gate 				}
2237c478bd9Sstevel@tonic-gate 			}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%s", msg);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 			if (IsActiveMode(BackCommentMode)) {
2287c478bd9Sstevel@tonic-gate 				if (mi->line && mi->file &&
229*6e54a631Smuffin 				    IsActiveMode(LineInfoMode)) {
2307c478bd9Sstevel@tonic-gate 					(void) fprintf(fp,
231*6e54a631Smuffin 					    "$ File:%s, line:%d\n",
232*6e54a631Smuffin 					    basename(mi->file), mi->line);
2337c478bd9Sstevel@tonic-gate 				}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 				if (mi->comment) {
2367c478bd9Sstevel@tonic-gate 					print_prefix(fp, "$ ", TRUE,
237*6e54a631Smuffin 					    mi->comment);
2387c478bd9Sstevel@tonic-gate 				}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 				if (IsActiveMode(DoubleLineMode)) {
2417c478bd9Sstevel@tonic-gate 					print_prefix(fp, "$ ", FALSE, msg);
2427c478bd9Sstevel@tonic-gate 				}
2437c478bd9Sstevel@tonic-gate 			}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\n");
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 			mi = mi->next;
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 		si = si->next;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate static SetID
2567c478bd9Sstevel@tonic-gate lookup_setid(int id)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	SetID si = setid_top;
2597c478bd9Sstevel@tonic-gate 	while (si) {
2607c478bd9Sstevel@tonic-gate 		if (si->id == id) {
2617c478bd9Sstevel@tonic-gate 			return (si);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		si = si->next;
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	return (NULL);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static MsgID
2697c478bd9Sstevel@tonic-gate lookup_msgid(SetID si, int msgid, char *msg, char *file, int line)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	MsgID mi = si->top;
2727c478bd9Sstevel@tonic-gate 	while (mi) {
2737c478bd9Sstevel@tonic-gate 		if (mi->id == msgid) {
2747c478bd9Sstevel@tonic-gate 			/* same setid & msgid, but different msg. */
2757c478bd9Sstevel@tonic-gate 			if (strcmp(mi->msg, msg)) {
276*6e54a631Smuffin 				src_err(file, line, gettext(
277*6e54a631Smuffin 			"multiple messages: set number %d, message number %d\n"
2787c478bd9Sstevel@tonic-gate 			"	current : \"%s\"\n"
2797c478bd9Sstevel@tonic-gate 			"	previous: \"%s\" : \"%s\", line %d"),
280*6e54a631Smuffin 				    si->id, mi->id,
281*6e54a631Smuffin 				    msg,
282*6e54a631Smuffin 				    mi->msg, mi->file, mi->line);
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 			return (mi);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		mi = mi->next;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	return (NULL);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate static void
2927c478bd9Sstevel@tonic-gate add_msgid(SetID si, int msgid, char *msg, char *file, int line, int no_write)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	MsgID mi = si->top, newmi, prev = NULL;
2957c478bd9Sstevel@tonic-gate 	int len = strlen(msg);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (msgid == 0) {
2987c478bd9Sstevel@tonic-gate 		src_err(file, line, gettext("improper message number: %d"),
299*6e54a631Smuffin 		    msgid);
3007c478bd9Sstevel@tonic-gate 		return;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (msgid > NL_MSGMAX) {
3047c478bd9Sstevel@tonic-gate 		src_err(file, line, gettext("too large message number: %d"),
305*6e54a631Smuffin 		    msgid);
3067c478bd9Sstevel@tonic-gate 		return;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (len > NL_TEXTMAX) {
3107c478bd9Sstevel@tonic-gate 		src_err(file, line, gettext("too long message text"));
3117c478bd9Sstevel@tonic-gate 		return;
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	while (mi) {
3157c478bd9Sstevel@tonic-gate 		if (mi->id > msgid) {
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 		prev = mi;
3197c478bd9Sstevel@tonic-gate 		mi = mi->next;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
322*6e54a631Smuffin 	if ((newmi = malloc(sizeof (MsgIDRec))) == NULL) {
3237c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
3247c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	newmi->no_write = no_write;
3287c478bd9Sstevel@tonic-gate 	newmi->id = msgid;
329*6e54a631Smuffin 	newmi->msg = ustrdup(msg);
330*6e54a631Smuffin 	newmi->file = ustrdup(file);
3317c478bd9Sstevel@tonic-gate 	newmi->line = line;
3327c478bd9Sstevel@tonic-gate 	newmi->next = mi;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (msg_comment) {
335*6e54a631Smuffin 		newmi->comment = ustrdup(msg_comment);
3367c478bd9Sstevel@tonic-gate 		free(msg_comment);
3377c478bd9Sstevel@tonic-gate 		msg_comment = NULL;
3387c478bd9Sstevel@tonic-gate 	} else {
3397c478bd9Sstevel@tonic-gate 		newmi->comment = NULL;
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (prev == NULL) {
3437c478bd9Sstevel@tonic-gate 		si->top = newmi;
3447c478bd9Sstevel@tonic-gate 	} else {
3457c478bd9Sstevel@tonic-gate 		prev->next = newmi;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate static void
3507c478bd9Sstevel@tonic-gate add_setid(int setid, int msgid, char *msg, char *file, int line, int no_write)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	SetID si = setid_top, newsi, prev = NULL;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	while (si) {
3557c478bd9Sstevel@tonic-gate 		if (si->id > setid) {
3567c478bd9Sstevel@tonic-gate 			break;
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 		prev = si;
3597c478bd9Sstevel@tonic-gate 		si = si->next;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
362*6e54a631Smuffin 	if ((newsi = malloc(sizeof (SetIDRec))) == NULL) {
3637c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
3647c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	newsi->id = setid;
3687c478bd9Sstevel@tonic-gate 	newsi->top = NULL;
3697c478bd9Sstevel@tonic-gate 	newsi->next = si;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if (set_comment) {
372*6e54a631Smuffin 		newsi->comment = ustrdup(set_comment);
3737c478bd9Sstevel@tonic-gate 		free(set_comment);
3747c478bd9Sstevel@tonic-gate 		set_comment = NULL;
3757c478bd9Sstevel@tonic-gate 	} else {
3767c478bd9Sstevel@tonic-gate 		newsi->comment = NULL;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (prev == NULL) {
3807c478bd9Sstevel@tonic-gate 		setid_top = newsi;
3817c478bd9Sstevel@tonic-gate 	} else {
3827c478bd9Sstevel@tonic-gate 		prev->next = newsi;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	add_msgid(newsi, msgid, msg, file, line, no_write);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate static void
3897c478bd9Sstevel@tonic-gate print_prefix(FILE *fp, char *prefix, int rm_blank, char *str)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s", prefix);
3927c478bd9Sstevel@tonic-gate 	while (*str) {
3937c478bd9Sstevel@tonic-gate 		(void) fputc(*str, fp);
3947c478bd9Sstevel@tonic-gate 		if (*str == '\n' && *(str+1) != '\0') {
3957c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%s", prefix);
3967c478bd9Sstevel@tonic-gate 			if (rm_blank == TRUE) {
3977c478bd9Sstevel@tonic-gate 				str++;
3987c478bd9Sstevel@tonic-gate 				SkipSpace(str);
3997c478bd9Sstevel@tonic-gate 				continue;
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		str++;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	if (*(str-1) != '\n') {
4057c478bd9Sstevel@tonic-gate 		(void) fputc('\n', fp);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate int
4107c478bd9Sstevel@tonic-gate read_projfile(char *file)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	FILE *fp;
4137c478bd9Sstevel@tonic-gate 	char line[LINE_MAX];
4147c478bd9Sstevel@tonic-gate 
415*6e54a631Smuffin 	if (file == NULL) {
4167c478bd9Sstevel@tonic-gate 		return (0);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL) {
4207c478bd9Sstevel@tonic-gate 		return (0);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
423*6e54a631Smuffin 	while (fgets(line, sizeof (line), fp) != NULL) {
4247c478bd9Sstevel@tonic-gate 		char *p = line;
4257c478bd9Sstevel@tonic-gate 		int n, setid, msgid;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		SkipSpace(p);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		if (*p == '#' || *p == '\n') {
4307c478bd9Sstevel@tonic-gate 			continue;
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		n = sscanf(p, "%d %d", &setid, &msgid);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		if (n == 2) {
4367c478bd9Sstevel@tonic-gate 			if (setid > NL_SETMAX) {
437*6e54a631Smuffin 				prg_err(gettext("%s: too large set number: %d"),
438*6e54a631Smuffin 				    file, setid);
4397c478bd9Sstevel@tonic-gate 				continue;
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 			msgid_table[setid] = msgid;
4427c478bd9Sstevel@tonic-gate 		} else {
443*6e54a631Smuffin 			prg_err(gettext(
444*6e54a631Smuffin 			    "warning: %s: missing or invalid entry"), file);
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	return (1);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate void
4547c478bd9Sstevel@tonic-gate write_projfile(char *file)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	FILE *fp;
4577c478bd9Sstevel@tonic-gate 	register int i;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (is_writable(file) == FALSE) {
4607c478bd9Sstevel@tonic-gate 		prg_err(gettext("cannot create \"%s\": permission denied"),
461*6e54a631Smuffin 		    file);
4627c478bd9Sstevel@tonic-gate 		return;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "w")) == NULL) {
4667c478bd9Sstevel@tonic-gate 		prg_err(gettext("cannot create \"%s\""), file);
4677c478bd9Sstevel@tonic-gate 		return;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	for (i = 1; i <= NL_SETMAX; i++) {
4717c478bd9Sstevel@tonic-gate 		if (msgid_table[i] > 0) {
4727c478bd9Sstevel@tonic-gate 			SetID si;
4737c478bd9Sstevel@tonic-gate 			char *com = NULL;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			if (IsActiveMode(SetCommentMode) &&
476*6e54a631Smuffin 			    (si = lookup_setid(i)) && si->comment) {
4777c478bd9Sstevel@tonic-gate 				com = si->comment;
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 			if (com && !IsActiveMode(BackCommentMode)) {
4817c478bd9Sstevel@tonic-gate 				print_prefix(fp, "# ", TRUE, com);
4827c478bd9Sstevel@tonic-gate 			}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%d\t%d\n", i, msgid_table[i]);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 			if (com && IsActiveMode(BackCommentMode)) {
4877c478bd9Sstevel@tonic-gate 				print_prefix(fp, "# ", TRUE, com);
4887c478bd9Sstevel@tonic-gate 			}
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate int
4967c478bd9Sstevel@tonic-gate get_msgid(char *file, int line, int setid, char *str)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	SetID si = setid_top;
4997c478bd9Sstevel@tonic-gate 	int id = msgid_table[setid];
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	while (si) {
5027c478bd9Sstevel@tonic-gate 		if (si->id == setid) {
5037c478bd9Sstevel@tonic-gate 			MsgID mi = si->top;
5047c478bd9Sstevel@tonic-gate 			while (mi) {
5057c478bd9Sstevel@tonic-gate 				if (strcmp(mi->msg, str) == 0) {
5067c478bd9Sstevel@tonic-gate 					return (mi->id);
5077c478bd9Sstevel@tonic-gate 				}
5087c478bd9Sstevel@tonic-gate 				mi = mi->next;
5097c478bd9Sstevel@tonic-gate 			}
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 		si = si->next;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	id++;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if (id > NL_MSGMAX) {
5177c478bd9Sstevel@tonic-gate 		src_err(file, line,
518*6e54a631Smuffin 		    gettext("run out of message number in set number: %d"),
519*6e54a631Smuffin 		    setid);
5207c478bd9Sstevel@tonic-gate 		return (NOMSGID);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	return (msgid_table[setid] = id);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate void
5277c478bd9Sstevel@tonic-gate set_msgid(int setid, int msgid)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	if (msgid_table[setid] < msgid) {
5307c478bd9Sstevel@tonic-gate 		msgid_table[setid] = msgid;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate void
5357c478bd9Sstevel@tonic-gate add_comment(Mode mode, char *str)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	char *tag = (mode == MsgCommentMode) ? mctag : sctag;
5387c478bd9Sstevel@tonic-gate 	char **comment = (mode == MsgCommentMode)
539*6e54a631Smuffin 	    ? &msg_comment : &set_comment;
5407c478bd9Sstevel@tonic-gate 
541*6e54a631Smuffin 	if (strstr(str, tag) == NULL) {
5427c478bd9Sstevel@tonic-gate 		return;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (*comment) {
5467c478bd9Sstevel@tonic-gate 		free(*comment);
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
549*6e54a631Smuffin 	*comment = ustrdup(str);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate void
5537c478bd9Sstevel@tonic-gate read_msgfile(char *file)
5547c478bd9Sstevel@tonic-gate {
5557c478bd9Sstevel@tonic-gate 	FILE *fp;
5567c478bd9Sstevel@tonic-gate 	char c = 0;
5577c478bd9Sstevel@tonic-gate 	int line = 0;
5587c478bd9Sstevel@tonic-gate 	int inmsg = FALSE;
5597c478bd9Sstevel@tonic-gate 	int setid = 0, unsetid = -1, msgid = 0;
5607c478bd9Sstevel@tonic-gate 	struct stat buf;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL) {
5637c478bd9Sstevel@tonic-gate 		prg_err(gettext("cannot open \"%s\""), file);
5647c478bd9Sstevel@tonic-gate 		ResetActiveMode(AppendMode);
5657c478bd9Sstevel@tonic-gate 		return;
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (stat(file, &buf) == -1 && buf.st_size == 0) {
5697c478bd9Sstevel@tonic-gate 		ResetActiveMode(AppendMode);
5707c478bd9Sstevel@tonic-gate 		return;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	quote = c;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
5767c478bd9Sstevel@tonic-gate 	while (1) {
5777c478bd9Sstevel@tonic-gate 		char buf[LINE_MAX];
5787c478bd9Sstevel@tonic-gate 		char *ptr;
5797c478bd9Sstevel@tonic-gate 		char msg[NL_TEXTMAX];
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		if (fgets(buf, sizeof (buf), fp) == NULL) {
5827c478bd9Sstevel@tonic-gate 			break;
5837c478bd9Sstevel@tonic-gate 		}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		line++;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		ptr = &buf[0];
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		SkipSpace(ptr);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		if ((*ptr == '$' && (*(ptr+1) == ' ' || *(ptr+1) == '\t')) ||
592*6e54a631Smuffin 		    ((*ptr == '\n') && inmsg == FALSE)) {
5937c478bd9Sstevel@tonic-gate 			inmsg = FALSE;
5947c478bd9Sstevel@tonic-gate 			continue;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		if (strncmp(ptr, SET_TOKEN, sizeof (SET_TOKEN) - 1) == 0) {
5987c478bd9Sstevel@tonic-gate 			if (sscanf(ptr, "%*s %d", &setid) != 1) {
5997c478bd9Sstevel@tonic-gate 				setid = 0;
6007c478bd9Sstevel@tonic-gate 			}
6017c478bd9Sstevel@tonic-gate 			inmsg = FALSE;
6027c478bd9Sstevel@tonic-gate 			continue;
6037c478bd9Sstevel@tonic-gate 		} else if (strncmp(ptr, DELSET_TOKEN,
604*6e54a631Smuffin 		    sizeof (DELSET_TOKEN) - 1) == 0) {
6057c478bd9Sstevel@tonic-gate 			if (sscanf(ptr, "%*s %d", &unsetid) != 1) {
6067c478bd9Sstevel@tonic-gate 				unsetid = -1;
6077c478bd9Sstevel@tonic-gate 			}
6087c478bd9Sstevel@tonic-gate 			inmsg = FALSE;
6097c478bd9Sstevel@tonic-gate 			continue;
6107c478bd9Sstevel@tonic-gate 		} else if (strncmp(ptr, QUOTE_TOKEN,
611*6e54a631Smuffin 		    sizeof (QUOTE_TOKEN) - 1) == 0) {
6127c478bd9Sstevel@tonic-gate 			if (sscanf(ptr, "%*s %c", &c) != 1) {
6137c478bd9Sstevel@tonic-gate 				c = 0;
6147c478bd9Sstevel@tonic-gate 			}
6157c478bd9Sstevel@tonic-gate 			quote = c;
6167c478bd9Sstevel@tonic-gate 			inmsg = FALSE;
6177c478bd9Sstevel@tonic-gate 			continue;
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		if (setid == unsetid) {
6217c478bd9Sstevel@tonic-gate 			continue;
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		if (inmsg) {
6257c478bd9Sstevel@tonic-gate 			if (is_bs_terminated(ptr)) {
626*6e54a631Smuffin 				(void) strlcat(msg, ptr, sizeof (msg));
6277c478bd9Sstevel@tonic-gate 				inmsg = TRUE;
6287c478bd9Sstevel@tonic-gate 			} else {
6297c478bd9Sstevel@tonic-gate 				int len = strlen(ptr);
6307c478bd9Sstevel@tonic-gate 				*(ptr + len - 1) = '\0';
6317c478bd9Sstevel@tonic-gate 				if (c && (*(ptr + len - 2) == c)) {
6327c478bd9Sstevel@tonic-gate 					*(ptr + len - 2) = '\0';
6337c478bd9Sstevel@tonic-gate 				}
634*6e54a631Smuffin 				(void) strlcat(msg, ptr, sizeof (msg));
6357c478bd9Sstevel@tonic-gate 				add_msg(setid, msgid, msg, file, line, TRUE);
6367c478bd9Sstevel@tonic-gate 				inmsg = FALSE;
6377c478bd9Sstevel@tonic-gate 			}
6387c478bd9Sstevel@tonic-gate 			continue;
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 
641*6e54a631Smuffin 		if (isdigit((unsigned char)*ptr)) {
642*6e54a631Smuffin 			char	*pptr;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 			SkipSpace(ptr);
6457c478bd9Sstevel@tonic-gate 
646*6e54a631Smuffin 			msgid = (int)strtol(ptr, &pptr, 10);
647*6e54a631Smuffin 			ptr = pptr;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 			SkipSpace(ptr);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 			if (is_bs_terminated(ptr)) {
652*6e54a631Smuffin 				(void) memset(msg, 0, sizeof (msg));
6537c478bd9Sstevel@tonic-gate 				if (c && (*ptr == c)) {
6547c478bd9Sstevel@tonic-gate 					ptr++;
6557c478bd9Sstevel@tonic-gate 				}
656*6e54a631Smuffin 				(void) strlcpy(msg, ptr, sizeof (msg));
6577c478bd9Sstevel@tonic-gate 				inmsg = TRUE;
6587c478bd9Sstevel@tonic-gate 			} else {
6597c478bd9Sstevel@tonic-gate 				int len = strlen(ptr);
6607c478bd9Sstevel@tonic-gate 				*(ptr + len - 1) = '\0';
6617c478bd9Sstevel@tonic-gate 				if (c && ((*ptr == c) &&
662*6e54a631Smuffin 				    (*(ptr + len - 2) == c))) {
6637c478bd9Sstevel@tonic-gate 					*(ptr + len - 2) = '\0';
6647c478bd9Sstevel@tonic-gate 					ptr++;
6657c478bd9Sstevel@tonic-gate 				}
6667c478bd9Sstevel@tonic-gate 				add_msg(setid, msgid, ptr, file, line, TRUE);
6677c478bd9Sstevel@tonic-gate 				inmsg = FALSE;
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate static int
6767c478bd9Sstevel@tonic-gate is_bs_terminated(char *msg)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 	int len = strlen(msg);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	while (--len >= 0) {
6817c478bd9Sstevel@tonic-gate 		if (msg[len] == ' ' || msg[len] == '\t' || msg[len] == '\n') {
6827c478bd9Sstevel@tonic-gate 			continue;
6837c478bd9Sstevel@tonic-gate 		} else if (msg[len] == '\\') {
6847c478bd9Sstevel@tonic-gate 			len--;
6857c478bd9Sstevel@tonic-gate 			if (len >= 0 && msg[len] == '\\')
6867c478bd9Sstevel@tonic-gate 				return (0);
6877c478bd9Sstevel@tonic-gate 			return (1);
6887c478bd9Sstevel@tonic-gate 		} else {
6897c478bd9Sstevel@tonic-gate 			return (0);
6907c478bd9Sstevel@tonic-gate 		}
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 	return (0);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate static char *
6967c478bd9Sstevel@tonic-gate ustrdup(char *str)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	char *tmp = strdup(str);
699*6e54a631Smuffin 	if (tmp == NULL) {
7007c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
7017c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 	return (tmp);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate int
7077c478bd9Sstevel@tonic-gate file_copy(char *in, char *out)
7087c478bd9Sstevel@tonic-gate {
7097c478bd9Sstevel@tonic-gate 	int ret = TRUE;
7107c478bd9Sstevel@tonic-gate 	FILE *fin, *fout;
7117c478bd9Sstevel@tonic-gate 	int c;
7127c478bd9Sstevel@tonic-gate 	sigset_t newmask, oldmask;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&newmask);
7157c478bd9Sstevel@tonic-gate 	(void) sigaddset(&newmask, SIGQUIT);
7167c478bd9Sstevel@tonic-gate 	(void) sigaddset(&newmask, SIGINT);
7177c478bd9Sstevel@tonic-gate 	(void) sigaddset(&newmask, SIGHUP);
7187c478bd9Sstevel@tonic-gate 	(void) sigaddset(&newmask, SIGTERM);
7197c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &newmask, &oldmask);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if ((fin = fopen(in, "r")) == NULL) {
7227c478bd9Sstevel@tonic-gate 		prg_err(gettext("cannot open \"%s\""), in);
7237c478bd9Sstevel@tonic-gate 		ret = FALSE;
7247c478bd9Sstevel@tonic-gate 		goto done;
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	if ((fout = fopen(out, "w")) == NULL) {
7287c478bd9Sstevel@tonic-gate 		prg_err(gettext("cannot create \"%s\""), out);
7297c478bd9Sstevel@tonic-gate 		ret = FALSE;
7307c478bd9Sstevel@tonic-gate 		goto done;
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	while ((c = getc(fin)) != EOF)
7347c478bd9Sstevel@tonic-gate 		(void) putc(c, fout);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	(void) fclose(fin);
7377c478bd9Sstevel@tonic-gate 	(void) fclose(fout);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate done:
7407c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oldmask, NULL);
7417c478bd9Sstevel@tonic-gate 	return (ret);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate static void
7457c478bd9Sstevel@tonic-gate makeup_msg(char **pmsg)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	char buf[NL_TEXTMAX];
7487c478bd9Sstevel@tonic-gate 	char *msg;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	msg = *pmsg;
751*6e54a631Smuffin 	buf[0] = '\0';
7527c478bd9Sstevel@tonic-gate 
753*6e54a631Smuffin 	if (IsActiveMode(TripleMode) &&	strchr(msg, '%') == NULL) {
7547c478bd9Sstevel@tonic-gate 		/* there is no '%' in message. */
7557c478bd9Sstevel@tonic-gate 		int len = strlen(msg);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		if (msg[len-2] == '\\' && msg[len-1] == 'n') {
7587c478bd9Sstevel@tonic-gate 			msg[len-2] = '\0';
759*6e54a631Smuffin 			(void) strlcat(buf, msg, sizeof (buf));
760*6e54a631Smuffin 			(void) strlcat(buf, msg, sizeof (buf));
761*6e54a631Smuffin 			(void) strlcat(buf, msg, sizeof (buf));
762*6e54a631Smuffin 			(void) strlcat(buf, "\\n", sizeof (buf));
7637c478bd9Sstevel@tonic-gate 		} else {
764*6e54a631Smuffin 			(void) strlcat(buf, msg, sizeof (buf));
765*6e54a631Smuffin 			(void) strlcat(buf, msg, sizeof (buf));
766*6e54a631Smuffin 			(void) strlcat(buf, msg, sizeof (buf));
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 		free(msg);
769*6e54a631Smuffin 		*pmsg = ustrdup(buf);
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	msg = *pmsg;
773*6e54a631Smuffin 	buf[0] = '\0';
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (IsActiveMode(PrefixMode)) {
776*6e54a631Smuffin 		(void) strlcat(buf, premsg, sizeof (buf));
777*6e54a631Smuffin 		(void) strlcat(buf, msg, sizeof (buf));
7787c478bd9Sstevel@tonic-gate 		free(msg);
779*6e54a631Smuffin 		*pmsg = ustrdup(buf);
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	msg = *pmsg;
783*6e54a631Smuffin 	buf[0] = '\0';
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	if (IsActiveMode(SuffixMode)) {
7867c478bd9Sstevel@tonic-gate 		int len = strlen(msg);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		if (msg[len-2] == '\\' && msg[len-1] == 'n') {
7897c478bd9Sstevel@tonic-gate 			msg[len-2] = '\0';
790*6e54a631Smuffin 			(void) strlcat(buf, msg, sizeof (buf));
791*6e54a631Smuffin 			(void) strlcat(buf, sufmsg, sizeof (buf));
792*6e54a631Smuffin 			(void) strlcat(buf, "\\n", sizeof (buf));
7937c478bd9Sstevel@tonic-gate 		} else {
794*6e54a631Smuffin 			(void) strlcat(buf, msg, sizeof (buf));
795*6e54a631Smuffin 			(void) strlcat(buf, sufmsg, sizeof (buf));
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 		free(msg);
798*6e54a631Smuffin 		*pmsg = ustrdup(buf);
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate void
8037c478bd9Sstevel@tonic-gate prg_err(char *fmt, ...)
8047c478bd9Sstevel@tonic-gate {
8057c478bd9Sstevel@tonic-gate 	va_list ap;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
8087c478bd9Sstevel@tonic-gate 
809*6e54a631Smuffin 	(void) fprintf(stderr, "%s: ", program);
810*6e54a631Smuffin 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
811*6e54a631Smuffin 	(void) vfprintf(stderr, fmt, ap);
812*6e54a631Smuffin 	(void) fprintf(stderr, "\n");
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	va_end(ap);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate void
8187c478bd9Sstevel@tonic-gate src_err(char *file, int line, char *fmt, ...)
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	va_list ap;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	if (suppress_error == TRUE) {
8237c478bd9Sstevel@tonic-gate 		return;
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
8277c478bd9Sstevel@tonic-gate 
828*6e54a631Smuffin 	(void) fprintf(stderr, gettext("\"%s\", line %d: "), file, line);
829*6e54a631Smuffin 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
830*6e54a631Smuffin 	(void) vfprintf(stderr, fmt, ap);
831*6e54a631Smuffin 	(void) fprintf(stderr, "\n");
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	va_end(ap);
8347c478bd9Sstevel@tonic-gate }
835