js_file.c 65.6 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 2011 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
#include "js_request.h"
48

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
	BOOL	pipe;		/* Opened with popen() use pclose() to close */
63
64
65

} private_t;

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

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

84
85
86
87
88
89
90
91
/* 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;
92
93
	else
		flags|=O_TEXT;
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

	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;
	}

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

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

/* File Object Methods */

static JSBool
129
js_open(JSContext *cx, uintN argc, jsval *arglist)
130
{
131
132
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
133
	char*		mode="w+";	/* default mode */
134
135
	BOOL		shareable=FALSE;
	int			file;
136
	uintN		i;
137
	jsint		bufsize=2*1024;
138
139
	JSString*	str;
	private_t*	p;
deuce's avatar
deuce committed
140
	jsrefcount	rc;
141

142
	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
143

144
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
145
		JS_ReportError(cx,getprivate_failure,WHERE);
146
		return(JS_FALSE);
147
	}
148
149
150

	if(p->fp!=NULL)  
		return(JS_TRUE);
151
152
153
154

	for(i=0;i<argc;i++) {
		if(JSVAL_IS_STRING(argv[i])) {	/* mode */
			if((str = JS_ValueToString(cx, argv[i]))==NULL) {
155
156
				JS_ReportError(cx,"Invalid mode specified: %s",str);
				return(JS_TRUE);
157
			}
deuce's avatar
deuce committed
158
			JSSTRING_TO_STRING(cx, str, mode);
159
160
		} else if(JSVAL_IS_BOOLEAN(argv[i]))	/* shareable */
			shareable=JSVAL_TO_BOOLEAN(argv[i]);
161
162
163
164
		else if(JSVAL_IS_NUMBER(argv[i])) {	/* bufsize */
			if(!JS_ValueToInt32(cx,argv[i],&bufsize))
				return(JS_FALSE);
		}
165
	}
166
	SAFECOPY(p->mode,mode);
167

168
	rc=JS_SUSPENDREQUEST(cx);
169
170
171
172
	if(shareable)
		p->fp=fopen(p->name,p->mode);
	else {
		if((file=nopen(p->name,fopenflags(p->mode)))!=-1) {
173
174
175
176
			char fdomode[4];
			SAFECOPY(fdomode,p->mode);
			fdomode[strspn(fdomode,"abrwt+")]=0;	/* MSVC10 fdopen() asserts when passed a mode with an unsupported char (e.g. 'e') */
			if((p->fp=fdopen(file,fdomode))==NULL)
177
178
179
				close(file);
		}
	}
180
	if(p->fp!=NULL) {
181
		JS_SET_RVAL(cx, arglist, JSVAL_TRUE);
182
		dbprintf(FALSE, p, "opened: %s",p->name);
183
184
185
186
187
		if(!bufsize)
			setvbuf(p->fp,NULL,_IONBF,0);	/* no buffering */
		else
			setvbuf(p->fp,NULL,_IOFBF,bufsize);
	}
188
	JS_RESUMEREQUEST(cx, rc);
189
190
191
192

	return(JS_TRUE);
}

193
static JSBool
194
js_popen(JSContext *cx, uintN argc, jsval *arglist)
195
{
196
197
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
198
199
200
201
202
	char*		mode="r+";	/* default mode */
	uintN		i;
	jsint		bufsize=2*1024;
	JSString*	str;
	private_t*	p;
deuce's avatar
deuce committed
203
	jsrefcount	rc;
204

205
	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

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

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

	for(i=0;i<argc;i++) {
		if(JSVAL_IS_STRING(argv[i])) {	/* mode */
			if((str = JS_ValueToString(cx, argv[i]))==NULL) {
				JS_ReportError(cx,"Invalid mode specified: %s",str);
				return(JS_TRUE);
			}
deuce's avatar
deuce committed
221
			JSSTRING_TO_STRING(cx, str, mode);
222
223
224
225
226
227
228
229
		}
		else if(JSVAL_IS_NUMBER(argv[i])) {	/* bufsize */
			if(!JS_ValueToInt32(cx,argv[i],&bufsize))
				return(JS_FALSE);
		}
	}
	SAFECOPY(p->mode,mode);

230
	rc=JS_SUSPENDREQUEST(cx);
231
232
233
	p->fp=popen(p->name,p->mode);
	if(p->fp!=NULL) {
		p->pipe=TRUE;
234
		JS_SET_RVAL(cx, arglist, JSVAL_TRUE);
235
236
237
238
239
240
		dbprintf(FALSE, p, "popened: %s",p->name);
		if(!bufsize)
			setvbuf(p->fp,NULL,_IONBF,0);	/* no buffering */
		else
			setvbuf(p->fp,NULL,_IOFBF,bufsize);
	}
241
	JS_RESUMEREQUEST(cx, rc);
242
243
244

	return(JS_TRUE);
}
245
246

