xref: /titanic_51/usr/src/cmd/msgfmt/gnu_handle.c (revision ccfce1d8e7219e7616d8df1e8cfca8f97febf5b8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*ccfce1d8Sna195498  * Common Development and Distribution License (the "License").
6*ccfce1d8Sna195498  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ccfce1d8Sna195498  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include "gnu_msgfmt.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate static int	next_entry_is_fuzzy = 0;
317c478bd9Sstevel@tonic-gate static int	next_entry_is_c_format = 0;
327c478bd9Sstevel@tonic-gate static struct catalog	*cur_catalog = NULL;
337c478bd9Sstevel@tonic-gate static char	*cur_mo = NULL;
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate FILE	*fp;
367c478bd9Sstevel@tonic-gate iconv_t	cd = (iconv_t)-1;
377c478bd9Sstevel@tonic-gate struct catalog	*catalog_head = NULL;
387c478bd9Sstevel@tonic-gate int	cur_po_index = 0;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static size_t
417c478bd9Sstevel@tonic-gate search_alias(char **paddr, size_t size, const char *variant)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	char	*addr = *paddr;
447c478bd9Sstevel@tonic-gate 	char 	*p, *sp, *q;
457c478bd9Sstevel@tonic-gate 	size_t	var_len, can_len;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	var_len = strlen(variant);
487c478bd9Sstevel@tonic-gate 	p = addr;
497c478bd9Sstevel@tonic-gate 	q = addr + size;
507c478bd9Sstevel@tonic-gate 	while (q > p) {
517c478bd9Sstevel@tonic-gate 		if (*p == '#') {
527c478bd9Sstevel@tonic-gate 			/*
537c478bd9Sstevel@tonic-gate 			 * Line beginning with '#' is a comment
547c478bd9Sstevel@tonic-gate 			 */
557c478bd9Sstevel@tonic-gate 			p++;
567c478bd9Sstevel@tonic-gate 			while ((q > p) && (*p++ != '\n'))
577c478bd9Sstevel@tonic-gate 				;
587c478bd9Sstevel@tonic-gate 			continue;
597c478bd9Sstevel@tonic-gate 		}
607c478bd9Sstevel@tonic-gate 		/* skip leading spaces */
617c478bd9Sstevel@tonic-gate 		while ((q > p) &&
627c478bd9Sstevel@tonic-gate 		    ((*p == ' ') || (*p == '\t')))
637c478bd9Sstevel@tonic-gate 			p++;
647c478bd9Sstevel@tonic-gate 		if (q <= p)
657c478bd9Sstevel@tonic-gate 			break;
667c478bd9Sstevel@tonic-gate 		sp = p;
677c478bd9Sstevel@tonic-gate 		while ((q > p) && (*p != ' ') &&
687c478bd9Sstevel@tonic-gate 		    (*p != '\t') && (*p != '\n'))
697c478bd9Sstevel@tonic-gate 			p++;
707c478bd9Sstevel@tonic-gate 		if (q <= p) {
717c478bd9Sstevel@tonic-gate 			/* invalid entry */
727c478bd9Sstevel@tonic-gate 			break;
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 		if (*p == '\n') {
757c478bd9Sstevel@tonic-gate 			/* invalid entry */
767c478bd9Sstevel@tonic-gate 			p++;
777c478bd9Sstevel@tonic-gate 			continue;
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		if (((p - sp) != var_len) ||
817c478bd9Sstevel@tonic-gate 		    ((strncmp(sp, variant, var_len) != 0) &&
827c478bd9Sstevel@tonic-gate 		    (strncasecmp(sp, variant, var_len) != 0))) {
837c478bd9Sstevel@tonic-gate 			/*
847c478bd9Sstevel@tonic-gate 			 * didn't match
857c478bd9Sstevel@tonic-gate 			 */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 			/* skip remaining chars in this line */
887c478bd9Sstevel@tonic-gate 			p++;
897c478bd9Sstevel@tonic-gate 			while ((q > p) && (*p++ != '\n'))
907c478bd9Sstevel@tonic-gate 				;
917c478bd9Sstevel@tonic-gate 			continue;
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 		/* matching entry found */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		/* skip spaces */
977c478bd9Sstevel@tonic-gate 		while ((q > p) &&
987c478bd9Sstevel@tonic-gate 		    ((*p == ' ') || (*p == '\t')))
997c478bd9Sstevel@tonic-gate 			p++;
1007c478bd9Sstevel@tonic-gate 		if (q <= p)
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		sp = p;
1037c478bd9Sstevel@tonic-gate 		while ((q > p) && (*p != ' ') &&
1047c478bd9Sstevel@tonic-gate 		    (*p != '\t') && (*p != '\n'))
1057c478bd9Sstevel@tonic-gate 			p++;
1067c478bd9Sstevel@tonic-gate 		can_len = p - sp;
1077c478bd9Sstevel@tonic-gate 		if (can_len == 0) {
1087c478bd9Sstevel@tonic-gate 			while ((q > p) && (*p++ != '\n'))
1097c478bd9Sstevel@tonic-gate 				;
1107c478bd9Sstevel@tonic-gate 			continue;
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 		*paddr = sp;
1137c478bd9Sstevel@tonic-gate 		return (can_len);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	return (0);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Checks if the specified charset is equivalent to UTF-8.
1207c478bd9Sstevel@tonic-gate  * If it's equivalent to UTF-8, returns 1; Otherwise, returns 0.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static int
1237c478bd9Sstevel@tonic-gate check_utf8(const char *charset)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	int	fd;
1267c478bd9Sstevel@tonic-gate 	struct stat64	statbuf;
1277c478bd9Sstevel@tonic-gate 	caddr_t	addr;
1287c478bd9Sstevel@tonic-gate 	size_t	buflen, charset_len, utf8_len;
1297c478bd9Sstevel@tonic-gate 	char	*c_charset, *c_utf8, *p;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (strcmp(charset, DEST_CHARSET) == 0)
1327c478bd9Sstevel@tonic-gate 		return (1);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	fd = open(_ENCODING_ALIAS_PATH, O_RDONLY);
1357c478bd9Sstevel@tonic-gate 	if (fd == -1) {
1367c478bd9Sstevel@tonic-gate 		/* no alias file found */
1377c478bd9Sstevel@tonic-gate 		return (0);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	if (fstat64(fd, &statbuf) == -1) {
1407c478bd9Sstevel@tonic-gate 		(void) close(fd);
1417c478bd9Sstevel@tonic-gate 		return (0);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	buflen = (size_t)statbuf.st_size;
1447c478bd9Sstevel@tonic-gate 	addr = mmap(NULL, buflen, PROT_READ, MAP_SHARED, fd, 0);
1457c478bd9Sstevel@tonic-gate 	(void) close(fd);
1467c478bd9Sstevel@tonic-gate 	if (addr == MAP_FAILED) {
1477c478bd9Sstevel@tonic-gate 		warning("mmap() for %s failed.", _ENCODING_ALIAS_PATH);
1487c478bd9Sstevel@tonic-gate 		return (0);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	p = (char *)addr;
1517c478bd9Sstevel@tonic-gate 	charset_len = search_alias(&p, buflen, charset);
1527c478bd9Sstevel@tonic-gate 	if (charset_len) {
1537c478bd9Sstevel@tonic-gate 		c_charset = alloca(charset_len + 1);
1547c478bd9Sstevel@tonic-gate 		(void) memcpy(c_charset, p, charset_len);
1557c478bd9Sstevel@tonic-gate 		c_charset[charset_len] = '\0';
1567c478bd9Sstevel@tonic-gate 	} else {
1577c478bd9Sstevel@tonic-gate 		c_charset = (char *)charset;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	p = (char *)addr;
1607c478bd9Sstevel@tonic-gate 	utf8_len = search_alias(&p, buflen, DEST_CHARSET);
1617c478bd9Sstevel@tonic-gate 	if (utf8_len) {
1627c478bd9Sstevel@tonic-gate 		c_utf8 = alloca(utf8_len + 1);
1637c478bd9Sstevel@tonic-gate 		(void) memcpy(c_utf8, p, utf8_len);
1647c478bd9Sstevel@tonic-gate 		c_utf8[utf8_len] = '\0';
1657c478bd9Sstevel@tonic-gate 	} else {
1667c478bd9Sstevel@tonic-gate 		c_utf8 = DEST_CHARSET;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	(void) munmap(addr, buflen);
1697c478bd9Sstevel@tonic-gate 	if (charset_len == 0 && utf8_len == 0) {
1707c478bd9Sstevel@tonic-gate 		/*
1717c478bd9Sstevel@tonic-gate 		 * Entry for neither charset nor utf8 found
1727c478bd9Sstevel@tonic-gate 		 */
1737c478bd9Sstevel@tonic-gate 		return (0);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (strcmp(c_charset, c_utf8) == 0)
1777c478bd9Sstevel@tonic-gate 		return (1);
1787c478bd9Sstevel@tonic-gate 	else
1797c478bd9Sstevel@tonic-gate 		return (0);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate static void
1837c478bd9Sstevel@tonic-gate conv_init(const char *charset)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	if (charset == NULL) {
1867c478bd9Sstevel@tonic-gate 		/*
1877c478bd9Sstevel@tonic-gate 		 * No conversion
1887c478bd9Sstevel@tonic-gate 		 */
1897c478bd9Sstevel@tonic-gate 		cd = (iconv_t)-1;
1907c478bd9Sstevel@tonic-gate 		return;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	if (check_utf8(charset)) {
1937c478bd9Sstevel@tonic-gate 		/*
1947c478bd9Sstevel@tonic-gate 		 * Charset is UTF-8.
1957c478bd9Sstevel@tonic-gate 		 * No conversion is required.
1967c478bd9Sstevel@tonic-gate 		 */
1977c478bd9Sstevel@tonic-gate 		cd = (iconv_t)-1;
1987c478bd9Sstevel@tonic-gate 		return;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	cd = iconv_open(DEST_CHARSET, charset);
2017c478bd9Sstevel@tonic-gate 	if (cd == (iconv_t)-1) {
2027c478bd9Sstevel@tonic-gate 		/*
2037c478bd9Sstevel@tonic-gate 		 * No such a conversion
2047c478bd9Sstevel@tonic-gate 		 */
2057c478bd9Sstevel@tonic-gate 		warning(gettext(WARN_NOCONV),
2067c478bd9Sstevel@tonic-gate 			cur_line, cur_po, charset, DEST_CHARSET);
2077c478bd9Sstevel@tonic-gate 		return;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate void
2127c478bd9Sstevel@tonic-gate clear_state(void)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	next_entry_is_fuzzy = 0;
2157c478bd9Sstevel@tonic-gate 	next_entry_is_c_format = 0;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate void
2197c478bd9Sstevel@tonic-gate handle_domain(char *domainname)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	if (outfile) {
2227c478bd9Sstevel@tonic-gate 		/*
2237c478bd9Sstevel@tonic-gate 		 * outfile has been specified by -o option
2247c478bd9Sstevel@tonic-gate 		 * ignore all domain directives
2257c478bd9Sstevel@tonic-gate 		 */
2267c478bd9Sstevel@tonic-gate 		if (verbose_flag) {
2277c478bd9Sstevel@tonic-gate 			diag(gettext(DIAG_IGNORE_DOMAIN),
2287c478bd9Sstevel@tonic-gate 				cur_line, cur_po, domainname);
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 		free(domainname);
2317c478bd9Sstevel@tonic-gate 		return;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (strict_flag) {
2357c478bd9Sstevel@tonic-gate 		/*
2367c478bd9Sstevel@tonic-gate 		 * add ".mo" to the domain
2377c478bd9Sstevel@tonic-gate 		 */
2387c478bd9Sstevel@tonic-gate 		char	*tmp;
2397c478bd9Sstevel@tonic-gate 		tmp = Xrealloc(domainname, strlen(domainname) + 3 + 1);
2407c478bd9Sstevel@tonic-gate 		(void) strcat(tmp, ".mo");
2417c478bd9Sstevel@tonic-gate 		domainname = tmp;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	catalog_init(domainname);
2447c478bd9Sstevel@tonic-gate 	free(domainname);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate void
2487c478bd9Sstevel@tonic-gate catalog_init(const char *filename)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	struct catalog	*p;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (!catalog_head) {
2537c478bd9Sstevel@tonic-gate 		p = Xcalloc(1, sizeof (struct catalog));
2547c478bd9Sstevel@tonic-gate 		p->fname = Xstrdup(filename);
2557c478bd9Sstevel@tonic-gate 		p->msg_size = DEF_MSG_NUM;
2567c478bd9Sstevel@tonic-gate 		p->nmsg = 0;
2577c478bd9Sstevel@tonic-gate 		p->msg = Xcalloc(p->msg_size, sizeof (struct messages));
2587c478bd9Sstevel@tonic-gate 		p->thash_size = find_prime(DEF_MSG_NUM);
2597c478bd9Sstevel@tonic-gate 		p->thash = Xcalloc(p->thash_size, sizeof (unsigned int));
2607c478bd9Sstevel@tonic-gate 		catalog_head = p;
2617c478bd9Sstevel@tonic-gate 	} else {
2627c478bd9Sstevel@tonic-gate 		p = catalog_head;
2637c478bd9Sstevel@tonic-gate 		for (; ; ) {
2647c478bd9Sstevel@tonic-gate 			struct catalog	*tmp;
2657c478bd9Sstevel@tonic-gate 			if (strcmp(p->fname, filename) == 0) {
2667c478bd9Sstevel@tonic-gate 				/* already registered */
2677c478bd9Sstevel@tonic-gate 				break;
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 			if (p->next) {
2707c478bd9Sstevel@tonic-gate 				p = p->next;
2717c478bd9Sstevel@tonic-gate 				continue;
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 			/*
2747c478bd9Sstevel@tonic-gate 			 * this domain hasn't been registered
2757c478bd9Sstevel@tonic-gate 			 */
2767c478bd9Sstevel@tonic-gate 			tmp = Xcalloc(1, sizeof (struct catalog));
2777c478bd9Sstevel@tonic-gate 			tmp->fname = Xstrdup(filename);
2787c478bd9Sstevel@tonic-gate 			tmp->msg_size = DEF_MSG_NUM;
2797c478bd9Sstevel@tonic-gate 			tmp->nmsg = 0;
2807c478bd9Sstevel@tonic-gate 			tmp->msg = Xcalloc(tmp->msg_size,
2817c478bd9Sstevel@tonic-gate 			    sizeof (struct messages));
2827c478bd9Sstevel@tonic-gate 			tmp->thash_size = find_prime(DEF_MSG_NUM);
2837c478bd9Sstevel@tonic-gate 			tmp->thash = Xcalloc(tmp->thash_size,
2847c478bd9Sstevel@tonic-gate 			    sizeof (unsigned int));
2857c478bd9Sstevel@tonic-gate 			p->next = tmp;
2867c478bd9Sstevel@tonic-gate 			p = tmp;
2877c478bd9Sstevel@tonic-gate 			break;
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 	cur_catalog = p;
2917c478bd9Sstevel@tonic-gate 	cur_mo = p->fname;
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate void
2967c478bd9Sstevel@tonic-gate handle_comment(char *comment)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	char	*p;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	p = comment;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (*p != ',') {
3037c478bd9Sstevel@tonic-gate 		/*
3047c478bd9Sstevel@tonic-gate 		 * This comment is just informative only.
3057c478bd9Sstevel@tonic-gate 		 */
3067c478bd9Sstevel@tonic-gate 		free(comment);
3077c478bd9Sstevel@tonic-gate 		return;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	/*
3107c478bd9Sstevel@tonic-gate 	 * Checks "fuzzy", "c-format", and "no-c-format"
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	p++;
3137c478bd9Sstevel@tonic-gate 	if (strstr(p, "fuzzy") != NULL) {
3147c478bd9Sstevel@tonic-gate 		next_entry_is_fuzzy = 1;
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	if (strstr(p, "no-c-format") != NULL) {
3177c478bd9Sstevel@tonic-gate 		next_entry_is_c_format = 0;
3187c478bd9Sstevel@tonic-gate 	} else if (strstr(p, "c-format") != NULL) {
3197c478bd9Sstevel@tonic-gate 		next_entry_is_c_format = 1;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	free(comment);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate void
3267c478bd9Sstevel@tonic-gate handle_message(struct entry *id, struct entry *str)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	char	*charset, *nplurals, *tmp, *p;
3297c478bd9Sstevel@tonic-gate 	struct messages	*msg, *dupmsg;
3307c478bd9Sstevel@tonic-gate 	size_t	len;
3317c478bd9Sstevel@tonic-gate 	unsigned int	hash_val;
3327c478bd9Sstevel@tonic-gate 	unsigned int	nmsg, n, thash_idx;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (cur_mo == NULL) {
3357c478bd9Sstevel@tonic-gate 		/*
3367c478bd9Sstevel@tonic-gate 		 * output file hasn't been specified, nor
3377c478bd9Sstevel@tonic-gate 		 * no domain directive found
3387c478bd9Sstevel@tonic-gate 		 */
3397c478bd9Sstevel@tonic-gate 		char	*default_domain;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		default_domain = strict_flag ? DEFAULT_DOMAIN_MO :
3427c478bd9Sstevel@tonic-gate 		    DEFAULT_DOMAIN;
3437c478bd9Sstevel@tonic-gate 		catalog_init(default_domain);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/*
3477c478bd9Sstevel@tonic-gate 	 * cur_catalog should be valid, at this point
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	hash_val = hashpjw(id->str);
3517c478bd9Sstevel@tonic-gate 	dupmsg = search_msg(cur_catalog, id->str, hash_val);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (dupmsg) {
3547c478bd9Sstevel@tonic-gate 		if ((dupmsg->str_len == str->len) &&
3557c478bd9Sstevel@tonic-gate 		    (memcmp(dupmsg->str, str->str, str->len) == 0)) {
3567c478bd9Sstevel@tonic-gate 			/* totally same entry */
3577c478bd9Sstevel@tonic-gate 			if (verbose_flag) {
3587c478bd9Sstevel@tonic-gate 				warning(gettext(WARN_DUP_ENTRIES),
3597c478bd9Sstevel@tonic-gate 				    dupmsg->num, po_names[dupmsg->po],
3607c478bd9Sstevel@tonic-gate 				    id->num, cur_po);
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 			free(id->str);
3637c478bd9Sstevel@tonic-gate 			if (id->pos)
3647c478bd9Sstevel@tonic-gate 				free(id->pos);
3657c478bd9Sstevel@tonic-gate 			free(str->str);
3667c478bd9Sstevel@tonic-gate 			if (str->pos)
3677c478bd9Sstevel@tonic-gate 				free(str->pos);
3687c478bd9Sstevel@tonic-gate 			return;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		/* duplicate msgid */
3717c478bd9Sstevel@tonic-gate 		if (verbose_flag) {
3727c478bd9Sstevel@tonic-gate 			diag(gettext(ERR_DUP_ENTRIES),
3737c478bd9Sstevel@tonic-gate 			    dupmsg->num, po_names[dupmsg->po],
3747c478bd9Sstevel@tonic-gate 			    id->num, cur_po);
3757c478bd9Sstevel@tonic-gate 			po_error++;
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 		/* ignore this etnry */
3787c478bd9Sstevel@tonic-gate 		free(id->str);
3797c478bd9Sstevel@tonic-gate 		if (id->pos)
3807c478bd9Sstevel@tonic-gate 			free(id->pos);
3817c478bd9Sstevel@tonic-gate 		free(str->str);
3827c478bd9Sstevel@tonic-gate 		if (str->pos)
3837c478bd9Sstevel@tonic-gate 			free(str->pos);
3847c478bd9Sstevel@tonic-gate 		return;
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	if (next_entry_is_fuzzy) {
3887c478bd9Sstevel@tonic-gate 		/* fuzzy entry */
3897c478bd9Sstevel@tonic-gate 		cur_catalog->fnum++;
3907c478bd9Sstevel@tonic-gate 		if (!fuzzy_flag) {
3917c478bd9Sstevel@tonic-gate 			/* ignore this entry */
3927c478bd9Sstevel@tonic-gate 			free(id->str);
3937c478bd9Sstevel@tonic-gate 			if (id->pos)
3947c478bd9Sstevel@tonic-gate 				free(id->pos);
3957c478bd9Sstevel@tonic-gate 			free(str->str);
3967c478bd9Sstevel@tonic-gate 			if (str->pos)
3977c478bd9Sstevel@tonic-gate 				free(str->pos);
3987c478bd9Sstevel@tonic-gate 			return;
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (str->len == str->no) {
4037c478bd9Sstevel@tonic-gate 		/* this entry is not translated */
4047c478bd9Sstevel@tonic-gate 		cur_catalog->unum++;
4057c478bd9Sstevel@tonic-gate 		free(id->str);
4067c478bd9Sstevel@tonic-gate 		if (id->pos)
4077c478bd9Sstevel@tonic-gate 			free(id->pos);
4087c478bd9Sstevel@tonic-gate 		free(str->str);
4097c478bd9Sstevel@tonic-gate 		if (str->pos)
4107c478bd9Sstevel@tonic-gate 			free(str->pos);
4117c478bd9Sstevel@tonic-gate 		return;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/* Checks if this is the header entry */
4157c478bd9Sstevel@tonic-gate 	if ((id->no == 1) && (id->len == 1)) {
4167c478bd9Sstevel@tonic-gate 		/*
4177c478bd9Sstevel@tonic-gate 		 * Header entry
4187c478bd9Sstevel@tonic-gate 		 */
4197c478bd9Sstevel@tonic-gate 		cur_catalog->header++;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		/*
4227c478bd9Sstevel@tonic-gate 		 * Need to extract the charset information
4237c478bd9Sstevel@tonic-gate 		 */
4247c478bd9Sstevel@tonic-gate 		charset = strstr(str->str, CHARSET_STR);
4257c478bd9Sstevel@tonic-gate 		if (charset == NULL) {
4267c478bd9Sstevel@tonic-gate 			/* no charset information */
4277c478bd9Sstevel@tonic-gate 			warning(gettext(WARN_NOCHARSET),
4287c478bd9Sstevel@tonic-gate 			    id->num, cur_po, str->num);
4297c478bd9Sstevel@tonic-gate 			conv_init(NULL);
4307c478bd9Sstevel@tonic-gate 		} else {
4317c478bd9Sstevel@tonic-gate 			charset += CHARSET_LEN;
432*ccfce1d8Sna195498 			p = strpbrk(charset, " \t\n");
433*ccfce1d8Sna195498 			if (p != NULL) {
434*ccfce1d8Sna195498 				/* p points to a space, tab or new line char */
4357c478bd9Sstevel@tonic-gate 				len = p - charset;
436*ccfce1d8Sna195498 			} else {
437*ccfce1d8Sna195498 				/* not found */
438*ccfce1d8Sna195498 				len = strlen(charset);
439*ccfce1d8Sna195498 			}
4407c478bd9Sstevel@tonic-gate 			tmp = Xmalloc(len + 1);
4417c478bd9Sstevel@tonic-gate 			(void) memcpy(tmp, charset, len);
4427c478bd9Sstevel@tonic-gate 			*(tmp + len) = '\0';
4437c478bd9Sstevel@tonic-gate 			charset = tmp;
4447c478bd9Sstevel@tonic-gate 			conv_init(charset);
4457c478bd9Sstevel@tonic-gate 			free(charset);
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 		nplurals = strstr(str->str, NPLURALS_STR);
4487c478bd9Sstevel@tonic-gate 		if (nplurals == NULL) {
4497c478bd9Sstevel@tonic-gate 			cur_catalog->nplurals = 0;
4507c478bd9Sstevel@tonic-gate 		} else {
4517c478bd9Sstevel@tonic-gate 			unsigned int	num;
4527c478bd9Sstevel@tonic-gate 			nplurals += NPLURALS_LEN;
4537c478bd9Sstevel@tonic-gate 			p = nplurals;
4547c478bd9Sstevel@tonic-gate 			num = 0;
4557c478bd9Sstevel@tonic-gate 			while (isdigit((unsigned char)*p)) {
4567c478bd9Sstevel@tonic-gate 				num = num * 10 + *p++ - '0';
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 			cur_catalog->nplurals = num;
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (verbose_flag)
4637c478bd9Sstevel@tonic-gate 		check_format(id, str, next_entry_is_c_format);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (id->pos)
4667c478bd9Sstevel@tonic-gate 		free(id->pos);
4677c478bd9Sstevel@tonic-gate 	if (str->pos)
4687c478bd9Sstevel@tonic-gate 		free(str->pos);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	msg = cur_catalog->msg;
4717c478bd9Sstevel@tonic-gate 	nmsg = cur_catalog->nmsg;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	msg[nmsg].po = cur_po_index;
4747c478bd9Sstevel@tonic-gate 	msg[nmsg].num = id->num;
4757c478bd9Sstevel@tonic-gate 	msg[nmsg].id = id->str;
4767c478bd9Sstevel@tonic-gate 	msg[nmsg].id_len = id->len;
4777c478bd9Sstevel@tonic-gate 	msg[nmsg].str = str->str;
4787c478bd9Sstevel@tonic-gate 	msg[nmsg].str_len = str->len;
4797c478bd9Sstevel@tonic-gate 	msg[nmsg].hash = hash_val;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	thash_idx = get_hash_index(cur_catalog->thash,
4827c478bd9Sstevel@tonic-gate 	    hash_val, cur_catalog->thash_size);
4837c478bd9Sstevel@tonic-gate 	cur_catalog->thash[thash_idx] = nmsg + 1;
4847c478bd9Sstevel@tonic-gate 	cur_catalog->nmsg++;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (cur_catalog->nmsg >= cur_catalog->msg_size) {
4877c478bd9Sstevel@tonic-gate 		/* no vacancy in message array */
4887c478bd9Sstevel@tonic-gate 		cur_catalog->msg_size += DEF_MSG_NUM;
4897c478bd9Sstevel@tonic-gate 		cur_catalog->msg = Xrealloc(cur_catalog->msg,
4907c478bd9Sstevel@tonic-gate 		    cur_catalog->msg_size * sizeof (struct messages));
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		cur_catalog->thash_size =
4937c478bd9Sstevel@tonic-gate 			find_prime(cur_catalog->msg_size);
4947c478bd9Sstevel@tonic-gate 		free(cur_catalog->thash);
4957c478bd9Sstevel@tonic-gate 		cur_catalog->thash = Xcalloc(cur_catalog->thash_size,
4967c478bd9Sstevel@tonic-gate 		    sizeof (unsigned int));
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		for (n = 0; n < cur_catalog->nmsg; n++) {
4997c478bd9Sstevel@tonic-gate 			thash_idx = get_hash_index(cur_catalog->thash,
5007c478bd9Sstevel@tonic-gate 			    cur_catalog->msg[n].hash,
5017c478bd9Sstevel@tonic-gate 			    cur_catalog->thash_size);
5027c478bd9Sstevel@tonic-gate 			cur_catalog->thash[thash_idx] = n + 1;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate void
5087c478bd9Sstevel@tonic-gate po_init(const char *file)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	char	*filename;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (!inputdir) {
5137c478bd9Sstevel@tonic-gate 		filename = Xstrdup(file);
5147c478bd9Sstevel@tonic-gate 	} else {
5157c478bd9Sstevel@tonic-gate 		size_t	dirlen, filelen, len;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		dirlen = strlen(inputdir);
5187c478bd9Sstevel@tonic-gate 		filelen = strlen(file);
5197c478bd9Sstevel@tonic-gate 		len = dirlen + 1 + filelen + 1;
5207c478bd9Sstevel@tonic-gate 		filename = Xmalloc(len);
5217c478bd9Sstevel@tonic-gate 		(void) memcpy(filename, inputdir, dirlen);
5227c478bd9Sstevel@tonic-gate 		*(filename + dirlen) = '/';
5237c478bd9Sstevel@tonic-gate 		(void) memcpy(filename + dirlen + 1, file, filelen);
5247c478bd9Sstevel@tonic-gate 		*(filename + dirlen + 1 + filelen) = '\0';
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	fp = fopen(filename, "r");
5287c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
5297c478bd9Sstevel@tonic-gate 		error(gettext(ERR_OPEN_FAILED), filename);
5307c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	po_names[cur_po_index] = filename;
5347c478bd9Sstevel@tonic-gate 	cur_line = 1;
5357c478bd9Sstevel@tonic-gate 	cd = (iconv_t)-1;
5367c478bd9Sstevel@tonic-gate 	if (!outfile)
5377c478bd9Sstevel@tonic-gate 		cur_mo = NULL;
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate void
5417c478bd9Sstevel@tonic-gate po_fini(void)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate 	cur_po_index++;
5447c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5457c478bd9Sstevel@tonic-gate 	if (cd != (iconv_t)-1)
5467c478bd9Sstevel@tonic-gate 		(void) iconv_close(cd);
5477c478bd9Sstevel@tonic-gate }
548