js_file.c 25 KB
Newer Older
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
/* 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)		*
 *																			*
 * Copyright 2001 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * 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"

#ifdef JAVASCRIPT

typedef struct
{
44
	FILE*	fp;
45
46
	char	name[MAX_PATH+1];
	char	mode[4];
47
	uchar	etx;
48
49
50
51
52
	BOOL	external;	/* externally created, don't close */
	BOOL	debug;

} private_t;

53
static const char* getprivate_failure = "line %d %s JS_GetPrivate failed";
54
55
56
57
58
59
60
61
62
63

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);
64
65
    vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;
66
67
    va_end(argptr);
	
68
	lprintf("%04u File %s%s",p->fp ? fileno(p->fp) : 0,error ? "ERROR: ":"",sbuf);
69
70
}

71
72
73
74
75
76
77
78
/* 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;
79
80
	else
		flags|=O_TEXT;
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

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

	return(flags);
}
109
110
111
112
113
114
115

/* File Object Methods */

static JSBool
js_open(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		mode="w+";	/* default mode */
116
117
	BOOL		shareable=FALSE;
	int			file;
118
	uintN		i;
119
	jsint		bufsize=2*1024;
120
121
122
	JSString*	str;
	private_t*	p;

123
	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
124

125
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
126
		JS_ReportError(cx,getprivate_failure,WHERE);
127
		return(JS_FALSE);
128
	}
129
130
131

	if(p->fp!=NULL)  
		return(JS_TRUE);
132
133
134
135

	for(i=0;i<argc;i++) {
		if(JSVAL_IS_STRING(argv[i])) {	/* mode */
			if((str = JS_ValueToString(cx, argv[i]))==NULL) {
136
137
				JS_ReportError(cx,"Invalid mode specified: %s",str);
				return(JS_TRUE);
138
139
			}
			mode=JS_GetStringBytes(str);
140
141
142
		} else if(JSVAL_IS_BOOLEAN(argv[i]))	/* shareable */
			shareable=JSVAL_TO_BOOLEAN(argv[i]);
		else	/* bufsize */
143
			JS_ValueToInt32(cx,argv[i],&bufsize);
144
	}
145
	SAFECOPY(p->mode,mode);
146

147
148
149
150
151
152
153
154
	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);
		}
	}
155
156
	if(p->fp!=NULL) {
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);
157
		dbprintf(FALSE, p, "opened: %s",p->name);
158
159
160
161
162
		if(!bufsize)
			setvbuf(p->fp,NULL,_IONBF,0);	/* no buffering */
		else
			setvbuf(p->fp,NULL,_IOFBF,bufsize);
	}
163
164
165
166
167
168
169
170
171
172

	return(JS_TRUE);
}


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

173
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
174
		JS_ReportError(cx,getprivate_failure,WHERE);
175
		return(JS_FALSE);
176
	}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

	*rval = JSVAL_VOID;

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

	fclose(p->fp);

	dbprintf(FALSE, p, "closed");

	p->fp=NULL; 

	return(JS_TRUE);
}

static JSBool
193
js_read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
194
195
{
	char*		cp;
196
	char*		buf;
197
	int32		len=512;
198
199
200
	JSString*	str;
	private_t*	p;

201
202
	*rval = JSVAL_NULL;

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

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

211
	if(argc)
212
		JS_ValueToInt32(cx,argv[0],&len);
213

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
	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; 
	}

	str = JS_NewStringCopyZ(cx, buf);

	free(buf);

	*rval = STRING_TO_JSVAL(str);

	dbprintf(FALSE, p, "read %u bytes",len);
234
235
236
237
238
		
	return(JS_TRUE);
}