static JSBool
247
js_close(JSContext *cx, uintN argc, jsval *arglist)
248
{
249
250
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
251
	private_t*	p;
deuce's avatar
deuce committed
252
	jsrefcount	rc;
253

deuce's avatar
deuce committed
254
	JS_SET_RVAL(cx, arglist, JSVAL_VOID);
255
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
256
		JS_ReportError(cx,getprivate_failure,WHERE);
257
		return(JS_FALSE);
258
	}
259
260
261
262

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

263
	rc=JS_SUSPENDREQUEST(cx);
264
265
266
267
268
269
#ifdef __unix__
	if(p->pipe)
		pclose(p->fp);
	else
#endif
		fclose(p->fp);
270
271
272
273

	dbprintf(FALSE, p, "closed");

	p->fp=NULL; 
274
	JS_RESUMEREQUEST(cx, rc);
275
276
277
278
279

	return(JS_TRUE);
}

static JSBool
280
js_read(JSContext *cx, uintN argc, jsval *arglist)
281
{
282
283
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
284
	char*		cp;
285
	char*		buf;
286
	char*		uubuf;
287
288
	int32		len;
	int32		offset;
289
	int32		uulen;
290
291
	JSString*	str;
	private_t*	p;
deuce's avatar
deuce committed
292
	jsrefcount	rc;
293

294
	JS_SET_RVAL(cx, arglist, 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
305
306
307
	if(argc) {
		if(!JS_ValueToInt32(cx,argv[0],&len))
			return(JS_FALSE);
	} else {
308
		rc=JS_SUSPENDREQUEST(cx);
309
310
		len=(long)filelength(fileno(p->fp));
		offset=(long)ftell(p->fp);
311
312
		if(offset>0)
			len-=offset;
313
		JS_RESUMEREQUEST(cx, rc);
314
315
316
	}
	if(len<0)
		len=512;
317

318
	if((buf=malloc(len+1))==NULL)
319
320
		return(JS_TRUE);

321
	rc=JS_SUSPENDREQUEST(cx);
322
323
324
325
326
327
328
329
	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; 
330
		len=strlen(buf);
331
332
	}

333
334
335
	if(p->rot13)
		rot13(buf);

336
	if(p->uuencoded || p->b64encoded || p->yencoded) {
337
		uulen=len*2;
deuce's avatar
deuce committed
338
		if((uubuf=malloc(uulen))==NULL) {
339
			free(buf);
340
			JS_RESUMEREQUEST(cx, rc);
341
			return(JS_TRUE);
342
		}
343
344
		if(p->uuencoded)
			uulen=uuencode(uubuf,uulen,buf,len);
345
346
		else if(p->yencoded)
			uulen=yencode(uubuf,uulen,buf,len);
347
348
		else
			uulen=b64_encode(uubuf,uulen,buf,len);
349
		if(uulen>=0) {
350
			free(buf);
351
			buf=uubuf;
352
			len=uulen;
deuce's avatar
deuce committed
353
		}
354
355
		else
			free(uubuf);
356
	}
357
	JS_RESUMEREQUEST(cx, rc);
358

359
	str = JS_NewStringCopyN(cx, buf, len);
360
	free(buf);
361

362
363
364
	if(str==NULL)
		return(JS_FALSE);

365
	JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(str));
366

