xref: /original-bsd/usr.bin/hexdump/hexsyntax.c (revision e59fb703)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)hexsyntax.c	5.2 (Berkeley) 05/08/90";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <stdio.h>
14 #include "hexdump.h"
15 
16 off_t skip;				/* bytes to skip */
17 
18 newsyntax(argc, argvp)
19 	int argc;
20 	char ***argvp;
21 {
22 	extern enum _vflag vflag;
23 	extern FS *fshead;
24 	extern char *optarg;
25 	extern int length, optind;
26 	int ch;
27 	char *p, **argv;
28 
29 	argv = *argvp;
30 	while ((ch = getopt(argc, argv, "bcde:f:n:os:vx")) != EOF)
31 		switch (ch) {
32 		case 'b':
33 			add("\"%07.7_Ax\n\"");
34 			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
35 			break;
36 		case 'c':
37 			add("\"%07.7_Ax\n\"");
38 			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
39 			break;
40 		case 'd':
41 			add("\"%07.7_Ax\n\"");
42 			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
43 			break;
44 		case 'e':
45 			add(optarg);
46 			break;
47 		case 'f':
48 			addfile(optarg);
49 			break;
50 		case 'n':
51 			if ((length = atoi(optarg)) < 0) {
52 				(void)fprintf(stderr,
53 				    "hexdump: bad length value.\n");
54 				exit(1);
55 			}
56 			break;
57 		case 'o':
58 			add("\"%07.7_Ax\n\"");
59 			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
60 			break;
61 		case 's':
62 			if ((skip = strtol(optarg, &p, 0)) < 0) {
63 				(void)fprintf(stderr,
64 				    "hexdump: bad skip value.\n");
65 				exit(1);
66 			}
67 			switch(*p) {
68 			case 'b':
69 				skip *= 512;
70 				break;
71 			case 'k':
72 				skip *= 1024;
73 				break;
74 			case 'm':
75 				skip *= 1048576;
76 				break;
77 			}
78 			break;
79 		case 'v':
80 			vflag = ALL;
81 			break;
82 		case 'x':
83 			add("\"%07.7_Ax\n\"");
84 			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
85 			break;
86 		case '?':
87 			usage();
88 			exit(1);
89 		}
90 
91 	if (!fshead) {
92 		add("\"%07.7_Ax\n\"");
93 		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
94 	}
95 
96 	*argvp += optind;
97 }
98 
99 usage()
100 {
101 	(void)fprintf(stderr,
102 "hexdump: [-bcdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]\n");
103 	exit(1);
104 }
105