10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*4933Smuffin * Common Development and Distribution License (the "License").
6*4933Smuffin * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*4933Smuffin * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23*4933Smuffin * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <limits.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdarg.h>
330Sstevel@tonic-gate #include <libintl.h>
340Sstevel@tonic-gate #include <locale.h>
350Sstevel@tonic-gate #include <libgen.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <signal.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include "genmsg.h"
430Sstevel@tonic-gate
440Sstevel@tonic-gate #define SET_TOKEN "$set"
450Sstevel@tonic-gate #define DELSET_TOKEN "$delset"
460Sstevel@tonic-gate #define QUOTE_TOKEN "$quote"
470Sstevel@tonic-gate
480Sstevel@tonic-gate #define SkipSpace(s) while (*(s) == ' ' || *(s) == '\t') s++
490Sstevel@tonic-gate
500Sstevel@tonic-gate extern char *program; /* from main.c */
510Sstevel@tonic-gate extern char *mctag; /* from main.c */
520Sstevel@tonic-gate extern char *sctag; /* from main.c */
530Sstevel@tonic-gate extern char *premsg; /* from main.c */
540Sstevel@tonic-gate extern char *sufmsg; /* from main.c */
550Sstevel@tonic-gate extern int suppress_error; /* from main.c */
560Sstevel@tonic-gate extern void warning(char *); /* from genmsg.l */
570Sstevel@tonic-gate
580Sstevel@tonic-gate typedef struct _SetID *SetID;
590Sstevel@tonic-gate typedef struct _MsgID *MsgID;
600Sstevel@tonic-gate
610Sstevel@tonic-gate typedef struct _SetID SetIDRec;
620Sstevel@tonic-gate struct _SetID {
630Sstevel@tonic-gate int id;
640Sstevel@tonic-gate char *comment;
650Sstevel@tonic-gate MsgID top;
660Sstevel@tonic-gate SetID next;
670Sstevel@tonic-gate };
680Sstevel@tonic-gate
690Sstevel@tonic-gate typedef struct _MsgID MsgIDRec;
700Sstevel@tonic-gate struct _MsgID {
710Sstevel@tonic-gate int no_write;
720Sstevel@tonic-gate int id;
730Sstevel@tonic-gate char *msg;
740Sstevel@tonic-gate int line;
750Sstevel@tonic-gate char *file;
760Sstevel@tonic-gate char *comment;
770Sstevel@tonic-gate MsgID next;
780Sstevel@tonic-gate };
790Sstevel@tonic-gate
800Sstevel@tonic-gate
810Sstevel@tonic-gate /* Top pointer of the setid list. */
820Sstevel@tonic-gate static SetID setid_top;
830Sstevel@tonic-gate
840Sstevel@tonic-gate /* comment for messages. */
850Sstevel@tonic-gate static char *msg_comment;
860Sstevel@tonic-gate
870Sstevel@tonic-gate /* comment for set numbers. */
880Sstevel@tonic-gate static char *set_comment;
890Sstevel@tonic-gate
900Sstevel@tonic-gate /* List of set number's maximum message numbers. */
910Sstevel@tonic-gate static int msgid_table[NL_SETMAX+1];
920Sstevel@tonic-gate
930Sstevel@tonic-gate /* Quote character to surround messages. */
940Sstevel@tonic-gate static char quote = QUOTE;
950Sstevel@tonic-gate
960Sstevel@tonic-gate /* Internal functions. */
970Sstevel@tonic-gate static void add_msgid(SetID, int, char *, char *, int, int);
980Sstevel@tonic-gate static void add_setid(int, int, char *, char *, int, int);
990Sstevel@tonic-gate static SetID lookup_setid(int);
1000Sstevel@tonic-gate static MsgID lookup_msgid(SetID, int, char *, char *, int);
1010Sstevel@tonic-gate static void print_prefix(FILE *, char *, int, char *);
1020Sstevel@tonic-gate static int is_bs_terminated(char *);
1030Sstevel@tonic-gate static char *ustrdup(char *);
1040Sstevel@tonic-gate static void makeup_msg(char **);
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate void
add_msg(int setid,int msgid,char * msg,char * file,int line,int no_write)1070Sstevel@tonic-gate add_msg(int setid, int msgid, char *msg, char *file, int line, int no_write)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate SetID si;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if (si = lookup_setid(setid)) {
1120Sstevel@tonic-gate if (lookup_msgid(si, msgid, msg, file, line)) {
1130Sstevel@tonic-gate return; /* we already have the one. */
1140Sstevel@tonic-gate } else {
1150Sstevel@tonic-gate add_msgid(si, msgid, msg, file, line, no_write);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate } else {
1180Sstevel@tonic-gate add_setid(setid, msgid, msg, file, line, no_write);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate int
is_writable(char * file)1230Sstevel@tonic-gate is_writable(char *file)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate struct stat buf;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate if (stat(file, &buf) == -1)
1280Sstevel@tonic-gate return (TRUE);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (access(file, W_OK) == 0)
1310Sstevel@tonic-gate return (TRUE);
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate return (FALSE);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate void
write_msgfile(char * file)1370Sstevel@tonic-gate write_msgfile(char *file)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate FILE *fp;
1400Sstevel@tonic-gate SetID si = setid_top;
1410Sstevel@tonic-gate char *mode = "w";
1420Sstevel@tonic-gate char pquote[2];
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if (is_writable(file) == FALSE) {
1450Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\": permission denied"),
146*4933Smuffin file);
1470Sstevel@tonic-gate return;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate if (IsActiveMode(AppendMode)) {
1510Sstevel@tonic-gate mode = "a";
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate if ((fp = fopen(file, mode)) == NULL) {
1550Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\""), file);
1560Sstevel@tonic-gate return;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (quote) {
160*4933Smuffin pquote[0] = quote;
1610Sstevel@tonic-gate } else {
162*4933Smuffin pquote[0] = '\0';
1630Sstevel@tonic-gate }
164*4933Smuffin pquote[1] = '\0';
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate /* AppendMode is already turned off if the file doesn't exist. */
1670Sstevel@tonic-gate if (!IsActiveMode(AppendMode)) {
1680Sstevel@tonic-gate (void) fprintf(fp, "\n$quote %s\n\n", pquote);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate while (si) {
1720Sstevel@tonic-gate int is_set = FALSE;
1730Sstevel@tonic-gate MsgID mi = si->top;
1740Sstevel@tonic-gate while (mi) {
1750Sstevel@tonic-gate char msg[NL_TEXTMAX+32]; /* 32 is some other stuff. */
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (mi->no_write) {
1780Sstevel@tonic-gate mi = mi->next;
1790Sstevel@tonic-gate continue;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate if (is_set == FALSE) {
1820Sstevel@tonic-gate if (si->comment &&
183*4933Smuffin !IsActiveMode(BackCommentMode)) {
1840Sstevel@tonic-gate (void) fprintf(fp, "\n");
1850Sstevel@tonic-gate print_prefix(fp, "$ ", TRUE,
186*4933Smuffin si->comment);
1870Sstevel@tonic-gate (void) fprintf(fp, "$set\t%d\n",
188*4933Smuffin si->id);
1890Sstevel@tonic-gate } else {
1900Sstevel@tonic-gate (void) fprintf(fp, "\n$set\t%d\n",
191*4933Smuffin si->id);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate if (si->comment &&
194*4933Smuffin IsActiveMode(BackCommentMode)) {
1950Sstevel@tonic-gate print_prefix(fp, "$ ", TRUE,
196*4933Smuffin si->comment);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate (void) fprintf(fp, "\n");
1990Sstevel@tonic-gate is_set = TRUE;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate makeup_msg(&(mi->msg));
2030Sstevel@tonic-gate
204*4933Smuffin (void) snprintf(msg, sizeof (msg), "%d\t%s%s%s\n",
205*4933Smuffin mi->id, pquote, mi->msg, pquote);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (!IsActiveMode(BackCommentMode)) {
2080Sstevel@tonic-gate if (mi->line && mi->file &&
209*4933Smuffin IsActiveMode(LineInfoMode)) {
2100Sstevel@tonic-gate (void) fprintf(fp,
211*4933Smuffin "$ File:%s, line:%d\n",
212*4933Smuffin basename(mi->file), mi->line);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if (mi->comment) {
2160Sstevel@tonic-gate print_prefix(fp, "$ ", TRUE,
217*4933Smuffin mi->comment);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if (IsActiveMode(DoubleLineMode)) {
2210Sstevel@tonic-gate print_prefix(fp, "$ ", FALSE, msg);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate (void) fprintf(fp, "%s", msg);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (IsActiveMode(BackCommentMode)) {
2280Sstevel@tonic-gate if (mi->line && mi->file &&
229*4933Smuffin IsActiveMode(LineInfoMode)) {
2300Sstevel@tonic-gate (void) fprintf(fp,
231*4933Smuffin "$ File:%s, line:%d\n",
232*4933Smuffin basename(mi->file), mi->line);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate if (mi->comment) {
2360Sstevel@tonic-gate print_prefix(fp, "$ ", TRUE,
237*4933Smuffin mi->comment);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate if (IsActiveMode(DoubleLineMode)) {
2410Sstevel@tonic-gate print_prefix(fp, "$ ", FALSE, msg);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate (void) fprintf(fp, "\n");
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate mi = mi->next;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate si = si->next;
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate (void) fclose(fp);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate static SetID
lookup_setid(int id)2560Sstevel@tonic-gate lookup_setid(int id)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate SetID si = setid_top;
2590Sstevel@tonic-gate while (si) {
2600Sstevel@tonic-gate if (si->id == id) {
2610Sstevel@tonic-gate return (si);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate si = si->next;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate return (NULL);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate static MsgID
lookup_msgid(SetID si,int msgid,char * msg,char * file,int line)2690Sstevel@tonic-gate lookup_msgid(SetID si, int msgid, char *msg, char *file, int line)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate MsgID mi = si->top;
2720Sstevel@tonic-gate while (mi) {
2730Sstevel@tonic-gate if (mi->id == msgid) {
2740Sstevel@tonic-gate /* same setid & msgid, but different msg. */
2750Sstevel@tonic-gate if (strcmp(mi->msg, msg)) {
276*4933Smuffin src_err(file, line, gettext(
277*4933Smuffin "multiple messages: set number %d, message number %d\n"
2780Sstevel@tonic-gate " current : \"%s\"\n"
2790Sstevel@tonic-gate " previous: \"%s\" : \"%s\", line %d"),
280*4933Smuffin si->id, mi->id,
281*4933Smuffin msg,
282*4933Smuffin mi->msg, mi->file, mi->line);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate return (mi);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate mi = mi->next;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate return (NULL);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate static void
add_msgid(SetID si,int msgid,char * msg,char * file,int line,int no_write)2920Sstevel@tonic-gate add_msgid(SetID si, int msgid, char *msg, char *file, int line, int no_write)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate MsgID mi = si->top, newmi, prev = NULL;
2950Sstevel@tonic-gate int len = strlen(msg);
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if (msgid == 0) {
2980Sstevel@tonic-gate src_err(file, line, gettext("improper message number: %d"),
299*4933Smuffin msgid);
3000Sstevel@tonic-gate return;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate if (msgid > NL_MSGMAX) {
3040Sstevel@tonic-gate src_err(file, line, gettext("too large message number: %d"),
305*4933Smuffin msgid);
3060Sstevel@tonic-gate return;
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate if (len > NL_TEXTMAX) {
3100Sstevel@tonic-gate src_err(file, line, gettext("too long message text"));
3110Sstevel@tonic-gate return;
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate while (mi) {
3150Sstevel@tonic-gate if (mi->id > msgid) {
3160Sstevel@tonic-gate break;
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate prev = mi;
3190Sstevel@tonic-gate mi = mi->next;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate
322*4933Smuffin if ((newmi = malloc(sizeof (MsgIDRec))) == NULL) {
3230Sstevel@tonic-gate prg_err(gettext("fatal: out of memory"));
3240Sstevel@tonic-gate exit(EXIT_FAILURE);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate newmi->no_write = no_write;
3280Sstevel@tonic-gate newmi->id = msgid;
329*4933Smuffin newmi->msg = ustrdup(msg);
330*4933Smuffin newmi->file = ustrdup(file);
3310Sstevel@tonic-gate newmi->line = line;
3320Sstevel@tonic-gate newmi->next = mi;
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate if (msg_comment) {
335*4933Smuffin newmi->comment = ustrdup(msg_comment);
3360Sstevel@tonic-gate free(msg_comment);
3370Sstevel@tonic-gate msg_comment = NULL;
3380Sstevel@tonic-gate } else {
3390Sstevel@tonic-gate newmi->comment = NULL;
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate if (prev == NULL) {
3430Sstevel@tonic-gate si->top = newmi;
3440Sstevel@tonic-gate } else {
3450Sstevel@tonic-gate prev->next = newmi;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate static void
add_setid(int setid,int msgid,char * msg,char * file,int line,int no_write)3500Sstevel@tonic-gate add_setid(int setid, int msgid, char *msg, char *file, int line, int no_write)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate SetID si = setid_top, newsi, prev = NULL;
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate while (si) {
3550Sstevel@tonic-gate if (si->id > setid) {
3560Sstevel@tonic-gate break;
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate prev = si;
3590Sstevel@tonic-gate si = si->next;
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate
362*4933Smuffin if ((newsi = malloc(sizeof (SetIDRec))) == NULL) {
3630Sstevel@tonic-gate prg_err(gettext("fatal: out of memory"));
3640Sstevel@tonic-gate exit(EXIT_FAILURE);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate newsi->id = setid;
3680Sstevel@tonic-gate newsi->top = NULL;
3690Sstevel@tonic-gate newsi->next = si;
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate if (set_comment) {
372*4933Smuffin newsi->comment = ustrdup(set_comment);
3730Sstevel@tonic-gate free(set_comment);
3740Sstevel@tonic-gate set_comment = NULL;
3750Sstevel@tonic-gate } else {
3760Sstevel@tonic-gate newsi->comment = NULL;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate if (prev == NULL) {
3800Sstevel@tonic-gate setid_top = newsi;
3810Sstevel@tonic-gate } else {
3820Sstevel@tonic-gate prev->next = newsi;
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate add_msgid(newsi, msgid, msg, file, line, no_write);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate static void
print_prefix(FILE * fp,char * prefix,int rm_blank,char * str)3890Sstevel@tonic-gate print_prefix(FILE *fp, char *prefix, int rm_blank, char *str)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate (void) fprintf(fp, "%s", prefix);
3920Sstevel@tonic-gate while (*str) {
3930Sstevel@tonic-gate (void) fputc(*str, fp);
3940Sstevel@tonic-gate if (*str == '\n' && *(str+1) != '\0') {
3950Sstevel@tonic-gate (void) fprintf(fp, "%s", prefix);
3960Sstevel@tonic-gate if (rm_blank == TRUE) {
3970Sstevel@tonic-gate str++;
3980Sstevel@tonic-gate SkipSpace(str);
3990Sstevel@tonic-gate continue;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate str++;
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate if (*(str-1) != '\n') {
4050Sstevel@tonic-gate (void) fputc('\n', fp);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate int
read_projfile(char * file)4100Sstevel@tonic-gate read_projfile(char *file)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate FILE *fp;
4130Sstevel@tonic-gate char line[LINE_MAX];
4140Sstevel@tonic-gate
415*4933Smuffin if (file == NULL) {
4160Sstevel@tonic-gate return (0);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) {
4200Sstevel@tonic-gate return (0);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate
423*4933Smuffin while (fgets(line, sizeof (line), fp) != NULL) {
4240Sstevel@tonic-gate char *p = line;
4250Sstevel@tonic-gate int n, setid, msgid;
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate SkipSpace(p);
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate if (*p == '#' || *p == '\n') {
4300Sstevel@tonic-gate continue;
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate n = sscanf(p, "%d %d", &setid, &msgid);
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate if (n == 2) {
4360Sstevel@tonic-gate if (setid > NL_SETMAX) {
437*4933Smuffin prg_err(gettext("%s: too large set number: %d"),
438*4933Smuffin file, setid);
4390Sstevel@tonic-gate continue;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate msgid_table[setid] = msgid;
4420Sstevel@tonic-gate } else {
443*4933Smuffin prg_err(gettext(
444*4933Smuffin "warning: %s: missing or invalid entry"), file);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate (void) fclose(fp);
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate return (1);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate void
write_projfile(char * file)4540Sstevel@tonic-gate write_projfile(char *file)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate FILE *fp;
4570Sstevel@tonic-gate register int i;
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate if (is_writable(file) == FALSE) {
4600Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\": permission denied"),
461*4933Smuffin file);
4620Sstevel@tonic-gate return;
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate if ((fp = fopen(file, "w")) == NULL) {
4660Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\""), file);
4670Sstevel@tonic-gate return;
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate for (i = 1; i <= NL_SETMAX; i++) {
4710Sstevel@tonic-gate if (msgid_table[i] > 0) {
4720Sstevel@tonic-gate SetID si;
4730Sstevel@tonic-gate char *com = NULL;
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate if (IsActiveMode(SetCommentMode) &&
476*4933Smuffin (si = lookup_setid(i)) && si->comment) {
4770Sstevel@tonic-gate com = si->comment;
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate if (com && !IsActiveMode(BackCommentMode)) {
4810Sstevel@tonic-gate print_prefix(fp, "# ", TRUE, com);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate (void) fprintf(fp, "%d\t%d\n", i, msgid_table[i]);
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate if (com && IsActiveMode(BackCommentMode)) {
4870Sstevel@tonic-gate print_prefix(fp, "# ", TRUE, com);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate (void) fclose(fp);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate int
get_msgid(char * file,int line,int setid,char * str)4960Sstevel@tonic-gate get_msgid(char *file, int line, int setid, char *str)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate SetID si = setid_top;
4990Sstevel@tonic-gate int id = msgid_table[setid];
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate while (si) {
5020Sstevel@tonic-gate if (si->id == setid) {
5030Sstevel@tonic-gate MsgID mi = si->top;
5040Sstevel@tonic-gate while (mi) {
5050Sstevel@tonic-gate if (strcmp(mi->msg, str) == 0) {
5060Sstevel@tonic-gate return (mi->id);
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate mi = mi->next;
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate si = si->next;
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate id++;
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate if (id > NL_MSGMAX) {
5170Sstevel@tonic-gate src_err(file, line,
518*4933Smuffin gettext("run out of message number in set number: %d"),
519*4933Smuffin setid);
5200Sstevel@tonic-gate return (NOMSGID);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate return (msgid_table[setid] = id);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate void
set_msgid(int setid,int msgid)5270Sstevel@tonic-gate set_msgid(int setid, int msgid)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate if (msgid_table[setid] < msgid) {
5300Sstevel@tonic-gate msgid_table[setid] = msgid;
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate void
add_comment(Mode mode,char * str)5350Sstevel@tonic-gate add_comment(Mode mode, char *str)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate char *tag = (mode == MsgCommentMode) ? mctag : sctag;
5380Sstevel@tonic-gate char **comment = (mode == MsgCommentMode)
539*4933Smuffin ? &msg_comment : &set_comment;
5400Sstevel@tonic-gate
541*4933Smuffin if (strstr(str, tag) == NULL) {
5420Sstevel@tonic-gate return;
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate if (*comment) {
5460Sstevel@tonic-gate free(*comment);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate
549*4933Smuffin *comment = ustrdup(str);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate void
read_msgfile(char * file)5530Sstevel@tonic-gate read_msgfile(char *file)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate FILE *fp;
5560Sstevel@tonic-gate char c = 0;
5570Sstevel@tonic-gate int line = 0;
5580Sstevel@tonic-gate int inmsg = FALSE;
5590Sstevel@tonic-gate int setid = 0, unsetid = -1, msgid = 0;
5600Sstevel@tonic-gate struct stat buf;
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) {
5630Sstevel@tonic-gate prg_err(gettext("cannot open \"%s\""), file);
5640Sstevel@tonic-gate ResetActiveMode(AppendMode);
5650Sstevel@tonic-gate return;
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate if (stat(file, &buf) == -1 && buf.st_size == 0) {
5690Sstevel@tonic-gate ResetActiveMode(AppendMode);
5700Sstevel@tonic-gate return;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate quote = c;
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate /*CONSTCOND*/
5760Sstevel@tonic-gate while (1) {
5770Sstevel@tonic-gate char buf[LINE_MAX];
5780Sstevel@tonic-gate char *ptr;
5790Sstevel@tonic-gate char msg[NL_TEXTMAX];
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate if (fgets(buf, sizeof (buf), fp) == NULL) {
5820Sstevel@tonic-gate break;
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate line++;
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate ptr = &buf[0];
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate SkipSpace(ptr);
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate if ((*ptr == '$' && (*(ptr+1) == ' ' || *(ptr+1) == '\t')) ||
592*4933Smuffin ((*ptr == '\n') && inmsg == FALSE)) {
5930Sstevel@tonic-gate inmsg = FALSE;
5940Sstevel@tonic-gate continue;
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate if (strncmp(ptr, SET_TOKEN, sizeof (SET_TOKEN) - 1) == 0) {
5980Sstevel@tonic-gate if (sscanf(ptr, "%*s %d", &setid) != 1) {
5990Sstevel@tonic-gate setid = 0;
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate inmsg = FALSE;
6020Sstevel@tonic-gate continue;
6030Sstevel@tonic-gate } else if (strncmp(ptr, DELSET_TOKEN,
604*4933Smuffin sizeof (DELSET_TOKEN) - 1) == 0) {
6050Sstevel@tonic-gate if (sscanf(ptr, "%*s %d", &unsetid) != 1) {
6060Sstevel@tonic-gate unsetid = -1;
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate inmsg = FALSE;
6090Sstevel@tonic-gate continue;
6100Sstevel@tonic-gate } else if (strncmp(ptr, QUOTE_TOKEN,
611*4933Smuffin sizeof (QUOTE_TOKEN) - 1) == 0) {
6120Sstevel@tonic-gate if (sscanf(ptr, "%*s %c", &c) != 1) {
6130Sstevel@tonic-gate c = 0;
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate quote = c;
6160Sstevel@tonic-gate inmsg = FALSE;
6170Sstevel@tonic-gate continue;
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate if (setid == unsetid) {
6210Sstevel@tonic-gate continue;
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate if (inmsg) {
6250Sstevel@tonic-gate if (is_bs_terminated(ptr)) {
626*4933Smuffin (void) strlcat(msg, ptr, sizeof (msg));
6270Sstevel@tonic-gate inmsg = TRUE;
6280Sstevel@tonic-gate } else {
6290Sstevel@tonic-gate int len = strlen(ptr);
6300Sstevel@tonic-gate *(ptr + len - 1) = '\0';
6310Sstevel@tonic-gate if (c && (*(ptr + len - 2) == c)) {
6320Sstevel@tonic-gate *(ptr + len - 2) = '\0';
6330Sstevel@tonic-gate }
634*4933Smuffin (void) strlcat(msg, ptr, sizeof (msg));
6350Sstevel@tonic-gate add_msg(setid, msgid, msg, file, line, TRUE);
6360Sstevel@tonic-gate inmsg = FALSE;
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate continue;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate
641*4933Smuffin if (isdigit((unsigned char)*ptr)) {
642*4933Smuffin char *pptr;
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate SkipSpace(ptr);
6450Sstevel@tonic-gate
646*4933Smuffin msgid = (int)strtol(ptr, &pptr, 10);
647*4933Smuffin ptr = pptr;
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate SkipSpace(ptr);
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate if (is_bs_terminated(ptr)) {
652*4933Smuffin (void) memset(msg, 0, sizeof (msg));
6530Sstevel@tonic-gate if (c && (*ptr == c)) {
6540Sstevel@tonic-gate ptr++;
6550Sstevel@tonic-gate }
656*4933Smuffin (void) strlcpy(msg, ptr, sizeof (msg));
6570Sstevel@tonic-gate inmsg = TRUE;
6580Sstevel@tonic-gate } else {
6590Sstevel@tonic-gate int len = strlen(ptr);
6600Sstevel@tonic-gate *(ptr + len - 1) = '\0';
6610Sstevel@tonic-gate if (c && ((*ptr == c) &&
662*4933Smuffin (*(ptr + len - 2) == c))) {
6630Sstevel@tonic-gate *(ptr + len - 2) = '\0';
6640Sstevel@tonic-gate ptr++;
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate add_msg(setid, msgid, ptr, file, line, TRUE);
6670Sstevel@tonic-gate inmsg = FALSE;
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate (void) fclose(fp);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate static int
is_bs_terminated(char * msg)6760Sstevel@tonic-gate is_bs_terminated(char *msg)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate int len = strlen(msg);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate while (--len >= 0) {
6810Sstevel@tonic-gate if (msg[len] == ' ' || msg[len] == '\t' || msg[len] == '\n') {
6820Sstevel@tonic-gate continue;
6830Sstevel@tonic-gate } else if (msg[len] == '\\') {
6840Sstevel@tonic-gate len--;
6850Sstevel@tonic-gate if (len >= 0 && msg[len] == '\\')
6860Sstevel@tonic-gate return (0);
6870Sstevel@tonic-gate return (1);
6880Sstevel@tonic-gate } else {
6890Sstevel@tonic-gate return (0);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate return (0);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate static char *
ustrdup(char * str)6960Sstevel@tonic-gate ustrdup(char *str)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate char *tmp = strdup(str);
699*4933Smuffin if (tmp == NULL) {
7000Sstevel@tonic-gate prg_err(gettext("fatal: out of memory"));
7010Sstevel@tonic-gate exit(EXIT_FAILURE);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate return (tmp);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate int
file_copy(char * in,char * out)7070Sstevel@tonic-gate file_copy(char *in, char *out)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate int ret = TRUE;
7100Sstevel@tonic-gate FILE *fin, *fout;
7110Sstevel@tonic-gate int c;
7120Sstevel@tonic-gate sigset_t newmask, oldmask;
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate (void) sigemptyset(&newmask);
7150Sstevel@tonic-gate (void) sigaddset(&newmask, SIGQUIT);
7160Sstevel@tonic-gate (void) sigaddset(&newmask, SIGINT);
7170Sstevel@tonic-gate (void) sigaddset(&newmask, SIGHUP);
7180Sstevel@tonic-gate (void) sigaddset(&newmask, SIGTERM);
7190Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &newmask, &oldmask);
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate if ((fin = fopen(in, "r")) == NULL) {
7220Sstevel@tonic-gate prg_err(gettext("cannot open \"%s\""), in);
7230Sstevel@tonic-gate ret = FALSE;
7240Sstevel@tonic-gate goto done;
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate
7270Sstevel@tonic-gate if ((fout = fopen(out, "w")) == NULL) {
7280Sstevel@tonic-gate prg_err(gettext("cannot create \"%s\""), out);
7290Sstevel@tonic-gate ret = FALSE;
7300Sstevel@tonic-gate goto done;
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate
7330Sstevel@tonic-gate while ((c = getc(fin)) != EOF)
7340Sstevel@tonic-gate (void) putc(c, fout);
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate (void) fclose(fin);
7370Sstevel@tonic-gate (void) fclose(fout);
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate done:
7400Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oldmask, NULL);
7410Sstevel@tonic-gate return (ret);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate static void
makeup_msg(char ** pmsg)7450Sstevel@tonic-gate makeup_msg(char **pmsg)
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate char buf[NL_TEXTMAX];
7480Sstevel@tonic-gate char *msg;
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate msg = *pmsg;
751*4933Smuffin buf[0] = '\0';
7520Sstevel@tonic-gate
753*4933Smuffin if (IsActiveMode(TripleMode) && strchr(msg, '%') == NULL) {
7540Sstevel@tonic-gate /* there is no '%' in message. */
7550Sstevel@tonic-gate int len = strlen(msg);
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate if (msg[len-2] == '\\' && msg[len-1] == 'n') {
7580Sstevel@tonic-gate msg[len-2] = '\0';
759*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
760*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
761*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
762*4933Smuffin (void) strlcat(buf, "\\n", sizeof (buf));
7630Sstevel@tonic-gate } else {
764*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
765*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
766*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate free(msg);
769*4933Smuffin *pmsg = ustrdup(buf);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate msg = *pmsg;
773*4933Smuffin buf[0] = '\0';
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate if (IsActiveMode(PrefixMode)) {
776*4933Smuffin (void) strlcat(buf, premsg, sizeof (buf));
777*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
7780Sstevel@tonic-gate free(msg);
779*4933Smuffin *pmsg = ustrdup(buf);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate msg = *pmsg;
783*4933Smuffin buf[0] = '\0';
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate if (IsActiveMode(SuffixMode)) {
7860Sstevel@tonic-gate int len = strlen(msg);
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate if (msg[len-2] == '\\' && msg[len-1] == 'n') {
7890Sstevel@tonic-gate msg[len-2] = '\0';
790*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
791*4933Smuffin (void) strlcat(buf, sufmsg, sizeof (buf));
792*4933Smuffin (void) strlcat(buf, "\\n", sizeof (buf));
7930Sstevel@tonic-gate } else {
794*4933Smuffin (void) strlcat(buf, msg, sizeof (buf));
795*4933Smuffin (void) strlcat(buf, sufmsg, sizeof (buf));
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate free(msg);
798*4933Smuffin *pmsg = ustrdup(buf);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate void
prg_err(char * fmt,...)8030Sstevel@tonic-gate prg_err(char *fmt, ...)
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate va_list ap;
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate va_start(ap, fmt);
8080Sstevel@tonic-gate
809*4933Smuffin (void) fprintf(stderr, "%s: ", program);
810*4933Smuffin /* LINTED: E_SEC_PRINTF_VAR_FMT */
811*4933Smuffin (void) vfprintf(stderr, fmt, ap);
812*4933Smuffin (void) fprintf(stderr, "\n");
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate va_end(ap);
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate void
src_err(char * file,int line,char * fmt,...)8180Sstevel@tonic-gate src_err(char *file, int line, char *fmt, ...)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate va_list ap;
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate if (suppress_error == TRUE) {
8230Sstevel@tonic-gate return;
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate va_start(ap, fmt);
8270Sstevel@tonic-gate
828*4933Smuffin (void) fprintf(stderr, gettext("\"%s\", line %d: "), file, line);
829*4933Smuffin /* LINTED: E_SEC_PRINTF_VAR_FMT */
830*4933Smuffin (void) vfprintf(stderr, fmt, ap);
831*4933Smuffin (void) fprintf(stderr, "\n");
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate va_end(ap);
8340Sstevel@tonic-gate }
835