Add support for global LDFLAGS
[platform/upstream/rpm.git] / build / parseScript.c
index 713d998..1a149e1 100644 (file)
@@ -6,10 +6,12 @@
 #include "system.h"
 
 #include <rpm/header.h>
-#include <rpm/rpmbuild.h>
 #include <rpm/rpmlog.h>
 
 #include "rpmio/rpmlua.h"
+#include "lib/rpmscript.h"     /* script flags */
+#include "build/rpmbuild_internal.h"
+#include "build/rpmbuild_misc.h"
 
 #include "debug.h"
 
@@ -17,7 +19,7 @@
 /**
  */
 static int addTriggerIndex(Package pkg, const char *file,
-       const char *script, const char *prog)
+       const char *script, const char *prog, rpmscriptFlags flags)
 {
     struct TriggerFileEntry *tfe;
     struct TriggerFileEntry *list = pkg->triggerFiles;
@@ -37,6 +39,7 @@ static int addTriggerIndex(Package pkg, const char *file,
     tfe->fileName = (file) ? xstrdup(file) : NULL;
     tfe->script = (script && *script != '\0') ? xstrdup(script) : NULL;
     tfe->prog = xstrdup(prog);
+    tfe->flags = flags;
     tfe->index = index;
     tfe->next = NULL;
 
@@ -48,17 +51,6 @@ static int addTriggerIndex(Package pkg, const char *file,
     return index;
 }
 
-/* these have to be global because of stupid compilers */
-    static const char *name = NULL;
-    static const char *prog = NULL;
-    static const char *file = NULL;
-    static struct poptOption optionsTable[] = {
-       { NULL, 'p', POPT_ARG_STRING, &prog, 'p',       NULL, NULL},
-       { NULL, 'n', POPT_ARG_STRING, &name, 'n',       NULL, NULL},
-       { NULL, 'f', POPT_ARG_STRING, &file, 'f',       NULL, NULL},
-       { 0, 0, 0, 0, 0,        NULL, NULL}
-    };
-
 /* %trigger is a strange combination of %pre and Requires: behavior */
 /* We can handle it by parsing the args before "--" in parseScript. */
 /* We then pass the remaining arguments to parseRCPOT, along with   */
@@ -73,14 +65,16 @@ int parseScript(rpmSpec spec, int parsePart)
     /*  -p "<sh> <args>..."                */
     /*  -f <file>                          */
 
-    char *p;
+    const char *p;
     const char **progArgv = NULL;
     int progArgc;
     const char *partname = NULL;
-    rpmTag reqtag = 0;
-    rpmTag tag = 0;
+    rpmTagVal reqtag = 0;
+    rpmTagVal tag = 0;
     rpmsenseFlags tagflags = 0;
-    rpmTag progtag = 0;
+    rpmTagVal progtag = 0;
+    rpmTagVal flagtag = 0;
+    rpmscriptFlags scriptFlags = 0;
     int flag = PART_SUBNAME;
     Package pkg;
     StringBuf sb = NULL;
@@ -93,52 +87,68 @@ int parseScript(rpmSpec spec, int parsePart)
     int arg;
     const char **argv = NULL;
     poptContext optCon = NULL;
-    
-    name = NULL;
-    prog = "/bin/sh";
-    file = NULL;
-    
+    const char *name = NULL;
+    const char *prog = "/bin/sh";
+    const char *file = NULL;
+    struct poptOption optionsTable[] = {
+       { NULL, 'p', POPT_ARG_STRING, &prog, 'p',       NULL, NULL},
+       { NULL, 'n', POPT_ARG_STRING, &name, 'n',       NULL, NULL},
+       { NULL, 'f', POPT_ARG_STRING, &file, 'f',       NULL, NULL},
+       { NULL, 'e', POPT_BIT_SET, &scriptFlags, RPMSCRIPT_FLAG_EXPAND,
+         NULL, NULL},
+       { NULL, 'q', POPT_BIT_SET, &scriptFlags, RPMSCRIPT_FLAG_QFORMAT,
+         NULL, NULL},
+       { 0, 0, 0, 0, 0,        NULL, NULL}
+    };
+
     switch (parsePart) {
       case PART_PRE:
        tag = RPMTAG_PREIN;
        tagflags = RPMSENSE_SCRIPT_PRE;
        progtag = RPMTAG_PREINPROG;
+       flagtag = RPMTAG_PREINFLAGS;
        partname = "%pre";
        break;
       case PART_POST:
        tag = RPMTAG_POSTIN;
        tagflags = RPMSENSE_SCRIPT_POST;
        progtag = RPMTAG_POSTINPROG;
+       flagtag = RPMTAG_POSTINFLAGS;
        partname = "%post";
        break;
       case PART_PREUN:
        tag = RPMTAG_PREUN;
        tagflags = RPMSENSE_SCRIPT_PREUN;
        progtag = RPMTAG_PREUNPROG;
+       flagtag = RPMTAG_PREUNFLAGS;
        partname = "%preun";
        break;
       case PART_POSTUN:
        tag = RPMTAG_POSTUN;
        tagflags = RPMSENSE_SCRIPT_POSTUN;
        progtag = RPMTAG_POSTUNPROG;
+       flagtag = RPMTAG_POSTUNFLAGS;
        partname = "%postun";
        break;
       case PART_PRETRANS:
        tag = RPMTAG_PRETRANS;
-       tagflags = 0;
+       tagflags = RPMSENSE_PRETRANS;
        progtag = RPMTAG_PRETRANSPROG;
+       flagtag = RPMTAG_PRETRANSFLAGS;
        partname = "%pretrans";
        break;
       case PART_POSTTRANS:
        tag = RPMTAG_POSTTRANS;
-       tagflags = 0;
+       tagflags = RPMSENSE_POSTTRANS;
        progtag = RPMTAG_POSTTRANSPROG;
+       flagtag = RPMTAG_POSTTRANSFLAGS;
        partname = "%posttrans";
        break;
       case PART_VERIFYSCRIPT:
        tag = RPMTAG_VERIFYSCRIPT;
        tagflags = RPMSENSE_SCRIPT_VERIFY;
        progtag = RPMTAG_VERIFYSCRIPTPROG;
+       flagtag = RPMTAG_VERIFYSCRIPTFLAGS;
        partname = "%verifyscript";
        break;
       case PART_TRIGGERPREIN:
@@ -146,6 +156,7 @@ int parseScript(rpmSpec spec, int parsePart)
        tagflags = 0;
        reqtag = RPMTAG_TRIGGERPREIN;
        progtag = RPMTAG_TRIGGERSCRIPTPROG;
+       flagtag = RPMTAG_TRIGGERSCRIPTFLAGS;
        partname = "%triggerprein";
        break;
       case PART_TRIGGERIN:
@@ -153,6 +164,7 @@ int parseScript(rpmSpec spec, int parsePart)
        tagflags = 0;
        reqtag = RPMTAG_TRIGGERIN;
        progtag = RPMTAG_TRIGGERSCRIPTPROG;
+       flagtag = RPMTAG_TRIGGERSCRIPTFLAGS;
        partname = "%triggerin";
        break;
       case PART_TRIGGERUN:
@@ -160,6 +172,7 @@ int parseScript(rpmSpec spec, int parsePart)
        tagflags = 0;
        reqtag = RPMTAG_TRIGGERUN;
        progtag = RPMTAG_TRIGGERSCRIPTPROG;
+       flagtag = RPMTAG_TRIGGERSCRIPTFLAGS;
        partname = "%triggerun";
        break;
       case PART_TRIGGERPOSTUN:
@@ -167,21 +180,22 @@ int parseScript(rpmSpec spec, int parsePart)
        tagflags = 0;
        reqtag = RPMTAG_TRIGGERPOSTUN;
        progtag = RPMTAG_TRIGGERSCRIPTPROG;
+       flagtag = RPMTAG_TRIGGERSCRIPTFLAGS;
        partname = "%triggerpostun";
        break;
     }
 
     if (tag == RPMTAG_TRIGGERSCRIPTS) {
        /* break line into two */
-       p = strstr(spec->line, "--");
-       if (!p) {
+       char *s = strstr(spec->line, "--");
+       if (!s) {
            rpmlog(RPMLOG_ERR, _("line %d: triggers must have --: %s\n"),
                     spec->lineNum, spec->line);
            return PART_ERROR;
        }
 
-       *p = '\0';
-       reqargs = xstrdup(p + 2);
+       *s = '\0';
+       reqargs = xstrdup(s + 2);
     }
     
     if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
@@ -273,7 +287,7 @@ int parseScript(rpmSpec spec, int parsePart)
     p = getStringBuf(sb);
 
 #ifdef WITH_LUA
-    if (!strcmp(progArgv[0], "<lua>")) {
+    if (rstreq(progArgv[0], "<lua>")) {
        rpmlua lua = NULL; /* Global state. */
        if (rpmluaCheckScript(lua, p, partname) != RPMRC_OK) {
            goto exit;
@@ -288,18 +302,28 @@ int parseScript(rpmSpec spec, int parsePart)
                 spec->lineNum, progArgv[0]);
        goto exit;
     } else {
-        (void) addReqProv(spec, pkg->header, RPMTAG_REQUIRENAME,
+        (void) addReqProv(pkg->header, RPMTAG_REQUIRENAME,
                progArgv[0], NULL, (tagflags | RPMSENSE_INTERP), 0);
     }
 
+    if (scriptFlags) {
+       rpmlibNeedsFeature(pkg->header, "ScriptletExpansion", "4.9.0-1");
+    }
+
     /* Trigger script insertion is always delayed in order to */
     /* get the index right.                                   */
     if (tag == RPMTAG_TRIGGERSCRIPTS) {
+       if (progArgc > 1) {
+           rpmlog(RPMLOG_ERR,
+             _("line %d: interpreter arguments not allowed in triggers: %s\n"),
+             spec->lineNum, prog);
+           goto exit;
+       }
        /* Add file/index/prog triple to the trigger file list */
-       index = addTriggerIndex(pkg, file, p, progArgv[0]);
+       index = addTriggerIndex(pkg, file, p, progArgv[0], scriptFlags);
 
        /* Generate the trigger tags */
-       if ((rc = parseRCPOT(spec, pkg, reqargs, reqtag, index, tagflags)))
+       if (parseRCPOT(spec, pkg, reqargs, reqtag, index, tagflags))
            goto exit;
     } else {
        struct rpmtd_s td;
@@ -325,6 +349,9 @@ int parseScript(rpmSpec spec, int parsePart)
        if (*p != '\0') {
            headerPutString(pkg->header, tag, p);
        }
+       if (scriptFlags) {
+           headerPutUint32(pkg->header, flagtag, &scriptFlags, 1);
+       }
 
        if (file) {
            switch (parsePart) {
@@ -356,10 +383,10 @@ int parseScript(rpmSpec spec, int parsePart)
     
 exit:
     free(reqargs);
-    sb = freeStringBuf(sb);
-    progArgv = _free(progArgv);
-    argv = _free(argv);
-    optCon = poptFreeContext(optCon);
+    freeStringBuf(sb);
+    free(progArgv);
+    free(argv);
+    poptFreeContext(optCon);
     
     return res;
 }