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