js_file.c 62.1 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
#include "jsdate.h"	/* Yes, I know this is a private header file */

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

} private_t;

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

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

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

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

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

124
125
	return(flags);
}
126
127
128
129
130
131
132

/* File Object Methods */

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

141
	*rval = JSVAL_FALSE;
142

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

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

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

167
	rc=JS_SUSPENDREQUEST(cx);
168
169
170
171
	if(shareable)
		p->fp=fopen(p->name,p->mode);
	else {
		if((file=nopen(p->name,fopenflags(p->mode)))!=-1) {
172
173
174
175
			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)
176
177
178
				close(file);
		}
	}
179
	if(p->fp!=NULL) {
180
		*rval = JSVAL_TRUE;
181
		dbprintf(FALSE, p, "opened: %s",p->name);
182
183
184
185
186
		if(!bufsize)
			setvbuf(p->fp,NULL,_IONBF,0);	/* no buffering */
		else
			setvbuf(p->fp,NULL,_IOFBF,bufsize);
	}
187
	JS_RESUMEREQUEST(cx, rc);
188
189
190
191

	return(JS_TRUE);
}

192
193
194
195
196
197
198
199
static JSBool
js_popen(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		mode="r+";	/* default mode */
	uintN		i;
	jsint		bufsize=2*1024;
	JSString*	str;
	private_t*	p;
deuce's avatar
deuce committed
200
	jsrefcount	rc;
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

	*rval = JSVAL_FALSE;

	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);
			}
			mode=JS_GetStringBytes(str);
		}
		else if(JSVAL_IS_NUMBER(argv[i])) {	/* bufsize */
			if(!JS_ValueToInt32(cx,argv[i],&bufsize))
				return(JS_FALSE);
		}
	}
	SAFECOPY(p->mode,mode);

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

	return(JS_TRUE);
}
242
243
244
245
246

static JSBool
js_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;
deuce's avatar
deuce committed
247
	jsrefcount	rc;
248

249
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
250
		JS_ReportError(cx,getprivate_failure,WHERE);
251
		return(JS_FALSE);
252
	}
253
254
255
256

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

257
	rc=JS_SUSPENDREQUEST(cx);
258
259
260
261
262
263
#ifdef __unix__
	if(p->pipe)
		pclose(p->fp);
	else
#endif
		fclose(p->fp);
264
265
266
267

	dbprintf(FALSE, p, "closed");

	p->fp=NULL; 
268
	JS_RESUMEREQUEST(cx, rc);
269
270
271
272
273

	return(JS_TRUE);
}

static JSBool
274
js_read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
275
276
{
	char*		cp;
277
	char*		buf;
278
	char*		uubuf;
279
280
	int32		len;
	int32		offset;
281
	int32		uulen;
282
283
	JSString*	str;
	private_t*	p;
deuce's avatar
deuce committed
284
	jsrefcount	rc;
285

286
287
	*rval = JSVAL_NULL;

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

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

296
297
298
299
	if(argc) {
		if(!JS_ValueToInt32(cx,argv[0],&len))
			return(JS_FALSE);
	} else {
300
		rc=JS_SUSPENDREQUEST(cx);
301
302
		len=(long)filelength(fileno(p->fp));
		offset=(long)ftell(p->fp);
303
304
		if(offset>0)
			len-=offset;
305
		JS_RESUMEREQUEST(cx, rc);
306
307
308
	}
	if(len<0)
		len=512;
309

310
	if((buf=malloc(len+1))==NULL)
311
312
		return(JS_TRUE);

313
	rc=JS_SUSPENDREQUEST(cx);
314
315
316
317
318
319
320
321
	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; 
322
		len=strlen(buf);
323
324
	}

325
326
327
	if(p->rot13)
		rot13(buf);

