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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/* actions.c - ck actions ----------------------------------------------*- C -*-
*
* This file is part of ck, the config keeper
*
* -----------------------------------------------------------------------------
*
* Copyright (C) 2018 Anastasis Grammenos
* GPLv3 (see LICENCE for the full notice)
*
* -------------------------------------------------------------------------- */
#include "actions.h"
#include "dblayer.h"
#include "ckutil.h"
int run_INIT(UserOpt * opt, Conf *conf) {
if (db_exists(opt)) {
printf("Current configuration file location: %s\n", opt->confDir);
PRINT_ERR("ck is already initialized.\n");
return 0;
}
if (init_create_config_file(opt)) {
return 0;
}
DB db = init_make_DB(opt);
if (db.error == SQL_NO_ERR) {
init_make_tables(&db);
}
sqlite3_close(db.ptr);
return 1;
}
AddOpt make_add_options(const int argc, char **argv) {
AddOpt addOpt = {
.progName = argv[0],
.confPath = NULL,
.secret = 0,
.prime = 0,
.err = ADD_NO_ERR
};
/* the first two argumens have to exist since we are here */
if (!util_is_file_rw(argv[1])) {
addOpt.err = ADD_ERR_WRONG_CONFIG;
return addOpt;
}
addOpt.confPath = argv[1];
if (argc == 3) {
if (strcmp(argv[2], "-s") == 0) {
addOpt.secret = 1;
} else if (strcmp(argv[2], "-p") == 0) {
addOpt.prime = 1;
} else {
addOpt.err = ADD_ERR_WRONG_FLAGS;
return addOpt;
}
} else if (argc == 4) {
if (strcmp(argv[2], "-s") == 0) {
addOpt.secret = 1;
} else if (strcmp(argv[2], "-p") == 0) {
addOpt.prime = 1;
} else {
addOpt.err = ADD_ERR_WRONG_FLAGS;
return addOpt;
}
if (strcmp(argv[3], "-s") == 0) {
addOpt.secret = 1;
} else if (strcmp(argv[3], "-p") == 0) {
addOpt.prime = 1;
} else {
addOpt.err = ADD_ERR_WRONG_FLAGS;
return addOpt;
}
}
return addOpt;
}
void add_print_opts(AddOpt *opt) {
printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath);
if (opt->prime && opt->secret) {
printf("Options:\tsecret, primary\n");
} else if (opt->prime) {
printf("Options:\tprimary\n");
} else if (opt->secret) {
printf("Options:\tsecret\n");
}
}
int run_ADD(UserOpt * opt, Conf *conf) {
DB db = open_DB(opt);
if (db.ptr == NULL) {
if (db.error == SQL_ERR_NO_TABLES) {
PRINT_ERR("The database file is currupted. Run ck init anew.\n");
}
}
AddOpt addOpt = make_add_options(opt->argc, opt->argv);
switch (addOpt.err) {
case ADD_NO_ERR:
break;
case ADD_ERR_WRONG_CONFIG:
PRINT_ERR("The config file specified doesn't exist.\n");
close_DB(&db);
return 0;
case ADD_ERR_WRONG_FLAGS:
PRINT_ERR("Flags are: -s for secret and -p for primary.\n");
close_DB(&db);
return 0;
}
add_print_opts(&addOpt);
if (add_transaction_begin(&db, addOpt.progName,
addOpt.confPath, addOpt.secret, addOpt.prime) == 0) {
return 0;
}
// do the linking
close_DB(&db);
return 1;
}
int run_DEL(UserOpt * opt, Conf *conf) {
printf("Running %s\n", "del");
return 0;
}
int run_EDIT(UserOpt * opt, Conf *conf) {
printf("Running %s\n", "edit");
return 0;
}
int run_LIST(UserOpt * opt, Conf *conf) {
printf("Running %s\n", "list");
DB db = open_DB(opt);
if (db.ptr == NULL) {
if (db.error == SQL_ERR_NO_TABLES) {
printf("no tables\n");
}
}
for (int i = 0; i < opt->argc; i++) {
printf("[%d]: %s\n", i, opt->argv[i]);
}
close_DB(&db);
return 0;
}
int run_SEARCH(UserOpt * opt, Conf *conf) {
printf("Running %s\n", "search");
return 0;
}
int run_HELP(UserOpt * opt, Conf *conf) {
printf("Running %s\n", "help");
return 0;
}
void print_INIT_result(int ok) {
if (ok) {
printf("Initialized empty ckdb.\n");
}
}
void print_ADD_result(int ok) {
if (ok) {
printf("ckdb updated succesfully.\n");
return;
}
printf("Could not complete add transaction.\n");
}
void print_DEL_result(int ok) {
if (ok) {
printf("succes\n");
return;
}
printf("failure\n");
}
void print_EDIT_result(int ok) {
if (ok) {
printf("succes\n");
return;
}
printf("failure\n");
}
void print_LIST_result(int ok) {
if (ok) {
printf("succes\n");
return;
}
printf("failure\n");
}
void print_SEARCH_result(int ok) {
if (ok) {
printf("succes\n");
return;
}
printf("failure\n");
}
void print_HELP_result(int ok) {
if (ok) {
printf("succes\n");
return;
}
printf("failure\n");
}
|