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)
118 char *opt_options = NULL;
119 char *opt_input = NULL;
120 char *opt_output = NULL;
121 char *opt_simplify = NULL;
124 argc = xo_parse_args(argc, argv);
128 while ((rc = getopt_long(argc, argv, "f:no:O:s:W",
129 long_opts, NULL)) != -1) {
144 opt_options = optarg;
148 opt_simplify = optarg;
153 xo_set_flags(NULL, XOF_WARN);
157 xo_errx(1, "missing argument");
165 } else if (opts.o_version) {
174 bzero(&opts, sizeof(opts)); /* Reset all the options */
187 rc = xo_set_options(NULL, opt_options);
189 xo_errx(1, "invalid options: %s", opt_options);
195 fmt = xo_simplify_format(NULL, opt_simplify, opt_numbers, NULL);
197 xo_emit("{:format}\n", fmt);
203 static char msgid[] = "msgid ";
204 char buf[BUFSIZ], *cp, *ep;
207 TAILQ_INIT(&field_list);
210 infile = fopen(opt_input, "r");
212 xo_emit_err(1, "count not open input file: '{:filename}'",
219 outfile = fopen(opt_output, "w");
221 xo_emit_err(1, "count not open output file: '{:filename}'",
228 for (line = 1;; line++) {
229 if (fgets(buf, sizeof(buf), infile) == NULL)
232 if (buf[0] == '#' && buf[1] == '\n')
235 blank = (buf[0] == '\n' && buf[1] == '\0');
237 if (strncmp(buf, msgid, sizeof(msgid) - 1) != 0) {
238 fprintf(outfile, "%s", buf);
242 for (cp = buf + sizeof(msgid); *cp; cp++)
243 if (!isspace((int) *cp))
249 ep = cp + strlen(cp);
253 while (isspace((int) *ep) && ep > cp)
261 cp = xo_simplify_format(NULL, cp, opt_numbers, xopo_msg_cb);
263 fprintf(outfile, "msgid \"%s\"\n", cp);
269 fprintf(outfile, "\n");
272 TAILQ_FOREACH(xmp, &field_list, xm_link) {
273 if (xmp->xm_plural) {
274 fprintf(outfile, "msgid \"%s\"\n"
275 "msgid_plural \"%s\"\n"
277 "msgstr[1] \"\"\n\n",
278 xmp->xm_data, xmp->xm_plural);
280 fprintf(outfile, "msgid \"%s\"\nmsgstr \"\"\n\n", xmp->xm_data);
286 if (outfile != stdout)