dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / gettext / gettext.c
blob4f97372c21810e3a70df9238076f91f00c3a780e
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <locale.h>
42 * TEXTDOMAIN should be defined in Makefile
43 * in case it isn't, define it here
45 #if !defined(TEXT_DOMAIN)
46 #define TEXT_DOMAIN "SYS_TEST"
47 #endif
49 static char *
50 expand_metas(char *in) /* walk thru string interpreting \n etc. */
52 register char *out, *cp;
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;
79 } else
80 *out = *in;
82 *out = '\0';
83 return (cp);
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"
92 static void
93 usage(void) {
94 (void) fprintf(stderr, gettext(ERR_USAGE));
95 exit(-1);
98 int
99 main(int argc, char *argv[]) /* shell script equivalent of gettext(3) */
101 char *domainpath, *msgid;
102 char *domain = NULL;
103 char c, *arg;
104 int exp_flag = 0;
105 int no_newline = 0;
106 int echo_flag = 0;
108 (void) setlocale(LC_ALL, "");
109 (void) textdomain(TEXT_DOMAIN);
111 argv++;
112 while (--argc > 1) {
113 arg = *argv;
114 if (*arg == '-') {
115 if (!*(arg + 1)) {
116 /* not an option */
117 break;
119 loop:
120 if ((c = *++arg) == '\0') {
121 /* next argument */
122 argv++;
123 continue;
124 } else if (c != '-') {
125 switch (c) {
126 case 'd':
127 /* domainname */
128 if (*(arg + 1)) {
130 * no spaces between -d and
131 * optarg
133 domain = ++arg;
134 argv++;
135 continue;
137 if (--argc > 1) {
138 domain = *++argv;
139 argv++;
140 continue;
142 /* not enough args */
143 usage();
144 /* NOTREACHED */
145 break;
146 case 'e':
147 /* enable escape sequence expansion */
148 exp_flag = 1;
149 goto loop;
150 /* NOTREACHED */
151 case 'n':
152 /* suppress tailing newline */
153 no_newline = 1;
154 goto loop;
155 /* NOTREACHED */
156 case 's':
157 echo_flag = 1;
158 goto loop;
159 /* NOTREACHED */
160 default:
161 /* illegal option */
162 usage();
163 /* NOTREACHED */
164 break;
166 /* NOTREACHED */
168 /* c == '-' */
169 if (*(arg + 1) == '\0') {
170 /* "--" found, option end */
171 argv++;
172 argc--;
173 break;
176 /* long option */
177 arg++;
178 if (strncmp(arg, "domain=", 7) == 0) {
179 /* domainname */
180 arg += 7;
181 if (*arg == '\0') {
182 /* illegal option */
183 usage();
184 /* NOTREACHED */
186 domain = arg;
187 argv++;
188 continue;
190 /* illegal option */
191 usage();
192 /* NOTREACHED */
194 break;
196 if (argc == 0) {
197 usage();
198 /* NOTREACHED */
201 domainpath = getenv("TEXTDOMAINDIR");
202 if (!echo_flag) {
203 /* traditional mode */
204 if (argc == 2) {
206 * textdomain is specified by the argument.
208 domain = *argv++;
209 } else if (!domain) {
211 * textdomain is not specified by the argument.
212 * TEXTDOMAIN will be used.
214 domain = getenv("TEXTDOMAIN");
215 if (!domain) {
217 * no domain specified
218 * Just print the argument given.
220 (void) printf("%s", expand_metas(*argv));
221 exit(1);
224 if (domainpath) {
225 (void) bindtextdomain(domain, domainpath);
227 msgid = expand_metas(*argv);
228 (void) fputs(dgettext(domain, msgid), stdout);
229 exit(*domain == '\0');
231 /* echo mode */
232 if (!domain) {
233 domain = getenv("TEXTDOMAIN");
235 if (domainpath && domain) {
236 (void) bindtextdomain(domain, domainpath);
238 while (argc-- > 0) {
239 if (exp_flag)
240 msgid = expand_metas(*argv++);
241 else
242 msgid = *argv++;
243 (void) fputs(domain ? dgettext(domain, msgid) : msgid,
244 stdout);
246 if (argc > 0)
247 (void) fputc(' ', stdout);
249 if (!no_newline)
250 (void) fputc('\n', stdout);
252 return ((domain == NULL) || (*domain == '\0'));