367
	rc=JS_SUSPENDREQUEST(cx);
368
	dbprintf(FALSE, p, "read %u bytes",len);
369
	JS_RESUMEREQUEST(cx, rc);
370
371
372
373
374
		
	return(JS_TRUE);
}

static JSBool
375
js_readln(JSContext *cx, uintN argc, jsval *arglist)
376
{
377
378
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
379
380
	char*		cp;
	char*		buf;
381
	int32		len=512;
382
	JSString*	js_str;
383
	private_t*	p;
deuce's avatar
deuce committed
384
	jsrefcount	rc;
385

386
	JS_SET_RVAL(cx, arglist, JSVAL_NULL);
387

388
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
389
		JS_ReportError(cx,getprivate_failure,WHERE);
390
		return(JS_FALSE);
391
	}
392
393
394

	if(p->fp==NULL)
		return(JS_TRUE);
395
	
396
397
398
399
	if(argc) {
		if(!JS_ValueToInt32(cx,argv[0],&len))
			return(JS_FALSE);
	}
400

401
	if((buf=alloca(len))==NULL)
402
403
		return(JS_TRUE);

404
	rc=JS_SUSPENDREQUEST(cx);
405
	if(fgets(buf,len,p->fp)!=NULL) {
406
407
408
409
		len=strlen(buf);
		while(len>0 && (buf[len-1]=='\r' || buf[len-1]=='\n'))
			len--;
		buf[len]=0;
410
411
412
413
		if(p->etx) {
			cp=strchr(buf,p->etx);
			if(cp) *cp=0; 
		}
414
415
		if(p->rot13)
			rot13(buf);
416
		JS_RESUMEREQUEST(cx, rc);
417
		if((js_str=JS_NewStringCopyZ(cx,buf))!=NULL)	/* exception here Feb-12-2005 */
418
			JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
419
420
	} else {
		JS_RESUMEREQUEST(cx, rc);
421
422
423
424
425
426
	}

	return(JS_TRUE);
}

static JSBool
427
js_readbin(JSContext *cx, uintN argc, jsval *arglist)
428
{
429
430
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
431
432
433
	BYTE		*b;
	WORD		*w;
	DWORD		*l;
434
435
	size_t		size=sizeof(DWORD);
	private_t*	p;
436
437
438
	size_t		count=1;
	size_t		retlen;
	void		*buffer=NULL;
rswindell's avatar
rswindell committed
439
	size_t		i;
440
441
    JSObject*	array;
    jsval       v;
deuce's avatar
deuce committed
442
	jsrefcount	rc;
443

444
	JS_SET_RVAL(cx, arglist, INT_TO_JSVAL(-1));
445

446
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
447
		JS_ReportError(cx,getprivate_failure,WHERE);
448
		return(JS_FALSE);
449
	}
450
451
452
453

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

454
455
456
	if(argc) {
		if(!JS_ValueToInt32(cx,argv[0],(int32*)&size))
			return(JS_FALSE);
457
458
459
460
		if(argc>1) {
			if(!JS_ValueToInt32(cx,argv[1],(int32*)&count))
				return(JS_FALSE);
		}
461
	}
462

463
	rc=JS_SUSPENDREQUEST(cx);
