mirror of
https://git.disroot.org/80486DX2-66/polonium.git
synced 2024-12-27 23:50:29 +05:30
simplify arguments
This commit is contained in:
parent
f81100fbf1
commit
a881b80a2b
3
Makefile
3
Makefile
@ -91,7 +91,8 @@ distclean: testclean
|
||||
|
||||
# Variables for testing
|
||||
EXEC_LOG = ./exec.log
|
||||
PROGRAM_ARGS := -p 60 -t 2 -n 2 --noconfirm -c -s 999999999
|
||||
PROGRAM_ARGS := -probability 60 -threshold 2 -passes 2 -noconfirm -contents \
|
||||
-seed 999999999
|
||||
|
||||
test:
|
||||
@if [ ! -e "$(EXECPATH)" ]; then \
|
||||
|
96
src/main.c
96
src/main.c
@ -34,14 +34,16 @@ enum arg_destinations {
|
||||
};
|
||||
|
||||
/* constant values: arguments */
|
||||
const char* ARGS_HELP[] = { "-h", "--help" };
|
||||
const char* ARGS_PROBABILITY[] = { "-p", "--probability" };
|
||||
const char* ARGS_THRESHOLD[] = { "-t", "--threshold" };
|
||||
const char* ARGS_PASSES[] = { "-n", "--passes" };
|
||||
const char* ARGS_CONTENTS[] = { "-c", "--contents" };
|
||||
const char* ARGS_LINE_ENDINGS[] = { "-l", "--line-endings" };
|
||||
const char* ARGS_PRINTABLE[] = { "-P", "--printable" };
|
||||
const char* ARGS_SEED[] = { "-s", "--seed" };
|
||||
const char* ARGS_HELP[] = { "-help", "-h", "--help", NULL };
|
||||
#define ARG_HELP (ARGS_HELP[0])
|
||||
const char* ARG_PROBABILITY = "-probability";
|
||||
const char* ARG_THRESHOLD = "-threshold";
|
||||
const char* ARG_PASSES = "-passes";
|
||||
const char* ARG_NOCONFIRM = "-noconfirm";
|
||||
const char* ARG_CONTENTS = "-contents";
|
||||
const char* ARG_LINE_ENDINGS = "-line-endings";
|
||||
const char* ARG_PRINTABLE = "-printable";
|
||||
const char* ARG_SEED = "-seed";
|
||||
|
||||
/* global variables */
|
||||
uint32_t PRNG_seed_value;
|
||||
@ -55,7 +57,7 @@ uint8_t config = C_CONFIRM;
|
||||
}
|
||||
|
||||
/* macros: lambdas */
|
||||
#define ARG_MATCH(s, args2) (!strcmp(s, args2[0]) || !strcmp(s, args2[1]))
|
||||
#define ARG_MATCH(s1, s2) (!strcmp(s1, s2))
|
||||
|
||||
/* default values */
|
||||
static uint16_t probability = 100;
|
||||
@ -66,6 +68,7 @@ static size_t passes = 1;
|
||||
static char* correct_slashes(const char* path);
|
||||
static char* my_basename(const char* raw_path);
|
||||
static void parse_value(uint8_t destination, const char* arg);
|
||||
bool args_match(const char* arg, const char* args_list[]);
|
||||
|
||||
/* function implementations */
|
||||
static char* correct_slashes(const char* path) {
|
||||
@ -151,10 +154,18 @@ static void parse_value(uint8_t destination, const char* arg) {
|
||||
}
|
||||
}
|
||||
|
||||
bool args_match(const char* arg, const char* args_list[]) {
|
||||
for (const char** p = args_list; *p != NULL; ++p)
|
||||
if (!strcmp(arg, *p))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
puts("Polonium: a file corrupter\n");
|
||||
|
||||
if (argc < 2 || ARG_MATCH(argv[argc - 1], ARGS_HELP)) {
|
||||
if (argc < 2 || args_match(argv[argc - 1], ARGS_HELP)) {
|
||||
char* program_name = my_basename(argv[0]);
|
||||
printf(
|
||||
"Usage: %s <file to corrupt> [parameters] [options]\n"
|
||||
@ -162,45 +173,43 @@ int main(int argc, char** argv) {
|
||||
"Both parameters and options are optional.\n"
|
||||
"\n"
|
||||
"[parameters]:\n"
|
||||
" --probability : Determines the likelihood of a neutron "
|
||||
" -probability : Determines the likelihood of a neutron "
|
||||
"radiation event\n"
|
||||
" occurring. The higher the value, the more "
|
||||
" occurring. The higher the value, the more "
|
||||
"likely the bits are\n"
|
||||
" to be corrupted.\n"
|
||||
" Value range: [1..%" PRIu16 "]\n"
|
||||
" Default value: %" PRIu16 "\n"
|
||||
" to be corrupted.\n"
|
||||
" Value range: [1..%" PRIu16 "]\n"
|
||||
" Default value: %" PRIu16 "\n"
|
||||
"\n"
|
||||
" --threshold : Controls the amount of change allowed for each "
|
||||
" -threshold : Controls the amount of change allowed for each "
|
||||
"byte. The\n"
|
||||
" higher the value, the more extensive the "
|
||||
" higher the value, the more extensive the "
|
||||
"modifications will\n"
|
||||
" be.\n"
|
||||
" Value range: [1.." INT2STR(CHAR_BIT) "]\n"
|
||||
" Default value: %" PRIu8 "\n"
|
||||
" be.\n"
|
||||
" Value range: [1.." INT2STR(CHAR_BIT) "]\n"
|
||||
" Default value: %" PRIu8 "\n"
|
||||
"\n"
|
||||
" --passes : This parameter determines the number of times "
|
||||
" -passes : This parameter determines the number of times "
|
||||
"the file will\n"
|
||||
" be processed. The higher the value, the longer "
|
||||
" be processed. The higher the value, the longer "
|
||||
"the overall\n"
|
||||
" processing time will be.\n"
|
||||
" Value range: [1..%" PRIuMAX "]\n"
|
||||
" Default value: %" PRIuMAX "\n"
|
||||
" processing time will be.\n"
|
||||
" Value range: [1..%" PRIuMAX "]\n"
|
||||
" Default value: %" PRIuMAX "\n"
|
||||
"\n"
|
||||
"[options]:\n"
|
||||
" --noconfirm : (Caution!) Do not ask user to confirm "
|
||||
" -noconfirm : (Caution!) Do not ask user to confirm "
|
||||
"deletion\n"
|
||||
"\n"
|
||||
" -c, --contents : Only corrupt contents of files (PNG, BMP, WAV)\n"
|
||||
" -contents : Only corrupt contents of files (PNG, BMP, WAV)\n"
|
||||
"\n"
|
||||
" -l,\n"
|
||||
" --line-endings : Preserve line endings\n"
|
||||
" -line-endings : Preserve line endings\n"
|
||||
"\n"
|
||||
" -P,\n"
|
||||
" --printable : Work only with printable ASCII characters\n"
|
||||
" -printable : Work only with printable ASCII characters\n"
|
||||
"\n"
|
||||
" --seed : Specify a 32-bit seed for the PRNG. If you "
|
||||
" -seed : Specify a 32-bit seed for the PRNG. If you "
|
||||
"want to keep it\n"
|
||||
" random, set the option to `random`.\n",
|
||||
" random, set the option to `random`.\n",
|
||||
program_name, UINT16_MAX, probability, threshold,
|
||||
(uintmax_t) SIZE_MAX, passes);
|
||||
free(program_name);
|
||||
@ -225,27 +234,26 @@ int main(int argc, char** argv) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ARG_MATCH(arg, ARGS_PROBABILITY))
|
||||
if (ARG_MATCH(arg, ARG_PROBABILITY))
|
||||
arg_destination = ARG_DEST_POSSIBILITY;
|
||||
else if (ARG_MATCH(arg, ARGS_THRESHOLD))
|
||||
else if (ARG_MATCH(arg, ARG_THRESHOLD))
|
||||
arg_destination = ARG_DEST_THRESHOLD;
|
||||
else if (ARG_MATCH(arg, ARGS_PASSES))
|
||||
else if (ARG_MATCH(arg, ARG_PASSES))
|
||||
arg_destination = ARG_DEST_PASSES;
|
||||
else if (ARG_MATCH(arg, ARGS_SEED))
|
||||
else if (ARG_MATCH(arg, ARG_SEED))
|
||||
arg_destination = ARG_DEST_SEED;
|
||||
else if (!strcmp(arg, "--noconfirm")) {
|
||||
else if (ARG_MATCH(arg, ARG_NOCONFIRM)) {
|
||||
LOOP_ACTION_CONFIG(CLEAR_CONFIG, C_CONFIRM);
|
||||
} else if (ARG_MATCH(arg, ARGS_CONTENTS)) {
|
||||
} else if (ARG_MATCH(arg, ARG_CONTENTS)) {
|
||||
LOOP_ACTION_CONFIG(SET_CONFIG, C_CONTENTS);
|
||||
} else if (ARG_MATCH(arg, ARGS_LINE_ENDINGS)) {
|
||||
} else if (ARG_MATCH(arg, ARG_LINE_ENDINGS)) {
|
||||
LOOP_ACTION_CONFIG(SET_CONFIG, C_LINE_ENDINGS);
|
||||
} else if (ARG_MATCH(arg, ARGS_PRINTABLE)) {
|
||||
} else if (ARG_MATCH(arg, ARG_PRINTABLE)) {
|
||||
LOOP_ACTION_CONFIG(SET_CONFIG, C_PRINTABLE);
|
||||
} else
|
||||
FATAL_ERROR_RET("Unknown command line parameter '%s'.\n"
|
||||
"Please invoke the program with argument %s or "
|
||||
"%s to display help.\n",
|
||||
arg, ARGS_HELP[0], ARGS_HELP[1]);
|
||||
"Please invoke the program with argument %s to "
|
||||
"display help.\n", arg, ARG_HELP);
|
||||
|
||||
if (last_arg)
|
||||
FATAL_ERROR_RET("Please provide the value.");
|
||||
|
Loading…
Reference in New Issue
Block a user