summaryrefslogtreecommitdiff
path: root/src/stream.c
blob: 0afde36515871a4d3cc20abefa1a1d23dff4fb9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
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
795
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/*
 * Copyright 2007-2018 Adrian Thurston <thurston@colm.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#ifdef HAVE_FOPENCOOKIE
#define _GNU_SOURCE
#include <sys/types.h>
#endif

#include <colm/input.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <stdbool.h>

#include <colm/pdarun.h>
#include <colm/debug.h>
#include <colm/program.h>
#include <colm/tree.h>
#include <colm/bytecode.h>
#include <colm/pool.h>
#include <colm/struct.h>

DEF_STREAM_FUNCS( stream_funcs_data, stream_impl_data );

extern struct stream_funcs_data file_funcs;
extern struct stream_funcs_data accum_funcs;

#ifdef HAVE_FOPENCOOKIE

struct colm_file_cookie
{
	int fd;
};

static ssize_t cfc_read(void *cookie, char *buf, size_t size)
{
	return read( ((struct colm_file_cookie*)cookie)->fd, buf, size );
}

static ssize_t cfc_write(void *cookie, const char *buf, size_t size)
{
	return write( ((struct colm_file_cookie*)cookie)->fd, buf, size );
}

static int cfc_seek(void *cookie, off_t *offset, int whence)
{
	return -1;
}

static int cfc_close(void *cookie)
{
	free(cookie);
	return 0;
}

FILE *colm_fd_open( int fd, const char *mode )
{
	cookie_io_functions_t cf = {
		cfc_read,
		cfc_write,
		cfc_seek,
		cfc_close,
	};

	struct colm_file_cookie *cfc = malloc(sizeof(struct colm_file_cookie));
	cfc->fd = fd;
	return fopencookie( cfc, mode, cf );
}

#else

FILE *colm_fd_open( int fd, const char *mode )
{
	return fdopen( fd, mode );
}

#endif

void stream_impl_push_line( struct stream_impl_data *ss, int ll )
{
	if ( ss->line_len == 0 ) {
		ss->lines_cur = 0;
		ss->lines_alloc = 16;
		ss->line_len = malloc( sizeof(int) * ss->lines_alloc );
	}
	else if ( ss->lines_cur == ss->lines_alloc ) {
		int lines_alloc_new = ss->lines_alloc * 2;
		int *line_len_new = malloc( sizeof(int) * lines_alloc_new );
		memcpy( line_len_new, ss->line_len, sizeof(int) * ss->lines_alloc );
		free( ss->line_len );
		ss->lines_alloc = lines_alloc_new;
		ss->line_len = line_len_new;
	}

	ss->line_len[ ss->lines_cur ] = ll;
	ss->lines_cur += 1;
}

int stream_impl_pop_line( struct stream_impl_data *ss )
{
	int len = 0;
	if ( ss->lines_cur > 0 ) {
		ss->lines_cur -= 1;
		len = ss->line_len[ss->lines_cur];
	}
	return len;
}

#ifdef DEBUG
static void dump_contents( struct colm_program *prg, struct stream_impl_data *sid )
{
	struct run_buf *rb = sid->queue.head;
	while ( rb != 0 ) {
		debug( prg, REALM_INPUT, " %p contents |%d|%d|%d|%.*s|\n", sid,
				rb->offset, rb->length,
				rb->length - rb->offset,
				(int)rb->length - rb->offset,
				rb->data + rb->offset );
		rb = rb->next;
	}
}
#endif

static bool loc_set( location_t *loc )
{
	return loc->line != 0;
}

static void close_stream_file( FILE *file )
{
	if ( file != stdin && file != stdout && file != stderr ) {
		fclose( file );
	}
}

static void si_data_push_tail( struct stream_impl_data *ss, struct run_buf *run_buf )
{
	if ( ss->queue.head == 0 ) {
		run_buf->prev = run_buf->next = 0;
		ss->queue.head = ss->queue.tail = run_buf;
	}
	else {
		ss->queue.tail->next = run_buf;
		run_buf->prev = ss->queue.tail;
		run_buf->next = 0;
		ss->queue.tail = run_buf;
	}
}

static struct run_buf *si_data_pop_tail( struct stream_impl_data *ss )
{
	struct run_buf *ret = ss->queue.tail;
	ss->queue.tail = ss->queue.tail->prev;
	if ( ss->queue.tail == 0 )
		ss->queue.head = 0;
	else
		ss->queue.tail->next = 0;
	return ret;
}


static void si_data_push_head( struct stream_impl_data *ss, struct run_buf *run_buf )
{
	if ( ss->queue.head == 0 ) {
		run_buf->prev = run_buf->next = 0;
		ss->queue.head = ss->queue.tail = run_buf;
	}
	else {
		ss->queue.head->prev = run_buf;
		run_buf->prev = 0;
		run_buf->next = ss->queue.head;
		ss->queue.head = run_buf;
	}
}

static struct run_buf *si_data_pop_head( struct stream_impl_data *ss )
{
	struct run_buf *ret = ss->queue.head;
	ss->queue.head = ss->queue.head->next;
	if ( ss->queue.head == 0 )
		ss->queue.tail = 0;
	else
		ss->queue.head->prev = 0;
	return ret;
}


struct run_buf *new_run_buf( int sz )
{
	struct run_buf *rb;
	if ( sz > FSM_BUFSIZE ) {
		int ssz = sizeof(struct run_buf) + sz - FSM_BUFSIZE;
		rb = (struct run_buf*) malloc( ssz );
		memset( rb, 0, ssz );
	}
	else {
		rb = (struct run_buf*) malloc( sizeof(struct run_buf) );
		memset( rb, 0, sizeof(struct run_buf) );
	}
	return rb;
}

/* Keep the position up to date after consuming text. */
void update_position_data( struct stream_impl_data *is, const alph_t *data, long length )
{
	int i;
	for ( i = 0; i < length; i++ ) {
		if ( data[i] == '\n' ) {
			stream_impl_push_line( is, is->column );
			is->line += 1;
			is->column = 1;
		}
		else {
			is->column += 1;
		}
	}

	is->byte += length;
}

