8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / cmd / gettext / gettext.c
bloba4b49c1d3a1c73d7c0a874b1786c12760f17d1ec
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 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <locale.h>
44 * TEXTDOMAIN should be defined in Makefile
45 * in case it isn't, define it here
47 #if !defined(TEXT_DOMAIN)
48 #define TEXT_DOMAIN "SYS_TEST"
49 #endif
51 static char *
52 expand_metas(char *in) /* walk thru string interpreting \n etc. */
54 register char *out, *cp;
56 for (cp = out = in; *in != NULL; out++, in++) {
57 if (*in == '\\') {
58 switch (*++in) {
59 case 'b' :
60 *out = '\b';
61 break;
62 case 'f' :
63 *out = '\f';
64 break;
65 case 'n' :
66 *out = '\n';
67 break;
68 case 'r' :
69 *out = '\r';
70 break;
71 case 't' :
72 *out = '\t';
73 break;
74 case 'v' :
75 *out = '\v';
76 break;
77 default:
78 *out = *in;
79 break;
81 } else
82 *out = *in;
84 *out = NULL;
85 return (cp);
88 #define ERR_USAGE \
89 "Usage: gettext [-d domainname | --domain=domainname ] " \
90 "[domain] \"msgid\"\n" \
91 " gettext -s [-d domainname | --domain=domainname] [-e] [-n] "\
92 "\"msgid\" ...\n"
94 static void
95 usage(void) {
96 (void) fprintf(stderr, gettext(ERR_USAGE));
97 exit(-1);
101 main(int argc, char *argv[]) /* shell script equivalent of gettext(3) */
103 char *domainpath, *msgid;
104 char *domain = NULL;
105 char c, *arg;
106 int exp_flag = 0;
107 int no_newline = 0;
108 int echo_flag = 0;
110 (void) setlocale(LC_ALL, "");
111 (void) textdomain(TEXT_DOMAIN);
113 argv++;
114 while (--argc > 1) {
115 arg = *argv;
116 if (*arg == '-') {
117 if (!*(arg + 1)) {
118 /* not an option */
119 break;
121 loop:
122 if ((c = *++arg) == '\0') {
123 /* next argument */
124 argv++;
125 continue;
126 } else if (c != '-') {
127 switch (c) {
128 case 'd':
129 /* domainname */
130 if (*(arg + 1)) {
132 * no spaces between -d and
133 * optarg
135 domain = ++arg;
136 argv++;
137 continue;
139 if (--argc > 1) {
140 domain = *++argv;
141 argv++;
142 continue;
144 /* not enough args */
145 usage();
146 /* NOTREACHED */
147 break;
148 case 'e':
149 /* enable escape sequence expansion */
150 exp_flag = 1;
151 goto loop;
152 /* NOTREACHED */
153 case 'n':
154 /* suppress tailing newline */
155 no_newline = 1;
156 goto loop;
157 /* NOTREACHED */
158 case 's':
159 echo_flag = 1;
160 goto loop;
161 /* NOTREACHED */
162 default:
163 /* illegal option */
164 usage();
165 /* NOTREACHED */
166 break;
168 /* NOTREACHED */
170 /* c == '-' */
171 if (*(arg + 1) == '\0') {
172 /* "--" found, option end */
173 argv++;
174 argc--;
175 break;
178 /* long option */
179 arg++;
180 if (strncmp(arg, "domain=", 7) == 0) {
181 /* domainname */
182 arg += 7;
183 if (*arg == '\0') {
184 /* illegal option */
185 usage();
186 /* NOTREACHED */
188 domain = arg;
189 argv++;
190 continue;
192 /* illegal option */
193 usage();
194 /* NOTREACHED */
196 break;
198 if (argc == 0) {
199 usage();
200 /* NOTREACHED */
203 domainpath = getenv("TEXTDOMAINDIR");
204 if (!echo_flag) {
205 /* traditional mode */
206 if (argc == 2) {
208 * textdomain is specified by the argument.
210 domain = *argv++;
211 } else if (!domain) {
213 * textdomain is not specified by the argument.
214 * TEXTDOMAIN will be used.
216 domain = getenv("TEXTDOMAIN");
217 if (!domain) {
219 * no domain specified
220 * Just print the argument given.
222 (void) printf("%s", expand_metas(*argv));
223 exit(1);
226 if (domainpath) {
227 (void) bindtextdomain(domain, domainpath);
229 msgid = expand_metas(*argv);
230 (void) fputs(dgettext(domain, msgid), stdout);
231 exit(*domain == NULL);
233 /* echo mode */
234 if (!domain) {
235 domain = getenv("TEXTDOMAIN");
237 if (domainpath && domain) {
238 (void) bindtextdomain(domain, domainpath);
240 while (argc-- > 0) {
241 if (exp_flag)
242 msgid = expand_metas(*argv++);
243 else
244 msgid = *argv++;
245 (void) fputs(domain ? dgettext(domain, msgid) : msgid,
246 stdout);
248 if (argc > 0)
249 (void) fputc(' ', stdout);
251 if (!no_newline)
252 (void) fputc('\n', stdout);
254 return ((domain == NULL) || (*domain == NULL));