1 | /* |
---|
2 | * Copyright (c) 1989, 1993 |
---|
3 | * The Regents of the University of California. 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 | #ifndef lint |
---|
35 | static const char copyright[] = |
---|
36 | "@(#) Copyright (c) 1989, 1993\n\ |
---|
37 | The Regents of the University of California. All rights reserved.\n"; |
---|
38 | #endif /* not lint */ |
---|
39 | |
---|
40 | #ifndef lint |
---|
41 | #if 0 |
---|
42 | static char sccsid[] = "@(#)hexdump.c 8.1 (Berkeley) 6/6/93"; |
---|
43 | #include <sys/cdefs.h> |
---|
44 | __FBSDID("$FreeBSD: src/usr.bin/hexdump/hexdump.c,v 1.7 2002/09/04 23:29:01 dwmalone Exp $"); |
---|
45 | #endif |
---|
46 | #endif /* not lint */ |
---|
47 | |
---|
48 | #include <rtems.h> |
---|
49 | #include <rtems/shell.h> |
---|
50 | #include <rtems/shellconfig.h> |
---|
51 | |
---|
52 | #include <sys/types.h> |
---|
53 | #include <locale.h> |
---|
54 | #include <stdlib.h> |
---|
55 | #include <stdio.h> |
---|
56 | #include <string.h> |
---|
57 | #include "hexdump.h" |
---|
58 | |
---|
59 | #if RTEMS_REMOVED |
---|
60 | FS *fshead; /* head of format strings */ |
---|
61 | int blocksize; /* data block size */ |
---|
62 | int exitval; /* final exit value */ |
---|
63 | int length = -1; /* max bytes to read */ |
---|
64 | #endif |
---|
65 | |
---|
66 | void |
---|
67 | rtems_shell_hexdump_exit (rtems_shell_hexdump_globals* globals, int code) |
---|
68 | { |
---|
69 | globals->exit_code = code; |
---|
70 | longjmp (globals->exit_jmp, 1); |
---|
71 | } |
---|
72 | |
---|
73 | static int main_hexdump(rtems_shell_hexdump_globals* globals, int argc, char *argv[]); |
---|
74 | |
---|
75 | int |
---|
76 | rtems_shell_main_hexdump(int argc, char *argv[]) |
---|
77 | { |
---|
78 | rtems_shell_hexdump_globals hexdump_globals; |
---|
79 | rtems_shell_hexdump_globals* globals = &hexdump_globals; |
---|
80 | memset (globals, 0, sizeof (hexdump_globals)); |
---|
81 | vflag = FIRST; |
---|
82 | ateof = 1; |
---|
83 | hexdump_globals.exit_code = 1; |
---|
84 | if (setjmp (hexdump_globals.exit_jmp) == 0) |
---|
85 | hexdump_globals.exit_code = main_hexdump (globals, argc, argv); |
---|
86 | if (curp) |
---|
87 | free (curp); |
---|
88 | if (savp) |
---|
89 | free (savp); |
---|
90 | while (fshead) |
---|
91 | { |
---|
92 | FS* nextfs = fshead->nextfs; |
---|
93 | while (fshead->nextfu) |
---|
94 | { |
---|
95 | FU* nextfu = fshead->nextfu->nextfu; |
---|
96 | if (fshead->nextfu->fmt) |
---|
97 | free(fshead->nextfu->fmt); |
---|
98 | while (fshead->nextfu->nextpr) |
---|
99 | { |
---|
100 | PR* nextpr = fshead->nextfu->nextpr->nextpr; |
---|
101 | if (((fshead->nextfu->nextpr->flags & F_TEXT) == 0) && |
---|
102 | fshead->nextfu->nextpr->fmt) |
---|
103 | free(fshead->nextfu->nextpr->fmt); |
---|
104 | free(fshead->nextfu->nextpr); |
---|
105 | fshead->nextfu->nextpr = nextpr; |
---|
106 | } |
---|
107 | free(fshead->nextfu); |
---|
108 | fshead->nextfu = nextfu; |
---|
109 | } |
---|
110 | free(fshead); |
---|
111 | fshead = nextfs; |
---|
112 | } |
---|
113 | if (hdstdin) |
---|
114 | { |
---|
115 | fclose (hdstdin); |
---|
116 | free (hdstdin); |
---|
117 | } |
---|
118 | return hexdump_globals.exit_code; |
---|
119 | } |
---|
120 | |
---|
121 | int |
---|
122 | main_hexdump(rtems_shell_hexdump_globals* globals, int argc, char *argv[]) |
---|
123 | { |
---|
124 | FS *tfs; |
---|
125 | char *p; |
---|
126 | |
---|
127 | #if RTEMS_REMOVED |
---|
128 | (void)setlocale(LC_ALL, ""); |
---|
129 | #endif |
---|
130 | |
---|
131 | if (!(p = rindex(argv[0], 'o')) || strcmp(p, "od")) |
---|
132 | newsyntax(globals, argc, &argv); |
---|
133 | else |
---|
134 | oldsyntax(globals, argc, &argv); |
---|
135 | |
---|
136 | /* figure out the data block size */ |
---|
137 | for (blocksize = 0, tfs = fshead; tfs; tfs = tfs->nextfs) { |
---|
138 | tfs->bcnt = size(globals, tfs); |
---|
139 | if (blocksize < tfs->bcnt) |
---|
140 | blocksize = tfs->bcnt; |
---|
141 | } |
---|
142 | /* rewrite the rules, do syntax checking */ |
---|
143 | for (tfs = fshead; tfs; tfs = tfs->nextfs) |
---|
144 | rewrite(globals, tfs); |
---|
145 | |
---|
146 | (void)next(globals, argv); |
---|
147 | display(globals); |
---|
148 | exit(exitval); |
---|
149 | return exitval; |
---|
150 | } |
---|
151 | |
---|
152 | rtems_shell_cmd_t rtems_shell_HEXDUMP_Command = { |
---|
153 | "hexdump", /* name */ |
---|
154 | "hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length]\n" /* usage */ |
---|
155 | " [-s skip] [file ...]", |
---|
156 | "files", /* topic */ |
---|
157 | rtems_shell_main_hexdump, /* command */ |
---|
158 | NULL, /* alias */ |
---|
159 | NULL /* next */ |
---|
160 | }; |
---|
161 | |
---|