mess822x 1.23
mess822x
Loading...
Searching...
No Matches
config.c
Go to the documentation of this file.
1#include "open.h"
2#include "readwrite.h"
3#include "buffer.h"
4#include "error.h"
5#include "getln.h"
6#include "stralloc.h"
7#include "config.h"
8#include "env.h"
9
11{
12 if (c->flagconf) return 0;
13 if (!s) return 0;
14 if (!stralloc_copys(&c->sa,s)) return -1;
15 c->flagconf = 1;
16 return 0;
17}
18
20{
21 if (c->flagconf) return 0;
22 if (!d->flagconf) return 0;
23 if (!stralloc_copy(&c->sa,&d->sa)) return -1;
24 c->flagconf = 1;
25 return 0;
26}
27
28int config_env(config_str *c,char *s)
29{
30 if (c->flagconf) return 0;
31 s = env_get(s);
32 if (!s) return 0;
33 if (!stralloc_copys(&c->sa,s)) return -1;
34 c->flagconf = 1;
35 return 0;
36}
37
38static void process(stralloc *sa)
39{
40 int i;
41
42 while (sa->len > 0)
43 switch(sa->s[sa->len - 1]) {
44 case '\n': case ' ': case '\t':
45 --sa->len; break;
46 default:
47 for (i = 0;i < sa->len;++i)
48 if (sa->s[i] == 0)
49 sa->s[i] = '\n';
50 return;
51 }
52}
53
54static char inbuf[128];
55static stralloc line = {0};
56
58{
59 buffer bi;
60 int fd;
61 int match;
62
63 if (c->flagconf) return 0;
64
65 fd = open_read(fn);
66 if (fd == -1) {
67 if (errno == error_noent) return 0;
68 return -1;
69 }
70 buffer_init(&bi,read,fd,inbuf,sizeof inbuf);
71
72 if (getln(&bi,&line,&match,'\n') == -1) {
73 close(fd);
74 return -1;
75 }
76 close(fd);
77
78 process(&line);
79 if (!stralloc_copy(&c->sa,&line)) return -1;
80 c->flagconf = 1;
81 return 0;
82}
83
85{
86 buffer bi;
87 int fd;
88 int match;
89
90 if (c->flagconf) return 0;
91
92 if (!stralloc_copys(&c->sa,"")) return -1;
93
94 fd = open_read(fn);
95 if (fd == -1) {
96 if (errno == error_noent) return 0;
97 return -1;
98 }
99 buffer_init(&bi,read,fd,inbuf,sizeof(inbuf));
100
101 for (;;) {
102 if (getln(&bi,&line,&match,'\n') == -1) { close(fd); return -1; }
103 process(&line);
104 if (!stralloc_0(&line)) { close(fd); return -1; }
105 if (line.s[0])
106 if (line.s[0] != '#')
107 if (!stralloc_cat(&c->sa,&line)) { close(fd); return -1; }
108 if (!match) break;
109 }
110 close(fd);
111
112 c->flagconf = 1;
113 return 0;
114}
int config_readline(config_str *c, char *fn)
Definition: config.c:57
int config_copy(config_str *c, config_str *d)
Definition: config.c:19
int config_env(config_str *c, char *s)
Definition: config.c:28
int config_default(config_str *c, char *s)
Definition: config.c:10
int config_readfile(config_str *c, char *fn)
Definition: config.c:84
buffer bi
Definition: install.c:43
int errno
stralloc line
Definition: 822body.c:9
int match
Definition: 822body.c:10
void d(char *, char *, int, int, int)
Definition: install.c:28
void c(char *, char *, char *, int, int, int)
Definition: install.c:46