static JSBool
239
js_readln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
240
{
241
242
	char*		cp;
	char*		buf;
243
	int32		len=512;
244
245
	private_t*	p;

246
	*rval = JSVAL_NULL;
247

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

	if(p->fp==NULL)
		return(JS_TRUE);
255
256
	
	if(argc)
257
		JS_ValueToInt32(cx,argv[0],&len);
258

259
260
261
262
	if((buf=malloc(len))==NULL)
		return(JS_TRUE);

	if(fgets(buf,len,p->fp)!=NULL) {
263
264
265
266
		len=strlen(buf);
		while(len>0 && (buf[len-1]=='\r' || buf[len-1]=='\n'))
			len--;
		buf[len]=0;
267
268
269
270
271
		if(p->etx) {
			cp=strchr(buf,p->etx);
			if(cp) *cp=0; 
		}
		*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,buf));
272
273
	}

274
275
	free(buf);

276
277
278
279
	return(JS_TRUE);
}

static JSBool
280
js_readbin(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
281
282
283
284
285
286
287
{
	BYTE		b;
	WORD		w;
	DWORD		l;
	size_t		size=sizeof(DWORD);
	private_t*	p;

288
	*rval = INT_TO_JSVAL(-1);
289

290
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
291
		JS_ReportError(cx,getprivate_failure,WHERE);
292
		return(JS_FALSE);
293
	}
294
295
296
297

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

298
	if(argc) 
299
		JS_ValueToInt32(cx,argv[0],(int32*)&size);
300
301
302

	switch(size) {
		case sizeof(BYTE):
303
304
			if(fread(&b,1,size,p->fp)==size)
				*rval = INT_TO_JSVAL(b);
305
306
			break;
		case sizeof(WORD):
307
308
			if(fread(&w,1,size,p->fp)==size)
				*rval = INT_TO_JSVAL(w);
309
310
			break;
		case sizeof(DWORD):
311
312
			if(fread(&l,1,size,p->fp)==size)
				*rval = INT_TO_JSVAL(l);
313
314
315
316
317
318
319
			break;
	}
		
	return(JS_TRUE);
}

static JSBool
320
js_readall(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
321
{
322
323
324
    jsint       len=0;
    jsval       line;
    JSObject*	array;
325
326
	private_t*	p;

327
328
	*rval = JSVAL_NULL;

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

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

337
    array = JS_NewArrayObject(cx, 0, NULL);
338

339
340
341
342
    while(!feof(p->fp)) {
		js_readln(cx, obj, 0, NULL, &line); 
		if(line==JSVAL_NULL)
			break;
343
344
        if(!JS_SetElement(cx, array, len++, &line))
			break;
345
	}
346
    *rval = OBJECT_TO_JSVAL(array);
347

348
349
    return(JS_TRUE);
}
350

351
352
353
354
static JSBool
js_write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		cp;
355
356
	size_t		len;	/* string length */
	size_t		tlen;	/* total length to write (may be greater than len) */
357
358
	JSString*	str;
	private_t*	p;
359

360
361
	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

362
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
363
		JS_ReportError(cx,getprivate_failure,WHERE);
364
		return(JS_FALSE);
365
	}
366
367
368
369
370
371
372

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

	str = JS_ValueToString(cx, argv[0]);
	cp = JS_GetStringBytes(str);
	len = strlen(cp);
373
374
	tlen = len;
	if(argc>1) {
375
		JS_ValueToInt32(cx,argv[1],(int32*)&tlen);
376
377
378
		if(len>tlen)
			len=tlen;
	}
379
	if(fwrite(cp,1,len,p->fp)==len) {
380
381
382
383
384
385
386
387
388
389
390
391
		if(tlen>len) {
			len=tlen-len;
			if((cp=malloc(len))==NULL) {
				dbprintf(TRUE, p, "malloc failure of %u bytes", len);
				*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
				return(JS_TRUE);
			}
			memset(cp,p->etx,len);
			fwrite(cp,1,len,p->fp);
			free(cp);
		}
		dbprintf(FALSE, p, "wrote %u bytes",tlen);
392
393
394
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);
	} else 
		dbprintf(TRUE, p, "write of %u bytes failed",len);