464
465
466
	if(size != sizeof(BYTE) && size != sizeof(WORD) && size != sizeof(DWORD)) {
		/* unknown size */
		dbprintf(TRUE, p, "unsupported binary read size: %d",size);
467
		JS_RESUMEREQUEST(cx, rc);
468
469
470
471
472
473
		return(JS_TRUE);
	}

	buffer=malloc(size*count);
	if(buffer==NULL) {
		dbprintf(TRUE, p, "malloc failure of %u bytes", size*count);
474
		JS_RESUMEREQUEST(cx, rc);
475
476
477
478
479
480
481
482
483
484
		return(JS_FALSE);
	}
	b=buffer;
	w=buffer;
	l=buffer;
	retlen=fread(buffer, size, count, p->fp);
	if(count==1) {
		if(retlen==1) {
			switch(size) {
				case sizeof(BYTE):
485
					JS_SET_RVAL(cx, arglist, INT_TO_JSVAL(*b));
486
487
					break;
				case sizeof(WORD):
488
					JS_SET_RVAL(cx, arglist, INT_TO_JSVAL(*w));
489
490
					break;
				case sizeof(DWORD):
491
					JS_SET_RVAL(cx, arglist, UINT_TO_JSVAL(*l));
492
					break;
493
			}
494
495
496
		}
	}
	else {
497
		JS_RESUMEREQUEST(cx, rc);
498
499
500
501
502
503
504
505
506
507
508
    	array = JS_NewArrayObject(cx, 0, NULL);

		for(i=0; i<retlen; i++) {
			switch(size) {
				case sizeof(BYTE):
					v = INT_TO_JSVAL(*(b++));
					break;
				case sizeof(WORD):
					v = INT_TO_JSVAL(*(w++));
					break;
				case sizeof(DWORD):
509
					v=UINT_TO_JSVAL(*(l++));
510
					break;
511
			}
deuce's avatar
deuce committed
512
        	if(!JS_SetElement(cx, array, i, &v)) {
513
				rc=JS_SUSPENDREQUEST(cx);
514
				goto end;
deuce's avatar
deuce committed
515
			}
516
		}
517
    	JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(array));
518
	}
519
520
521

end:
	free(buffer);
522
	JS_RESUMEREQUEST(cx, rc);
523
524
525
526
	return(JS_TRUE);
}

static JSBool
527
js_readall(JSContext *cx, uintN argc, jsval *arglist)
528
{
529
530
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
531
532
    jsint       len=0;
    JSObject*	array;
533
534
	private_t*	p;

535
	JS_SET_RVAL(cx, arglist, JSVAL_NULL);
536

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

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

545
    array = JS_NewArrayObject(cx, 0, NULL);
546

547
    while(!feof(p->fp)) {
548
549
		js_readln(cx, argc, arglist);
		if(JS_RVAL(cx, arglist)==JSVAL_NULL)
550
			break;
551
        if(!JS_SetElement(cx, array, len++, &JS_RVAL(cx, arglist)))
552
			break;
553
	}
554
    JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(array));
555

556
557
    return(JS_TRUE);
}
558

559
560
561
562
563
564
565
566
567
568
569
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++) {
570
		if(*p=='.' && !f)
571
572
573
574
			f=TRUE;
		else if(!isdigit(*p))
			break;
	}
575
576
577
578
579
	if(*p==0) {
		if(f)
			val=DOUBLE_TO_JSVAL(atof(value));
		else
			val=DOUBLE_TO_JSVAL((double)strtoul(value,NULL,10));
580
581
582
583
584
585
586
587
		return(val);
	}
	/* hexadecimal number? */
	if(!strncmp(value,"0x",2)) {	
		for(p=value+2;*p;p++)
			if(!isxdigit(*p))
				break;
		if(*p==0) {	
588
			val=DOUBLE_TO_JSVAL((double)strtoul(value,NULL,0));
589
590
591
592
593
594
595
596
597
598
599
600
601
			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)));
}

deuce's avatar
deuce committed
602
603
604
605
606
607
608
609
610
static double js_DateGetMsecSinceEpoch(JSContext *cx, JSObject *obj)
{
	jsval	rval;

	if(!JS_CallFunctionName(cx, obj, "UTC", 0, NULL, &rval))
		return ((double)time(NULL))*1000;
	return JSVAL_TO_DOUBLE(rval);
}

