1
/* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* $Id: config.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $
20
constant cvs_version="$Id: config.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $";
24
#define METHOD_READLINE 0
26
static mapping vars = ([ ]);
27
private static string sContext;
29
Stdio.Readline readln = Stdio.Readline(Stdio.stdin);
36
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
39
mapping handle_options(array argv, mapping options)
41
mapping mOptions = ([ "options": ({ }) ]);
43
foreach ( argv, string arg ) {
45
if ( sscanf(arg, "--%s=%s", oname, oval) == 2 ) {
46
mOptions[oname] = oval;
48
else if ( sscanf(arg, "--%s", oname) == 1 )
50
else if ( sscanf(arg, "-%s", oname) == 1 ) {
51
if ( stringp(options[oname]) )
52
mOptions[options[oname]] = 1;
54
werror("Unknown Option: " + arg + "\n");
57
mOptions["options"] += ({ arg });
68
* @author Thomas Bopp (astra@upb.de)
71
mixed read_input(string desc, mixed def_value, void|mixed ... values)
78
if ( method == METHOD_GETS ) {
79
write(desc+" ["+def_value+"]: ");
80
str = Stdio.stdin.gets();
83
str = readln->read(desc + " ["+def_value+"]: ");
86
if ( !stringp(str) || strlen(str) == 0 ) {
91
if ( sscanf(str, "%d", value) != 1 || str != (string)value )
94
if ( stringp(def_value) && stringp(value) )
96
else if ( intp(def_value) && intp(value) )
98
if ( arrayp(values) && sizeof(values) > 0 &&
99
search(values, value) < 0 )
110
* @param fname - a file that should be detected at the path
112
* @author Thomas Bopp (astra@upb.de)
115
string read_path(string desc, string def_value, string fname)
122
server_path = read_input(desc, def_value);
123
if ( server_path[strlen(server_path)-1] != '/' )
126
f = Stdio.File(server_path + fname, "r");
141
* @author Thomas Bopp (astra@upb.de)
144
static mixed exchange_vars(Parser.HTML p, string data)
146
if ( stringp(data) && strlen(data) > 0 )
148
if ( data[0] == '$' &&
149
zero_type(vars[data[1..]]) != 1 ) {
150
data = (string)vars[data[1..]];
161
* @author Thomas Bopp (astra@upb.de)
164
void copy_config_file(string from, string to, object parser)
169
f = Stdio.File(from, "r");
172
parser->feed(content);
174
content = parser->read();
175
f = Stdio.File(to, "wct");
185
* @author Thomas Bopp (astra@upb.de)
188
private static int cb_context(Parser.HTML p, string tag)
190
tag = p->parse_tag_name(tag);
191
tag = tag[1..strlen(tag)-2];
192
sContext = replace(tag,":", "_");
201
* @author Thomas Bopp (astra@upb.de)
204
private static int read_config(Parser.HTML p, string data)
208
if ( sContext[0] == '/' ) return 0;
210
// empty config file ?
211
if ( stringp(data) && data[0] == '$' )
214
if ( sscanf(data, "%d", d) == 1 )
217
vars[sContext] = data;
221
static void read_configs(string fname)
225
if ( !Stdio.exist(fname) )
226
error("Configuration file " + fname + " not found.");
228
content = Stdio.read_file(fname);
229
object p = Parser.HTML();
230
p->_set_tag_callback(cb_context);
231
p->_set_data_callback(read_config);