]>
git.cameronkatri.com Git - cgit.git/blob - parsing.c
74a248449f35fb2a5331f571d3bea30a2806acab
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
)
73 /* cancel deeply nested include-commands */
76 if (!(f
= fopen(filename
, "r")))
79 while((len
= read_config_line(f
, line
, &value
, sizeof(line
))) > 0)
86 char *convert_query_hexchar(char *txt
)
89 if (strlen(txt
) < 3) {
93 d1
= hextoint(*(txt
+1));
94 d2
= hextoint(*(txt
+2));
100 strcpy(txt
+1, txt
+3);
105 int cgit_parse_query(char *txt
, configfn fn
)
107 char *t
, *value
= NULL
, c
;
112 t
= txt
= xstrdup(txt
);
114 while((c
=*t
) != '\0') {
121 t
= convert_query_hexchar(t
);
136 * url syntax: [repo ['/' cmd [ '/' path]]]
137 * repo: any valid repo url, may contain '/'
138 * cmd: log | commit | diff | tree | view | blob | snapshot
139 * path: any valid path, may contain '/'
142 void cgit_parse_url(const char *url
)
147 if (!url
|| url
[0] == '\0')
150 cgit_repo
= cgit_get_repoinfo(url
);
152 cgit_query_repo
= cgit_repo
->url
;
156 cmd
= strchr(url
, '/');
157 while (!cgit_repo
&& cmd
) {
159 cgit_repo
= cgit_get_repoinfo(url
);
160 if (cgit_repo
== NULL
) {
162 cmd
= strchr(cmd
+ 1, '/');
166 cgit_query_repo
= cgit_repo
->url
;
167 p
= strchr(cmd
+ 1, '/');
171 cgit_query_path
= xstrdup(p
+ 1);
173 cgit_cmd
= cgit_get_cmd_index(cmd
+ 1);
174 cgit_query_page
= xstrdup(cmd
+ 1);
179 char *substr(const char *head
, const char *tail
)
183 buf
= xmalloc(tail
- head
+ 1);
184 strncpy(buf
, head
, tail
- head
);
185 buf
[tail
- head
] = '\0';
189 struct commitinfo
*cgit_parse_commit(struct commit
*commit
)
191 struct commitinfo
*ret
;
192 char *p
= commit
->buffer
, *t
= commit
->buffer
;
194 ret
= xmalloc(sizeof(*ret
));
195 ret
->commit
= commit
;
197 ret
->author_email
= NULL
;
198 ret
->committer
= NULL
;
199 ret
->committer_email
= NULL
;
206 if (strncmp(p
, "tree ", 5))
207 die("Bad commit: %s", sha1_to_hex(commit
->object
.sha1
));
209 p
+= 46; // "tree " + hex[40] + "\n"
211 while (!strncmp(p
, "parent ", 7))
212 p
+= 48; // "parent " + hex[40] + "\n"
214 if (!strncmp(p
, "author ", 7)) {
216 t
= strchr(p
, '<') - 1;
217 ret
->author
= substr(p
, t
);
219 t
= strchr(t
, '>') + 1;
220 ret
->author_email
= substr(p
, t
);
221 ret
->author_date
= atol(++t
);
222 p
= strchr(t
, '\n') + 1;
225 if (!strncmp(p
, "committer ", 9)) {
227 t
= strchr(p
, '<') - 1;
228 ret
->committer
= substr(p
, t
);
230 t
= strchr(t
, '>') + 1;
231 ret
->committer_email
= substr(p
, t
);
232 ret
->committer_date
= atol(++t
);
233 p
= strchr(t
, '\n') + 1;
237 p
= strchr(p
, '\n') + 1;
242 ret
->subject
= strdup("** empty **");
244 ret
->subject
= substr(p
, t
);
248 p
= strchr(p
, '\n') + 1;
251 ret
->subject
= substr(p
, p
+strlen(p
));
257 struct taginfo
*cgit_parse_tag(struct tag
*tag
)
260 enum object_type type
;
265 data
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
266 if (!data
|| type
!= OBJ_TAG
) {
271 ret
= xmalloc(sizeof(*ret
));
273 ret
->tagger_email
= NULL
;
274 ret
->tagger_date
= 0;
283 if (!strncmp(p
, "tagger ", 7)) {
285 t
= strchr(p
, '<') - 1;
286 ret
->tagger
= substr(p
, t
);
288 t
= strchr(t
, '>') + 1;
289 ret
->tagger_email
= substr(p
, t
);
290 ret
->tagger_date
= atol(++t
);
292 p
= strchr(p
, '\n') + 1;
295 while (p
&& (*p
== '\n'))
296 p
= strchr(p
, '\n') + 1;
298 ret
->msg
= xstrdup(p
);