4-variable-simplifier/cmdln.c

214 lines
6 KiB
C

#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <assert.h>
#include "cmdln.h"
#include "debug.h"
struct cmdln_flags parse_args(int argc, char* const* argv)
{
struct cmdln_flags flags = {};
flags.argv0 = argv[0];
bool unset_operators = true;
bool unset_print_with_color = true;
for (int opt; (opt = getopt(argc, argv, "pyqmvc:eEo:C:B")) != -1; )
{
switch (opt)
{
case 'p':
{
flags.print_all_and_quit = true;
break;
}
case 'y':
{
flags.assume_yes = true;
break;
}
case 'q':
{
flags.quiet = true;
break;
}
case 'm':
{
flags.print_max_operators_needed_and_quit = true;
break;
}
case 'v':
{
flags.verbose = true;
break;
}
case 'c':
{
flags.command = optarg;
break;
}
case 'E':
{
flags.use_operators.ternary = true;
}
// fallthrough
case 'e':
{
flags.use_operators.not = true;
flags.use_operators.or = true;
flags.use_operators.and = true;
flags.use_operators.orn = true;
flags.use_operators.nor = true;
flags.use_operators.andn = true;
flags.use_operators.nand = true;
flags.use_operators. xor = true;
flags.use_operators.nxor = true;
unset_operators = false;
break;
}
case 'o':
{
unset_operators = false;
for (char* moving; (moving = strtok_r(NULL, ",", &optarg)); )
{
if (!strcmp(moving, "not") || !strcmp(moving, "!"))
{
flags.use_operators.not = true;
}
else if (!strcmp(moving, "or") || !strcmp(moving, "||"))
{
flags.use_operators.or = true;
}
else if (!strcmp(moving, "orn") || !strcmp(moving, "|!"))
{
flags.use_operators.orn = true;
}
else if (!strcmp(moving, "nor") || !strcmp(moving, "!|"))
{
flags.use_operators.nor = true;
}
else if (!strcmp(moving, "and") || !strcmp(moving, "&&"))
{
flags.use_operators.and = true;
}
else if (!strcmp(moving, "andn") || !strcmp(moving, "&!"))
{
flags.use_operators.andn = true;
}
else if (!strcmp(moving, "nand") || !strcmp(moving, "!&"))
{
flags.use_operators.nand = true;
}
else if (!strcmp(moving, "xor") || !strcmp(moving, "!="))
{
flags.use_operators.xor = true;
}
else if (!strcmp(moving, "nxor") || !strcmp(moving, "=="))
{
flags.use_operators.nxor = true;
}
else if (!strcmp(moving, "lt") || !strcmp(moving, "<"))
{
flags.use_operators.lt = true;
}
else if (!strcmp(moving, "lte") || !strcmp(moving, "<="))
{
flags.use_operators.lte = true;
}
else if (!strcmp(moving, "gt") || !strcmp(moving, ">"))
{
flags.use_operators.gt = true;
}
else if (!strcmp(moving, "gte") || !strcmp(moving, ">="))
{
flags.use_operators.gte = true;
}
else if (!strcmp(moving, "ternary") || !strcmp(moving, "?:"))
{
flags.use_operators.ternary = true;
}
else if (!strcmp(moving, "assign") || !strcmp(moving, "="))
{
flags.use_operators.assignment = true;
}
else
{
assert(!"TODO");
}
}
break;
}
case 'C':
{
unset_print_with_color = false;
if (!strcmp(optarg, "yes") || !strcmp(optarg, "on"))
{
flags.print_with_color = true;
}
else if (!strcmp(optarg, "no") || !strcmp(optarg, "off"))
{
flags.print_with_color = false;
}
else if (!strcmp(optarg, "auto"))
{
flags.print_with_color = isatty(1);
}
else
{
assert(!"TODO");
}
break;
}
case 'B':
{
flags.force_rebuild = true;
break;
}
default:
{
assert(!"TODO");
break;
}
}
}
if (unset_operators)
{
flags.use_operators.not = true;
flags.use_operators.or = true;
flags.use_operators.and = true;
}
if (unset_print_with_color)
{
flags.print_with_color = isatty(1);
}
return flags;
}