File: configvar

package info (click to toggle)
roxen 1.2beta2-3
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 16,920 kB
  • ctags: 8,589
  • sloc: ansic: 89,632; asm: 8,431; sh: 2,915; makefile: 1,784; cpp: 377
file content (286 lines) | stat: -rwxr-xr-x 6,084 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/sh
# -*- Emacs, this is: lpc -*-
# This file use the built-in configuration file parsers in Roxen.
# It is not at all fast, but it _does_ work all the time, and it will
# continue to work even if the save-file format is changed in the future.

if [ ! -f base_server/roxen.pike ] ; then
  echo "This program must be run from the roxen/server directory"
  exit
fi

# Pike default Master-program
if [ -f lib/pike/master.pike ]; then
  DEFINES="$DEFINES -mlib/pike/master.pike"
else
  # This is used with localinstall
  if [ -f ../pike/src/lib/master.pike ]; then
    DEFINES="$DEFINES -m../pike/src/lib/master.pike"
  fi
fi

# Extra module-path
if [ -d etc/modules ]; then
  DEFINES="$DEFINES -Metc/modules"
fi

# Extra include-path
if [ -d etc/include ]; then
  DEFINES="$DEFINES -Ietc/include"
fi

# Extra include-path (2)
if [ -d base_server ]; then
  DEFINES="$DEFINES -Ibase_server"
fi

pike=pike
if [ -x bin/pike ] ; then pike=bin/pike; fi

cat > /tmp/roxen$$.pike  << "____________________"
#42 "configvar"
import Stdio;

void report_error(string s) 
{
  werror(s);
}

void roxen_perror(mixed ... args)
{
  werror(sprintf(@args));
}

object roxenp() 
{
  return this_object();
}

object|void open(string filename, string mode)
{
  object o;
  o=File();
  if(o->open(filename, mode))
    return o;
  destruct(o);
}

void mkdirhier(string from)
{
  string a, b;
  array f;

  f=(from/"/");
  b="";

  foreach(f[0..sizeof(f)-2], a)
  {
    mkdir(b+a);
    b+=a+"/";
  }
}

class fake_config_object
{
  string name;
  void create(string s)
  {
    name = s;
  }
};

#define IN_INSTALL
#define VAR_VALUE 0
#define BASE ""
#include "base_server/read_config.pike"

string find_arg(array argv, array|string shortform, 
		array|string|void longform, 
		array|string|void envvars, 
		string|void def)
{
  string value;
  int i;

  for(i=1; i<sizeof(argv); i++)
  {
    if(argv[i] && strlen(argv[i]) > 1)
    {
      if(argv[i][0] == '-')
      {
	if(argv[i][1] == '-')
	{
	  string tmp;
	  int nf;
	  if(!sscanf(argv[i], "%s=%s", tmp, value))
	  {
	    if(i < sizeof(argv)-1)
	      value = argv[i+1];
	    else
	      value = argv[i];
	    tmp = argv[i];
	    nf=1;
	  }
	  if(arrayp(longform) && search(longform, tmp[2..1000]) != -1)
	  {
	    argv[i] = 0;
	    if(i < sizeof(argv)-1)
	      argv[i+nf] = 0;
	    return value;
	  } else if(longform && longform == tmp[2..10000]) {
	    argv[i] = 0;
	    if(i < sizeof(argv)-1)
	      argv[i+nf] = 0;
	    return value;
	  }
	}
	if((arrayp(shortform) && ((search(shortform, argv[i][1..1]) != -1)))
	   || (stringp(shortform) && (shortform == argv[i][1..1])))
	{
	  if(strlen(argv[i]) == 2)
	  {
	    if(i < sizeof(argv)-1)
	      value =argv[i+1];
	    argv[i] = argv[i+1] = 0;
	    return value;
	  } else {
	    value=argv[i][2..100000];
	    argv[i]=0;
	    return value;
	  }
	}
      }
    }
  }

  if(arrayp(envvars))
    foreach(envvars, value)
      if(getenv(value))
	return getenv(value);
  
  if(stringp(envvars))
    if(getenv(envvars))
      return getenv(envvars);

  return def;
}

object current_configuration;

mixed my_decode(string from)
{
  return compile_string("mixed value = "+from+";", "Supplied value")()->value;
}

void main(int argc, string *argv)
{
  int query_only, quiet;
  string region;
  string file;

  add_constant("roxen", 0);
  add_constant("perror", roxen_perror);
  add_constant("roxen_perror", roxen_perror);
  

  if((argc <= 1) || find_arg(argv, "?", "help"))
  {
    write(sprintf("Syntax: roxenvar [flags] VAR[=VALUE] ...\n"
	  "Args are:\n"
" -n, --quiet              Just output the values of the variables.\n"
" -d, --config-dir=DIR     This is where the configuration files are\n"
"                          Default is '/etc/roxen/'.\n"
" -q, --query              Only query the variables, never ever set.\n"
" -r, --region=REG         Set the region of the configuration file\n"
"                          to search in. This is typicaly names of modules\n"
"                          e.g. 'redirect#0', or 'Variables' for the\n"
"                          global variables from the Global_Variables file.\n"
" -c, --configuration=CONF Set the name of the configurationfile to use.\n"
"                          The default value is 'Global_Variables'\n"
		  , argv[0]));
    exit(0);
  }

  if(find_arg(argv, "q", "query"))
    query_only = 1;


  if(find_arg(argv, "n", "quiet"))
    quiet = 1;


  region = find_arg(argv, "r", "region", 0, "Variables");
  file   = find_arg(argv, "c", "configuration", 0, "Global_Variables");

  current_configuration = fake_config_object( file );

  configuration_dir = find_arg(argv, "d", "config-dir", 0,"../configurations");

  if(configuration_dir[-1] != "/")
    configuration_dir += "/";
  argv -= ({ 0 });
  
  list_all_configurations();

  string tmp;
  mapping vars;
  int save_needed;

  vars = retrieve(region, current_configuration);

  foreach(argv[1..1000], tmp)
  {
    if(query_only)
    {
      if(!quiet) write(tmp+"=");
      if(!vars[tmp] && zero_type(vars[tmp]))
	write("UNDEF\n");
      else
	if(!stringp(vars[tmp]))
	  write(sprintf("%O\n", vars[tmp]));
	else
	  write(sprintf("\"%O\"\n", vars[tmp]));
    } else {
      string var, value;
      if(sscanf(tmp, "%s=%s", var, value) == 2)
      {
	if(value!="UNDEF")
	  vars[var] = my_decode(value);
	else
	  m_delete(vars, var);
	if(!quiet) 
	  if(!vars[var] && zero_type(vars[var]))
	    perror("Removing "+var+".\n");
	  else
	    perror("Setting "+var+" to "+ sprintf("%O", vars[var]) +"\n");
	save_needed = 1;
      } else {
	if(!quiet) 
	  write(tmp+"=");
	if(!vars[tmp] && zero_type(vars[tmp]))
	  write("UNDEF\n");
	else
	  if(!stringp(vars[tmp]))
	    write(sprintf("%O\n", vars[tmp]));
	  else
	    write(sprintf("\"%O\"\n", vars[tmp]));
      }
    }
  }
  if(save_needed)
  {
    if(!quiet) 
      perror("Saving file.\n");
    store(region, vars, 1, current_configuration);
  }
}




// The line below _must_ be there.. 
____________________

echo Starting $pike $DEFINES /tmp/roxen$$.pike "$@"...
$pike $DEFINES /tmp/roxen$$.pike "$@"
/bin/rm /tmp/roxen$$.pike