395
396
397
398
399
		
	return(JS_TRUE);
}

static JSBool
400
js_writeln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
401
{
402
403
404
405
406
407
	char*		cp="";
	JSString*	str;
	private_t*	p;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

408
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
409
		JS_ReportError(cx,getprivate_failure,WHERE);
410
		return(JS_FALSE);
411
	}
412
413
414
415
416

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

	if(argc) {
417
418
		if((str = JS_ValueToString(cx, argv[0]))==NULL) {
			JS_ReportError(cx,"JS_ValueToString failed");
419
			return(JS_FALSE);
420
		}
421
422
423
424
425
426
		cp = JS_GetStringBytes(str);
	}

	if(fprintf(p->fp,"%s\n",cp)!=0)
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);

427
428
	return(JS_TRUE);
}
429

430
static JSBool
431
js_writebin(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
432
{
433
434
435
	BYTE		b;
	WORD		w;
	DWORD		l;
436
	int32		val=0;
437
	size_t		wr=0;
438
	size_t		size=sizeof(DWORD);
439
440
	private_t*	p;

441
	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
442

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

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

451
	if(argc>1) 
452
		JS_ValueToInt32(cx,argv[1],(int32*)&size);
453

454
455
	switch(size) {
		case sizeof(BYTE):
456
457
			JS_ValueToInt32(cx,argv[0],&val);
			b = (BYTE)val;
458
			wr=fwrite(&b,1,size,p->fp);
459
460
			break;
		case sizeof(WORD):
461
462
			JS_ValueToInt32(cx,argv[0],&val);
			w = (WORD)val;
463
			wr=fwrite(&w,1,size,p->fp);
464
465
			break;
		case sizeof(DWORD):
466
467
			JS_ValueToInt32(cx,argv[0],&val);
			l = val;
468
469
470
471
472
			wr=fwrite(&l,1,size,p->fp);
			break;
		default:	
			/* unknown size */
			dbprintf(TRUE, p, "unsupported binary write size: %d",size);
473
474
			break;
	}
475
476
	if(wr==size)
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);
477
		
478
479
480
	return(JS_TRUE);
}

481
482
483
484
485
486
487
488
489
490
491
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;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

492
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
493
		JS_ReportError(cx,getprivate_failure,WHERE);
494
		return(JS_FALSE);
495
	}
496
497
498
499

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

500
	if(!JSVAL_IS_OBJECT(argv[0]))
501
502
503
504
		return(JS_TRUE);

    array = JSVAL_TO_OBJECT(argv[0]);

505
506
507
    if(!JS_IsArrayObject(cx, array))
		return(JS_TRUE);

508
509
    JS_GetArrayLength(cx, array, &limit);

510
    *rval = BOOLEAN_TO_JSVAL(JS_TRUE);
511

512
513
514
515
    for(i=0;i<limit;i++) {
        if(!JS_GetElement(cx, array, i, &elemval))
			break;
        js_writeln(cx, obj, 1, &elemval, rval);
516
		if(*rval!=BOOLEAN_TO_JSVAL(JS_TRUE))
517
			break;
518
519
520
521
522
    }

    return(JS_TRUE);
}

