js_file.c 46.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* js_file.c */

/* Synchronet JavaScript "File" Object */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html		*
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
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
39
40
#include "md5.h"
#include "base64.h"
41
#include "uucode.h"
42
#include "yenc.h"
43
#include "ini_file.h"
44
45
46

#ifdef JAVASCRIPT

47
48
#include "jsdate.h"	/* Yes, I know this is a private header file */

49
50
typedef struct
{
51
	FILE*	fp;
52
53
	char	name[MAX_PATH+1];
	char	mode[4];
54
	uchar	etx;
55
56
	BOOL	external;	/* externally created, don't close */
	BOOL	debug;
57
	BOOL	rot13;
58
	BOOL	yencoded;
59
	BOOL	uuencoded;
60
61
	BOOL	b64encoded;
	BOOL	network_byte_order;
62
63
64

} private_t;

65
static const char* getprivate_failure = "line %d %s JS_GetPrivate failed";
66
67
68
69
70
71
72
73
74
75

static void dbprintf(BOOL error, private_t* p, char* fmt, ...)
{
	va_list argptr;
	char sbuf[1024];

	if(p==NULL || (!p->debug && !error))
		return;

    va_start(argptr,fmt);
76
77
    vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;
78
79
    va_end(argptr);
	
80
	lprintf(LOG_DEBUG,"%04u File %s%s",p->fp ? fileno(p->fp) : 0,error ? "ERROR: ":"",sbuf);
81
82
}

83
84
85
86
87
88
89
90
/* Converts fopen() style 'mode' string into open() style 'flags' integer */

static int fopenflags(char *mode)
{
	int flags=0;

	if(strchr(mode,'b'))
		flags|=O_BINARY;
91
92
	else
		flags|=O_TEXT;
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

	if(strchr(mode,'w')) {
		flags|=O_CREAT|O_TRUNC;
		if(strchr(mode,'+'))
			flags|=O_RDWR;
		else
			flags|=O_WRONLY;
		return(flags);
	}

	if(strchr(mode,'a')) {
		flags|=O_CREAT|O_APPEND;
		if(strchr(mode,'+'))
			flags|=O_RDWR;
		else
			flags|=O_WRONLY;
		return(flags);
	}

	if(strchr(mode,'r')) {
		if(strchr(mode,'+'))
			flags|=O_RDWR;
		else
			flags|=O_RDONLY;
	}

119
120
121
	if(strchr(mode,'e'))
		flags|=O_EXCL;

122
123
	return(flags);
}
124
125
126
127
128
129
130

/* File Object Methods */

static JSBool
js_open(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		mode="w+";	/* default mode */
131
132
	BOOL		shareable=FALSE;
	int			file;
133
	uintN		i;
134
	jsint		bufsize=2*1024;
135
136
137
	JSString*	str;
	private_t*	p;

138
	*rval = JSVAL_FALSE;
139

140
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
141
		JS_ReportError(cx,getprivate_failure,WHERE);
142
		return(JS_FALSE);
143
	}
144
145
146

	if(p->fp!=NULL)  
		return(JS_TRUE);
147
148
149
150

	for(i=0;i<argc;i++) {
		if(JSVAL_IS_STRING(argv[i])) {	/* mode */
			if((str = JS_ValueToString(cx, argv[i]))==NULL) {
151
152
				JS_ReportError(cx,"Invalid mode specified: %s",str);
				return(JS_TRUE);
153
154
			}
			mode=JS_GetStringBytes(str);
155
156
		} else if(JSVAL_IS_BOOLEAN(argv[i]))	/* shareable */
			shareable=JSVAL_TO_BOOLEAN(argv[i]);
157
		else if(JSVAL_IS_NUMBER(argv[i]))	/* bufsize */
158
			JS_ValueToInt32(cx,argv[i],&bufsize);
159
	}
160
	SAFECOPY(p->mode,mode);
161

162
163
164
165
166
167
168
169
	if(shareable)
		p->fp=fopen(p->name,p->mode);
	else {
		if((file=nopen(p->name,fopenflags(p->mode)))!=-1) {
			if((p->fp=fdopen(file,p->mode))==NULL)
				close(file);
		}
	}
