]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/parse.c
2 * Copyright (c) 1989 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 /* 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
35 * - added Native Language Support
38 #include <sys/types.h>
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
);
54 FU
*endfu
; /* format at end-of-data */
56 void addfile(char *name
)
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
);
72 for (p
= buf
; *p
&& isspace((unsigned char)*p
); ++p
);
80 void add(const char *fmt
)
83 static FS
**nextfs
= NULL
;
88 /* Start new linked list of format units. */
89 tfs
= xcalloc(1, sizeof(FS
));
95 nextfs
= &tfs
->nextfs
;
96 nextfu
= &tfs
->nextfu
;
98 /* Take the format string and break it up into format units. */
100 /* Skip leading white space. */
101 for (; isspace((unsigned char)*p
); ++p
);
105 /* Allocate a new format unit and link it in. */
106 tfu
= xcalloc(1, sizeof(FU
));
108 nextfu
= &tfu
->nextfu
;
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
!= '/')
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
);
123 /* Skip slash and trailing white space. */
125 while (isspace((unsigned char)*++p
));
128 if (isdigit((unsigned char)*p
)) {
129 for (savep
= p
; isdigit((unsigned char)*p
); ++p
);
130 if (!isspace((unsigned char)*p
))
132 tfu
->bcnt
= atoi(savep
);
133 /* skip trailing white space */
134 for (++p
; isspace((unsigned char)*p
); ++p
);
140 for (savep
= ++p
; *p
!= '"';)
143 tfu
->fmt
= xmalloc(p
- savep
+ 1);
144 (void) strncpy(tfu
->fmt
, savep
, p
- savep
);
145 tfu
->fmt
[p
- savep
] = '\0';
151 static const char *spec
= ".#-+ 0123456789";
153 int block_size(FS
*fs
)
160 /* figure out the data block size needed for each format unit */
161 for (cursize
= 0, fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
163 cursize
+= fu
->bcnt
* fu
->reps
;
166 for (bcnt
= prec
= 0, fmt
= fu
->fmt
; *fmt
; ++fmt
) {
170 * skip any special chars -- save precision in
171 * case it's a %s format.
173 while (strchr(spec
+ 1, *++fmt
));
174 if (*fmt
== '.' && isdigit((unsigned char)*++fmt
)) {
176 while (isdigit((unsigned char)*++fmt
));
182 case 'd': case 'i': case 'o': case 'u':
186 case 'e': case 'E': case 'f': case 'g': case 'G':
194 case 'c': case 'p': case 'u':
200 cursize
+= bcnt
* fu
->reps
;
207 enum { NOTOKAY
, USEBCNT
, USEPREC
} sokay
;
211 char savech
, *fmtp
, cs
[3];
217 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
219 * Break each format unit into print units; each
220 * conversion character gets its own.
222 for (nconv
= 0, fmtp
= fu
->fmt
; *fmtp
; nextpr
= &pr
->nextpr
) {
223 pr
= xcalloc(1, sizeof(PR
));
229 /* Skip preceding text and up to the next % sign. */
230 for (p1
= fmtp
; *p1
&& *p1
!= '%'; ++p1
);
232 /* Only text in the string. */
240 * Get precision for %s -- if have a byte count, don't
245 /* skip to conversion character */
246 for (++p1
; strchr(spec
, *p1
); ++p1
);
248 /* skip any special chars, field width */
249 while (strchr(spec
+ 1, *++p1
));
251 isdigit((unsigned char)*++p1
)) {
254 while (isdigit((unsigned char)*++p1
));
259 p2
= p1
+ 1; /* Set end pointer. */
260 cs
[0] = *p1
; /* Set conversion string. */
264 * Figure out the byte count for each conversion;
265 * rewrite the format as necessary, set up blank-
266 * padding for end of data.
283 case 'o': case 'u': case 'x': case 'X':
306 case 'e': case 'E': case 'f': case 'g': case 'G':
338 fu
->flags
|= F_IGNORE
;
341 pr
->flags
= F_ADDRESS
;
344 case 'd': case 'o': case'x':
356 /* cs[0] = 'c'; set in conv_c */
364 /* cs[0] = 'c'; set in conv_u */
365 isint2
: switch(fu
->bcnt
) {
385 * Copy to PR format string, set conversion character
386 * pointer, update original.
390 pr
->fmt
= xmalloc(strlen(fmtp
) + strlen(cs
) + 1);
391 (void)strcpy(pr
->fmt
, fmtp
);
392 (void)strcat(pr
->fmt
, cs
);
394 pr
->cchar
= pr
->fmt
+ (p1
- fmtp
);
397 /* Only one conversion character if byte count */
398 if (!(pr
->flags
&F_ADDRESS
) && fu
->bcnt
&& nconv
++)
400 _("byte count with multiple conversion characters"));
403 * If format unit byte count not specified, figure it out
404 * so can adjust rep count later.
407 for (pr
= fu
->nextpr
; pr
; pr
= pr
->nextpr
)
408 fu
->bcnt
+= pr
->bcnt
;
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.
416 * If rep count is greater than 1, no trailing whitespace
417 * gets output from the last iteration of the format unit.
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
;
425 for (pr
= fu
->nextpr
; ; pr
= pr
->nextpr
)
428 for (p1
= pr
->fmt
, p2
= NULL
; *p1
; ++p1
)
429 p2
= isspace((unsigned char)*p1
) ? p1
: NULL
;
438 static void escape(char *p1
)
442 /* alphabetic escape sequences have to be done in place */
443 for (p2
= p1
;; ++p1
, ++p2
) {
479 static void badcnt(const char *s
)
481 errx(EXIT_FAILURE
, _("bad byte count for conversion character %s"), s
);
484 static void badsfmt(void)
486 errx(EXIT_FAILURE
, _("%%s requires a precision or a byte count"));
489 static void badfmt(const char *fmt
)
491 errx(EXIT_FAILURE
, _("bad format {%s}"), fmt
);
494 static void badconv(const char *ch
)
496 errx(EXIT_FAILURE
, _("bad conversion character %%%s"), ch
);