328
	if(p->uuencoded || p->b64encoded || p->yencoded) {
329
		uulen=len*2;
deuce's avatar
deuce committed
330
		if((uubuf=malloc(uulen))==NULL) {
331
			free(buf);
332
			JS_RESUMEREQUEST(cx, rc);
333
			return(JS_TRUE);
334
		}
335
336
		if(p->uuencoded)
			uulen=uuencode(uubuf,uulen,buf,len);
337
338
		else if(p->yencoded)
			uulen=yencode(uubuf,uulen,buf,len);
339
340
		else
			uulen=b64_encode(uubuf,uulen,buf,len);
341
		if(uulen>=0) {
342
			free(buf);
343
			buf=uubuf;
344
			len=uulen;
deuce's avatar
deuce committed
345
		}
346
347
		else
			free(uubuf);
348
	}
349
	JS_RESUMEREQUEST(cx, rc);
350

351
	str = JS_NewStringCopyN(cx, buf, len);
352
	free(buf);
353

354
355
356
	if(str==NULL)
		return(JS_FALSE);

357
358
	*rval = STRING_TO_JSVAL(str);

359
	rc=JS_SUSPENDREQUEST(cx);
360
	dbprintf(FALSE, p, "read %u bytes",len);
361
	JS_RESUMEREQUEST(cx, rc);
362
363
364
365
366
		
	return(JS_TRUE);
}

static JSBool
367
js_readln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
368
{
369
370
	char*		cp;
	char*		buf;
371
	int32		len=512;
372
	JSString*	js_str;
373
	private_t*	p;
deuce's avatar
deuce committed
374
	jsrefcount	rc;
375

376
	*rval = JSVAL_NULL;
377

378
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
379
		JS_ReportError(cx,getprivate_failure,WHERE);
380
		return(JS_FALSE);
381
	}
382
383
384

	if(p->fp==NULL)
		return(JS_TRUE);
385
	
386
387
388
389
	if(argc) {
		if(!JS_ValueToInt32(cx,argv[0],&len))
			return(JS_FALSE);
	}
390

391
	if((buf=alloca(len))==NULL)
392
393
		return(JS_TRUE);

394
	rc=JS_SUSPENDREQUEST(cx);
395
	if(fgets(buf,len,p->fp)!=NULL) {
396
397
398
399
		len=strlen(buf);
		while(len>0 && (buf[len-1]=='\r' || buf[len-1]=='\n'))
			len--;
		buf[len]=0;
400
401
402
403
		if(p->etx) {
			cp=strchr(buf,p->etx);
			if(cp) *cp=0; 
		}
404
405
		if(p->rot13)
			rot13(buf);
406
		JS_RESUMEREQUEST(cx, rc);
407
408
		if((js_str=JS_NewStringCopyZ(cx,buf))!=NULL)	/* exception here Feb-12-2005 */
			*rval = STRING_TO_JSVAL(js_str);			/* _CrtDbgBreak from _heap_alloc_dbg */
409
410
	} else {
		JS_RESUMEREQUEST(cx, rc);
411
412
413
414
415
416
	}

	return(JS_TRUE);
}

