Compare commits
2 commits
feat/comma
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f7878a0246 | ||
| 27797d1ea0 |
31 changed files with 307 additions and 383 deletions
|
|
@ -3,11 +3,12 @@
|
||||||
|
|
||||||
#include <cli.h>
|
#include <cli.h>
|
||||||
|
|
||||||
int cli_handle_help(const char *arg, void *config);
|
int cli_handle_help(const char *arg, void *config_void);
|
||||||
int cli_handle_version(const char *arg, void *config);
|
int cli_handle_version(const char *arg, void *config);
|
||||||
int cli_handle_p(const char *arg, void *config);
|
int digest_handle_help(const char *arg, void *config);
|
||||||
int cli_handle_q(const char *arg, void *config);
|
int digest_handle_p(const char *arg, void *config);
|
||||||
int cli_handle_r(const char *arg, void *config);
|
int digest_handle_q(const char *arg, void *config);
|
||||||
int cli_handle_s(const char *arg, void *config);
|
int digest_handle_r(const char *arg, void *config);
|
||||||
|
int digest_handle_s(const char *arg, void *config);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,15 @@ enum command_category {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct command_descriptor {
|
struct command_descriptor {
|
||||||
const char *name;
|
const char *name;
|
||||||
enum command_category category;
|
enum command_category category;
|
||||||
const struct option_descriptor *opts;
|
const struct option_descriptor *opts;
|
||||||
size_t nb_opts;
|
size_t nb_opts;
|
||||||
const void *impl;
|
const void *impl;
|
||||||
int (*run)(int argc, char **argv, const struct command_descriptor *self);
|
int (*run)(int argc, char **argv, const struct command_descriptor *self);
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct command_descriptor *find_command(const char *name);
|
const struct command_descriptor *find_command(const char *name);
|
||||||
void print_commands(FILE *out);
|
void print_commands(FILE *out);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -5,32 +5,14 @@
|
||||||
#include <cli.h>
|
#include <cli.h>
|
||||||
|
|
||||||
#include "internal/cli/cli_handlers.h"
|
#include "internal/cli/cli_handlers.h"
|
||||||
#include "internal/cmd/command.h"
|
#include "internal/cli/command.h"
|
||||||
|
|
||||||
#define DIGEST_OPTION_LIST \
|
#define DIGEST_OPTION_LIST \
|
||||||
X( \
|
|
||||||
'h', \
|
|
||||||
"help", \
|
|
||||||
no_argument, \
|
|
||||||
cli_handle_help, \
|
|
||||||
OPT_ARG_NONE, \
|
|
||||||
"Display this help and exit", \
|
|
||||||
0 \
|
|
||||||
) \
|
|
||||||
X( \
|
|
||||||
'V', \
|
|
||||||
"version", \
|
|
||||||
no_argument, \
|
|
||||||
cli_handle_version, \
|
|
||||||
OPT_ARG_NONE, \
|
|
||||||
"Display version information and exit", \
|
|
||||||
0 \
|
|
||||||
) \
|
|
||||||
X( \
|
X( \
|
||||||
'p', \
|
'p', \
|
||||||
"stdin", \
|
"stdin", \
|
||||||
no_argument, \
|
no_argument, \
|
||||||
cli_handle_p, \
|
digest_handle_p, \
|
||||||
OPT_ARG_NONE, \
|
OPT_ARG_NONE, \
|
||||||
"Echo stdin to stdout and append checksum to output", \
|
"Echo stdin to stdout and append checksum to output", \
|
||||||
0 \
|
0 \
|
||||||
|
|
@ -39,7 +21,7 @@
|
||||||
'q', \
|
'q', \
|
||||||
"quiet", \
|
"quiet", \
|
||||||
no_argument, \
|
no_argument, \
|
||||||
cli_handle_q, \
|
digest_handle_q, \
|
||||||
OPT_ARG_NONE, \
|
OPT_ARG_NONE, \
|
||||||
"Quiet mode: only print the digest", \
|
"Quiet mode: only print the digest", \
|
||||||
0 \
|
0 \
|
||||||
|
|
@ -48,7 +30,7 @@
|
||||||
'r', \
|
'r', \
|
||||||
"reverse", \
|
"reverse", \
|
||||||
no_argument, \
|
no_argument, \
|
||||||
cli_handle_r, \
|
digest_handle_r, \
|
||||||
OPT_ARG_NONE, \
|
OPT_ARG_NONE, \
|
||||||
"Reverse the output format (digest then filename)", \
|
"Reverse the output format (digest then filename)", \
|
||||||
0 \
|
0 \
|
||||||
|
|
@ -57,11 +39,29 @@
|
||||||
's', \
|
's', \
|
||||||
"string", \
|
"string", \
|
||||||
required_argument, \
|
required_argument, \
|
||||||
cli_handle_s, \
|
digest_handle_s, \
|
||||||
OPT_ARG_STRING, \
|
OPT_ARG_STRING, \
|
||||||
"Hash a string", \
|
"Hash a string", \
|
||||||
CLI_OPT_F_REPEATABLE \
|
CLI_OPT_F_REPEATABLE \
|
||||||
|
) \
|
||||||
|
X( \
|
||||||
|
'h', \
|
||||||
|
"help", \
|
||||||
|
no_argument, \
|
||||||
|
digest_handle_help, \
|
||||||
|
OPT_ARG_NONE, \
|
||||||
|
"Display this help and exit", \
|
||||||
|
0 \
|
||||||
)
|
)
|
||||||
|
// X( \
|
||||||
|
// 'V', \
|
||||||
|
// "version", \
|
||||||
|
// no_argument, \
|
||||||
|
// digest_handle_version, \
|
||||||
|
// OPT_ARG_NONE, \
|
||||||
|
// "Display version information and exit", \
|
||||||
|
// 0 \
|
||||||
|
// )
|
||||||
|
|
||||||
#undef X
|
#undef X
|
||||||
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1
|
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1
|
||||||
6
includes/internal/cli/dispatch.h
Normal file
6
includes/internal/cli/dispatch.h
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef FT_SSL_CLI_DISPATCH_H
|
||||||
|
#define FT_SSL_CLI_DISPATCH_H
|
||||||
|
|
||||||
|
int ft_ssl_dispatch(int argc, char **argv);
|
||||||
|
|
||||||
|
#endif
|
||||||
45
includes/internal/cli/global_cmd.h
Normal file
45
includes/internal/cli/global_cmd.h
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#ifndef FT_SSL_CMD_GLOBAL_H
|
||||||
|
#define FT_SSL_CMD_GLOBAL_H
|
||||||
|
|
||||||
|
#include <getopt.h>
|
||||||
|
#include <cli.h>
|
||||||
|
|
||||||
|
#include "internal/cli/cli_handlers.h"
|
||||||
|
|
||||||
|
#define GLOBAL_OPTION_LIST \
|
||||||
|
X( \
|
||||||
|
'h', \
|
||||||
|
"help", \
|
||||||
|
no_argument, \
|
||||||
|
cli_handle_help, \
|
||||||
|
OPT_ARG_NONE, \
|
||||||
|
"Display this help and exit", \
|
||||||
|
0 \
|
||||||
|
) \
|
||||||
|
X( \
|
||||||
|
'v', \
|
||||||
|
"version", \
|
||||||
|
no_argument, \
|
||||||
|
cli_handle_version, \
|
||||||
|
OPT_ARG_NONE, \
|
||||||
|
"Display version information and exit", \
|
||||||
|
0 \
|
||||||
|
)
|
||||||
|
|
||||||
|
#undef X
|
||||||
|
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1
|
||||||
|
enum { GLOBAL_OPT_LEN = (0 GLOBAL_OPTION_LIST) };
|
||||||
|
|
||||||
|
#undef X
|
||||||
|
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
|
||||||
|
+ (1 * (has_arg == no_argument) \
|
||||||
|
+ (2 * (has_arg == required_argument) \
|
||||||
|
+ (3 * (has_arg == optional_argument))))
|
||||||
|
enum { GLOBAL_OPTSTR_LEN = (2 GLOBAL_OPTION_LIST) };
|
||||||
|
/* +2 for ':' to disable getopt error messages and \0 */
|
||||||
|
|
||||||
|
#undef X
|
||||||
|
|
||||||
|
extern const struct option_descriptor g_global_opts[];
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -2,74 +2,31 @@
|
||||||
#define FT_SSL_OPT_H
|
#define FT_SSL_OPT_H
|
||||||
|
|
||||||
#include <cli.h>
|
#include <cli.h>
|
||||||
|
#include <libft_ssl.h>
|
||||||
|
|
||||||
#define FT_SSL_OPTION_LIST \
|
#include "internal/cli/command.h"
|
||||||
X( \
|
#include "internal/cli/digest_cmd.h"
|
||||||
'h', \
|
|
||||||
"help", \
|
|
||||||
no_argument, \
|
|
||||||
cli_handle_help, \
|
|
||||||
OPT_ARG_NONE, \
|
|
||||||
"Display this help and exit", \
|
|
||||||
0 \
|
|
||||||
) \
|
|
||||||
X( \
|
|
||||||
'V', \
|
|
||||||
"version", \
|
|
||||||
no_argument, \
|
|
||||||
cli_handle_version, \
|
|
||||||
OPT_ARG_NONE, \
|
|
||||||
"Display version information and exit", \
|
|
||||||
0 \
|
|
||||||
) \
|
|
||||||
X( \
|
|
||||||
'p', \
|
|
||||||
"stdin", \
|
|
||||||
no_argument, \
|
|
||||||
cli_handle_p, \
|
|
||||||
OPT_ARG_NONE, \
|
|
||||||
"Echo stdin to stdout and append checksum to output", \
|
|
||||||
0 \
|
|
||||||
) \
|
|
||||||
X( \
|
|
||||||
'q', \
|
|
||||||
"quiet", \
|
|
||||||
no_argument, \
|
|
||||||
cli_handle_q, \
|
|
||||||
OPT_ARG_NONE, \
|
|
||||||
"Quiet mode: only print the digest", \
|
|
||||||
0 \
|
|
||||||
) \
|
|
||||||
X( \
|
|
||||||
'r', \
|
|
||||||
"reverse", \
|
|
||||||
no_argument, \
|
|
||||||
cli_handle_r, \
|
|
||||||
OPT_ARG_NONE, \
|
|
||||||
"Reverse the output format (digest then filename)", \
|
|
||||||
0 \
|
|
||||||
) \
|
|
||||||
X( \
|
|
||||||
's', \
|
|
||||||
"string", \
|
|
||||||
required_argument, \
|
|
||||||
cli_handle_s, \
|
|
||||||
OPT_ARG_STRING, \
|
|
||||||
"Hash a string", \
|
|
||||||
CLI_OPT_F_REPEATABLE \
|
|
||||||
)
|
|
||||||
|
|
||||||
#undef X
|
|
||||||
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1
|
|
||||||
enum { FT_SSL_OPT_LEN = (0 FT_SSL_OPTION_LIST ) };
|
|
||||||
|
|
||||||
#undef X
|
/* Forward-declare all digest algo globals */
|
||||||
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
|
#define DIGEST_ALGO(lower, ds, bs) extern const struct digest_algo g_##lower;
|
||||||
+ (1 * (has_arg == no_argument) \
|
#include <digest_algos.h>
|
||||||
+ (2 * (has_arg == required_argument) \
|
#undef DIGEST_ALGO
|
||||||
+ (3 * (has_arg == optional_argument))))
|
|
||||||
enum { FT_SSL_OPTSTR_LEN = (2 FT_SSL_OPTION_LIST) };
|
static const struct command_descriptor g_commands[] = {
|
||||||
/* +2 for ':' to disable getopt error messages and \0 */
|
/* Digest commands */
|
||||||
|
#define DIGEST_ALGO(lower, ds, bs) \
|
||||||
|
{ #lower, CMD_DIGEST, g_digest_opts, DIGEST_OPT_LEN, &g_##lower, digest_run },
|
||||||
|
#include <digest_algos.h>
|
||||||
|
#undef DIGEST_ALGO
|
||||||
|
/* Cipher commands (not yet implemented) */
|
||||||
|
{ "base64", CMD_CIPHER, NULL, 0, NULL, NULL },
|
||||||
|
{ "des", CMD_CIPHER, NULL, 0, NULL, NULL },
|
||||||
|
{ "des-ecb", CMD_CIPHER, NULL, 0, NULL, NULL },
|
||||||
|
{ "des-cbc", CMD_CIPHER, NULL, 0, NULL, NULL },
|
||||||
|
/* Sentinel */
|
||||||
|
{ NULL, 0, NULL, 0, NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
void print_usage(const char *prog);
|
void print_usage(const char *prog);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef FT_SSL_SSL_H
|
#ifndef FT_SSL_CMD_DIGEST_ENGINE_H
|
||||||
#define FT_SSL_SSL_H
|
#define FT_SSL_CMD_DIGEST_ENGINE_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
@ -8,9 +8,9 @@
|
||||||
#define MAX_DIGEST_SIZE 64
|
#define MAX_DIGEST_SIZE 64
|
||||||
#define READ_BUFSIZE 4096
|
#define READ_BUFSIZE 4096
|
||||||
|
|
||||||
int run_string(const struct digest_config *config, const char *s);
|
int digest_run_string(const struct digest_config *config, const char *s);
|
||||||
int run_file(const struct digest_config *config, const char *path);
|
int digest_run_file(const struct digest_config *config, const char *path);
|
||||||
int run_stdin(const struct digest_config *config);
|
int digest_run_stdin(const struct digest_config *config);
|
||||||
int digest_stream(const struct digest_config *config, int fd,
|
int digest_stream(const struct digest_config *config, int fd,
|
||||||
const char *label, int show_algo, int echo);
|
const char *label, int show_algo, int echo);
|
||||||
void print_digest(const struct digest_config *config, const uint8_t *digest,
|
void print_digest(const struct digest_config *config, const uint8_t *digest,
|
||||||
|
|
@ -41,21 +41,25 @@ FORCE:
|
||||||
|
|
||||||
ft_ssl_SOURCES = \
|
ft_ssl_SOURCES = \
|
||||||
main.c \
|
main.c \
|
||||||
cmd/command_table.c \
|
cli/dispatch.c \
|
||||||
cmd/digest_run.c \
|
cli/command_table.c \
|
||||||
ssl/run_stdin.c \
|
cli/print_version.c \
|
||||||
ssl/run_string.c \
|
cli/print_help.c \
|
||||||
ssl/run_file.c \
|
|
||||||
ssl/digest.c \
|
|
||||||
ssl/output.c \
|
|
||||||
cli/version.c \
|
|
||||||
cli/handlers/option_map.c \
|
|
||||||
cli/handlers/handle_help.c \
|
|
||||||
cli/handlers/handle_version.c \
|
cli/handlers/handle_version.c \
|
||||||
cli/handlers/handle_p.c \
|
cli/handlers/handle_help.c \
|
||||||
cli/handlers/handle_q.c \
|
cli/handlers/global/option_map.c \
|
||||||
cli/handlers/handle_r.c \
|
cli/handlers/digest/option_map.c \
|
||||||
cli/handlers/handle_s.c
|
cli/handlers/digest/handle_help.c \
|
||||||
|
cli/handlers/digest/handle_p.c \
|
||||||
|
cli/handlers/digest/handle_q.c \
|
||||||
|
cli/handlers/digest/handle_r.c \
|
||||||
|
cli/handlers/digest/handle_s.c \
|
||||||
|
cmd/digest/run.c \
|
||||||
|
cmd/digest/run_stdin.c \
|
||||||
|
cmd/digest/run_string.c \
|
||||||
|
cmd/digest/run_file.c \
|
||||||
|
cmd/digest/digest.c \
|
||||||
|
cmd/digest/output.c
|
||||||
|
|
||||||
ft_ssl_CPPFLAGS = \
|
ft_ssl_CPPFLAGS = \
|
||||||
-I $(top_srcdir)/includes \
|
-I $(top_srcdir)/includes \
|
||||||
|
|
|
||||||
16
src/cli/command_table.c
Normal file
16
src/cli/command_table.c
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "internal/cli/option.h"
|
||||||
|
|
||||||
|
|
||||||
|
const struct command_descriptor *
|
||||||
|
find_command(const char *name)
|
||||||
|
{
|
||||||
|
const struct command_descriptor *cmd;
|
||||||
|
|
||||||
|
for (cmd = g_commands; NULL != cmd->name; cmd++)
|
||||||
|
if (0 == strcmp(cmd->name, name))
|
||||||
|
return cmd;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
79
src/cli/dispatch.c
Normal file
79
src/cli/dispatch.c
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
|
#include <cli.h>
|
||||||
|
|
||||||
|
#include "internal/cli/command.h"
|
||||||
|
#include "internal/cli/dispatch.h"
|
||||||
|
#include "internal/cli/global_cmd.h"
|
||||||
|
#include "version_gen.h"
|
||||||
|
|
||||||
|
/* Forward declarations */
|
||||||
|
static int find_subcommand_index(int argc, char **argv);
|
||||||
|
static enum cli_code parse_global_options(char **argv, int sub_idx);
|
||||||
|
static int dispatch_command(int argc, char **argv, int sub_idx);
|
||||||
|
/* ------------------- */
|
||||||
|
|
||||||
|
int
|
||||||
|
ft_ssl_dispatch(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int sub_idx;
|
||||||
|
enum cli_code ret;
|
||||||
|
|
||||||
|
sub_idx = find_subcommand_index(argc, argv);
|
||||||
|
ret = parse_global_options(argv, sub_idx);
|
||||||
|
if (CLI_SUCCESS != ret)
|
||||||
|
return ((int)CLI_ERROR == ret) ? 2 : EXIT_SUCCESS;
|
||||||
|
return dispatch_command(argc, argv, sub_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
find_subcommand_index(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 1; i < argc; i++)
|
||||||
|
if ('-' != argv[i][0])
|
||||||
|
return i;
|
||||||
|
return argc;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum cli_code
|
||||||
|
parse_global_options(char **argv, int sub_idx)
|
||||||
|
{
|
||||||
|
char opt_str[GLOBAL_OPTSTR_LEN + 1];
|
||||||
|
struct option long_opts[GLOBAL_OPT_LEN + 1];
|
||||||
|
|
||||||
|
return cli_parse(sub_idx, argv, NULL, g_global_opts, GLOBAL_OPT_LEN,
|
||||||
|
opt_str, long_opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
dispatch_command(int argc, char **argv, int sub_idx)
|
||||||
|
{
|
||||||
|
const struct command_descriptor *cmd;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (sub_idx >= argc)
|
||||||
|
{
|
||||||
|
print_commands(stderr);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
cmd = find_command(argv[sub_idx]);
|
||||||
|
if (NULL == cmd)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: Error: %s is an invalid command.\n",
|
||||||
|
g_prog_name, argv[sub_idx]);
|
||||||
|
print_commands(stderr);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
if (NULL == cmd->run)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: %s: not yet implemented\n",
|
||||||
|
g_prog_name, argv[sub_idx]);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
ret = cmd->run(argc - sub_idx, argv + sub_idx, cmd);
|
||||||
|
return ((int)CLI_ERROR == ret) ? 2 : EXIT_SUCCESS;
|
||||||
|
}
|
||||||
17
src/cli/handlers/digest/handle_help.c
Normal file
17
src/cli/handlers/digest/handle_help.c
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#include <cli.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "compiler.h"
|
||||||
|
#include "ft_ssl.h"
|
||||||
|
#include "internal/cli/digest_cmd.h"
|
||||||
|
#include "version_gen.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
digest_handle_help(__unused const char *arg, void *config_void)
|
||||||
|
{
|
||||||
|
struct digest_config *config = (struct digest_config *)config_void;
|
||||||
|
|
||||||
|
printf("Usage: %s %s [OPTIONS] [FILE...]\n\n", g_prog_name, config->cmd_name);
|
||||||
|
cli_print_options(g_digest_opts, DIGEST_OPT_LEN, cli_arg_type_to_str);
|
||||||
|
return CLI_EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
#include "internal/cli/cli_handlers.h"
|
#include "internal/cli/cli_handlers.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_p(__unused const char *arg, void *config_void)
|
digest_handle_p(__unused const char *arg, void *config_void)
|
||||||
{
|
{
|
||||||
struct digest_config *config = (struct digest_config *)config_void;
|
struct digest_config *config = (struct digest_config *)config_void;
|
||||||
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
#include "internal/cli/cli_handlers.h"
|
#include "internal/cli/cli_handlers.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_q(__unused const char *arg, void *config_void)
|
digest_handle_q(__unused const char *arg, void *config_void)
|
||||||
{
|
{
|
||||||
struct digest_config *config = (struct digest_config *)config_void;
|
struct digest_config *config = (struct digest_config *)config_void;
|
||||||
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
#include "internal/cli/cli_handlers.h"
|
#include "internal/cli/cli_handlers.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_r(__unused const char *arg, void *config_void)
|
digest_handle_r(__unused const char *arg, void *config_void)
|
||||||
{
|
{
|
||||||
struct digest_config *config = (struct digest_config *)config_void;
|
struct digest_config *config = (struct digest_config *)config_void;
|
||||||
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
#include "internal/cli/cli_handlers.h"
|
#include "internal/cli/cli_handlers.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_s(const char *arg, void *config_void)
|
digest_handle_s(const char *arg, void *config_void)
|
||||||
{
|
{
|
||||||
struct digest_config *config = (struct digest_config *)config_void;
|
struct digest_config *config = (struct digest_config *)config_void;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "internal/cmd/digest_cmd.h"
|
#include "internal/cli/digest_cmd.h"
|
||||||
|
|
||||||
#undef X
|
#undef X
|
||||||
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
|
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
|
||||||
12
src/cli/handlers/global/option_map.c
Normal file
12
src/cli/handlers/global/option_map.c
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#include "internal/cli/global_cmd.h"
|
||||||
|
|
||||||
|
#undef X
|
||||||
|
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
|
||||||
|
{ short_opt, long_opt, has_arg, handler, arg_type, desc, flags },
|
||||||
|
|
||||||
|
const struct option_descriptor g_global_opts[] = {
|
||||||
|
GLOBAL_OPTION_LIST
|
||||||
|
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL, 0}
|
||||||
|
};
|
||||||
|
|
||||||
|
#undef X
|
||||||
|
|
@ -1,17 +1,18 @@
|
||||||
#include <cli.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <libft_ssl.h>
|
||||||
|
#include <cli.h>
|
||||||
|
|
||||||
|
#include "internal/cli/global_cmd.h"
|
||||||
|
#include "internal/cli/option.h"
|
||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
#include "ft_ssl.h"
|
|
||||||
#include "internal/cmd/digest_cmd.h"
|
|
||||||
#include "version_gen.h"
|
#include "version_gen.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_help(__unused const char *arg, void *config_void)
|
cli_handle_help(__unused const char *arg, __unused void *config_void)
|
||||||
{
|
{
|
||||||
struct digest_config *config = (struct digest_config *)config_void;
|
printf("Usage: %s [OPTIONS] <command> [args...]\n\n", g_prog_name);
|
||||||
|
cli_print_options(g_global_opts, GLOBAL_OPT_LEN, cli_arg_type_to_str);
|
||||||
printf("Usage: %s %s [OPTIONS] [FILE...]\n\n", g_prog_name, config->cmd_name);
|
print_commands(stdout);
|
||||||
cli_print_options(g_digest_opts, DIGEST_OPT_LEN, cli_arg_type_to_str);
|
|
||||||
return CLI_EXIT_SUCCESS;
|
return CLI_EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <cli.h>
|
#include <cli.h>
|
||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,91 +0,0 @@
|
||||||
#include <getopt.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <cli.h>
|
|
||||||
#include <libft_ssl.h>
|
|
||||||
|
|
||||||
#include "ft_ssl.h"
|
|
||||||
#include "internal/cli/cli_handlers.h"
|
|
||||||
#include "internal/cli/interactive.h"
|
|
||||||
#include "internal/cli/option.h"
|
|
||||||
|
|
||||||
/* Forward declarations */
|
|
||||||
static void init_config(struct ssl_config *config);
|
|
||||||
static const struct digest_algo *find_algo(const char *name);
|
|
||||||
static enum cli_code parse_subcommand(int argc, char **argv, int first_arg,
|
|
||||||
struct ssl_config *config);
|
|
||||||
/* ------------------- */
|
|
||||||
|
|
||||||
static const struct digest_algo * const s_algos[] = {
|
|
||||||
#define DIGEST_ALGO(lower, ds, bs) &g_##lower,
|
|
||||||
#include <digest_algos.h>
|
|
||||||
#undef DIGEST_ALGO
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
|
|
||||||
enum cli_code
|
|
||||||
cli_parse_arguments(int argc, char **argv, struct ssl_config *config)
|
|
||||||
{
|
|
||||||
char opt_str[FT_SSL_OPTSTR_LEN];
|
|
||||||
struct option long_opts[FT_SSL_OPT_LEN + 1];
|
|
||||||
enum cli_code ret;
|
|
||||||
|
|
||||||
init_config(config);
|
|
||||||
ret = cli_parse(argc, argv, config, g_options, FT_SSL_OPT_LEN, opt_str, long_opts);
|
|
||||||
if (CLI_SUCCESS != ret)
|
|
||||||
return ret;
|
|
||||||
return parse_subcommand(argc, argv, optind, config);
|
|
||||||
}
|
|
||||||
|
|
||||||
static enum cli_code
|
|
||||||
parse_subcommand(int argc, char **argv, int first_arg,
|
|
||||||
struct ssl_config *config)
|
|
||||||
{
|
|
||||||
if (first_arg >= argc)
|
|
||||||
return run_interactive();
|
|
||||||
config->algo = find_algo(argv[first_arg]);
|
|
||||||
if (NULL == config->algo)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: unknown command -- '%s'\n", argv[0], argv[first_arg]);
|
|
||||||
print_usage(argv[0]);
|
|
||||||
return CLI_ERROR;
|
|
||||||
}
|
|
||||||
config->files = argv + first_arg + 1;
|
|
||||||
config->nb_files = argc - first_arg - 1;
|
|
||||||
return CLI_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct digest_algo *
|
|
||||||
find_algo(const char *name)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i = 0; NULL != s_algos[i]; i++)
|
|
||||||
{
|
|
||||||
if (0 == strcmp(s_algos[i]->name, name))
|
|
||||||
return s_algos[i];
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
print_usage(const char *prog)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
fprintf(stderr, "usage: %s {", prog);
|
|
||||||
for (i = 0; NULL != s_algos[i]; i++)
|
|
||||||
{
|
|
||||||
if (0 != i)
|
|
||||||
fprintf(stderr, "|");
|
|
||||||
fprintf(stderr, "%s", s_algos[i]->name);
|
|
||||||
}
|
|
||||||
fprintf(stderr, "} [-pqr] [-s string] [file...]\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
init_config(struct ssl_config *config)
|
|
||||||
{
|
|
||||||
memset(config, 0, sizeof(struct ssl_config));
|
|
||||||
}
|
|
||||||
20
src/cli/print_help.c
Normal file
20
src/cli/print_help.c
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <libft_ssl.h>
|
||||||
|
|
||||||
|
#include "internal/cli/option.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
print_commands(FILE *out)
|
||||||
|
{
|
||||||
|
const struct command_descriptor *cmd;
|
||||||
|
|
||||||
|
fprintf(out, "\nMessage Digest commands:\n");
|
||||||
|
for (cmd = g_commands; NULL != cmd->name; cmd++)
|
||||||
|
if (CMD_DIGEST == cmd->category)
|
||||||
|
fprintf(out, " %s\n", cmd->name);
|
||||||
|
fprintf(out, "\nCipher commands:\n");
|
||||||
|
for (cmd = g_commands; NULL != cmd->name; cmd++)
|
||||||
|
if (CMD_CIPHER == cmd->category)
|
||||||
|
fprintf(out, " %s\n", cmd->name);
|
||||||
|
}
|
||||||
|
|
@ -1,54 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <libft_ssl.h>
|
|
||||||
|
|
||||||
#include "internal/cmd/command.h"
|
|
||||||
#include "internal/cmd/digest_cmd.h"
|
|
||||||
|
|
||||||
/* Forward-declare all digest algo globals */
|
|
||||||
#define DIGEST_ALGO(lower, ds, bs) extern const struct digest_algo g_##lower;
|
|
||||||
#include <digest_algos.h>
|
|
||||||
#undef DIGEST_ALGO
|
|
||||||
|
|
||||||
static const struct command_descriptor s_commands[] = {
|
|
||||||
/* Digest commands */
|
|
||||||
#define DIGEST_ALGO(lower, ds, bs) \
|
|
||||||
{ #lower, CMD_DIGEST, g_digest_opts, DIGEST_OPT_LEN, &g_##lower, digest_run },
|
|
||||||
#include <digest_algos.h>
|
|
||||||
#undef DIGEST_ALGO
|
|
||||||
/* Cipher commands (not yet implemented) */
|
|
||||||
{ "base64", CMD_CIPHER, NULL, 0, NULL, NULL },
|
|
||||||
{ "des", CMD_CIPHER, NULL, 0, NULL, NULL },
|
|
||||||
{ "des-ecb", CMD_CIPHER, NULL, 0, NULL, NULL },
|
|
||||||
{ "des-cbc", CMD_CIPHER, NULL, 0, NULL, NULL },
|
|
||||||
/* Sentinel */
|
|
||||||
{ NULL, 0, NULL, 0, NULL, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
const struct command_descriptor *
|
|
||||||
find_command(const char *name)
|
|
||||||
{
|
|
||||||
const struct command_descriptor *cmd;
|
|
||||||
|
|
||||||
for (cmd = s_commands; NULL != cmd->name; cmd++)
|
|
||||||
if (0 == strcmp(cmd->name, name))
|
|
||||||
return cmd;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
print_commands(FILE *out)
|
|
||||||
{
|
|
||||||
const struct command_descriptor *cmd;
|
|
||||||
|
|
||||||
fprintf(out, "\nStandard commands:\n");
|
|
||||||
fprintf(out, "\nMessage Digest commands:\n");
|
|
||||||
for (cmd = s_commands; NULL != cmd->name; cmd++)
|
|
||||||
if (CMD_DIGEST == cmd->category)
|
|
||||||
fprintf(out, " %s\n", cmd->name);
|
|
||||||
fprintf(out, "\nCipher commands:\n");
|
|
||||||
for (cmd = s_commands; NULL != cmd->name; cmd++)
|
|
||||||
if (CMD_CIPHER == cmd->category)
|
|
||||||
fprintf(out, " %s\n", cmd->name);
|
|
||||||
}
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
#include <libft_ssl.h>
|
#include <libft_ssl.h>
|
||||||
|
|
||||||
#include "ft_ssl.h"
|
#include "ft_ssl.h"
|
||||||
#include "internal/ssl/ssl.h"
|
#include "internal/cmd/digest.h"
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
static void process_chunk(const struct digest_config *config, union digest_ctx *ctx,
|
static void process_chunk(const struct digest_config *config, union digest_ctx *ctx,
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
#include "ft_ssl.h"
|
#include "ft_ssl.h"
|
||||||
#include "ft_ssl_flags.h"
|
#include "ft_ssl_flags.h"
|
||||||
#include "internal/ssl/ssl.h"
|
#include "internal/cmd/digest.h"
|
||||||
|
|
||||||
enum fmt_mode
|
enum fmt_mode
|
||||||
{
|
{
|
||||||
|
|
@ -7,9 +7,9 @@
|
||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
#include "ft_ssl.h"
|
#include "ft_ssl.h"
|
||||||
#include "ft_ssl_flags.h"
|
#include "ft_ssl_flags.h"
|
||||||
#include "internal/cmd/command.h"
|
#include "internal/cli/command.h"
|
||||||
#include "internal/cmd/digest_cmd.h"
|
#include "internal/cli/digest_cmd.h"
|
||||||
#include "internal/ssl/ssl.h"
|
#include "internal/cmd/digest.h"
|
||||||
#include "version_gen.h"
|
#include "version_gen.h"
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
|
|
@ -32,7 +32,7 @@ digest_run(int argc, char **argv, const struct command_descriptor *self)
|
||||||
config.cmd_name = self->name;
|
config.cmd_name = self->name;
|
||||||
|
|
||||||
ret = cli_parse(argc, argv, &config, self->opts, self->nb_opts,
|
ret = cli_parse(argc, argv, &config, self->opts, self->nb_opts,
|
||||||
opt_str, long_opts);
|
opt_str, long_opts);
|
||||||
if (CLI_SUCCESS != ret)
|
if (CLI_SUCCESS != ret)
|
||||||
return (int)ret;
|
return (int)ret;
|
||||||
|
|
||||||
|
|
@ -45,11 +45,11 @@ digest_run(int argc, char **argv, const struct command_descriptor *self)
|
||||||
ret_val = (int)CLI_SUCCESS;
|
ret_val = (int)CLI_SUCCESS;
|
||||||
has_input = (0 < config.nb_strings || 0 < config.nb_files);
|
has_input = (0 < config.nb_strings || 0 < config.nb_files);
|
||||||
if (HAS_FLAG(config.flags, FLAG_P) || !has_input)
|
if (HAS_FLAG(config.flags, FLAG_P) || !has_input)
|
||||||
update_ret(&ret_val, run_stdin(&config));
|
update_ret(&ret_val, digest_run_stdin(&config));
|
||||||
for (i = 0; i < config.nb_strings; i++)
|
for (i = 0; i < config.nb_strings; i++)
|
||||||
update_ret(&ret_val, run_string(&config, config.strings[i]));
|
update_ret(&ret_val, digest_run_string(&config, config.strings[i]));
|
||||||
for (i = 0; i < config.nb_files; i++)
|
for (i = 0; i < config.nb_files; i++)
|
||||||
update_ret(&ret_val, run_file(&config, config.files[i]));
|
update_ret(&ret_val, digest_run_file(&config, config.files[i]));
|
||||||
return ret_val;
|
return ret_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,7 +61,7 @@ check_algo(const struct digest_config *config)
|
||||||
|| NULL == config->algo->final)
|
|| NULL == config->algo->final)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: %s: not yet implemented\n",
|
fprintf(stderr, "%s: %s: not yet implemented\n",
|
||||||
g_prog_name, config->algo->name);
|
g_prog_name, config->algo->name);
|
||||||
return (int)CLI_ERROR;
|
return (int)CLI_ERROR;
|
||||||
}
|
}
|
||||||
return (int)CLI_SUCCESS;
|
return (int)CLI_SUCCESS;
|
||||||
|
|
@ -7,11 +7,11 @@
|
||||||
#include <cli.h>
|
#include <cli.h>
|
||||||
|
|
||||||
#include "ft_ssl.h"
|
#include "ft_ssl.h"
|
||||||
#include "internal/ssl/ssl.h"
|
#include "internal/cmd/digest.h"
|
||||||
#include "version_gen.h"
|
#include "version_gen.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
run_file(const struct digest_config *config, const char *path)
|
digest_run_file(const struct digest_config *config, const char *path)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
#include "ft_ssl.h"
|
#include "ft_ssl.h"
|
||||||
#include "ft_ssl_flags.h"
|
#include "ft_ssl_flags.h"
|
||||||
#include "internal/ssl/ssl.h"
|
#include "internal/cmd/digest.h"
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
static int run_stdin_p(const struct digest_config *config);
|
static int run_stdin_p(const struct digest_config *config);
|
||||||
|
|
@ -17,7 +17,7 @@ static ssize_t read_and_hash(const struct digest_config *config, union digest_ct
|
||||||
/* ------------------- */
|
/* ------------------- */
|
||||||
|
|
||||||
int
|
int
|
||||||
run_stdin(const struct digest_config *config)
|
digest_run_stdin(const struct digest_config *config)
|
||||||
{
|
{
|
||||||
if (HAS_FLAG(config->flags, FLAG_P) && !HAS_FLAG(config->flags, FLAG_Q))
|
if (HAS_FLAG(config->flags, FLAG_P) && !HAS_FLAG(config->flags, FLAG_Q))
|
||||||
return run_stdin_p(config);
|
return run_stdin_p(config);
|
||||||
|
|
@ -5,10 +5,10 @@
|
||||||
#include <libft_ssl.h>
|
#include <libft_ssl.h>
|
||||||
|
|
||||||
#include "ft_ssl.h"
|
#include "ft_ssl.h"
|
||||||
#include "internal/ssl/ssl.h"
|
#include "internal/cmd/digest.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
run_string(const struct digest_config *config, const char *s)
|
digest_run_string(const struct digest_config *config, const char *s)
|
||||||
{
|
{
|
||||||
union digest_ctx ctx;
|
union digest_ctx ctx;
|
||||||
uint8_t digest[MAX_DIGEST_SIZE];
|
uint8_t digest[MAX_DIGEST_SIZE];
|
||||||
35
src/main.c
35
src/main.c
|
|
@ -1,10 +1,6 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <cli.h>
|
#include <cli.h>
|
||||||
|
|
||||||
#include "internal/cmd/command.h"
|
#include "internal/cli/dispatch.h"
|
||||||
#include "version_gen.h"
|
#include "version_gen.h"
|
||||||
|
|
||||||
char *g_prog_name = NULL;
|
char *g_prog_name = NULL;
|
||||||
|
|
@ -12,34 +8,7 @@ char *g_prog_name = NULL;
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
const struct command_descriptor *cmd;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
g_prog_name = argv[0];
|
g_prog_name = argv[0];
|
||||||
cli_set_prog_name(g_prog_name);
|
cli_set_prog_name(g_prog_name);
|
||||||
if (argc < 2)
|
return ft_ssl_dispatch(argc, argv);
|
||||||
{
|
|
||||||
print_commands(stderr);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
if (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help"))
|
|
||||||
{
|
|
||||||
print_commands(stdout);
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
||||||
cmd = find_command(argv[1]);
|
|
||||||
if (NULL == cmd)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: Error: %s is an invalid command.\n",
|
|
||||||
argv[0], argv[1]);
|
|
||||||
print_commands(stderr);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
if (NULL == cmd->run)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: %s: not yet implemented\n", argv[0], argv[1]);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
ret = cmd->run(argc - 1, argv + 1, cmd);
|
|
||||||
return ((int)CLI_ERROR == ret) ? 2 : EXIT_SUCCESS;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,56 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <cli.h>
|
|
||||||
#include <libft_ssl.h>
|
|
||||||
|
|
||||||
#include "compiler.h"
|
|
||||||
#include "ft_ssl.h"
|
|
||||||
#include "ft_ssl_flags.h"
|
|
||||||
#include "internal/ssl/ssl.h"
|
|
||||||
#include "version_gen.h"
|
|
||||||
|
|
||||||
/* Forward declarations */
|
|
||||||
static int check_algo(const struct ssl_config *config);
|
|
||||||
static inline void update_ret(int *ret, int result);
|
|
||||||
/* ------------------- */
|
|
||||||
|
|
||||||
int
|
|
||||||
ssl_run(const struct ssl_config *config)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
int has_input;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (CLI_SUCCESS != check_algo(config))
|
|
||||||
return CLI_ERROR;
|
|
||||||
ret = CLI_SUCCESS;
|
|
||||||
has_input = (0 < config->nb_strings || 0 < config->nb_files);
|
|
||||||
if (HAS_FLAG(config->flags, FLAG_P) || !has_input)
|
|
||||||
update_ret(&ret, run_stdin(config));
|
|
||||||
for (i = 0; i < config->nb_strings; i++)
|
|
||||||
update_ret(&ret, run_string(config, config->strings[i]));
|
|
||||||
for (i = 0; i < config->nb_files; i++)
|
|
||||||
update_ret(&ret, run_file(config, config->files[i]));
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
check_algo(const struct ssl_config *config)
|
|
||||||
{
|
|
||||||
if (NULL == config->algo->init
|
|
||||||
|| NULL == config->algo->update
|
|
||||||
|| NULL == config->algo->final)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: %s: not yet implemented\n",
|
|
||||||
g_prog_name, config->algo->name);
|
|
||||||
return CLI_ERROR;
|
|
||||||
}
|
|
||||||
return CLI_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void
|
|
||||||
update_ret(int *ret, int result)
|
|
||||||
{
|
|
||||||
if (CLI_ERROR == result)
|
|
||||||
*ret = CLI_ERROR;
|
|
||||||
}
|
|
||||||
Loading…
Add table
Reference in a new issue