gettext.c revision 1.1 1 1.1 christos /* $NetBSD: gettext.c,v 1.1 2015/06/03 14:32:17 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.1 christos __RCSID("$NetBSD: gettext.c,v 1.1 2015/06/03 14:32:17 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.1 christos static __dead void
43 1.1 christos usage(int exit_status)
44 1.1 christos {
45 1.1 christos
46 1.1 christos fprintf(stderr, "Usage: %s [-ehn] [[<textdomain>] <msgid>]\n",
47 1.1 christos getprogname());
48 1.1 christos fprintf(stderr, "Usage: %s -s [<msgid>]...\n", getprogname());
49 1.1 christos exit(exit_status);
50 1.1 christos }
51 1.1 christos
52 1.1 christos static bool
53 1.1 christos expand(char *str)
54 1.1 christos {
55 1.1 christos char *fp, *sp, ch, pl;
56 1.1 christos bool nflag = false;
57 1.1 christos
58 1.1 christos for (fp = str, sp = str; *fp != 0;) {
59 1.1 christos if (*fp == '\\') {
60 1.1 christos switch (*++fp) {
61 1.1 christos case 'a':
62 1.1 christos *sp++ = '\a';
63 1.1 christos fp++;
64 1.1 christos break;
65 1.1 christos case 'b':
66 1.1 christos *sp++ = '\b';
67 1.1 christos fp++;
68 1.1 christos break;
69 1.1 christos case 'c':
70 1.1 christos nflag = true;
71 1.1 christos fp++;
72 1.1 christos break;
73 1.1 christos case 'f':
74 1.1 christos *sp++ = '\f';
75 1.1 christos fp++;
76 1.1 christos break;
77 1.1 christos case 'n':
78 1.1 christos *sp++ = '\n';
79 1.1 christos fp++;
80 1.1 christos break;
81 1.1 christos case 'r':
82 1.1 christos *sp++ = '\r';
83 1.1 christos fp++;
84 1.1 christos break;
85 1.1 christos case 't':
86 1.1 christos *sp++ = '\t';
87 1.1 christos fp++;
88 1.1 christos break;
89 1.1 christos case 'v':
90 1.1 christos *sp++ = '\v';
91 1.1 christos fp++;
92 1.1 christos break;
93 1.1 christos case '\\':
94 1.1 christos *sp++ = '\\';
95 1.1 christos fp++;
96 1.1 christos break;
97 1.1 christos case '0':
98 1.1 christos case '1':
99 1.1 christos case '2':
100 1.1 christos case '3':
101 1.1 christos case '4':
102 1.1 christos case '5':
103 1.1 christos case '6':
104 1.1 christos case '7':
105 1.1 christos ch = *fp++ - '0';
106 1.1 christos pl = 0;
107 1.1 christos while (*fp >= '0' && *fp <= '7' && pl < 2) {
108 1.1 christos ch *= 8;
109 1.1 christos ch += *fp++ - '0';
110 1.1 christos pl++;
111 1.1 christos }
112 1.1 christos
113 1.1 christos *sp++ = ch;
114 1.1 christos break;
115 1.1 christos default:
116 1.1 christos *sp++ = '\\';
117 1.1 christos break;
118 1.1 christos }
119 1.1 christos }
120 1.1 christos *sp++ = *fp++;
121 1.1 christos }
122 1.1 christos
123 1.1 christos *sp = '\0';
124 1.1 christos return nflag;
125 1.1 christos }
126 1.1 christos
127 1.1 christos int
128 1.1 christos main(int argc, char **argv)
129 1.1 christos {
130 1.1 christos char *msgdomain = NULL;
131 1.1 christos char *msgdomaindir = NULL;
132 1.1 christos char *translation = NULL;
133 1.1 christos char *s;
134 1.1 christos bool eflag = false;
135 1.1 christos bool sflag = false;
136 1.1 christos bool nflag = false;
137 1.1 christos int ch;
138 1.1 christos
139 1.1 christos setlocale(LC_ALL, "");
140 1.1 christos setprogname(argv[0]);
141 1.1 christos
142 1.1 christos while ((ch = getopt(argc, argv, "d:EehnsV")) != -1) {
143 1.1 christos switch (ch) {
144 1.1 christos case 'd':
145 1.1 christos msgdomain = estrdup(optarg);
146 1.1 christos break;
147 1.1 christos case 'E':
148 1.1 christos /* GNU gettext compat */
149 1.1 christos break;
150 1.1 christos case 'e':
151 1.1 christos eflag = true;
152 1.1 christos break;
153 1.1 christos case 'V':
154 1.1 christos case 'h':
155 1.1 christos free(msgdomain);
156 1.1 christos usage(EXIT_SUCCESS);
157 1.1 christos /* NOTREACHED */
158 1.1 christos case 'n':
159 1.1 christos nflag = true;
160 1.1 christos break;
161 1.1 christos case 's':
162 1.1 christos sflag = true;
163 1.1 christos break;
164 1.1 christos default:
165 1.1 christos free(msgdomain);
166 1.1 christos usage(EXIT_FAILURE);
167 1.1 christos /* NOTREACHED */
168 1.1 christos }
169 1.1 christos }
170 1.1 christos argc -= optind;
171 1.1 christos argv += optind;
172 1.1 christos
173 1.1 christos if (argc == 0) {
174 1.1 christos free(msgdomain);
175 1.1 christos errx(EXIT_FAILURE, "missing msgid");
176 1.1 christos }
177 1.1 christos
178 1.1 christos /* msgdomain can be passed as optional arg iff -s is not passed */
179 1.1 christos if (!sflag) {
180 1.1 christos if (argc == 2) {
181 1.1 christos free(msgdomain);
182 1.1 christos msgdomain = estrdup(argv[0]);
183 1.1 christos
184 1.1 christos argc -= 1;
185 1.1 christos argv += 1;
186 1.1 christos } else if (argc > 2)
187 1.1 christos errx(EXIT_FAILURE, "too many arguments");
188 1.1 christos }
189 1.1 christos
190 1.1 christos /* msgdomain can be passed as env var */
191 1.1 christos if (msgdomain == NULL) {
192 1.1 christos if ((s = getenv("TEXTDOMAIN")) != NULL)
193 1.1 christos msgdomain = estrdup(s);
194 1.1 christos }
195 1.1 christos
196 1.1 christos if (msgdomain != NULL) {
197 1.1 christos if ((s = getenv("TEXTDOMAINDIR")) != NULL)
198 1.1 christos msgdomaindir = estrdup(s);
199 1.1 christos if (msgdomaindir)
200 1.1 christos bindtextdomain(msgdomain, msgdomaindir);
201 1.1 christos }
202 1.1 christos
203 1.1 christos do {
204 1.1 christos if (eflag)
205 1.1 christos nflag |= expand(*argv);
206 1.1 christos
207 1.1 christos translation = dgettext(msgdomain, argv[0]);
208 1.1 christos printf("%s", translation);
209 1.1 christos
210 1.1 christos argc--;
211 1.1 christos argv++;
212 1.1 christos if (argc)
213 1.1 christos printf(" ");
214 1.1 christos } while (sflag && argc != 0);
215 1.1 christos
216 1.1 christos if (sflag && !nflag)
217 1.1 christos printf("\n");
218 1.1 christos
219 1.1 christos free(msgdomain);
220 1.1 christos free(msgdomaindir);
221 1.1 christos
222 1.1 christos return EXIT_SUCCESS;
223 1.1 christos }
224