/* Keep the position up to date after sending back text. */
void undo_position_data( struct stream_impl_data *is, const alph_t *data, long length )
{
	/* FIXME: this needs to fetch the position information from the parsed
	 * token and restore based on that.. */
	int i;
	for ( i = 0; i < length; i++ ) {
		if ( data[i] == '\n' ) {
			is->line -= 1;
			is->column = stream_impl_pop_line( is );
		}
		else {
			is->column -= 1;
		}
	}

	is->byte -= length;
}


/*
 * Interface
 */

static void data_transfer_loc( struct colm_program *prg, location_t *loc,
		struct stream_impl_data *ss )
{
	loc->name = ss->name;
	loc->line = ss->line;
	loc->column = ss->column;
	loc->byte = ss->byte;
}

/*
 * Data inputs: files, strings, etc.
 */

static int data_get_data( struct colm_program *prg, struct stream_impl_data *ss,
		alph_t *dest, int length )
{
	int copied = 0;

	/* Move over skip bytes. */
	struct run_buf *buf = ss->queue.head;
	while ( true ) {
		if ( buf == 0 ) {
			/* Got through the in-mem buffers without copying anything. */
			struct run_buf *run_buf = new_run_buf( 0 );
			int received = ss->funcs->get_data_source( prg,
					(struct stream_impl*)ss, run_buf->data, FSM_BUFSIZE );
			if ( received == 0 ) {
				free( run_buf );
				break;
			}

			run_buf->length = received;
			si_data_push_tail( ss, run_buf );

			buf = run_buf;
		}

		int avail = buf->length - buf->offset;

		/* Anything available in the current buffer. */
		if ( avail > 0 ) {
			/* The source data from the current buffer. */
			alph_t *src = &buf->data[buf->offset];

			int slen = avail < length ? avail : length;
			memcpy( dest+copied, src, slen ) ;
			copied += slen;
			length -= slen;
		}

		if ( length == 0 ) {
			//debug( REALM_INPUT, "exiting get data\n", length );
			break;
		}

		buf = buf->next;
	}

	return copied;
}