170
	if(p->fp!=NULL) {
171
		*rval = JSVAL_TRUE;
172
		dbprintf(FALSE, p, "opened: %s",p->name);
173
174
175
176
177
		if(!bufsize)
			setvbuf(p->fp,NULL,_IONBF,0);	/* no buffering */
		else
			setvbuf(p->fp,NULL,_IOFBF,bufsize);
	}
178
179
180
181
182
183
184
185
186
187

	return(JS_TRUE);
}


static JSBool
js_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;

188
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
189
		JS_ReportError(cx,getprivate_failure,WHERE);
190
		return(JS_FALSE);
191
	}
192
193
194
195
196
197
198
199
200
201
202
203
204
205

	if(p->fp==NULL)
		return(JS_TRUE);

	fclose(p->fp);

	dbprintf(FALSE, p, "closed");

	p->fp=NULL; 

	return(JS_TRUE);
}

static JSBool
206
js_read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
207
208
{
	char*		cp;
209
	char*		buf;
210
	char*		uubuf;
211
212
	int32		len;
	int32		offset;
213
	int32		uulen;
214
215
216
	JSString*	str;
	private_t*	p;

217
218
	*rval = JSVAL_NULL;

219
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
220
		JS_ReportError(cx,getprivate_failure,WHERE);
221
		return(JS_FALSE);
222
	}
223

224
225
	if(p->fp==NULL)
		return(JS_TRUE);
226

227
	if(argc)
228
		JS_ValueToInt32(cx,argv[0],&len);
229
230
231
232
233
234
235
236
	else {
		len=filelength(fileno(p->fp));
		offset=ftell(p->fp);
		if(offset>0)
			len-=offset;
	}
	if(len<0)
		len=512;
237

238
239
240
241
242
243
244
245
246
247
248
249
250
	if((buf=malloc(len+1))==NULL)
		return(JS_TRUE);

	len = fread(buf,1,len,p->fp);
	if(len<0) 
		len=0;
	buf[len]=0;

	if(p->etx) {
		cp=strchr(buf,p->etx);
		if(cp) *cp=0; 
	}

251
252
253
	if(p->rot13)
		rot13(buf);

254
	if(p->uuencoded || p->b64encoded || p->yencoded) {
255
256
257
		uulen=len*2;
		if((uubuf=malloc(uulen))==NULL)
			return(JS_TRUE);
258
259
		if(p->uuencoded)
			uulen=uuencode(uubuf,uulen,buf,len);
260
261
		else if(p->yencoded)
			uulen=yencode(uubuf,uulen,buf,len);
262
263
		else
			uulen=b64_encode(uubuf,uulen,buf,len);
264
265
266
267
268
269
270
		if(uulen>=0) {
			free(buf);
			buf=uubuf;
		} else
			free(uubuf);
	}

271
272
273
274
	str = JS_NewStringCopyZ(cx, buf);

	free(buf);

275
276
277
	if(str==NULL)
		return(JS_FALSE);

278
279
280
	*rval = STRING_TO_JSVAL(str);

	dbprintf(FALSE, p, "read %u bytes",len);
281
282
283
284
285
		
	return(JS_TRUE);
}

static JSBool
286
js_readln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
287
{
288
289
	char*		cp;
	char*		buf;
290
	int32		len=512;
291
	JSString*	js_str;
292
293
	private_t*	p;

294
	*rval = JSVAL_NULL;
295

296
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
297
		JS_ReportError(cx,getprivate_failure,WHERE);
298
		return(JS_FALSE);
299
	}
300
301
302

	if(p->fp==NULL)
		return(JS_TRUE);
303
304
	
	if(argc)
305
		JS_ValueToInt32(cx,argv[0],&len);
306

