]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
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 | ||
b50945d4 | 34 | /* 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c KZ |
35 | * - added Native Language Support |
36 | */ | |
37 | ||
6dbe3af9 KZ |
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" | |
7eda085c | 45 | #include "nls.h" |
85bf44b7 | 46 | #include "xalloc.h" |
bec2d458 | 47 | #include "strutils.h" |
6dbe3af9 | 48 | |
ffc43748 KZ |
49 | static void escape(char *p1); |
50 | static void badcnt(const char *s); | |
fd6b7a7f | 51 | static void badsfmt(void); |
ffc43748 KZ |
52 | static void badfmt(const char *fmt); |
53 | static void badconv(const char *ch); | |
fd6b7a7f | 54 | |
f65e62e0 OO |
55 | #define in(s,f) strchr(f, *(s)) |
56 | ||
6dbe3af9 KZ |
57 | FU *endfu; /* format at end-of-data */ |
58 | ||
fd6b7a7f | 59 | void addfile(char *name) |
6dbe3af9 | 60 | { |
acf74fc2 | 61 | char *p, *buf = NULL; |
6dbe3af9 | 62 | FILE *fp; |
96ea3d32 | 63 | size_t n; |
6dbe3af9 | 64 | |
85bf44b7 SK |
65 | if ((fp = fopen(name, "r")) == NULL) |
66 | err(EXIT_FAILURE, _("can't read %s"), name); | |
96ea3d32 OO |
67 | |
68 | while (getline(&buf, &n, fp) != -1) { | |
89dd9eb3 | 69 | p = buf; |
96ea3d32 | 70 | |
89dd9eb3 OO |
71 | while (*p && isspace(*p)) |
72 | ++p; | |
6dbe3af9 KZ |
73 | if (!*p || *p == '#') |
74 | continue; | |
96ea3d32 | 75 | |
89dd9eb3 | 76 | add(p); |
6dbe3af9 | 77 | } |
96ea3d32 OO |
78 | |
79 | free(buf); | |
9db51207 | 80 | fclose(fp); |
6dbe3af9 KZ |
81 | } |
82 | ||
ffc43748 | 83 | void add(const char *fmt) |
6dbe3af9 | 84 | { |
acf74fc2 | 85 | const unsigned char *p, *savep; |
6dbe3af9 | 86 | FS *tfs; |
9db51207 | 87 | FU *tfu; |
6dbe3af9 | 88 | |
ffc43748 | 89 | /* Start new linked list of format units. */ |
c53c38b9 | 90 | tfs = xcalloc(1, sizeof(FS)); |
bbc8c153 OO |
91 | INIT_LIST_HEAD(&tfs->fslist); |
92 | INIT_LIST_HEAD(&tfs->fulist); | |
93 | list_add_tail(&tfs->fslist, &fshead); | |
bafd2d46 | 94 | |
ffc43748 | 95 | /* Take the format string and break it up into format units. */ |
9db51207 OO |
96 | p = (unsigned char *)fmt; |
97 | while (TRUE) { | |
ffc43748 | 98 | /* Skip leading white space. */ |
9db51207 OO |
99 | while (isspace(*p) && ++p) |
100 | ; | |
6dbe3af9 KZ |
101 | if (!*p) |
102 | break; | |
103 | ||
ffc43748 | 104 | /* Allocate a new format unit and link it in. */ |
c53c38b9 | 105 | tfu = xcalloc(1, sizeof(FU)); |
acf74fc2 OO |
106 | tfu->reps = 1; |
107 | ||
bbc8c153 OO |
108 | INIT_LIST_HEAD(&tfu->fulist); |
109 | INIT_LIST_HEAD(&tfu->prlist); | |
110 | list_add_tail(&tfu->fulist, &tfs->fulist); | |
6dbe3af9 | 111 | |
ffc43748 | 112 | /* If leading digit, repetition count. */ |
9db51207 OO |
113 | if (isdigit(*p)) { |
114 | savep = p; | |
115 | while (isdigit(*p) && ++p) | |
116 | ; | |
117 | if (!isspace(*p) && *p != '/') | |
6dbe3af9 KZ |
118 | badfmt(fmt); |
119 | /* may overwrite either white space or slash */ | |
9db51207 | 120 | tfu->reps = atoi((char *)savep); |
6dbe3af9 KZ |
121 | tfu->flags = F_SETREP; |
122 | /* skip trailing white space */ | |
d6e5614e | 123 | while (isspace(*++p)) |
9db51207 | 124 | ; |
6dbe3af9 KZ |
125 | } |
126 | ||
ffc43748 | 127 | /* Skip slash and trailing white space. */ |
6dbe3af9 | 128 | if (*p == '/') |
9db51207 OO |
129 | while (isspace(*++p)) |
130 | ; | |
6dbe3af9 KZ |
131 | |
132 | /* byte count */ | |
9db51207 OO |
133 | if (isdigit(*p)) { |
134 | savep = p; | |
135 | while (isdigit(*p) && ++p) | |
136 | ; | |
137 | if (!isspace(*p)) | |
6dbe3af9 | 138 | badfmt(fmt); |
9db51207 | 139 | tfu->bcnt = atoi((char *)savep); |
6dbe3af9 | 140 | /* skip trailing white space */ |
d6e5614e | 141 | while (isspace(*++p)) |
9db51207 | 142 | ; |
6dbe3af9 KZ |
143 | } |
144 | ||
145 | /* format */ | |
146 | if (*p != '"') | |
147 | badfmt(fmt); | |
9db51207 | 148 | savep = ++p; |
acf74fc2 | 149 | while (*p != '"') { |
7e6e290b | 150 | if (!*p++) |
6dbe3af9 | 151 | badfmt(fmt); |
acf74fc2 | 152 | } |
85bf44b7 | 153 | tfu->fmt = xmalloc(p - savep + 1); |
bec2d458 | 154 | xstrncpy(tfu->fmt, (char *)savep, p - savep + 1); |
6dbe3af9 | 155 | escape(tfu->fmt); |
acf74fc2 | 156 | ++p; |
6dbe3af9 KZ |
157 | } |
158 | } | |
159 | ||
ffc43748 | 160 | static const char *spec = ".#-+ 0123456789"; |
fd6b7a7f | 161 | |
89a13b46 | 162 | int block_size(FS *fs) |
6dbe3af9 | 163 | { |
ffc43748 | 164 | FU *fu; |
acf74fc2 | 165 | int bcnt, prec, cursize = 0; |
9db51207 OO |
166 | unsigned char *fmt; |
167 | struct list_head *p; | |
6dbe3af9 KZ |
168 | |
169 | /* figure out the data block size needed for each format unit */ | |
bbc8c153 OO |
170 | list_for_each (p, &fs->fulist) { |
171 | fu = list_entry(p, FU, fulist); | |
6dbe3af9 KZ |
172 | if (fu->bcnt) { |
173 | cursize += fu->bcnt * fu->reps; | |
174 | continue; | |
175 | } | |
9db51207 OO |
176 | bcnt = prec = 0; |
177 | fmt = (unsigned char *)fu->fmt; | |
178 | while (*fmt) { | |
179 | if (*fmt != '%') { | |
180 | ++fmt; | |
6dbe3af9 | 181 | continue; |
9db51207 | 182 | } |
6dbe3af9 KZ |
183 | /* |
184 | * skip any special chars -- save precision in | |
185 | * case it's a %s format. | |
186 | */ | |
acf74fc2 OO |
187 | while (strchr(spec + 1, *++fmt)) |
188 | ; | |
9db51207 OO |
189 | if (*fmt == '.' && isdigit(*++fmt)) { |
190 | prec = atoi((char *)fmt); | |
191 | while (isdigit(*++fmt)) | |
192 | ; | |
6dbe3af9 | 193 | } |
f65e62e0 | 194 | if (in(fmt, "diouxX")) |
6dbe3af9 | 195 | bcnt += 4; |
f65e62e0 | 196 | else if (in(fmt, "efgEG")) |
6dbe3af9 | 197 | bcnt += 8; |
f65e62e0 | 198 | else if (*fmt == 's') |
6dbe3af9 | 199 | bcnt += prec; |
f65e62e0 OO |
200 | else if (*fmt == 'c' || (*fmt == '_' && in(++fmt, "cpu"))) |
201 | ++bcnt; | |
9db51207 | 202 | ++fmt; |
6dbe3af9 KZ |
203 | } |
204 | cursize += bcnt * fu->reps; | |
205 | } | |
206 | return(cursize); | |
207 | } | |
208 | ||
fd6b7a7f | 209 | void rewrite(FS *fs) |
6dbe3af9 KZ |
210 | { |
211 | enum { NOTOKAY, USEBCNT, USEPREC } sokay; | |
9db51207 | 212 | PR *pr; |
ffc43748 | 213 | FU *fu; |
9db51207 | 214 | struct list_head *p, *q; |
acf74fc2 OO |
215 | char *p1, *p2, *fmtp; |
216 | char savech, cs[3]; | |
217 | int nconv, prec = 0; | |
6dbe3af9 | 218 | |
bbc8c153 OO |
219 | list_for_each (p, &fs->fulist) { |
220 | fu = list_entry(p, FU, fulist); | |
6dbe3af9 | 221 | /* |
ffc43748 | 222 | * Break each format unit into print units; each |
6dbe3af9 KZ |
223 | * conversion character gets its own. |
224 | */ | |
9db51207 OO |
225 | nconv = 0; |
226 | fmtp = fu->fmt; | |
227 | while (*fmtp) { | |
c53c38b9 | 228 | pr = xcalloc(1, sizeof(PR)); |
bbc8c153 OO |
229 | INIT_LIST_HEAD(&pr->prlist); |
230 | list_add_tail(&pr->prlist, &fu->prlist); | |
6dbe3af9 | 231 | |
ffc43748 | 232 | /* Skip preceding text and up to the next % sign. */ |
eef27d32 OO |
233 | p1 = fmtp; |
234 | while (*p1 && *p1 != '%') | |
235 | ++p1; | |
6dbe3af9 | 236 | |
ffc43748 | 237 | /* Only text in the string. */ |
6dbe3af9 KZ |
238 | if (!*p1) { |
239 | pr->fmt = fmtp; | |
240 | pr->flags = F_TEXT; | |
241 | break; | |
242 | } | |
243 | ||
244 | /* | |
ffc43748 | 245 | * Get precision for %s -- if have a byte count, don't |
6dbe3af9 KZ |
246 | * need it. |
247 | */ | |
248 | if (fu->bcnt) { | |
249 | sokay = USEBCNT; | |
250 | /* skip to conversion character */ | |
eef27d32 OO |
251 | while (++p1 && strchr(spec, *p1)) |
252 | ; | |
6dbe3af9 KZ |
253 | } else { |
254 | /* skip any special chars, field width */ | |
eef27d32 OO |
255 | while (strchr(spec + 1, *++p1)) |
256 | ; | |
dabfe2ad | 257 | if (*p1 == '.' && isdigit(*++p1)) { |
6dbe3af9 KZ |
258 | sokay = USEPREC; |
259 | prec = atoi(p1); | |
dabfe2ad | 260 | while (isdigit(*++p1)) |
eef27d32 | 261 | ; |
ffc43748 | 262 | } else |
6dbe3af9 KZ |
263 | sokay = NOTOKAY; |
264 | } | |
265 | ||
ffc43748 KZ |
266 | p2 = p1 + 1; /* Set end pointer. */ |
267 | cs[0] = *p1; /* Set conversion string. */ | |
268 | cs[1] = 0; | |
6dbe3af9 KZ |
269 | |
270 | /* | |
ffc43748 | 271 | * Figure out the byte count for each conversion; |
6dbe3af9 KZ |
272 | * rewrite the format as necessary, set up blank- |
273 | * padding for end of data. | |
274 | */ | |
f65e62e0 OO |
275 | if (*cs == 'c') { |
276 | pr->flags = F_CHAR; | |
277 | switch(fu->bcnt) { | |
278 | case 0: | |
279 | case 1: | |
280 | pr->bcnt = 1; | |
281 | break; | |
282 | default: | |
283 | p1[1] = '\0'; | |
284 | badcnt(p1); | |
285 | } | |
286 | } else if (in(cs, "di")) { | |
287 | pr->flags = F_INT; | |
288 | goto isint; | |
289 | } else if (in(cs, "ouxX")) { | |
290 | pr->flags = F_UINT; | |
291 | isint: cs[2] = '\0'; | |
292 | cs[1] = cs[0]; | |
293 | cs[0] = 'q'; | |
294 | switch(fu->bcnt) { | |
295 | case 0: | |
296 | pr->bcnt = 4; | |
297 | break; | |
298 | case 1: | |
299 | case 2: | |
300 | case 4: | |
301 | case 8: | |
302 | pr->bcnt = fu->bcnt; | |
303 | break; | |
304 | default: | |
305 | p1[1] = '\0'; | |
306 | badcnt(p1); | |
307 | } | |
308 | } else if (in(cs, "efgEG")) { | |
309 | pr->flags = F_DBL; | |
310 | switch(fu->bcnt) { | |
311 | case 0: | |
312 | pr->bcnt = 8; | |
313 | break; | |
314 | case 4: | |
315 | case 8: | |
316 | pr->bcnt = fu->bcnt; | |
317 | break; | |
318 | default: | |
319 | p1[1] = '\0'; | |
320 | badcnt(p1); | |
321 | } | |
322 | } else if(*cs == 's') { | |
323 | pr->flags = F_STR; | |
324 | switch(sokay) { | |
eef27d32 OO |
325 | case NOTOKAY: |
326 | badsfmt(); | |
327 | case USEBCNT: | |
328 | pr->bcnt = fu->bcnt; | |
329 | break; | |
330 | case USEPREC: | |
331 | pr->bcnt = prec; | |
332 | break; | |
f65e62e0 OO |
333 | } |
334 | } else if (*cs == '_') { | |
335 | ++p2; | |
336 | switch(p1[1]) { | |
337 | case 'A': | |
338 | endfu = fu; | |
339 | fu->flags |= F_IGNORE; | |
340 | /* FALLTHROUGH */ | |
341 | case 'a': | |
342 | pr->flags = F_ADDRESS; | |
343 | ++p2; | |
344 | if (in(p1 + 2, "dox")) { | |
345 | cs[0] = 'q'; | |
346 | cs[1] = p1[2]; | |
347 | cs[2] = '\0'; | |
348 | } else { | |
349 | p1[3] = '\0'; | |
eef27d32 | 350 | badconv(p1); |
f65e62e0 OO |
351 | } |
352 | break; | |
353 | case 'c': | |
354 | pr->flags = F_C; | |
355 | /* cs[0] = 'c'; set in conv_c */ | |
356 | goto isint2; | |
357 | case 'p': | |
358 | pr->flags = F_P; | |
359 | cs[0] = 'c'; | |
360 | goto isint2; | |
361 | case 'u': | |
362 | pr->flags = F_U; | |
363 | /* cs[0] = 'c'; set in conv_u */ | |
364 | isint2: switch(fu->bcnt) { | |
365 | case 0: | |
366 | 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 | } else { | |
379 | p1[1] = '\0'; | |
380 | badconv(p1); | |
6dbe3af9 KZ |
381 | } |
382 | ||
383 | /* | |
ffc43748 | 384 | * Copy to PR format string, set conversion character |
6dbe3af9 KZ |
385 | * pointer, update original. |
386 | */ | |
387 | savech = *p2; | |
ffc43748 | 388 | p1[0] = '\0'; |
85bf44b7 | 389 | pr->fmt = xmalloc(strlen(fmtp) + strlen(cs) + 1); |
eef27d32 OO |
390 | strcpy(pr->fmt, fmtp); |
391 | strcat(pr->fmt, cs); | |
6dbe3af9 KZ |
392 | *p2 = savech; |
393 | pr->cchar = pr->fmt + (p1 - fmtp); | |
394 | fmtp = p2; | |
395 | ||
ffc43748 | 396 | /* Only one conversion character if byte count */ |
85bf44b7 SK |
397 | if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) |
398 | errx(EXIT_FAILURE, | |
399 | _("byte count with multiple conversion characters")); | |
6dbe3af9 KZ |
400 | } |
401 | /* | |
ffc43748 | 402 | * If format unit byte count not specified, figure it out |
6dbe3af9 KZ |
403 | * so can adjust rep count later. |
404 | */ | |
405 | if (!fu->bcnt) | |
bbc8c153 OO |
406 | list_for_each(q, &fu->prlist) |
407 | fu->bcnt += (list_entry(q, PR, prlist))->bcnt; | |
6dbe3af9 KZ |
408 | } |
409 | /* | |
ffc43748 | 410 | * If the format string interprets any data at all, and it's |
6dbe3af9 KZ |
411 | * not the same as the blocksize, and its last format unit |
412 | * interprets any data at all, and has no iteration count, | |
413 | * repeat it as necessary. | |
414 | * | |
ffc43748 | 415 | * If rep count is greater than 1, no trailing whitespace |
6dbe3af9 KZ |
416 | * gets output from the last iteration of the format unit. |
417 | */ | |
bbc8c153 OO |
418 | list_for_each (p, &fs->fulist) { |
419 | fu = list_entry(p, FU, fulist); | |
acf74fc2 OO |
420 | |
421 | if (list_entry_is_last(&fu->fulist, &fs->fulist) && | |
422 | fs->bcnt < blocksize && | |
423 | !(fu->flags&F_SETREP) && fu->bcnt) | |
424 | fu->reps += (blocksize - fs->bcnt) / fu->bcnt; | |
6dbe3af9 | 425 | if (fu->reps > 1) { |
bbc8c153 OO |
426 | if (!list_empty(&fu->prlist)) { |
427 | pr = list_last_entry(&fu->prlist, PR, prlist); | |
d6e5614e OO |
428 | for (p1 = pr->fmt, p2 = NULL; *p1; ++p1) |
429 | p2 = isspace(*p1) ? p1 : NULL; | |
430 | if (p2) | |
431 | pr->nospace = p2; | |
f1107b4a | 432 | } |
6dbe3af9 | 433 | } |
6dbe3af9 KZ |
434 | } |
435 | } | |
436 | ||
437 | ||
ffc43748 | 438 | static void escape(char *p1) |
6dbe3af9 | 439 | { |
ffc43748 | 440 | char *p2; |
6dbe3af9 KZ |
441 | |
442 | /* alphabetic escape sequences have to be done in place */ | |
9c2cb9b0 OO |
443 | p2 = p1; |
444 | while (TRUE) { | |
6dbe3af9 KZ |
445 | if (!*p1) { |
446 | *p2 = *p1; | |
447 | break; | |
448 | } | |
449 | if (*p1 == '\\') | |
450 | switch(*++p1) { | |
451 | case 'a': | |
452 | /* *p2 = '\a'; */ | |
453 | *p2 = '\007'; | |
454 | break; | |
455 | case 'b': | |
456 | *p2 = '\b'; | |
457 | break; | |
458 | case 'f': | |
459 | *p2 = '\f'; | |
460 | break; | |
461 | case 'n': | |
462 | *p2 = '\n'; | |
463 | break; | |
464 | case 'r': | |
465 | *p2 = '\r'; | |
466 | break; | |
467 | case 't': | |
468 | *p2 = '\t'; | |
469 | break; | |
470 | case 'v': | |
471 | *p2 = '\v'; | |
472 | break; | |
473 | default: | |
474 | *p2 = *p1; | |
475 | break; | |
476 | } | |
9c2cb9b0 | 477 | ++p1; ++p2; |
6dbe3af9 KZ |
478 | } |
479 | } | |
480 | ||
ffc43748 | 481 | static void badcnt(const char *s) |
6dbe3af9 | 482 | { |
85bf44b7 | 483 | errx(EXIT_FAILURE, _("bad byte count for conversion character %s"), s); |
6dbe3af9 KZ |
484 | } |
485 | ||
22853e4a | 486 | static void badsfmt(void) |
6dbe3af9 | 487 | { |
85bf44b7 | 488 | errx(EXIT_FAILURE, _("%%s requires a precision or a byte count")); |
6dbe3af9 KZ |
489 | } |
490 | ||
ffc43748 | 491 | static void badfmt(const char *fmt) |
6dbe3af9 | 492 | { |
85bf44b7 | 493 | errx(EXIT_FAILURE, _("bad format {%s}"), fmt); |
6dbe3af9 KZ |
494 | } |
495 | ||
ffc43748 | 496 | static void badconv(const char *ch) |
6dbe3af9 | 497 | { |
85bf44b7 | 498 | errx(EXIT_FAILURE, _("bad conversion character %%%s"), ch); |
6dbe3af9 | 499 | } |