611
static JSBool
612
js_iniGetValue(JSContext *cx, uintN argc, jsval *arglist)
613
{
614
615
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
rswindell's avatar
rswindell committed
616
	char*	section=ROOT_SECTION;
617
	char*	key;
618
	char**	list;
619
	char	buf[INI_MAX_VALUE_LEN];
620
621
	int32	i;
	jsval	val;
622
623
	jsval	dflt=argv[2];
	private_t*	p;
624
	JSObject*	array;
625
626
	JSObject*	dflt_obj;
	JSObject*	date_obj;
deuce's avatar
deuce committed
627
628
629
630
631
	jsrefcount	rc;
	double		dbl;
	time_t		tt;
	char*		cstr;
	char*		cstr2;
632

633
634
	JS_SET_RVAL(cx, arglist, JSVAL_VOID);

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

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

643
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
deuce's avatar
deuce committed
644
645
		JSVALUE_TO_STRING(cx, argv[0], section);
	JSVALUE_TO_STRING(cx, argv[1], key);
646
647

	if(dflt==JSVAL_VOID) {	/* unspecified default value */
648
		rc=JS_SUSPENDREQUEST(cx);
649
		JS_SET_RVAL(cx, arglist,get_value(cx,iniReadString(p->fp,section,key,NULL,buf)));
650
		JS_RESUMEREQUEST(cx, rc);
651
652
653
		return(JS_TRUE);
	}

deuce's avatar
deuce committed
654
655
656
657
658
659
660
	if(JSVAL_IS_BOOLEAN(dflt)) {
		JS_SET_RVAL(cx,arglist,BOOLEAN_TO_JSVAL(
			iniReadBool(p->fp,section,key,JSVAL_TO_BOOLEAN(dflt))));
	}
	else if(JSVAL_IS_OBJECT(dflt)) {
		if((dflt_obj = JSVAL_TO_OBJECT(dflt))!=NULL && (strcmp("Date",JS_GetClass(cx, dflt_obj)->name)==0)) {
			tt=(time_t)(js_DateGetMsecSinceEpoch(cx,dflt_obj)/1000.0);
661
			rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
662
			dbl=iniReadDateTime(p->fp,section,key,tt);
663
			JS_RESUMEREQUEST(cx, rc);
deuce's avatar
deuce committed
664
665
666
667
668
			date_obj = JS_NewDateObjectMsec(cx, DOUBLE_TO_JSVAL(dbl));
			if(date_obj!=NULL)
				JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(date_obj));
		}
		else {
669
		    array = JS_NewArrayObject(cx, 0, NULL);
deuce's avatar
deuce committed
670
			JSVALUE_TO_STRING(cx, dflt, cstr);
671
			rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
672
			list=iniReadStringList(p->fp,section,key,",",cstr);
673
			JS_RESUMEREQUEST(cx, rc);
674
675
676
677
678
			for(i=0;list && list[i];i++) {
				val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,list[i]));
				if(!JS_SetElement(cx, array, i, &val))
					break;
			}
679
			rc=JS_SUSPENDREQUEST(cx);
680
			iniFreeStringList(list);
681
			JS_RESUMEREQUEST(cx, rc);
682
			JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(array));
deuce's avatar
deuce committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
		}
	}
	else if(JSVAL_IS_DOUBLE(dflt)) {
		rc=JS_SUSPENDREQUEST(cx);
		dbl=iniReadFloat(p->fp,section,key,JSVAL_TO_DOUBLE(dflt));
		JS_RESUMEREQUEST(cx, rc);
		JS_SET_RVAL(cx, arglist,DOUBLE_TO_JSVAL(dbl));
	}
	else if(JSVAL_IS_NUMBER(dflt)) {
		if(!JS_ValueToInt32(cx,dflt,&i))
			return(JS_FALSE);
		rc=JS_SUSPENDREQUEST(cx);
		i=iniReadInteger(p->fp,section,key,i);
		JS_RESUMEREQUEST(cx, rc);
		JS_SET_RVAL(cx, arglist,INT_TO_JSVAL(i));
	} else {
deuce's avatar
deuce committed
699
		JSVALUE_TO_STRING(cx, dflt, cstr);
deuce's avatar
deuce committed
700
701
702
703
		rc=JS_SUSPENDREQUEST(cx);
		cstr2=iniReadString(p->fp,section,key,cstr,buf);
		JS_RESUMEREQUEST(cx, rc);
		JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, cstr2)));
704
705
706
707
708
	}

	return(JS_TRUE);
}