307
308
309
310
	if((buf=malloc(len))==NULL)
		return(JS_TRUE);

	if(fgets(buf,len,p->fp)!=NULL) {
311
312
313
314
		len=strlen(buf);
		while(len>0 && (buf[len-1]=='\r' || buf[len-1]=='\n'))
			len--;
		buf[len]=0;
315
316
317
318
		if(p->etx) {
			cp=strchr(buf,p->etx);
			if(cp) *cp=0; 
		}
319
320
		if(p->rot13)
			rot13(buf);
321
322
		if((js_str=JS_NewStringCopyZ(cx,buf))!=NULL)	// exception here Feb-12-2005
			*rval = STRING_TO_JSVAL(js_str);			// _CrtDbgBreak from _heap_alloc_dbg
323
324
	}

325
326
	free(buf);

327
328
329
330
	return(JS_TRUE);
}

static JSBool
331
js_readbin(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
332
333
334
335
336
337
338
{
	BYTE		b;
	WORD		w;
	DWORD		l;
	size_t		size=sizeof(DWORD);
	private_t*	p;

339
	*rval = INT_TO_JSVAL(-1);
340

341
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
342
		JS_ReportError(cx,getprivate_failure,WHERE);
343
		return(JS_FALSE);
344
	}
345
346
347
348

	if(p->fp==NULL)
		return(JS_TRUE);

349
	if(argc) 
350
		JS_ValueToInt32(cx,argv[0],(int32*)&size);
351
352
353

	switch(size) {
		case sizeof(BYTE):
354
355
			if(fread(&b,1,size,p->fp)==size)
				*rval = INT_TO_JSVAL(b);
356
357
			break;
		case sizeof(WORD):
358
359
360
			if(fread(&w,1,size,p->fp)==size) {
				if(p->network_byte_order)
					w=ntohs(w);
361
				*rval = INT_TO_JSVAL(w);
362
			}
363
364
			break;
		case sizeof(DWORD):
365
366
367
			if(fread(&l,1,size,p->fp)==size) {
				if(p->network_byte_order)
					l=ntohl(l);
368
				JS_NewNumberValue(cx,l,rval);
369
			}
370
371
372
373
374
375
376
			break;
	}
		
	return(JS_TRUE);
}

static JSBool
377
js_readall(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
378
{
379
380
381
    jsint       len=0;
    jsval       line;
    JSObject*	array;
382
383
	private_t*	p;

384
385
	*rval = JSVAL_NULL;

386
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
387
		JS_ReportError(cx,getprivate_failure,WHERE);
388
		return(JS_FALSE);
389
	}
390

391
392
	if(p->fp==NULL)
		return(JS_TRUE);
393

394
    array = JS_NewArrayObject(cx, 0, NULL);
395

396
397
398
399
    while(!feof(p->fp)) {
		js_readln(cx, obj, 0, NULL, &line); 
		if(line==JSVAL_NULL)
			break;
400
401
        if(!JS_SetElement(cx, array, len++, &line))
			break;
402
	}
403
    *rval = OBJECT_TO_JSVAL(array);
404

405
406
    return(JS_TRUE);
}
407

408
409
410
411
412
413
414
415
416
417
418
static jsval get_value(JSContext *cx, char* value)
{
	char*	p;
	BOOL	f=FALSE;
	jsval	val;

	if(value==NULL || *value==0)
		return(JSVAL_VOID);

	/* integer or float? */
	for(p=value;*p;p++) {
419
		if(*p=='.' && !f)
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
			f=TRUE;
		else if(!isdigit(*p))
			break;
	}
	if(*p==0) {	
		JS_NewNumberValue(cx, f ? atof(value) : strtoul(value,NULL,10), &val);
		return(val);
	}
	/* hexadecimal number? */
	if(!strncmp(value,"0x",2)) {	
		for(p=value+2;*p;p++)
			if(!isxdigit(*p))
				break;
		if(*p==0) {	
			JS_NewNumberValue(cx,strtoul(value,NULL,0),&val);
			return(val);
		}
	}
	/* Boolean? */
	if(!stricmp(value,"true"))
		return(JSVAL_TRUE);
	if(!stricmp(value,"false"))
		return(JSVAL_FALSE);

	/* String */
	return(STRING_TO_JSVAL(JS_NewStringCopyZ(cx,value)));
}