523
static JSBool
524
js_lock(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
525
{
526
527
	int32		offset=0;
	int32		len=0;
528
529
530
531
	private_t*	p;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

532
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
533
		JS_ReportError(cx,getprivate_failure,WHERE);
534
		return(JS_FALSE);
535
	}
536
537
538
539

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

540
541
542
543
544
545
546
547
548
549
550
551
	/* 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)
552
553
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);

554
555
	return(JS_TRUE);
}
556

557
static JSBool
558
js_unlock(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
559
{
560
561
	int32		offset=0;
	int32		len=0;
562
563
564
565
	private_t*	p;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

566
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
567
		JS_ReportError(cx,getprivate_failure,WHERE);
568
		return(JS_FALSE);
569
	}
570
571
572
573

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

574
575
576
577
578
579
580
581
582
583
584
	/* 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;

585
	if(unlock(fileno(p->fp),offset,len)==0)
586
587
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);

588
589
590
	return(JS_TRUE);
}

591
592
593
594
595
static JSBool
js_delete(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;

596
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
597
		JS_ReportError(cx,getprivate_failure,WHERE);
598
		return(JS_FALSE);
599
	}
600
601
602
603
604
605
606
607
608
609

	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);
}
610
611
612
613
614
615

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

616
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
617
		JS_ReportError(cx,getprivate_failure,WHERE);
618
		return(JS_FALSE);
619
	}
620
621
622
623
624
625
626
627
628
629
630
631
632
633

	if(p->fp==NULL)
		*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
	else 
		*rval = BOOLEAN_TO_JSVAL(fflush(p->fp)==0);

	return(JS_TRUE);
}

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

634
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
635
		JS_ReportError(cx,getprivate_failure,WHERE);
636
		return(JS_FALSE);
637
	}
638
639
640
641
642
643
644
645
646
647
648

	if(p->fp==NULL)
		*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
	else  {
		clearerr(p->fp);
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);
	}

	return(JS_TRUE);
}

649
650
651
652
653
654
655
656
657
658
659
660
static JSBool
js_fprintf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		cp;
    uintN		i;
	JSString *	fmt;
    JSString *	str;
	va_list		arglist[64];
	private_t*	p;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

661
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
662
		JS_ReportError(cx,getprivate_failure,WHERE);
663
		return(JS_FALSE);
664
	}
665
666
667
668

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

669
670
	if((fmt=JS_ValueToString(cx, argv[0]))==NULL) {
		JS_ReportError(cx,"JS_ValueToString failed");
671
		return(JS_FALSE);
672
	}
673
674
675
676
677

	memset(arglist,0,sizeof(arglist));	/* Initialize arglist to NULLs */

    for (i = 1; i < argc && i<sizeof(arglist)/sizeof(arglist[0]); i++) {
		if(JSVAL_IS_STRING(argv[i])) {
678
679
			if((str=JS_ValueToString(cx, argv[i]))==NULL) {
				JS_ReportError(cx,"JS_ValueToString failed");
680
			    return(JS_FALSE);
681
			}
682
683
684
685
686
687
688
689
690
691
			arglist[i-1]=JS_GetStringBytes(str);	/* exception here July-29-2002 */
		}
		else if(JSVAL_IS_DOUBLE(argv[i]))
			arglist[i-1]=(char*)(unsigned long)*JSVAL_TO_DOUBLE(argv[i]);
		else if(JSVAL_IS_INT(argv[i]) || JSVAL_IS_BOOLEAN(argv[i]))
			arglist[i-1]=(char *)JSVAL_TO_INT(argv[i]);
		else
			arglist[i-1]=NULL;
	}

692
693
	if((cp=JS_vsmprintf(JS_GetStringBytes(fmt),(char*)arglist))==NULL) {
		JS_ReportError(cx,"JS_vsmprintf failed");
694
		return(JS_FALSE);
695
	}
696
697
698
699
700
701
702

	*rval = INT_TO_JSVAL(fwrite(cp,1,strlen(cp),p->fp));
	JS_smprintf_free(cp);
	
    return(JS_TRUE);
}

703
704
705
706

/* File Object Properites */
enum {
	 FILE_PROP_NAME		
707
708
	,FILE_PROP_MODE
	,FILE_PROP_ETX
709
710
711
712
713
714
715
716
717
718
719
720
	,FILE_PROP_EXISTS	
	,FILE_PROP_DATE		
	,FILE_PROP_IS_OPEN	
	,FILE_PROP_EOF		
	,FILE_PROP_ERROR	
	,FILE_PROP_DESCRIPTOR
	,FILE_PROP_DEBUG	
	,FILE_PROP_POSITION	
	,FILE_PROP_LENGTH	
	,FILE_PROP_ATTRIBUTES
};