static struct stream_impl *data_split_consumed( program_t *prg, struct stream_impl_data *sid )
{
	struct stream_impl *split_off = 0;
	if ( sid->consumed > 0 ) {
		debug( prg, REALM_INPUT, "maybe split: consumed is > 0, splitting\n" );
		split_off = colm_impl_consumed( "<text3>", sid->consumed );
		sid->consumed = 0;
	}
	return split_off;
}

int data_append_data( struct colm_program *prg, struct stream_impl_data *sid,
		const alph_t *data, int length )
{
	struct run_buf *tail = sid->queue.tail;
	if ( tail == 0 || length > (FSM_BUFSIZE - tail->length) ) {
		debug( prg, REALM_INPUT, "data_append_data: allocating run buf\n" );
		tail = new_run_buf( length );
		si_data_push_tail( sid, tail );
	}

	debug( prg, REALM_INPUT, "data_append_data: appending to "
			"accum tail, offset: %d, length: %d, dlen: %d\n",
			tail->offset, tail->length, length );

	memcpy( tail->data + tail->length, data, length );
	tail->length += length;

#ifdef DEBUG
	dump_contents( prg, sid );
#endif

	return length;
}

int data_undo_append_data( struct colm_program *prg, struct stream_impl_data *sid, int length )
{
	int consumed = 0;
	int remaining = length;

	/* Move over skip bytes. */
	while ( true ) {
		struct run_buf *buf = sid->queue.tail;

		if ( buf == 0 )
			break;

		/* Anything available in the current buffer. */
		int avail = buf->length - buf->offset;
		if ( avail > 0 ) {
			/* The source data from the current buffer. */
			int slen = avail <= remaining ? avail : remaining;
			consumed += slen;
			remaining -= slen;
			buf->length -= slen;
			//sid->consumed += slen;
		}

		if ( remaining == 0 )
			break;

		struct run_buf *run_buf = si_data_pop_tail( sid );
		free( run_buf );
	}

	debug( prg, REALM_INPUT, "data_undo_append_data: stream %p "
			"ask: %d, consumed: %d, now: %d\n", sid, length, consumed );

#ifdef DEBUG
	dump_contents( prg, sid );
#endif

	return consumed;

}

static void data_destructor( program_t *prg, tree_t **sp, struct stream_impl_data *si )
{
	if ( si->file != 0 && !si->no_file_close )
		close_stream_file( si->file );
	
	if ( si->collect != 0 ) {
		str_collect_destroy( si->collect );
		free( si->collect );
	}

	struct run_buf *buf = si->queue.head;
	while ( buf != 0 ) {
		struct run_buf *next = buf->next;
		free( buf );
		buf = next;
	}

	si->queue.head = 0;

	if ( si->data != 0 )
		free( (char*)si->data );

	/* FIXME: Need to leak this for now. Until we can return strings to a
	 * program loader and free them at a later date (after the colm program is
	 * deleted). */
	// if ( si->name != 0 )
	//	free( si->name );

	if ( si->line_len != 0 )
		free( si->line_len );

	free( si );
}

static str_collect_t *data_get_collect( struct colm_program *prg, struct stream_impl_data *si )
{
	return si->collect;
}

static void data_flush_stream( struct colm_program *prg, struct stream_impl_data *si )
{
	if ( si->file != 0 )
		fflush( si->file );
}

static void data_close_stream( struct colm_program *prg, struct stream_impl_data *si )
{
	if ( si->file != 0 && !si->no_file_close )
		close_stream_file( si->file );

	si->file = 0;
}

static int data_get_option( struct colm_program *prg, struct stream_impl_data *si, int option )
{
	return si->auto_trim;
}

static void data_set_option( struct colm_program *prg, struct stream_impl_data *si, int option, int value )
{
	si->auto_trim = value ? 1 : 0;
}

static void data_print_tree( struct colm_program *prg, tree_t **sp,
		struct stream_impl_data *si, tree_t *tree, int trim )
{
	if ( si->file != 0 )
		colm_print_tree_file( prg, sp, si, tree, trim );
	else if ( si->collect != 0 )
		colm_print_tree_collect( prg, sp, si->collect, tree, trim );
}