709
static JSBool
deuce's avatar
deuce committed
710
js_iniSetValue_internal(JSContext *cx, JSObject *obj, uintN argc, jsval* argv, jsval *rval)
711
{
rswindell's avatar
rswindell committed
712
	char*	section=ROOT_SECTION;
713
714
715
716
717
718
	char*	key;
	char*	result=NULL;
	int32	i;
	jsval	value=argv[2];
	private_t*	p;
	str_list_t	list;
719
	JSObject*	value_obj;
deuce's avatar
deuce committed
720
721
722
	jsrefcount	rc;
	char*		cstr;
	time_t		tt;
723

deuce's avatar
deuce committed
724
	*rval = JSVAL_FALSE;
725
726
727
728
729
730

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

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

734
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
deuce's avatar
deuce committed
735
736
		JSVALUE_TO_STRING(cx, argv[0], section);
	JSVALUE_TO_STRING(cx, argv[1], key);
737

738
	rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
739
	if((list=iniReadFile(p->fp))==NULL) {
740
		JS_RESUMEREQUEST(cx, rc);
741
		return(JS_TRUE);
deuce's avatar
deuce committed
742
	}
743
	JS_RESUMEREQUEST(cx, rc);
744

deuce's avatar
deuce committed
745
	if(value==JSVAL_VOID) { 	/* unspecified value */
746
		rc=JS_SUSPENDREQUEST(cx);
747
		result = iniSetString(&list,section,key,"",NULL);
748
		JS_RESUMEREQUEST(cx, rc);
deuce's avatar
deuce committed
749
	}
deuce's avatar
deuce committed
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
	else if(JSVAL_IS_BOOLEAN(value)) {
		result = iniSetBool(&list,section,key,JSVAL_TO_BOOLEAN(value),NULL);
	}
	else if(JSVAL_IS_DOUBLE(value)) {
		result = iniSetFloat(&list,section,key,JSVAL_TO_DOUBLE(value),NULL);
	}
	else if(JSVAL_IS_NUMBER(value)) {
		if(!JS_ValueToInt32(cx,value,&i))
			return(JS_FALSE);
		rc=JS_SUSPENDREQUEST(cx);
		result = iniSetInteger(&list,section,key,i,NULL);
		JS_RESUMEREQUEST(cx, rc);
	} else if(JSVAL_IS_OBJECT(value) 
			&& (value_obj = JSVAL_TO_OBJECT(value))!=NULL
			&& (strcmp("Date",JS_GetClass(cx, value_obj)->name)==0)) {
		tt=(time_t)(js_DateGetMsecSinceEpoch(cx,value_obj)/1000.0);
		rc=JS_SUSPENDREQUEST(cx);
		result = iniSetDateTime(&list,section,key,/* include_time */TRUE, tt,NULL);
		JS_RESUMEREQUEST(cx, rc);
	} else {
deuce's avatar
deuce committed
770
		JSVALUE_TO_STRING(cx, value, cstr);
deuce's avatar
deuce committed
771
772
773
		rc=JS_SUSPENDREQUEST(cx);
		result = iniSetString(&list,section,key, cstr,NULL);
		JS_RESUMEREQUEST(cx, rc);
774
775
	}

776
	rc=JS_SUSPENDREQUEST(cx);
777
	if(result != NULL)
deuce's avatar
deuce committed
778
		*rval = BOOLEAN_TO_JSVAL(iniWriteFile(p->fp,list));
779
780

	strListFree(&list);
781
	JS_RESUMEREQUEST(cx, rc);
782
783
784
785

	return(JS_TRUE);
}

deuce's avatar
deuce committed
786
787
788
789
790
791
792
793
794
795
796
797
798
static JSBool
js_iniSetValue(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
	jsval	rval;
	JSBool	ret;

	ret=js_iniSetValue_internal(cx, obj, argc, argv, &rval);
	JS_SET_RVAL(cx, arglist, rval);
	return ret;
}

