2 * \file build/parseScript.c
3 * Parse install-time script section from spec file.
8 #include <rpm/header.h>
9 #include <rpm/rpmbuild.h>
10 #include <rpm/rpmlog.h>
12 #include "rpmio/rpmlua.h"
19 static int addTriggerIndex(Package pkg, const char *file,
20 const char *script, const char *prog)
22 struct TriggerFileEntry *tfe;
23 struct TriggerFileEntry *list = pkg->triggerFiles;
24 struct TriggerFileEntry *last = NULL;
33 index = last->index + 1;
35 tfe = xcalloc(1, sizeof(*tfe));
37 tfe->fileName = (file) ? xstrdup(file) : NULL;
38 tfe->script = (script && *script != '\0') ? xstrdup(script) : NULL;
39 tfe->prog = xstrdup(prog);
46 pkg->triggerFiles = tfe;
51 /* these have to be global because of stupid compilers */
52 static const char *name = NULL;
53 static const char *prog = NULL;
54 static const char *file = NULL;
55 static struct poptOption optionsTable[] = {
56 { NULL, 'p', POPT_ARG_STRING, &prog, 'p', NULL, NULL},
57 { NULL, 'n', POPT_ARG_STRING, &name, 'n', NULL, NULL},
58 { NULL, 'f', POPT_ARG_STRING, &file, 'f', NULL, NULL},
59 { 0, 0, 0, 0, 0, NULL, NULL}
62 /* %trigger is a strange combination of %pre and Requires: behavior */
63 /* We can handle it by parsing the args before "--" in parseScript. */
64 /* We then pass the remaining arguments to parseRCPOT, along with */
65 /* an index we just determined. */
67 int parseScript(rpmSpec spec, int parsePart)
69 /* There are a few options to scripts: */
73 /* -p "<sh> <args>..." */
77 const char **progArgv = NULL;
79 const char *partname = NULL;
82 rpmsenseFlags tagflags = 0;
84 int flag = PART_SUBNAME;
89 char * reqargs = NULL;
91 int res = PART_ERROR; /* assume failure */
94 const char **argv = NULL;
95 poptContext optCon = NULL;
104 tagflags = RPMSENSE_SCRIPT_PRE;
105 progtag = RPMTAG_PREINPROG;
110 tagflags = RPMSENSE_SCRIPT_POST;
111 progtag = RPMTAG_POSTINPROG;
116 tagflags = RPMSENSE_SCRIPT_PREUN;
117 progtag = RPMTAG_PREUNPROG;
122 tagflags = RPMSENSE_SCRIPT_POSTUN;
123 progtag = RPMTAG_POSTUNPROG;
124 partname = "%postun";
127 tag = RPMTAG_PRETRANS;
129 progtag = RPMTAG_PRETRANSPROG;
130 partname = "%pretrans";
133 tag = RPMTAG_POSTTRANS;
135 progtag = RPMTAG_POSTTRANSPROG;
136 partname = "%posttrans";
138 case PART_VERIFYSCRIPT:
139 tag = RPMTAG_VERIFYSCRIPT;
140 tagflags = RPMSENSE_SCRIPT_VERIFY;
141 progtag = RPMTAG_VERIFYSCRIPTPROG;
142 partname = "%verifyscript";
144 case PART_TRIGGERPREIN:
145 tag = RPMTAG_TRIGGERSCRIPTS;
147 reqtag = RPMTAG_TRIGGERPREIN;
148 progtag = RPMTAG_TRIGGERSCRIPTPROG;
149 partname = "%triggerprein";
152 tag = RPMTAG_TRIGGERSCRIPTS;
154 reqtag = RPMTAG_TRIGGERIN;
155 progtag = RPMTAG_TRIGGERSCRIPTPROG;
156 partname = "%triggerin";
159 tag = RPMTAG_TRIGGERSCRIPTS;
161 reqtag = RPMTAG_TRIGGERUN;
162 progtag = RPMTAG_TRIGGERSCRIPTPROG;
163 partname = "%triggerun";
165 case PART_TRIGGERPOSTUN:
166 tag = RPMTAG_TRIGGERSCRIPTS;
168 reqtag = RPMTAG_TRIGGERPOSTUN;
169 progtag = RPMTAG_TRIGGERSCRIPTPROG;
170 partname = "%triggerpostun";
174 if (tag == RPMTAG_TRIGGERSCRIPTS) {
175 /* break line into two */
176 p = strstr(spec->line, "--");
178 rpmlog(RPMLOG_ERR, _("line %d: triggers must have --: %s\n"),
179 spec->lineNum, spec->line);
184 reqargs = xstrdup(p + 2);
187 if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
188 rpmlog(RPMLOG_ERR, _("line %d: Error parsing %s: %s\n"),
189 spec->lineNum, partname, poptStrerror(rc));
193 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
194 while ((arg = poptGetNextOpt(optCon)) > 0) {
197 if (prog[0] == '<') {
198 if (prog[strlen(prog)-1] != '>') {
200 _("line %d: internal script must end "
201 "with \'>\': %s\n"), spec->lineNum, prog);
204 } else if (prog[0] != '/') {
206 _("line %d: script program must begin "
207 "with \'/\': %s\n"), spec->lineNum, prog);
218 rpmlog(RPMLOG_ERR, _("line %d: Bad option %s: %s\n"),
220 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
225 if (poptPeekArg(optCon)) {
227 name = poptGetArg(optCon);
228 if (poptPeekArg(optCon)) {
229 rpmlog(RPMLOG_ERR, _("line %d: Too many names: %s\n"),
236 if (lookupPackage(spec, name, flag, &pkg)) {
237 rpmlog(RPMLOG_ERR, _("line %d: Package does not exist: %s\n"),
238 spec->lineNum, spec->line);
242 if (tag != RPMTAG_TRIGGERSCRIPTS) {
243 if (headerIsEntry(pkg->header, progtag)) {
244 rpmlog(RPMLOG_ERR, _("line %d: Second %s\n"),
245 spec->lineNum, partname);
250 if ((rc = poptParseArgvString(prog, &progArgc, &progArgv))) {
251 rpmlog(RPMLOG_ERR, _("line %d: Error parsing %s: %s\n"),
252 spec->lineNum, partname, poptStrerror(rc));
257 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
258 nextPart = PART_NONE;
262 while (! (nextPart = isPart(spec->line))) {
263 appendStringBuf(sb, spec->line);
264 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
265 nextPart = PART_NONE;
272 stripTrailingBlanksStringBuf(sb);
273 p = getStringBuf(sb);
276 if (!strcmp(progArgv[0], "<lua>")) {
277 rpmlua lua = NULL; /* Global state. */
278 if (rpmluaCheckScript(lua, p, partname) != RPMRC_OK) {
281 (void) rpmlibNeedsFeature(pkg->header,
282 "BuiltinLuaScripts", "4.2.2-1");
285 if (progArgv[0][0] == '<') {
287 _("line %d: unsupported internal script: %s\n"),
288 spec->lineNum, progArgv[0]);
291 (void) addReqProv(spec, pkg->header, RPMTAG_REQUIRENAME,
292 progArgv[0], NULL, (tagflags | RPMSENSE_INTERP), 0);
295 /* Trigger script insertion is always delayed in order to */
296 /* get the index right. */
297 if (tag == RPMTAG_TRIGGERSCRIPTS) {
298 /* Add file/index/prog triple to the trigger file list */
299 index = addTriggerIndex(pkg, file, p, progArgv[0]);
301 /* Generate the trigger tags */
302 if ((rc = parseRCPOT(spec, pkg, reqargs, reqtag, index, tagflags)))
308 * XXX Ancient rpm uses STRING, not STRING_ARRAY type here. Construct
309 * the td manually and preserve legacy compat for now...
316 td.type = RPM_STRING_TYPE;
318 (void) rpmlibNeedsFeature(pkg->header,
319 "ScriptletInterpreterArgs", "4.0.3-1");
320 td.type = RPM_STRING_ARRAY_TYPE;
322 headerPut(pkg->header, &td, HEADERPUT_DEFAULT);
325 if (rpmtdFromString(&td, tag, p))
326 headerPut(pkg->header, &td, HEADERPUT_DEFAULT);
332 pkg->preInFile = xstrdup(file);
335 pkg->postInFile = xstrdup(file);
338 pkg->preUnFile = xstrdup(file);
341 pkg->postUnFile = xstrdup(file);
344 pkg->preTransFile = xstrdup(file);
347 pkg->postTransFile = xstrdup(file);
349 case PART_VERIFYSCRIPT:
350 pkg->verifyFile = xstrdup(file);
359 sb = freeStringBuf(sb);
360 progArgv = _free(progArgv);
362 optCon = poptFreeContext(optCon);