summaryrefslogtreecommitdiffstats
path: root/conf.c
blob: 09889f6738aa7d9f59c51aaa32ba1ddc70c3919e (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
200
201
202
203
204
205
206
#include <ctype.h>
#include <errno.h>
#include <glob.h>
#include <string.h>
#include <stdlib.h>

#include "conf.h"
#include "util.h"

static size_t strtrim(char *str)
{
  char *left = str, *right;

  if(!str || *str == '\0') {
    return 0;
  }

  while(isspace((unsigned char)*left)) {
    left++;
  }
  if(left != str) {
    memmove(str, left, (strlen(left) + 1));
    left = str;
  }

  if(*str == '\0') {
    return 0;
  }

  right = strchr(str, '\0') - 1;
  while(isspace((unsigned char)*right)) {
    right--;
  }
  *++right = '\0';

  return right - left;
}

static int is_section(const char *s, int n)
{
  return s[0] == '[' && s[n-1] == ']';
}

static int config_add_repo(config_t *config, char *reponame)
{
  /* first time setup */
  if(config->repos == NULL) {
    config->repos = calloc(10, sizeof(char*));
    if(config->repos == NULL) {
      return -ENOMEM;
    }

    config->size = 0;
    config->capacity = 10;
  }

  /* grow when needed */
  if(config->size == config->capacity) {
    void *ptr;

    ptr = realloc(config->repos, config->capacity * 2.5 * sizeof(char*));
    if(ptr == NULL) {
      return -ENOMEM;
    }

    config->repos = ptr;
  }

  config->repos[config->size] = strdup(reponame);
  ++config->size;

  return 0;
}

static int parse_one_file(config_t *config, const char *filename, char **section);

static int parse_include(config_t *config, const char *include, char **section) {
  glob_t globbuf;
  int r = 0;

  if(glob(include, GLOB_NOCHECK, NULL, &globbuf) != 0) {
    fprintf(stderr, "warning: globbing failed on '%s': out of memory\n",
            include);
    return -ENOMEM;
  }

  for(size_t i = 0; i < globbuf.gl_pathc; ++i) {
    r = parse_one_file(config, globbuf.gl_pathv[i], section);
    if(r < 0) {
      break;
    }
  }

  globfree(&globbuf);
  return r;
}

static int parse_one_file(config_t *config, const char *filename, char **section)
{
  _cleanup_(fclosep) FILE *fp = NULL;
  _cleanup_free_ char *line = NULL;
  size_t n = 0;
  int in_options = 0;

  if(*section) {
    in_options = strcmp(*section, "options") == 0;
  }

  fp = fopen(filename, "r");
  if(fp == NULL) {
    return -errno;
  }

  for(;;) {
    ssize_t len;

    errno = 0;
    len = getline(&line, &n, fp);
    if(len < 0) {
      if(errno != 0) {
        return -errno;
      }

      /* EOF */
      break;
    }

    len = strtrim(line);
    if(len == 0 || line[0] == '#') {
      continue;
    }

    if(is_section(line, len)) {
      free(*section);
      *section = strndup(&line[1], len - 2);
      if(*section == NULL) {
        return -ENOMEM;
      }

      in_options = strcmp(*section, "options") == 0;
      if(!in_options) {
        int r;

        r = config_add_repo(config, *section);
        if(r < 0) {
          return r;
        }

      }
      continue;
    }

    if(in_options && memchr(line, '=', len)) {
      char *val = line;

      strsep(&val, "=");
      strtrim(line);
      strtrim(val);

      if(strcmp(line, "Include") == 0) {
        int k;

        k = parse_include(config, val, section);
        if(k < 0) {
          return k;
        }
      } else if(strcmp(line, "DBPath") == 0) {
        config->dbpath = strdup(val);
        if(config->dbpath == NULL) {
          return -ENOMEM;
        }
      } else if(strcmp(line, "RootDir") == 0) {
        config->dbroot = strdup(val);
        if(config->dbpath == NULL) {
          return -ENOMEM;
        }
      }
    }
  }

  return 0;
}

void config_reset(config_t *config)
{
  if(config == NULL) {
    return;
  }

  for(int i = 0; i < config->size; ++i) {
    free(config->repos[i]);
  }

  free(config->dbroot);
  free(config->dbpath);
  free(config->repos);
}

int config_parse(config_t *config, const char *filename)
{
  _cleanup_free_ char *section = NULL;

  return parse_one_file(config, filename, &section);
}

/* vim: set et ts=2 sw=2: */