static JSBool
417
js_readbin(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
418
{
419
420
421
	BYTE		*b;
	WORD		*w;
	DWORD		*l;
422
423
	size_t		size=sizeof(DWORD);
	private_t*	p;
424
425
426
	size_t		count=1;
	size_t		retlen;
	void		*buffer=NULL;
rswindell's avatar
rswindell committed
427
	size_t		i;
428
429
    JSObject*	array;
    jsval       v;
deuce's avatar
deuce committed
430
	jsrefcount	rc;
431

432
	*rval = INT_TO_JSVAL(-1);
433

434
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
435
		JS_ReportError(cx,getprivate_failure,WHERE);
436
		return(JS_FALSE);
437
	}
438
439
440
441

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

442
443
444
	if(argc) {
		if(!JS_ValueToInt32(cx,argv[0],(int32*)&size))
			return(JS_FALSE);
445
446
447
448
		if(argc>1) {
			if(!JS_ValueToInt32(cx,argv[1],(int32*)&count))
				return(JS_FALSE);
		}
449
	}
450

451
	rc=JS_SUSPENDREQUEST(cx);
452
453
454
	if(size != sizeof(BYTE) && size != sizeof(WORD) && size != sizeof(DWORD)) {
		/* unknown size */
		dbprintf(TRUE, p, "unsupported binary read size: %d",size);
455
		JS_RESUMEREQUEST(cx, rc);
456
457
458
459
460
461
		return(JS_TRUE);
	}

	buffer=malloc(size*count);
	if(buffer==NULL) {
		dbprintf(TRUE, p, "malloc failure of %u bytes", size*count);
462
		JS_RESUMEREQUEST(cx, rc);
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
		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):
					*rval = INT_TO_JSVAL(*b);
					break;
				case sizeof(WORD):
					*rval = INT_TO_JSVAL(*w);
					break;
				case sizeof(DWORD):
479
					JS_RESUMEREQUEST(cx, rc);
480
					JS_NewNumberValue(cx,*l,rval);
481
					rc=JS_SUSPENDREQUEST(cx);
482
					break;
483
			}
484
485
486
		}
	}
	else {
487
		JS_RESUMEREQUEST(cx, rc);
488
489
490
491
492
493
494
495
496
497
498
499
500
    	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):
					JS_NewNumberValue(cx,*(l++),&v);
					break;
501
			}
deuce's avatar
deuce committed
502
        	if(!JS_SetElement(cx, array, i, &v)) {
503
				rc=JS_SUSPENDREQUEST(cx);
504
				goto end;
deuce's avatar
deuce committed
505
			}
506
507
		}
    	*rval = OBJECT_TO_JSVAL(array);
508
	}
509
510
511

end:
	free(buffer);
512
	JS_RESUMEREQUEST(cx, rc);
513
514
515
516
	return(JS_TRUE);
}

static JSBool
517
js_readall(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
518
{
519
520
521
    jsint       len=0;
    jsval       line;
    JSObject*	array;
522
523
	private_t*	p;

524
525
	*rval = JSVAL_NULL;

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

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

534
    array = JS_NewArrayObject(cx, 0, NULL);
535

536
    while(!feof(p->fp)) {
537
		js_readln(cx, obj, argc, argv, &line);
538
539
		if(line==JSVAL_NULL)
			break;
540
541
        if(!JS_SetElement(cx, array, len++, &line))
			break;
542
	}
543
    *rval = OBJECT_TO_JSVAL(array);
544

545
546
    return(JS_TRUE);
}
547

548
549
550
551
552
553
554
555
556
557
558
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++) {
559
		if(*p=='.' && !f)
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
			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)));
}