rswindell's avatar
rswindell committed
721

722
723
724
725
726
static JSBool js_file_set(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
    jsint       tiny;
	private_t*	p;

727
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
728
		JS_ReportError(cx,getprivate_failure,WHERE);
729
		return(JS_FALSE);
730
	}
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750

    tiny = JSVAL_TO_INT(id);

	dbprintf(FALSE, p, "setting property %d",tiny);

	switch(tiny) {
		case FILE_PROP_DEBUG:
			p->debug = JSVAL_TO_BOOLEAN(*vp);
			break;
		case FILE_PROP_POSITION:
			if(p->fp!=NULL)
				fseek(p->fp,JSVAL_TO_INT(*vp),SEEK_SET);
			break;
		case FILE_PROP_LENGTH:
			if(p->fp!=NULL)
				chsize(fileno(p->fp),JSVAL_TO_INT(*vp));
			break;
		case FILE_PROP_ATTRIBUTES:
			CHMOD(p->name,JSVAL_TO_INT(*vp));
			break;
751
752
753
		case FILE_PROP_ETX:
			p->etx = (uchar)JSVAL_TO_INT(*vp);
			break;
754
755
756
757
758
759
760
761
762
763
	}

	return(JS_TRUE);
}

static JSBool js_file_get(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
    jsint       tiny;
	private_t*	p;

764
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
765
		JS_ReportError(cx,getprivate_failure,WHERE);
766
		return(JS_FALSE);
767
	}
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794

    tiny = JSVAL_TO_INT(id);

#if 0 /* just too much */
	dbprintf(FALSE, sock, "getting property %d",tiny);
#endif

	switch(tiny) {
		case FILE_PROP_NAME:
			*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, p->name));
			break;
		case FILE_PROP_MODE:
			*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, p->mode));
			break;
		case FILE_PROP_EXISTS:
			if(p->fp)	/* open? */
				*vp = BOOLEAN_TO_JSVAL(JS_TRUE);
			else
				*vp = BOOLEAN_TO_JSVAL(fexist(p->name));
			break;
		case FILE_PROP_DATE:
			*vp = INT_TO_JSVAL(fdate(p->name));
			break;
		case FILE_PROP_IS_OPEN:
			*vp = BOOLEAN_TO_JSVAL(p->fp!=NULL);
			break;
		case FILE_PROP_EOF:
795
796
797
798
			if(p->fp)
				*vp = BOOLEAN_TO_JSVAL(feof(p->fp)!=0);
			else
				*vp = BOOLEAN_TO_JSVAL(JS_TRUE);
799
800
			break;
		case FILE_PROP_ERROR:
801
802
803
804
			if(p->fp)
				*vp = INT_TO_JSVAL(ferror(p->fp));
			else
				*vp = INT_TO_JSVAL(0);
805
806
			break;
		case FILE_PROP_POSITION:
807
808
809
810
			if(p->fp)
				*vp = INT_TO_JSVAL(ftell(p->fp));
			else
				*vp = INT_TO_JSVAL(-1);
811
812
813
814
815
816
817
818
819
820
821
822
823
824
			break;
		case FILE_PROP_LENGTH:
			if(p->fp)	/* open? */
				*vp = INT_TO_JSVAL(filelength(fileno(p->fp)));
			else
				*vp = INT_TO_JSVAL(flength(p->name));
			break;
		case FILE_PROP_ATTRIBUTES:
			*vp = INT_TO_JSVAL(getfattr(p->name));
			break;
		case FILE_PROP_DEBUG:
			*vp = INT_TO_JSVAL(p->debug);
			break;
		case FILE_PROP_DESCRIPTOR:
