summaryrefslogtreecommitdiffstats
path: root/contrib/libxo/xopo/xopo.c
blob: 991b75795ed70e2b4ee527c3844671dcc8ce7fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (c) 2014, 2015, Juniper Networks, Inc.
 * All rights reserved.
 * This SOFTWARE is licensed under the LICENSE provided in the
 * ../Copyright file. By downloading, installing, copying, or otherwise
 * using the SOFTWARE, you agree to be bound by the terms of that
 * LICENSE.
 * Phil Shafer, July 2015
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/queue.h>

#include "xo_config.h"
#include "xo.h"

#include <getopt.h>		/* Include after xo.h for testing */

#ifndef UNUSED
#define UNUSED __attribute__ ((__unused__))
#endif /* UNUSED */

static int opt_warn;		/* Enable warnings */
static int opt_numbers;		/* Number our fields */

typedef struct xopo_msg_s {
    TAILQ_ENTRY(xopo_msg_s) xm_link;
    char *xm_plural;		/* If plural, points to the second part */
    char xm_data[0];		/* Start of data */
} xopo_msg_t;

typedef TAILQ_HEAD(xopo_msg_list_s, xopo_msg_s) xopo_msg_list_t;

static xopo_msg_list_t field_list;

static void
xopo_msg_cb (const char *str, unsigned len, int plural)
{
    int sz = sizeof(xopo_msg_t) + len + 1;
    xopo_msg_t *xmp = malloc(sz);
    if (xmp == NULL)
	return;

    bzero(xmp, sz);
    memcpy(xmp->xm_data, str, len);
    xmp->xm_data[len] = '\0';

    if (plural) {
	char *cp = strchr(xmp->xm_data, ',');
	if (cp) {
	    *cp++ = '\0';
	    xmp->xm_plural = cp;
	}
    }

    xopo_msg_t *xmp2;

    TAILQ_FOREACH(xmp2, &field_list, xm_link) {
	if (strcmp(xmp->xm_data, xmp2->xm_data) == 0) {
	    /* Houston, we have a negative on that trajectory */
	    free(xmp);
	    return;
	}
    }

    TAILQ_INSERT_TAIL(&field_list, xmp, xm_link);
}

static void
print_version (void)
{
    fprintf(stderr, "libxo version %s%s\n",
	    xo_version, xo_version_extra);
    fprintf(stderr, "xopo version %s%s\n",
	    LIBXO_VERSION, LIBXO_VERSION_EXTRA);
}

static void
print_help (void)
{
    fprintf(stderr,
"Usage: xopo [options] format [fields]\n"
"    --help                Display this help text\n"
"    --option <opts> -or -O <opts> Give formatting options\n"
"    --output <file> -or -o <file> Use file as output destination\n"
"    --po <file> or -f <file> Generate new msgid's for a po file\n"
"    --simplify <text> OR -s <text> Show simplified form of the format string\n"
"    --version             Display version information\n"
"    --warn OR -W          Display warnings in text on stderr\n"
);
}

static struct opts {
    int o_help;
    int o_version;
} opts;

static struct option long_opts[] = {
    { "help", no_argument, &opts.o_help, 1 },
    { "number", no_argument, NULL, 'n' },
    { "option", required_argument, NULL, 'O' },
    { "output", required_argument, NULL, 'o' },
    { "po", required_argument, NULL, 'f' },
    { "simplify", no_argument, NULL, 'S' },
    { "warn", no_argument, NULL, 'W' },
    { NULL, 0, NULL, 0 }
};

