-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
398 lines (355 loc) · 13.6 KB
/
config.cpp
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//*****************************************************************
// config.cpp - NDIR config file handling
//
// Written by: Daniel D. Miller (the Derelict)
//
//*****************************************************************
#include <windows.h>
#include <stdio.h>
#ifdef _lint
#include <stdlib.h>
#endif
#include <ctype.h>
#include "common.h"
#include "ndir32.h"
#define MAX_EXT 200
struct attrib_list {
uchar attr ;
TCHAR ext[MAX_EXT_SIZE+1] ;
} ;
static attrib_list attr_table[MAX_EXT] ;
static unsigned attrib_count = 0 ;
//#########################################################################
// INI-file handlers
//#########################################################################
//*********************************************************************
// default file colors
static attrib_list const attr_default_list[] = {
{ 0x02, _T(".1ST") }, { 0x0E, _T(".ARC") }, { 0x03, _T(".ASM") }, { 0x03, _T(".BAS") },
{ 0x04, _T(".BAT") }, { 0x03, _T(".C") }, { 0x0C, _T(".COM") }, { 0x03, _T(".CPP") },
{ 0x02, _T(".DOC") }, { 0x0C, _T(".EXE") }, { 0x0D, _T(".H") }, { 0x0D, _T(".HEX") },
{ 0x03, _T(".HPP") }, { 0x0D, _T(".INC") }, { 0x0D, _T(".LIB") }, { 0x0A, _T(".LST") },
{ 0x05, _T(".MAP") }, { 0x02, _T(".ME") }, { 0x02, _T(".NOW") }, { 0x0D, _T(".OBJ") },
{ 0x03, _T(".PAS") }, { 0x02, _T(".TXT") }, { 0, _T("") }
} ;
// default directory colors
static uchar const dir_default_list[MAX_DIR_ENTRY] = {
3, 4, 5, 6, 7, 8, 9, 0x0A,
3, 4, 5, 6, 7, 8, 9, 0x0A,
3, 4, 5, 6 } ;
//***************************************************************
void getcolor(ffdata *fnew)
{
unsigned j;
attrib_list *aptr;
for (j = 0; j < attrib_count; j++) {
aptr = &attr_table[j];
if (strcmpiwc (fnew->ext, aptr->ext) != 0) {
fnew->color = aptr->attr;
return;
}
}
fnew->color = n.colordefalt; // if not found, assign default color
} //lint !e429 Custodial pointer 'fnew' has not been freed or returned
//*********************************************************************
static void parse_color_entry(TCHAR *iniptr)
{
char *eqptr ;
char *hdptr ;
char *tlptr ;
uchar atr ;
attrib_list *aptr ;
// check for multiple-color-entry forms...
//0x32:.com,.bat,.btm,.sys
//14:.arc,.tgz,.tar,.gz,.z,.zip,.bz2,.rar,.7z,.iso,.zcp
hdptr = _tcschr(iniptr, ':') ;
if (hdptr != 0) {
*hdptr++ = 0 ; // terminate attribute, point to first extension
atr = (uchar) _tcstoul(iniptr, 0, 0) ;
if (atr == 0)
return ;
while (LOOP_FOREVER) {
// make sure we don't overrun our table
if (attrib_count >= MAX_EXT)
return ;
// if (*hdptr != '.')
// return ;
tlptr = _tcschr(hdptr, ',') ;
// see if we're at end of line
if (tlptr != 0) {
*tlptr++ = 0 ; // NULL-term extension
}
// check for too-long extensions in INI file
// If extension in INI file is too long, just discard it
uint extlen = (*hdptr == '.') ? MAX_EXT_SIZE : MAX_EXT_SIZE-1 ;
if (_tcslen(hdptr) <= extlen) {
aptr = &attr_table[attrib_count++] ;
if (*hdptr == '.') {
_tcscpy(aptr->ext, hdptr) ;
}
else {
sprintf(aptr->ext, ".%s", hdptr) ;
}
aptr->attr = atr ;
}
if (tlptr == 0) {
break;
}
hdptr = tlptr ;
}
}
// handle single-color entries
//.FAQ=2
else {
// make sure we don't overrun our table
if (attrib_count >= MAX_EXT)
return ;
eqptr = _tcschr(iniptr, '=') ;
if (eqptr == 0)
return ;
*eqptr++ = 0 ; // NULL-terminate lvalue, point to rvalue
//.ARC=14
aptr = &attr_table[attrib_count++] ;
_tcsncpy(aptr->ext, iniptr, MAX_EXT_SIZE) ;
aptr->attr = (uchar) _tcstoul(eqptr, 0, 0) ;
}
}
//*********************************************************************
static void parse_dir_color_entry(TCHAR *iniptr)
{
static int dcIdx = 0 ;
iniptr++ ; // skip colon flag
if (dcIdx < MAX_DIR_ENTRY) {
dtree_colors[dcIdx++] = (uchar) _tcstoul(iniptr, 0, 0) ;
}
}
//*********************************************************************
static int write_default_ini_file(TCHAR *ini_str)
{
FILE *ofile ;
int j ;
ofile = _tfopen(ini_str, _T("wt")) ;
if (ofile == 0)
return errno ;
fprintf(ofile, "; Default configuration file for NDIR32.EXE\n") ;
fprintf(ofile, "; This file was generated automatically by NDIR, \n") ;
fprintf(ofile, "; but it will not be over-written. \n") ;
fprintf(ofile, "; Edit this file as you desire to re-configure NDIR.\n") ;
fprintf(ofile, "; \n") ;
fprintf(ofile, "; Lines beginning with semicolons, and blank lines, are ignored\n") ;
fprintf(ofile, "\n") ;
// generate state flags
fprintf(ofile, "ucase=%u ; use upper-case for all names\n", n.ucase) ;
fprintf(ofile, "sort=%u ; 0=ext, 1=name, 2=size, 3=date/time, 4=none\n", n.sort) ;
fprintf(ofile, "reverse=%u ; reverse normal sort order\n", n.reverse) ;
fprintf(ofile, "clear=%u ; clear screen before display\n", n.clear) ;
fprintf(ofile, "pause=%u ; pause on full screen\n", n.pause) ;
fprintf(ofile, "format=%u ; output columns, 0=1, 1=2, 2=4, 3=6, 5=3\n", n.format) ;
fprintf(ofile, "minimize=%u ; minimize header/footer\n", n.minimize) ;
fprintf(ofile, "show_all=%u ; show S/H/R files\n", n.show_all) ;
fprintf(ofile, "dir_first=%u ; list directories only\n", n.dir_first) ;
fprintf(ofile, "exec_only=%u ; show executables only (exe,com,bat,btm)\n", n.exec_only) ;
fprintf(ofile, "long_attr=%u ; show executables only (exe,com,bat,btm)\n", n.long_attr) ;
fprintf(ofile, "horz=%u ; list files horizontally\n", n.horz) ;
fprintf(ofile, "low_ascii=%u\n", n.low_ascii) ;
fprintf(ofile, "color=%u\n", n.color) ;
fprintf(ofile, "showSHRfiles=%u ; use diff attrib for S/H/R files\n", n.showSHRfiles) ;
// fprintf(ofile, "ega_keep=%u ; switch to 50-line mode\n", n.ega_keep) ;
fprintf(ofile, "fdate_option=%u ; 0=LastWriteTime (default), 1=LastAccessTime, 2=CreationTime\n", n.fdate_option) ;
// generate default colors
fprintf(ofile, "\n") ;
fprintf(ofile, "global display colors.\n") ;
fprintf(ofile, "All colors can be decimal or hex (preceded by 0x)\n") ;
fprintf(ofile, "colorlogo=%u\n", n.colorlogo) ;
fprintf(ofile, "colornhead=%u\n", n.colornhead) ;
fprintf(ofile, "colorxhead=%u\n", n.colorxhead) ;
fprintf(ofile, "colorframe=%u\n", n.colorframe) ;
fprintf(ofile, "colorattr=%u\n", n.colorattr) ;
fprintf(ofile, "colorsize=%u\n", n.colorsize) ;
fprintf(ofile, "colordate=%u\n", n.colordate) ;
fprintf(ofile, "colortime=%u\n", n.colortime) ;
fprintf(ofile, "colordir=%u\n", n.colordir) ;
fprintf(ofile, "colorSHR=%u\n", n.colorSHR) ;
fprintf(ofile, "colordefalt=%u\n", n.colordefalt) ;
// generate colors for desired extentions
// attrib_list attr_default_list[] = {
fprintf(ofile, "\n") ;
fprintf(ofile, "; assign colors to file extensions.\n") ;
fprintf(ofile, "; You can add your extensions here, limited to 200 extentions\n") ;
fprintf(ofile, "; Wildcards are supported!!\n") ;
fprintf(ofile, "; two formats are supported:\n") ;
fprintf(ofile, "; ext=color and\n") ;
fprintf(ofile, "; color:ext,ext,ext,ext\n") ;
fprintf(ofile, "; Note that period is required for each extension\n") ;
for (j=0; attr_default_list[j].ext[0] != 0; j++) {
fprintf(ofile, "%s=%u\n", attr_default_list[j].ext,
attr_default_list[j].attr ) ;
}
// generate directory-tree colors
fprintf(ofile, "\n") ;
fprintf(ofile, "; assigned colors for levels in directory tree.\n") ;
fprintf(ofile, "; Each line defines an additional level, limit=20 colors\n") ;
for (j=0; j<MAX_DIR_ENTRY; j++) {
fprintf(ofile, ":%u\n", dir_default_list[j] ) ;
}
fclose(ofile) ;
return 0;
}
//*********************************************************************
struct ini_entry {
char *lvalue ;
uchar *rvalue ;
};
static ini_entry const ndir_ini[] = {
{ "ucase", &n.ucase },
{ "sort", &n.sort },
{ "reverse", &n.reverse },
{ "clear", &n.clear },
{ "pause", &n.pause },
{ "format", &n.format },
{ "minimize", &n.minimize },
{ "show_all", &n.show_all },
{ "dir_first", &n.dir_first },
{ "exec_only", &n.exec_only },
{ "horz", &n.horz },
{ "low_ascii", &n.low_ascii },
{ "color", &n.color },
{ "showSHRfiles", &n.showSHRfiles },
// { "ega_keep", &n.ega_keep },
// { "lfn_off", &n.lfn_off },
{ "fdate_option", &n.fdate_option },
{ "long_attr", &n.long_attr },
{ "colorlogo", &n.colorlogo },
{ "colornhead", &n.colornhead },
{ "colorxhead", &n.colorxhead },
{ "colorframe", &n.colorframe },
{ "colorattr", &n.colorattr },
{ "colorsize", &n.colorsize },
{ "colordate", &n.colordate },
{ "colortime", &n.colortime },
{ "colordir", &n.colordir },
{ "colorSHR", &n.colorSHR },
{ "colordefalt", &n.colordefalt },
{ "sizeopt", &n.size_display },
{ 0, 0} } ;
static void parse_ini_line(char *iniptr)
{
char *eqptr ;
int j ;
eqptr = _tcschr(iniptr, '=') ;
if (eqptr == 0)
return ;
*eqptr++ = 0 ; // NULL-terminate lvalue, point to rvalue
for (j=0; ndir_ini[j].lvalue != 0; j++) {
// if we find the lvalue (pointed to by iniptr) in the
// lookup table, assigned the rvalue (pointed to by eqptr)
// to the variable stored
if (strcmpiwc(iniptr, ndir_ini[j].lvalue)) {
*(ndir_ini[j].rvalue) = (uchar) _tcstoul(eqptr, 0, 0) ;
// printf("found %s=%u\n", ndir_ini[j].lvalue, (int) *(ndir_ini[j].rvalue)) ;
break;
}
}
}
//*********************************************************************
static int read_ini_file(TCHAR const * ini_str)
{
FILE *ofile ;
int slen ;
char *strptr ;
static char line[PATH_MAX] ;
// printf("reading %s\n", ini_str) ;
ofile = _tfopen(ini_str, _T("rt")) ;
if (ofile == 0)
return errno ;
while (fgets(line, sizeof(line), ofile) != 0) {
// strip off newline char
slen = _tcslen(line) ;
strptr = &line[slen-1] ;
if (*strptr == '\n') {
*strptr-- = 0 ; // strip off newline
// slen-- ;
}
// next, find and strip off comments
strptr = _tcschr(line, ';') ;
if (strptr != 0)
*strptr-- = 0 ;
// skip blank lines
slen = _tcslen(line) ;
if (slen == 0)
continue;
strptr = &line[slen-1] ;
// then strip off tailing spaces
while (slen > 0 && *strptr == ' ') {
*strptr-- = 0 ;
slen-- ;
}
if (slen == 0)
continue;
// now we should have a simple line in field=value format.
// See if we can parse it...
// see whether we're dealing with an extention-color entry,
// or a flags entry
if (line[0] == '!') {
parse_command_string(line) ;
} else if (line[0] == '.') {
parse_color_entry(line) ;
} else if (line[0] >= '0' && line[0] <= '9') {
parse_color_entry(line) ;
} else if (line[0] == ':') {
parse_dir_color_entry(line) ;
} else {
// printf("line=%s", line) ;
parse_ini_line(line) ;
}
}
fclose(ofile) ;
return 0;
}
//*********************************************************************
static char const local_ini_name[] = ".\\ndir.ini" ;
static char ini_path[PATH_MAX] ;
void read_config_file(void)
{
int result ;
// search for existing file.
// 1. look in current directory
// 2. if not found, search location of executable
// 3. if not found, generate default file in location of executable
// printf("seek local ini=%s\n", local_ini_name) ;
result = read_ini_file(local_ini_name) ;
if (result == 0) {
return ;
}
// If search for local file failed, try location of executable,
// if that isn't the local directory.
// If global INI filename isn't present, give up on search.
// This will usually mean that we are running under WinNT 4.0,
// and the executable is already in the current directory.
// Just write the file in the current directory.
// printf("ininame=%s\n", ininame) ;
// getchar() ;
if (ininame[0] == 0) {
_tcscpy(ini_path, local_ini_name) ;
}
// If global INI filename IS present, try to load it
else {
result = read_ini_file(ininame) ;
if (result == 0) {
return ;
}
_tcscpy(ini_path, ininame) ;
}
// If we couldn't open any existing INI files,
// generate default file in appropriate location.
result = write_default_ini_file(ini_path) ;
if (result != 0) {
// perror(ini_path) ;
sprintf (tempstr, "path [%s]\n", ini_path);
nputs (0xA, tempstr);
sprintf (tempstr, "FindFirst: %s\n", get_system_message ());
nputs (0xA, tempstr);
}
// try to read again, after writing defaults
read_ini_file(ini_path) ;
}