825
826
827
828
			if(p->fp)
				*vp = INT_TO_JSVAL(fileno(p->fp));
			else
				*vp = INT_TO_JSVAL(-1);
829
			break;
830
831
832
		case FILE_PROP_ETX:
			*vp = INT_TO_JSVAL(p->etx);
			break;
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
	}

	return(TRUE);
}

#define FILE_PROP_FLAGS JSPROP_ENUMERATE|JSPROP_READONLY

static struct JSPropertySpec js_file_properties[] = {
/*		 name				,tinyid					,flags,				getter,	setter	*/
	{	"name"				,FILE_PROP_NAME			,FILE_PROP_FLAGS,	NULL,NULL},
	{	"mode"				,FILE_PROP_MODE			,FILE_PROP_FLAGS,	NULL,NULL},
	{	"exists"			,FILE_PROP_EXISTS		,FILE_PROP_FLAGS,	NULL,NULL},
	{	"date"				,FILE_PROP_DATE			,FILE_PROP_FLAGS,	NULL,NULL},
	{	"is_open"			,FILE_PROP_IS_OPEN		,FILE_PROP_FLAGS,	NULL,NULL},
	{	"eof"				,FILE_PROP_EOF			,FILE_PROP_FLAGS,	NULL,NULL},
	{	"error"				,FILE_PROP_ERROR		,FILE_PROP_FLAGS,	NULL,NULL},
	{	"descriptor"		,FILE_PROP_DESCRIPTOR	,FILE_PROP_FLAGS,	NULL,NULL},
	/* writeable */
851
	{	"etx"				,FILE_PROP_ETX			,JSPROP_ENUMERATE,  NULL,NULL},
852
853
854
855
856
857
858
	{	"debug"				,FILE_PROP_DEBUG		,JSPROP_ENUMERATE,	NULL,NULL},
	{	"position"			,FILE_PROP_POSITION		,JSPROP_ENUMERATE,	NULL,NULL},
	{	"length"			,FILE_PROP_LENGTH		,JSPROP_ENUMERATE,	NULL,NULL},
	{	"attributes"		,FILE_PROP_ATTRIBUTES	,JSPROP_ENUMERATE,	NULL,NULL},
	{0}
};

rswindell's avatar
rswindell committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
#ifdef _DEBUG
static char* file_prop_desc[] = {
	 "filename specified in constructor - <small>READ ONLY</small>"
	,"mode string specified in <i>open</i> call - <small>READ ONLY</small>"
	,"<i>true</i> if the file exists - <small>READ ONLY</small>"
	,"last modified date/time (time_t format) - <small>READ ONLY</small>"
	,"<i>true</i> if the file has been opened successfully - <small>READ ONLY</small>"
	,"<i>true</i> if the current file position is at the <b>end of file</b> - <small>READ ONLY</small>"
	,"the last occurred error value (use clear_error to clear) - <small>READ ONLY</small>"
	,"the open file descriptor (advanced use only) - <small>READ ONLY</small>"
	,"end-of-text character (advanced use only), if non-zero used by <i>read</i>, <i>readln</i>, and <i>write</i>"
	,"<i>true</i> if debug output is enabled"
	,"the current file position (offset in bytes), change value to seek within file"
	,"the current length of the file (in bytes)"
	,"file mode/attributes"
	,NULL
};
#endif