static int data_get_parse_block( struct colm_program *prg, struct stream_impl_data *ss,
		int *pskip, alph_t **pdp, int *copied )
{
	int ret = 0;
	*copied = 0;

	/* Move over skip bytes. */
	struct run_buf *buf = ss->queue.head;
	while ( true ) {
		if ( buf == 0 ) {
			/* Got through the in-mem buffers without copying anything. */
			struct run_buf *run_buf = new_run_buf( 0 );
			int received = ss->funcs->get_data_source( prg,
					(struct stream_impl*)ss, run_buf->data, FSM_BUFSIZE );
			if ( received == 0 ) {
				free( run_buf );
				ret = INPUT_EOD;
				break;
			}

			run_buf->length = received;
			si_data_push_tail( ss, run_buf );

			int slen = received;
			*pdp = run_buf->data;
			*copied = slen;
			ret = INPUT_DATA;
			break;
		}

		int avail = buf->length - buf->offset;

		/* Anything available in the current buffer. */
		if ( avail > 0 ) {
			/* The source data from the current buffer. */
			alph_t *src = &buf->data[buf->offset];

			/* Need to skip? */
			if ( *pskip > 0 && *pskip >= avail ) {
				/* Skipping the the whole source. */
				*pskip -= avail;
			}
			else {
				/* Either skip is zero, or less than slen. Skip goes to zero.
				 * Some data left over, copy it. */
				src += *pskip;
				avail -= *pskip;
				*pskip = 0;

				int slen = avail;
				*pdp = src;
				*copied += slen;
				ret = INPUT_DATA;
				break;
			}
		}

		buf = buf->next;
	}

	return ret;
}

static int data_consume_data( struct colm_program *prg, struct stream_impl_data *sid,
		int length, location_t *loc )
{
	int consumed = 0;
	int remaining = length;

	/* Move over skip bytes. */
	while ( true ) {
		struct run_buf *buf = sid->queue.head;

		if ( buf == 0 )
			break;

		/* Anything available in the current buffer. */
		int avail = buf->length - buf->offset;
		if ( avail > 0 ) {

			if ( !loc_set( loc ) )
				data_transfer_loc( prg, loc, sid );

			/* The source data from the current buffer. */
			int slen = avail <= remaining ? avail : remaining;
			consumed += slen;
			remaining -= slen;
			update_position_data( sid, buf->data + buf->offset, slen );
			buf->offset += slen;
			sid->consumed += slen;
		}

		if ( remaining == 0 )
			break;

		struct run_buf *run_buf = si_data_pop_head( sid );
		free( run_buf );
	}

	debug( prg, REALM_INPUT, "data_consume_data: stream %p "
			"ask: %d, consumed: %d, now: %d\n", sid, length, consumed, sid->consumed );

#ifdef DEBUG
	dump_contents( prg, sid );
#endif

	return consumed;
}

static int data_undo_consume_data( struct colm_program *prg, struct stream_impl_data *sid,
		const alph_t *data, int length )
{
	const alph_t *end = data + length;
	int amount = length;
	if ( amount > sid->consumed )
		amount = sid->consumed;

	int remaining = amount;
	struct run_buf *head = sid->queue.head;
	if ( head != 0 && head->offset > 0 ) {
		/* Fill into the offset space. */
		int fill = remaining > head->offset ? head->offset : remaining;
		end -= fill;
		remaining -= fill;

		undo_position_data( sid, end, fill );
		memcpy( head->data + (head->offset - fill), end, fill );

		head->offset -= fill;
		sid->consumed -= fill;
	}

	if ( remaining > 0 ) {
		end -= remaining;
		struct run_buf *new_buf = new_run_buf( 0 );
		new_buf->length = remaining;
		undo_position_data( sid, end, remaining );
		memcpy( new_buf->data, end, remaining );
		si_data_push_head( sid, new_buf );
		sid->consumed -= amount;
	}

	debug( prg, REALM_INPUT, "data_undo_consume_data: stream %p "
			"undid consume %d of %d bytes, consumed now %d, \n",
			sid, amount, length, sid->consumed );

#ifdef DEBUG
	dump_contents( prg, sid );
#endif

	return amount;
}