448
static JSBool
449
js_iniGetValue(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
450
{
rswindell's avatar
rswindell committed
451
	char*	section=ROOT_SECTION;
452
	char*	key;
453
	char**	list;
454
	char	buf[INI_MAX_VALUE_LEN];
455
456
	int32	i;
	jsval	val;
457
458
	jsval	dflt=argv[2];
	private_t*	p;
459
	JSObject*	array;
460
461
	JSObject*	dflt_obj;
	JSObject*	date_obj;
462
463
464
465
466
467

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);
	}

468
469
470
	if(p->fp==NULL)
		return(JS_TRUE);

471
472
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
473
	key=JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
474
475

	if(dflt==JSVAL_VOID) {	/* unspecified default value */
476
		*rval=get_value(cx,iniReadString(p->fp,section,key,NULL,buf));
477
478
479
		return(JS_TRUE);
	}

480
481
482
	switch(JSVAL_TAG(dflt)) {
		case JSVAL_BOOLEAN:
			*rval = BOOLEAN_TO_JSVAL(
483
				iniReadBool(p->fp,section,key,JSVAL_TO_BOOLEAN(dflt)));
484
485
486
			break;
		case JSVAL_DOUBLE:
			JS_NewNumberValue(cx
487
				,iniReadFloat(p->fp,section,key,*JSVAL_TO_DOUBLE(dflt)),rval);
488
			break;
489
		case JSVAL_OBJECT:
490
			if((dflt_obj = JSVAL_TO_OBJECT(dflt))!=NULL && js_DateIsValid(cx, dflt_obj)) {
491
492
493
494
495
496
497
498
				date_obj = js_NewDateObjectMsec(cx
					,(jsdouble)iniReadDateTime(p->fp,section,key
						,(time_t)(js_DateGetMsecSinceEpoch(cx,dflt_obj)/1000.0))
					*1000.0);
				if(date_obj!=NULL)
					*rval = OBJECT_TO_JSVAL(date_obj);
				break;
			}
499
		    array = JS_NewArrayObject(cx, 0, NULL);
500
			list=iniReadStringList(p->fp,section,key,",",JS_GetStringBytes(JS_ValueToString(cx,dflt)));
501
502
503
504
505
506
507
508
			for(i=0;list && list[i];i++) {
				val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,list[i]));
				if(!JS_SetElement(cx, array, i, &val))
					break;
			}
			iniFreeStringList(list);
			*rval = OBJECT_TO_JSVAL(array);
			break;
509
		default:
510
511
			if(JSVAL_IS_NUMBER(dflt)) {
				JS_ValueToInt32(cx,dflt,&i);
512
				JS_NewNumberValue(cx,iniReadInteger(p->fp,section,key,i),rval);
513
514
515
			} else
				*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx
					,iniReadString(p->fp,section,key,JS_GetStringBytes(JS_ValueToString(cx,dflt)),buf)));
516
517
518
519
520
521
			break;
	}

	return(JS_TRUE);
}

522
523
524
static JSBool
js_iniSetValue(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
rswindell's avatar
rswindell committed
525
	char*	section=ROOT_SECTION;
526
527
528
529
530
531
	char*	key;
	char*	result=NULL;
	int32	i;
	jsval	value=argv[2];
	private_t*	p;
	str_list_t	list;
532
	JSObject*	value_obj;
533
534
535
536
537
538
539
540

	*rval = JSVAL_FALSE;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);
	}

541
542
543
	if(p->fp==NULL)
		return(JS_TRUE);

