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 #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <limits.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdarg.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <locale.h>
337c478bd9Sstevel@tonic-gate #include <libgen.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include "genmsg.h"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #define SET_TOKEN "$set"
437c478bd9Sstevel@tonic-gate #define DELSET_TOKEN "$delset"
447c478bd9Sstevel@tonic-gate #define QUOTE_TOKEN "$quote"
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define SkipSpace(s) while (*(s) == ' ' || *(s) == '\t') s++
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate extern char *program; /* from main.c */
497c478bd9Sstevel@tonic-gate extern char *mctag; /* from main.c */
507c478bd9Sstevel@tonic-gate extern char *sctag; /* from main.c */
517c478bd9Sstevel@tonic-gate extern char *premsg; /* from main.c */
527c478bd9Sstevel@tonic-gate extern char *sufmsg; /* from main.c */
537c478bd9Sstevel@tonic-gate extern int suppress_error; /* from main.c */
547c478bd9Sstevel@tonic-gate extern void warning(char *); /* from genmsg.l */
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate typedef struct _SetID *SetID;
577c478bd9Sstevel@tonic-gate typedef struct _MsgID *MsgID;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate typedef struct _SetID SetIDRec;
607c478bd9Sstevel@tonic-gate struct _SetID {
617c478bd9Sstevel@tonic-gate int id;
627c478bd9Sstevel@tonic-gate char *comment;
637c478bd9Sstevel@tonic-gate MsgID top;
647c478bd9Sstevel@tonic-gate SetID next;
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate typedef struct _MsgID MsgIDRec;
687c478bd9Sstevel@tonic-gate struct _MsgID {
697c478bd9Sstevel@tonic-gate int no_write;
707c478bd9Sstevel@tonic-gate int id;
717c478bd9Sstevel@tonic-gate char *msg;
727c478bd9Sstevel@tonic-gate int line;
737c478bd9Sstevel@tonic-gate char *file;
747c478bd9Sstevel@tonic-gate char *comment;
757c478bd9Sstevel@tonic-gate MsgID next;
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /* Top pointer of the setid list. */
807c478bd9Sstevel@tonic-gate static SetID setid_top;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /* comment for messages. */
837c478bd9Sstevel@tonic-gate static char *msg_comment;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /* comment for set numbers. */
867c478bd9Sstevel@tonic-gate static char *set_comment;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate /* List of set number's maximum message numbers. */
897c478bd9Sstevel@tonic-gate static int msgid_table[NL_SETMAX+1];
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /* Quote character to surround messages. */
927c478bd9Sstevel@tonic-gate static char quote = QUOTE;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /* Internal functions. */
957c478bd9Sstevel@tonic-gate static void add_msgid(SetID, int, char *, char *, int, int);
967c478bd9Sstevel@tonic-gate static void add_setid(int, int, char *, char *, int, int);
977c478bd9Sstevel@tonic-gate static SetID lookup_setid(int);
987c478bd9Sstevel@tonic-gate static MsgID lookup_msgid(SetID, int, char *, char *, int);
997c478bd9Sstevel@tonic-gate static void print_prefix(FILE *, char *, int, char *);
1007c478bd9Sstevel@tonic-gate static int is_bs_terminated(char *);
1017c478bd9Sstevel@tonic-gate static char *ustrdup(char *);
1027c478bd9Sstevel@tonic-gate static void makeup_msg(char **);
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate void
add_msg(int setid,int msgid,char * msg,char * file,int line,int no_write)1057c478bd9Sstevel@tonic-gate add_msg(int setid, int msgid, char *msg, char *file, int line, int no_write)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate SetID si;
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (si = lookup_setid(setid)) {
1107c478bd9Sstevel@tonic-gate if (lookup_msgid(si, msgid, msg, file, line)) {
1117c478bd9Sstevel@tonic-gate return; /* we already have the one. */
1127c478bd9Sstevel@tonic-gate } else {
1137c478bd9Sstevel@tonic-gate add_msgid(si, msgid, msg, file, line, no_write);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate } else {
1167c478bd9Sstevel@tonic-gate add_setid(setid, msgid, msg, file, line, no_write);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate int
is_writable(char * file)1217c478bd9Sstevel@tonic-gate is_writable(char *file)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate struct stat buf;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate if (stat(file, &buf) == -1)
1267c478bd9Sstevel@tonic-gate return (TRUE);
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate if (access(file, W_OK) == 0)
1297c478bd9Sstevel@tonic-gate return (TRUE);
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate return (FALSE);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate void
write_msgfile(char * file)1357c478bd9Sstevel@tonic-gate write_msgfile(char *file)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate FILE *fp;
1387c478bd9Sstevel@tonic-gate SetID si = setid_top;
1397c478bd9Sstevel@tonic-gate char *mode = "w";
1407c478bd9Sstevel@tonic-gate char pquote[2];
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate if (is_writable(file) == FALSE) {
1437c478bd9Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\": permission denied"),
1447c478bd9Sstevel@tonic-gate file);
1457c478bd9Sstevel@tonic-gate return;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if (IsActiveMode(AppendMode)) {
1497c478bd9Sstevel@tonic-gate mode = "a";
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if ((fp = fopen(file, mode)) == NULL) {
1537c478bd9Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\""), file);
1547c478bd9Sstevel@tonic-gate return;
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate if (quote) {
158*6e54a631Smuffin pquote[0] = quote;
1597c478bd9Sstevel@tonic-gate } else {
160*6e54a631Smuffin pquote[0] = '\0';
1617c478bd9Sstevel@tonic-gate }
162*6e54a631Smuffin pquote[1] = '\0';
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate /* AppendMode is already turned off if the file doesn't exist. */
1657c478bd9Sstevel@tonic-gate if (!IsActiveMode(AppendMode)) {
1667c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\n$quote %s\n\n", pquote);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate while (si) {
1707c478bd9Sstevel@tonic-gate int is_set = FALSE;
1717c478bd9Sstevel@tonic-gate MsgID mi = si->top;
1727c478bd9Sstevel@tonic-gate while (mi) {
1737c478bd9Sstevel@tonic-gate char msg[NL_TEXTMAX+32]; /* 32 is some other stuff. */
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate if (mi->no_write) {
1767c478bd9Sstevel@tonic-gate mi = mi->next;
1777c478bd9Sstevel@tonic-gate continue;
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate if (is_set == FALSE) {
1807c478bd9Sstevel@tonic-gate if (si->comment &&
1817c478bd9Sstevel@tonic-gate !IsActiveMode(BackCommentMode)) {
1827c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\n");
1837c478bd9Sstevel@tonic-gate print_prefix(fp, "$ ", TRUE,
1847c478bd9Sstevel@tonic-gate si->comment);
1857c478bd9Sstevel@tonic-gate (void) fprintf(fp, "$set\t%d\n",
1867c478bd9Sstevel@tonic-gate si->id);
1877c478bd9Sstevel@tonic-gate } else {
1887c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\n$set\t%d\n",
1897c478bd9Sstevel@tonic-gate si->id);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate if (si->comment &&
1927c478bd9Sstevel@tonic-gate IsActiveMode(BackCommentMode)) {
1937c478bd9Sstevel@tonic-gate print_prefix(fp, "$ ", TRUE,
1947c478bd9Sstevel@tonic-gate si->comment);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\n");
1977c478bd9Sstevel@tonic-gate is_set = TRUE;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate makeup_msg(&(mi->msg));
2017c478bd9Sstevel@tonic-gate
202*6e54a631Smuffin (void) snprintf(msg, sizeof (msg), "%d\t%s%s%s\n",
2037c478bd9Sstevel@tonic-gate mi->id, pquote, mi->msg, pquote);
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate if (!IsActiveMode(BackCommentMode)) {
2067c478bd9Sstevel@tonic-gate if (mi->line && mi->file &&
2077c478bd9Sstevel@tonic-gate IsActiveMode(LineInfoMode)) {
2087c478bd9Sstevel@tonic-gate (void) fprintf(fp,
2097c478bd9Sstevel@tonic-gate "$ File:%s, line:%d\n",
2107c478bd9Sstevel@tonic-gate basename(mi->file), mi->line);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate if (mi->comment) {
2147c478bd9Sstevel@tonic-gate print_prefix(fp, "$ ", TRUE,
2157c478bd9Sstevel@tonic-gate mi->comment);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate if (IsActiveMode(DoubleLineMode)) {
2197c478bd9Sstevel@tonic-gate print_prefix(fp, "$ ", FALSE, msg);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%s", msg);
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate if (IsActiveMode(BackCommentMode)) {
2267c478bd9Sstevel@tonic-gate if (mi->line && mi->file &&
2277c478bd9Sstevel@tonic-gate IsActiveMode(LineInfoMode)) {
2287c478bd9Sstevel@tonic-gate (void) fprintf(fp,
2297c478bd9Sstevel@tonic-gate "$ File:%s, line:%d\n",
2307c478bd9Sstevel@tonic-gate basename(mi->file), mi->line);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate if (mi->comment) {
2347c478bd9Sstevel@tonic-gate print_prefix(fp, "$ ", TRUE,
2357c478bd9Sstevel@tonic-gate mi->comment);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate if (IsActiveMode(DoubleLineMode)) {
2397c478bd9Sstevel@tonic-gate print_prefix(fp, "$ ", FALSE, msg);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\n");
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate mi = mi->next;
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate si = si->next;
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate (void) fclose(fp);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate static SetID
lookup_setid(int id)2547c478bd9Sstevel@tonic-gate lookup_setid(int id)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate SetID si = setid_top;
2577c478bd9Sstevel@tonic-gate while (si) {
2587c478bd9Sstevel@tonic-gate if (si->id == id) {
2597c478bd9Sstevel@tonic-gate return (si);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate si = si->next;
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate return (NULL);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate static MsgID
lookup_msgid(SetID si,int msgid,char * msg,char * file,int line)2677c478bd9Sstevel@tonic-gate lookup_msgid(SetID si, int msgid, char *msg, char *file, int line)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate MsgID mi = si->top;
2707c478bd9Sstevel@tonic-gate while (mi) {
2717c478bd9Sstevel@tonic-gate if (mi->id == msgid) {
2727c478bd9Sstevel@tonic-gate /* same setid & msgid, but different msg. */
2737c478bd9Sstevel@tonic-gate if (strcmp(mi->msg, msg)) {
274*6e54a631Smuffin src_err(file, line, gettext(
275*6e54a631Smuffin "multiple messages: set number %d, message number %d\n"
2767c478bd9Sstevel@tonic-gate " current : \"%s\"\n"
2777c478bd9Sstevel@tonic-gate " previous: \"%s\" : \"%s\", line %d"),
2787c478bd9Sstevel@tonic-gate si->id, mi->id,
2797c478bd9Sstevel@tonic-gate msg,
2807c478bd9Sstevel@tonic-gate mi->msg, mi->file, mi->line);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate return (mi);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate mi = mi->next;
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate return (NULL);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate static void
add_msgid(SetID si,int msgid,char * msg,char * file,int line,int no_write)2907c478bd9Sstevel@tonic-gate add_msgid(SetID si, int msgid, char *msg, char *file, int line, int no_write)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate MsgID mi = si->top, newmi, prev = NULL;
2937c478bd9Sstevel@tonic-gate int len = strlen(msg);
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate if (msgid == 0) {
2967c478bd9Sstevel@tonic-gate src_err(file, line, gettext("improper message number: %d"),
2977c478bd9Sstevel@tonic-gate msgid);
2987c478bd9Sstevel@tonic-gate return;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate if (msgid > NL_MSGMAX) {
3027c478bd9Sstevel@tonic-gate src_err(file, line, gettext("too large message number: %d"),
3037c478bd9Sstevel@tonic-gate msgid);
3047c478bd9Sstevel@tonic-gate return;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate if (len > NL_TEXTMAX) {
3087c478bd9Sstevel@tonic-gate src_err(file, line, gettext("too long message text"));
3097c478bd9Sstevel@tonic-gate return;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate while (mi) {
3137c478bd9Sstevel@tonic-gate if (mi->id > msgid) {
3147c478bd9Sstevel@tonic-gate break;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate prev = mi;
3177c478bd9Sstevel@tonic-gate mi = mi->next;
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
320*6e54a631Smuffin if ((newmi = malloc(sizeof (MsgIDRec))) == NULL) {
3217c478bd9Sstevel@tonic-gate prg_err(gettext("fatal: out of memory"));
3227c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate newmi->no_write = no_write;
3267c478bd9Sstevel@tonic-gate newmi->id = msgid;
327*6e54a631Smuffin newmi->msg = ustrdup(msg);
328*6e54a631Smuffin newmi->file = ustrdup(file);
3297c478bd9Sstevel@tonic-gate newmi->line = line;
3307c478bd9Sstevel@tonic-gate newmi->next = mi;
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate if (msg_comment) {
333*6e54a631Smuffin newmi->comment = ustrdup(msg_comment);
3347c478bd9Sstevel@tonic-gate free(msg_comment);
3357c478bd9Sstevel@tonic-gate msg_comment = NULL;
3367c478bd9Sstevel@tonic-gate } else {
3377c478bd9Sstevel@tonic-gate newmi->comment = NULL;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate if (prev == NULL) {
3417c478bd9Sstevel@tonic-gate si->top = newmi;
3427c478bd9Sstevel@tonic-gate } else {
3437c478bd9Sstevel@tonic-gate prev->next = newmi;
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate static void
add_setid(int setid,int msgid,char * msg,char * file,int line,int no_write)3487c478bd9Sstevel@tonic-gate add_setid(int setid, int msgid, char *msg, char *file, int line, int no_write)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate SetID si = setid_top, newsi, prev = NULL;
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate while (si) {
3537c478bd9Sstevel@tonic-gate if (si->id > setid) {
3547c478bd9Sstevel@tonic-gate break;
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate prev = si;
3577c478bd9Sstevel@tonic-gate si = si->next;
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate
360*6e54a631Smuffin if ((newsi = malloc(sizeof (SetIDRec))) == NULL) {
3617c478bd9Sstevel@tonic-gate prg_err(gettext("fatal: out of memory"));
3627c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate newsi->id = setid;
3667c478bd9Sstevel@tonic-gate newsi->top = NULL;
3677c478bd9Sstevel@tonic-gate newsi->next = si;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate if (set_comment) {
370*6e54a631Smuffin newsi->comment = ustrdup(set_comment);
3717c478bd9Sstevel@tonic-gate free(set_comment);
3727c478bd9Sstevel@tonic-gate set_comment = NULL;
3737c478bd9Sstevel@tonic-gate } else {
3747c478bd9Sstevel@tonic-gate newsi->comment = NULL;
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate if (prev == NULL) {
3787c478bd9Sstevel@tonic-gate setid_top = newsi;
3797c478bd9Sstevel@tonic-gate } else {
3807c478bd9Sstevel@tonic-gate prev->next = newsi;
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate add_msgid(newsi, msgid, msg, file, line, no_write);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate static void
print_prefix(FILE * fp,char * prefix,int rm_blank,char * str)3877c478bd9Sstevel@tonic-gate print_prefix(FILE *fp, char *prefix, int rm_blank, char *str)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%s", prefix);
3907c478bd9Sstevel@tonic-gate while (*str) {
3917c478bd9Sstevel@tonic-gate (void) fputc(*str, fp);
3927c478bd9Sstevel@tonic-gate if (*str == '\n' && *(str+1) != '\0') {
3937c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%s", prefix);
3947c478bd9Sstevel@tonic-gate if (rm_blank == TRUE) {
3957c478bd9Sstevel@tonic-gate str++;
3967c478bd9Sstevel@tonic-gate SkipSpace(str);
3977c478bd9Sstevel@tonic-gate continue;
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate str++;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate if (*(str-1) != '\n') {
4037c478bd9Sstevel@tonic-gate (void) fputc('\n', fp);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate int
read_projfile(char * file)4087c478bd9Sstevel@tonic-gate read_projfile(char *file)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate FILE *fp;
4117c478bd9Sstevel@tonic-gate char line[LINE_MAX];
4127c478bd9Sstevel@tonic-gate
413*6e54a631Smuffin if (file == NULL) {
4147c478bd9Sstevel@tonic-gate return (0);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) {
4187c478bd9Sstevel@tonic-gate return (0);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate
421*6e54a631Smuffin while (fgets(line, sizeof (line), fp) != NULL) {
4227c478bd9Sstevel@tonic-gate char *p = line;
4237c478bd9Sstevel@tonic-gate int n, setid, msgid;
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate SkipSpace(p);
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate if (*p == '#' || *p == '\n') {
4287c478bd9Sstevel@tonic-gate continue;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate n = sscanf(p, "%d %d", &setid, &msgid);
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate if (n == 2) {
4347c478bd9Sstevel@tonic-gate if (setid > NL_SETMAX) {
4357c478bd9Sstevel@tonic-gate prg_err(gettext("%s: too large set number: %d"),
4367c478bd9Sstevel@tonic-gate file, setid);
4377c478bd9Sstevel@tonic-gate continue;
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate msgid_table[setid] = msgid;
4407c478bd9Sstevel@tonic-gate } else {
441*6e54a631Smuffin prg_err(gettext(
442*6e54a631Smuffin "warning: %s: missing or invalid entry"), file);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate (void) fclose(fp);
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate return (1);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate void
write_projfile(char * file)4527c478bd9Sstevel@tonic-gate write_projfile(char *file)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate FILE *fp;
4557c478bd9Sstevel@tonic-gate register int i;
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate if (is_writable(file) == FALSE) {
4587c478bd9Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\": permission denied"),
4597c478bd9Sstevel@tonic-gate file);
4607c478bd9Sstevel@tonic-gate return;
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "w")) == NULL) {
4647c478bd9Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\""), file);
4657c478bd9Sstevel@tonic-gate return;
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate for (i = 1; i <= NL_SETMAX; i++) {
4697c478bd9Sstevel@tonic-gate if (msgid_table[i] > 0) {
4707c478bd9Sstevel@tonic-gate SetID si;
4717c478bd9Sstevel@tonic-gate char *com = NULL;
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate if (IsActiveMode(SetCommentMode) &&
4747c478bd9Sstevel@tonic-gate (si = lookup_setid(i)) && si->comment) {
4757c478bd9Sstevel@tonic-gate com = si->comment;
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate if (com && !IsActiveMode(BackCommentMode)) {
4797c478bd9Sstevel@tonic-gate print_prefix(fp, "# ", TRUE, com);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%d\t%d\n", i, msgid_table[i]);
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate if (com && IsActiveMode(BackCommentMode)) {
4857c478bd9Sstevel@tonic-gate print_prefix(fp, "# ", TRUE, com);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate (void) fclose(fp);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate int
get_msgid(char * file,int line,int setid,char * str)4947c478bd9Sstevel@tonic-gate get_msgid(char *file, int line, int setid, char *str)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate SetID si = setid_top;
4977c478bd9Sstevel@tonic-gate int id = msgid_table[setid];
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate while (si) {
5007c478bd9Sstevel@tonic-gate if (si->id == setid) {
5017c478bd9Sstevel@tonic-gate MsgID mi = si->top;
5027c478bd9Sstevel@tonic-gate while (mi) {
5037c478bd9Sstevel@tonic-gate if (strcmp(mi->msg, str) == 0) {
5047c478bd9Sstevel@tonic-gate return (mi->id);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate mi = mi->next;
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate si = si->next;
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate id++;
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate if (id > NL_MSGMAX) {
5157c478bd9Sstevel@tonic-gate src_err(file, line,
5167c478bd9Sstevel@tonic-gate gettext("run out of message number in set number: %d"),
5177c478bd9Sstevel@tonic-gate setid);
5187c478bd9Sstevel@tonic-gate return (NOMSGID);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate return (msgid_table[setid] = id);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate void
set_msgid(int setid,int msgid)5257c478bd9Sstevel@tonic-gate set_msgid(int setid, int msgid)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate if (msgid_table[setid] < msgid) {
5287c478bd9Sstevel@tonic-gate msgid_table[setid] = msgid;
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate void
add_comment(Mode mode,char * str)5337c478bd9Sstevel@tonic-gate add_comment(Mode mode, char *str)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate char *tag = (mode == MsgCommentMode) ? mctag : sctag;
5367c478bd9Sstevel@tonic-gate char **comment = (mode == MsgCommentMode)
5377c478bd9Sstevel@tonic-gate ? &msg_comment : &set_comment;
5387c478bd9Sstevel@tonic-gate
539*6e54a631Smuffin if (strstr(str, tag) == NULL) {
5407c478bd9Sstevel@tonic-gate return;
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate if (*comment) {
5447c478bd9Sstevel@tonic-gate free(*comment);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate
547*6e54a631Smuffin *comment = ustrdup(str);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate void
read_msgfile(char * file)5517c478bd9Sstevel@tonic-gate read_msgfile(char *file)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate FILE *fp;
5547c478bd9Sstevel@tonic-gate char c = 0;
5557c478bd9Sstevel@tonic-gate int line = 0;
5567c478bd9Sstevel@tonic-gate int inmsg = FALSE;
5577c478bd9Sstevel@tonic-gate int setid = 0, unsetid = -1, msgid = 0;
5587c478bd9Sstevel@tonic-gate struct stat buf;
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) {
5617c478bd9Sstevel@tonic-gate prg_err(gettext("cannot open \"%s\""), file);
5627c478bd9Sstevel@tonic-gate ResetActiveMode(AppendMode);
5637c478bd9Sstevel@tonic-gate return;
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate if (stat(file, &buf) == -1 && buf.st_size == 0) {
5677c478bd9Sstevel@tonic-gate ResetActiveMode(AppendMode);
5687c478bd9Sstevel@tonic-gate return;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate quote = c;
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate /*CONSTCOND*/
5747c478bd9Sstevel@tonic-gate while (1) {
5757c478bd9Sstevel@tonic-gate char buf[LINE_MAX];
5767c478bd9Sstevel@tonic-gate char *ptr;
5777c478bd9Sstevel@tonic-gate char msg[NL_TEXTMAX];
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate if (fgets(buf, sizeof (buf), fp) == NULL) {
5807c478bd9Sstevel@tonic-gate break;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate line++;
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate ptr = &buf[0];
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate SkipSpace(ptr);
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate if ((*ptr == '$' && (*(ptr+1) == ' ' || *(ptr+1) == '\t')) ||
5907c478bd9Sstevel@tonic-gate ((*ptr == '\n') && inmsg == FALSE)) {
5917c478bd9Sstevel@tonic-gate inmsg = FALSE;
5927c478bd9Sstevel@tonic-gate continue;
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate if (strncmp(ptr, SET_TOKEN, sizeof (SET_TOKEN) - 1) == 0) {
5967c478bd9Sstevel@tonic-gate if (sscanf(ptr, "%*s %d", &setid) != 1) {
5977c478bd9Sstevel@tonic-gate setid = 0;
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate inmsg = FALSE;
6007c478bd9Sstevel@tonic-gate continue;
6017c478bd9Sstevel@tonic-gate } else if (strncmp(ptr, DELSET_TOKEN,
6027c478bd9Sstevel@tonic-gate sizeof (DELSET_TOKEN) - 1) == 0) {
6037c478bd9Sstevel@tonic-gate if (sscanf(ptr, "%*s %d", &unsetid) != 1) {
6047c478bd9Sstevel@tonic-gate unsetid = -1;
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate inmsg = FALSE;
6077c478bd9Sstevel@tonic-gate continue;
6087c478bd9Sstevel@tonic-gate } else if (strncmp(ptr, QUOTE_TOKEN,
6097c478bd9Sstevel@tonic-gate sizeof (QUOTE_TOKEN) - 1) == 0) {
6107c478bd9Sstevel@tonic-gate if (sscanf(ptr, "%*s %c", &c) != 1) {
6117c478bd9Sstevel@tonic-gate c = 0;
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate quote = c;
6147c478bd9Sstevel@tonic-gate inmsg = FALSE;
6157c478bd9Sstevel@tonic-gate continue;
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate
6187c478bd9Sstevel@tonic-gate if (setid == unsetid) {
6197c478bd9Sstevel@tonic-gate continue;
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate if (inmsg) {
6237c478bd9Sstevel@tonic-gate if (is_bs_terminated(ptr)) {
624*6e54a631Smuffin (void) strlcat(msg, ptr, sizeof (msg));
6257c478bd9Sstevel@tonic-gate inmsg = TRUE;
6267c478bd9Sstevel@tonic-gate } else {
6277c478bd9Sstevel@tonic-gate int len = strlen(ptr);
6287c478bd9Sstevel@tonic-gate *(ptr + len - 1) = '\0';
6297c478bd9Sstevel@tonic-gate if (c && (*(ptr + len - 2) == c)) {
6307c478bd9Sstevel@tonic-gate *(ptr + len - 2) = '\0';
6317c478bd9Sstevel@tonic-gate }
632*6e54a631Smuffin (void) strlcat(msg, ptr, sizeof (msg));
6337c478bd9Sstevel@tonic-gate add_msg(setid, msgid, msg, file, line, TRUE);
6347c478bd9Sstevel@tonic-gate inmsg = FALSE;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate continue;
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate
639*6e54a631Smuffin if (isdigit((unsigned char)*ptr)) {
640*6e54a631Smuffin char *pptr;
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate SkipSpace(ptr);
6437c478bd9Sstevel@tonic-gate
644*6e54a631Smuffin msgid = (int)strtol(ptr, &pptr, 10);
645*6e54a631Smuffin ptr = pptr;
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate SkipSpace(ptr);
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate if (is_bs_terminated(ptr)) {
650*6e54a631Smuffin (void) memset(msg, 0, sizeof (msg));
6517c478bd9Sstevel@tonic-gate if (c && (*ptr == c)) {
6527c478bd9Sstevel@tonic-gate ptr++;
6537c478bd9Sstevel@tonic-gate }
654*6e54a631Smuffin (void) strlcpy(msg, ptr, sizeof (msg));
6557c478bd9Sstevel@tonic-gate inmsg = TRUE;
6567c478bd9Sstevel@tonic-gate } else {
6577c478bd9Sstevel@tonic-gate int len = strlen(ptr);
6587c478bd9Sstevel@tonic-gate *(ptr + len - 1) = '\0';
6597c478bd9Sstevel@tonic-gate if (c && ((*ptr == c) &&
6607c478bd9Sstevel@tonic-gate (*(ptr + len - 2) == c))) {
6617c478bd9Sstevel@tonic-gate *(ptr + len - 2) = '\0';
6627c478bd9Sstevel@tonic-gate ptr++;
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate add_msg(setid, msgid, ptr, file, line, TRUE);
6657c478bd9Sstevel@tonic-gate inmsg = FALSE;
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate (void) fclose(fp);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate
6737c478bd9Sstevel@tonic-gate static int
is_bs_terminated(char * msg)6747c478bd9Sstevel@tonic-gate is_bs_terminated(char *msg)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate int len = strlen(msg);
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate while (--len >= 0) {
6797c478bd9Sstevel@tonic-gate if (msg[len] == ' ' || msg[len] == '\t' || msg[len] == '\n') {
6807c478bd9Sstevel@tonic-gate continue;
6817c478bd9Sstevel@tonic-gate } else if (msg[len] == '\\') {
6827c478bd9Sstevel@tonic-gate len--;
6837c478bd9Sstevel@tonic-gate if (len >= 0 && msg[len] == '\\')
6847c478bd9Sstevel@tonic-gate return (0);
6857c478bd9Sstevel@tonic-gate return (1);
6867c478bd9Sstevel@tonic-gate } else {
6877c478bd9Sstevel@tonic-gate return (0);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate return (0);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate static char *
ustrdup(char * str)6947c478bd9Sstevel@tonic-gate ustrdup(char *str)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate char *tmp = strdup(str);
697*6e54a631Smuffin if (tmp == NULL) {
6987c478bd9Sstevel@tonic-gate prg_err(gettext("fatal: out of memory"));
6997c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate return (tmp);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate int
file_copy(char * in,char * out)7057c478bd9Sstevel@tonic-gate file_copy(char *in, char *out)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate int ret = TRUE;
7087c478bd9Sstevel@tonic-gate FILE *fin, *fout;
7097c478bd9Sstevel@tonic-gate int c;
7107c478bd9Sstevel@tonic-gate sigset_t newmask, oldmask;
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate (void) sigemptyset(&newmask);
7137c478bd9Sstevel@tonic-gate (void) sigaddset(&newmask, SIGQUIT);
7147c478bd9Sstevel@tonic-gate (void) sigaddset(&newmask, SIGINT);
7157c478bd9Sstevel@tonic-gate (void) sigaddset(&newmask, SIGHUP);
7167c478bd9Sstevel@tonic-gate (void) sigaddset(&newmask, SIGTERM);
7177c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &newmask, &oldmask);
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate if ((fin = fopen(in, "r")) == NULL) {
7207c478bd9Sstevel@tonic-gate prg_err(gettext("cannot open \"%s\""), in);
7217c478bd9Sstevel@tonic-gate ret = FALSE;
7227c478bd9Sstevel@tonic-gate goto done;
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate if ((fout = fopen(out, "w")) == NULL) {
7267c478bd9Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\""), out);
7277c478bd9Sstevel@tonic-gate ret = FALSE;
7287c478bd9Sstevel@tonic-gate goto done;
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate while ((c = getc(fin)) != EOF)
7327c478bd9Sstevel@tonic-gate (void) putc(c, fout);
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate (void) fclose(fin);
7357c478bd9Sstevel@tonic-gate (void) fclose(fout);
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate done:
7387c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oldmask, NULL);
7397c478bd9Sstevel@tonic-gate return (ret);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate static void
makeup_msg(char ** pmsg)7437c478bd9Sstevel@tonic-gate makeup_msg(char **pmsg)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate char buf[NL_TEXTMAX];
7467c478bd9Sstevel@tonic-gate char *msg;
7477c478bd9Sstevel@tonic-gate
7487c478bd9Sstevel@tonic-gate msg = *pmsg;
749*6e54a631Smuffin buf[0] = '\0';
7507c478bd9Sstevel@tonic-gate
751*6e54a631Smuffin if (IsActiveMode(TripleMode) && strchr(msg, '%') == NULL) {
7527c478bd9Sstevel@tonic-gate /* there is no '%' in message. */
7537c478bd9Sstevel@tonic-gate int len = strlen(msg);
7547c478bd9Sstevel@tonic-gate
7557c478bd9Sstevel@tonic-gate if (msg[len-2] == '\\' && msg[len-1] == 'n') {
7567c478bd9Sstevel@tonic-gate msg[len-2] = '\0';
757*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
758*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
759*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
760*6e54a631Smuffin (void) strlcat(buf, "\\n", sizeof (buf));
7617c478bd9Sstevel@tonic-gate } else {
762*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
763*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
764*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate free(msg);
767*6e54a631Smuffin *pmsg = ustrdup(buf);
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate msg = *pmsg;
771*6e54a631Smuffin buf[0] = '\0';
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate if (IsActiveMode(PrefixMode)) {
774*6e54a631Smuffin (void) strlcat(buf, premsg, sizeof (buf));
775*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
7767c478bd9Sstevel@tonic-gate free(msg);
777*6e54a631Smuffin *pmsg = ustrdup(buf);
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate
7807c478bd9Sstevel@tonic-gate msg = *pmsg;
781*6e54a631Smuffin buf[0] = '\0';
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate if (IsActiveMode(SuffixMode)) {
7847c478bd9Sstevel@tonic-gate int len = strlen(msg);
7857c478bd9Sstevel@tonic-gate
7867c478bd9Sstevel@tonic-gate if (msg[len-2] == '\\' && msg[len-1] == 'n') {
7877c478bd9Sstevel@tonic-gate msg[len-2] = '\0';
788*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
789*6e54a631Smuffin (void) strlcat(buf, sufmsg, sizeof (buf));
790*6e54a631Smuffin (void) strlcat(buf, "\\n", sizeof (buf));
7917c478bd9Sstevel@tonic-gate } else {
792*6e54a631Smuffin (void) strlcat(buf, msg, sizeof (buf));
793*6e54a631Smuffin (void) strlcat(buf, sufmsg, sizeof (buf));
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate free(msg);
796*6e54a631Smuffin *pmsg = ustrdup(buf);
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate
8007c478bd9Sstevel@tonic-gate void
prg_err(char * fmt,...)8017c478bd9Sstevel@tonic-gate prg_err(char *fmt, ...)
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate va_list ap;
8047c478bd9Sstevel@tonic-gate
8057c478bd9Sstevel@tonic-gate va_start(ap, fmt);
8067c478bd9Sstevel@tonic-gate
807*6e54a631Smuffin (void) fprintf(stderr, "%s: ", program);
808*6e54a631Smuffin /* LINTED: E_SEC_PRINTF_VAR_FMT */
809*6e54a631Smuffin (void) vfprintf(stderr, fmt, ap);
810*6e54a631Smuffin (void) fprintf(stderr, "\n");
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate va_end(ap);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate
8157c478bd9Sstevel@tonic-gate void
src_err(char * file,int line,char * fmt,...)8167c478bd9Sstevel@tonic-gate src_err(char *file, int line, char *fmt, ...)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate va_list ap;
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate if (suppress_error == TRUE) {
8217c478bd9Sstevel@tonic-gate return;
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate
8247c478bd9Sstevel@tonic-gate va_start(ap, fmt);
8257c478bd9Sstevel@tonic-gate
826*6e54a631Smuffin (void) fprintf(stderr, gettext("\"%s\", line %d: "), file, line);
827*6e54a631Smuffin /* LINTED: E_SEC_PRINTF_VAR_FMT */
828*6e54a631Smuffin (void) vfprintf(stderr, fmt, ap);
829*6e54a631Smuffin (void) fprintf(stderr, "\n");
8307c478bd9Sstevel@tonic-gate
8317c478bd9Sstevel@tonic-gate va_end(ap);
8327c478bd9Sstevel@tonic-gate }
833