3 * Common test driver for encoding and decoding
5 * (c) 2009 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
47 /*----- Static variables --------------------------------------------------*/
49 static const codec_class
*cctab
[] = {
60 static const struct { const char *name
; unsigned f
; } flagtab
[] = {
61 { "lowerc", CDCF_LOWERC
},
62 { "igncase", CDCF_IGNCASE
},
63 { "noeqpad", CDCF_NOEQPAD
},
64 { "igneqpad", CDCF_IGNEQPAD
},
65 { "igneqmid", CDCF_IGNEQMID
},
66 { "ignzpad", CDCF_IGNZPAD
},
67 { "ignnewl", CDCF_IGNNEWL
},
68 { "igninvch", CDCF_IGNINVCH
},
69 { "ignjunk", CDCF_IGNJUNK
},
73 /*----- Main code ---------------------------------------------------------*/
75 static void usage(FILE *fp
)
78 "Usage: $ [-de] [-f FLAGS] [-i INDENT] [-m MAXLINE] [-o OUTPUT]\n"
79 "\tCODEC [FILE ...]\n");
82 static void version(FILE *fp
)
83 { pquis(fp
, "$, mLib version " VERSION
"\n"); }
85 static void help(FILE *fp
)
93 Encodes and decodes binary files. Options provided:\n\
95 -h, --help Show this help text.\n\
96 -v, --version Show the program's version number.\n\
97 -u, --usage Show a terse usage message.\n\
99 -d, --decode Decode binary FILEs.\n\
100 -e, --encode Encode binary FILEs (default).\n\
101 -f, --flags=FLAGS Set encoding/decoding FLAGS.\n\
102 -i, --indent=INDENT Indent each line with INDENT.\n\
103 -m, --maxline=MAXLINE Limit line length to (about) MAXLINE.\n\
104 -o, --output=OUTPUT Write encoded/decoded data to OUTPUT.\n\
106 #define ENUM(label, tab, end, getname) do { \
107 fputs(label ":", fp); \
108 for (i = 0; tab[i]end; i++) fprintf(fp, " %s", tab[i]getname); \
111 ENUM("Codecs", cctab
, != 0, ->name
);
112 ENUM("Flags", flagtab
, .name
, .name
);
116 static void code(codec
*c
, const char *ifile
, FILE *ifp
, FILE *ofp
)
124 n
= fread(buf
, 1, sizeof(buf
), ifp
);
126 if ((err
= c
->ops
->code(c
, buf
, n
, &d
)) != 0)
127 die(EXIT_FAILURE
, "decoding error: %s", codec_strerror(err
));
128 } while (fwrite(d
.buf
, 1, d
.len
, ofp
) == d
.len
&& n
== sizeof(buf
));
130 die(EXIT_FAILURE
, "error reading `%s': %s", ifile
, strerror(errno
));
133 int main(int argc
, char *argv
[])
135 enum { encode
, decode
};
137 const codec_class
**cc
;
139 const char *indent
= "";
140 const char *imode
, *omode
, *ofile
= 0;
141 unsigned maxline
= 64;
142 unsigned f
= CDCF_IGNNEWL
;
145 FILE *ifp
, *ofp
= stdout
;
152 #define f_bogus 32768u
157 static const struct option opts
[] = {
158 { "help", 0, 0, 'h' },
159 { "version", 0, 0, 'v' },
160 { "usage", 0, 0, 'u' },
161 { "encode", 0, 0, 'e' },
162 { "decode", 0, 0, 'd' },
163 { "indent", OPTF_ARGREQ
, 0, 'i' },
164 { "maxline", OPTF_ARGREQ
, 0, 'm' },
165 { "output", OPTF_ARGREQ
, 0, 'o' },
166 { "flags", OPTF_ARGREQ
, 0, 'f' },
170 if ((i
= mdwopt(argc
, argv
, "hvu" "edf:i:m:o:", opts
, 0, 0, 0)) < 0)
173 case 'h': help(stdout
); exit(0);
174 case 'v': version(stdout
); exit(0);
175 case 'u': usage(stdout
); exit(0);
177 case 'e': mode
= encode
; break;
178 case 'd': mode
= decode
; break;
179 case 'i': indent
= optarg
; break;
180 case 'o': ofile
= optarg
; break;
184 maxline
= strtoul(optarg
, &q
, 0);
185 if (*q
|| errno
) die(EXIT_FAILURE
, "bad integer `%s'", optarg
);
191 if (*p
== '-') { sense
= 0; p
++; }
192 else if (*p
== '+') { sense
= 1; p
++; }
195 for (i
= 0; flagtab
[i
].name
; i
++) {
196 if (strlen(flagtab
[i
].name
) == n
&&
197 strncmp(flagtab
[i
].name
, p
, n
) == 0)
200 die(EXIT_FAILURE
, "unknown flag `%.*s'", (int)n
, p
);
202 if (sense
) f
|= flagtab
[i
].f
;
203 else f
&= ~flagtab
[i
].f
;
209 default: f
|= f_bogus
; break;
212 argv
+= optind
; argc
-= optind
;
213 if ((f
& f_bogus
) || !argc
) { usage(stderr
); exit(EXIT_FAILURE
); }
215 for (cc
= cctab
;; cc
++) {
216 if (!*cc
) die(EXIT_FAILURE
, "unknown codec `%s'", *argv
);
217 else if (strcmp(*argv
, (*cc
)->name
) == 0) break;
224 for (p
= indent
;; p
++) {
229 case 'a': DPUTC(&d
, '\n'); break;
230 case 'b': DPUTC(&d
, '\b'); break;
231 case 'f': DPUTC(&d
, '\f'); break;
232 case 'n': DPUTC(&d
, '\n'); break;
233 case 'r': DPUTC(&d
, '\r'); break;
234 case 't': DPUTC(&d
, '\t'); break;
235 case 'v': DPUTC(&d
, '\v'); break;
236 case 0: goto done_indent
; break;
237 default: DPUTC(&d
, *p
); break;
249 c
= (*cc
)->encoder(f
, d
.buf
, maxline
);
250 imode
= "rb"; omode
= "w";
253 c
= (*cc
)->decoder(f
);
254 imode
= "r"; omode
= "wb";
260 if (ofile
&& (ofp
= fopen(ofile
, omode
)) == 0) {
261 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
262 ofile
, strerror(errno
));
265 if (mode
== encode
) fwrite(d
.buf
+ 1, 1, d
.len
- 1, ofp
);
267 code(c
, "<stdin>", stdin
, ofp
);
268 else for (i
= 0; i
< argc
; i
++) {
269 if (strcmp(argv
[i
], "-") == 0)
270 code(c
, "<stdin>", stdin
, ofp
);
271 else if ((ifp
= fopen(argv
[i
], imode
)) == 0) {
272 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
273 argv
[i
], strerror(errno
));
275 code(c
, argv
[i
], ifp
, ofp
);
280 if ((err
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
281 die(EXIT_FAILURE
, "decoding error: %s", codec_strerror(err
));
282 if (mode
== encode
) DPUTC(&d
, '\n');
283 fwrite(d
.buf
, 1, d
.len
, ofp
);
284 c
->ops
->destroy(c
); DDESTROY(&d
);
286 if (ferror(ofp
) || fflush(ofp
) || fclose(ofp
)) {
287 die(EXIT_FAILURE
, "error writing to `%s': %s",
288 ofile ? ofile
: "<stdout>", strerror(errno
));
293 /*----- That's all, folks -------------------------------------------------*/