588
static JSBool
589
js_iniGetValue(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
590
{
rswindell's avatar
rswindell committed
591
	char*	section=ROOT_SECTION;
592
	char*	key;
593
	char**	list;
594
	char	buf[INI_MAX_VALUE_LEN];
595
596
	int32	i;
	jsval	val;
597
598
	jsval	dflt=argv[2];
	private_t*	p;
599
	JSObject*	array;
600
601
	JSObject*	dflt_obj;
	JSObject*	date_obj;
deuce's avatar
deuce committed
602
603
604
605
606
	jsrefcount	rc;
	double		dbl;
	time_t		tt;
	char*		cstr;
	char*		cstr2;
607
608
609
610
611
612

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

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

616
617
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
618
	key=JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
619
620

	if(dflt==JSVAL_VOID) {	/* unspecified default value */
621
		rc=JS_SUSPENDREQUEST(cx);
622
		*rval=get_value(cx,iniReadString(p->fp,section,key,NULL,buf));
623
		JS_RESUMEREQUEST(cx, rc);
624
625
626
		return(JS_TRUE);
	}

627
628
629
	switch(JSVAL_TAG(dflt)) {
		case JSVAL_BOOLEAN:
			*rval = BOOLEAN_TO_JSVAL(
630
				iniReadBool(p->fp,section,key,JSVAL_TO_BOOLEAN(dflt)));
631
632
			break;
		case JSVAL_DOUBLE:
633
			rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
634
			dbl=iniReadFloat(p->fp,section,key,*JSVAL_TO_DOUBLE(dflt));
635
			JS_RESUMEREQUEST(cx, rc);
636
			JS_NewNumberValue(cx
deuce's avatar
deuce committed
637
				,dbl,rval);
638
			break;
639
		case JSVAL_OBJECT:
640
			if((dflt_obj = JSVAL_TO_OBJECT(dflt))!=NULL && js_DateIsValid(cx, dflt_obj)) {
deuce's avatar
deuce committed
641
				tt=(time_t)(js_DateGetMsecSinceEpoch(cx,dflt_obj)/1000.0);
642
				rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
643
				dbl=iniReadDateTime(p->fp,section,key,tt);
644
				JS_RESUMEREQUEST(cx, rc);
deuce's avatar
deuce committed
645
				date_obj = js_NewDateObjectMsec(cx, dbl);
646
647
648
649
				if(date_obj!=NULL)
					*rval = OBJECT_TO_JSVAL(date_obj);
				break;
			}
650
		    array = JS_NewArrayObject(cx, 0, NULL);
deuce's avatar
deuce committed
651
			cstr=JS_GetStringBytes(JS_ValueToString(cx,dflt));
652
			rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
653
			list=iniReadStringList(p->fp,section,key,",",cstr);
654
			JS_RESUMEREQUEST(cx, rc);
655
656
657
658
659
			for(i=0;list && list[i];i++) {
				val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,list[i]));
				if(!JS_SetElement(cx, array, i, &val))
					break;
			}
660
			rc=JS_SUSPENDREQUEST(cx);
661
			iniFreeStringList(list);
662
			JS_RESUMEREQUEST(cx, rc);
663
664
			*rval = OBJECT_TO_JSVAL(array);
			break;
665
		default:
666
			if(JSVAL_IS_NUMBER(dflt)) {
667
668
				if(!JS_ValueToInt32(cx,dflt,&i))
					return(JS_FALSE);
669
				rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
670
				i=iniReadInteger(p->fp,section,key,i);
671
				JS_RESUMEREQUEST(cx, rc);
deuce's avatar
deuce committed
672
673
674
				JS_NewNumberValue(cx,i,rval);
			} else {
				cstr=JS_GetStringBytes(JS_ValueToString(cx,dflt));
675
				rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
676
				cstr2=iniReadString(p->fp,section,key,cstr,buf);
677
				JS_RESUMEREQUEST(cx, rc);
deuce's avatar
deuce committed
678
679
				*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, cstr2));
			}
680
681
682
683
684
685
			break;
	}

	return(JS_TRUE);
}