799
static JSBool
800
js_iniRemoveKey(JSContext *cx, uintN argc, jsval *arglist)
801
{
802
803
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
804
805
806
807
	char*	section=ROOT_SECTION;
	char*	key;
	private_t*	p;
	str_list_t	list;
deuce's avatar
deuce committed
808
	jsrefcount	rc;
809

810
	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
811
812
813
814
815
816
817
818
819
820

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

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

	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
deuce's avatar
deuce committed
821
822
		JSVALUE_TO_STRING(cx, argv[0], section);
	JSVALUE_TO_STRING(cx, argv[1], key);
823

824
	rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
825
	if((list=iniReadFile(p->fp))==NULL) {
826
		JS_RESUMEREQUEST(cx, rc);
827
		return(JS_TRUE);
deuce's avatar
deuce committed
828
	}
829
830

	if(iniRemoveKey(&list,section,key))
831
		JS_SET_RVAL(cx, arglist, BOOLEAN_TO_JSVAL(iniWriteFile(p->fp,list)));
832
833

	strListFree(&list);
834
	JS_RESUMEREQUEST(cx, rc);
835
836
837
838
839

	return(JS_TRUE);
}

static JSBool
840
js_iniRemoveSection(JSContext *cx, uintN argc, jsval *arglist)
841
{
842
843
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
844
845
846
	char*	section=ROOT_SECTION;
	private_t*	p;
	str_list_t	list;
deuce's avatar
deuce committed
847
	jsrefcount	rc;
848

849
	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
850
851
852
853
854
855
856
857
858
859

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

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

	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
deuce's avatar
deuce committed
860
		JSVALUE_TO_STRING(cx, argv[0], section);
861

862
	rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
863
	if((list=iniReadFile(p->fp))==NULL) {
864
		JS_RESUMEREQUEST(cx, rc);
865
		return(JS_TRUE);
deuce's avatar
deuce committed
866
	}
867
868

	if(iniRemoveSection(&list,section))
869
		JS_SET_RVAL(cx, arglist, BOOLEAN_TO_JSVAL(iniWriteFile(p->fp,list)));
870
871

	strListFree(&list);
872
	JS_RESUMEREQUEST(cx, rc);
873
874
875
876

	return(JS_TRUE);
}

877

878
static JSBool
879
js_iniGetSections(JSContext *cx, uintN argc, jsval *arglist)
880
{
881
882
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
883
	char*		prefix=NULL;
884
885
886
887
888
	char**		list;
    jsint       i;
    jsval       val;
    JSObject*	array;
	private_t*	p;
deuce's avatar
deuce committed
889
	jsrefcount	rc;
890

891
	JS_SET_RVAL(cx, arglist, JSVAL_NULL);
892
893
894
895
896
897

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

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

901
	if(argc)
deuce's avatar
deuce committed
902
		JSVALUE_TO_STRING(cx, argv[0], prefix);
903

904
905
    array = JS_NewArrayObject(cx, 0, NULL);

906
	rc=JS_SUSPENDREQUEST(cx);
907
	list = iniReadSectionList(p->fp,prefix);
908
	JS_RESUMEREQUEST(cx, rc);
909
910
911
912
913
    for(i=0;list && list[i];i++) {
		val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,list[i]));
        if(!JS_SetElement(cx, array, i, &val))
			break;
	}
914
	rc=JS_SUSPENDREQUEST(cx);
915
	iniFreeStringList(list);
916
	JS_RESUMEREQUEST(cx, rc);
917

918
    JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(array));
919
920
921
922
923

    return(JS_TRUE);
}

static JSBool
924
js_iniGetKeys(JSContext *cx, uintN argc, jsval *arglist)
925
{
926
927
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
rswindell's avatar
rswindell committed
928
	char*		section=ROOT_SECTION;
929
930
931
932
933
	char**		list;
    jsint       i;
    jsval       val;
    JSObject*	array;
	private_t*	p;
deuce's avatar
deuce committed
934
	jsrefcount	rc;
935

936
	JS_SET_RVAL(cx, arglist, JSVAL_NULL);
937
938
939
940
941
942

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

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

946
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
deuce's avatar
deuce committed
947
		JSVALUE_TO_STRING(cx, argv[0], section);
948
949
    array = JS_NewArrayObject(cx, 0, NULL);

950
	rc=JS_SUSPENDREQUEST(cx);