544
545
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
	key=JS_GetStringBytes(JS_ValueToString(cx, argv[1]));

	if((list=iniReadFile(p->fp))==NULL)
		return(JS_TRUE);

	if(value==JSVAL_VOID) 	/* unspecified value */
		result = iniSetString(&list,section,key,"",NULL);
	else {

		switch(JSVAL_TAG(value)) {
			case JSVAL_BOOLEAN:
				result = iniSetBool(&list,section,key,JSVAL_TO_BOOLEAN(value),NULL);
				break;
			case JSVAL_DOUBLE:
				result = iniSetFloat(&list,section,key,*JSVAL_TO_DOUBLE(value),NULL);
				break;
			default:
				if(JSVAL_IS_NUMBER(value)) {
					JS_ValueToInt32(cx,value,&i);
					result = iniSetInteger(&list,section,key,i,NULL);
566
567
568
569
570
571
572
573
574
575
				} else {
					if(JSVAL_IS_OBJECT(value) 
						&& (value_obj = JSVAL_TO_OBJECT(value))!=NULL
						&& js_DateIsValid(cx, value_obj)) {
						result = iniSetDateTime(&list,section,key,/* include_time */TRUE
									,(time_t)(js_DateGetMsecSinceEpoch(cx,value_obj)/1000.0),NULL);
					} else
						result = iniSetString(&list,section,key
									,JS_GetStringBytes(JS_ValueToString(cx,value)),NULL);
				}
576
577
578
579
580
581
582
583
584
585
586
587
588
				break;
		}
	}

	if(result != NULL)
		*rval = BOOLEAN_TO_JSVAL(iniWriteFile(p->fp,list));

	strListFree(&list);

	return(JS_TRUE);
}


589
590
591
static JSBool
js_iniGetSections(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
592
	char*		prefix=NULL;
593
594
595
596
597
598
599
600
601
602
603
604
605
	char**		list;
    jsint       i;
    jsval       val;
    JSObject*	array;
	private_t*	p;

	*rval = JSVAL_NULL;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);
	}

606
607
608
	if(p->fp==NULL)
		return(JS_TRUE);

609
610
611
	if(argc)
		prefix=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));

612
613
    array = JS_NewArrayObject(cx, 0, NULL);

614
	list = iniReadSectionList(p->fp,prefix);
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    for(i=0;list && list[i];i++) {
		val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,list[i]));
        if(!JS_SetElement(cx, array, i, &val))
			break;
	}
	iniFreeStringList(list);

    *rval = OBJECT_TO_JSVAL(array);

    return(JS_TRUE);
}

static JSBool
js_iniGetKeys(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
rswindell's avatar
rswindell committed
630
	char*		section=ROOT_SECTION;
631
632
633
634
635
636
637
638
639
640
641
642
643
	char**		list;
    jsint       i;
    jsval       val;
    JSObject*	array;
	private_t*	p;

	*rval = JSVAL_NULL;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);
	}

644
645
646
	if(p->fp==NULL)
		return(JS_TRUE);

647
648
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
649
650
    array = JS_NewArrayObject(cx, 0, NULL);

651
	list = iniReadKeyList(p->fp,section);
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
    for(i=0;list && list[i];i++) {
		val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,list[i]));
        if(!JS_SetElement(cx, array, i, &val))
			break;
	}
	iniFreeStringList(list);

    *rval = OBJECT_TO_JSVAL(array);

    return(JS_TRUE);
}

static JSBool
js_iniGetObject(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
rswindell's avatar
rswindell committed
667
	char*		section=ROOT_SECTION;
668
669
670
    jsint       i;
    JSObject*	object;
	private_t*	p;
671
	named_string_t** list;
672
673
674
675
676
677
678
679

	*rval = JSVAL_NULL;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);
	}

680
681
682
	if(p->fp==NULL)
		return(JS_TRUE);

683
684
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
685
686
    object = JS_NewObject(cx, NULL, NULL, obj);

687
	list = iniReadNamedStringList(p->fp,section);
688
    for(i=0;list && list[i];i++) {
689
		JS_DefineProperty(cx, object, list[i]->name
690
			,get_value(cx,list[i]->value)
691
692
693
			,NULL,NULL,JSPROP_ENUMERATE);

	}
694
	iniFreeNamedStringList(list);
695
696
697
698
699
700

    *rval = OBJECT_TO_JSVAL(object);

    return(JS_TRUE);
}