int
main (int argc UNUSED, char **argv)
{
    char *fmt = NULL;
    char *opt_options = NULL;
    char *opt_input = NULL;
    char *opt_output = NULL;
    char *opt_simplify = NULL;
    int rc;

    argc = xo_parse_args(argc, argv);
    if (argc < 0)
	return 1;

    while ((rc = getopt_long(argc, argv, "f:no:O:s:W",
				long_opts, NULL)) != -1) {
	switch (rc) {
	case 'f':
	    opt_input = optarg;
	    break;

	case 'n':
	    opt_numbers = 1;
	    break;

	case 'o':
	    opt_output = optarg;
	    break;

	case 'O':
	    opt_options = optarg;
	    break;

	case 's':
	    opt_simplify = optarg;
	    break;

	case 'W':
	    opt_warn = 1;
	    xo_set_flags(NULL, XOF_WARN);
	    break;

	case ':':
	    xo_errx(1, "missing argument");
	    break;

	case 0:
	    if (opts.o_help) {
		print_help();
		return 1;

	    } else if (opts.o_version) {
		print_version();
		return 0;

	    } else {
		print_help();
		return 1;
	    }

	    bzero(&opts, sizeof(opts)); /* Reset all the options */
	    break;

	default:
	    print_help();
	    return 1;
	}
    }

    argc -= optind;
    argv += optind;

    if (opt_options) {
	rc = xo_set_options(NULL, opt_options);
	if (rc < 0)
	    xo_errx(1, "invalid options: %s", opt_options);
    }

    fmt = *argv++;

    if (opt_simplify) {
	fmt = xo_simplify_format(NULL, opt_simplify, opt_numbers, NULL);
	if (fmt) {
	    xo_emit("{:format}\n", fmt);
	    free(fmt);
	}
	exit(0);
    }

    static char msgid[] = "msgid ";
    char buf[BUFSIZ], *cp, *ep;
    FILE *infile;
    FILE *outfile;
    TAILQ_INIT(&field_list);

    if (opt_input) {
	infile = fopen(opt_input, "r");
	if (infile == NULL)
	    xo_emit_err(1, "count not open input file: '{:filename}'",
			opt_input);
    } else
	infile = stdin;

    if (opt_output) {
	unlink(opt_output);
	outfile = fopen(opt_output, "w");
	if (outfile == NULL)
	    xo_emit_err(1, "count not open output file: '{:filename}'",
			opt_output);
    } else
	outfile = stdout;

    int blank = 0, line;

    for (line = 1;; line++) {
	if (fgets(buf, sizeof(buf), infile) == NULL)
	    break;

	if (buf[0] == '#' && buf[1] == '\n')
	    continue;

	blank = (buf[0] == '\n' && buf[1] == '\0');

	if (strncmp(buf, msgid, sizeof(msgid) - 1) != 0) {
	    fprintf(outfile, "%s", buf);
	    continue;
	}

	for (cp = buf + sizeof(msgid); *cp; cp++)
	    if (!isspace((int) *cp))
		break;

	if (*cp == '"')
	    cp += 1;

	ep = cp + strlen(cp);
	if (ep > cp)
	    ep -= 1;

	while (isspace((int) *ep) && ep > cp)
	    ep -= 1;

	if (*ep != '"')
	    *ep += 1;

	*ep = '\0';

	cp = xo_simplify_format(NULL, cp, opt_numbers, xopo_msg_cb);
	if (cp) {
	    fprintf(outfile, "msgid \"%s\"\n", cp);
	    free(cp);
	}
    }

    if (!blank)
	fprintf(outfile, "\n");

    xopo_msg_t *xmp;
    TAILQ_FOREACH(xmp, &field_list, xm_link) {
	if (xmp->xm_plural) {
	    fprintf(outfile, "msgid \"%s\"\n"
		    "msgid_plural \"%s\"\n"
		    "msgstr[0] \"\"\n"
		    "msgstr[1] \"\"\n\n",
		    xmp->xm_data, xmp->xm_plural);
	} else {
	    fprintf(outfile, "msgid \"%s\"\nmsgstr \"\"\n\n", xmp->xm_data);
	}
    }

    if (infile != stdin)
	fclose(infile);
    if (outfile != stdout)
	fclose(outfile);

    xo_finish();

    return 0;
}
OpenPOWER on IntegriCloud