summaryrefslogtreecommitdiff
path: root/build/read.c
blob: b5790fe3ae4c1428894e6ac75b857408a5f21c2c (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
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "spec.h"
#include "rpmlib.h"
#include "messages.h"
#include "macro.h"
#include "misc.h"

static int matchTok(char *token, char *line);

/* returns 0 - success */
/*         1 - EOF     */
/*        <0 - error   */

int readLine(Spec spec, int strip)
{
    char *from, *to, *last, *s, *arch, *os;
    int match;
    char ch;
    struct ReadLevelEntry *rl;

    /* Make sure the spec file is open */
    if (!spec->file) {
	if (!(spec->file = fopen(spec->specFile, "r"))) {
	    rpmError(RPMERR_BADSPEC, "Unable to open: %s\n", spec->specFile);
	    return RPMERR_BADSPEC;
	}
	spec->lineNum = 0;
    }

    /* Make sure we have something in the read buffer */
    if (!spec->readPtr || ! *(spec->readPtr)) {
	if (!fgets(spec->readBuf, BUFSIZ, spec->file)) {
	    /* EOF */
	    if (spec->readStack->next) {
		rpmError(RPMERR_UNMATCHEDIF, "Unclosed %%if");
	        return RPMERR_UNMATCHEDIF;
	    }
	    return 1;
	}
	spec->readPtr = spec->readBuf;
	spec->lineNum++;
	/*rpmMessage(RPMMESS_DEBUG, "LINE: %s", spec->readBuf);*/
    }
    
    /* Copy a single line to the line buffer */
    from = spec->readPtr;
    to = last = spec->line;
    ch = ' ';
    while (*from && ch != '\n') {
	ch = *to++ = *from++;
	if (!isspace(ch)) {
	    last = to;
	}
    }
    *to = '\0';
    spec->readPtr = from;
    
    if (strip) {
	*last = '\0';
    }

    rpmGetArchInfo(&arch, NULL);
    rpmGetOsInfo(&os, NULL);
    s = spec->line;
    SKIPSPACE(s);
    match = -1;
    if (! strncmp("%ifarch", s, 7)) {
	match = matchTok(arch, s);
    } else if (! strncmp("%ifnarch", s, 8)) {
	match = !matchTok(arch, s);
    } else if (! strncmp("%ifos", s, 5)) {
	match = matchTok(os, s);
    } else if (! strncmp("%ifnos", s, 6)) {
	match = !matchTok(os, s);
    } else if (! strncmp("%else", s, 5)) {
	if (! spec->readStack->next) {
	    /* Got an else with no %if ! */
	    rpmError(RPMERR_UNMATCHEDIF, "line %d: Got a %%else with no if",
		     spec->lineNum);
	    return RPMERR_UNMATCHEDIF;
	}
	spec->readStack->reading =
	    spec->readStack->next->reading && ! spec->readStack->reading;
	spec->line[0] = '\0';
    } else if (! strncmp("%endif", s, 6)) {
	if (! spec->readStack->next) {
	    /* Got an end with no %if ! */
	    rpmError(RPMERR_UNMATCHEDIF, "line %d: Got a %%endif with no if",
		     spec->lineNum);
	    return RPMERR_UNMATCHEDIF;
	}
	rl = spec->readStack;
	spec->readStack = spec->readStack->next;
	free(rl);
	spec->line[0] = '\0';
    }
    if (match != -1) {
	rl = malloc(sizeof(struct ReadLevelEntry));
	rl->reading = spec->readStack->reading && match;
	rl->next = spec->readStack;
	spec->readStack = rl;
	spec->line[0] = '\0';
    }
    
    if (spec->readStack->reading) {
	expandMacros(&spec->macros, spec->line);
    } else {
	spec->line[0] = '\0';
    }

    return 0;
}

static int matchTok(char *token, char *line)
{
    char buf[BUFSIZ], *tok;

    strcpy(buf, line);
    strtok(buf, " \n\t");
    while ((tok = strtok(NULL, " \n\t"))) {
	if (! strcmp(tok, token)) {
	    return 1;
	}
    }

    return 0;
}

void closeSpec(Spec spec)
{
    if (spec->file) {
	fclose(spec->file);
    }
    spec->file = NULL;
}