]> git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/parse.c
docs: standardize the phrases for --help and --version in all man pages
[thirdparty/util-linux.git] / text-utils / parse.c
1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /* 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
35 * - added Native Language Support
36 */
37
38 #include <sys/types.h>
39 #include <sys/file.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <ctype.h>
43 #include <string.h>
44 #include "hexdump.h"
45 #include "nls.h"
46 #include "xalloc.h"
47
48 static void escape(char *p1);
49 static void badcnt(const char *s);
50 static void badsfmt(void);
51 static void badfmt(const char *fmt);
52 static void badconv(const char *ch);
53
54 FU *endfu; /* format at end-of-data */
55
56 void addfile(char *name)
57 {
58 char *p;
59 FILE *fp;
60 int ch;
61 char buf[2048 + 1];
62
63 if ((fp = fopen(name, "r")) == NULL)
64 err(EXIT_FAILURE, _("can't read %s"), name);
65 while (fgets(buf, sizeof(buf), fp)) {
66 if ((p = strchr(buf, '\n')) == NULL) {
67 warnx(_("line too long"));
68 while ((ch = getchar()) != '\n' && ch != EOF);
69 continue;
70 }
71 *p = '\0';
72 for (p = buf; *p && isspace((unsigned char)*p); ++p);
73 if (!*p || *p == '#')
74 continue;
75 add(p);
76 }
77 (void)fclose(fp);
78 }
79
80 void add(const char *fmt)
81 {
82 const char *p;
83 static FS **nextfs = NULL;
84 FS *tfs;
85 FU *tfu, **nextfu;
86 const char *savep;
87
88 /* Start new linked list of format units. */
89 tfs = xcalloc(1, sizeof(FS));
90 if (!fshead)
91 fshead = tfs;
92 else if (nextfs)
93 *nextfs = tfs;
94
95 nextfs = &tfs->nextfs;
96 nextfu = &tfs->nextfu;
97
98 /* Take the format string and break it up into format units. */
99 for (p = fmt;;) {
100 /* Skip leading white space. */
101 for (; isspace((unsigned char)*p); ++p);
102 if (!*p)
103 break;
104
105 /* Allocate a new format unit and link it in. */
106 tfu = xcalloc(1, sizeof(FU));
107 *nextfu = tfu;
108 nextfu = &tfu->nextfu;
109 tfu->reps = 1;
110
111 /* If leading digit, repetition count. */
112 if (isdigit((unsigned char)*p)) {
113 for (savep = p; isdigit((unsigned char)*p); ++p);
114 if (!isspace((unsigned char)*p) && *p != '/')
115 badfmt(fmt);
116 /* may overwrite either white space or slash */
117 tfu->reps = atoi(savep);
118 tfu->flags = F_SETREP;
119 /* skip trailing white space */
120 for (++p; isspace((unsigned char)*p); ++p);
121 }
122
123 /* Skip slash and trailing white space. */
124 if (*p == '/')
125 while (isspace((unsigned char)*++p));
126
127 /* byte count */
128 if (isdigit((unsigned char)*p)) {
129 for (savep = p; isdigit((unsigned char)*p); ++p);
130 if (!isspace((unsigned char)*p))
131 badfmt(fmt);
132 tfu->bcnt = atoi(savep);
133 /* skip trailing white space */
134 for (++p; isspace((unsigned char)*p); ++p);
135 }
136
137 /* format */
138 if (*p != '"')
139 badfmt(fmt);
140 for (savep = ++p; *p != '"';)
141 if (*p++ == 0)
142 badfmt(fmt);
143 tfu->fmt = xmalloc(p - savep + 1);
144 (void) strncpy(tfu->fmt, savep, p - savep);
145 tfu->fmt[p - savep] = '\0';
146 escape(tfu->fmt);
147 p++;
148 }
149 }
150
151 static const char *spec = ".#-+ 0123456789";
152
153 int block_size(FS *fs)
154 {
155 FU *fu;
156 int bcnt, cursize;
157 char *fmt;
158 int prec;
159
160 /* figure out the data block size needed for each format unit */
161 for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
162 if (fu->bcnt) {
163 cursize += fu->bcnt * fu->reps;
164 continue;
165 }
166 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
167 if (*fmt != '%')
168 continue;
169 /*
170 * skip any special chars -- save precision in
171 * case it's a %s format.
172 */
173 while (strchr(spec + 1, *++fmt));
174 if (*fmt == '.' && isdigit((unsigned char)*++fmt)) {
175 prec = atoi(fmt);
176 while (isdigit((unsigned char)*++fmt));
177 }
178 switch(*fmt) {
179 case 'c':
180 bcnt += 1;
181 break;
182 case 'd': case 'i': case 'o': case 'u':
183 case 'x': case 'X':
184 bcnt += 4;
185 break;
186 case 'e': case 'E': case 'f': case 'g': case 'G':
187 bcnt += 8;
188 break;
189 case 's':
190 bcnt += prec;
191 break;
192 case '_':
193 switch(*++fmt) {
194 case 'c': case 'p': case 'u':
195 bcnt += 1;
196 break;
197 }
198 }
199 }
200 cursize += bcnt * fu->reps;
201 }
202 return(cursize);
203 }
204
205 void rewrite(FS *fs)
206 {
207 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
208 PR *pr, **nextpr;
209 FU *fu;
210 char *p1, *p2;
211 char savech, *fmtp, cs[3];
212 int nconv, prec;
213
214 nextpr = NULL;
215 prec = 0;
216
217 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
218 /*
219 * Break each format unit into print units; each
220 * conversion character gets its own.
221 */
222 for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
223 pr = xcalloc(1, sizeof(PR));
224 if (!fu->nextpr)
225 fu->nextpr = pr;
226 else if (nextpr)
227 *nextpr = pr;
228
229 /* Skip preceding text and up to the next % sign. */
230 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
231
232 /* Only text in the string. */
233 if (!*p1) {
234 pr->fmt = fmtp;
235 pr->flags = F_TEXT;
236 break;
237 }
238
239 /*
240 * Get precision for %s -- if have a byte count, don't
241 * need it.
242 */
243 if (fu->bcnt) {
244 sokay = USEBCNT;
245 /* skip to conversion character */
246 for (++p1; strchr(spec, *p1); ++p1);
247 } else {
248 /* skip any special chars, field width */
249 while (strchr(spec + 1, *++p1));
250 if (*p1 == '.' &&
251 isdigit((unsigned char)*++p1)) {
252 sokay = USEPREC;
253 prec = atoi(p1);
254 while (isdigit((unsigned char)*++p1));
255 } else
256 sokay = NOTOKAY;
257 }
258
259 p2 = p1 + 1; /* Set end pointer. */
260 cs[0] = *p1; /* Set conversion string. */
261 cs[1] = 0;
262
263 /*
264 * Figure out the byte count for each conversion;
265 * rewrite the format as necessary, set up blank-
266 * padding for end of data.
267 */
268 switch(cs[0]) {
269 case 'c':
270 pr->flags = F_CHAR;
271 switch(fu->bcnt) {
272 case 0: case 1:
273 pr->bcnt = 1;
274 break;
275 default:
276 p1[1] = '\0';
277 badcnt(p1);
278 }
279 break;
280 case 'd': case 'i':
281 pr->flags = F_INT;
282 goto isint;
283 case 'o': case 'u': case 'x': case 'X':
284 pr->flags = F_UINT;
285 isint: cs[2] = '\0';
286 cs[1] = cs[0];
287 cs[0] = 'q';
288 switch(fu->bcnt) {
289 case 0: case 4:
290 pr->bcnt = 4;
291 break;
292 case 1:
293 pr->bcnt = 1;
294 break;
295 case 2:
296 pr->bcnt = 2;
297 break;
298 case 8:
299 pr->bcnt = 8;
300 break;
301 default:
302 p1[1] = '\0';
303 badcnt(p1);
304 }
305 break;
306 case 'e': case 'E': case 'f': case 'g': case 'G':
307 pr->flags = F_DBL;
308 switch(fu->bcnt) {
309 case 0: case 8:
310 pr->bcnt = 8;
311 break;
312 case 4:
313 pr->bcnt = 4;
314 break;
315 default:
316 p1[1] = '\0';
317 badcnt(p1);
318 }
319 break;
320 case 's':
321 pr->flags = F_STR;
322 switch(sokay) {
323 case NOTOKAY:
324 badsfmt();
325 case USEBCNT:
326 pr->bcnt = fu->bcnt;
327 break;
328 case USEPREC:
329 pr->bcnt = prec;
330 break;
331 }
332 break;
333 case '_':
334 ++p2;
335 switch(p1[1]) {
336 case 'A':
337 endfu = fu;
338 fu->flags |= F_IGNORE;
339 /* FALLTHROUGH */
340 case 'a':
341 pr->flags = F_ADDRESS;
342 ++p2;
343 switch(p1[2]) {
344 case 'd': case 'o': case'x':
345 cs[0] = 'q';
346 cs[1] = p1[2];
347 cs[2] = '\0';
348 break;
349 default:
350 p1[3] = '\0';
351 badconv(p1);
352 }
353 break;
354 case 'c':
355 pr->flags = F_C;
356 /* cs[0] = 'c'; set in conv_c */
357 goto isint2;
358 case 'p':
359 pr->flags = F_P;
360 cs[0] = 'c';
361 goto isint2;
362 case 'u':
363 pr->flags = F_U;
364 /* cs[0] = 'c'; set in conv_u */
365 isint2: switch(fu->bcnt) {
366 case 0: case 1:
367 pr->bcnt = 1;
368 break;
369 default:
370 p1[2] = '\0';
371 badcnt(p1);
372 }
373 break;
374 default:
375 p1[2] = '\0';
376 badconv(p1);
377 }
378 break;
379 default:
380 p1[1] = '\0';
381 badconv(p1);
382 }
383
384 /*
385 * Copy to PR format string, set conversion character
386 * pointer, update original.
387 */
388 savech = *p2;
389 p1[0] = '\0';
390 pr->fmt = xmalloc(strlen(fmtp) + strlen(cs) + 1);
391 (void)strcpy(pr->fmt, fmtp);
392 (void)strcat(pr->fmt, cs);
393 *p2 = savech;
394 pr->cchar = pr->fmt + (p1 - fmtp);
395 fmtp = p2;
396
397 /* Only one conversion character if byte count */
398 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
399 errx(EXIT_FAILURE,
400 _("byte count with multiple conversion characters"));
401 }
402 /*
403 * If format unit byte count not specified, figure it out
404 * so can adjust rep count later.
405 */
406 if (!fu->bcnt)
407 for (pr = fu->nextpr; pr; pr = pr->nextpr)
408 fu->bcnt += pr->bcnt;
409 }
410 /*
411 * If the format string interprets any data at all, and it's
412 * not the same as the blocksize, and its last format unit
413 * interprets any data at all, and has no iteration count,
414 * repeat it as necessary.
415 *
416 * If rep count is greater than 1, no trailing whitespace
417 * gets output from the last iteration of the format unit.
418 */
419 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
420 if (!fu->nextfu && fs->bcnt < blocksize &&
421 !(fu->flags&F_SETREP) && fu->bcnt)
422 fu->reps += (blocksize - fs->bcnt) / fu->bcnt;
423 if (fu->reps > 1) {
424 if (fu->nextpr) {
425 for (pr = fu->nextpr; ; pr = pr->nextpr)
426 if (!pr->nextpr)
427 break;
428 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
429 p2 = isspace((unsigned char)*p1) ? p1 : NULL;
430 if (p2)
431 pr->nospace = p2;
432 }
433 }
434 }
435 }
436
437
438 static void escape(char *p1)
439 {
440 char *p2;
441
442 /* alphabetic escape sequences have to be done in place */
443 for (p2 = p1;; ++p1, ++p2) {
444 if (!*p1) {
445 *p2 = *p1;
446 break;
447 }
448 if (*p1 == '\\')
449 switch(*++p1) {
450 case 'a':
451 /* *p2 = '\a'; */
452 *p2 = '\007';
453 break;
454 case 'b':
455 *p2 = '\b';
456 break;
457 case 'f':
458 *p2 = '\f';
459 break;
460 case 'n':
461 *p2 = '\n';
462 break;
463 case 'r':
464 *p2 = '\r';
465 break;
466 case 't':
467 *p2 = '\t';
468 break;
469 case 'v':
470 *p2 = '\v';
471 break;
472 default:
473 *p2 = *p1;
474 break;
475 }
476 }
477 }
478
479 static void badcnt(const char *s)
480 {
481 errx(EXIT_FAILURE, _("bad byte count for conversion character %s"), s);
482 }
483
484 static void badsfmt(void)
485 {
486 errx(EXIT_FAILURE, _("%%s requires a precision or a byte count"));
487 }
488
489 static void badfmt(const char *fmt)
490 {
491 errx(EXIT_FAILURE, _("bad format {%s}"), fmt);
492 }
493
494 static void badconv(const char *ch)
495 {
496 errx(EXIT_FAILURE, _("bad conversion character %%%s"), ch);
497 }