-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
273 lines (231 loc) · 9.05 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
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
/*****************************\
* Manage pastes to pb *
* Author: Sam Stuewe (C) 2015 *
* License: GPLv3 *
\*****************************/
#include "main.h"
#include "pb.h"
#include "pbpst_db.h"
json_t * mem_db, * pastes = 0, * prov_pastes = 0;
static json_t * def_prov = 0;
struct pbpst_state state;
const char * def_provider = 0;
bool point_of_no_return = false;
signed
main (signed argc, char * argv []) {
if ( argc <= 1 ) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
fprintf(stderr, "%s%s%s", cmds_help, gen_help, more_info);
#pragma clang diagnostic pop
return EXIT_FAILURE;
}
signal(SIGINT, signal_handler);
signed exit_status = EXIT_SUCCESS;
const char * vos = opts_for[NON];
for ( signed oi = 0, c = getopt_long(argc, argv, vos, os, &oi);
c != -1; c = getopt_long(argc, argv, vos, os, &oi) ) {
size_t l = optarg ? strlen(optarg) + 1 : 0;
char ** state_var = 0;
switch ( c ) {
case 'S': case 'R': case 'U': case 'D':
if ( !state.cmd ) {
vos = opts_for[(state.cmd = (enum pb_cmd )c)];
optind = 1;
} else if ( state.cmd != c ) {
fputs("pbpst: You can only run one operation at a time\n",
stderr);
exit_status = EXIT_FAILURE; goto cleanup;
} break;
case 's':
if ( !state.cmd ) {
vos = opts_for[(state.cmd = (enum pb_cmd )c)];
optind = 1;
} else {
state_var = &state.url; goto svcase;
} break;
case 'f': state_var = &state.path; goto svcase;
case 'l': state_var = &state.lexer; goto svcase;
case 'T': state_var = &state.theme; goto svcase;
case 'F': state_var = &state.format; goto svcase;
case 'e': state_var = &state.ext; goto svcase;
case 'v': state_var = &state.vanity; goto svcase;
case 'u': state_var = &state.uuid; goto svcase;
case 'm': state_var = &state.msg; goto svcase;
case 'q': state_var = &state.query; goto svcase;
case 'd': state_var = &state.del; goto svcase;
case 'P': state_var = &state.provider; goto svcase;
case 'b': state_var = &state.dbfile; goto svcase;
case 'L': state_var = &state.ln; goto svcase;
case 'x': state_var = &state.secs; goto svcase;
svcase:
*state_var = (char * )malloc(l);
if ( !*state_var ) {
fputs("pbpst: Could not store arguments: Out of Memory\n",
stderr); goto cleanup;
} snprintf(*state_var, l, "%s", optarg); break;
case '#': state.prog = true; break;
case 'r': state.rend = true; break;
case 't': state.term = true; break;
case 'i': state.init = true; break;
case 'y': state.prun = true; break;
case 'p': state.priv = true; break;
case 'h': state.help = true; break;
case 'H': state.lspv = true; break;
case 'V': state.verb += 1; break;
case 256: printf(version_str); goto cleanup;
case 257: state.llex = true; break;
case 258: state.lthm = true; break;
case 259: state.lfrm = true; break;
default: exit_status = EXIT_FAILURE; goto cleanup;
}
}
if ( state.help ) {
switch ( state.cmd ) {
case SNC: printf("%s%s", sync_help, gen_help); break;
case RMV: printf("%s%s", rem_help, gen_help); break;
case UPD: printf("%s%s", upd_help, gen_help); break;
case DBS: printf("%s%s", dbs_help, gen_help); break;
case SHR:
case NON: printf("%s%s%s", cmds_help, gen_help, more_info); break;
} goto cleanup;
}
if ( !(state.llex || state.lthm || state.lfrm) &&
!pbpst_test_options(&state) ) {
exit_status = EXIT_FAILURE; goto cleanup;
}
if ( state.path ) {
struct stat st;
errno = 0;
if ( stat(state.path, &st) ) {
exit_status = errno;
perror("pbpst: Error checking file");
goto cleanup;
}
if ( !S_ISREG(st.st_mode) ) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
fprintf(stderr, "pbpst: %s is not a regular file\n", state.path);
#pragma clang diagnostic pop
exit_status = EXIT_FAILURE; goto cleanup;
}
if ( st.st_size > PB_FILE_MAX ) {
fputs("pbpst: File too large\n", stderr);
exit_status = EXIT_FAILURE; goto cleanup;
}
}
if ( !(db_loc = db_locate(&state)) || !(swp_db_loc = db_swp_init(db_loc)) ||
!(mem_db = db_read(db_loc)) ) {
exit_status = EXIT_FAILURE; goto cleanup;
}
if ( (def_prov = json_object_get(mem_db, "default_provider")) ) {
json_incref(def_prov);
def_provider = json_string_value(def_prov);
} else if ( !state.provider ) {
size_t len = strlen(FALLBACK_PROVIDER) + 1;
state.provider = malloc(len);
if ( !state.provider ) {
exit_status = CURLE_OUT_OF_MEMORY;
goto cleanup;
} snprintf(state.provider, len, FALLBACK_PROVIDER);
} else {
size_t len = strlen(state.provider);
if ( state.provider[len - 1] != '/' ) {
state.provider = realloc(state.provider, len + 2);
char * suffix = state.provider + len;
*suffix = '/'; *(suffix + 1) = '\0';
}
}
exit_status = pbpst_dispatch(&state);
if ( exit_status == CURLE_HTTP_RETURNED_ERROR ) { goto cleanup; }
if ( db_swp_flush(mem_db, swp_db_loc) == -1 ) {
exit_status = EXIT_FAILURE; goto cleanup;
}
if ( exit_status != EXIT_SUCCESS ) { goto cleanup; }
if ( db_swp_cleanup(db_loc, swp_db_loc) == -1 ) {
exit_status = EXIT_FAILURE; goto cleanup;
}
cleanup:
pbpst_cleanup();
return exit_status;
}
bool
pbpst_test_options (const struct pbpst_state * s) {
char cl = 0;
switch ( s->cmd ) {
case SNC: case SHR: break;
case RMV: cl = !s->uuid && !s->prun ? 'R' : cl; break;
case UPD: cl = !s->uuid ? 'U' : cl; break;
case DBS: cl = !s->init && !s->lspv && !s->query && !s->del && !s->prun
? 'D' : cl; break;
case NON: cl = 'N'; break;
}
if ( cl == 'N' ) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
fprintf(stderr, "%s%s%s", cmds_help, gen_help, more_info);
#pragma clang diagnostic pop
} else if ( cl != 0 ) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
fprintf(stderr, "pbpst: erroneous option(s). See `pbpst -%ch`\n", cl);
#pragma clang diagnostic pop
} return cl == 0;
}
signed
pbpst_dispatch (const struct pbpst_state * s) {
if ( s->llex || s->lthm || s->lfrm ) { return pb_list(s); }
const char * provider = s->provider ? s->provider : def_provider,
* uuid = s->uuid ? s->uuid : s->del;
switch ( s->cmd ) {
case SNC:
case UPD: return pb_paste(s);
case SHR: return pb_shorten(provider, s->url, s->verb);
case RMV: return (s->prun ? pb_prune(s)
: pb_remove(provider, uuid, s->verb));
case DBS: return pbpst_db(s);
case NON: return EXIT_FAILURE; // should never get here
}
}
noreturn void
signal_handler (signed signum) {
#define SIGMSG "\rpbpst: Caught \x1b[?25h"
const char * const siglist [] = {
[SIGINT] = SIGMSG "Interrupt\n"
};
fputs(siglist[signum], stderr);
pbpst_cleanup();
exit(EXIT_FAILURE);
}
void
pbpst_cleanup (void) {
free(state.url);
free(state.path);
free(state.msg);
free(state.lexer);
free(state.theme);
free(state.format);
free(state.ext);
free(state.vanity);
free(state.uuid);
free(state.query);
free(state.del);
free(state.provider);
free(state.ln);
free(state.secs);
json_decref(mem_db);
json_decref(pastes);
json_decref(prov_pastes);
json_decref(def_prov);
if ( swp_db_loc ) {
struct stat st;
if ( stat(swp_db_loc, &st) == 0 && st.st_size == 0 ) {
fputs( !point_of_no_return && !remove(swp_db_loc)
? "pbpst: removed empty swap (contingency)\n"
: "pbpst: You need to manually check your swap db\n", stderr);
} free(swp_db_loc);
}
if ( db_loc != state.dbfile ) { free(db_loc); }
free(state.dbfile);
}
// vim: set ts=4 sw=4 et: