tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[PATCH 1/2] Add BSD-licensed gettext(1) implementation



Simple gettext implementation that models GNU gettext(1). Is
feature-complete compared to GNU gettext(1).
---
 src/usr.bin/gettext/Makefile  |  11 +++
 src/usr.bin/gettext/gettext.c | 212 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 223 insertions(+)
 create mode 100644 src/usr.bin/gettext/Makefile
 create mode 100644 src/usr.bin/gettext/gettext.c

diff --git a/src/usr.bin/gettext/Makefile b/src/usr.bin/gettext/Makefile
new file mode 100644
index 0000000..f37ec5f
--- /dev/null
+++ b/src/usr.bin/gettext/Makefile
@@ -0,0 +1,11 @@
+.include <bsd.own.mk>
+
+PROG=	gettext
+SRCS=	gettext.c
+
+LDADD+=	-lintl -lutil
+DPADD+=	${LIBINTL} ${LIBUTIL}
+
+MAN=	gettext.1
+
+.include <bsd.prog.mk>
diff --git a/src/usr.bin/gettext/gettext.c b/src/usr.bin/gettext/gettext.c
new file mode 100644
index 0000000..490c3d7
--- /dev/null
+++ b/src/usr.bin/gettext/gettext.c
@@ -0,0 +1,212 @@
+/*-
+ * Copyright (c) 2015 William Orr <will%worrbase.com@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <err.h>
+#include <errno.h>
+#include <getopt.h>
+#include <libintl.h>
+#include <locale.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <util.h>
+
+static bool nflag;
+
+static void
+usage(int exit_status)
+{
+
+	fprintf(stderr, "Usage: %s [-ehn] [[TEXTDOMAIN] MSGID]\n", getprogname());
+	fprintf(stderr, "Usage: %s -s [MSGID]...\n", getprogname());
+	exit(exit_status);
+}
+
+static void
+expand(char *str)
+{
+	char *fp, *sp, ch, pl;
+
+	for (fp = str, sp = str; *fp != 0;) {
+		if (*fp == '\\') {
+			switch (*++fp) {
+			case 'a':
+				*sp++ = '\a';
+				fp++;
+				break;
+			case 'b':
+				*sp++ = '\b';
+				fp++;
+				break;
+			case 'c':
+				nflag = true;
+				fp++;
+				break;
+			case 'f':
+				*sp++ = '\f';
+				fp++;
+				break;
+			case 'n':
+				*sp++ = '\n';
+				fp++;
+				break;
+			case 'r':
+				*sp++ = '\r';
+				fp++;
+				break;
+			case 't':
+				*sp++ = '\t';
+				fp++;
+				break;
+			case 'v':
+				*sp++ = '\v';
+				fp++;
+				break;
+			case '\\':
+				*sp++ = '\\';
+				fp++;
+				break;
+			case '0':
+			case '1':
+			case '2':
+			case '3':
+			case '4':
+			case '5':
+			case '6':
+			case '7':
+				ch = *fp++ - '0';
+				pl = 0;
+				while (*fp >= '0' && *fp <= '7' && pl < 2) {
+					ch *= 8;
+					ch += *fp++ - '0';
+					pl++;
+				}
+
+				*sp++ = ch;
+				break;
+			default:
+				*sp++ = '\\';
+				break;
+			}
+		}
+		*sp++ = *fp++;
+	}
+
+	*sp = '\0';
+}
+
+int
+main(int argc, char **argv)
+{
+	char *msgdomain = NULL;
+	char *msgdomaindir = NULL;
+	char *msgid = NULL;
+	char *translation = NULL;
+	bool eflag = false;
+	bool sflag = false;
+	int ch;
+
+	setlocale(LC_ALL, "");
+	setprogname(argv[0]);
+
+	while ((ch = getopt(argc, argv, "d:eEhnsV")) != -1) {
+		switch (ch) {
+		case 'd':
+			msgdomain = estrdup(optarg);
+			break;
+		case 'e':
+			eflag = true;
+			break;
+		case 'E':
+			/* GNU gettext compat */
+			break;
+		case 'h':
+			free(msgdomain);
+			usage(EXIT_SUCCESS);
+			/* NOTREACHED */
+		case 'n':
+			nflag = true;
+			break;
+		case 's':
+			sflag = true;
+			break;
+		default:
+			free(msgdomain);
+			usage(EXIT_FAILURE);
+			/* NOTREACHED */
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc == 0) {
+		free(msgdomain);
+		warnx("missing msgid");
+		usage(EXIT_FAILURE);
+	}
+	/* msgdomain can be passed as optional arg iff -s is not passed */
+	if (argc == 2 && !sflag) {
+		msgdomain = estrdup(argv[0]);
+
+		argc -= 1;
+		argv += 1;
+	} else if (argc > 2 && !sflag) {
+		warnx("%s: too many arguments", getprogname());
+		usage(EXIT_FAILURE);
+	}
+	/* msgdomain can be passed as env var */
+	if (msgdomain == NULL) {
+		if (getenv("TEXTDOMAIN"))
+			msgdomain = estrdup(getenv("TEXTDOMAIN"));
+	}
+
+	if (msgdomain != NULL) {
+		if (getenv("TEXTDOMAINDIR"))
+			msgdomaindir = estrdup(getenv("TEXTDOMAINDIR"));
+		if (msgdomaindir)
+			bindtextdomain(msgdomain, msgdomaindir);
+	}
+	do {
+		if (eflag)
+			expand(*argv);
+
+		translation = dgettext(msgdomain, argv[0]);
+		printf("%s", translation);
+
+		argc--;
+		argv++;
+		if (argc)
+			printf(" ");
+	} while (sflag && argc != 0);
+
+	if (sflag && !nflag)
+		printf("\n");
+
+	free(msgdomain);
+	free(msgdomaindir);
+	return EXIT_SUCCESS;
+}
-- 
2.4.2



Home | Main Index | Thread Index | Old Index