/*
 * File Inputs
 */

static int file_get_data_source( struct colm_program *prg, struct stream_impl_data *si,
		alph_t *dest, int length )
{
	return fread( dest, 1, length, si->file );
}

/*
 * Text inputs
 */

static int accum_get_data_source( struct colm_program *prg, struct stream_impl_data *si,
		alph_t *dest, int want )
{
	long avail = si->dlen - si->offset;
	long take = avail < want ? avail : want;
	if ( take > 0 ) 
		memcpy( dest, si->data + si->offset, take );
	si->offset += take;
	return take;
}

char stream_get_eof_sent( struct colm_program *prg, struct input_impl_seq *si )
{
	return si->eof_sent;
}

void stream_set_eof_sent( struct colm_program *prg, struct input_impl_seq *si, char eof_sent )
{
	si->eof_sent = eof_sent;
}

struct stream_funcs_data file_funcs = 
{
	&data_get_parse_block,
	&data_get_data,
	&file_get_data_source,

	&data_consume_data,
	&data_undo_consume_data,

	&data_transfer_loc,
	&data_get_collect,
	&data_flush_stream,
	&data_close_stream,
	&data_print_tree,

	&data_split_consumed,
	&data_append_data,
	&data_undo_append_data,
	&data_destructor,

	&data_get_option,
	&data_set_option,
};

struct stream_funcs_data accum_funcs = 
{
	&data_get_parse_block,
	&data_get_data,
	&accum_get_data_source,

	&data_consume_data,
	&data_undo_consume_data,

	&data_transfer_loc,
	&data_get_collect,
	&data_flush_stream,
	&data_close_stream,
	&data_print_tree,

	&data_split_consumed,
	&data_append_data,
	&data_undo_append_data,
	&data_destructor,

	&data_get_option,
	&data_set_option,
};

static void si_data_init( struct stream_impl_data *is, char *name )
{
	memset( is, 0, sizeof(struct stream_impl_data) );

	is->type = 'D';
	is->name = name;
	is->line = 1;
	is->column = 1;
	is->byte = 0;

	/* Indentation turned off. */
	is->indent.level = COLM_INDENT_OFF;
	is->indent.indent = 0;
}

struct stream_impl *colm_impl_new_accum( char *name )
{
	struct stream_impl_data *si = (struct stream_impl_data*)
			malloc(sizeof(struct stream_impl_data));
	si_data_init( si, name );
	si->funcs = (struct stream_funcs*)&accum_funcs;

	return (struct stream_impl*)si;
}

static struct stream_impl *colm_impl_new_file( char *name, FILE *file )
{
	struct stream_impl_data *ss = (struct stream_impl_data*)
			malloc(sizeof(struct stream_impl_data));
	si_data_init( ss, name );
	ss->funcs = (struct stream_funcs*)&file_funcs;
	ss->file = file;
	return (struct stream_impl*)ss;
}

static struct stream_impl *colm_impl_new_fd( char *name, long fd )
{
	struct stream_impl_data *si = (struct stream_impl_data*)
			malloc(sizeof(struct stream_impl_data));
	si_data_init( si, name );
	si->funcs = (struct stream_funcs*)&file_funcs;

	const char *mode = ( fd == 0 ) ? "r" : "w";
	si->file = colm_fd_open( fd, mode );
#ifndef HAVE_FOPENCOOKIE
	if ( fd == 0 || fd == 1 || fd == 2 )
		si->no_file_close = 1;
#endif
	return (struct stream_impl*)si;
}

struct stream_impl *colm_impl_consumed( char *name, int len )
{
	struct stream_impl_data *si = (struct stream_impl_data*)
			malloc(sizeof(struct stream_impl_data));
	si_data_init( si, name );
	si->funcs = (struct stream_funcs*)&accum_funcs;

	si->data = 0;
	si->consumed = len;
	si->offset = len;

	si->dlen = len;

