xref: /titanic_52/usr/src/cmd/gettext/gettext.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <stdio.h>
40*7c478bd9Sstevel@tonic-gate #include <string.h>
41*7c478bd9Sstevel@tonic-gate #include <locale.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * TEXTDOMAIN should be defined in Makefile
45*7c478bd9Sstevel@tonic-gate  * in case it isn't, define it here
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
48*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
49*7c478bd9Sstevel@tonic-gate #endif
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static char *
52*7c478bd9Sstevel@tonic-gate expand_metas(char *in)	/* walk thru string interpreting \n etc. */
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	register char *out, *cp;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	for (cp = out = in; *in != NULL; out++, in++) {
57*7c478bd9Sstevel@tonic-gate 		if (*in == '\\') {
58*7c478bd9Sstevel@tonic-gate 			switch (*++in) {
59*7c478bd9Sstevel@tonic-gate 			case 'b' :
60*7c478bd9Sstevel@tonic-gate 				*out = '\b';
61*7c478bd9Sstevel@tonic-gate 				break;
62*7c478bd9Sstevel@tonic-gate 			case 'f' :
63*7c478bd9Sstevel@tonic-gate 				*out = '\f';
64*7c478bd9Sstevel@tonic-gate 				break;
65*7c478bd9Sstevel@tonic-gate 			case 'n' :
66*7c478bd9Sstevel@tonic-gate 				*out = '\n';
67*7c478bd9Sstevel@tonic-gate 				break;
68*7c478bd9Sstevel@tonic-gate 			case 'r' :
69*7c478bd9Sstevel@tonic-gate 				*out = '\r';
70*7c478bd9Sstevel@tonic-gate 				break;
71*7c478bd9Sstevel@tonic-gate 			case 't' :
72*7c478bd9Sstevel@tonic-gate 				*out = '\t';
73*7c478bd9Sstevel@tonic-gate 				break;
74*7c478bd9Sstevel@tonic-gate 			case 'v' :
75*7c478bd9Sstevel@tonic-gate 				*out = '\v';
76*7c478bd9Sstevel@tonic-gate 				break;
77*7c478bd9Sstevel@tonic-gate 			default:
78*7c478bd9Sstevel@tonic-gate 				*out = *in;
79*7c478bd9Sstevel@tonic-gate 				break;
80*7c478bd9Sstevel@tonic-gate 			}
81*7c478bd9Sstevel@tonic-gate 		} else
82*7c478bd9Sstevel@tonic-gate 			*out = *in;
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 	*out = NULL;
85*7c478bd9Sstevel@tonic-gate 	return (cp);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate #define	ERR_USAGE \
89*7c478bd9Sstevel@tonic-gate 	"Usage: gettext [-d domainname | --domain=domainname ]  " \
90*7c478bd9Sstevel@tonic-gate 		"[domain] \"msgid\"\n" \
91*7c478bd9Sstevel@tonic-gate 	"       gettext -s [-d domainname | --domain=domainname] [-e] [-n] "\
92*7c478bd9Sstevel@tonic-gate 		"\"msgid\" ...\n"
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate static void
95*7c478bd9Sstevel@tonic-gate usage(void) {
96*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(ERR_USAGE));
97*7c478bd9Sstevel@tonic-gate 	exit(-1);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate int
101*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])	/* shell script equivalent of gettext(3) */
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	char	*domainpath, *msgid;
104*7c478bd9Sstevel@tonic-gate 	char	*domain = NULL;
105*7c478bd9Sstevel@tonic-gate 	char	c, *arg;
106*7c478bd9Sstevel@tonic-gate 	int	exp_flag = 0;
107*7c478bd9Sstevel@tonic-gate 	int	no_newline = 0;
108*7c478bd9Sstevel@tonic-gate 	int	echo_flag = 0;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
111*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	argv++;
114*7c478bd9Sstevel@tonic-gate 	while (--argc > 1) {
115*7c478bd9Sstevel@tonic-gate 		arg = *argv;
116*7c478bd9Sstevel@tonic-gate 		if (*arg == '-') {
117*7c478bd9Sstevel@tonic-gate 			if (!*(arg + 1)) {
118*7c478bd9Sstevel@tonic-gate 				/* not an option */
119*7c478bd9Sstevel@tonic-gate 				break;
120*7c478bd9Sstevel@tonic-gate 			}
121*7c478bd9Sstevel@tonic-gate loop:
122*7c478bd9Sstevel@tonic-gate 			if ((c = *++arg) == '\0') {
123*7c478bd9Sstevel@tonic-gate 				/* next argument */
124*7c478bd9Sstevel@tonic-gate 				argv++;
125*7c478bd9Sstevel@tonic-gate 				continue;
126*7c478bd9Sstevel@tonic-gate 			} else if (c != '-') {
127*7c478bd9Sstevel@tonic-gate 				switch (c) {
128*7c478bd9Sstevel@tonic-gate 				case 'd':
129*7c478bd9Sstevel@tonic-gate 					/* domainname */
130*7c478bd9Sstevel@tonic-gate 					if (*(arg + 1)) {
131*7c478bd9Sstevel@tonic-gate 						/*
132*7c478bd9Sstevel@tonic-gate 						 * no spaces between -d and
133*7c478bd9Sstevel@tonic-gate 						 * optarg
134*7c478bd9Sstevel@tonic-gate 						 */
135*7c478bd9Sstevel@tonic-gate 						domain = ++arg;
136*7c478bd9Sstevel@tonic-gate 						argv++;
137*7c478bd9Sstevel@tonic-gate 						continue;
138*7c478bd9Sstevel@tonic-gate 					}
139*7c478bd9Sstevel@tonic-gate 					if (--argc > 1) {
140*7c478bd9Sstevel@tonic-gate 						domain = *++argv;
141*7c478bd9Sstevel@tonic-gate 						argv++;
142*7c478bd9Sstevel@tonic-gate 						continue;
143*7c478bd9Sstevel@tonic-gate 					}
144*7c478bd9Sstevel@tonic-gate 					/* not enough args */
145*7c478bd9Sstevel@tonic-gate 					usage();
146*7c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
147*7c478bd9Sstevel@tonic-gate 					break;
148*7c478bd9Sstevel@tonic-gate 				case 'e':
149*7c478bd9Sstevel@tonic-gate 					/* enable escape sequence expansion */
150*7c478bd9Sstevel@tonic-gate 					exp_flag = 1;
151*7c478bd9Sstevel@tonic-gate 					goto loop;
152*7c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
153*7c478bd9Sstevel@tonic-gate 				case 'n':
154*7c478bd9Sstevel@tonic-gate 					/* suppress tailing newline */
155*7c478bd9Sstevel@tonic-gate 					no_newline = 1;
156*7c478bd9Sstevel@tonic-gate 					goto loop;
157*7c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
158*7c478bd9Sstevel@tonic-gate 				case 's':
159*7c478bd9Sstevel@tonic-gate 					echo_flag = 1;
160*7c478bd9Sstevel@tonic-gate 					goto loop;
161*7c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
162*7c478bd9Sstevel@tonic-gate 				default:
163*7c478bd9Sstevel@tonic-gate 					/* illegal option */
164*7c478bd9Sstevel@tonic-gate 					usage();
165*7c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
166*7c478bd9Sstevel@tonic-gate 					break;
167*7c478bd9Sstevel@tonic-gate 				}
168*7c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
169*7c478bd9Sstevel@tonic-gate 			}
170*7c478bd9Sstevel@tonic-gate 			/* c == '-' */
171*7c478bd9Sstevel@tonic-gate 			if (*(arg + 1) == '\0') {
172*7c478bd9Sstevel@tonic-gate 				/* "--" found, option end */
173*7c478bd9Sstevel@tonic-gate 				argv++;
174*7c478bd9Sstevel@tonic-gate 				argc--;
175*7c478bd9Sstevel@tonic-gate 				break;
176*7c478bd9Sstevel@tonic-gate 			}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 			/* long option */
179*7c478bd9Sstevel@tonic-gate 			arg++;
180*7c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "domain=", 7) == 0) {
181*7c478bd9Sstevel@tonic-gate 				/* domainname */
182*7c478bd9Sstevel@tonic-gate 				arg += 7;
183*7c478bd9Sstevel@tonic-gate 				if (*arg == '\0') {
184*7c478bd9Sstevel@tonic-gate 					/* illegal option */
185*7c478bd9Sstevel@tonic-gate 					usage();
186*7c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
187*7c478bd9Sstevel@tonic-gate 				}
188*7c478bd9Sstevel@tonic-gate 				domain = arg;
189*7c478bd9Sstevel@tonic-gate 				argv++;
190*7c478bd9Sstevel@tonic-gate 				continue;
191*7c478bd9Sstevel@tonic-gate 			}
192*7c478bd9Sstevel@tonic-gate 			/* illegal option */
193*7c478bd9Sstevel@tonic-gate 			usage();
194*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
195*7c478bd9Sstevel@tonic-gate 		}
196*7c478bd9Sstevel@tonic-gate 		break;
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 	if (argc == 0) {
199*7c478bd9Sstevel@tonic-gate 		usage();
200*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
201*7c478bd9Sstevel@tonic-gate 	}
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	domainpath = getenv("TEXTDOMAINDIR");
204*7c478bd9Sstevel@tonic-gate 	if (!echo_flag) {
205*7c478bd9Sstevel@tonic-gate 		/* traditional mode */
206*7c478bd9Sstevel@tonic-gate 		if (argc == 2) {
207*7c478bd9Sstevel@tonic-gate 			/*
208*7c478bd9Sstevel@tonic-gate 			 * textdomain is specified by the argument.
209*7c478bd9Sstevel@tonic-gate 			 */
210*7c478bd9Sstevel@tonic-gate 			domain = *argv++;
211*7c478bd9Sstevel@tonic-gate 		} else if (!domain) {
212*7c478bd9Sstevel@tonic-gate 			/*
213*7c478bd9Sstevel@tonic-gate 			 * textdomain is not specified by the argument.
214*7c478bd9Sstevel@tonic-gate 			 * TEXTDOMAIN will be used.
215*7c478bd9Sstevel@tonic-gate 			 */
216*7c478bd9Sstevel@tonic-gate 			domain = getenv("TEXTDOMAIN");
217*7c478bd9Sstevel@tonic-gate 			if (!domain) {
218*7c478bd9Sstevel@tonic-gate 				/*
219*7c478bd9Sstevel@tonic-gate 				 * no domain specified
220*7c478bd9Sstevel@tonic-gate 				 * Just print the argument given.
221*7c478bd9Sstevel@tonic-gate 				 */
222*7c478bd9Sstevel@tonic-gate 				(void) printf("%s", expand_metas(*argv));
223*7c478bd9Sstevel@tonic-gate 				exit(1);
224*7c478bd9Sstevel@tonic-gate 			}
225*7c478bd9Sstevel@tonic-gate 		}
226*7c478bd9Sstevel@tonic-gate 		if (domainpath) {
227*7c478bd9Sstevel@tonic-gate 			(void) bindtextdomain(domain, domainpath);
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 		msgid = expand_metas(*argv);
230*7c478bd9Sstevel@tonic-gate 		(void) fputs(dgettext(domain, msgid), stdout);
231*7c478bd9Sstevel@tonic-gate 		exit(*domain == NULL);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 	/* echo mode */
234*7c478bd9Sstevel@tonic-gate 	if (!domain) {
235*7c478bd9Sstevel@tonic-gate 		domain = getenv("TEXTDOMAIN");
236*7c478bd9Sstevel@tonic-gate 	}
237*7c478bd9Sstevel@tonic-gate 	if (domainpath && domain) {
238*7c478bd9Sstevel@tonic-gate 		(void) bindtextdomain(domain, domainpath);
239*7c478bd9Sstevel@tonic-gate 	}
240*7c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
241*7c478bd9Sstevel@tonic-gate 		if (exp_flag)
242*7c478bd9Sstevel@tonic-gate 			msgid = expand_metas(*argv++);
243*7c478bd9Sstevel@tonic-gate 		else
244*7c478bd9Sstevel@tonic-gate 			msgid = *argv++;
245*7c478bd9Sstevel@tonic-gate 		(void) fputs(domain ? dgettext(domain, msgid) : msgid,
246*7c478bd9Sstevel@tonic-gate 			stdout);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 		if (argc > 0)
249*7c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 	if (!no_newline)
252*7c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stdout);
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	return ((domain == NULL) || (*domain == NULL));
255*7c478bd9Sstevel@tonic-gate }
256