686
687
688
static JSBool
js_iniSetValue(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
rswindell's avatar
rswindell committed
689
	char*	section=ROOT_SECTION;
690
691
692
693
694
695
	char*	key;
	char*	result=NULL;
	int32	i;
	jsval	value=argv[2];
	private_t*	p;
	str_list_t	list;
696
	JSObject*	value_obj;
deuce's avatar
deuce committed
697
698
699
	jsrefcount	rc;
	char*		cstr;
	time_t		tt;
700
701
702
703
704
705
706
707

	*rval = JSVAL_FALSE;

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

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

711
712
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
713
714
	key=JS_GetStringBytes(JS_ValueToString(cx, argv[1]));

715
	rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
716
	if((list=iniReadFile(p->fp))==NULL) {
717
		JS_RESUMEREQUEST(cx, rc);
718
		return(JS_TRUE);
deuce's avatar
deuce committed
719
	}
720
	JS_RESUMEREQUEST(cx, rc);
721

deuce's avatar
deuce committed
722
	if(value==JSVAL_VOID) { 	/* unspecified value */
723
		rc=JS_SUSPENDREQUEST(cx);
724
		result = iniSetString(&list,section,key,"",NULL);
725
		JS_RESUMEREQUEST(cx, rc);
deuce's avatar
deuce committed
726
	}
727
728
729
730
731
732
733
734
735
736
737
	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)) {
738
739
					if(!JS_ValueToInt32(cx,value,&i))
						return(JS_FALSE);
740
					rc=JS_SUSPENDREQUEST(cx);
741
					result = iniSetInteger(&list,section,key,i,NULL);
742
					JS_RESUMEREQUEST(cx, rc);
743
744
745
746
				} else {
					if(JSVAL_IS_OBJECT(value) 
						&& (value_obj = JSVAL_TO_OBJECT(value))!=NULL
						&& js_DateIsValid(cx, value_obj)) {
deuce's avatar
deuce committed
747
						tt=(time_t)(js_DateGetMsecSinceEpoch(cx,value_obj)/1000.0);
748
						rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
749
						result = iniSetDateTime(&list,section,key,/* include_time */TRUE, tt,NULL);
750
						JS_RESUMEREQUEST(cx, rc);
deuce's avatar
deuce committed
751
752
					} else {
						cstr=JS_GetStringBytes(JS_ValueToString(cx,value));
753
						rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
754
						result = iniSetString(&list,section,key, cstr,NULL);
755
						JS_RESUMEREQUEST(cx, rc);
deuce's avatar
deuce committed
756
					}
757
				}
758
759
760
761
				break;
		}
	}

762
	rc=JS_SUSPENDREQUEST(cx);
763
764
765
766
	if(result != NULL)
		*rval = BOOLEAN_TO_JSVAL(iniWriteFile(p->fp,list));

	strListFree(&list);
767
	JS_RESUMEREQUEST(cx, rc);
768
769
770
771

	return(JS_TRUE);
}

772
773
774
775
776
777
778
static JSBool
js_iniRemoveKey(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*	section=ROOT_SECTION;
	char*	key;
	private_t*	p;
	str_list_t	list;
deuce's avatar
deuce committed
779
	jsrefcount	rc;
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794

	*rval = JSVAL_FALSE;

	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)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
	key=JS_GetStringBytes(JS_ValueToString(cx, argv[1]));

795
	rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
796
	if((list=iniReadFile(p->fp))==NULL) {
797
		JS_RESUMEREQUEST(cx, rc);
798
		return(JS_TRUE);
deuce's avatar
deuce committed
799
	}
800
801
802
803
804

	if(iniRemoveKey(&list,section,key))
		*rval = BOOLEAN_TO_JSVAL(iniWriteFile(p->fp,list));

	strListFree(&list);
805
	JS_RESUMEREQUEST(cx, rc);
806
807
808
809
810
811
812
813
814
815

	return(JS_TRUE);
}

static JSBool
js_iniRemoveSection(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*	section=ROOT_SECTION;
	private_t*	p;
	str_list_t	list;
deuce's avatar
deuce committed
816
	jsrefcount	rc;
817
818
819
820
821
822
823
824
825
826
827
828
829
830

	*rval = JSVAL_FALSE;

	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)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));

831
	rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
832
	if((list=iniReadFile(p->fp))==NULL) {
833
		JS_RESUMEREQUEST(cx, rc);
834
		return(JS_TRUE);
deuce's avatar
deuce committed
835
	}
836
837
838
839
840

	if(iniRemoveSection(&list,section))
		*rval = BOOLEAN_TO_JSVAL(iniWriteFile(p->fp,list));

	strListFree(&list);
841
	JS_RESUMEREQUEST(cx, rc);
842
843
844
845

	return(JS_TRUE);
}

846

847
848
849
static JSBool
js_iniGetSections(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
850
	char*		prefix=NULL;
851
852
853
854
855
	char**		list;
    jsint       i;
    jsval       val;
    JSObject*	array;
	private_t*	p;
deuce's avatar
deuce committed
856
	jsrefcount	rc;
857
858
859
860
861
862
863
864

	*rval = JSVAL_NULL;

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

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

868
869
870
	if(argc)
		prefix=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));

