]>
git.cameronkatri.com Git - cgit.git/blob - parsing.c
1 /* config.c: parsing of config files
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 int next_char(FILE *f
)
24 void skip_line(FILE *f
)
28 while((c
=next_char(f
)) && c
!='\n' && c
!=EOF
)
32 int read_config_line(FILE *f
, char *line
, const char **value
, int bufsize
)
34 int i
= 0, isname
= 0;
39 if (!isname
&& (c
=='#' || c
==';')) {
43 if (!isname
&& isspace(c
))
46 if (c
=='=' && !*value
) {
49 } else if (c
=='\n' && !isname
) {
52 } else if (c
=='\n' || c
==EOF
) {
65 int cgit_read_config(const char *filename
, configfn fn
)
70 FILE *f
= fopen(filename
, "r");
75 while((len
= read_config_line(f
, line
, &value
, sizeof(line
))) > 0)
82 char *convert_query_hexchar(char *txt
)
85 if (strlen(txt
) < 3) {
89 d1
= hextoint(*(txt
+1));
90 d2
= hextoint(*(txt
+2));
101 int cgit_parse_query(char *txt
, configfn fn
)
103 char *t
, *value
= NULL
, c
;
108 t
= txt
= xstrdup(txt
);
110 while((c
=*t
) != '\0') {
117 t
= convert_query_hexchar(t
);
131 char *substr(const char *head
, const char *tail
)
135 buf
= xmalloc(tail
- head
+ 1);
136 strncpy(buf
, head
, tail
- head
);
137 buf
[tail
- head
] = '\0';
141 struct commitinfo
*cgit_parse_commit(struct commit
*commit
)
143 struct commitinfo
*ret
;
144 char *p
= commit
->buffer
, *t
= commit
->buffer
;
146 ret
= xmalloc(sizeof(*ret
));
147 ret
->commit
= commit
;
149 ret
->author_email
= NULL
;
150 ret
->committer
= NULL
;
151 ret
->committer_email
= NULL
;
155 if (strncmp(p
, "tree ", 5))
156 die("Bad commit: %s", sha1_to_hex(commit
->object
.sha1
));
158 p
+= 46; // "tree " + hex[40] + "\n"
160 while (!strncmp(p
, "parent ", 7))
161 p
+= 48; // "parent " + hex[40] + "\n"
163 if (!strncmp(p
, "author ", 7)) {
165 t
= strchr(p
, '<') - 1;
166 ret
->author
= substr(p
, t
);
168 t
= strchr(t
, '>') + 1;
169 ret
->author_email
= substr(p
, t
);
170 ret
->author_date
= atol(++t
);
171 p
= strchr(t
, '\n') + 1;
174 if (!strncmp(p
, "committer ", 9)) {
176 t
= strchr(p
, '<') - 1;
177 ret
->committer
= substr(p
, t
);
179 t
= strchr(t
, '>') + 1;
180 ret
->committer_email
= substr(p
, t
);
181 ret
->committer_date
= atol(++t
);
182 p
= strchr(t
, '\n') + 1;
186 p
= strchr(p
, '\n') + 1;
190 ret
->subject
= substr(p
, t
);
194 p
= strchr(p
, '\n') + 1;
201 struct taginfo
*cgit_parse_tag(struct tag
*tag
)
209 data
= read_sha1_file(tag
->object
.sha1
, type
, &size
);
210 if (!data
|| strcmp(type
, tag_type
)) {
215 ret
= xmalloc(sizeof(*ret
));
217 ret
->tagger_email
= NULL
;
218 ret
->tagger_date
= 0;
227 if (!strncmp(p
, "tagger ", 7)) {
229 t
= strchr(p
, '<') - 1;
230 ret
->tagger
= substr(p
, t
);
232 t
= strchr(t
, '>') + 1;
233 ret
->tagger_email
= substr(p
, t
);
234 ret
->tagger_date
= atol(++t
);
236 p
= strchr(p
, '\n') + 1;
239 while (p
&& (*p
== '\n'))
240 p
= strchr(p
, '\n') + 1;
242 ret
->msg
= xstrdup(p
);