aboutsummaryrefslogtreecommitdiffstats
path: root/cmdline.c
blob: 182a29dade828d2bdbefb40b296ea8cf3ba5384c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* See LICENSE file for copyright and license details. */
#include "common.h"


static enum command command = ANYSUM;
static enum libhashsum_algorithm algorithm;


#define USAGE_COMMON   "[-c [-w]] [-W options] ... [-z] [file] ..."
#define USAGE_BSUM     "[-l bits] [-S salt] "USAGE_COMMON
#define USAGE_SHA3SUM  "[-a bits] "USAGE_COMMON
#define USAGE_KECCAK   "[-R rate] [-C capacity] [-N output-size] [-S state-size] [-W word-size] [-Z squeeze-count] "USAGE_COMMON
#define USAGE_SHAKE    "[-N output-bits] "USAGE_COMMON
#define USAGE_RAWSHAKE "[-N output-bits] "USAGE_COMMON
#define USAGE_ANYSUM   "(-c [-w] | [-a algoritms] ... ) [-W options] ... [-z] [file] ..."

#define static
NUSAGE(libsimple_default_failure_exit,
       (command == ANYSUM  ? USAGE_ANYSUM :
	command == BSUM    ? USAGE_BSUM :
	command == SHA3SUM ? USAGE_SHA3SUM :
	(algorithm == LIBHASHSUM_BLAKE224 ||
	 algorithm == LIBHASHSUM_BLAKE256 ||
	 algorithm == LIBHASHSUM_BLAKE384 ||
	 algorithm == LIBHASHSUM_BLAKE512) ? USAGE_BSUM :
	(algorithm == LIBHASHSUM_SHAKE128 ||
	 algorithm == LIBHASHSUM_SHAKE256 ||
	 algorithm == LIBHASHSUM_SHAKE512) ? USAGE_SHAKE :
	(algorithm == LIBHASHSUM_RAWSHAKE128 ||
	 algorithm == LIBHASHSUM_RAWSHAKE256 ||
	 algorithm == LIBHASHSUM_RAWSHAKE512) ? USAGE_RAWSHAKE :
	algorithm == LIBHASHSUM_KECCAK ? USAGE_KECCAK : USAGE_COMMON));
#undef static


void
cmdline(int *argcp, char ***argvp, struct config *config)
{
	int old_argc = *argcp;
	int argc = *argcp;
	char **argv = *argvp;
	const char *algostr = NULL;
	char *algostrbuf = NULL;

	argv0 = argv[0];
	command = getcommand(&algostr, &algorithm);
	switch (getsupercommand(command, algorithm)) {
	case BSUM:
		argc = cmdline_bsum(argc, argv, command, config, &algostr, &algorithm, &algostrbuf);
		break;
	case SHA3SUM:
		argc = cmdline_sha3sum(argc, argv, command, config, &algostr, &algorithm, &algostrbuf);
		break;
	case ANYSUM:
	case SPECIALISED:
	default:
		argc = cmdline_other(argc, argv, command, config);
		break;
	}

	if (config->warn_improper_format > config->verify)
		usage();

	if (command == ANYSUM) {
		if (config->verify && config->nalgorithms)
			usage();
		config->format |= WITH_ALGOSTR;
	}
	if (!config->verify) {
		if ((config->format & FORMAT_MASK) == BINARY)
			config->format &= FORMAT_MASK;
	}

	if (algostr) {
		config->algorithms = erealloc(config->algorithms, sizeof(*config->algorithms));
		(config->algorithms)[0].algostr = algostr;
		config->nalgorithms = 1U;
	}

	*argcp = argc;
	*argvp = &argv[old_argc - argc];

	free(algostrbuf);
}