701
702
703
704
705
706
707
708
709
710
711
712
static JSBool
js_iniSetObject(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    jsint       i;
    JSObject*	object;
	JSIdArray*	id_array;
	jsval		set_argv[3];

	*rval = JSVAL_FALSE;

	set_argv[0]=argv[0];	/* section */

713
	if(!JSVAL_IS_OBJECT(argv[1]) || argv[1]==JSVAL_NULL)
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
		return(JS_TRUE);

    object = JSVAL_TO_OBJECT(argv[1]);

	if((id_array=JS_Enumerate(cx,object))==NULL)
		return(JS_TRUE);

	for(i=0; i<id_array->length; i++)  {
		/* property */
		JS_IdToValue(cx,id_array->vector[i],&set_argv[1]);	
		/* value */
		JS_GetProperty(cx,object,JS_GetStringBytes(JSVAL_TO_STRING(set_argv[1])),&set_argv[2]);
		if(!js_iniSetValue(cx,obj,3,set_argv,rval))
			break;
	}

730
731
	JS_DestroyIdArray(cx,id_array);

732
733
734
735
    return(JS_TRUE);
}


736
737
738
739
static JSBool
js_iniGetAllObjects(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		name="name";
740
	char*		sec_name;
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
	char*		prefix=NULL;
	char**		sec_list;
    jsint       i,k;
    jsval       val;
    JSObject*	array;
    JSObject*	object;
	private_t*	p;
	named_string_t** key_list;

	*rval = JSVAL_NULL;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);
	}

757
758
759
	if(p->fp==NULL)
		return(JS_TRUE);

760
761
762
763
764
765
766
767
	if(argc)
		name=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));

	if(argc>1)
		prefix=JS_GetStringBytes(JS_ValueToString(cx, argv[1]));

    array = JS_NewArrayObject(cx, 0, NULL);

768
	sec_list = iniReadSectionList(p->fp,prefix);
769
770
771
    for(i=0;sec_list && sec_list[i];i++) {
	    object = JS_NewObject(cx, NULL, NULL, obj);

772
773
774
		sec_name=sec_list[i];
		if(prefix!=NULL)
			sec_name+=strlen(prefix);
775
		JS_DefineProperty(cx, object, name
776
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,sec_name))
777
778
			,NULL,NULL,JSPROP_ENUMERATE);

779
		key_list = iniReadNamedStringList(p->fp,sec_list[i]);
780
781
		for(k=0;key_list && key_list[k];k++)
			JS_DefineProperty(cx, object, key_list[k]->name
782
				,get_value(cx,key_list[k]->value)
783
784
785
786
787
788
789
790
791
792
793
794
795
				,NULL,NULL,JSPROP_ENUMERATE);
		iniFreeNamedStringList(key_list);

		val=OBJECT_TO_JSVAL(object);
        if(!JS_SetElement(cx, array, i, &val))
			break;
	}
	iniFreeStringList(sec_list);

    *rval = OBJECT_TO_JSVAL(array);

    return(JS_TRUE);
}
796

797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
static JSBool
js_iniSetAllObjects(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		name="name";
    jsuint      i;
    jsuint      count;
    JSObject*	array;
	jsval		set_argv[2];

	*rval = JSVAL_FALSE;

	if(!JSVAL_IS_OBJECT(argv[0]))
		return(JS_TRUE);

    array = JSVAL_TO_OBJECT(argv[0]);

	if(!JS_IsArrayObject(cx, array))
		return(JS_TRUE);

    if(!JS_GetArrayLength(cx, array, &count))
		return(JS_TRUE);

	if(argc>1)
		name=JS_GetStringBytes(JS_ValueToString(cx, argv[1]));

	/* enumerate the array */
	for(i=0; i<count; i++)  {
        if(!JS_GetElement(cx, array, i, &set_argv[1]))
			break;
		if(!JSVAL_IS_OBJECT(set_argv[1]))	/* must be an array of objects */
			break;
		if(!JS_GetProperty(cx, JSVAL_TO_OBJECT(set_argv[1]), name, &set_argv[0]))
			continue;
		if(!js_iniSetObject(cx, obj, 2, set_argv, rval))
			break;
	}

    return(JS_TRUE);
}

837
838
839
840
static JSBool
js_write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		cp;
841
842
843
	char*		uubuf=NULL;
	int			len;	/* string length */
	int			tlen;	/* total length to write (may be greater than len) */
