]> git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/hexdump-parse.c
su: use lib/pty-session.c code for --pty
[thirdparty/util-linux.git] / text-utils / hexdump-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 #include "strutils.h"
48 #include "colors.h"
49
50 static void escape(char *p1);
51 static struct list_head *color_fmt(char *cfmt, int bcnt);
52
53 static void __attribute__ ((__noreturn__)) badcnt(const char *s)
54 {
55 errx(EXIT_FAILURE, _("bad byte count for conversion character %s"), s);
56 }
57
58 static void __attribute__ ((__noreturn__)) badsfmt(void)
59 {
60 errx(EXIT_FAILURE, _("%%s requires a precision or a byte count"));
61 }
62
63 static void __attribute__ ((__noreturn__)) badfmt(const char *fmt)
64 {
65 errx(EXIT_FAILURE, _("bad format {%s}"), fmt);
66 }
67
68 static void __attribute__ ((__noreturn__)) badconv(const char *ch)
69 {
70 errx(EXIT_FAILURE, _("bad conversion character %%%s"), ch);
71 }
72
73 #define first_letter(s,f) strchr(f, *(s))
74
75 struct hexdump_fu *endfu; /* format at end-of-data */
76
77 void addfile(char *name, struct hexdump *hex)
78 {
79 char *fmt, *buf = NULL;
80 FILE *fp;
81 size_t n;
82
83 if ((fp = fopen(name, "r")) == NULL)
84 err(EXIT_FAILURE, _("can't read %s"), name);
85
86 while (getline(&buf, &n, fp) != -1) {
87 fmt = buf;
88
89 while (*fmt && isspace(*fmt))
90 ++fmt;
91 if (!*fmt || *fmt == '#')
92 continue;
93
94 add_fmt(fmt, hex);
95 }
96
97 free(buf);
98 fclose(fp);
99 }
100
101 void add_fmt(const char *fmt, struct hexdump *hex)
102 {
103 const char *p, *savep;
104 struct hexdump_fs *tfs;
105 struct hexdump_fu *tfu;
106
107 /* Start new linked list of format units. */
108 tfs = xcalloc(1, sizeof(struct hexdump_fs));
109 INIT_LIST_HEAD(&tfs->fslist);
110 INIT_LIST_HEAD(&tfs->fulist);
111 list_add_tail(&tfs->fslist, &hex->fshead);
112
113 /* Take the format string and break it up into format units. */
114 p = fmt;
115 while (TRUE) {
116 /* Skip leading white space. */
117 if (!*(p = skip_space(p)))
118 break;
119
120 /* Allocate a new format unit and link it in. */
121 tfu = xcalloc(1, sizeof(struct hexdump_fu));
122 tfu->reps = 1;
123
124 INIT_LIST_HEAD(&tfu->fulist);
125 INIT_LIST_HEAD(&tfu->prlist);
126 list_add_tail(&tfu->fulist, &tfs->fulist);
127
128 /* If leading digit, repetition count. */
129 if (isdigit(*p)) {
130 savep = p;
131 while (isdigit(*p))
132 p++;
133 if (!isspace(*p) && *p != '/')
134 badfmt(fmt);
135 /* may overwrite either white space or slash */
136 tfu->reps = atoi(savep);
137 tfu->flags = F_SETREP;
138 /* skip trailing white space */
139 p = skip_space(++p);
140 }
141
142 /* Skip slash and trailing white space. */
143 if (*p == '/')
144 p = skip_space(++p);
145
146 /* byte count */
147 if (isdigit(*p)) {
148 savep = p;
149 while (isdigit(*p))
150 p++;
151 if (!isspace(*p))
152 badfmt(fmt);
153 tfu->bcnt = atoi(savep);
154 /* skip trailing white space */
155 p = skip_space(++p);
156 }
157
158 /* format */
159 if (*p != '"')
160 badfmt(fmt);
161 savep = ++p;
162 while (*p != '"') {
163 if (!*p++)
164 badfmt(fmt);
165 }
166 tfu->fmt = xmalloc(p - savep + 1);
167 xstrncpy(tfu->fmt, savep, p - savep + 1);
168 escape(tfu->fmt);
169 ++p;
170 }
171 }
172
173 static const char *spec = ".#-+ 0123456789";
174
175 int block_size(struct hexdump_fs *fs)
176 {
177 struct hexdump_fu *fu;
178 int bcnt, prec, cursize = 0;
179 char *fmt;
180 struct list_head *p;
181
182 /* figure out the data block size needed for each format unit */
183 list_for_each (p, &fs->fulist) {
184 fu = list_entry(p, struct hexdump_fu, fulist);
185 if (fu->bcnt) {
186 cursize += fu->bcnt * fu->reps;
187 continue;
188 }
189 bcnt = prec = 0;
190 fmt = fu->fmt;
191 while (*fmt) {
192 if (*fmt != '%') {
193 ++fmt;
194 continue;
195 }
196 /*
197 * skip any special chars -- save precision in
198 * case it's a %s format.
199 */
200 while (strchr(spec + 1, *++fmt))
201 ;
202 if (*fmt == '.' && isdigit(*++fmt)) {
203 prec = atoi(fmt);
204 while (isdigit(*++fmt))
205 ;
206 }
207 if (first_letter(fmt, "diouxX"))
208 bcnt += 4;
209 else if (first_letter(fmt, "efgEG"))
210 bcnt += 8;
211 else if (*fmt == 's')
212 bcnt += prec;
213 else if (*fmt == 'c' || (*fmt == '_' && first_letter(++fmt, "cpu")))
214 ++bcnt;
215 ++fmt;
216 }
217 cursize += bcnt * fu->reps;
218 }
219 return(cursize);
220 }
221
222 void rewrite_rules(struct hexdump_fs *fs, struct hexdump *hex)
223 {
224 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
225 struct hexdump_pr *pr;
226 struct hexdump_fu *fu;
227 struct list_head *p, *q;
228 char *p1, *p2, *fmtp;
229 char savech, cs[4];
230 int nconv, prec = 0;
231
232 list_for_each (p, &fs->fulist) {
233 fu = list_entry(p, struct hexdump_fu, fulist);
234 /*
235 * Break each format unit into print units; each
236 * conversion character gets its own.
237 */
238 nconv = 0;
239 fmtp = fu->fmt;
240 while (*fmtp) {
241 pr = xcalloc(1, sizeof(struct hexdump_pr));
242 INIT_LIST_HEAD(&pr->prlist);
243 list_add_tail(&pr->prlist, &fu->prlist);
244
245 /* Skip preceding text and up to the next % sign. */
246 p1 = fmtp;
247 while (*p1 && *p1 != '%')
248 ++p1;
249
250 /* Only text in the string. */
251 if (!*p1) {
252 pr->fmt = xstrdup(fmtp);
253 pr->flags = F_TEXT;
254 break;
255 }
256
257 /*
258 * Get precision for %s -- if have a byte count, don't
259 * need it.
260 */
261 if (fu->bcnt) {
262 sokay = USEBCNT;
263 /* skip to conversion character */
264 for (p1++; strchr(spec, *p1); p1++)
265 ;
266 } else {
267 /* skip any special chars, field width */
268 while (strchr(spec + 1, *++p1))
269 ;
270 if (*p1 == '.' && isdigit(*++p1)) {
271 sokay = USEPREC;
272 prec = atoi(p1);
273 while (isdigit(*++p1))
274 ;
275 } else
276 sokay = NOTOKAY;
277 }
278
279 p2 = p1 + 1; /* Set end pointer. */
280 cs[0] = *p1; /* Set conversion string. */
281 cs[1] = 0;
282
283 /*
284 * Figure out the byte count for each conversion;
285 * rewrite the format as necessary, set up blank-
286 * padding for end of data.
287 */
288 if (*cs == 'c') {
289 pr->flags = F_CHAR;
290 switch(fu->bcnt) {
291 case 0:
292 case 1:
293 pr->bcnt = 1;
294 break;
295 default:
296 p1[1] = '\0';
297 badcnt(p1);
298 }
299 } else if (first_letter(cs, "di")) {
300 pr->flags = F_INT;
301 goto isint;
302 } else if (first_letter(cs, "ouxX")) {
303 pr->flags = F_UINT;
304 isint: cs[3] = '\0';
305 cs[2] = cs[0];
306 cs[1] = 'l';
307 cs[0] = 'l';
308 switch(fu->bcnt) {
309 case 0:
310 pr->bcnt = 4;
311 break;
312 case 1:
313 case 2:
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 (first_letter(cs, "efgEG")) {
323 pr->flags = F_DBL;
324 switch(fu->bcnt) {
325 case 0:
326 pr->bcnt = 8;
327 break;
328 case 4:
329 case 8:
330 pr->bcnt = fu->bcnt;
331 break;
332 default:
333 p1[1] = '\0';
334 badcnt(p1);
335 }
336 } else if(*cs == 's') {
337 pr->flags = F_STR;
338 switch(sokay) {
339 case NOTOKAY:
340 badsfmt();
341 case USEBCNT:
342 pr->bcnt = fu->bcnt;
343 break;
344 case USEPREC:
345 pr->bcnt = prec;
346 break;
347 }
348 } else if (*cs == '_') {
349 ++p2;
350 switch(p1[1]) {
351 case 'A':
352 endfu = fu;
353 fu->flags |= F_IGNORE;
354 /* fallthrough */
355 case 'a':
356 pr->flags = F_ADDRESS;
357 ++p2;
358 if (first_letter(p1 + 2, "dox")) {
359 cs[0] = 'l';
360 cs[1] = 'l';
361 cs[2] = p1[2];
362 cs[3] = '\0';
363 } else {
364 p1[3] = '\0';
365 badconv(p1);
366 }
367 break;
368 case 'c':
369 pr->flags = F_C;
370 /* cs[0] = 'c'; set in conv_c */
371 goto isint2;
372 case 'p':
373 pr->flags = F_P;
374 cs[0] = 'c';
375 goto isint2;
376 case 'u':
377 pr->flags = F_U;
378 /* cs[0] = 'c'; set in conv_u */
379 isint2: switch(fu->bcnt) {
380 case 0:
381 case 1:
382 pr->bcnt = 1;
383 break;
384 default:
385 p1[2] = '\0';
386 badcnt(p1);
387 }
388 break;
389 default:
390 p1[2] = '\0';
391 badconv(p1);
392 }
393 } else {
394 p1[1] = '\0';
395 badconv(p1);
396 }
397
398 /* Color unit(s) specified */
399 if (*p2 == '_' && p2[1] == 'L') {
400 if (colors_wanted()) {
401 char *a;
402
403 /* "cut out" the color_unit(s) */
404 a = strchr(p2, '[');
405 p2 = strrchr(p2, ']');
406 if (a++ && p2)
407 pr->colorlist = color_fmt(xstrndup(a, p2++ - a), pr->bcnt);
408 else
409 badconv(p2);
410 }
411 /* we don't want colors, quietly skip over them */
412 else {
413 p2 = strrchr(p2, ']');
414 /* be a bit louder if we don't know how to skip over them */
415 if (!p2)
416 badconv("_L");
417 ++p2;
418 }
419 }
420 /*
421 * Copy to hexdump_pr format string, set conversion character
422 * pointer, update original.
423 */
424 savech = *p2;
425 p1[0] = '\0';
426 pr->fmt = xmalloc(strlen(fmtp) + strlen(cs) + 1);
427 strcpy(pr->fmt, fmtp);
428 strcat(pr->fmt, cs);
429 *p2 = savech;
430 pr->cchar = pr->fmt + (p1 - fmtp);
431 fmtp = p2;
432
433 /* Only one conversion character if byte count */
434 if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
435 errx(EXIT_FAILURE,
436 _("byte count with multiple conversion characters"));
437 }
438 /*
439 * If format unit byte count not specified, figure it out
440 * so can adjust rep count later.
441 */
442 if (!fu->bcnt)
443 list_for_each(q, &fu->prlist)
444 fu->bcnt
445 += (list_entry(q, struct hexdump_pr, prlist))->bcnt;
446 }
447 /*
448 * If the format string interprets any data at all, and it's
449 * not the same as the blocksize, and its last format unit
450 * interprets any data at all, and has no iteration count,
451 * repeat it as necessary.
452 *
453 * If rep count is greater than 1, no trailing whitespace
454 * gets output from the last iteration of the format unit.
455 */
456 list_for_each (p, &fs->fulist) {
457 fu = list_entry(p, struct hexdump_fu, fulist);
458
459 if (list_entry_is_last(&fu->fulist, &fs->fulist) &&
460 fs->bcnt < hex->blocksize &&
461 !(fu->flags&F_SETREP) && fu->bcnt)
462 fu->reps += (hex->blocksize - fs->bcnt) / fu->bcnt;
463 if (fu->reps > 1 && !list_empty(&fu->prlist)) {
464 pr = list_last_entry(&fu->prlist, struct hexdump_pr, prlist);
465 if (!pr)
466 continue;
467 for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
468 p2 = isspace(*p1) ? p1 : NULL;
469 if (p2)
470 pr->nospace = p2;
471 }
472 }
473 }
474
475 /* [!]color[:string|:hex_number|:oct_number][@offt|@offt_start-offt_end],... */
476 static struct list_head *color_fmt(char *cfmt, int bcnt)
477 {
478 struct hexdump_clr *hc, *hcnext;
479 struct list_head *ret_head;
480 char *clr, *fmt;
481
482 ret_head = xmalloc(sizeof(struct list_head));
483 hcnext = hc = xcalloc(1, sizeof(struct hexdump_clr));
484
485 INIT_LIST_HEAD(&hc->colorlist);
486 INIT_LIST_HEAD(ret_head);
487 list_add_tail(&hc->colorlist, ret_head);
488
489 fmt = cfmt;
490 while (cfmt && *cfmt) {
491 char *end;
492 /* invert this condition */
493 if (*cfmt == '!') {
494 hcnext->invert = 1;
495 ++cfmt;
496 }
497
498 clr = xstrndup(cfmt, strcspn(cfmt, ":@,"));
499 cfmt += strlen(clr);
500 hcnext->fmt = color_sequence_from_colorname(clr);
501 free(clr);
502
503 if (!hcnext->fmt)
504 return NULL;
505
506 /* only colorize this specific value */
507 if (*cfmt == ':') {
508 ++cfmt;
509 /* a hex or oct value */
510 if (*cfmt == '0') {
511 /* hex */
512 errno = 0;
513 end = NULL;
514 if (cfmt[1] == 'x' || cfmt[1] == 'X')
515 hcnext->val = strtoul(cfmt + 2, &end, 16);
516 else
517 hcnext->val = strtoul(cfmt, &end, 8);
518 if (errno || end == cfmt)
519 badfmt(fmt);
520 cfmt = end;
521 /* a string */
522 } else {
523 off_t fmt_end;
524 char endchar;
525 char *endstr;
526
527 hcnext->val = -1;
528 /* temporarily null-delimit the format, so we can reverse-search
529 * for the start of an offset specifier */
530 fmt_end = strcspn(cfmt, ",");
531 endchar = cfmt[fmt_end];
532 cfmt[fmt_end] = '\0';
533 endstr = strrchr(cfmt, '@');
534
535 if (endstr) {
536 if (endstr[1] != '\0')
537 --endstr;
538 hcnext->str = xstrndup(cfmt, endstr - cfmt + 1);
539 } else
540 hcnext->str = xstrndup(cfmt, fmt_end);
541
542 /* restore the character */
543 cfmt[fmt_end] = endchar;
544 cfmt += strlen(hcnext->str);
545 }
546
547 /* no specific value */
548 } else
549 hcnext->val = -1;
550
551 /* only colorize at this offset */
552 hcnext->range = bcnt;
553 if (cfmt && *cfmt == '@') {
554 errno = 0;
555 hcnext->offt = strtoul(++cfmt, &cfmt, 10);
556 if (errno)
557 badfmt(fmt);
558
559 /* offset range */
560 if (*cfmt == '-') {
561 ++cfmt;
562 errno = 0;
563
564 hcnext->range =
565 strtoul(cfmt, &cfmt, 10) - hcnext->offt + 1;
566 if (errno)
567 badfmt(fmt);
568 /* offset range must be between 0 and format byte count */
569 if (hcnext->range < 0)
570 badcnt("_L");
571 /* the offset extends over several print units, clone
572 * the condition, link it in and adjust the address/offset */
573 while (hcnext->range > bcnt) {
574 hc = xcalloc(1, sizeof(struct hexdump_clr));
575 memcpy(hc, hcnext, sizeof(struct hexdump_clr));
576
577 hc->range = bcnt;
578
579 INIT_LIST_HEAD(&hc->colorlist);
580 list_add_tail(&hc->colorlist, ret_head);
581
582 hcnext->offt += bcnt;
583 hcnext->range -= bcnt;
584 }
585 }
586 /* no specific offset */
587 } else
588 hcnext->offt = (off_t)-1;
589
590 /* check if the string we're looking for is the same length as the range */
591 if (hcnext->str && (int)strlen(hcnext->str) != hcnext->range)
592 badcnt("_L");
593
594 /* link in another condition */
595 if (cfmt && *cfmt == ',') {
596 ++cfmt;
597
598 hcnext = xcalloc(1, sizeof(struct hexdump_clr));
599 INIT_LIST_HEAD(&hcnext->colorlist);
600 list_add_tail(&hcnext->colorlist, ret_head);
601 }
602 }
603 return ret_head;
604 }
605
606 static void escape(char *p1)
607 {
608 char *p2;
609
610 /* alphabetic escape sequences have to be done in place */
611 p2 = p1;
612 while (TRUE) {
613 if (!*p1) {
614 *p2 = *p1;
615 break;
616 }
617 if (*p1 == '\\')
618 switch(*++p1) {
619 case 'a':
620 /* *p2 = '\a'; */
621 *p2 = '\007';
622 break;
623 case 'b':
624 *p2 = '\b';
625 break;
626 case 'f':
627 *p2 = '\f';
628 break;
629 case 'n':
630 *p2 = '\n';
631 break;
632 case 'r':
633 *p2 = '\r';
634 break;
635 case 't':
636 *p2 = '\t';
637 break;
638 case 'v':
639 *p2 = '\v';
640 break;
641 default:
642 *p2 = *p1;
643 break;
644 }
645 ++p1; ++p2;
646 }
647 }