|
| 1 | +/* RPM - Copyright (C) 1995 Red Hat Software |
| 2 | + * |
| 3 | + * spec.c - routines for parsing a spec file |
| 4 | + */ |
| 5 | + |
| 6 | +#include "header.h" |
| 7 | +#include "spec.h" |
| 8 | +#include "rpmerr.h" |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#define LINE_BUF_SIZE 1024 |
| 13 | + |
| 14 | +struct spec { |
| 15 | +Header header; |
| 16 | +char *prep; |
| 17 | +char *build; |
| 18 | +char *install; |
| 19 | +char *clean; |
| 20 | +}; |
| 21 | + |
| 22 | +void free_spec(Spec s) |
| 23 | +{ |
| 24 | +freeHeader(s->header); |
| 25 | +free(s->prep); |
| 26 | +free(s->build); |
| 27 | +free(s->install); |
| 28 | +free(s->clean); |
| 29 | +free(s); |
| 30 | +} |
| 31 | + |
| 32 | +static int read_line(FILE *f, char *line); |
| 33 | +static int match_arch(char *s); |
| 34 | +static int match_os(char *s); |
| 35 | + |
| 36 | +static int match_arch(char *s) |
| 37 | +{ |
| 38 | +if (! strncmp(s, "%ifarch i386", 12)) { |
| 39 | +return 1; |
| 40 | + } else { |
| 41 | +return 0; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +static int match_os(char *s) |
| 46 | +{ |
| 47 | +if (! strncmp(s, "%ifos linux", 11)) { |
| 48 | +return 1; |
| 49 | + } else { |
| 50 | +return 0; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static int reading = 0; |
| 55 | +static int iflevels = 0; |
| 56 | +static int skiplevels = 0; |
| 57 | +static int skip = 0; |
| 58 | + |
| 59 | +static int read_line(FILE *f, char *line) |
| 60 | +{ |
| 61 | +char *r = fgets(line, LINE_BUF_SIZE, f); |
| 62 | + |
| 63 | +if (! r) { |
| 64 | +/* the end */ |
| 65 | +if (iflevels) return RPMERR_UNMATCHEDIF; |
| 66 | +return 0; |
| 67 | + } |
| 68 | + |
| 69 | +skip = 0; |
| 70 | + |
| 71 | +if (! strncmp("%ifarch", line, 7)) { |
| 72 | +iflevels++; |
| 73 | +skip = 1; |
| 74 | +if (! match_arch(line)) { |
| 75 | +reading = 0; |
| 76 | +skiplevels++; |
| 77 | + } |
| 78 | + } |
| 79 | +if (! strncmp("%ifos", line, 5)) { |
| 80 | +iflevels++; |
| 81 | +skip = 1; |
| 82 | +if (! match_os(line)) { |
| 83 | +reading = 0; |
| 84 | +skiplevels++; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +if (! strncmp("%endif", line, 6)) { |
| 89 | +iflevels--; |
| 90 | +skip = 1; |
| 91 | +if (skiplevels) { |
| 92 | +if (! --skiplevels) reading = 1; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +return 1; |
| 97 | +} |
| 98 | + |
| 99 | +Spec parse_spec(FILE *f) |
| 100 | +{ |
| 101 | +char line[LINE_BUF_SIZE]; |
| 102 | +Spec s = (struct spec *) malloc(sizeof(struct spec)); |
| 103 | +int x; |
| 104 | + |
| 105 | +reading = 1; |
| 106 | + |
| 107 | +while ((x = read_line(f, line))) { |
| 108 | +if (!reading) continue; |
| 109 | +if (skip) continue; |
| 110 | +puts(line); |
| 111 | + } |
| 112 | +if (x < 0) { |
| 113 | +/* error */ |
| 114 | +return NULL; |
| 115 | + } |
| 116 | + |
| 117 | +return s; |
| 118 | +} |