summaryrefslogtreecommitdiff
path: root/librols/default.c
blob: 10878c33fbb64c91037e0ec338336bf05c06112b (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
/* 
 * Copyright 2006 Eduard Bloch 
 *
 * Uses my config parser code and small wrappers to provide the old interface.
 *
 */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; see the file COPYING.  If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <mconfig.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

enum parstate {
	KEYBEGINSEARCH,
	KEYCOMPARE,
	EQSIGNSEARCH,
	VALBEGINSEARCH,
	LASTCHARSEARCH
};

#define GETVAL_BUF_LEN 512
#define isUspace(x) isspace( (int) (unsigned char) x)

static FILE *glob_cfg_ptr = NULL;

/*
 * Warning, uses static line buffer, not reentrant. NULL returned if the key isn't found.
 */
static char *get_value(FILE *srcfile, const char *key, int dorewind) {
	static char linebuf[GETVAL_BUF_LEN];

	if(!srcfile)
		return ((char *) NULL);

	if(dorewind)
		rewind(srcfile);

	if(!key)
		return NULL;

next_line:
	while(fgets(linebuf, sizeof(linebuf)-1, srcfile)) {
		int i;
		int keybeg=0;
		int s=KEYBEGINSEARCH;
		char *ret=NULL;
		int lastchar=0;

		/* simple state machine, char position moved by the states (or not),
		 * state change is done by the state (or not) */
		for( i=0 ; i<sizeof(linebuf) ; ) {
			/* printf("key: %s, %s, s: %d\n", key,  linebuf, s); */
			switch(s) {
				case(KEYBEGINSEARCH):
					{
						if(isUspace(linebuf[i]))
							i++;
						else if(linebuf[i] == '#' || linebuf[i]=='\0')
							goto next_line;
						else {
							s=KEYCOMPARE;
							keybeg=i;
						}
					}
					break;
				case(KEYCOMPARE): /* compare the key */
					{
						if(key[i-keybeg]=='\0') 
							/* end of key, next state decides what to do on this position */
							s=EQSIGNSEARCH;
						else {
							if(linebuf[i-keybeg]!=key[i-keybeg])
								goto next_line;
							else
								i++;
						}
					}
					break;
				case(EQSIGNSEARCH): /* skip whitespace, stop on =, break on anything else */
					{
						if(isUspace(linebuf[i]))
							i++;
						else if(linebuf[i]=='=') {
							s=VALBEGINSEARCH;
							i++;
						}
						else
							goto next_line;
					}
					break;
				case(VALBEGINSEARCH):
					{
						if(isUspace(linebuf[i]))
							i++;
						else {
							/* possible at EOF */
							if(linebuf[i] == '\0')
								return NULL;

							lastchar=i-1; /* lastchar can be a space, see below */
							ret= & linebuf[i];
							s=LASTCHARSEARCH;
						}
					}
					break;
				case(LASTCHARSEARCH):
					{
						if(linebuf[i]) {
							if(!isUspace(linebuf[i]))
								lastchar=i;
						}
						else { /* got string end, terminate after the last seen char */
							if(linebuf+lastchar < ret) /* no non-space found */
								return NULL;
							linebuf[lastchar+1]='\0';
							return ret;
						}
						i++;
					}
					break;
			}
		}
	}
	return NULL;
}

int cfg_open(const char *name)
{
	if(glob_cfg_ptr) {
		fclose(glob_cfg_ptr);
		glob_cfg_ptr=NULL;
	}
	if(!name) {
		glob_cfg_ptr=NULL;
		return 0;
	}
	glob_cfg_ptr = fopen(name, "r");
	return (glob_cfg_ptr ? 0 : -1);
}

int cfg_close()
{
	int r;
	if(!glob_cfg_ptr)
		return 0;
	r=fclose(glob_cfg_ptr);
	glob_cfg_ptr=NULL;
	return r;
}

void cfg_restart()
{
	get_value(glob_cfg_ptr, NULL, 1);
}

char *cfg_get(const char *key)
{
	return get_value(glob_cfg_ptr, key, 1);
}

char *cfg_get_next(const char *key)
{
	return get_value(glob_cfg_ptr, key, 0);
}