-
Notifications
You must be signed in to change notification settings - Fork 0
/
kraus.c
129 lines (105 loc) · 3.13 KB
/
kraus.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* ----------------------------------------------------------------------------
* "THE COFFEE-WARE LICENSE" (Revision 23):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can bring me a coffee in return.
* ----------------------------------------------------------------------------
*/
#include "kraus.h"
int main(int argc, char *argv[]) {
calendar_t cal = {0};
module_t * out_module;
module_t * strat_module;
char *output_s = NULL;
char *strategy_s = NULL;
int c;
while ((c = getopt(argc, argv, "hwc:o:s:")) != -1)
switch (c) {
case 'w': // Skip Weekends
cal.flags.weekday = 1;
break;
case 'c': // Count
if (optarg && atoi(optarg) > 0) {
cal.count = optarg ? atoi(optarg) : 1;
} else {
cal.count = 1;
}
break;
case 'o': // Output Module
output_s = optarg;
break;
case 's': // Strategy Module
strategy_s = optarg;
break;
case 'h':
usage();
return 0;
break;
case '?':
printf("craus %i.%i.%i\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
if (optopt == 'c' || optopt == 'o' || optopt == 's') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
usage();
} else if (isprint(optopt)) {
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
} else {
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
}
return 1;
default:
abort();
}
module_registry_t strategy_modules = {
.name = "Strategy",
};
module_registry_t output_modules = {
.name = "Output",
};
register_strategy(&strategy_modules);
register_output(&output_modules);
// Handle output Modules
if (output_s != NULL) {
out_module = search_module(&output_modules, (module_ident_t *) output_s);
if (out_module == NULL) {
printf("Output-module not found\n\n");
usage_output(&output_modules);
return -1;
}
} else {
out_module = get_default_module(&output_modules);
}
// Handle strategy Modules
if (strategy_s != NULL) {
strat_module = search_module(&strategy_modules, (module_ident_t *) strategy_s);
if (strat_module == NULL) {
printf("Strategy-module not found\n\n");
usage_output(&strategy_modules);
return -1;
}
} else {
strat_module = get_default_module(&strategy_modules);
}
time(&cal.start_date);
cal.count = cal.count ? cal.count : 1;
output = out_module->func;
strategy = strat_module->func;
output(&cal, strategy);
return 0;
}
void usage(void) {
module_registry_t strategy_registry = {
.name = "Strategy",
};
module_registry_t output_registry = {
.name = "Output",
};
register_output(&output_registry);
register_strategy(&strategy_registry);
printf("craus Version %i.%i.%i\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
printf("kraus [-o OUTPUT MODULE] [-s STRATEGY MODULE] [-w] [-c COUNT]\n\n");
printf("\n");
usage_modules(&output_registry);
printf("\n");
usage_modules(&strategy_registry);
}