hush: fix incorrect PS2 dispaly and trap handling while reading command
[oweals/busybox.git] / util-linux / hexdump.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * hexdump implementation for busybox
4  * Based on code from util-linux v 2.11l
5  *
6  * Copyright (c) 1989
7  * The Regents of the University of California.  All rights reserved.
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10  */
11
12 //usage:#define hexdump_trivial_usage
13 //usage:       "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..."
14 //usage:#define hexdump_full_usage "\n\n"
15 //usage:       "Display FILEs (or stdin) in a user specified format\n"
16 //usage:     "\nOptions:"
17 //usage:     "\n        -b              One-byte octal display"
18 //usage:     "\n        -c              One-byte character display"
19 //usage:     "\n        -C              Canonical hex+ASCII, 16 bytes per line"
20 //usage:     "\n        -d              Two-byte decimal display"
21 //usage:     "\n        -e FORMAT_STRING"
22 //usage:     "\n        -f FORMAT_FILE"
23 //usage:     "\n        -n LENGTH       Interpret only LENGTH bytes of input"
24 //usage:     "\n        -o              Two-byte octal display"
25 //usage:     "\n        -s OFFSET       Skip OFFSET bytes"
26 //usage:     "\n        -v              Display all input data"
27 //usage:     "\n        -x              Two-byte hexadecimal display"
28 //usage:        IF_FEATURE_HEXDUMP_REVERSE(
29 //usage:     "\n        -R              Reverse of 'hexdump -Cv'")
30 //usage:
31 //usage:#define hd_trivial_usage
32 //usage:       "FILE..."
33 //usage:#define hd_full_usage "\n\n"
34 //usage:       "hd is an alias for hexdump -C"
35
36 #include "libbb.h"
37 #include "dump.h"
38
39 /* This is a NOEXEC applet. Be very careful! */
40
41 static void bb_dump_addfile(dumper_t *dumper, char *name)
42 {
43         char *p;
44         FILE *fp;
45         char *buf;
46
47         fp = xfopen_for_read(name);
48         while ((buf = xmalloc_fgetline(fp)) != NULL) {
49                 p = skip_whitespace(buf);
50                 if (*p && (*p != '#')) {
51                         bb_dump_add(dumper, p);
52                 }
53                 free(buf);
54         }
55         fclose(fp);
56 }
57
58 static const char *const add_strings[] = {
59         "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"",   /* b */
60         "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"",   /* c */
61         "\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"",  /* d */
62         "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"",   /* o */
63         "\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"", /* x */
64 };
65
66 static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
67
68 static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
69
70 static const struct suffix_mult suffixes[] = {
71         { "b", 512 },
72         { "k", 1024 },
73         { "m", 1024*1024 },
74         { "", 0 }
75 };
76
77 int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78 int hexdump_main(int argc, char **argv)
79 {
80         dumper_t *dumper = alloc_dumper();
81         const char *p;
82         int ch;
83 #if ENABLE_FEATURE_HEXDUMP_REVERSE
84         FILE *fp;
85         smallint rdump = 0;
86 #endif
87
88         if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
89                 ch = 'C';
90                 goto hd_applet;
91         }
92
93         /* We cannot use getopt32: in hexdump options are cumulative.
94          * E.g. "hexdump -C -C file" should dump each line twice */
95         while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
96                 p = strchr(hexdump_opts, ch);
97                 if (!p)
98                         bb_show_usage();
99                 if ((p - hexdump_opts) < 5) {
100                         bb_dump_add(dumper, add_first);
101                         bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
102                 }
103                 /* Save a little bit of space below by omitting the 'else's. */
104                 if (ch == 'C') {
105  hd_applet:
106                         bb_dump_add(dumper, "\"%08.8_Ax\n\"");
107                         bb_dump_add(dumper, "\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
108                         bb_dump_add(dumper, "\"  |\" 16/1 \"%_p\" \"|\\n\"");
109                 }
110                 if (ch == 'e') {
111                         bb_dump_add(dumper, optarg);
112                 } /* else */
113                 if (ch == 'f') {
114                         bb_dump_addfile(dumper, optarg);
115                 } /* else */
116                 if (ch == 'n') {
117                         dumper->dump_length = xatoi_positive(optarg);
118                 } /* else */
119                 if (ch == 's') { /* compat: -s accepts hex numbers too */
120                         dumper->dump_skip = xstrtoul_range_sfx(optarg, /*base:*/ 0, /*lo:*/ 0, /*hi:*/ LONG_MAX, suffixes);
121                 } /* else */
122                 if (ch == 'v') {
123                         dumper->dump_vflag = ALL;
124                 }
125 #if ENABLE_FEATURE_HEXDUMP_REVERSE
126                 if (ch == 'R') {
127                         rdump = 1;
128                 }
129 #endif
130         }
131
132         if (!dumper->fshead) {
133                 bb_dump_add(dumper, add_first);
134                 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
135         }
136
137         argv += optind;
138
139 #if !ENABLE_FEATURE_HEXDUMP_REVERSE
140         return bb_dump_dump(dumper, argv);
141 #else
142         if (!rdump) {
143                 return bb_dump_dump(dumper, argv);
144         }
145
146         /* -R: reverse of 'hexdump -Cv' */
147         fp = stdin;
148         if (!*argv) {
149                 argv--;
150                 goto jump_in;
151         }
152
153         do {
154                 char *buf;
155                 fp = xfopen_for_read(*argv);
156  jump_in:
157                 while ((buf = xmalloc_fgetline(fp)) != NULL) {
158                         p = buf;
159                         while (1) {
160                                 /* skip address or previous byte */
161                                 while (isxdigit(*p)) p++;
162                                 while (*p == ' ') p++;
163                                 /* '|' char will break the line */
164                                 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
165                                         break;
166                                 putchar(ch);
167                         }
168                         free(buf);
169                 }
170                 fclose(fp);
171         } while (*++argv);
172
173         fflush_stdout_and_exit(EXIT_SUCCESS);
174 #endif
175 }