2012-06-15 00:08:37 +02:00
|
|
|
/* No, nothing to to with IDE from Apple Inc.
|
|
|
|
* testbed for ../util/hex-escape.c.
|
|
|
|
*
|
|
|
|
* usage:
|
|
|
|
* hex-xcode [--direction=(encode|decode)] [--omit-newline] < file
|
|
|
|
* hex-xcode [--direction=(encode|decode)] [--omit-newline] [--in-place] arg1 arg2 arg3 ...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notmuch-client.h"
|
|
|
|
#include "hex-escape.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
enum direction {
|
|
|
|
ENCODE,
|
|
|
|
DECODE
|
|
|
|
};
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
static bool inplace = false;
|
2012-06-15 00:08:37 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
xcode (void *ctx, enum direction dir, char *in, char **buf_p, size_t *size_p)
|
|
|
|
{
|
|
|
|
hex_status_t status;
|
|
|
|
|
|
|
|
if (dir == ENCODE)
|
|
|
|
status = hex_encode (ctx, in, buf_p, size_p);
|
|
|
|
else
|
|
|
|
if (inplace) {
|
|
|
|
status = hex_decode_inplace (in);
|
|
|
|
*buf_p = in;
|
|
|
|
*size_p = strlen(in);
|
|
|
|
} else {
|
|
|
|
status = hex_decode (ctx, in, buf_p, size_p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == HEX_SUCCESS)
|
|
|
|
fputs (*buf_p, stdout);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
int dir = DECODE;
|
2017-10-07 10:44:04 +02:00
|
|
|
bool omit_newline = false;
|
2012-06-15 00:08:37 +02:00
|
|
|
|
|
|
|
notmuch_opt_desc_t options[] = {
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_keyword = &dir, .name = "direction", .keywords =
|
2012-06-15 00:08:37 +02:00
|
|
|
(notmuch_keyword_t []){ { "encode", ENCODE },
|
|
|
|
{ "decode", DECODE },
|
|
|
|
{ 0, 0 } } },
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_bool = &omit_newline, .name = "omit-newline" },
|
|
|
|
{ .opt_bool = &inplace, .name = "in-place" },
|
|
|
|
{ }
|
2012-06-15 00:08:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int opt_index = parse_arguments (argc, argv, options, 1);
|
|
|
|
|
|
|
|
if (opt_index < 0)
|
|
|
|
exit (1);
|
|
|
|
|
|
|
|
void *ctx = talloc_new (NULL);
|
|
|
|
|
|
|
|
char *line = NULL;
|
|
|
|
size_t line_size;
|
|
|
|
ssize_t line_len;
|
|
|
|
|
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buf_size = 0;
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
bool read_stdin = true;
|
2012-06-15 00:08:37 +02:00
|
|
|
|
|
|
|
for (; opt_index < argc; opt_index++) {
|
|
|
|
|
|
|
|
if (xcode (ctx, dir, argv[opt_index],
|
|
|
|
&buffer, &buf_size) != HEX_SUCCESS)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (! omit_newline)
|
|
|
|
putchar ('\n');
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
read_stdin = false;
|
2012-06-15 00:08:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! read_stdin)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while ((line_len = getline (&line, &line_size, stdin)) != -1) {
|
|
|
|
|
|
|
|
chomp_newline (line);
|
|
|
|
|
|
|
|
if (xcode (ctx, dir, line, &buffer, &buf_size) != HEX_SUCCESS)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (! omit_newline)
|
|
|
|
putchar ('\n');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line)
|
|
|
|
free (line);
|
|
|
|
|
|
|
|
talloc_free (ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|