190 lines
3.8 KiB
C
190 lines
3.8 KiB
C
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <getopt.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <memory/smalloc.h>
|
|
|
|
#include "struct.h"
|
|
#include "new.h"
|
|
|
|
struct cmdln_flags* new_cmdln_flags(
|
|
int argc, char* const* argv)
|
|
{
|
|
ENTER;
|
|
|
|
bool echo = false;
|
|
|
|
bool print_with_colors = false;
|
|
|
|
bool read_init_file = true;
|
|
|
|
const char* init_path = "~/.13rc";
|
|
|
|
const char* input_string = NULL;
|
|
|
|
#ifndef RELEASE_BUILD
|
|
const char* test_interactive_using_input_file = NULL;
|
|
#endif
|
|
|
|
enum input_kind input_kind = ik_interactive;
|
|
|
|
static const struct option long_options[] = {
|
|
{"echo", no_argument, 0, 1},
|
|
|
|
{"color", no_argument, 0, 2},
|
|
|
|
{"no-init", no_argument, 0, 3},
|
|
|
|
{"custom-init-path", required_argument, 0, 4},
|
|
|
|
#ifndef RELEASE_BUILD
|
|
{"test-interactive-using-input-file", required_argument, 0, 5},
|
|
#endif
|
|
|
|
{"command", required_argument, 0, 'c'},
|
|
|
|
{0, 0, 0, 0}
|
|
};
|
|
|
|
for (int c; (c = getopt_long(argc, argv, ""
|
|
"\1\2\3\4\5:\6"
|
|
"c:"
|
|
"", long_options, NULL)) >= 0; )
|
|
{
|
|
switch (c)
|
|
{
|
|
case 1: // --echo
|
|
{
|
|
echo = true;
|
|
|
|
break;
|
|
}
|
|
|
|
case 2: // --color
|
|
{
|
|
echo = true;
|
|
|
|
print_with_colors = true;
|
|
|
|
break;
|
|
}
|
|
|
|
case 3: // --no-init
|
|
{
|
|
read_init_file = false;
|
|
|
|
break;
|
|
}
|
|
|
|
case 4: // --custom-init-path
|
|
{
|
|
init_path = optarg;
|
|
|
|
break;
|
|
}
|
|
|
|
#ifndef RELEASE_BUILD
|
|
case 5: // --test-interactive-using-input-file
|
|
{
|
|
test_interactive_using_input_file = optarg;
|
|
|
|
break;
|
|
}
|
|
#endif
|
|
|
|
case 'c': // --command
|
|
{
|
|
switch (input_kind)
|
|
{
|
|
case ik_interactive:
|
|
{
|
|
input_kind = ik_string;
|
|
|
|
input_string = optarg;
|
|
|
|
dpvp(input_string);
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
TODO;
|
|
break;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
TODO;
|
|
exit(1);
|
|
break;
|
|
}
|
|
}
|
|
|
|
struct cmdln_flags* flags = smalloc(sizeof(*flags));
|
|
|
|
switch (input_kind)
|
|
{
|
|
case ik_interactive:
|
|
{
|
|
if (argv[optind])
|
|
{
|
|
flags->input_file = argv[optind++];
|
|
|
|
flags->argv = &argv[optind++];
|
|
|
|
input_kind = ik_file;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case ik_string:
|
|
{
|
|
flags->argv = &argv[optind];
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
TODO;
|
|
break;
|
|
}
|
|
|
|
flags->echo = echo;
|
|
|
|
flags->print_with_colors = print_with_colors;
|
|
|
|
flags->read_init_file = read_init_file;
|
|
|
|
flags->init_path = init_path;
|
|
|
|
flags->input_kind = input_kind;
|
|
|
|
flags->input_string = input_string;
|
|
|
|
#ifndef RELEASE_BUILD
|
|
flags->test_interactive_using_input_file = test_interactive_using_input_file;
|
|
#endif
|
|
|
|
EXIT;
|
|
return flags;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|