Line data Source code
1 : //
2 : // substitute_repo_script.c
3 : // deployctl
4 : //
5 : // Created by Danny Goossen on 2/7/17.
6 : // Copyright (c) 2017 Danny Goossen. All rights reserved.
7 : //
8 :
9 : #include "deployd.h"
10 :
11 :
12 0 : char * substitute_repo_script(char * in, char * repo_name, char * repo_url, int PGP_repo, int PGP_pack)
13 : {
14 : struct MemoryStruct output;
15 0 : init_dynamicbuf( &output);
16 : regex_t regex;
17 : int reti;
18 0 : size_t prev_end=0;
19 0 : char regex_str[]= "@@([a-z])@@";
20 :
21 0 : int regex_match_cnt=2+1; // ( 2 matches in above expression: from @@..@@ and inbtween the @@, and 1 position to store the -1 for end.)
22 0 : char * subject=in;
23 :
24 : regmatch_t ovector[3];
25 : //regmatch_t * ovector= calloc(sizeof(regoff_t)*2,regex_match_cnt);
26 :
27 : /* Compile regular expression */
28 0 : regcomp(®ex, regex_str, REG_EXTENDED);
29 : /* Execute regular expression in loop */
30 :
31 : for (;;)
32 : {
33 0 : reti = regexec(®ex, subject+prev_end, regex_match_cnt, ovector, 0);
34 0 : if (!reti)
35 : {
36 : //puts("Match");
37 : int i=0;
38 : while (ovector[i].rm_so!=-1 && i<5)
39 : {
40 : // debug("found at %lld to %lld\n", ovector[i].rm_so+prev_end,ovector[i].rm_eo+prev_end);
41 : i++;
42 : }
43 0 : if (ovector[1].rm_eo !=-1 && ovector[0].rm_so !=-1 && (ovector[1].rm_eo-ovector[1].rm_so) == 1)
44 : {
45 0 : Writedynamicbuf_n((void *)(subject+ prev_end), ovector[0].rm_so, &output);
46 :
47 : // since we use here only 1 char as named substring, we'll have that in ovector[2]
48 0 : char stub_code=subject[prev_end+ovector[1].rm_so];
49 0 : prev_end=prev_end+ovector[0].rm_eo;
50 0 : switch (stub_code) {
51 : case 'u':
52 : {
53 0 : Writedynamicbuf(repo_url,&output);
54 0 : break;
55 : }
56 : case 'n':
57 : {
58 0 : Writedynamicbuf(repo_name,&output);
59 0 : break;
60 : }
61 : case 'p':
62 : {
63 : char tmp[20];
64 0 : sprintf(tmp,"%d", PGP_repo);
65 0 : Writedynamicbuf(tmp,&output);
66 : break;
67 : }
68 : case 'q':
69 : {
70 : char tmp[20];
71 0 : sprintf(tmp,"%d", PGP_pack);
72 0 : Writedynamicbuf(tmp,&output);
73 : break;
74 : }
75 : default:
76 0 : error("svg, regex unknown stub code %c @ pos : %d",subject[prev_end+ovector[1].rm_so],prev_end+ovector[1].rm_so);break;
77 : }
78 : }
79 :
80 : }
81 0 : else if (reti == REG_NOMATCH) {
82 : // copy rest from lastmatch end to end of subject
83 0 : Writedynamicbuf_n((void *)(subject+ prev_end), strlen(subject)-prev_end, &output);
84 0 : break;
85 : }
86 : else { prev_end=0;break; }
87 : }
88 :
89 : /* Free memory allocated to the pattern buffer by regcomp() */
90 0 : regfree(®ex);
91 :
92 0 : if (prev_end)
93 : {
94 0 : debug("success parsing script, len: %d \n",strlen(output.memory));
95 0 : return output.memory;
96 : }
97 : else
98 : {
99 0 : if (output.memory) {free(output.memory);}
100 : return NULL;
101 : }
102 :
103 : }
|