Newer
Older
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
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*/
#include <string.h>
#include <oblibs/log.h>
#include <oblibs/sastr.h>
#include <oblibs/files.h>
#include <skalibs/stralloc.h>
#include <66/resolve.h>
#include <66/utils.h>
#include <66/constants.h>
#include <66/module.h>
void regex_replace(stralloc *list, resolve_service_t *res)
{
log_flow() ;
int r ;
size_t pos = 0, idx = 0 ;
stralloc frontend = STRALLOC_ZERO ;
stralloc sa = STRALLOC_ZERO ;
if (!res->regex.ninfiles)
return ;
if (!sastr_clean_string(&sa, res->sa.s + res->regex.infiles))
log_dieu(LOG_EXIT_SYS, "clean string") ;
FOREACH_SASTR(list, pos) {
frontend.len = idx = 0 ;
char *str = list->s + pos ;
size_t len = strlen(str) ;
char bname[len + 1] ;
char dname[len + 1] ;
if (!ob_basename(bname, str))
log_dieu(LOG_EXIT_SYS, "get basename of: ", str) ;
if (!ob_dirname(dname, str))
log_dieu(LOG_EXIT_SYS, "get dirname of: ", str) ;
//log_trace("read service file of: ", dname, bname) ;
r = read_svfile(&frontend, bname, dname) ;
if (!r)
log_dieusys(LOG_EXIT_SYS, "read file: ", str) ;
else if (r == -1)
continue ;
{
FOREACH_SASTR(&sa, idx) {
int all = 0, fpos = 0 ;
char const *line = sa.s + idx ;
size_t linelen = strlen(line) ;
char filename[SS_MAX_SERVICE_NAME + 1] ;
char replace[linelen + 1] ;
char regex[linelen + 1] ;
if (linelen >= SS_MAX_PATH_LEN)
log_die(LOG_EXIT_SYS, "limit exceeded in service: ", res->sa.s + res->name) ;
if ((line[0] != ':') || (get_sep_before(line + 1, ':', '=') < 0))
log_die(LOG_EXIT_SYS, "bad format in line: ", line, " of key @infiles field") ;
memset(filename, 0, (SS_MAX_SERVICE_NAME + 1) * sizeof(char)); ;
memset(replace, 0, (linelen + 1) * sizeof(char)) ;
memset(regex, 0, (linelen + 1) * sizeof(char)) ;
fpos = regex_get_file_name(filename, line) ;
if (fpos < 3) all = 1 ;
regex_get_replace(replace, line + fpos) ;
regex_get_regex(regex, line + fpos) ;
if (!strcmp(bname, filename) || all) {
if (!sastr_replace_all(&frontend, replace, regex))
log_dieu(LOG_EXIT_SYS, "replace: ", replace, " by: ", regex, " in file: ", str) ;
if (!stralloc_0(&frontend))
log_dieusys(LOG_EXIT_SYS, "stralloc") ;
frontend.len-- ;
if (!file_write_unsafe(dname, bname, frontend.s, frontend.len))
log_dieusys(LOG_EXIT_SYS, "write: ", dname, "/", bname) ;
}
}
}
}
stralloc_free(&frontend) ;
stralloc_free(&sa) ;
}