879
static jsMethodSpec js_file_functions[] = {
880
	{"open",			js_open,			1,	JSTYPE_BOOLEAN,	JSDOCSTR("[string mode, boolean shareable, number buflen]")
rswindell's avatar
rswindell committed
881
882
883
884
885
886
887
888
889
	,JSDOCSTR("open file, <i>shareable</i> defaults to <i>false</i>, <i>buflen</i> defaults to 2048 bytes, "
		"mode (default: <tt>w+</tt>) specifies the type of access requested for the file, as follows:<br>"
		"<tt>r&nbsp</tt> open for reading; if the file does not exist or cannot be found, the open call fails<br>"
		"<tt>w&nbsp</tt> open an empty file for writing; if the given file exists, its contents are destroyed<br>"
		"<tt>a&nbsp</tt> open for writing at the end of the file (appending); creates the file first if it doesnt exist<br>"
		"<tt>r+</tt> open for both reading and writing (the file must exist)<br>"
		"<tt>w+</tt> open an empty file for both reading and writing; if the given file exists, its contents are destroyed<br>"
		"<tt>a+</tt> open for reading and appending<br>"
		"<tt>b&nbsp</tt> open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed (e.g. <tt>r+b</tt>)<br>"
rswindell's avatar
rswindell committed
890
		)
891
892
	},		
	{"close",			js_close,			0,	JSTYPE_VOID,	""
893
	,JSDOCSTR("close file")
894
	},		
895
896
	{"remove",			js_delete,			0,	JSTYPE_BOOLEAN, ""
	,JSDOCSTR("remove the file from the disk")
897
	},
898
	{"clearError",		js_clear_error,		0,	JSTYPE_ALIAS },
899
	{"clear_error",		js_clear_error,		0,	JSTYPE_BOOLEAN, ""
rswindell's avatar
rswindell committed
900
	,JSDOCSTR("clears the current error value (AKA clearError)")
901
902
	},
	{"flush",			js_flush,			0,	JSTYPE_BOOLEAN,	""
rswindell's avatar
rswindell committed
903
	,JSDOCSTR("flush/commit buffers to disk")
904
	},
905
	{"lock",			js_lock,			2,	JSTYPE_BOOLEAN,	JSDOCSTR("[offset, length]")
rswindell's avatar
rswindell committed
906
	,JSDOCSTR("lock file record for exclusive access (file must be opened <i>shareable</i>)")
907
	},		
908
	{"unlock",			js_unlock,			2,	JSTYPE_BOOLEAN,	JSDOCSTR("[offset, length]")
rswindell's avatar
rswindell committed
909
	,JSDOCSTR("unlock file record for exclusive access")
910
	},		
911
912
	{"read",			js_read,			0,	JSTYPE_STRING,	JSDOCSTR("[number maxlen]")
	,JSDOCSTR("read a string from file, maxlen defaults to 512 characters")
913
	},
914
915
	{"readln",			js_readln,			0,	JSTYPE_STRING,	JSDOCSTR("[number maxlen]")
	,JSDOCSTR("read a line-feed terminated string, maxlen defaults 512 characters")
916
	},		
917
918
	{"readBin",			js_readbin,			0,	JSTYPE_NUMBER,	JSDOCSTR("[number bytes]")
	,JSDOCSTR("read a binary integer from the file, default number of bytes is 4 (32-bits)")
919
920
	},
	{"readAll",			js_readall,			0,	JSTYPE_ARRAY,	""
921
	,JSDOCSTR("read all lines into an array of strings")
922
	},
923
924
	{"write",			js_write,			1,	JSTYPE_BOOLEAN,	JSDOCSTR("string text [,number len]")
	,JSDOCSTR("write a string to the file")
925
	},
926
927
	{"writeln",			js_writeln,			0,	JSTYPE_BOOLEAN, JSDOCSTR("[string text]")
	,JSDOCSTR("write a line-feed terminated string to the file")
928
	},
929
930
	{"writeBin",		js_writebin,		1,	JSTYPE_BOOLEAN,	JSDOCSTR("number value [,number bytes]")
	,JSDOCSTR("write a binary integer to the file, default number of bytes is 4 (32-bits)")
931
	},
932
933
	{"writeAll",		js_writeall,		0,	JSTYPE_BOOLEAN,	JSDOCSTR("array lines")
	,JSDOCSTR("write an array of strings to file")
934
	},		
935
	{"printf",			js_fprintf,			0,	JSTYPE_NUMBER,	JSDOCSTR("string format [,args]")
rswindell's avatar
rswindell committed
936
	,JSDOCSTR("write a formatted string to the file (ala fprintf)")
937
	},		
938
939
940
	{0}
};

