summaryrefslogtreecommitdiff
path: root/build/parseScript.c
blob: 55ba1dcf0611ba23b728512d0ee2e605abafd69e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <malloc.h>
#include <string.h>

#include "header.h"
#include "read.h"
#include "part.h"
#include "misc.h"
#include "rpmlib.h"
#include "popt/popt.h"
#include "reqprov.h"
#include "package.h"

/* Define this to be 1 to turn on -p "<prog> <args>..." ability */
#define USE_PROG_STRING_ARRAY 0

int parseScript(Spec spec, int parsePart)
{
    /* There are a few options to scripts: */
    /*  <pkg>                              */
    /*  -n <pkg>                           */
    /*  -p <sh>                            */
    /*  -p "<sh> <args>..."                */
    /*  -f <file>                          */

    char *p;
    char *name = NULL;
    char *prog = "/bin/sh";
    char *file = NULL;
    char **progArgv = NULL;
    int progArgc;
    char *partname = NULL;
    int tag = 0;
    int progtag = 0;
    int flag = PART_SUBNAME;
    Package pkg;
    StringBuf sb;
    int nextPart;
    
    int rc, argc;
    char arg, **argv = NULL;
    poptContext optCon = NULL;
    struct poptOption optionsTable[] = {
	{ NULL, 'p', POPT_ARG_STRING, &prog, 'p' },
	{ NULL, 'n', POPT_ARG_STRING, &name, 'n' },
	{ NULL, 'f', POPT_ARG_STRING, &file, 'f' },
	{ 0, 0, 0, 0, 0 }
    };
    
    switch (parsePart) {
      case PART_PRE:
	tag = RPMTAG_PREIN;
	progtag = RPMTAG_PREINPROG;
	partname = "%pre";
	break;
      case PART_POST:
	tag = RPMTAG_POSTIN;
	progtag = RPMTAG_POSTINPROG;
	partname = "%post";
	break;
      case PART_PREUN:
	tag = RPMTAG_PREUN;
	progtag = RPMTAG_PREUNPROG;
	partname = "%preun";
	break;
      case PART_POSTUN:
	tag = RPMTAG_POSTUN;
	progtag = RPMTAG_POSTUNPROG;
	partname = "%postun";
	break;
      case PART_VERIFYSCRIPT:
	tag = PART_VERIFYSCRIPT;
	progtag = RPMTAG_VERIFYSCRIPTPROG;
	partname = "%verifyscript";
    }

    if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
	rpmError(RPMERR_BADSPEC, "line %d: Error parsing %s: %s",
		 spec->lineNum, partname, poptStrerror(rc));
	return RPMERR_BADSPEC;
    }
    
    optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
    while ((arg = poptGetNextOpt(optCon)) > 0) {
	if (arg == 'p') {
	    if (prog[0] != '/') {
		rpmError(RPMERR_BADSPEC,
			 "line %d: script program must begin "
			 "with \'/\': %s", prog);
		FREE(argv);
		poptFreeContext(optCon);
		return RPMERR_BADSPEC;
	    }
	} else if (arg == 'n') {
	    flag = PART_NAME;
	}
    }
    
    if (arg < -1) {
	rpmError(RPMERR_BADSPEC, "line %d: Bad option %s: %s",
		 spec->lineNum,
		 poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
		 spec->line);
	FREE(argv);
	poptFreeContext(optCon);
	return RPMERR_BADSPEC;
    }
    
    if (poptPeekArg(optCon)) {
	if (! name) {
	    name = poptGetArg(optCon);
	}
	if (poptPeekArg(optCon)) {
	    rpmError(RPMERR_BADSPEC, "line %d: Too many names: %s",
		     spec->lineNum,
		     spec->line);
	    FREE(argv);
	    poptFreeContext(optCon);
	    return RPMERR_BADSPEC;
	}
    }
    
    if (lookupPackage(spec, name, flag, &pkg)) {
	rpmError(RPMERR_BADSPEC, "line %d: Package does not exist: %s",
		 spec->lineNum, spec->line);
	FREE(argv);
	poptFreeContext(optCon);
	return RPMERR_BADSPEC;
    }

    if (headerIsEntry(pkg->header, progtag)) {
	rpmError(RPMERR_BADSPEC, "line %d: Second %s",
		 spec->lineNum, partname);
	FREE(argv);
	poptFreeContext(optCon);
	return RPMERR_BADSPEC;
    }

    if ((rc = poptParseArgvString(prog, &progArgc, &progArgv))) {
	rpmError(RPMERR_BADSPEC, "line %d: Error parsing %s: %s",
		 spec->lineNum, partname, poptStrerror(rc));
	FREE(argv);
	poptFreeContext(optCon);
	return RPMERR_BADSPEC;
    }
    
    sb = newStringBuf();
    if (readLine(spec, STRIP_NOTHING) > 0) {
	nextPart = PART_NONE;
    } else {
	while (! (nextPart = isPart(spec->line))) {
	    appendStringBuf(sb, spec->line);
	    if (readLine(spec, STRIP_NOTHING) > 0) {
		nextPart = PART_NONE;
		break;
	    }
	}
    }
    stripTrailingBlanksStringBuf(sb);
    p = getStringBuf(sb);

#if USE_PROG_STRING_ARRAY
    addReqProv(spec, pkg, RPMSENSE_PREREQ, progArgv[0], NULL);
    headerAddEntry(pkg->header, progtag, RPM_STRING_ARRAY_TYPE,
		   progArgv, progArgc);
#else
    addReqProv(spec, pkg, RPMSENSE_PREREQ, prog, NULL);
    headerAddEntry(pkg->header, progtag, RPM_STRING_TYPE, prog, 1);
#endif
    if (*p) {
	headerAddEntry(pkg->header, tag, RPM_STRING_TYPE, p, 1);
    }

    if (file) {
	switch (parsePart) {
	  case PART_PRE:
	    pkg->preInFile = strdup(file);
	    break;
	  case PART_POST:
	    pkg->postInFile = strdup(file);
	    break;
	  case PART_PREUN:
	    pkg->preUnFile = strdup(file);
	    break;
	  case PART_POSTUN:
	    pkg->postUnFile = strdup(file);
	    break;
	  case PART_VERIFYSCRIPT:
	    pkg->verifyFile = strdup(file);
	    break;
	}
    }
    
    freeStringBuf(sb);
    FREE(progArgv);
    FREE(argv);
    poptFreeContext(optCon);
    
    return nextPart;
}