Permit file objects in python header constructor
[platform/upstream/rpm.git] / build / parseScript.c
1 /** \ingroup rpmbuild
2  * \file build/parseScript.c
3  *  Parse install-time script section from spec file.
4  */
5
6 #include "system.h"
7
8 #include <rpm/header.h>
9 #include <rpm/rpmbuild.h>
10 #include <rpm/rpmlog.h>
11
12 #include "rpmio/rpmlua.h"
13
14 #include "debug.h"
15
16
17 /**
18  */
19 static int addTriggerIndex(Package pkg, const char *file,
20         const char *script, const char *prog)
21 {
22     struct TriggerFileEntry *tfe;
23     struct TriggerFileEntry *list = pkg->triggerFiles;
24     struct TriggerFileEntry *last = NULL;
25     int index = 0;
26
27     while (list) {
28         last = list;
29         list = list->next;
30     }
31
32     if (last)
33         index = last->index + 1;
34
35     tfe = xcalloc(1, sizeof(*tfe));
36
37     tfe->fileName = (file) ? xstrdup(file) : NULL;
38     tfe->script = (script && *script != '\0') ? xstrdup(script) : NULL;
39     tfe->prog = xstrdup(prog);
40     tfe->index = index;
41     tfe->next = NULL;
42
43     if (last)
44         last->next = tfe;
45     else
46         pkg->triggerFiles = tfe;
47
48     return index;
49 }
50
51 /* %trigger is a strange combination of %pre and Requires: behavior */
52 /* We can handle it by parsing the args before "--" in parseScript. */
53 /* We then pass the remaining arguments to parseRCPOT, along with   */
54 /* an index we just determined.                                     */
55
56 int parseScript(rpmSpec spec, int parsePart)
57 {
58     /* There are a few options to scripts: */
59     /*  <pkg>                              */
60     /*  -n <pkg>                           */
61     /*  -p <sh>                            */
62     /*  -p "<sh> <args>..."                */
63     /*  -f <file>                          */
64
65     char *p;
66     const char **progArgv = NULL;
67     int progArgc;
68     const char *partname = NULL;
69     rpmTag reqtag = 0;
70     rpmTag tag = 0;
71     rpmsenseFlags tagflags = 0;
72     rpmTag progtag = 0;
73     int flag = PART_SUBNAME;
74     Package pkg;
75     StringBuf sb = NULL;
76     int nextPart;
77     int index;
78     char * reqargs = NULL;
79
80     int res = PART_ERROR; /* assume failure */
81     int rc, argc;
82     int arg;
83     const char **argv = NULL;
84     poptContext optCon = NULL;
85     const char *name = NULL;
86     const char *prog = "/bin/sh";
87     const char *file = NULL;
88     struct poptOption optionsTable[] = {
89         { NULL, 'p', POPT_ARG_STRING, &prog, 'p',       NULL, NULL},
90         { NULL, 'n', POPT_ARG_STRING, &name, 'n',       NULL, NULL},
91         { NULL, 'f', POPT_ARG_STRING, &file, 'f',       NULL, NULL},
92         { 0, 0, 0, 0, 0,        NULL, NULL}
93     };
94
95     switch (parsePart) {
96       case PART_PRE:
97         tag = RPMTAG_PREIN;
98         tagflags = RPMSENSE_SCRIPT_PRE;
99         progtag = RPMTAG_PREINPROG;
100         partname = "%pre";
101         break;
102       case PART_POST:
103         tag = RPMTAG_POSTIN;
104         tagflags = RPMSENSE_SCRIPT_POST;
105         progtag = RPMTAG_POSTINPROG;
106         partname = "%post";
107         break;
108       case PART_PREUN:
109         tag = RPMTAG_PREUN;
110         tagflags = RPMSENSE_SCRIPT_PREUN;
111         progtag = RPMTAG_PREUNPROG;
112         partname = "%preun";
113         break;
114       case PART_POSTUN:
115         tag = RPMTAG_POSTUN;
116         tagflags = RPMSENSE_SCRIPT_POSTUN;
117         progtag = RPMTAG_POSTUNPROG;
118         partname = "%postun";
119         break;
120       case PART_PRETRANS:
121         tag = RPMTAG_PRETRANS;
122         tagflags = 0;
123         progtag = RPMTAG_PRETRANSPROG;
124         partname = "%pretrans";
125         break;
126       case PART_POSTTRANS:
127         tag = RPMTAG_POSTTRANS;
128         tagflags = 0;
129         progtag = RPMTAG_POSTTRANSPROG;
130         partname = "%posttrans";
131         break;
132       case PART_VERIFYSCRIPT:
133         tag = RPMTAG_VERIFYSCRIPT;
134         tagflags = RPMSENSE_SCRIPT_VERIFY;
135         progtag = RPMTAG_VERIFYSCRIPTPROG;
136         partname = "%verifyscript";
137         break;
138       case PART_TRIGGERPREIN:
139         tag = RPMTAG_TRIGGERSCRIPTS;
140         tagflags = 0;
141         reqtag = RPMTAG_TRIGGERPREIN;
142         progtag = RPMTAG_TRIGGERSCRIPTPROG;
143         partname = "%triggerprein";
144         break;
145       case PART_TRIGGERIN:
146         tag = RPMTAG_TRIGGERSCRIPTS;
147         tagflags = 0;
148         reqtag = RPMTAG_TRIGGERIN;
149         progtag = RPMTAG_TRIGGERSCRIPTPROG;
150         partname = "%triggerin";
151         break;
152       case PART_TRIGGERUN:
153         tag = RPMTAG_TRIGGERSCRIPTS;
154         tagflags = 0;
155         reqtag = RPMTAG_TRIGGERUN;
156         progtag = RPMTAG_TRIGGERSCRIPTPROG;
157         partname = "%triggerun";
158         break;
159       case PART_TRIGGERPOSTUN:
160         tag = RPMTAG_TRIGGERSCRIPTS;
161         tagflags = 0;
162         reqtag = RPMTAG_TRIGGERPOSTUN;
163         progtag = RPMTAG_TRIGGERSCRIPTPROG;
164         partname = "%triggerpostun";
165         break;
166     }
167
168     if (tag == RPMTAG_TRIGGERSCRIPTS) {
169         /* break line into two */
170         p = strstr(spec->line, "--");
171         if (!p) {
172             rpmlog(RPMLOG_ERR, _("line %d: triggers must have --: %s\n"),
173                      spec->lineNum, spec->line);
174             return PART_ERROR;
175         }
176
177         *p = '\0';
178         reqargs = xstrdup(p + 2);
179     }
180     
181     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
182         rpmlog(RPMLOG_ERR, _("line %d: Error parsing %s: %s\n"),
183                  spec->lineNum, partname, poptStrerror(rc));
184         goto exit;
185     }
186     
187     optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
188     while ((arg = poptGetNextOpt(optCon)) > 0) {
189         switch (arg) {
190         case 'p':
191             if (prog[0] == '<') {
192                 if (prog[strlen(prog)-1] != '>') {
193                     rpmlog(RPMLOG_ERR,
194                              _("line %d: internal script must end "
195                              "with \'>\': %s\n"), spec->lineNum, prog);
196                     goto exit;
197                 }
198             } else if (prog[0] != '/') {
199                 rpmlog(RPMLOG_ERR,
200                          _("line %d: script program must begin "
201                          "with \'/\': %s\n"), spec->lineNum, prog);
202                 goto exit;
203             }
204             break;
205         case 'n':
206             flag = PART_NAME;
207             break;
208         }
209     }
210     
211     if (arg < -1) {
212         rpmlog(RPMLOG_ERR, _("line %d: Bad option %s: %s\n"),
213                  spec->lineNum,
214                  poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
215                  spec->line);
216         goto exit;
217     }
218
219     if (poptPeekArg(optCon)) {
220         if (name == NULL)
221             name = poptGetArg(optCon);
222         if (poptPeekArg(optCon)) {
223             rpmlog(RPMLOG_ERR, _("line %d: Too many names: %s\n"),
224                      spec->lineNum,
225                      spec->line);
226             goto exit;
227         }
228     }
229     
230     if (lookupPackage(spec, name, flag, &pkg)) {
231         rpmlog(RPMLOG_ERR, _("line %d: Package does not exist: %s\n"),
232                  spec->lineNum, spec->line);
233         goto exit;
234     }
235
236     if (tag != RPMTAG_TRIGGERSCRIPTS) {
237         if (headerIsEntry(pkg->header, progtag)) {
238             rpmlog(RPMLOG_ERR, _("line %d: Second %s\n"),
239                      spec->lineNum, partname);
240             goto exit;
241         }
242     }
243
244     if ((rc = poptParseArgvString(prog, &progArgc, &progArgv))) {
245         rpmlog(RPMLOG_ERR, _("line %d: Error parsing %s: %s\n"),
246                  spec->lineNum, partname, poptStrerror(rc));
247         goto exit;
248     }
249
250     sb = newStringBuf();
251     if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
252         nextPart = PART_NONE;
253     } else if (rc < 0) {
254         goto exit;
255     } else {
256         while (! (nextPart = isPart(spec->line))) {
257             appendStringBuf(sb, spec->line);
258             if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
259                 nextPart = PART_NONE;
260                 break;
261             } else if (rc < 0) {
262                 goto exit;
263             }
264         }
265     }
266     stripTrailingBlanksStringBuf(sb);
267     p = getStringBuf(sb);
268
269 #ifdef WITH_LUA
270     if (rstreq(progArgv[0], "<lua>")) {
271         rpmlua lua = NULL; /* Global state. */
272         if (rpmluaCheckScript(lua, p, partname) != RPMRC_OK) {
273             goto exit;
274         }
275         (void) rpmlibNeedsFeature(pkg->header,
276                                   "BuiltinLuaScripts", "4.2.2-1");
277     } else
278 #endif
279     if (progArgv[0][0] == '<') {
280         rpmlog(RPMLOG_ERR,
281                  _("line %d: unsupported internal script: %s\n"),
282                  spec->lineNum, progArgv[0]);
283         goto exit;
284     } else {
285         (void) addReqProv(spec, pkg->header, RPMTAG_REQUIRENAME,
286                 progArgv[0], NULL, (tagflags | RPMSENSE_INTERP), 0);
287     }
288
289     /* Trigger script insertion is always delayed in order to */
290     /* get the index right.                                   */
291     if (tag == RPMTAG_TRIGGERSCRIPTS) {
292         /* Add file/index/prog triple to the trigger file list */
293         index = addTriggerIndex(pkg, file, p, progArgv[0]);
294
295         /* Generate the trigger tags */
296         if ((rc = parseRCPOT(spec, pkg, reqargs, reqtag, index, tagflags)))
297             goto exit;
298     } else {
299         struct rpmtd_s td;
300
301         /*
302          * XXX Ancient rpm uses STRING, not STRING_ARRAY type here. Construct
303          * the td manually and preserve legacy compat for now...
304          */
305         rpmtdReset(&td);
306         td.tag = progtag;
307         td.count = progArgc;
308         if (progArgc == 1) {
309             td.data = (void *) *progArgv;
310             td.type = RPM_STRING_TYPE;
311         } else {
312             (void) rpmlibNeedsFeature(pkg->header,
313                         "ScriptletInterpreterArgs", "4.0.3-1");
314             td.data = progArgv;
315             td.type = RPM_STRING_ARRAY_TYPE;
316         }
317         headerPut(pkg->header, &td, HEADERPUT_DEFAULT);
318
319         if (*p != '\0') {
320             headerPutString(pkg->header, tag, p);
321         }
322
323         if (file) {
324             switch (parsePart) {
325               case PART_PRE:
326                 pkg->preInFile = xstrdup(file);
327                 break;
328               case PART_POST:
329                 pkg->postInFile = xstrdup(file);
330                 break;
331               case PART_PREUN:
332                 pkg->preUnFile = xstrdup(file);
333                 break;
334               case PART_POSTUN:
335                 pkg->postUnFile = xstrdup(file);
336                 break;
337               case PART_PRETRANS:
338                 pkg->preTransFile = xstrdup(file);
339                 break;
340               case PART_POSTTRANS:
341                 pkg->postTransFile = xstrdup(file);
342                 break;
343               case PART_VERIFYSCRIPT:
344                 pkg->verifyFile = xstrdup(file);
345                 break;
346             }
347         }
348     }
349     res = nextPart;
350     
351 exit:
352     free(reqargs);
353     sb = freeStringBuf(sb);
354     progArgv = _free(progArgv);
355     argv = _free(argv);
356     optCon = poptFreeContext(optCon);
357     
358     return res;
359 }