871
872
    array = JS_NewArrayObject(cx, 0, NULL);

873
	rc=JS_SUSPENDREQUEST(cx);
874
	list = iniReadSectionList(p->fp,prefix);
875
	JS_RESUMEREQUEST(cx, rc);
876
877
878
879
880
    for(i=0;list && list[i];i++) {
		val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,list[i]));
        if(!JS_SetElement(cx, array, i, &val))
			break;
	}
881
	rc=JS_SUSPENDREQUEST(cx);
882
	iniFreeStringList(list);
883
	JS_RESUMEREQUEST(cx, rc);
884
885
886
887
888
889
890
891
892

    *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
893
	char*		section=ROOT_SECTION;
894
895
896
897
898
	char**		list;
    jsint       i;
    jsval       val;
    JSObject*	array;
	private_t*	p;
deuce's avatar
deuce committed
899
	jsrefcount	rc;
900
901
902
903
904
905
906
907

	*rval = JSVAL_NULL;

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

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

911
912
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
913
914
    array = JS_NewArrayObject(cx, 0, NULL);

915
	rc=JS_SUSPENDREQUEST(cx);
916
	list = iniReadKeyList(p->fp,section);
917
	JS_RESUMEREQUEST(cx, rc);
918
919
920
921
922
    for(i=0;list && list[i];i++) {
		val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,list[i]));
        if(!JS_SetElement(cx, array, i, &val))
			break;
	}
923
	rc=JS_SUSPENDREQUEST(cx);
924
	iniFreeStringList(list);
925
	JS_RESUMEREQUEST(cx, rc);
926
927
928
929
930
931
932
933
934

    *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
935
	char*		section=ROOT_SECTION;
936
937
938
    jsint       i;
    JSObject*	object;
	private_t*	p;
939
	named_string_t** list;
deuce's avatar
deuce committed
940
	jsrefcount	rc;
941
942
943
944
945
946
947
948

	*rval = JSVAL_NULL;

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

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

952
953
	if(argv[0]!=JSVAL_VOID && argv[0]!=JSVAL_NULL)
		section=JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
954

955
	rc=JS_SUSPENDREQUEST(cx);
956
	list = iniReadNamedStringList(p->fp,section);
957
	JS_RESUMEREQUEST(cx, rc);
958

959
960
961
962
	if(list==NULL) {	/* New behavior at request of MCMLXXIX: return NULL/undefined if specified section doesn't exist */
		*rval = JSVAL_NULL;
		return(JS_TRUE);
	}
963
964
965

    object = JS_NewObject(cx, NULL, NULL, obj);

966
    for(i=0;list && list[i];i++) {
967
		JS_DefineProperty(cx, object, list[i]->name
968
			,get_value(cx,list[i]->value)
969
970
971
			,NULL,NULL,JSPROP_ENUMERATE);

	}
972
	rc=JS_SUSPENDREQUEST(cx);
973
	iniFreeNamedStringList(list);
974
	JS_RESUMEREQUEST(cx, rc);
975
976
977
978
979
980

    *rval = OBJECT_TO_JSVAL(object);

    return(JS_TRUE);
}

981
982
983
984
985
986
987
988
989
990
991
992
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 */

993
	if(!JSVAL_IS_OBJECT(argv[1]) || argv[1]==JSVAL_NULL)
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
		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;
	}

1010
1011
	JS_DestroyIdArray(cx,id_array);

1012
1013
1014
1015
    return(JS_TRUE);
}


1016
1017
1018
1019
static JSBool
js_iniGetAllObjects(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		name="name";
1020
	char*		sec_name;
1021
1022
1023
1024
1025
1026
1027
1028
	char*		prefix=NULL;
	char**		sec_list;
    jsint       i,k;
    js