844
	private_t*	p;
845

846
	*rval = JSVAL_FALSE;
847

848
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
849
		JS_ReportError(cx,getprivate_failure,WHERE);
850
		return(JS_FALSE);
851
	}
852
853
854
855

	if(p->fp==NULL)
		return(JS_TRUE);

856
857
858
	cp=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
	len=strlen(cp);

859
	if((p->uuencoded || p->b64encoded || p->yencoded)
860
861
862
		&& len && (uubuf=malloc(len))!=NULL) {
		if(p->uuencoded)
			len=uudecode(uubuf,len,cp,len);
863
864
		else if(p->yencoded)
			len=ydecode(uubuf,len,cp,len);
865
866
		else
			len=b64_decode(uubuf,len,cp,len);
867
868
869
870
871
872
873
		if(len<0) {
			free(uubuf);
			return(JS_TRUE);
		}
		cp=uubuf;
	}

874
875
876
	if(p->rot13)
		rot13(cp);

877
	tlen=len;
878
	if(argc>1) {
879
		JS_ValueToInt32(cx,argv[1],(int32*)&tlen);
880
881
882
		if(len>tlen)
			len=tlen;
	}
883
884

	if(fwrite(cp,1,len,p->fp)==(size_t)len) {
885
886
887
888
889
890
891
892
893
894
895
		if(tlen>len) {
			len=tlen-len;
			if((cp=malloc(len))==NULL) {
				dbprintf(TRUE, p, "malloc failure of %u bytes", len);
				return(JS_TRUE);
			}
			memset(cp,p->etx,len);
			fwrite(cp,1,len,p->fp);
			free(cp);
		}
		dbprintf(FALSE, p, "wrote %u bytes",tlen);
896
		*rval = JSVAL_TRUE;
897
898
	} else 
		dbprintf(TRUE, p, "write of %u bytes failed",len);
899
		
900
901
902
	if(uubuf!=NULL)
		free(uubuf);

903
904
905
906
	return(JS_TRUE);
}

static JSBool
907
js_writeln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
908
{
909
910
911
912
	char*		cp="";
	JSString*	str;
	private_t*	p;

913
	*rval = JSVAL_FALSE;
914

915
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
916
		JS_ReportError(cx,getprivate_failure,WHERE);
917
		return(JS_FALSE);
918
	}
919
920
921
922
923

	if(p->fp==NULL)
		return(JS_TRUE);

	if(argc) {
924
925
		if((str = JS_ValueToString(cx, argv[0]))==NULL) {
			JS_ReportError(cx,"JS_ValueToString failed");
926
			return(JS_FALSE);
927
		}
928
929
930
		cp = JS_GetStringBytes(str);
	}

931
932
933
	if(p->rot13)
		rot13(cp);

934
	if(fprintf(p->fp,"%s\n",cp)!=0)
935
		*rval = JSVAL_TRUE;
936

937
938
	return(JS_TRUE);
}
939

940
static JSBool
941
js_writebin(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
942
{
943
944
945
	BYTE		b;
	WORD		w;
	DWORD		l;
946
	int32		val=0;
947
	size_t		wr=0;
948
	size_t		size=sizeof(DWORD);
949
950
	private_t*	p;

951
	*rval = JSVAL_FALSE;
952

953
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
954
		JS_ReportError(cx,getprivate_failure,WHERE);
955
		return(JS_FALSE);
956
	}
957

958
959
	if(p->fp==NULL)
		return(JS_TRUE);
960

961
	JS_ValueToInt32(cx,argv[0],&val);
962
	if(argc>1) 
963
		JS_ValueToInt32(cx,argv[1],(int32*)&size);
964

965
966
	switch(size) {
		case sizeof(BYTE):
967
			b = (BYTE)val;
968
			wr=fwrite(&b,1,size,p->fp);
969
970
			break;
		case sizeof(WORD):
971
			w = (WORD)val;
972
973
			if(p->network_byte_order)
				w=htons(w);
974
			wr=fwrite(&w,1,size,p->fp);
975
976
			break;
		case sizeof(DWORD):
977
			l = val;
978
979
			if(p->network_byte_order)
				l=htonl(l);
980
981
982
983
984
			wr=fwrite(&l,1,size,p->fp);
			break;
		default:	
			/* unknown size */
			dbprintf(TRUE, p, "unsupported binary write size: %d",size);
985
986
			break;
	}
987
	if(wr==size)
988
		*rval = JSVAL_TRUE;
989
		
990
991
992
	return(JS_TRUE);
}

