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
5a194faf8Srie * Common Development and Distribution License (the "License").
6a194faf8Srie * 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 /*
22b23a7923SAli Bahrami * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate *
247c478bd9Sstevel@tonic-gate * sgsmsg generates several message files from an input template file. Messages
257c478bd9Sstevel@tonic-gate * are constructed for use with gettext(3i) - the default - or catgets(3c). The
267c478bd9Sstevel@tonic-gate * files generate are:
277c478bd9Sstevel@tonic-gate *
287c478bd9Sstevel@tonic-gate * msg.h a header file containing definitions for each message. The -h
297c478bd9Sstevel@tonic-gate * option triggers the creation of these definitions and specifies
307c478bd9Sstevel@tonic-gate * the name to use.
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * msg.c a data array of message strings. The msg.h definitions are
337c478bd9Sstevel@tonic-gate * offsets into this array. The -d option triggers the creation of
347c478bd9Sstevel@tonic-gate * these definitions and specifies the name to use.
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * messages a message file suitable for catgets(3c) or gettext(3i) use. The
377c478bd9Sstevel@tonic-gate * -m option triggers this output and specifies the filename to be
387c478bd9Sstevel@tonic-gate * used.
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * The template file is processed based on the first character of each line:
417c478bd9Sstevel@tonic-gate *
427c478bd9Sstevel@tonic-gate * # or $ entries are copied (as is) to the message file (messages).
437c478bd9Sstevel@tonic-gate *
447c478bd9Sstevel@tonic-gate * @ token(s) entries are translated. Two translations are possible dependent
457c478bd9Sstevel@tonic-gate * on whether one or more tokens are supplied:
467c478bd9Sstevel@tonic-gate *
477c478bd9Sstevel@tonic-gate * A single token is interpreted as one of two reserved message
487c478bd9Sstevel@tonic-gate * output indicators, or a message identifier. The reserved output
497c478bd9Sstevel@tonic-gate * indicator _START_ enables output to the message file - Note that
507c478bd9Sstevel@tonic-gate * the occurance of any other @ token will also enable message
517c478bd9Sstevel@tonic-gate * output. The reserved output indicator _END_ disables output to
527c478bd9Sstevel@tonic-gate * the message file. The use of these two indicators provides for
537c478bd9Sstevel@tonic-gate * only those message strings that require translation to be output
547c478bd9Sstevel@tonic-gate * to the message file.
557c478bd9Sstevel@tonic-gate *
567c478bd9Sstevel@tonic-gate * Besides the reserved output indicators, a single token is taken
577c478bd9Sstevel@tonic-gate * to be a message identifier which will be subsituted for a
587c478bd9Sstevel@tonic-gate * `setid' for catgets(3c) output, or a `domain' name for
597c478bd9Sstevel@tonic-gate * gettext(3i) output. This value is determine by substituting the
607c478bd9Sstevel@tonic-gate * token for the associated definition found in the message
617c478bd9Sstevel@tonic-gate * identifier file (specified with the -i option).
627c478bd9Sstevel@tonic-gate *
637c478bd9Sstevel@tonic-gate * Multiple tokens are taken to be a message definition followed by
647c478bd9Sstevel@tonic-gate * the associated message string. The message string is copied to
657c478bd9Sstevel@tonic-gate * the data array being built in msg.c. The index into this array
667c478bd9Sstevel@tonic-gate * becomes the `message' identifier created in the msg.h file.
677c478bd9Sstevel@tonic-gate */
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate #include <fcntl.h>
707c478bd9Sstevel@tonic-gate #include <stdlib.h>
717c478bd9Sstevel@tonic-gate #include <stdio.h>
727c478bd9Sstevel@tonic-gate #include <unistd.h>
737c478bd9Sstevel@tonic-gate #include <limits.h>
747c478bd9Sstevel@tonic-gate #include <string.h>
757c478bd9Sstevel@tonic-gate #include <ctype.h>
767c478bd9Sstevel@tonic-gate #include <errno.h>
777c478bd9Sstevel@tonic-gate #include <sys/param.h>
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate #include <sgs.h>
80a194faf8Srie #include <_string_table.h>
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * Define any error message strings.
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate static const char
867c478bd9Sstevel@tonic-gate * Errmsg_malt = "sgsmsg: file %s: line %d: malformed input "
877c478bd9Sstevel@tonic-gate "at line\n",
887c478bd9Sstevel@tonic-gate * Errmsg_nmem = "sgsmsg: memory allocation failed: %s\n",
897c478bd9Sstevel@tonic-gate * Errmsg_opne = "sgsmsg: file %s: open failed: %s\n",
907c478bd9Sstevel@tonic-gate * Errmsg_wrte = "sgsmsg: file %s: write failed: %s\n",
917c478bd9Sstevel@tonic-gate * Errmsg_read = "sgsmsg: file %s: read failed %s\n",
927c478bd9Sstevel@tonic-gate * Errmsg_stnw = "sgsmsg: st_new(): failed: %s\n",
937c478bd9Sstevel@tonic-gate * Errmsg_stin = "sgsmsg: Str_tbl insert failed: %s\n",
947c478bd9Sstevel@tonic-gate * Errmsg_mnfn = "sgsmsg: message not found in Str_tbl: %s\n",
957c478bd9Sstevel@tonic-gate * Errmsg_use = "usage: sgsmsg [-clv] [-d mesgdata] [-h mesgdefs] "
967c478bd9Sstevel@tonic-gate "[-m messages] [-n name] [-i mesgident] file ...\n";
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * Define all output filenames and associated descriptors.
1007c478bd9Sstevel@tonic-gate */
1017c478bd9Sstevel@tonic-gate static FILE *fddefs, *fddata, *fdmsgs, *fdmids, *fddesc;
1027c478bd9Sstevel@tonic-gate static char *fldefs, *fldata, *flmsgs, *flmids, *fldesc;
1037c478bd9Sstevel@tonic-gate static FILE *fdlint;
1047c478bd9Sstevel@tonic-gate static char fllint[MAXPATHLEN];
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate static uint_t vflag; /* verbose flag */
1077c478bd9Sstevel@tonic-gate static Str_tbl *stp; /* string table */
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * Define any default strings.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate static const char
1137c478bd9Sstevel@tonic-gate *nmlint = "/tmp/sgsmsg.lint",
1147c478bd9Sstevel@tonic-gate *interface = "sgs_msg",
1157c478bd9Sstevel@tonic-gate *start = "_START_",
1167c478bd9Sstevel@tonic-gate *end = "_END_";
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate * Define any default flags and data items.
1207c478bd9Sstevel@tonic-gate */
1217c478bd9Sstevel@tonic-gate static int cflag = 0, lflag = 0, prtmsgs = 0, line, ptr = 1, msgid = 0;
1227c478bd9Sstevel@tonic-gate static char *mesgid = 0, *setid = 0, *domain = 0;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate typedef struct msg_string {
1257c478bd9Sstevel@tonic-gate char *ms_defn;
1267c478bd9Sstevel@tonic-gate char *ms_message;
1277c478bd9Sstevel@tonic-gate struct msg_string *ms_next;
1287c478bd9Sstevel@tonic-gate } msg_string;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate static msg_string *msg_head;
1317c478bd9Sstevel@tonic-gate static msg_string *msg_tail;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate * message_append() is responsible for both inserting strings into
1357c478bd9Sstevel@tonic-gate * the master Str_tbl as well as maintaining a list of the
1367c478bd9Sstevel@tonic-gate * DEFINITIONS associated with each string.
1377c478bd9Sstevel@tonic-gate *
1387c478bd9Sstevel@tonic-gate * The list of strings is traversed at the end once the full
1397c478bd9Sstevel@tonic-gate * Str_tbl has been constructed - and string offsets can be
1407c478bd9Sstevel@tonic-gate * assigned.
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate static void
message_append(const char * defn,const char * message)1437c478bd9Sstevel@tonic-gate message_append(const char *defn, const char *message)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate msg_string *msg;
1467c478bd9Sstevel@tonic-gate if ((msg = calloc(sizeof (msg_string), 1)) == 0) {
1477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1487c478bd9Sstevel@tonic-gate exit(1);
1497c478bd9Sstevel@tonic-gate }
150a194faf8Srie
1517c478bd9Sstevel@tonic-gate /*
152a194faf8Srie * Initialize the string table.
1537c478bd9Sstevel@tonic-gate */
154a194faf8Srie if ((stp == 0) && ((stp = st_new(FLG_STNEW_COMPRESS)) == NULL)) {
1557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_stnw, strerror(errno));
1567c478bd9Sstevel@tonic-gate exit(1);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate if ((msg->ms_defn = strdup(defn)) == 0) {
1617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1627c478bd9Sstevel@tonic-gate exit(1);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate if ((msg->ms_message = strdup(message)) == 0) {
1657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1667c478bd9Sstevel@tonic-gate exit(1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate if (st_insert(stp, msg->ms_message) == -1) {
1707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_stin,
1717c478bd9Sstevel@tonic-gate message);
1727c478bd9Sstevel@tonic-gate exit(1);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate if (msg_head == 0) {
1767c478bd9Sstevel@tonic-gate msg_head = msg_tail = msg;
1777c478bd9Sstevel@tonic-gate return;
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate msg_tail->ms_next = msg;
1807c478bd9Sstevel@tonic-gate msg_tail = msg;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate * Initialize a setid value. Given a setid definition determine its numeric
1857c478bd9Sstevel@tonic-gate * value from the specified message identifier file (specified with the -i
1867c478bd9Sstevel@tonic-gate * option). Return a pointer to the numeric string.
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate static int
getmesgid(char * id)1897c478bd9Sstevel@tonic-gate getmesgid(char *id)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate char *buffer, *token, *_mesgid = 0, *_setid = 0, *_domain = 0;
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * If we're being asked to interpret a message id but the user didn't
1957c478bd9Sstevel@tonic-gate * provide the required message identifier file (-i option) we're in
1967c478bd9Sstevel@tonic-gate * trouble.
1977c478bd9Sstevel@tonic-gate */
1987c478bd9Sstevel@tonic-gate if (flmids == 0) {
1997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
2007c478bd9Sstevel@tonic-gate "unable to process mesgid\n\t"
2017c478bd9Sstevel@tonic-gate "no message identifier file specified "
2027c478bd9Sstevel@tonic-gate "(see -i option)\n", fldesc, line, id);
2037c478bd9Sstevel@tonic-gate return (1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate if ((buffer = malloc(LINE_MAX)) == 0) {
2077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
2087c478bd9Sstevel@tonic-gate return (1);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate * Read the message identifier file and locate the required mesgid.
2137c478bd9Sstevel@tonic-gate */
2147c478bd9Sstevel@tonic-gate rewind(fdmids);
2157c478bd9Sstevel@tonic-gate while (fgets(buffer, LINE_MAX, fdmids) != NULL) {
2167c478bd9Sstevel@tonic-gate if ((token = strstr(buffer, id)) == NULL)
2177c478bd9Sstevel@tonic-gate continue;
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate * Establish individual strings for the mesgid, setid and domain
2217c478bd9Sstevel@tonic-gate * values.
2227c478bd9Sstevel@tonic-gate */
2237c478bd9Sstevel@tonic-gate _mesgid = token;
2247c478bd9Sstevel@tonic-gate while (!(isspace(*token)))
2257c478bd9Sstevel@tonic-gate token++;
2267c478bd9Sstevel@tonic-gate *token++ = 0;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate while (isspace(*token))
2297c478bd9Sstevel@tonic-gate token++;
2307c478bd9Sstevel@tonic-gate _setid = token;
2317c478bd9Sstevel@tonic-gate while (!(isspace(*token)))
2327c478bd9Sstevel@tonic-gate token++;
2337c478bd9Sstevel@tonic-gate *token++ = 0;
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate while (isspace(*token))
2367c478bd9Sstevel@tonic-gate token++;
2377c478bd9Sstevel@tonic-gate _domain = token;
2387c478bd9Sstevel@tonic-gate while (!(isspace(*token)))
2397c478bd9Sstevel@tonic-gate token++;
2407c478bd9Sstevel@tonic-gate *token = 0;
2417c478bd9Sstevel@tonic-gate break;
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate * Did we find a match?
2467c478bd9Sstevel@tonic-gate */
2477c478bd9Sstevel@tonic-gate if ((_mesgid == 0) || (_setid == 0) || (_domain == 0)) {
2487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
2497c478bd9Sstevel@tonic-gate "unable to process mesgid\n\t"
2507c478bd9Sstevel@tonic-gate "identifier does not exist in file %s\n",
2517c478bd9Sstevel@tonic-gate fldesc, line, id, flmids);
2527c478bd9Sstevel@tonic-gate return (1);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * Have we been here before?
2577c478bd9Sstevel@tonic-gate */
2587c478bd9Sstevel@tonic-gate if (mesgid) {
2597c478bd9Sstevel@tonic-gate if (cflag == 1) {
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate * If we're being asked to process more than one mesgid
2627c478bd9Sstevel@tonic-gate * warn the user that only one mesgid can be used for
2637c478bd9Sstevel@tonic-gate * the catgets(3c) call.
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "sgsmsg: file %s: line %d: "
2667c478bd9Sstevel@tonic-gate "setid %s: warning: multiple mesgids "
2677c478bd9Sstevel@tonic-gate "encountered\n\t"
2687c478bd9Sstevel@tonic-gate "last setting used in messaging code\n",
2697c478bd9Sstevel@tonic-gate fldesc, line, id);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate mesgid = _mesgid;
2747c478bd9Sstevel@tonic-gate setid = _setid;
2757c478bd9Sstevel@tonic-gate domain = _domain;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate * Generate the message file output (insure output flag is enabled).
2797c478bd9Sstevel@tonic-gate */
2807c478bd9Sstevel@tonic-gate if (prtmsgs != -1)
2817c478bd9Sstevel@tonic-gate prtmsgs = 1;
2827c478bd9Sstevel@tonic-gate if (fdmsgs && (prtmsgs == 1)) {
2837c478bd9Sstevel@tonic-gate if (cflag == 1) {
2847c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, "$quote \"\n$set %s\n",
2857c478bd9Sstevel@tonic-gate setid) < 0) {
2867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, flmsgs,
2877c478bd9Sstevel@tonic-gate strerror(errno));
2887c478bd9Sstevel@tonic-gate return (1);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate } else {
2917c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, "domain\t\"%s\"\n", domain) < 0) {
2927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, flmsgs,
2937c478bd9Sstevel@tonic-gate strerror(errno));
2947c478bd9Sstevel@tonic-gate return (1);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate * For catgets(3c) output generate a setid definition in the message
3017c478bd9Sstevel@tonic-gate * definition file.
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate if (fddefs && (cflag == 1) &&
3047c478bd9Sstevel@tonic-gate (fprintf(fddefs, "#define\t%s\t%s\n\n", mesgid, setid) < 0)) {
3057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3067c478bd9Sstevel@tonic-gate return (1);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate return (0);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate * Dump contents of String Table to standard out
3147c478bd9Sstevel@tonic-gate */
3157c478bd9Sstevel@tonic-gate static void
dump_stringtab(Str_tbl * stp)3167c478bd9Sstevel@tonic-gate dump_stringtab(Str_tbl *stp)
3177c478bd9Sstevel@tonic-gate {
318a194faf8Srie uint_t cnt;
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
321cce0e03bSab196087 (void) printf("string table full size: %ld: uncompressed\n",
322a194faf8Srie stp->st_fullstrsize);
3237c478bd9Sstevel@tonic-gate return;
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
326cce0e03bSab196087 (void) printf("string table full size: %ld compressed down to: %ld\n\n",
327a194faf8Srie stp->st_fullstrsize, stp->st_strsize);
328a194faf8Srie (void) printf("string table compression information [%d buckets]:\n",
329a194faf8Srie stp->st_hbckcnt);
3307c478bd9Sstevel@tonic-gate
331a194faf8Srie for (cnt = 0; cnt < stp->st_hbckcnt; cnt++) {
332a194faf8Srie Str_hash *sthash = stp->st_hashbcks[cnt];
333a194faf8Srie
334a194faf8Srie if (sthash == 0)
335a194faf8Srie continue;
336a194faf8Srie
337a194faf8Srie (void) printf(" bucket: [%d]\n", cnt);
338a194faf8Srie
339a194faf8Srie while (sthash) {
340cce0e03bSab196087 size_t stroff = sthash->hi_mstr->sm_strlen -
341a194faf8Srie sthash->hi_strlen;
342a194faf8Srie
3437c478bd9Sstevel@tonic-gate if (stroff == 0) {
344cce0e03bSab196087 (void) printf(" [%ld]: '%s' <master>\n",
345a194faf8Srie sthash->hi_refcnt, sthash->hi_mstr->sm_str);
3467c478bd9Sstevel@tonic-gate } else {
347cce0e03bSab196087 (void) printf(" [%ld]: '%s' <suffix of: "
348a194faf8Srie "'%s'>\n", sthash->hi_refcnt,
349a194faf8Srie &sthash->hi_mstr->sm_str[stroff],
350a194faf8Srie sthash->hi_mstr->sm_str);
351a194faf8Srie }
352a194faf8Srie sthash = sthash->hi_next;
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate }
356a194faf8Srie
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate * Initialize the message definition header file stream.
3597c478bd9Sstevel@tonic-gate */
3607c478bd9Sstevel@tonic-gate static int
init_defs(void)3617c478bd9Sstevel@tonic-gate init_defs(void)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate static char guard[FILENAME_MAX + 6];
3647c478bd9Sstevel@tonic-gate char *optr;
3657c478bd9Sstevel@tonic-gate const char *iptr, *_ptr;
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate /*
3687c478bd9Sstevel@tonic-gate * Establish a header guard name using the files basename.
3697c478bd9Sstevel@tonic-gate */
3707c478bd9Sstevel@tonic-gate for (iptr = 0, _ptr = fldefs; _ptr && (*_ptr != '\0'); _ptr++) {
3717c478bd9Sstevel@tonic-gate if (*_ptr == '/')
3727c478bd9Sstevel@tonic-gate iptr = _ptr + 1;
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate if (iptr == 0)
3757c478bd9Sstevel@tonic-gate iptr = fldefs;
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate optr = guard;
3787c478bd9Sstevel@tonic-gate for (*optr++ = '_'; iptr && (*iptr != '\0'); iptr++, optr++) {
3797c478bd9Sstevel@tonic-gate if (*iptr == '.') {
3807c478bd9Sstevel@tonic-gate *optr++ = '_';
3817c478bd9Sstevel@tonic-gate *optr++ = 'D';
3827c478bd9Sstevel@tonic-gate *optr++ = 'O';
3837c478bd9Sstevel@tonic-gate *optr++ = 'T';
3847c478bd9Sstevel@tonic-gate *optr = '_';
3857c478bd9Sstevel@tonic-gate } else
3867c478bd9Sstevel@tonic-gate *optr = toupper(*iptr);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "#ifndef\t%s\n#define\t%s\n\n", guard, guard) < 0) {
3907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3917c478bd9Sstevel@tonic-gate return (1);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate
3944f680cc6SAli Bahrami if (fprintf(fddefs, "#include <sgsmsg.h>\t/* Msg typedef */\n\n") < 0) {
3954f680cc6SAli Bahrami (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3964f680cc6SAli Bahrami return (1);
3974f680cc6SAli Bahrami }
3984f680cc6SAli Bahrami
3997c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "#ifndef\t__lint\n\n") < 0) {
4007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4017c478bd9Sstevel@tonic-gate return (1);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate /*
4054f680cc6SAli Bahrami * The MSG_SGS_ARRAY_NAME macro supplies a generic way to
4064f680cc6SAli Bahrami * reference the string table regardless of its name.
4077c478bd9Sstevel@tonic-gate */
4084f680cc6SAli Bahrami if (fprintf(fddefs, "#define\tMSG_SGS_LOCAL_ARRAY\t__%s\n\n",
4094f680cc6SAli Bahrami interface) < 0) {
4107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4117c478bd9Sstevel@tonic-gate return (1);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate * If the associated data array is global define a prototype.
4167c478bd9Sstevel@tonic-gate * Define a macro to access the array elements.
4177c478bd9Sstevel@tonic-gate */
4187c478bd9Sstevel@tonic-gate if (lflag == 0) {
4197c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
4207c478bd9Sstevel@tonic-gate interface) < 0) {
4217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs,
4227c478bd9Sstevel@tonic-gate strerror(errno));
4237c478bd9Sstevel@tonic-gate return (1);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate }
4264f680cc6SAli Bahrami if (fprintf(fddefs,
4274f680cc6SAli Bahrami "#define\tMSG_ORIG_STRTAB(_x, _s)\t&_s[_x]\n\n") < 0) {
4284f680cc6SAli Bahrami (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4294f680cc6SAli Bahrami return (1);
4304f680cc6SAli Bahrami }
4314f680cc6SAli Bahrami if (fprintf(fddefs,
4324f680cc6SAli Bahrami "#define\tMSG_ORIG(x)\tMSG_ORIG_STRTAB(x, __%s)\n\n",
4337c478bd9Sstevel@tonic-gate interface) < 0) {
4347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4357c478bd9Sstevel@tonic-gate return (1);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate * Generate a prototype to access the associated data array.
4407c478bd9Sstevel@tonic-gate */
4417c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
4427c478bd9Sstevel@tonic-gate interface) < 0) {
4437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4447c478bd9Sstevel@tonic-gate return (1);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "#define\tMSG_INTL(x)\t_%s(x)\n\n",
4477c478bd9Sstevel@tonic-gate interface) < 0) {
4487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4497c478bd9Sstevel@tonic-gate return (1);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate return (0);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate * Finish the message definition header file.
4587c478bd9Sstevel@tonic-gate */
4597c478bd9Sstevel@tonic-gate static int
fini_defs(void)4607c478bd9Sstevel@tonic-gate fini_defs(void)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "\n#else\t/* __lint */\n\n") < 0) {
4637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4647c478bd9Sstevel@tonic-gate return (1);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate
4674f680cc6SAli Bahrami if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
4684f680cc6SAli Bahrami interface) < 0) {
4697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4707c478bd9Sstevel@tonic-gate return (1);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
4734f680cc6SAli Bahrami if (fprintf(fddefs, "#ifndef MSG_SGS_LOCAL_ARRAY\n"
4744f680cc6SAli Bahrami "#define\tMSG_SGS_LOCAL_ARRAY\t\"\"\n#endif\n\n") < 0) {
4757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4767c478bd9Sstevel@tonic-gate return (1);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate if (lflag == 0) {
4807c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
4817c478bd9Sstevel@tonic-gate interface) < 0) {
4827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs,
4837c478bd9Sstevel@tonic-gate strerror(errno));
4847c478bd9Sstevel@tonic-gate return (1);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate if (fprintf(fddefs,
4894f680cc6SAli Bahrami "#define MSG_ORIG_STRTAB(_x, _s)\t_x\n"
4907c478bd9Sstevel@tonic-gate "#define MSG_ORIG(x)\tx\n#define MSG_INTL(x)\tx\n") < 0) {
4917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4927c478bd9Sstevel@tonic-gate return (1);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate /*
4964f680cc6SAli Bahrami * Provide a way to get the array and function declarations above
4974f680cc6SAli Bahrami * without also getting the actual messages. This is useful in
4984f680cc6SAli Bahrami * our lintsup.c files that include more than one message header.
4994f680cc6SAli Bahrami * lintsup doesn't need the actual messages, and this prevents
5004f680cc6SAli Bahrami * macro name collisions.
5014f680cc6SAli Bahrami */
5024f680cc6SAli Bahrami if (fprintf(fddefs, "\n#ifndef LINTSUP_SUPPRESS_STRINGS\n") < 0) {
5034f680cc6SAli Bahrami (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5044f680cc6SAli Bahrami return (1);
5054f680cc6SAli Bahrami }
5064f680cc6SAli Bahrami
5074f680cc6SAli Bahrami /*
5087c478bd9Sstevel@tonic-gate * Copy the temporary lint defs file into the new header.
5097c478bd9Sstevel@tonic-gate */
5107c478bd9Sstevel@tonic-gate if (fdlint) {
5117c478bd9Sstevel@tonic-gate long size;
5127c478bd9Sstevel@tonic-gate char *buf;
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate size = ftell(fdlint);
5157c478bd9Sstevel@tonic-gate (void) rewind(fdlint);
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate if ((buf = malloc(size)) == 0) {
5187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
5197c478bd9Sstevel@tonic-gate return (1);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate if (fread(buf, size, 1, fdlint) == 0) {
5227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_read, fllint,
5237c478bd9Sstevel@tonic-gate strerror(errno));
5247c478bd9Sstevel@tonic-gate return (1);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate if (fwrite(buf, size, 1, fddefs) == 0) {
5277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs,
5287c478bd9Sstevel@tonic-gate strerror(errno));
5297c478bd9Sstevel@tonic-gate return (1);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate (void) free(buf);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate
5344f680cc6SAli Bahrami if (fprintf(fddefs, "\n#endif\t/* LINTSUP_SUPPRESS_STRINGS */\n") < 0) {
5354f680cc6SAli Bahrami (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5364f680cc6SAli Bahrami return (1);
5374f680cc6SAli Bahrami }
5384f680cc6SAli Bahrami
5397c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "\n#endif\t/* __lint */\n") < 0) {
5407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5417c478bd9Sstevel@tonic-gate return (1);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate if (fprintf(fddefs, "\n#endif\n") < 0) {
5457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5467c478bd9Sstevel@tonic-gate return (1);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate return (0);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate /*
5537c478bd9Sstevel@tonic-gate * The entire messaging file has been scanned - and all strings have been
5547c478bd9Sstevel@tonic-gate * inserted into the string_table. We can now walk the message queue
5557c478bd9Sstevel@tonic-gate * and create the '#define <DEFN>' for each string - with the strings
5567c478bd9Sstevel@tonic-gate * assigned offset into the string_table.
5577c478bd9Sstevel@tonic-gate */
5587c478bd9Sstevel@tonic-gate static int
output_defs(void)5597c478bd9Sstevel@tonic-gate output_defs(void)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate msg_string *msg;
562cce0e03bSab196087 size_t stbufsize;
5637c478bd9Sstevel@tonic-gate char *stbuf;
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate stbufsize = st_getstrtab_sz(stp);
5667c478bd9Sstevel@tonic-gate if ((stbuf = malloc(stbufsize)) == 0) {
5677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
5687c478bd9Sstevel@tonic-gate exit(1);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate (void) st_setstrbuf(stp, stbuf, stbufsize);
5717c478bd9Sstevel@tonic-gate for (msg = msg_head; msg; msg = msg->ms_next) {
572cce0e03bSab196087 size_t stoff;
5737c478bd9Sstevel@tonic-gate if ((st_setstring(stp, msg->ms_message, &stoff)) == -1) {
5747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_mnfn, msg->ms_message);
5757c478bd9Sstevel@tonic-gate return (1);
5767c478bd9Sstevel@tonic-gate }
577cce0e03bSab196087 if (fprintf(fddefs, "\n#define\t%s\t%ld\n",
5787c478bd9Sstevel@tonic-gate msg->ms_defn, stoff) < 0) {
5797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
5807c478bd9Sstevel@tonic-gate fldefs, strerror(errno));
5817c478bd9Sstevel@tonic-gate return (1);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate if (fddefs && fprintf(fddefs, "#define\t%s_SIZE\t%d\n",
5847c478bd9Sstevel@tonic-gate msg->ms_defn, strlen(msg->ms_message)) < 0) {
5857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
5867c478bd9Sstevel@tonic-gate fldefs, strerror(errno));
5877c478bd9Sstevel@tonic-gate return (1);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate return (0);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate * Finish off the data structure definition.
5967c478bd9Sstevel@tonic-gate */
5977c478bd9Sstevel@tonic-gate static int
output_data(void)5987c478bd9Sstevel@tonic-gate output_data(void)
5997c478bd9Sstevel@tonic-gate {
600cce0e03bSab196087 size_t stbufsize;
601cce0e03bSab196087 size_t ndx;
602cce0e03bSab196087 size_t column = 1;
6037c478bd9Sstevel@tonic-gate const char *stbuf;
6047c478bd9Sstevel@tonic-gate const char *fmtstr;
6057c478bd9Sstevel@tonic-gate
6067c478bd9Sstevel@tonic-gate stbufsize = st_getstrtab_sz(stp);
6077c478bd9Sstevel@tonic-gate stbuf = st_getstrbuf(stp);
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate assert(stbuf);
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate * Determine from the local flag whether the data declaration should
6137c478bd9Sstevel@tonic-gate * be static.
6147c478bd9Sstevel@tonic-gate */
6157c478bd9Sstevel@tonic-gate if (lflag)
6167c478bd9Sstevel@tonic-gate fmtstr = (const char *)"static const";
6177c478bd9Sstevel@tonic-gate else
6187c478bd9Sstevel@tonic-gate fmtstr = (const char *)"const";
6197c478bd9Sstevel@tonic-gate
620cce0e03bSab196087 if (fprintf(fddata, "\n%s char __%s[%ld] = { ",
6217c478bd9Sstevel@tonic-gate fmtstr, interface, stbufsize) < 0) {
6227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
6237c478bd9Sstevel@tonic-gate return (1);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate
6267c478bd9Sstevel@tonic-gate for (ndx = 0; ndx < (stbufsize - 1); ndx++) {
6277c478bd9Sstevel@tonic-gate if (column == 1) {
6287c478bd9Sstevel@tonic-gate if (fddata && fprintf(fddata,
629cce0e03bSab196087 "\n/* %4ld */ 0x%.2x,", ndx,
6307c478bd9Sstevel@tonic-gate (unsigned char)stbuf[ndx]) < 0) {
6317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
6327c478bd9Sstevel@tonic-gate fldata, strerror(errno));
6337c478bd9Sstevel@tonic-gate return (1);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate } else {
6367c478bd9Sstevel@tonic-gate if (fddata && fprintf(fddata, " 0x%.2x,",
6377c478bd9Sstevel@tonic-gate (unsigned char)stbuf[ndx]) < 0) {
6387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
6397c478bd9Sstevel@tonic-gate fldata, strerror(errno));
6407c478bd9Sstevel@tonic-gate return (1);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate if (column++ == 10)
6457c478bd9Sstevel@tonic-gate column = 1;
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate if (column == 1)
6497c478bd9Sstevel@tonic-gate fmtstr = "\n\t0x%.2x };\n";
6507c478bd9Sstevel@tonic-gate else
6517c478bd9Sstevel@tonic-gate fmtstr = " 0x%.2x };\n";
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate if (fprintf(fddata, fmtstr, (unsigned char)stbuf[stbufsize - 1]) < 0) {
6547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
6557c478bd9Sstevel@tonic-gate return (1);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate return (0);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate static int
file()6627c478bd9Sstevel@tonic-gate file()
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate char buffer[LINE_MAX], * token;
6657c478bd9Sstevel@tonic-gate uint_t bufsize;
6667c478bd9Sstevel@tonic-gate char *token_buffer;
6677c478bd9Sstevel@tonic-gate int escape = 0;
668*eae164ebSRichard Lowe int len = 0;
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate if ((token_buffer = malloc(LINE_MAX)) == 0) {
6717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
6727c478bd9Sstevel@tonic-gate return (1);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate bufsize = LINE_MAX;
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate line = 1;
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate while ((token = fgets(buffer, LINE_MAX, fddesc)) != NULL) {
6797c478bd9Sstevel@tonic-gate char defn[PATH_MAX], * _defn, * str;
6807c478bd9Sstevel@tonic-gate
6817c478bd9Sstevel@tonic-gate switch (*token) {
6827c478bd9Sstevel@tonic-gate case '#':
6837c478bd9Sstevel@tonic-gate case '$':
6847c478bd9Sstevel@tonic-gate if (escape) {
6857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_malt, fldesc,
6867c478bd9Sstevel@tonic-gate line);
6877c478bd9Sstevel@tonic-gate return (1);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate * If a msgid has been output a msgstr must follow
6927c478bd9Sstevel@tonic-gate * before we digest the new token. A msgid is only set
6937c478bd9Sstevel@tonic-gate * if fdmsgs is in use.
6947c478bd9Sstevel@tonic-gate */
6957c478bd9Sstevel@tonic-gate if (msgid) {
6967c478bd9Sstevel@tonic-gate msgid = 0;
6977c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
6987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
6997c478bd9Sstevel@tonic-gate flmsgs, strerror(errno));
7007c478bd9Sstevel@tonic-gate return (1);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate * Pass lines directly through to the output message
7067c478bd9Sstevel@tonic-gate * file.
7077c478bd9Sstevel@tonic-gate */
7087c478bd9Sstevel@tonic-gate if (fdmsgs && (prtmsgs == 1)) {
7097c478bd9Sstevel@tonic-gate char comment;
7107c478bd9Sstevel@tonic-gate
7117c478bd9Sstevel@tonic-gate if (cflag == 0)
7127c478bd9Sstevel@tonic-gate comment = '#';
7137c478bd9Sstevel@tonic-gate else
7147c478bd9Sstevel@tonic-gate comment = '$';
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, "%c%s", comment,
7177c478bd9Sstevel@tonic-gate ++token) < 0) {
7187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
7197c478bd9Sstevel@tonic-gate flmsgs, strerror(errno));
7207c478bd9Sstevel@tonic-gate return (1);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate break;
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate case '@':
7267c478bd9Sstevel@tonic-gate if (escape) {
7277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_malt, fldesc,
7287c478bd9Sstevel@tonic-gate line);
7297c478bd9Sstevel@tonic-gate return (1);
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate
7327c478bd9Sstevel@tonic-gate /*
7337c478bd9Sstevel@tonic-gate * If a msgid has been output a msgstr must follow
7347c478bd9Sstevel@tonic-gate * before we digest the new token.
7357c478bd9Sstevel@tonic-gate */
7367c478bd9Sstevel@tonic-gate if (msgid) {
7377c478bd9Sstevel@tonic-gate msgid = 0;
7387c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
7397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
7407c478bd9Sstevel@tonic-gate flmsgs, strerror(errno));
7417c478bd9Sstevel@tonic-gate return (1);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate * Determine whether we have one or more tokens.
7477c478bd9Sstevel@tonic-gate */
7487c478bd9Sstevel@tonic-gate token++;
7497c478bd9Sstevel@tonic-gate while (isspace(*token)) /* rid any whitespace */
7507c478bd9Sstevel@tonic-gate token++;
7517c478bd9Sstevel@tonic-gate _defn = token; /* definition start */
7527c478bd9Sstevel@tonic-gate while (!(isspace(*token)))
7537c478bd9Sstevel@tonic-gate token++;
7547c478bd9Sstevel@tonic-gate *token++ = 0;
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate while (isspace(*token)) /* rid any whitespace */
7577c478bd9Sstevel@tonic-gate token++;
7587c478bd9Sstevel@tonic-gate
7597c478bd9Sstevel@tonic-gate /*
7607c478bd9Sstevel@tonic-gate * Determine whether the single token is one of the
7617c478bd9Sstevel@tonic-gate * reserved message output delimiters otherwise
7627c478bd9Sstevel@tonic-gate * translate it as a message identifier.
7637c478bd9Sstevel@tonic-gate */
7647c478bd9Sstevel@tonic-gate if (*token == 0) {
7657c478bd9Sstevel@tonic-gate if (strcmp(_defn, start) == 0)
7667c478bd9Sstevel@tonic-gate prtmsgs = 1;
7677c478bd9Sstevel@tonic-gate else if (strcmp(_defn, end) == 0)
7687c478bd9Sstevel@tonic-gate prtmsgs = -1;
7697c478bd9Sstevel@tonic-gate else if (getmesgid(_defn) == 1)
7707c478bd9Sstevel@tonic-gate return (1);
7717c478bd9Sstevel@tonic-gate break;
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate * Multiple tokens are translated by taking the first
7767c478bd9Sstevel@tonic-gate * token as the message definition, and the rest of the
7777c478bd9Sstevel@tonic-gate * line as the message itself. A message line ending
7787c478bd9Sstevel@tonic-gate * with an escape ('\') is expected to be continued on
7797c478bd9Sstevel@tonic-gate * the next line.
7807c478bd9Sstevel@tonic-gate */
7817c478bd9Sstevel@tonic-gate if (prtmsgs != -1)
7827c478bd9Sstevel@tonic-gate prtmsgs = 1;
7837c478bd9Sstevel@tonic-gate if (fdmsgs && (prtmsgs == 1)) {
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate * For catgets(3c) make sure a message
7867c478bd9Sstevel@tonic-gate * identifier has been established (this is
7877c478bd9Sstevel@tonic-gate * normally a domain for gettext(3i), but for
7887c478bd9Sstevel@tonic-gate * sgsmsg use this could be argued as being
7897c478bd9Sstevel@tonic-gate * redundent). Also make sure that the message
7907c478bd9Sstevel@tonic-gate * definitions haven't exceeeded the maximum
7917c478bd9Sstevel@tonic-gate * value allowed by gencat(1) before generating
7927c478bd9Sstevel@tonic-gate * any message file entries.
7937c478bd9Sstevel@tonic-gate */
7947c478bd9Sstevel@tonic-gate if (cflag == 1) {
7957c478bd9Sstevel@tonic-gate if (setid == 0) {
7967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "file "
7977c478bd9Sstevel@tonic-gate "%s: no message identifier "
7987c478bd9Sstevel@tonic-gate "has been established\n",
7997c478bd9Sstevel@tonic-gate fldesc);
8007c478bd9Sstevel@tonic-gate return (1);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate if (ptr > NL_MSGMAX) {
8037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "file "
8047c478bd9Sstevel@tonic-gate "%s: message definition "
8057c478bd9Sstevel@tonic-gate "(%d) exceeds allowable "
8067c478bd9Sstevel@tonic-gate "limit (NL_MSGMAX)\n",
8077c478bd9Sstevel@tonic-gate fldesc, ptr);
8087c478bd9Sstevel@tonic-gate return (1);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate * For catgets(3c) write the definition and the
8147c478bd9Sstevel@tonic-gate * message string to the message file. For
8157c478bd9Sstevel@tonic-gate * gettext(3i) write the message string as a
8167c478bd9Sstevel@tonic-gate * mesgid - indicate a mesgid has been output
8177c478bd9Sstevel@tonic-gate * so that a msgstr can follow.
8187c478bd9Sstevel@tonic-gate */
8197c478bd9Sstevel@tonic-gate if (cflag == 1) {
8207c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, "%d\t%s", ptr,
8217c478bd9Sstevel@tonic-gate token) < 0) {
8227c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8237c478bd9Sstevel@tonic-gate Errmsg_wrte, flmsgs,
8247c478bd9Sstevel@tonic-gate strerror(errno));
8257c478bd9Sstevel@tonic-gate return (1);
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate } else {
8287c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, "msgid\t\"") < 0) {
8297c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8307c478bd9Sstevel@tonic-gate Errmsg_wrte, flmsgs,
8317c478bd9Sstevel@tonic-gate strerror(errno));
8327c478bd9Sstevel@tonic-gate return (1);
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate msgid = 1;
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate * The message itself is a quoted string as this makes
8407c478bd9Sstevel@tonic-gate * embedding spaces at the start (or the end) of the
8417c478bd9Sstevel@tonic-gate * string very easy.
8427c478bd9Sstevel@tonic-gate */
8437c478bd9Sstevel@tonic-gate if (*token != '"') {
8447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_malt, fldesc,
8457c478bd9Sstevel@tonic-gate line);
8467c478bd9Sstevel@tonic-gate return (1);
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate (void) strcpy(defn, _defn);
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate * Write the tag to the lint definitions.
8537c478bd9Sstevel@tonic-gate */
8547c478bd9Sstevel@tonic-gate if (fdlint) {
8557c478bd9Sstevel@tonic-gate if (fprintf(fdlint, "\n#define\t%s\t",
8567c478bd9Sstevel@tonic-gate _defn) < 0) {
8577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
8587c478bd9Sstevel@tonic-gate fllint, strerror(errno));
8597c478bd9Sstevel@tonic-gate return (1);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate
8637c478bd9Sstevel@tonic-gate len = 0;
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate /*
8667c478bd9Sstevel@tonic-gate * Write each character of the message string to the
8677c478bd9Sstevel@tonic-gate * data array. Translate any escaped characters - use
8687c478bd9Sstevel@tonic-gate * the same specially recognized characters as defined
8697c478bd9Sstevel@tonic-gate * by gencat(1).
8707c478bd9Sstevel@tonic-gate */
8717c478bd9Sstevel@tonic-gate message:
8727c478bd9Sstevel@tonic-gate if (*token == '"') {
8737c478bd9Sstevel@tonic-gate if (fdlint &&
8747c478bd9Sstevel@tonic-gate (fprintf(fdlint, "%c", *token) < 0)) {
8757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
8767c478bd9Sstevel@tonic-gate fllint, strerror(errno));
8777c478bd9Sstevel@tonic-gate return (1);
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate token++;
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate while (*token) {
8827c478bd9Sstevel@tonic-gate char _token;
8837c478bd9Sstevel@tonic-gate
8847c478bd9Sstevel@tonic-gate if ((*token == '\\') && (escape == 0)) {
8857c478bd9Sstevel@tonic-gate escape = 1;
8867c478bd9Sstevel@tonic-gate if (fdlint && (*(token + 1) != '\n') &&
8877c478bd9Sstevel@tonic-gate fprintf(fdlint, "%c", *token) < 0) {
8887c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8897c478bd9Sstevel@tonic-gate Errmsg_wrte, fllint,
8907c478bd9Sstevel@tonic-gate strerror(errno));
8917c478bd9Sstevel@tonic-gate return (1);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate token++;
8947c478bd9Sstevel@tonic-gate continue;
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate if (escape) {
8977c478bd9Sstevel@tonic-gate if (*token == 'n')
8987c478bd9Sstevel@tonic-gate _token = '\n';
8997c478bd9Sstevel@tonic-gate else if (*token == 't')
9007c478bd9Sstevel@tonic-gate _token = '\t';
9017c478bd9Sstevel@tonic-gate else if (*token == 'v')
9027c478bd9Sstevel@tonic-gate _token = '\v';
9037c478bd9Sstevel@tonic-gate else if (*token == 'b')
9047c478bd9Sstevel@tonic-gate _token = '\b';
9057c478bd9Sstevel@tonic-gate else if (*token == 'f')
9067c478bd9Sstevel@tonic-gate _token = '\f';
9077c478bd9Sstevel@tonic-gate else if (*token == '\\')
9087c478bd9Sstevel@tonic-gate _token = '\\';
9097c478bd9Sstevel@tonic-gate else if (*token == '"')
9107c478bd9Sstevel@tonic-gate _token = '"';
9117c478bd9Sstevel@tonic-gate else if (*token == '\n')
9127c478bd9Sstevel@tonic-gate break;
9137c478bd9Sstevel@tonic-gate else
9147c478bd9Sstevel@tonic-gate _token = *token;
9157c478bd9Sstevel@tonic-gate
9167c478bd9Sstevel@tonic-gate if (fdmsgs && (prtmsgs == 1) &&
9177c478bd9Sstevel@tonic-gate (fprintf(fdmsgs, "\\") < 0)) {
9187c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
9197c478bd9Sstevel@tonic-gate Errmsg_wrte, flmsgs,
9207c478bd9Sstevel@tonic-gate strerror(errno));
9217c478bd9Sstevel@tonic-gate return (1);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate } else {
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate * If this is the trailing quote then
9267c478bd9Sstevel@tonic-gate * thats the last of the message string.
9277c478bd9Sstevel@tonic-gate * Eat up any remaining white space and
9287c478bd9Sstevel@tonic-gate * unless an escape character is found
9297c478bd9Sstevel@tonic-gate * terminate the data string with a 0.
9307c478bd9Sstevel@tonic-gate */
931a194faf8Srie /* BEGIN CSTYLED */
9327c478bd9Sstevel@tonic-gate if (*token == '"') {
9337c478bd9Sstevel@tonic-gate if (fdlint && (fprintf(fdlint,
9347c478bd9Sstevel@tonic-gate "%c", *token) < 0)) {
9357c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
9367c478bd9Sstevel@tonic-gate Errmsg_wrte, fllint,
9377c478bd9Sstevel@tonic-gate strerror(errno));
9387c478bd9Sstevel@tonic-gate return (1);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate
9417c478bd9Sstevel@tonic-gate if (fdmsgs && (prtmsgs == 1) &&
9427c478bd9Sstevel@tonic-gate (fprintf(fdmsgs, "%c",
9437c478bd9Sstevel@tonic-gate *token) < 0)) {
9447c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
9457c478bd9Sstevel@tonic-gate Errmsg_wrte, flmsgs,
9467c478bd9Sstevel@tonic-gate strerror(errno));
9477c478bd9Sstevel@tonic-gate return (1);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate while (*++token) {
9517c478bd9Sstevel@tonic-gate if (*token == '\n')
9527c478bd9Sstevel@tonic-gate break;
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate _token = '\0';
9557c478bd9Sstevel@tonic-gate } else
9567c478bd9Sstevel@tonic-gate _token = *token;
957a194faf8Srie /* END CSTYLED */
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate
9607c478bd9Sstevel@tonic-gate if (fdmsgs && (prtmsgs == 1) &&
9617c478bd9Sstevel@tonic-gate (fprintf(fdmsgs, "%c", *token) < 0)) {
9627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
9637c478bd9Sstevel@tonic-gate flmsgs, strerror(errno));
9647c478bd9Sstevel@tonic-gate return (1);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate
9677c478bd9Sstevel@tonic-gate if (fdlint && fprintf(fdlint,
9687c478bd9Sstevel@tonic-gate "%c", *token) < 0) {
9697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
9707c478bd9Sstevel@tonic-gate fllint, strerror(errno));
9717c478bd9Sstevel@tonic-gate return (1);
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate
9747c478bd9Sstevel@tonic-gate if (len >= bufsize) {
9757c478bd9Sstevel@tonic-gate bufsize += LINE_MAX;
9767c478bd9Sstevel@tonic-gate if ((token_buffer = realloc(
9777c478bd9Sstevel@tonic-gate token_buffer, bufsize)) == 0) {
9787c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
9797c478bd9Sstevel@tonic-gate Errmsg_nmem,
9807c478bd9Sstevel@tonic-gate strerror(errno));
9817c478bd9Sstevel@tonic-gate return (1);
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate token_buffer[len] = _token;
9857c478bd9Sstevel@tonic-gate ptr++, token++, len++;
9867c478bd9Sstevel@tonic-gate escape = 0;
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate if (_token == '\0')
9897c478bd9Sstevel@tonic-gate break;
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate * After the complete message string has been processed
9947c478bd9Sstevel@tonic-gate * (including its continuation beyond one line), create
9957c478bd9Sstevel@tonic-gate * a string size definition.
9967c478bd9Sstevel@tonic-gate */
9977c478bd9Sstevel@tonic-gate if (escape == 0) {
9987c478bd9Sstevel@tonic-gate const char *form = "#define\t%s_SIZE\t%d\n";
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate token_buffer[len] = '\0';
10017c478bd9Sstevel@tonic-gate
10027c478bd9Sstevel@tonic-gate message_append(defn, token_buffer);
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate if (fdlint && fprintf(fdlint, form, defn,
10057c478bd9Sstevel@tonic-gate (len - 1)) < 0) {
10067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte,
10077c478bd9Sstevel@tonic-gate fllint, strerror(errno));
10087c478bd9Sstevel@tonic-gate return (1);
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate break;
10127c478bd9Sstevel@tonic-gate
10137c478bd9Sstevel@tonic-gate default:
10147c478bd9Sstevel@tonic-gate /*
10157c478bd9Sstevel@tonic-gate * Empty lines are passed through to the message file.
10167c478bd9Sstevel@tonic-gate */
10177c478bd9Sstevel@tonic-gate while (isspace(*token))
10187c478bd9Sstevel@tonic-gate token++;
10197c478bd9Sstevel@tonic-gate
10207c478bd9Sstevel@tonic-gate if (*token == 0) {
10217c478bd9Sstevel@tonic-gate if (msgid || (fdmsgs && (prtmsgs == 1))) {
10227c478bd9Sstevel@tonic-gate /*
10237c478bd9Sstevel@tonic-gate * If a msgid has been output a msgstr
10247c478bd9Sstevel@tonic-gate * must follow before we digest the new
10257c478bd9Sstevel@tonic-gate * token.
10267c478bd9Sstevel@tonic-gate */
10277c478bd9Sstevel@tonic-gate if (msgid) {
10287c478bd9Sstevel@tonic-gate msgid = 0;
10297c478bd9Sstevel@tonic-gate str = "msgstr\t\"\"\n\n";
10307c478bd9Sstevel@tonic-gate } else
10317c478bd9Sstevel@tonic-gate str = "\n";
10327c478bd9Sstevel@tonic-gate
10337c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, str) < 0) {
10347c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10357c478bd9Sstevel@tonic-gate Errmsg_wrte, flmsgs,
10367c478bd9Sstevel@tonic-gate strerror(errno));
10377c478bd9Sstevel@tonic-gate return (1);
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate break;
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate
10437c478bd9Sstevel@tonic-gate /*
10447c478bd9Sstevel@tonic-gate * If an escape is in effect then any tokens are taken
10457c478bd9Sstevel@tonic-gate * to be message continuations.
10467c478bd9Sstevel@tonic-gate */
10477c478bd9Sstevel@tonic-gate if (escape) {
10487c478bd9Sstevel@tonic-gate escape = 0;
10497c478bd9Sstevel@tonic-gate goto message;
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate
10527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "file %s: line %d: invalid "
10537c478bd9Sstevel@tonic-gate "input does not start with #, $ or @\n", fldesc,
10547c478bd9Sstevel@tonic-gate line);
10557c478bd9Sstevel@tonic-gate return (1);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate line++;
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate free(token_buffer);
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate return (0);
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate
10657c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)10667c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate opterr = 0;
10697c478bd9Sstevel@tonic-gate while ((line = getopt(argc, argv, "cd:h:lm:n:i:v")) != EOF) {
10707c478bd9Sstevel@tonic-gate switch (line) {
10717c478bd9Sstevel@tonic-gate case 'c': /* catgets instead of gettext */
10727c478bd9Sstevel@tonic-gate cflag = 1;
10737c478bd9Sstevel@tonic-gate break;
10747c478bd9Sstevel@tonic-gate case 'd': /* new message data filename */
10757c478bd9Sstevel@tonic-gate fldata = optarg; /* (msg.c is default) */
10767c478bd9Sstevel@tonic-gate break;
10777c478bd9Sstevel@tonic-gate case 'h': /* new message defs filename */
10787c478bd9Sstevel@tonic-gate fldefs = optarg; /* (msg.h is default) */
10797c478bd9Sstevel@tonic-gate break;
10807c478bd9Sstevel@tonic-gate case 'i': /* input message ids from */
10817c478bd9Sstevel@tonic-gate flmids = optarg; /* from this file */
10827c478bd9Sstevel@tonic-gate break;
10837c478bd9Sstevel@tonic-gate case 'l': /* define message data arrays */
10847c478bd9Sstevel@tonic-gate lflag = 1; /* to be local (static) */
10857c478bd9Sstevel@tonic-gate break;
10867c478bd9Sstevel@tonic-gate case 'm': /* generate message database */
10877c478bd9Sstevel@tonic-gate flmsgs = optarg; /* to this file */
10887c478bd9Sstevel@tonic-gate break;
10897c478bd9Sstevel@tonic-gate case 'n': /* new data array and func */
10907c478bd9Sstevel@tonic-gate interface = optarg; /* name (msg is default) */
10917c478bd9Sstevel@tonic-gate break;
10927c478bd9Sstevel@tonic-gate case 'v':
10937c478bd9Sstevel@tonic-gate vflag = 1; /* set verbose flag */
10947c478bd9Sstevel@tonic-gate break;
10957c478bd9Sstevel@tonic-gate case '?':
10967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_use, argv[0]);
10977c478bd9Sstevel@tonic-gate exit(1);
10987c478bd9Sstevel@tonic-gate default:
10997c478bd9Sstevel@tonic-gate break;
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate * Validate the we have been given at least one input file.
11057c478bd9Sstevel@tonic-gate */
11067c478bd9Sstevel@tonic-gate if ((argc - optind) < 1) {
11077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_use);
11087c478bd9Sstevel@tonic-gate exit(1);
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate
11117c478bd9Sstevel@tonic-gate /*
11127c478bd9Sstevel@tonic-gate * Open all the required output files.
11137c478bd9Sstevel@tonic-gate */
11147c478bd9Sstevel@tonic-gate if (fldefs) {
11157c478bd9Sstevel@tonic-gate if ((fddefs = fopen(fldefs, "w+")) == NULL) {
11167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_opne, fldefs,
11177c478bd9Sstevel@tonic-gate strerror(errno));
11187c478bd9Sstevel@tonic-gate return (1);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate if (fldata) {
11227c478bd9Sstevel@tonic-gate if (fldefs && (strcmp(fldefs, fldata) == 0))
11237c478bd9Sstevel@tonic-gate fddata = fddefs;
11247c478bd9Sstevel@tonic-gate else if ((fddata = fopen(fldata, "w+")) == NULL) {
11257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_opne, fldata,
11267c478bd9Sstevel@tonic-gate strerror(errno));
11277c478bd9Sstevel@tonic-gate return (1);
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate if (fddefs && fddata) {
1131b23a7923SAli Bahrami (void) sprintf(fllint, "%s.%d.XXXXXX", nmlint, (int)getpid());
1132b23a7923SAli Bahrami if ((mkstemp(fllint) == -1) ||
1133b23a7923SAli Bahrami ((fdlint = fopen(fllint, "w+")) == NULL)) {
11347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_opne, fllint,
11357c478bd9Sstevel@tonic-gate strerror(errno));
11367c478bd9Sstevel@tonic-gate return (1);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate if (flmsgs) {
11407c478bd9Sstevel@tonic-gate if ((fdmsgs = fopen(flmsgs, "w+")) == NULL) {
11417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_opne, flmsgs,
11427c478bd9Sstevel@tonic-gate strerror(errno));
11437c478bd9Sstevel@tonic-gate return (1);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate if (flmids) {
11477c478bd9Sstevel@tonic-gate if ((fdmids = fopen(flmids, "r")) == NULL) {
11487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_opne, flmids,
11497c478bd9Sstevel@tonic-gate strerror(errno));
11507c478bd9Sstevel@tonic-gate return (1);
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate
11547c478bd9Sstevel@tonic-gate
11557c478bd9Sstevel@tonic-gate /*
11567c478bd9Sstevel@tonic-gate * Initialize the message definition and message data streams.
11577c478bd9Sstevel@tonic-gate */
11587c478bd9Sstevel@tonic-gate if (fddefs) {
11597c478bd9Sstevel@tonic-gate if (init_defs())
11607c478bd9Sstevel@tonic-gate return (1);
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate
11637c478bd9Sstevel@tonic-gate /*
11647c478bd9Sstevel@tonic-gate * Read the input message file, and for each line process accordingly.
11657c478bd9Sstevel@tonic-gate */
11667c478bd9Sstevel@tonic-gate for (; optind < argc; optind++) {
11677c478bd9Sstevel@tonic-gate int err;
11687c478bd9Sstevel@tonic-gate
11697c478bd9Sstevel@tonic-gate fldesc = argv[optind];
11707c478bd9Sstevel@tonic-gate
11717c478bd9Sstevel@tonic-gate if ((fddesc = fopen(fldesc, "r")) == NULL) {
11727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_opne, fldesc,
11737c478bd9Sstevel@tonic-gate strerror(errno));
11747c478bd9Sstevel@tonic-gate return (1);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate err = file();
11777c478bd9Sstevel@tonic-gate (void) fclose(fddesc);
11787c478bd9Sstevel@tonic-gate
11797c478bd9Sstevel@tonic-gate if (err != 0)
11807c478bd9Sstevel@tonic-gate return (1);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate
11837c478bd9Sstevel@tonic-gate /*
11847c478bd9Sstevel@tonic-gate * If a msgid has been output a msgstr must follow before we end the
11857c478bd9Sstevel@tonic-gate * file.
11867c478bd9Sstevel@tonic-gate */
11877c478bd9Sstevel@tonic-gate if (msgid) {
11887c478bd9Sstevel@tonic-gate msgid = 0;
11897c478bd9Sstevel@tonic-gate if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
11907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Errmsg_wrte, flmsgs,
11917c478bd9Sstevel@tonic-gate strerror(errno));
11927c478bd9Sstevel@tonic-gate return (1);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate
11967c478bd9Sstevel@tonic-gate if (fdmids)
11977c478bd9Sstevel@tonic-gate (void) fclose(fdmids);
11987c478bd9Sstevel@tonic-gate if (fdmsgs)
11997c478bd9Sstevel@tonic-gate (void) fclose(fdmsgs);
12007c478bd9Sstevel@tonic-gate
12017c478bd9Sstevel@tonic-gate if (fddefs) {
12027c478bd9Sstevel@tonic-gate if (output_defs())
12037c478bd9Sstevel@tonic-gate return (1);
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate * Finish off any generated data and header file.
12087c478bd9Sstevel@tonic-gate */
12097c478bd9Sstevel@tonic-gate if (fldata) {
12107c478bd9Sstevel@tonic-gate if (output_data())
12117c478bd9Sstevel@tonic-gate return (1);
12127c478bd9Sstevel@tonic-gate }
12137c478bd9Sstevel@tonic-gate if (fddefs) {
12147c478bd9Sstevel@tonic-gate if (fini_defs())
12157c478bd9Sstevel@tonic-gate return (1);
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate
12187c478bd9Sstevel@tonic-gate if (vflag)
12197c478bd9Sstevel@tonic-gate dump_stringtab(stp);
12207c478bd9Sstevel@tonic-gate
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate * Close up everything and go home.
12237c478bd9Sstevel@tonic-gate */
12247c478bd9Sstevel@tonic-gate if (fddata)
12257c478bd9Sstevel@tonic-gate (void) fclose(fddata);
12267c478bd9Sstevel@tonic-gate if (fddefs && (fddefs != fddata))
12277c478bd9Sstevel@tonic-gate (void) fclose(fddefs);
12287c478bd9Sstevel@tonic-gate if (fddefs && fddata) {
12297c478bd9Sstevel@tonic-gate (void) fclose(fdlint);
12307c478bd9Sstevel@tonic-gate (void) unlink(fllint);
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate
12337c478bd9Sstevel@tonic-gate if (stp)
12347c478bd9Sstevel@tonic-gate st_destroy(stp);
12357c478bd9Sstevel@tonic-gate
12367c478bd9Sstevel@tonic-gate return (0);
12377c478bd9Sstevel@tonic-gate }
1238