xref: /illumos-gate/usr/src/cmd/gettext/gettext.c (revision 2e837a72)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*2e837a72SToomas Soome /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <locale.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * TEXTDOMAIN should be defined in Makefile
437c478bd9Sstevel@tonic-gate  * in case it isn't, define it here
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
467c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static char *
expand_metas(char * in)507c478bd9Sstevel@tonic-gate expand_metas(char *in)	/* walk thru string interpreting \n etc. */
517c478bd9Sstevel@tonic-gate {
52*2e837a72SToomas Soome 	char *out, *cp;
537c478bd9Sstevel@tonic-gate 
54*2e837a72SToomas Soome 	for (cp = out = in; *in != '\0'; out++, in++) {
557c478bd9Sstevel@tonic-gate 		if (*in == '\\') {
567c478bd9Sstevel@tonic-gate 			switch (*++in) {
577c478bd9Sstevel@tonic-gate 			case 'b' :
587c478bd9Sstevel@tonic-gate 				*out = '\b';
597c478bd9Sstevel@tonic-gate 				break;
607c478bd9Sstevel@tonic-gate 			case 'f' :
617c478bd9Sstevel@tonic-gate 				*out = '\f';
627c478bd9Sstevel@tonic-gate 				break;
637c478bd9Sstevel@tonic-gate 			case 'n' :
647c478bd9Sstevel@tonic-gate 				*out = '\n';
657c478bd9Sstevel@tonic-gate 				break;
667c478bd9Sstevel@tonic-gate 			case 'r' :
677c478bd9Sstevel@tonic-gate 				*out = '\r';
687c478bd9Sstevel@tonic-gate 				break;
697c478bd9Sstevel@tonic-gate 			case 't' :
707c478bd9Sstevel@tonic-gate 				*out = '\t';
717c478bd9Sstevel@tonic-gate 				break;
727c478bd9Sstevel@tonic-gate 			case 'v' :
737c478bd9Sstevel@tonic-gate 				*out = '\v';
747c478bd9Sstevel@tonic-gate 				break;
757c478bd9Sstevel@tonic-gate 			default:
767c478bd9Sstevel@tonic-gate 				*out = *in;
777c478bd9Sstevel@tonic-gate 				break;
787c478bd9Sstevel@tonic-gate 			}
797c478bd9Sstevel@tonic-gate 		} else
807c478bd9Sstevel@tonic-gate 			*out = *in;
817c478bd9Sstevel@tonic-gate 	}
82*2e837a72SToomas Soome 	*out = '\0';
837c478bd9Sstevel@tonic-gate 	return (cp);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	ERR_USAGE \
877c478bd9Sstevel@tonic-gate 	"Usage: gettext [-d domainname | --domain=domainname ]  " \
887c478bd9Sstevel@tonic-gate 		"[domain] \"msgid\"\n" \
897c478bd9Sstevel@tonic-gate 	"       gettext -s [-d domainname | --domain=domainname] [-e] [-n] "\
907c478bd9Sstevel@tonic-gate 		"\"msgid\" ...\n"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static void
usage(void)93*2e837a72SToomas Soome usage(void)
94*2e837a72SToomas Soome {
957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(ERR_USAGE));
967c478bd9Sstevel@tonic-gate 	exit(-1);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1007c478bd9Sstevel@tonic-gate main(int argc, char *argv[])	/* shell script equivalent of gettext(3) */
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	char	*domainpath, *msgid;
1037c478bd9Sstevel@tonic-gate 	char	*domain = NULL;
1047c478bd9Sstevel@tonic-gate 	char	c, *arg;
1057c478bd9Sstevel@tonic-gate 	int	exp_flag = 0;
1067c478bd9Sstevel@tonic-gate 	int	no_newline = 0;
1077c478bd9Sstevel@tonic-gate 	int	echo_flag = 0;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1107c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	argv++;
1137c478bd9Sstevel@tonic-gate 	while (--argc > 1) {
1147c478bd9Sstevel@tonic-gate 		arg = *argv;
1157c478bd9Sstevel@tonic-gate 		if (*arg == '-') {
1167c478bd9Sstevel@tonic-gate 			if (!*(arg + 1)) {
1177c478bd9Sstevel@tonic-gate 				/* not an option */
1187c478bd9Sstevel@tonic-gate 				break;
1197c478bd9Sstevel@tonic-gate 			}
1207c478bd9Sstevel@tonic-gate loop:
1217c478bd9Sstevel@tonic-gate 			if ((c = *++arg) == '\0') {
1227c478bd9Sstevel@tonic-gate 				/* next argument */
1237c478bd9Sstevel@tonic-gate 				argv++;
1247c478bd9Sstevel@tonic-gate 				continue;
1257c478bd9Sstevel@tonic-gate 			} else if (c != '-') {
1267c478bd9Sstevel@tonic-gate 				switch (c) {
1277c478bd9Sstevel@tonic-gate 				case 'd':
1287c478bd9Sstevel@tonic-gate 					/* domainname */
1297c478bd9Sstevel@tonic-gate 					if (*(arg + 1)) {
1307c478bd9Sstevel@tonic-gate 						/*
1317c478bd9Sstevel@tonic-gate 						 * no spaces between -d and
1327c478bd9Sstevel@tonic-gate 						 * optarg
1337c478bd9Sstevel@tonic-gate 						 */
1347c478bd9Sstevel@tonic-gate 						domain = ++arg;
1357c478bd9Sstevel@tonic-gate 						argv++;
1367c478bd9Sstevel@tonic-gate 						continue;
1377c478bd9Sstevel@tonic-gate 					}
1387c478bd9Sstevel@tonic-gate 					if (--argc > 1) {
1397c478bd9Sstevel@tonic-gate 						domain = *++argv;
1407c478bd9Sstevel@tonic-gate 						argv++;
1417c478bd9Sstevel@tonic-gate 						continue;
1427c478bd9Sstevel@tonic-gate 					}
1437c478bd9Sstevel@tonic-gate 					/* not enough args */
1447c478bd9Sstevel@tonic-gate 					usage();
1457c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
1467c478bd9Sstevel@tonic-gate 					break;
1477c478bd9Sstevel@tonic-gate 				case 'e':
1487c478bd9Sstevel@tonic-gate 					/* enable escape sequence expansion */
1497c478bd9Sstevel@tonic-gate 					exp_flag = 1;
1507c478bd9Sstevel@tonic-gate 					goto loop;
1517c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
1527c478bd9Sstevel@tonic-gate 				case 'n':
1537c478bd9Sstevel@tonic-gate 					/* suppress tailing newline */
1547c478bd9Sstevel@tonic-gate 					no_newline = 1;
1557c478bd9Sstevel@tonic-gate 					goto loop;
1567c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
1577c478bd9Sstevel@tonic-gate 				case 's':
1587c478bd9Sstevel@tonic-gate 					echo_flag = 1;
1597c478bd9Sstevel@tonic-gate 					goto loop;
1607c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
1617c478bd9Sstevel@tonic-gate 				default:
1627c478bd9Sstevel@tonic-gate 					/* illegal option */
1637c478bd9Sstevel@tonic-gate 					usage();
1647c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
1657c478bd9Sstevel@tonic-gate 					break;
1667c478bd9Sstevel@tonic-gate 				}
1677c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 			/* c == '-' */
1707c478bd9Sstevel@tonic-gate 			if (*(arg + 1) == '\0') {
1717c478bd9Sstevel@tonic-gate 				/* "--" found, option end */
1727c478bd9Sstevel@tonic-gate 				argv++;
1737c478bd9Sstevel@tonic-gate 				argc--;
1747c478bd9Sstevel@tonic-gate 				break;
1757c478bd9Sstevel@tonic-gate 			}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 			/* long option */
1787c478bd9Sstevel@tonic-gate 			arg++;
1797c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "domain=", 7) == 0) {
1807c478bd9Sstevel@tonic-gate 				/* domainname */
1817c478bd9Sstevel@tonic-gate 				arg += 7;
1827c478bd9Sstevel@tonic-gate 				if (*arg == '\0') {
1837c478bd9Sstevel@tonic-gate 					/* illegal option */
1847c478bd9Sstevel@tonic-gate 					usage();
1857c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
1867c478bd9Sstevel@tonic-gate 				}
1877c478bd9Sstevel@tonic-gate 				domain = arg;
1887c478bd9Sstevel@tonic-gate 				argv++;
1897c478bd9Sstevel@tonic-gate 				continue;
1907c478bd9Sstevel@tonic-gate 			}
1917c478bd9Sstevel@tonic-gate 			/* illegal option */
1927c478bd9Sstevel@tonic-gate 			usage();
1937c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		break;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	if (argc == 0) {
1987c478bd9Sstevel@tonic-gate 		usage();
1997c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	domainpath = getenv("TEXTDOMAINDIR");
2037c478bd9Sstevel@tonic-gate 	if (!echo_flag) {
2047c478bd9Sstevel@tonic-gate 		/* traditional mode */
2057c478bd9Sstevel@tonic-gate 		if (argc == 2) {
2067c478bd9Sstevel@tonic-gate 			/*
2077c478bd9Sstevel@tonic-gate 			 * textdomain is specified by the argument.
2087c478bd9Sstevel@tonic-gate 			 */
2097c478bd9Sstevel@tonic-gate 			domain = *argv++;
2107c478bd9Sstevel@tonic-gate 		} else if (!domain) {
2117c478bd9Sstevel@tonic-gate 			/*
2127c478bd9Sstevel@tonic-gate 			 * textdomain is not specified by the argument.
2137c478bd9Sstevel@tonic-gate 			 * TEXTDOMAIN will be used.
2147c478bd9Sstevel@tonic-gate 			 */
2157c478bd9Sstevel@tonic-gate 			domain = getenv("TEXTDOMAIN");
2167c478bd9Sstevel@tonic-gate 			if (!domain) {
2177c478bd9Sstevel@tonic-gate 				/*
2187c478bd9Sstevel@tonic-gate 				 * no domain specified
2197c478bd9Sstevel@tonic-gate 				 * Just print the argument given.
2207c478bd9Sstevel@tonic-gate 				 */
2217c478bd9Sstevel@tonic-gate 				(void) printf("%s", expand_metas(*argv));
2227c478bd9Sstevel@tonic-gate 				exit(1);
2237c478bd9Sstevel@tonic-gate 			}
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 		if (domainpath) {
2267c478bd9Sstevel@tonic-gate 			(void) bindtextdomain(domain, domainpath);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		msgid = expand_metas(*argv);
2297c478bd9Sstevel@tonic-gate 		(void) fputs(dgettext(domain, msgid), stdout);
230*2e837a72SToomas Soome 		exit(*domain == '\0');
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	/* echo mode */
2337c478bd9Sstevel@tonic-gate 	if (!domain) {
2347c478bd9Sstevel@tonic-gate 		domain = getenv("TEXTDOMAIN");
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	if (domainpath && domain) {
2377c478bd9Sstevel@tonic-gate 		(void) bindtextdomain(domain, domainpath);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
2407c478bd9Sstevel@tonic-gate 		if (exp_flag)
2417c478bd9Sstevel@tonic-gate 			msgid = expand_metas(*argv++);
2427c478bd9Sstevel@tonic-gate 		else
2437c478bd9Sstevel@tonic-gate 			msgid = *argv++;
244*2e837a72SToomas Soome 		(void) fputs(domain ? dgettext(domain, msgid) : msgid, stdout);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		if (argc > 0)
2477c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	if (!no_newline)
2507c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stdout);
2517c478bd9Sstevel@tonic-gate 
252*2e837a72SToomas Soome 	return ((domain == NULL) || (*domain == '\0'));
2537c478bd9Sstevel@tonic-gate }
254