4-variable-simplifier/cmdln/process.c
2026-04-25 16:00:10 -04:00

254 lines
6.3 KiB
C

#include <debug.h>
#include <memory/smalloc.h>
#include "struct.h"
#include "process.h"
#include "print_usage.h"
static struct option long_options[] = {
{"print-all-and-quit", no_argument, NULL, 't'},
{"command", required_argument, NULL, 'c'},
{"operators", required_argument, NULL, 'o'},
{"print-stats", no_argument, NULL, 'm'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
struct cmdln_flags* process_cmdln_flags(
int argc,
char* const* argv)
{
ENTER;
const char* command = NULL;
bool unset_operators = true;
struct operators selected_operators = {};
bool unset_print_with_color = true;
bool print_with_color;
bool print_all_and_quit = false;
bool print_stats = false;
for (int opt, idx;
(opt = getopt_long(argc, argv, "tc:o:mh", long_options, &idx)) != -1;)
{
switch (opt)
{
case 't':
{
print_all_and_quit = true;
break;
}
case 'c':
{
command = optarg;
break;
}
case 'o':
{
TODO;
break;
}
case 'm':
{
TODO;
break;
}
case 'h':
{
print_usage();
exit(0);
break;
}
default:
{
print_usage();
exit(1);
break;
}
}
}
if (unset_operators)
{
selected_operators.not = true;
selected_operators.or = true;
selected_operators.and = true;
}
if (unset_print_with_color)
{
if (isatty(1))
{
print_with_color = true;
}
else
{
print_with_color = false;
}
}
struct cmdln_flags* flags = smalloc(sizeof(*flags));
flags->command = command;
flags->selected_operators = selected_operators;
flags->print_with_color = print_with_color;
flags->print_stats = print_stats;
flags->print_all_and_quit = print_all_and_quit;
EXIT;
return flags;
}
#if 0
static void parse_args(int argc, char* const* argv)
{
bool unset_operators = true;
print_with_color = isatty(1);
for (int opt; (opt = getopt(argc, argv, "tpyqmvc:eEo:C:B")) != -1; ) switch (opt)
{
case 't':
print_truthtable = true;
break;
case 'p':
print_all_and_quit = true;
break;
case 'y':
assume_yes = true;
break;
case 'q':
quiet = true;
break;
case 'm':
print_stats = true;
break;
case 'v':
verbose = true;
break;
case 'c':
command = optarg;
break;
case 'E':
use_operators.ternary = true;
// fallthrough
case 'e':
{
use_operators.not = true;
use_operators.or = true;
use_operators.and = true;
use_operators.orn = true;
use_operators.nor = true;
use_operators.andn = true;
use_operators.nand = true;
use_operators. xor = true;
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, "!"))
use_operators.not = true;
else if (!strcmp(moving, "or") || !strcmp(moving, "||"))
use_operators.or = true;
else if (!strcmp(moving, "orn") || !strcmp(moving, "|!"))
use_operators.orn = true;
else if (!strcmp(moving, "nor") || !strcmp(moving, "!|"))
use_operators.nor = true;
else if (!strcmp(moving, "and") || !strcmp(moving, "&&"))
use_operators.and = true;
else if (!strcmp(moving, "andn") || !strcmp(moving, "&!"))
use_operators.andn = true;
else if (!strcmp(moving, "nand") || !strcmp(moving, "!&"))
use_operators.nand = true;
else if (!strcmp(moving, "xor") || !strcmp(moving, "!="))
use_operators.xor = true;
else if (!strcmp(moving, "nxor") || !strcmp(moving, "=="))
use_operators.nxor = true;
else if (!strcmp(moving, "lt") || !strcmp(moving, "<"))
use_operators.lt = true;
else if (!strcmp(moving, "lte") || !strcmp(moving, "<="))
use_operators.lte = true;
else if (!strcmp(moving, "gt") || !strcmp(moving, ">"))
use_operators.gt = true;
else if (!strcmp(moving, "gte") || !strcmp(moving, ">="))
use_operators.gte = true;
else if (!strcmp(moving, "ternary") || !strcmp(moving, "?:"))
use_operators.ternary = true;
else
{
assert(!"TODO");
}
}
break;
}
case 'C':
{
if (!strcmp(optarg, "yes") || !strcmp(optarg, "on"))
print_with_color = true;
else if (!strcmp(optarg, "no") || !strcmp(optarg, "off"))
print_with_color = false;
else if (!strcmp(optarg, "auto"))
print_with_color = isatty(1);
else
{
assert(!"TODO");
}
break;
}
case 'B':
{
force_rebuild = true;
break;
}
default:
{
printf("Unknown option '%c'!" "\n", opt);
exit(1);
break;
}
}
if (unset_operators)
{
use_operators.not = true;
use_operators.or = true;
use_operators.and = true;
}
}
#endif