993
994
995
996
997
998
999
1000
1001
static JSBool
js_writeall(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    jsuint      i;
    jsuint      limit;
    JSObject*	array;
    jsval       elemval;
	private_t*	p;

1002
	*rval = JSVAL_FALSE;
1003

1004
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
1005
		JS_ReportError(cx,getprivate_failure,WHERE);
1006
		return(JS_FALSE);
1007
	}
1008
1009
1010
1011

	if(p->fp==NULL)
		return(JS_TRUE);

1012
	if(!JSVAL_IS_OBJECT(argv[0]))
1013
1014
1015
1016
		return(JS_TRUE);

    array = JSVAL_TO_OBJECT(argv[0]);

1017
1018
1019
    if(!JS_IsArrayObject(cx, array))
		return(JS_TRUE);

1020
1021
    if(!JS_GetArrayLength(cx, array, &limit))
		return(JS_FALSE);
1022

1023
    *rval = JSVAL_TRUE;
1024

1025
1026
1027
1028
    for(i=0;i<limit;i++) {
        if(!JS_GetElement(cx, array, i, &elemval))
			break;
        js_writeln(cx, obj, 1, &elemval, rval);
1029
		if(*rval!=JSVAL_TRUE)
1030
			break;
1031
1032
1033
1034
1035
    }

    return(JS_TRUE);
}

1036
static JSBool
1037
js_lock(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
1038
{
1039
1040
	int32		offset=0;
	int32		len=0;
1041
1042
	private_t*	p;

1043
	*rval = JSVAL_FALSE;
1044

1045
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
1046
		JS_ReportError(cx,getprivate_failure,WHERE);
1047
		return(JS_FALSE);
1048
	}
1049
1050
1051
1052

	if(p->fp==NULL)
		return(JS_TRUE);

1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
	/* offset */
	if(argc)
		JS_ValueToInt32(cx,argv[0],&offset);

	/* length */
	if(argc>1)
		JS_ValueToInt32(cx,argv[1],&len);

	if(len==0)
		len=filelength(fileno(p->fp))-offset;

	if(lock(fileno(p->fp),offset,len)==0)
1065
		*rval = JSVAL_TRUE;
1066

1067
1068
	return(JS_TRUE);
}
1069

1070
static JSBool
1071
js_unlock(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
1072
{
1073
1074
	int32		offset=0;
	int32		len=0;
1075
1076
	private_t*	p;

1077
	*rval = JSVAL_FALSE;
1078

1079
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
1080
		JS_ReportError(cx,getprivate_failure,WHERE);
1081
		return(JS_FALSE);
1082
	}
1083
1084
1085
1086

	if(p->fp==NULL)
		return(JS_TRUE);

1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
	/* offset */
	if(argc)
		JS_ValueToInt32(cx,argv[0],&offset);

	/* length */
	if(argc>1)
		JS_ValueToInt32(cx,argv[1],&len);

	if(len==0)
		len=filelength(fileno(p->fp))-offset;

1098
	if(unlock(fileno(p->fp),offset,len)==0)
1099
		*rval = JSVAL_TRUE;
1100

1101
1102
1103
	return(JS_TRUE);
}

1104
1105
1106
1107
1108
static JSBool
js_delete(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;

1109
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
1110
		JS_ReportError(cx,getprivate_failure,WHERE);
1111
		return(JS_FALSE);
1112
	}
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122

	if(p->fp!=NULL) {	/* close it if it's open */
		fclose(p->fp);
		p->fp=NULL;
	}

	*rval = BOOLEAN_TO_JSVAL(remove(p->name)==0);

	return(JS_TRUE);
}