-
Notifications
You must be signed in to change notification settings - Fork 0
/
rer.c
405 lines (378 loc) · 10.4 KB
/
rer.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright © 2014,2015 Serpent7776. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Project: rer
* File: rer.c
* By: Serpent7776
*/
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "platform.h"
#include "rer_int.h"
#include "replace_str.h"
#include "debug.h"
static void rer_rename_dry_run(const char*, const char*);
static int rer_replace_part(RER);
//global variables
static Rer_defaults rer_defaults = {
16 //ovec_size
};
static Rer_mod_map pcre_mod_map[] = {
{'i', PCRE_CASELESS},
{'x', PCRE_EXTENDED},
{'U', PCRE_UNGREEDY},
{0, 0}
};
static Rer_mod_map rer_mod_map[]= {
{'g', RER_F_GLOBAL},
{0, 0}
};
int rer_files_callback(const char* path, void* param) {
Rer* rer = (Rer*)param;
char* newfilename = rer_processname(rer, path);
if (rer->status == RER_STATUS_OK) {
rer_rename(rer, path, newfilename);
}
if (rer->callback) {
rer->callback(path, newfilename, rer->status, rer->callback_userparam);
}
return 0;
}
RER rer_create(const char* pattern, const char* replacement, const char* modifiers) {
if (pattern == NULL || replacement == NULL || modifiers == NULL) {
return NULL;
}
DEBUG_WRITE("rer: creating new rer object\n");
Rer* rer = malloc(sizeof(Rer));
if (rer) {
int errorpos;
const char* errormsg;
Rer_modifiers rmods;
//TODO: cleanup on error
rer->pattern = strdup(pattern);
rer->replacement = strdup(replacement);
rer->modifiers = strdup(modifiers);
rer_translate_modifiers(modifiers, &rmods);
rer->flags = rmods.rer_mods;
rer->re_flags = rmods.pcre_mods;
rer->files = fdlist_create();
rer->re_pattern = pcre_compile(pattern, rer->re_flags, &errormsg, &errorpos, NULL);
rer->callback = NULL;
rer->callback_userparam = NULL;
rer->counter = 0;
//TODO: get vecsize from pcre_info
rer->re_ovecsize = (rer_defaults.ovec_size+1)*3;
rer->re_ovec = malloc(sizeof(int*)*rer->re_ovecsize);
rer->newfilename = NULL;
DEBUG_WRITE("rer: created object %p\n", rer);
return rer;
}
return NULL;
}
void rer_destroy(RER _rer) {
if (_rer) {
Rer* rer = (Rer*)_rer;
DEBUG_WRITE("rer: destroying rer object %p\n", rer);
free(rer->re_ovec);
free(rer->pattern);
pcre_free(rer->re_pattern);
free(rer->replacement);
free(rer->modifiers);
if (rer->newfilename) {
free(rer->newfilename);
}
fdlist_destroy(rer->files);
free(rer);
DEBUG_WRITE("rer: destroying done\n");
}
}
Rer_error rer_addfile(RER _rer, const char* path) {
if (_rer && path) {
Rer* rer = (Rer*)_rer;
DEBUG_WRITE("rer: adding path '%s' to rer %p\n", path, rer);
fdlist_node* node = fdlist_addfile(rer->files, path, rer->files->lastnode);
if (node) {
return RER_ERR_OK;
}
return RER_ERR_NULL;
}
return RER_ERR_NULLPTR;
}
void rer_setcallback(RER _rer, rer_callback callback, void* param) {
if (_rer) {
Rer* rer = (Rer*)_rer;
rer->callback = callback;
rer->callback_userparam = param;
}
}
int rer_getflags(RER _rer) {
int flags = -1;
if (_rer) {
Rer* rer = (Rer*)_rer;
flags = rer->flags;
}
return flags;
}
int rer_setflags(RER _rer, int flags) {
int ret = -1;
if (_rer) {
Rer* rer = (Rer*)_rer;
rer->flags = flags;
ret = rer->flags;
}
return ret;
}
int rer_setflag(RER _rer, Rer_flag flag) {
int flags = -1;
if (_rer) {
Rer* rer = (Rer*)_rer;
rer->flags |= flag;
flags = rer->flags;
}
return flags;
}
int rer_clrflag(RER _rer, Rer_flag flag) {
int flags = -1;
if (_rer) {
Rer* rer = (Rer*)_rer;
rer->flags &= ~flag;
flags = rer->flags;
}
return flags;
}
int rer_chkflag(RER _rer, Rer_flag flag) {
int ret = 0;
if (_rer) {
Rer* rer = (Rer*)_rer;
ret = rer->flags & flag;
}
return ret;
}
int rer_setdefopti(Rer_option option, int value) {
int ret = 0;
switch (option) {
case RER_OPT_OVEC_SIZE:
ret = rer_defaults.ovec_size;
rer_defaults.ovec_size = value;
break;
}
return ret;
}
int rer_getdefopti(Rer_option option) {
int ret = 0;
switch (option) {
case RER_OPT_OVEC_SIZE:
ret = rer_defaults.ovec_size;
break;
}
return ret;
}
Rer_error rer_exec(RER _rer) {
if (_rer) {
Rer* rer = (Rer*)_rer;
DEBUG_WRITE("rer: executing file list for rer %p\n", rer);
fdlist_walk(rer->files, rer_files_callback, _rer);
}
return RER_ERR_OK;
}
char* rer_processname(RER _rer, const char* path) {
if (_rer && path) {
Rer* rer = (Rer*)_rer;
DEBUG_WRITE("rer: processing path '%s' for rer %p\n", path, rer);
if (rer->newfilename) {
free(rer->newfilename);
rer->newfilename = NULL;
}
char* file_name = NULL;
char* file_dir = NULL;
int result_ok = 0;
PLAT_BASE_DIR_NAME(path, file_name, file_dir, result_ok);
DEBUG_WRITE("basename: %s, dirname: %s\n", file_name, file_dir);
if (result_ok) {
const size_t file_dir_length = strlen(file_dir);
rer->offset = 0;
rer->newfilename = strdup(file_name);
size_t repl_step = 0;
const int is_global = rer->flags & RER_F_GLOBAL;
if (is_global) {
for (; rer_replace_part(rer) > 0; repl_step++) {
// empty
}
} else {
if (rer_replace_part(rer) > 0) {
repl_step = 1;
}
}
if (rer->newfilename && repl_step>0) {
const size_t newfilename_length = strlen(rer->newfilename);
const size_t newpath_length = file_dir_length+newfilename_length+2;
char* buf = malloc(newpath_length);
snprintf(buf, newpath_length, "%s/%s", file_dir, rer->newfilename);
free(rer->newfilename);
rer->newfilename = buf;
rer->status = RER_STATUS_OK;
} else {
rer->status = repl_step>0 ? RER_STATUS_ERROR : RER_STATUS_NOMATCH;
}
return rer->newfilename;
}
}
return NULL;
}
Rer_status rer_rename(RER _rer, const char* from_name, const char* to_name) {
//if RER_F_DRY_RUN is set just forward call to rer_rename_dry_run
if (rer_chkflag(_rer, RER_F_DRY_RUN)) {
rer_rename_dry_run(from_name, to_name);
return RER_STATUS_IGNORED;
}
const int file_exists = access(to_name, F_OK);
if (file_exists == -1 && errno == ENOENT) {
//file to_name does not exists
DEBUG_WRITE("rer: renaming file from '%s' to '%s'\n", from_name, to_name);
const int status = rename(from_name, to_name);
if (_rer) {
Rer* rer = (Rer*)_rer;
rer->status = status == 0 ? RER_STATUS_OK : RER_STATUS_RENAME_FAILED;
}
return status;
}
DEBUG_WRITE("rer: could not rename file '%s' to '%s': file exists\n", from_name, to_name);
return RER_STATUS_FILE_EXISTS;
}
Rer_error rer_reset(RER _rer) {
if (_rer) {
Rer* rer = (Rer*)_rer;
DEBUG_WRITE("rer: resetting rer %p\n", rer);
fdlist_clear(rer->files);
free(rer->newfilename);
rer->newfilename = NULL;
rer->offset = 0;
rer->counter = 0;
rer->status = RER_STATUS_OK;
return RER_ERR_OK;
}
return RER_ERR_NULLPTR;
}
/*
* internal functions:
*/
static void rer_rename_dry_run(const char* from_name, const char* to_name) {
fprintf(stdout, "rename %s => %s", from_name, to_name);
const int file_exists = access(to_name, F_OK);
if (file_exists == 0) {
fprintf(stdout, "; Warning: output file already exists");
}
fprintf(stdout, "\n");
}
static char* rer_replace_backref(Rer* rer, int N, int i, char* repl) {
if (rer->re_ovec[2 * i] != -1 && rer->re_ovec[2 * i + 1] != -1) {
const char* s;
const size_t ref_maxsize = 8;
char ref[ref_maxsize];
snprintf(ref, ref_maxsize, "\\%i", i);
pcre_get_substring(rer->newfilename, rer->re_ovec, N, i, &s);
char* newstring = replace_str(repl, ref, s);
pcre_free_substring(s);
return newstring;
}
return NULL;
}
static void rer_replace_match(Rer* rer, char* repl) {
const int match_length = rer->re_ovec[1] - rer->re_ovec[0];
const int repl_length = strlen(repl);
if (rer->re_ovec[0] != -1 && rer->re_ovec[1] != -1) {
char* buf = replace_stri(rer->newfilename, rer->re_ovec[0], match_length, repl);
free(repl);
if (buf) {
if (rer->newfilename) {
free(rer->newfilename);
}
rer->newfilename = buf;
}
rer->offset = rer->re_ovec[1] + (repl_length - match_length);
}
}
//TODO: this function is too complicated
static int rer_replace_part(RER _rer) {
if (_rer) {
Rer* rer = (Rer*)_rer;
const size_t newfilename_length = strlen(rer->newfilename);
const int N = pcre_exec(rer->re_pattern, NULL, rer->newfilename, newfilename_length, rer->offset, 0, rer->re_ovec, rer->re_ovecsize);
if (N>0) {
char* repl = strdup(rer->replacement);
if (repl) {
for (int i = 1; i < N; i++) {
char* newstring = rer_replace_backref(rer, N, i, repl);
free(repl);
if (newstring) {
repl = newstring;
} else {
return 0;
}
}
} else {
return 0;
}
if (repl) {
rer_replace_match(rer, repl);
} else {
return 0;
}
}
return N;
}
return 0;
}
int rer_translate_modifiers(const char* mods, Rer_modifiers* rmods) {
if (mods && rmods) {
const int mods_length = strlen(mods);
rmods->pcre_mods = 0;
rmods->rer_mods = 0;
for (int i = 0; i<mods_length; i++) {
const char ch = mods[i];
int found = 0;
for (int j = 0; pcre_mod_map[j].mod_flag; j++) {
if (ch == pcre_mod_map[j].modifier) {
rmods->pcre_mods |= pcre_mod_map[j].mod_flag;
found = 1;
break;
}
}
if (!found) {
for (int j = 0; rer_mod_map[j].mod_flag; j++) {
if (ch == rer_mod_map[j].modifier) {
rmods->rer_mods |= rer_mod_map[j].mod_flag;
break;
}
}
}
}
return 0;
}
return 1;
}