00001
00006 #include "system.h"
00007
00008 #include "rpmbuild.h"
00009 #include "debug.h"
00010
00011
00012
00013
00016 static int addTriggerIndex(Package pkg, const char *file,
00017 const char *script, const char *prog)
00018 {
00019 struct TriggerFileEntry *tfe;
00020 struct TriggerFileEntry *list = pkg->triggerFiles;
00021 struct TriggerFileEntry *last = NULL;
00022 int index = 0;
00023
00024 while (list) {
00025 last = list;
00026 list = list->next;
00027 }
00028
00029 if (last) {
00030 index = last->index + 1;
00031 }
00032
00033 tfe = xmalloc(sizeof(*tfe));
00034
00035 tfe->fileName = (file) ? xstrdup(file) : NULL;
00036 tfe->script = (script && *script != '\0') ? xstrdup(script) : NULL;
00037 tfe->prog = xstrdup(prog);
00038 tfe->index = index;
00039 tfe->next = NULL;
00040
00041 if (last) {
00042 last->next = tfe;
00043 } else {
00044 pkg->triggerFiles = tfe;
00045 }
00046
00047 return index;
00048 }
00049
00050
00051 static const char *name = NULL;
00052 static const char *prog = NULL;
00053 static const char *file = NULL;
00054 static struct poptOption optionsTable[] = {
00055 { NULL, 'p', POPT_ARG_STRING, &prog, 'p', NULL, NULL},
00056 { NULL, 'n', POPT_ARG_STRING, &name, 'n', NULL, NULL},
00057 { NULL, 'f', POPT_ARG_STRING, &file, 'f', NULL, NULL},
00058 { 0, 0, 0, 0, 0, NULL, NULL}
00059 };
00060
00061
00062
00063
00064
00065
00066 int parseScript(Spec spec, int parsePart)
00067 {
00068
00069
00070
00071
00072
00073
00074
00075 char *p;
00076 const char **progArgv = NULL;
00077 int progArgc;
00078 char *partname = NULL;
00079 int reqtag = 0;
00080 int tag = 0;
00081 int tagflags = 0;
00082 int progtag = 0;
00083 int flag = PART_SUBNAME;
00084 Package pkg;
00085 StringBuf sb = NULL;
00086 int nextPart;
00087 int index;
00088 char reqargs[BUFSIZ];
00089
00090 int rc, argc;
00091 int arg;
00092 const char **argv = NULL;
00093 poptContext optCon = NULL;
00094
00095 name = NULL;
00096 prog = "/bin/sh";
00097 file = NULL;
00098
00099 switch (parsePart) {
00100 case PART_PRE:
00101 tag = RPMTAG_PREIN;
00102 tagflags = RPMSENSE_SCRIPT_PRE;
00103 progtag = RPMTAG_PREINPROG;
00104 partname = "%pre";
00105 break;
00106 case PART_POST:
00107 tag = RPMTAG_POSTIN;
00108 tagflags = RPMSENSE_SCRIPT_POST;
00109 progtag = RPMTAG_POSTINPROG;
00110 partname = "%post";
00111 break;
00112 case PART_PREUN:
00113 tag = RPMTAG_PREUN;
00114 tagflags = RPMSENSE_SCRIPT_PREUN;
00115 progtag = RPMTAG_PREUNPROG;
00116 partname = "%preun";
00117 break;
00118 case PART_POSTUN:
00119 tag = RPMTAG_POSTUN;
00120 tagflags = RPMSENSE_SCRIPT_POSTUN;
00121 progtag = RPMTAG_POSTUNPROG;
00122 partname = "%postun";
00123 break;
00124 case PART_VERIFYSCRIPT:
00125 tag = RPMTAG_VERIFYSCRIPT;
00126 tagflags = RPMSENSE_SCRIPT_VERIFY;
00127 progtag = RPMTAG_VERIFYSCRIPTPROG;
00128 partname = "%verifyscript";
00129 break;
00130 case PART_TRIGGERIN:
00131 tag = RPMTAG_TRIGGERSCRIPTS;
00132 tagflags = 0;
00133 reqtag = RPMTAG_TRIGGERIN;
00134 progtag = RPMTAG_TRIGGERSCRIPTPROG;
00135 partname = "%triggerin";
00136 break;
00137 case PART_TRIGGERUN:
00138 tag = RPMTAG_TRIGGERSCRIPTS;
00139 tagflags = 0;
00140 reqtag = RPMTAG_TRIGGERUN;
00141 progtag = RPMTAG_TRIGGERSCRIPTPROG;
00142 partname = "%triggerun";
00143 break;
00144 case PART_TRIGGERPOSTUN:
00145 tag = RPMTAG_TRIGGERSCRIPTS;
00146 tagflags = 0;
00147 reqtag = RPMTAG_TRIGGERPOSTUN;
00148 progtag = RPMTAG_TRIGGERSCRIPTPROG;
00149 partname = "%triggerpostun";
00150 break;
00151 }
00152
00153 if (tag == RPMTAG_TRIGGERSCRIPTS) {
00154
00155 p = strstr(spec->line, "--");
00156 if (!p) {
00157 rpmError(RPMERR_BADSPEC, _("line %d: triggers must have --: %s\n"),
00158 spec->lineNum, spec->line);
00159 return RPMERR_BADSPEC;
00160 }
00161
00162 *p = '\0';
00163 strcpy(reqargs, p + 2);
00164 }
00165
00166 if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
00167 rpmError(RPMERR_BADSPEC, _("line %d: Error parsing %s: %s\n"),
00168 spec->lineNum, partname, poptStrerror(rc));
00169 return RPMERR_BADSPEC;
00170 }
00171
00172 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
00173 while ((arg = poptGetNextOpt(optCon)) > 0) {
00174 switch (arg) {
00175 case 'p':
00176 if (prog[0] != '/') {
00177 rpmError(RPMERR_BADSPEC,
00178 _("line %d: script program must begin "
00179 "with \'/\': %s\n"), spec->lineNum, prog);
00180 rc = RPMERR_BADSPEC;
00181 goto exit;
00182 }
00183 break;
00184 case 'n':
00185 flag = PART_NAME;
00186 break;
00187 }
00188 }
00189
00190 if (arg < -1) {
00191 rpmError(RPMERR_BADSPEC, _("line %d: Bad option %s: %s\n"),
00192 spec->lineNum,
00193 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
00194 spec->line);
00195 rc = RPMERR_BADSPEC;
00196 goto exit;
00197 }
00198
00199 if (poptPeekArg(optCon)) {
00200 if (name == NULL)
00201 name = poptGetArg(optCon);
00202 if (poptPeekArg(optCon)) {
00203 rpmError(RPMERR_BADSPEC, _("line %d: Too many names: %s\n"),
00204 spec->lineNum,
00205 spec->line);
00206 rc = RPMERR_BADSPEC;
00207 goto exit;
00208 }
00209 }
00210
00211 if (lookupPackage(spec, name, flag, &pkg)) {
00212 rpmError(RPMERR_BADSPEC, _("line %d: Package does not exist: %s\n"),
00213 spec->lineNum, spec->line);
00214 rc = RPMERR_BADSPEC;
00215 goto exit;
00216 }
00217
00218 if (tag != RPMTAG_TRIGGERSCRIPTS) {
00219 if (headerIsEntry(pkg->header, progtag)) {
00220 rpmError(RPMERR_BADSPEC, _("line %d: Second %s\n"),
00221 spec->lineNum, partname);
00222 rc = RPMERR_BADSPEC;
00223 goto exit;
00224 }
00225 }
00226
00227 if ((rc = poptParseArgvString(prog, &progArgc, &progArgv))) {
00228 rpmError(RPMERR_BADSPEC, _("line %d: Error parsing %s: %s\n"),
00229 spec->lineNum, partname, poptStrerror(rc));
00230 rc = RPMERR_BADSPEC;
00231 goto exit;
00232 }
00233
00234 sb = newStringBuf();
00235 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
00236 nextPart = PART_NONE;
00237 } else {
00238 if (rc)
00239 goto exit;
00240 while (! (nextPart = isPart(spec->line))) {
00241 appendStringBuf(sb, spec->line);
00242 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
00243 nextPart = PART_NONE;
00244 break;
00245 }
00246 if (rc)
00247 goto exit;
00248 }
00249 }
00250 stripTrailingBlanksStringBuf(sb);
00251 p = getStringBuf(sb);
00252
00253 (void) addReqProv(spec, pkg->header, (tagflags | RPMSENSE_INTERP), progArgv[0], NULL, 0);
00254
00255
00256
00257 if (tag == RPMTAG_TRIGGERSCRIPTS) {
00258
00259 index = addTriggerIndex(pkg, file, p, progArgv[0]);
00260
00261
00262 if ((rc = parseRCPOT(spec, pkg, reqargs, reqtag, index, tagflags)))
00263 goto exit;
00264 } else {
00265 if (progArgc == 1)
00266 (void) headerAddEntry(pkg->header, progtag, RPM_STRING_TYPE,
00267 *progArgv, progArgc);
00268 else {
00269 (void) rpmlibNeedsFeature(pkg->header,
00270 "ScriptletInterpreterArgs", "4.0.3-1");
00271 (void) headerAddEntry(pkg->header, progtag, RPM_STRING_ARRAY_TYPE,
00272 progArgv, progArgc);
00273 }
00274
00275 if (*p != '\0')
00276 (void) headerAddEntry(pkg->header, tag, RPM_STRING_TYPE, p, 1);
00277
00278 if (file) {
00279 switch (parsePart) {
00280 case PART_PRE:
00281 pkg->preInFile = xstrdup(file);
00282 break;
00283 case PART_POST:
00284 pkg->postInFile = xstrdup(file);
00285 break;
00286 case PART_PREUN:
00287 pkg->preUnFile = xstrdup(file);
00288 break;
00289 case PART_POSTUN:
00290 pkg->postUnFile = xstrdup(file);
00291 break;
00292 case PART_VERIFYSCRIPT:
00293 pkg->verifyFile = xstrdup(file);
00294 break;
00295 }
00296 }
00297 }
00298 rc = nextPart;
00299
00300 exit:
00301 sb = freeStringBuf(sb);
00302 progArgv = _free(progArgv);
00303 argv = _free(argv);
00304 optCon = poptFreeContext(optCon);
00305
00306 return rc;
00307 }