-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
188 lines (162 loc) · 4.76 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* DECLARE_DATA makes his.h define, not just declare variables that
are marked EXTERN */
#define DEFINE_DATA
#include "his.h"
typedef enum {
LIST, /* Selected when no flag like --add, --export is given */
ADD, /* Selected when --add is seen */
IMPORT, /* Selected when --import is seen */
PURGE, /* Selected when --purge is seen */
} Action;
int main(int argc, char **argv) {
int opt;
char *home;
Action action = LIST;
/* Initialize globals */
if ( (home = getenv("HOME")) ) {
db = xstrdup(home);
db = xstrcat(db, "/.his.db");
}
format = DEFAULT_FORMAT;
/* A negative count means: not initialized. It is overruled in
list_cmds() unless it's set by a flag. */
count = -1;
/* Supported flags */
struct option flags[] = {
{ "accept-his", 0, 0, 'A' },
{ "add", 0, 0, 'a' },
{ "count", 1, 0, 'c' },
{ "db", 1, 0, 'd' },
{ "first", 1, 0, 'f' },
{ "format", 1, 0, 'F' },
{ "help", 0, 0, 'h' },
{ "import", 0, 0, 'i' },
{ "last", 1, 0, 'l' },
{ "list-formats", 0, 0, 'L' },
{ "multi-args", 0, 0, 'm' },
{ "purge", 0, 0, 'p' },
{ "report-missing-args", 0, 0, 'R' },
{ "utc", 0, 0, 'u' },
{ "verbose", 0, 0, 'v' },
{ 0, 0, 0, 0 },
};
while ( (opt = getopt_long(argc, argv, "aAc:d:f:F:il:Lmpvuh?",
flags, 0)) > 0 ) {
switch (opt) {
case 'a':
/* Add cmd to history */
action = ADD;
break;
case 'A':
/* Store cmds that start with 'his' */
accept_his++;
break;
case 'c':
/* Set count for --most-recent */
if (!optarg || !*optarg)
error("missing --count value");
if ( (count = atoi(optarg)) < 0 )
error("bad --count value, must be a non-negative number");
break;
case 'd':
/* sqlite3 db name */
if (!optarg || !*optarg)
error("missing --db value");
free(db);
db = optarg;
break;
case 'f':
/* First searching/dumping timestamp */
if (!optarg || !*optarg)
error("missing --first value");
first_timestamp = str2timestamp(optarg);
break;
case 'F':
/* Set the format for adding. */
if (!optarg || !*optarg)
error("missing --format value");
if (sscanf(optarg, "%d", &format) < 1)
error("bad --format value, not a number");
if (format < 1 || format > MAX_FORMAT)
error("bad --format value, must be at least 1 and at most %d",
MAX_FORMAT);
break;
case 'i':
/* Import list from stdin */
action = IMPORT;
multiargs = 0;
break;
case 'l':
/* Last searching/dumping timestamp */
if (!optarg || !*optarg)
error("missing --last value");
last_timestamp = str2timestamp(optarg);
break;
case 'L':
/* List formats */
list_formats();
break;
case 'm':
/* Add should expect multiple args */
multiargs++;
break;
case 'p':
/* Purge stuff */
action = PURGE;
break;
case 'R':
/* Report missing args ON */
report_missing_args++;
break;
case 'u':
/* Import or export timestamps are UTC */
utc_time++;
break;
case 'v':
/* Enable verbose mode */
verbose++;
break;
case 'h':
case '?':
default:
/* Show usage help */
usage();
break;
}
}
/* Uninitialized errors */
if (!db)
error("failed to determine sqlite3 db path from $HOME or flag --db");
msg("sqlite3 db is %s", db);
sqlinit();
/* Act. */
switch (action) {
case LIST:
list_cmds(argc - optind, argv + optind);
break;
case ADD:
if (argc - optind <= 0)
error("no command to add");
add(argc - optind, argv + optind);
break;
break;
case IMPORT:
if (argc - optind != 0)
error("no arguments expected after --import");\
format = 2;
import_cmds();
break;
case PURGE:
purge();
break;
}
/* Run a consistency checker if we have hit <magicnumber> of inserts
since the last checker run. */
db_cleanup();
/* A transaction was started in sqlinit(). We got here because we didn't
error() out so it's safe to commit. We are not running this through
sqlrun() because that might error out. If this COMMIT doesn't work then
we can't do much about it anyway. */
sqlite3_exec(dbconn, "COMMIT TRANSACTION", 0, 0, 0);
return 0;
}