-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjobcfg.c
100 lines (87 loc) · 2.53 KB
/
jobcfg.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
/*
* Copyright (c) 2018 Mark Heily <mark@heily.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <libgen.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "job.h"
#include "logger.h"
#include "parser.h"
#include "database.h"
static char *progname;
static void
usage(void)
{
fprintf(stderr, "usage: %s [-v] [-f path] import|init\n", progname);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
int c;
char *command = NULL;
char *f_flag = NULL;
if (logger_init() < 0)
errx(1, "unable to initialize logging");
logger_add_stderr_appender();
if (db_init() < 0)
errx(1, "unable to initialize database functions");
progname = basename(argv[0]);
while ((c = getopt(argc, argv, "f:hv")) != -1) {
switch (c) {
case 'f':
f_flag = strdup(optarg);
if (!f_flag)
err(1, "strdup");
break;
case 'h':
usage();
break;
case 'v':
logger_set_verbose(1);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
command = argv[0];
if (!strcmp(command, "init")) {
if (db_create(NULL, NULL) < 0)
errx(1, "unable to create the database");
} else {
if (db_open(NULL, 0) < 0)
errx(1, "unable to open the database");
if (!strcmp(command, "import")) {
if (parser_import(f_flag ? f_flag : "/dev/stdin") < 0)
goto err_out;
}
}
if (db_close(dbh) < 0)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
err_out:
(void) db_close(dbh);
exit(EXIT_FAILURE);
}