-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcclip.c
376 lines (317 loc) · 10.1 KB
/
cclip.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* This file is part of cclip, clipboard manager for wayland
* Copyright (C) 2024 heather7283
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <sqlite3.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <inttypes.h>
#include "common.h"
#include "db.h"
#ifndef CCLIP_VERSION
#define CCLIP_VERSION "uknown_version"
#endif
unsigned int DEBUG_LEVEL = 0;
int argc;
char** argv;
char* prog_name;
char* db_path = NULL;
bool secure_delete = false;
int print_row(void* data, int argc, char** argv, char** column_names) {
UNUSED(data);
UNUSED(column_names);
for (int i = 0; i < argc - 1; i++) {
printf("%s\t", argv[i]);
}
printf("%s\n", argv[argc - 1]);
return 0;
}
int list(char* fields) {
/* IMPORTANT: EDIT THIS IF YOU EVER ADD MORE THAN 3 ALIASES */
static const char* allowed_fields[][4] = {
{ "rowid", "id", NULL },
{ "timestamp", "time", NULL },
{ "mime_type", "mime", "type", NULL },
{ "preview", NULL },
{ "data_size", "size", NULL },
};
static const int allowed_fields_count = sizeof(allowed_fields) / sizeof(allowed_fields[0]);
const char* selected_fields[allowed_fields_count];
int selected_fields_count = 0;
if (fields == NULL) {
selected_fields[0] = "rowid";
selected_fields[1] = "mime_type";
selected_fields[2] = "preview";
selected_fields_count = 3;
} else {
char* token = strtok(fields, ",");
while (token != NULL) {
if (selected_fields_count >= allowed_fields_count) {
critical("extra field: %s, up to %d allowed\n", token, allowed_fields_count);
return 1;
}
bool is_valid_token = false;
for (int i = 0; i < allowed_fields_count; i++) {
for (int j = 0; allowed_fields[i][j] != NULL; j++) {
if (strcmp(token, allowed_fields[i][j]) == 0) {
selected_fields[selected_fields_count] = allowed_fields[i][0];
selected_fields_count += 1;
is_valid_token = true;
goto loop_out;
}
}
} loop_out:
if (!is_valid_token) {
critical("invalid field: %s\n", token);
return 1;
}
token = strtok(NULL, ",");
}
}
if (selected_fields_count < 1) {
critical("no fields selected\n");
return 1;
}
char* errmsg = NULL;
int retcode;
char sql[256] = "SELECT ";
for (int i = 0; i < selected_fields_count; i++) {
strcat(sql, selected_fields[i]);
if (i < selected_fields_count - 1) {
strcat(sql, ",");
}
}
strcat(sql, " FROM history ORDER BY timestamp DESC");
retcode = sqlite3_exec(db, sql, print_row, NULL, &errmsg);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", errmsg);
return 1;
}
return 0;
}
int get(int64_t id) {
sqlite3_stmt* stmt;
int retcode;
const char* sql = "SELECT data FROM history WHERE rowid = ?";
retcode = sqlite3_prepare(db, sql, -1, &stmt, NULL);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
return 1;
}
sqlite3_bind_int(stmt, 1, id);
retcode = sqlite3_step(stmt);
if (retcode == SQLITE_ROW) {
int data_size = sqlite3_column_bytes(stmt, 0);
const void* data = sqlite3_column_blob(stmt, 0);
fwrite(data, 1, data_size, stdout);
} else if (retcode == SQLITE_DONE) {
critical("no entry found with specified id\n");
return 1;
} else {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
return 1;
}
sqlite3_finalize(stmt);
return 0;
}
int delete(int64_t id) {
sqlite3_stmt* stmt;
int retcode;
const char* sql = "DELETE FROM history WHERE rowid = ?";
retcode = sqlite3_prepare(db, sql, -1, &stmt, NULL);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
return 1;
}
sqlite3_bind_int(stmt, 1, id);
retcode = sqlite3_step(stmt);
if (retcode == SQLITE_DONE) {
if (sqlite3_changes(db) == 0) {
warn("table was not modified, maybe entry with specified id does not exists\n");
}
} else {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
return 1;
}
sqlite3_finalize(stmt);
return 0;
}
int wipe(void) {
sqlite3_stmt* stmt;
int retcode;
const char* sql = "DELETE FROM history";
retcode = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
return 1;
}
retcode = sqlite3_step(stmt);
if (retcode != SQLITE_DONE) {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
return 1;
}
sqlite3_finalize(stmt);
return 0;
}
int vacuum(void) {
int retcode;
char* errmsg;
retcode = sqlite3_exec(db, "VACUUM", NULL, NULL, &errmsg);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", errmsg);
return 1;
}
return 0;
}
void print_version_and_exit(void) {
fprintf(stderr, "cclip version %s\n", CCLIP_VERSION);
exit(0);
}
void print_help_and_exit(int exit_status) {
const char* help_string =
"cclip - command line interface for cclip database\n"
"\n"
"usage:\n"
" cclip [-sVh] [-d DB_PATH] ACTION ACTION_ARG\n"
"\n"
"command line options:\n"
" -d DB_PATH specify path to database file\n"
" -s enable secure_delete pragma\n"
" -V display version and exit\n"
" -h print this help message and exit\n"
"\n"
"actions:\n"
" list [FIELDS] print list of all saved entries to stdout\n"
" get ID print entry with specified ID to stdout\n"
" delete ID delete entry with specified ID from database\n"
" wipe delete all entries from database\n"
" vacuum repack database into minimal amount of space\n";
fputs(help_string, stderr);
exit(exit_status);
}
void parse_command_line(void) {
int opt;
while ((opt = getopt(argc, argv, ":d:sVh")) != -1) {
switch (opt) {
case 'd':
db_path = strdup(optarg);
break;
case 's':
secure_delete = true;
break;
case 'V':
print_version_and_exit();
break;
case 'h':
print_help_and_exit(0);
break;
case '?':
critical("unknown option: %c\n", optopt);
print_help_and_exit(1);
break;
case ':':
critical("missing arg for %c\n", optopt);
print_help_and_exit(1);
break;
default:
die("error while parsing command line options\n");
}
}
}
int main(int _argc, char** _argv) {
argc = _argc;
argv = _argv;
prog_name = argc > 0 ? argv[0] : "cclip";
int exit_status = 0;
parse_command_line();
if (db_path == NULL) {
db_path = get_default_db_path();
}
int retcode;
char* errmsg;
if ((retcode = sqlite3_open(db_path, &db)) != SQLITE_OK) {
die("sqlite error: %s\n", sqlite3_errstr(retcode));
}
if (secure_delete) {
retcode = sqlite3_exec(db, "PRAGMA secure_delete = 1", NULL, NULL, &errmsg);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", errmsg);
exit_status = 1;
goto cleanup;
}
}
if (argv[optind] == NULL) {
critical("no action provided\n");
exit_status = 1;
goto cleanup;
}
char* action = argv[optind];
if (strcmp(action, "list") == 0) {
char* output_format = NULL;
if (argv[optind + 1] != NULL) {
output_format = argv[optind + 1];
}
exit_status = list(output_format);
} else if (strcmp(action, "get") == 0) {
int64_t id = -1;
if (argv[optind + 1] == NULL) {
if (scanf("%" SCNd64 "\n", &id) != 1) {
critical("no id provided\n");
exit_status = 1;
goto cleanup;
}
} else {
id = atoll(argv[optind + 1]);
}
if (id <= 0) {
critical("id should be a positive integer, got %s\n", argv[optind + 1]);
exit_status = 1;
goto cleanup;
}
exit_status = get(id);
} else if (strcmp(action, "delete") == 0) {
int64_t id = -1;
if (argv[optind + 1] == NULL) {
if (scanf("%" SCNd64 "\n", &id) != 1) {
critical("no id provided\n");
exit_status = 1;
goto cleanup;
}
} else {
id = atoll(argv[optind + 1]);
}
if (id <= 0) {
critical("id should be a positive integer, got %s\n", argv[optind + 1]);
exit_status = 1;
goto cleanup;
}
exit_status = delete(id);
} else if (strcmp(action, "wipe") == 0) {
exit_status = wipe();
} else if (strcmp(action, "vacuum") == 0) {
exit_status = vacuum();
} else {
critical("invalid action: %s\n", action);
exit_status = 1;
goto cleanup;
}
cleanup:
sqlite3_close_v2(db);
exit(exit_status);
}