941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/* File Destructor */

static void js_finalize_file(JSContext *cx, JSObject *obj)
{
	private_t* p;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return;

	if(p->external==JS_FALSE && p->fp!=NULL)
		fclose(p->fp);

	dbprintf(FALSE, p, "closed: %s",p->name);

	free(p);

	JS_SetPrivate(cx, obj, NULL);
}

static JSClass js_file_class = {
     "File"					/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_file_get			/* getProperty	*/
	,js_file_set			/* setProperty	*/
	,JS_EnumerateStub		/* enumerate	*/
	,JS_ResolveStub			/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,js_finalize_file		/* finalize		*/
};

/* File Constructor (creates file descriptor) */

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

	if((str = JS_ValueToString(cx, argv[0]))==NULL) {
982
		JS_ReportError(cx,"No filename specified");
983
984
985
986
987
988
		return(JS_FALSE);
	}

	*rval = JSVAL_VOID;

	if((p=(private_t*)calloc(1,sizeof(private_t)))==NULL) {
989
		JS_ReportError(cx,"calloc failed");
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
		return(JS_FALSE);
	}

	SAFECOPY(p->name,JS_GetStringBytes(str));

	if(!JS_SetPrivate(cx, obj, p)) {
		dbprintf(TRUE, p, "JS_SetPrivate failed");
		return(JS_FALSE);
	}

	if(!js_DefineMethods(cx, obj, js_file_functions)) {
		dbprintf(TRUE, p, "js_DefineMethods failed");
		return(JS_FALSE);
	}

#ifdef _DEBUG
rswindell's avatar
rswindell committed
1006
	js_DescribeObject(cx,obj,"Class used for opening/creating files on the local file system");
rswindell's avatar
rswindell committed
1007
	js_DescribeConstructor(cx,obj,"To create a new File object: <tt>var f = new File(filename)</tt>");
rswindell's avatar
rswindell committed
1008
	js_CreateArrayOfStrings(cx, obj, "_property_desc_list", file_prop_desc, JSPROP_READONLY);
1009
1010
1011
1012
1013
#endif

	dbprintf(FALSE, p, "object constructed");
	return(JS_TRUE);
}
1014
1015
1016
1017
1018
1019
1020
1021

JSObject* DLLCALL js_CreateFileClass(JSContext* cx, JSObject* parent)
{
	JSObject*	sockobj;

	sockobj = JS_InitClass(cx, parent, NULL
		,&js_file_class
		,js_file_constructor
1022
		,1		/* number of constructor args */
1023
		,js_file_properties
1024
		,NULL	/* funcs, set in constructor */
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
		,NULL,NULL);

	return(sockobj);
}

JSObject* DLLCALL js_CreateFileObject(JSContext* cx, JSObject* parent, char *name, FILE* fp)
{
	JSObject* obj;
	private_t*	p;

1035
	obj = JS_DefineObject(cx, parent, name, &js_file_class, NULL, JSPROP_ENUMERATE);
1036
1037
1038
1039
1040
1041
1042

	if(obj==NULL)
		return(NULL);

	if(!JS_DefineProperties(cx, obj, js_file_properties))
		return(NULL);

1043
	if (!js_DefineMethods(cx, obj, js_file_functions)) 
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
		return(NULL);

	if((p=(private_t*)calloc(1,sizeof(private_t)))==NULL)
		return(NULL);

	p->fp=fp;
	p->debug=JS_FALSE;
	p->external=JS_TRUE;

	if(!JS_SetPrivate(cx, obj, p)) {
		dbprintf(TRUE, p, "JS_SetPrivate failed");
		return(NULL);
	}

	dbprintf(FALSE, p, "object created");

	return(obj);
}


1064
#endif	/* JAVSCRIPT */