	return (struct stream_impl*)si;
}

struct stream_impl *colm_impl_new_text( char *name, struct colm_location *loc, const alph_t *data, int len )
{
	struct stream_impl_data *si = (struct stream_impl_data*)
			malloc(sizeof(struct stream_impl_data));
	si_data_init( si, name );
	si->funcs = (struct stream_funcs*)&accum_funcs;

	alph_t *buf = (alph_t*)malloc( len );
	memcpy( buf, data, len );

	si->data = buf;
	si->dlen = len;

	if ( loc != 0 ) {
		si->line = loc->line;
		si->column = loc->column;
		si->byte = loc->byte;
	}

	return (struct stream_impl*)si;
}

struct stream_impl *colm_impl_new_collect( char *name )
{
	struct stream_impl_data *ss = (struct stream_impl_data*)
			malloc(sizeof(struct stream_impl_data));
	si_data_init( ss, name );
	ss->funcs = (struct stream_funcs*)&accum_funcs;
	ss->collect = (struct colm_str_collect*) malloc( sizeof( struct colm_str_collect ) );
	init_str_collect( ss->collect );
	return (struct stream_impl*)ss;
}

struct stream_impl *stream_to_impl( stream_t *ptr )
{
	return ptr->impl;
}

str_t *collect_string( program_t *prg, stream_t *s )
{
	str_collect_t *collect = s->impl->funcs->get_collect( prg, s->impl );
	head_t *head = string_alloc_full( prg, collect->data, collect->length );
	str_t *str = (str_t*)construct_string( prg, head );
	return str;
}

stream_t *colm_stream_open_fd( program_t *prg, char *name, long fd )
{
	struct stream_impl *impl = colm_impl_new_fd( colm_filename_add( prg, name ), fd );

	struct colm_stream *s = colm_stream_new_struct( prg );
	s->impl = impl;
	return s;
}

stream_t *colm_stream_open_file( program_t *prg, tree_t *name, tree_t *mode )
{
	head_t *head_name = ((str_t*)name)->value;
	head_t *head_mode = ((str_t*)mode)->value;
	stream_t *stream = 0;

	const char *given_mode = string_data(head_mode);
	const char *fopen_mode = 0;
	if ( memcmp( given_mode, "r", string_length(head_mode) ) == 0 )
		fopen_mode = "rb";
	else if ( memcmp( given_mode, "w", string_length(head_mode) ) == 0 )
		fopen_mode = "wb";
	else if ( memcmp( given_mode, "a", string_length(head_mode) ) == 0 )
		fopen_mode = "ab";
	else {
		fatal( "unknown file open mode: %s\n", given_mode );
	}
	
	/* Need to make a C-string (null terminated). */
	char *file_name = malloc(string_length(head_name)+1);
	memcpy( file_name, string_data(head_name), string_length(head_name) );
	file_name[string_length(head_name)] = 0;

	FILE *file = fopen( file_name, fopen_mode );
	if ( file != 0 ) {
		stream = colm_stream_new_struct( prg );
		stream->impl = colm_impl_new_file( colm_filename_add( prg, file_name ), file );
	}

	free( file_name );

	return stream;
}


void colm_stream_destroy( program_t *prg, tree_t **sp, struct_t *s )
{
	stream_t *stream = (stream_t*) s;
	struct stream_impl *si = stream->impl;
	si->funcs->destructor( prg, sp, si );
}

stream_t *colm_stream_new_struct( program_t *prg )
{
	size_t memsize = sizeof(struct colm_stream);
	struct colm_stream *stream = (struct colm_stream*) malloc( memsize );
	memset( stream, 0, memsize );
	colm_struct_add( prg, (struct colm_struct *)stream );
	stream->id = prg->rtd->struct_stream_id;
	stream->destructor = &colm_stream_destroy;
	return stream;
}

stream_t *colm_stream_open_collect( program_t *prg )
{
	struct stream_impl *impl = colm_impl_new_collect( colm_filename_add( prg, "<internal>" ) );
	struct colm_stream *stream = colm_stream_new_struct( prg );
	stream->impl = impl;
	return stream;
}