-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.c
82 lines (70 loc) · 1.62 KB
/
cmdline.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "common.h"
#include "cmdline.h"
#include "config.h"
static char optstring[] = "f:";
void gather_config (int argc, char* argv[], Config* conf)
{
/* Gathers configuration info from:
1) Command line arguments.
2) Configuration file (location specified using cmdline args)
3) environment variables (anticipated)
*/
/* Make a default configuration */
defaultConfig (conf);
parse_args (argc, argv, conf);
}
void parse_args (int argc, char* argv[], Config* conf)
{
char opt;
char* config_path = NULL;
int config_fd;
while ((opt = getopt (argc, argv, optstring)) != -1)
{
switch (opt)
{
case 'f':
config_path = optarg;
break;
case '?':
//printf (MESG_FAIL "Unknown option.\n");
exit (ERROR_CMDLINE_UNKNOWN_OPTION);
break;
}
}
if (config_path != NULL)
{
/* Try opening config file */
if ((config_fd = open (config_path, O_RDONLY)) == -1)
{
printf (MESG_FAIL "Unable to open \"%s\"\n", config_path);
exit (ERROR_CONFIG_OPEN);
}
printf (MESG_INFO "Config path: %s\n", config_path);
/* Try parsing it */
if (!read_from_file (config_fd, conf, config_path))
{
printf (MESG_WARN "Invalid configuration syntax, using default.\n");
defaultConfig (conf);
}
close (config_fd);
}
else
{
/* No config path provided, using default */
printf (MESG_WARN "No configuration path given, using default settings.\n");
defaultConfig (conf);
}
}
void parse_env (Config* conf)
{
/* Do nothing for now */
;
}