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