gettext.c revision 1.3 1 1.3 christos /* $NetBSD: gettext.c,v 1.3 2015/07/12 11:40:52 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2015 William Orr <will (at) worrbase.com>
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 christos * SUCH DAMAGE.
27 1.1 christos */
28 1.1 christos #include <sys/cdefs.h>
29 1.3 christos __RCSID("$NetBSD: gettext.c,v 1.3 2015/07/12 11:40:52 christos Exp $");
30 1.1 christos
31 1.1 christos #include <err.h>
32 1.1 christos #include <errno.h>
33 1.1 christos #include <getopt.h>
34 1.1 christos #include <libintl.h>
35 1.1 christos #include <locale.h>
36 1.1 christos #include <stdbool.h>
37 1.1 christos #include <stdio.h>
38 1.1 christos #include <stdlib.h>
39 1.1 christos #include <string.h>
40 1.1 christos #include <util.h>
41 1.1 christos
42 1.3 christos static struct option longopts[] = {
43 1.3 christos { "help", no_argument, NULL, 'h' },
44 1.3 christos { "domain", required_argument, NULL, 'd' },
45 1.3 christos { NULL, 0, NULL, '\0' },
46 1.3 christos };
47 1.3 christos
48 1.1 christos static __dead void
49 1.1 christos usage(int exit_status)
50 1.1 christos {
51 1.1 christos
52 1.1 christos fprintf(stderr, "Usage: %s [-ehn] [[<textdomain>] <msgid>]\n",
53 1.1 christos getprogname());
54 1.3 christos fprintf(stderr, "Usage: %s [-ehn] -d <textdomain> <msgid>\n",
55 1.3 christos getprogname());
56 1.1 christos fprintf(stderr, "Usage: %s -s [<msgid>]...\n", getprogname());
57 1.1 christos exit(exit_status);
58 1.1 christos }
59 1.1 christos
60 1.1 christos static bool
61 1.1 christos expand(char *str)
62 1.1 christos {
63 1.1 christos char *fp, *sp, ch, pl;
64 1.1 christos bool nflag = false;
65 1.1 christos
66 1.1 christos for (fp = str, sp = str; *fp != 0;) {
67 1.1 christos if (*fp == '\\') {
68 1.1 christos switch (*++fp) {
69 1.1 christos case 'a':
70 1.1 christos *sp++ = '\a';
71 1.1 christos fp++;
72 1.1 christos break;
73 1.1 christos case 'b':
74 1.1 christos *sp++ = '\b';
75 1.1 christos fp++;
76 1.1 christos break;
77 1.1 christos case 'c':
78 1.1 christos nflag = true;
79 1.1 christos fp++;
80 1.1 christos break;
81 1.1 christos case 'f':
82 1.1 christos *sp++ = '\f';
83 1.1 christos fp++;
84 1.1 christos break;
85 1.1 christos case 'n':
86 1.1 christos *sp++ = '\n';
87 1.1 christos fp++;
88 1.1 christos break;
89 1.1 christos case 'r':
90 1.1 christos *sp++ = '\r';
91 1.1 christos fp++;
92 1.1 christos break;
93 1.1 christos case 't':
94 1.1 christos *sp++ = '\t';
95 1.1 christos fp++;
96 1.1 christos break;
97 1.1 christos case 'v':
98 1.1 christos *sp++ = '\v';
99 1.1 christos fp++;
100 1.1 christos break;
101 1.1 christos case '\\':
102 1.1 christos *sp++ = '\\';
103 1.1 christos fp++;
104 1.1 christos break;
105 1.1 christos case '0':
106 1.1 christos case '1':
107 1.1 christos case '2':
108 1.1 christos case '3':
109 1.1 christos case '4':
110 1.1 christos case '5':
111 1.1 christos case '6':
112 1.1 christos case '7':
113 1.1 christos ch = *fp++ - '0';
114 1.1 christos pl = 0;
115 1.1 christos while (*fp >= '0' && *fp <= '7' && pl < 2) {
116 1.1 christos ch *= 8;
117 1.1 christos ch += *fp++ - '0';
118 1.1 christos pl++;
119 1.1 christos }
120 1.1 christos
121 1.1 christos *sp++ = ch;
122 1.1 christos break;
123 1.1 christos default:
124 1.1 christos *sp++ = '\\';
125 1.1 christos break;
126 1.1 christos }
127 1.2 enami continue;
128 1.1 christos }
129 1.1 christos *sp++ = *fp++;
130 1.1 christos }
131 1.1 christos
132 1.1 christos *sp = '\0';
133 1.1 christos return nflag;
134 1.1 christos }
135 1.1 christos
136 1.1 christos int
137 1.1 christos main(int argc, char **argv)
138 1.1 christos {
139 1.1 christos char *msgdomain = NULL;
140 1.1 christos char *msgdomaindir = NULL;
141 1.1 christos char *translation = NULL;
142 1.1 christos char *s;
143 1.1 christos bool eflag = false;
144 1.1 christos bool sflag = false;
145 1.1 christos bool nflag = false;
146 1.1 christos int ch;
147 1.1 christos
148 1.1 christos setlocale(LC_ALL, "");
149 1.1 christos setprogname(argv[0]);
150 1.1 christos
151 1.3 christos while ((ch = getopt_long(argc, argv, "d:eEhnsV", longopts, NULL)) != -1)
152 1.3 christos {
153 1.1 christos switch (ch) {
154 1.1 christos case 'd':
155 1.1 christos msgdomain = estrdup(optarg);
156 1.1 christos break;
157 1.1 christos case 'E':
158 1.1 christos /* GNU gettext compat */
159 1.1 christos break;
160 1.1 christos case 'e':
161 1.1 christos eflag = true;
162 1.1 christos break;
163 1.1 christos case 'V':
164 1.1 christos case 'h':
165 1.1 christos free(msgdomain);
166 1.1 christos usage(EXIT_SUCCESS);
167 1.1 christos /* NOTREACHED */
168 1.1 christos case 'n':
169 1.1 christos nflag = true;
170 1.1 christos break;
171 1.1 christos case 's':
172 1.1 christos sflag = true;
173 1.1 christos break;
174 1.1 christos default:
175 1.1 christos free(msgdomain);
176 1.1 christos usage(EXIT_FAILURE);
177 1.1 christos /* NOTREACHED */
178 1.1 christos }
179 1.1 christos }
180 1.1 christos argc -= optind;
181 1.1 christos argv += optind;
182 1.1 christos
183 1.1 christos if (argc == 0) {
184 1.1 christos free(msgdomain);
185 1.3 christos warnx("missing msgid");
186 1.3 christos usage(EXIT_FAILURE);
187 1.1 christos }
188 1.1 christos
189 1.1 christos /* msgdomain can be passed as optional arg iff -s is not passed */
190 1.1 christos if (!sflag) {
191 1.1 christos if (argc == 2) {
192 1.1 christos free(msgdomain);
193 1.1 christos msgdomain = estrdup(argv[0]);
194 1.1 christos
195 1.1 christos argc -= 1;
196 1.1 christos argv += 1;
197 1.3 christos } else if (argc > 2) {
198 1.3 christos warnx("too many arguments");
199 1.3 christos usage(EXIT_FAILURE);
200 1.3 christos }
201 1.1 christos }
202 1.1 christos
203 1.1 christos /* msgdomain can be passed as env var */
204 1.1 christos if (msgdomain == NULL) {
205 1.1 christos if ((s = getenv("TEXTDOMAIN")) != NULL)
206 1.1 christos msgdomain = estrdup(s);
207 1.1 christos }
208 1.1 christos
209 1.1 christos if (msgdomain != NULL) {
210 1.1 christos if ((s = getenv("TEXTDOMAINDIR")) != NULL)
211 1.1 christos msgdomaindir = estrdup(s);
212 1.1 christos if (msgdomaindir)
213 1.1 christos bindtextdomain(msgdomain, msgdomaindir);
214 1.1 christos }
215 1.1 christos
216 1.1 christos do {
217 1.1 christos if (eflag)
218 1.1 christos nflag |= expand(*argv);
219 1.1 christos
220 1.1 christos translation = dgettext(msgdomain, argv[0]);
221 1.1 christos printf("%s", translation);
222 1.1 christos
223 1.1 christos argc--;
224 1.1 christos argv++;
225 1.1 christos if (argc)
226 1.1 christos printf(" ");
227 1.1 christos } while (sflag && argc != 0);
228 1.1 christos
229 1.1 christos if (sflag && !nflag)
230 1.1 christos printf("\n");
231 1.1 christos
232 1.1 christos free(msgdomain);
233 1.1 christos free(msgdomaindir);
234 1.1 christos
235 1.1 christos return EXIT_SUCCESS;
236 1.1 christos }
237