about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLars Hjemli2008-03-28 00:09:11 +0100
committerLars Hjemli2008-03-28 00:09:11 +0100
commit20a33548b9a87a6eb23162ee5d137daa46d78613 (patch)
tree9c4ca364df64dcce640a531c7f515ee48bc99387
parentAdd cache.h (diff)
downloadcgit-20a33548b9a87a6eb23162ee5d137daa46d78613.tar.gz
cgit-20a33548b9a87a6eb23162ee5d137daa46d78613.zip
Move function for configfile parsing into configfile.[ch]
This is a generic function which wanted its own little object file.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r--Makefile1
-rw-r--r--cgit.c5
-rw-r--r--cgit.h1
-rw-r--r--configfile.c87
-rw-r--r--configfile.h8
-rw-r--r--parsing.c75
6 files changed, 99 insertions, 78 deletions
diff --git a/Makefile b/Makefile index 223765d..355186e 100644 --- a/Makefile +++ b/Makefile
@@ -51,6 +51,7 @@ OBJECTS =
51OBJECTS += cache.o 51OBJECTS += cache.o
52OBJECTS += cgit.o 52OBJECTS += cgit.o
53OBJECTS += cmd.o 53OBJECTS += cmd.o
54OBJECTS += configfile.o
54OBJECTS += html.o 55OBJECTS += html.o
55OBJECTS += parsing.o 56OBJECTS += parsing.o
56OBJECTS += shared.o 57OBJECTS += shared.o
diff --git a/cgit.c b/cgit.c index 73b1f02..1f46e0d 100644 --- a/cgit.c +++ b/cgit.c
@@ -9,6 +9,7 @@
9#include "cgit.h" 9#include "cgit.h"
10#include "cache.h" 10#include "cache.h"
11#include "cmd.h" 11#include "cmd.h"
12#include "configfile.h"
12#include "ui-shared.h" 13#include "ui-shared.h"
13 14
14const char *cgit_version = CGIT_VERSION; 15const char *cgit_version = CGIT_VERSION;
@@ -103,7 +104,7 @@ void config_cb(const char *name, const char *value)
103 else 104 else
104 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 105 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
105 } else if (!strcmp(name, "include")) 106 } else if (!strcmp(name, "include"))
106 cgit_read_config(value, config_cb); 107 parse_configfile(value, config_cb);
107} 108}
108 109
109static void querystring_cb(const char *name, const char *value) 110static void querystring_cb(const char *name, const char *value)
@@ -436,7 +437,7 @@ int main(int argc, const char **argv)
436 cgit_repolist.count = 0; 437 cgit_repolist.count = 0;
437 cgit_repolist.repos = NULL; 438 cgit_repolist.repos = NULL;
438 439
439 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 440 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
440 config_cb); 441 config_cb);
441 if (getenv("SCRIPT_NAME")) 442 if (getenv("SCRIPT_NAME"))
442 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 443 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
diff --git a/cgit.h b/cgit.h index f600912..91d18f8 100644 --- a/cgit.h +++ b/cgit.h
@@ -214,7 +214,6 @@ extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
214 214
215extern char *fmt(const char *format,...); 215extern char *fmt(const char *format,...);
216 216
217extern int cgit_read_config(const char *filename, configfn fn);
218extern int cgit_parse_query(char *txt, configfn fn); 217extern int cgit_parse_query(char *txt, configfn fn);
219extern struct commitinfo *cgit_parse_commit(struct commit *commit); 218extern struct commitinfo *cgit_parse_commit(struct commit *commit);
220extern struct taginfo *cgit_parse_tag(struct tag *tag); 219extern struct taginfo *cgit_parse_tag(struct tag *tag);
diff --git a/configfile.c b/configfile.c new file mode 100644 index 0000000..4908058 --- /dev/null +++ b/configfile.c
@@ -0,0 +1,87 @@
1/* configfile.c: parsing of config files
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include <ctype.h>
10#include <stdio.h>
11#include "configfile.h"
12
13int next_char(FILE *f)
14{
15 int c = fgetc(f);
16 if (c=='\r') {
17 c = fgetc(f);
18 if (c!='\n') {
19 ungetc(c, f);
20 c = '\r';
21 }
22 }
23 return c;
24}
25
26void skip_line(FILE *f)
27{
28 int c;
29
30 while((c=next_char(f)) && c!='\n' && c!=EOF)
31 ;
32}
33
34int read_config_line(FILE *f, char *line, const char **value, int bufsize)
35{
36 int i = 0, isname = 0;
37
38 *value = NULL;
39 while(i<bufsize-1) {
40 int c = next_char(f);
41 if (!isname && (c=='#' || c==';')) {
42 skip_line(f);
43 continue;
44 }
45 if (!isname && isspace(c))
46 continue;
47
48 if (c=='=' && !*value) {
49 line[i] = 0;
50 *value = &line[i+1];
51 } else if (c=='\n' && !isname) {
52 i = 0;
53 continue;
54 } else if (c=='\n' || c==EOF) {
55 line[i] = 0;
56 break;
57 } else {
58 line[i]=c;
59 }
60 isname = 1;
61 i++;
62 }
63 line[i+1] = 0;
64 return i;
65}
66
67int parse_configfile(const char *filename, configfile_value_fn fn)
68{
69 static int nesting;
70 int len;
71 char line[256];
72 const char *value;
73 FILE *f;
74
75 /* cancel deeply nested include-commands */
76 if (nesting > 8)
77 return -1;
78 if (!(f = fopen(filename, "r")))
79 return -1;
80 nesting++;
81 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
82 fn(line, value);
83 nesting--;
84 fclose(f);
85 return 0;
86}
87
diff --git a/configfile.h b/configfile.h new file mode 100644 index 0000000..04235e5 --- /dev/null +++ b/configfile.h
@@ -0,0 +1,8 @@
1#ifndef CONFIGFILE_H
2#define CONFIGFILE_H
3
4typedef void (*configfile_value_fn)(const char *name, const char *value);
5
6extern int parse_configfile(const char *filename, configfile_value_fn fn);
7
8#endif /* CONFIGFILE_H */
diff --git a/parsing.c b/parsing.c index 8dce488..9a4a7a3 100644 --- a/parsing.c +++ b/parsing.c
@@ -8,81 +8,6 @@
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11int next_char(FILE *f)
12{
13 int c = fgetc(f);
14 if (c=='\r') {
15 c = fgetc(f);
16 if (c!='\n') {
17 ungetc(c, f);
18 c = '\r';
19 }
20 }
21 return c;
22}
23
24void skip_line(FILE *f)
25{
26 int c;
27
28 while((c=next_char(f)) && c!='\n' && c!=EOF)
29 ;
30}
31
32int read_config_line(FILE *f, char *line, const char **value, int bufsize)
33{
34 int i = 0, isname = 0;
35
36 *value = NULL;
37 while(i<bufsize-1) {
38 int c = next_char(f);
39 if (!isname && (c=='#' || c==';')) {
40 skip_line(f);
41 continue;
42 }
43 if (!isname && isspace(c))
44 continue;
45
46 if (c=='=' && !*value) {
47 line[i] = 0;
48 *value = &line[i+1];
49 } else if (c=='\n' && !isname) {
50 i = 0;
51 continue;
52 } else if (c=='\n' || c==EOF) {
53 line[i] = 0;
54 break;
55 } else {
56 line[i]=c;
57 }
58 isname = 1;
59 i++;
60 }
61 line[i+1] = 0;
62 return i;
63}
64
65int cgit_read_config(const char *filename, configfn fn)
66{
67 static int nesting;
68 int len;
69 char line[256];
70 const char *value;
71 FILE *f;
72
73 /* cancel deeply nested include-commands */
74 if (nesting > 8)
75 return -1;
76 if (!(f = fopen(filename, "r")))
77 return -1;
78 nesting++;
79 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
80 (*fn)(line, value);
81 nesting--;
82 fclose(f);
83 return 0;
84}
85
86char *convert_query_hexchar(char *txt) 11char *convert_query_hexchar(char *txt)
87{ 12{
88 int d1, d2; 13 int d1, d2;