]>
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" |
098ab077 | 48 | #include "colors.h" |
6dbe3af9 | 49 | |
ffc43748 | 50 | static void escape(char *p1); |
098ab077 | 51 | static struct list_head *color_fmt(char *cfmt, int bcnt); |
1be6ed6a OO |
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 | } | |
fd6b7a7f | 72 | |
d01d144c | 73 | #define first_letter(s,f) strchr(f, *(s)) |
f65e62e0 | 74 | |
046921da | 75 | struct hexdump_fu *endfu; /* format at end-of-data */ |
6dbe3af9 | 76 | |
1f77e9c3 | 77 | void addfile(char *name, struct hexdump *hex) |
6dbe3af9 | 78 | { |
3917a95d | 79 | char *fmt, *buf = NULL; |
6dbe3af9 | 80 | FILE *fp; |
96ea3d32 | 81 | size_t n; |
6dbe3af9 | 82 | |
85bf44b7 SK |
83 | if ((fp = fopen(name, "r")) == NULL) |
84 | err(EXIT_FAILURE, _("can't read %s"), name); | |
96ea3d32 OO |
85 | |
86 | while (getline(&buf, &n, fp) != -1) { | |
3917a95d | 87 | fmt = buf; |
96ea3d32 | 88 | |
3917a95d OO |
89 | while (*fmt && isspace(*fmt)) |
90 | ++fmt; | |
91 | if (!*fmt || *fmt == '#') | |
6dbe3af9 | 92 | continue; |
96ea3d32 | 93 | |
1f77e9c3 | 94 | add_fmt(fmt, hex); |
6dbe3af9 | 95 | } |
96ea3d32 OO |
96 | |
97 | free(buf); | |
9db51207 | 98 | fclose(fp); |
6dbe3af9 KZ |
99 | } |
100 | ||
1f77e9c3 | 101 | void add_fmt(const char *fmt, struct hexdump *hex) |
6dbe3af9 | 102 | { |
cb986008 | 103 | const char *p, *savep; |
bb8ae572 | 104 | struct hexdump_fs *tfs; |
046921da | 105 | struct hexdump_fu *tfu; |
6dbe3af9 | 106 | |
ffc43748 | 107 | /* Start new linked list of format units. */ |
bb8ae572 | 108 | tfs = xcalloc(1, sizeof(struct hexdump_fs)); |
bbc8c153 OO |
109 | INIT_LIST_HEAD(&tfs->fslist); |
110 | INIT_LIST_HEAD(&tfs->fulist); | |
1f77e9c3 | 111 | list_add_tail(&tfs->fslist, &hex->fshead); |
bafd2d46 | 112 | |
ffc43748 | 113 | /* Take the format string and break it up into format units. */ |
cb986008 | 114 | p = fmt; |
9db51207 | 115 | while (TRUE) { |
ffc43748 | 116 | /* Skip leading white space. */ |
0acd3f5d | 117 | if (!*(p = skip_space(p))) |
6dbe3af9 KZ |
118 | break; |
119 | ||
ffc43748 | 120 | /* Allocate a new format unit and link it in. */ |
046921da | 121 | tfu = xcalloc(1, sizeof(struct hexdump_fu)); |
acf74fc2 OO |
122 | tfu->reps = 1; |
123 | ||
bbc8c153 OO |
124 | INIT_LIST_HEAD(&tfu->fulist); |
125 | INIT_LIST_HEAD(&tfu->prlist); | |
126 | list_add_tail(&tfu->fulist, &tfs->fulist); | |
6dbe3af9 | 127 | |
ffc43748 | 128 | /* If leading digit, repetition count. */ |
9db51207 OO |
129 | if (isdigit(*p)) { |
130 | savep = p; | |
577bb86f SK |
131 | while (isdigit(*p)) |
132 | p++; | |
9db51207 | 133 | if (!isspace(*p) && *p != '/') |
6dbe3af9 KZ |
134 | badfmt(fmt); |
135 | /* may overwrite either white space or slash */ | |
cb986008 | 136 | tfu->reps = atoi(savep); |
6dbe3af9 KZ |
137 | tfu->flags = F_SETREP; |
138 | /* skip trailing white space */ | |
0acd3f5d | 139 | p = skip_space(++p); |
6dbe3af9 KZ |
140 | } |
141 | ||
ffc43748 | 142 | /* Skip slash and trailing white space. */ |
6dbe3af9 | 143 | if (*p == '/') |
82233c2a | 144 | p = skip_space(++p); |
6dbe3af9 KZ |
145 | |
146 | /* byte count */ | |
9db51207 OO |
147 | if (isdigit(*p)) { |
148 | savep = p; | |
577bb86f SK |
149 | while (isdigit(*p)) |
150 | p++; | |
9db51207 | 151 | if (!isspace(*p)) |
6dbe3af9 | 152 | badfmt(fmt); |
cb986008 | 153 | tfu->bcnt = atoi(savep); |
6dbe3af9 | 154 | /* skip trailing white space */ |
0acd3f5d | 155 | p = skip_space(++p); |
6dbe3af9 KZ |
156 | } |
157 | ||
158 | /* format */ | |
159 | if (*p != '"') | |
160 | badfmt(fmt); | |
9db51207 | 161 | savep = ++p; |
acf74fc2 | 162 | while (*p != '"') { |
7e6e290b | 163 | if (!*p++) |
6dbe3af9 | 164 | badfmt(fmt); |
acf74fc2 | 165 | } |
85bf44b7 | 166 | tfu->fmt = xmalloc(p - savep + 1); |
cb986008 | 167 | xstrncpy(tfu->fmt, savep, p - savep + 1); |
6dbe3af9 | 168 | escape(tfu->fmt); |
acf74fc2 | 169 | ++p; |
6dbe3af9 KZ |
170 | } |
171 | } | |
172 | ||
ffc43748 | 173 | static const char *spec = ".#-+ 0123456789"; |
fd6b7a7f | 174 | |
bb8ae572 | 175 | int block_size(struct hexdump_fs *fs) |
6dbe3af9 | 176 | { |
046921da | 177 | struct hexdump_fu *fu; |
acf74fc2 | 178 | int bcnt, prec, cursize = 0; |
cb986008 | 179 | char *fmt; |
9db51207 | 180 | struct list_head *p; |
6dbe3af9 KZ |
181 | |
182 | /* figure out the data block size needed for each format unit */ | |
bbc8c153 | 183 | list_for_each (p, &fs->fulist) { |
046921da | 184 | fu = list_entry(p, struct hexdump_fu, fulist); |
6dbe3af9 KZ |
185 | if (fu->bcnt) { |
186 | cursize += fu->bcnt * fu->reps; | |
187 | continue; | |
188 | } | |
9db51207 | 189 | bcnt = prec = 0; |
cb986008 | 190 | fmt = fu->fmt; |
9db51207 OO |
191 | while (*fmt) { |
192 | if (*fmt != '%') { | |
193 | ++fmt; | |
6dbe3af9 | 194 | continue; |
9db51207 | 195 | } |
6dbe3af9 KZ |
196 | /* |
197 | * skip any special chars -- save precision in | |
198 | * case it's a %s format. | |
199 | */ | |
acf74fc2 OO |
200 | while (strchr(spec + 1, *++fmt)) |
201 | ; | |
9db51207 | 202 | if (*fmt == '.' && isdigit(*++fmt)) { |
cb986008 | 203 | prec = atoi(fmt); |
9db51207 OO |
204 | while (isdigit(*++fmt)) |
205 | ; | |
6dbe3af9 | 206 | } |
d01d144c | 207 | if (first_letter(fmt, "diouxX")) |
6dbe3af9 | 208 | bcnt += 4; |
d01d144c | 209 | else if (first_letter(fmt, "efgEG")) |
6dbe3af9 | 210 | bcnt += 8; |
f65e62e0 | 211 | else if (*fmt == 's') |
6dbe3af9 | 212 | bcnt += prec; |
d01d144c | 213 | else if (*fmt == 'c' || (*fmt == '_' && first_letter(++fmt, "cpu"))) |
f65e62e0 | 214 | ++bcnt; |
9db51207 | 215 | ++fmt; |
6dbe3af9 KZ |
216 | } |
217 | cursize += bcnt * fu->reps; | |
218 | } | |
219 | return(cursize); | |
220 | } | |
221 | ||
1f77e9c3 | 222 | void rewrite_rules(struct hexdump_fs *fs, struct hexdump *hex) |
6dbe3af9 KZ |
223 | { |
224 | enum { NOTOKAY, USEBCNT, USEPREC } sokay; | |
4c73d29c | 225 | struct hexdump_pr *pr; |
046921da | 226 | struct hexdump_fu *fu; |
9db51207 | 227 | struct list_head *p, *q; |
acf74fc2 | 228 | char *p1, *p2, *fmtp; |
7235e703 | 229 | char savech, cs[4]; |
acf74fc2 | 230 | int nconv, prec = 0; |
6dbe3af9 | 231 | |
bbc8c153 | 232 | list_for_each (p, &fs->fulist) { |
046921da | 233 | fu = list_entry(p, struct hexdump_fu, fulist); |
6dbe3af9 | 234 | /* |
ffc43748 | 235 | * Break each format unit into print units; each |
6dbe3af9 KZ |
236 | * conversion character gets its own. |
237 | */ | |
9db51207 OO |
238 | nconv = 0; |
239 | fmtp = fu->fmt; | |
240 | while (*fmtp) { | |
4c73d29c | 241 | pr = xcalloc(1, sizeof(struct hexdump_pr)); |
bbc8c153 OO |
242 | INIT_LIST_HEAD(&pr->prlist); |
243 | list_add_tail(&pr->prlist, &fu->prlist); | |
6dbe3af9 | 244 | |
ffc43748 | 245 | /* Skip preceding text and up to the next % sign. */ |
eef27d32 OO |
246 | p1 = fmtp; |
247 | while (*p1 && *p1 != '%') | |
248 | ++p1; | |
6dbe3af9 | 249 | |
ffc43748 | 250 | /* Only text in the string. */ |
6dbe3af9 | 251 | if (!*p1) { |
d2740b0e | 252 | pr->fmt = xstrdup(fmtp); |
6dbe3af9 KZ |
253 | pr->flags = F_TEXT; |
254 | break; | |
255 | } | |
256 | ||
257 | /* | |
ffc43748 | 258 | * Get precision for %s -- if have a byte count, don't |
6dbe3af9 KZ |
259 | * need it. |
260 | */ | |
261 | if (fu->bcnt) { | |
262 | sokay = USEBCNT; | |
263 | /* skip to conversion character */ | |
577bb86f | 264 | for (p1++; strchr(spec, *p1); p1++) |
eef27d32 | 265 | ; |
6dbe3af9 KZ |
266 | } else { |
267 | /* skip any special chars, field width */ | |
eef27d32 OO |
268 | while (strchr(spec + 1, *++p1)) |
269 | ; | |
dabfe2ad | 270 | if (*p1 == '.' && isdigit(*++p1)) { |
6dbe3af9 KZ |
271 | sokay = USEPREC; |
272 | prec = atoi(p1); | |
dabfe2ad | 273 | while (isdigit(*++p1)) |
eef27d32 | 274 | ; |
ffc43748 | 275 | } else |
6dbe3af9 KZ |
276 | sokay = NOTOKAY; |
277 | } | |
278 | ||
ffc43748 KZ |
279 | p2 = p1 + 1; /* Set end pointer. */ |
280 | cs[0] = *p1; /* Set conversion string. */ | |
281 | cs[1] = 0; | |
6dbe3af9 KZ |
282 | |
283 | /* | |
ffc43748 | 284 | * Figure out the byte count for each conversion; |
6dbe3af9 KZ |
285 | * rewrite the format as necessary, set up blank- |
286 | * padding for end of data. | |
287 | */ | |
f65e62e0 OO |
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 | } | |
d01d144c | 299 | } else if (first_letter(cs, "di")) { |
f65e62e0 OO |
300 | pr->flags = F_INT; |
301 | goto isint; | |
d01d144c | 302 | } else if (first_letter(cs, "ouxX")) { |
f65e62e0 | 303 | pr->flags = F_UINT; |
7235e703 NC |
304 | isint: cs[3] = '\0'; |
305 | cs[2] = cs[0]; | |
306 | cs[1] = 'l'; | |
307 | cs[0] = 'l'; | |
f65e62e0 OO |
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 | } | |
d01d144c | 322 | } else if (first_letter(cs, "efgEG")) { |
f65e62e0 OO |
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) { | |
eef27d32 OO |
339 | case NOTOKAY: |
340 | badsfmt(); | |
341 | case USEBCNT: | |
342 | pr->bcnt = fu->bcnt; | |
343 | break; | |
344 | case USEPREC: | |
345 | pr->bcnt = prec; | |
346 | break; | |
f65e62e0 OO |
347 | } |
348 | } else if (*cs == '_') { | |
349 | ++p2; | |
350 | switch(p1[1]) { | |
351 | case 'A': | |
352 | endfu = fu; | |
353 | fu->flags |= F_IGNORE; | |
b1557fe9 | 354 | /* fallthrough */ |
f65e62e0 OO |
355 | case 'a': |
356 | pr->flags = F_ADDRESS; | |
357 | ++p2; | |
d01d144c | 358 | if (first_letter(p1 + 2, "dox")) { |
7235e703 NC |
359 | cs[0] = 'l'; |
360 | cs[1] = 'l'; | |
361 | cs[2] = p1[2]; | |
362 | cs[3] = '\0'; | |
f65e62e0 OO |
363 | } else { |
364 | p1[3] = '\0'; | |
eef27d32 | 365 | badconv(p1); |
f65e62e0 OO |
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); | |
6dbe3af9 KZ |
396 | } |
397 | ||
098ab077 OO |
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 | } | |
6dbe3af9 | 420 | /* |
4c73d29c | 421 | * Copy to hexdump_pr format string, set conversion character |
6dbe3af9 KZ |
422 | * pointer, update original. |
423 | */ | |
424 | savech = *p2; | |
ffc43748 | 425 | p1[0] = '\0'; |
85bf44b7 | 426 | pr->fmt = xmalloc(strlen(fmtp) + strlen(cs) + 1); |
eef27d32 OO |
427 | strcpy(pr->fmt, fmtp); |
428 | strcat(pr->fmt, cs); | |
6dbe3af9 KZ |
429 | *p2 = savech; |
430 | pr->cchar = pr->fmt + (p1 - fmtp); | |
431 | fmtp = p2; | |
432 | ||
ffc43748 | 433 | /* Only one conversion character if byte count */ |
85bf44b7 SK |
434 | if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) |
435 | errx(EXIT_FAILURE, | |
436 | _("byte count with multiple conversion characters")); | |
6dbe3af9 KZ |
437 | } |
438 | /* | |
ffc43748 | 439 | * If format unit byte count not specified, figure it out |
6dbe3af9 KZ |
440 | * so can adjust rep count later. |
441 | */ | |
442 | if (!fu->bcnt) | |
bbc8c153 | 443 | list_for_each(q, &fu->prlist) |
4c73d29c OO |
444 | fu->bcnt |
445 | += (list_entry(q, struct hexdump_pr, prlist))->bcnt; | |
6dbe3af9 KZ |
446 | } |
447 | /* | |
ffc43748 | 448 | * If the format string interprets any data at all, and it's |
6dbe3af9 KZ |
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 | * | |
ffc43748 | 453 | * If rep count is greater than 1, no trailing whitespace |
6dbe3af9 KZ |
454 | * gets output from the last iteration of the format unit. |
455 | */ | |
bbc8c153 | 456 | list_for_each (p, &fs->fulist) { |
046921da | 457 | fu = list_entry(p, struct hexdump_fu, fulist); |
acf74fc2 OO |
458 | |
459 | if (list_entry_is_last(&fu->fulist, &fs->fulist) && | |
1f77e9c3 | 460 | fs->bcnt < hex->blocksize && |
acf74fc2 | 461 | !(fu->flags&F_SETREP) && fu->bcnt) |
1f77e9c3 | 462 | fu->reps += (hex->blocksize - fs->bcnt) / fu->bcnt; |
74ce680a SK |
463 | if (fu->reps > 1 && !list_empty(&fu->prlist)) { |
464 | pr = list_last_entry(&fu->prlist, struct hexdump_pr, prlist); | |
577bb86f SK |
465 | if (!pr) |
466 | continue; | |
74ce680a SK |
467 | for (p1 = pr->fmt, p2 = NULL; *p1; ++p1) |
468 | p2 = isspace(*p1) ? p1 : NULL; | |
469 | if (p2) | |
470 | pr->nospace = p2; | |
6dbe3af9 | 471 | } |
6dbe3af9 KZ |
472 | } |
473 | } | |
474 | ||
098ab077 OO |
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); | |
58ce71e6 | 500 | hcnext->fmt = color_sequence_from_colorname(clr); |
098ab077 OO |
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 */ | |
3bf7ede9 | 569 | if (hcnext->range < 0) |
098ab077 | 570 | badcnt("_L"); |
3bf7ede9 OO |
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 | } | |
098ab077 OO |
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 | } | |
6dbe3af9 | 605 | |
ffc43748 | 606 | static void escape(char *p1) |
6dbe3af9 | 607 | { |
ffc43748 | 608 | char *p2; |
6dbe3af9 KZ |
609 | |
610 | /* alphabetic escape sequences have to be done in place */ | |
9c2cb9b0 OO |
611 | p2 = p1; |
612 | while (TRUE) { | |
6dbe3af9 KZ |
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 | } | |
9c2cb9b0 | 645 | ++p1; ++p2; |
6dbe3af9 KZ |
646 | } |
647 | } |