2 * Copyright (c) 2014, 2015, Juniper Networks, Inc.
4 * This SOFTWARE is licensed under the LICENSE provided in the
5 * ../Copyright file. By downloading, installing, copying, or otherwise
6 * using the SOFTWARE, you agree to be bound by the terms of that
8 * Phil Shafer, July 2015
17 #include <sys/queue.h>
19 #include "xo_config.h"
22 #include <getopt.h> /* Include after xo.h for testing */
25 #define UNUSED __attribute__ ((__unused__))
28 static int opt_warn; /* Enable warnings */
29 static int opt_numbers; /* Number our fields */
31 typedef struct xopo_msg_s {
32 TAILQ_ENTRY(xopo_msg_s) xm_link;
33 char *xm_plural; /* If plural, points to the second part */
34 char xm_data[0]; /* Start of data */
37 typedef TAILQ_HEAD(xopo_msg_list_s, xopo_msg_s) xopo_msg_list_t;
39 static xopo_msg_list_t field_list;
42 xopo_msg_cb (const char *str, unsigned len, int plural)
44 int sz = sizeof(xopo_msg_t) + len + 1;
45 xopo_msg_t *xmp = malloc(sz);
50 memcpy(xmp->xm_data, str, len);
51 xmp->xm_data[len] = '\0';
54 char *cp = strchr(xmp->xm_data, ',');
63 TAILQ_FOREACH(xmp2, &field_list, xm_link) {
64 if (strcmp(xmp->xm_data, xmp2->xm_data) == 0) {
65 /* Houston, we have a negative on that trajectory */
71 TAILQ_INSERT_TAIL(&field_list, xmp, xm_link);
77 fprintf(stderr, "libxo version %s%s\n",
78 xo_version, xo_version_extra);
79 fprintf(stderr, "xopo version %s%s\n",
80 LIBXO_VERSION, LIBXO_VERSION_EXTRA);
87 "Usage: xopo [options] format [fields]\n"
88 " --help Display this help text\n"
89 " --option <opts> -or -O <opts> Give formatting options\n"
90 " --output <file> -or -o <file> Use file as output destination\n"
91 " --po <file> or -f <file> Generate new msgid's for a po file\n"
92 " --simplify <text> OR -s <text> Show simplified form of the format string\n"
93 " --version Display version information\n"
94 " --warn OR -W Display warnings in text on stderr\n"
103 static struct option long_opts[] = {
104 { "help", no_argument, &opts.o_help, 1 },
105 { "number", no_argument, NULL, 'n' },
106 { "option", required_argument, NULL, 'O' },
107 { "output", required_argument, NULL, 'o' },
108 { "po", required_argument, NULL, 'f' },
109 { "simplify", no_argument, NULL, 'S' },
110 { "warn", no_argument, NULL, 'W' },
115 main (int argc UNUSED, char **argv)
117 char *opt_options = NULL;
118 char *opt_input = NULL;
119 char *opt_output = NULL;
120 char *opt_simplify = NULL;
123 argc = xo_parse_args(argc, argv);
127 while ((rc = getopt_long(argc, argv, "f:no:O:s:W",
128 long_opts, NULL)) != -1) {
143 opt_options = optarg;
147 opt_simplify = optarg;
152 xo_set_flags(NULL, XOF_WARN);
156 xo_errx(1, "missing argument");
164 } else if (opts.o_version) {
173 bzero(&opts, sizeof(opts)); /* Reset all the options */
186 rc = xo_set_options(NULL, opt_options);
188 xo_errx(1, "invalid options: %s", opt_options);
192 char *fmt = xo_simplify_format(NULL, opt_simplify, opt_numbers, NULL);
194 xo_emit("{:format}\n", fmt);
200 static char msgid[] = "msgid ";
201 char buf[BUFSIZ], *cp, *ep;
204 TAILQ_INIT(&field_list);
207 infile = fopen(opt_input, "r");
209 xo_emit_err(1, "count not open input file: '{:filename}'",
216 outfile = fopen(opt_output, "w");
218 xo_emit_err(1, "count not open output file: '{:filename}'",
225 for (line = 1;; line++) {
226 if (fgets(buf, sizeof(buf), infile) == NULL)
229 if (buf[0] == '#' && buf[1] == '\n')
232 blank = (buf[0] == '\n' && buf[1] == '\0');
234 if (strncmp(buf, msgid, sizeof(msgid) - 1) != 0) {
235 fprintf(outfile, "%s", buf);
239 for (cp = buf + sizeof(msgid); *cp; cp++)
240 if (!isspace((int) *cp))
246 ep = cp + strlen(cp);
250 while (isspace((int) *ep) && ep > cp)
258 cp = xo_simplify_format(NULL, cp, opt_numbers, xopo_msg_cb);
260 fprintf(outfile, "msgid \"%s\"\n", cp);
266 fprintf(outfile, "\n");
269 TAILQ_FOREACH(xmp, &field_list, xm_link) {
270 if (xmp->xm_plural) {
271 fprintf(outfile, "msgid \"%s\"\n"
272 "msgid_plural \"%s\"\n"
274 "msgstr[1] \"\"\n\n",
275 xmp->xm_data, xmp->xm_plural);
277 fprintf(outfile, "msgid \"%s\"\nmsgstr \"\"\n\n", xmp->xm_data);
283 if (outfile != stdout)