-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportcfg.js
312 lines (297 loc) · 7.55 KB
/
exportcfg.js
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// $Id: exportcfg.js,v 1.4 2019/02/15 11:11:07 rswindell Exp $
// vi: tabstop=4
// *****************************************************************
// Output a list of configuration items (e.g. msg areas, file areas)
// *****************************************************************
"use strict";
// global options (per list)
var options = {
hdr: false,
ini: false,
delim: '\t', // field delimiter
term: '', // record terminator
quote: false, // JS-encode strings
strip: false, // Strip Ctrl/Ctrl-A codes from strings
ascii: false, // Convert to ASCII
quiet: false,
sort: false,
json: false,
};
const defaults = JSON.parse(JSON.stringify(options));
// property options (per property)
var popts = {
upper: [], // uppercase
lower: [], // lowercase
under: [], // replace space with underscore
};
var props = [];
var propfmt = {}; // printf-style format string
var propex = []; // properties to exclude
var grp = [];
var include = []; // items to include
var exclude = []; // items to exclude
var cnflib = load({}, "cnflib.js");
var file_cnf = cnflib.read("file.cnf");
var cfgtype;
var cfgtypes = {
'msg-grps': msg_area.grp,
'msg-subs': msg_area.sub,
'file-libs': file_area.lib,
'file-dirs': file_area.dir,
'file-prots': file_cnf.prot,
'file-extrs': file_cnf.fextr,
'file-comps': file_cnf.fcomp,
'file-viewers': file_cnf.fview,
'file-testers': file_cnf.ftest,
'file-dlevents':file_cnf.dlevent,
'xtrn-secs': xtrn_area.sec,
'xtrn-progs': xtrn_area.prog,
'xtrn-events': xtrn_area.event,
'xtrn-editors': xtrn_area.editor,
};
function usage(msg)
{
if(msg) {
writeln();
alert(msg);
writeln();
}
writeln("usage: exportcfg.js <cfg-type>");
writeln("\t[[-grp=<msg_area.grp.name | file_area.lib.name | xtrn_area.sec.code>] [...]]");
writeln("\t[[-inc=<internal_code> | -exc=<internal_code>] [...]]");
writeln("\t[-<option>[=<value>] [...]]");
writeln("\t[[[property][=<printf-format> [-upper | -lower | -under]] [...]]");
writeln("\t[[-ex=<property>] [...]]");
writeln();
writeln("cfg-types (choose one):");
for(var c in cfgtypes)
writeln("\t" + c);
writeln();
writeln("options:");
writeln(format("\t%-12s <default>", "<option>"));
for(var o in defaults)
writeln(format("\t%-12s =%s", '-' + o, JSON.stringify(defaults[o])));
writeln("\t(tip: use -json=4 for pretty JSON output)");
writeln("\t(tip: use -ini='\\t%s = %s\\n' for pretty .ini output)");
writeln();
writeln("per-property options:");
writeln(format("\t%-12s <default>", "<option>"));
for(var o in popts)
writeln(format("\t%-12s =%s", '-' + o, popts[o].length !== undefined ? 'false' : ''));
exit(0);
}
for(var i = 0; i < argc; i++) {
var value = undefined;
var arg = argv[i];
var eq = arg.indexOf('=');
if(eq >= 0) {
if(eq < 1)
throw("invalid format: " + arg);
arg = arg.slice(0, eq);
value = argv[i].slice(eq + 1);
}
if(arg.charAt(0) != '-') {
if(cfgtype === undefined) {
if(cfgtypes[arg] === undefined)
usage("unsuppoted cfg-type: " + arg);
cfgtype = arg;
} else {
props.push(arg);
if(value !== undefined)
propfmt[arg] = JSON.parse('"' + value + '"');
}
continue;
}
while(arg.charAt(0) == '-')
arg = arg.slice(1);
switch(arg) {
case 'inc': // include item (by code)
if(value === undefined)
value = argv[++i];
include.push(value.toLowerCase());
continue;
case 'exc': // exclude item (by code)
if(value === undefined)
value = argv[++i];
exclude.push(value.toLowerCase());
continue;
case 'lib':
case 'grp':
case 'sec':
if(value === undefined)
value = argv[++i];
grp.push(value.toLowerCase());
continue;
case 'ex': // exclude property
if(value === undefined)
value = argv[++i];
propex.push(value);
continue;
case '?':
case 'help':
usage();
break;
default:
if(options[arg] === undefined && popts[arg] === undefined) {
usage('unrecognized option: ' + arg);
}
if(popts[arg] !== undefined) {
if(props.length < 1)
throw(argv[i] + " must follow a property specification");
popts[arg][props[props.length -1]] = true;
continue;
}
if(value !== undefined) {
if(typeof options[arg] == 'boolean') {
try {
value = eval(value);
} catch(err) {}
}
options[arg] = JSON.parse('"' + value + '"'); // support C-encoded chars (e.g. \t\r\n)
} else {
if(typeof options[arg] == 'string') {
options[arg] = JSON.parse('"' + argv[++i] + '"'); // support C-encoded chars (e.g. \t\r\n)
if(options[arg] === undefined)
throw("option value undefined: " + arg);
} else {
if(typeof options[arg] == 'boolean') {
try {
value = eval(argv[i + 1]);
} catch(err) {}
if(typeof value == 'boolean') {
options[arg] = value, i++;
continue;
}
}
var value = parseInt(argv[i + 1], 10);
if(value >= 0)
options[arg] = value, i++;
else
options[arg] = true;
}
}
continue;
}
}
if(!cfgtype)
usage("cfg-type not specified");
if(0) {
writeln("props");
writeln(JSON.stringify(props, null, 4));
writeln();
writeln("popts");
writeln(JSON.stringify(popts, null, 4));
writeln();
writeln("exclude");
writeln(JSON.stringify(exclude, null, 4));
writeln();
}
var cfglist = cfgtypes[cfgtype];
var output = [];
for(var i in cfglist) {
var item = cfglist[i];
if(grp.length) {
switch(cfgtype) {
case 'msg-subs':
if(grp.indexOf(item.grp_name.toLowerCase()) < 0)
continue;
break;
case 'file-dirs':
if(grp.indexOf(item.lib_name.toLowerCase()) < 0)
continue;
break;
case 'xtrn-progs':
if(grp.indexOf(item.sec_code.toLowerCase()) < 0)
continue;
break;
}
}
if(include.length && include.indexOf(i) < 0)
continue;
if(exclude.length && exclude.indexOf(i) >= 0)
continue;
var obj = {};
if(props.length == 0) {
obj = item; //JSON.parse(JSON.stringify(item));
if(obj.code === undefined)
obj.code = i;
}
else {
for(var f in props) {
var value = item[props[f]];
if(value === undefined && props[f] == 'code')
value = i;
if(typeof value == 'string') {
if(popts.under[props[f]])
value = value.replace(' ', '_');
if(popts.upper[props[f]])
value = value.toUpperCase();
else if(popts.lower[props[f]])
value = value.toLowerCase();
}
obj[props[f]] = value;
}
}
// Remove excluded properties
for(var e in propex)
delete obj[propex[e]];
for(var p in obj) {
if(typeof obj[p] == 'object')
delete obj[p];
}
var text = '';
if(options.json !== false)
text = JSON.stringify(obj, null, parseInt(options.json, 10));
else {
if(options.ini) {
var section = item.code;
if(!section)
section = item.name;
text += format("[%s]\n", section);
}
else if(options.hdr == true && !output.length) {
for(var f in obj) {
if(propfmt[f])
write(format(propfmt[f], f));
else
write(f);
write(options.delim);
}
writeln();
}
for(var f in obj) {
var value = obj[f];
if(value === undefined) {
if(props.length)
throw(f + ' is undefined');
continue;
}
if(options.strip)
value = strip_ctrl(value);
if(options.ascii)
value = ascii_str(value);
if(options.quote)
value = JSON.stringify(value);
if(propfmt[f])
value = format(propfmt[f], value);
if(options.ini)
text += format(typeof options.ini == 'string' ? options.ini : "%s = %s\n", f, value);
else {
if(text.length)
text += options.delim;
text += value;
}
}
if(options.term)
text += options.term;
}
if(js.global.bbs)
line = lfexpand(text);
output.push(text);
}
if(options.sort)
output.sort();
if(!options.quiet)
for(var i in output)
writeln(output[i]);
output;