File: gio.c

package info (click to toggle)
klic 3.003-1.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,068 kB
  • ctags: 6,333
  • sloc: ansic: 101,584; makefile: 3,395; sh: 1,321; perl: 312; exp: 131; tcl: 111; asm: 102; lisp: 4; sed: 1
file content (480 lines) | stat: -rw-r--r-- 11,491 bytes parent folder | download | duplicates (2)
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
/* ----------------------------------------------------------
%   (C)1993,1994 Institute for New Generation Computer Technology
%       (Read COPYRIGHT for detailed information.)
%   (C)1996, 1997, 1998, 1999 Japan Information Processing Development Center
%       (Read COPYRIGHT-JIPDEC for detailed information.)
----------------------------------------------------------- */

#include <stdio.h>
#include <klic/gcobject.h>
#include <klic/g_pointer.h>
#include <klic/g_string.h>
#include <klic/gd_macro.h>
#ifdef DIST
#include <klic/distio.h>
#endif
#include "atom.h"
#include "funct.h"

struct file_io_object {
  struct consumer_object_method_table *method_table;
  int linecount;
  q inname, outname;
  q stream; /* saved input stream for suspension by some other reasons */
  FILE *infile, *outfile;
};

#define GC_CLASS_NAME() file__io
#define GC_OBJ_TYPE struct file_io_object
#define GC_OBJ_SIZE(obj)  G_SIZE_IN_Q(GC_OBJ_TYPE)

GD_USE_CLASS(byte__string);
GD_USE_CLASS(pointer);

#include <klic/gc_macro.h>

/* basic method definitions */

static long do_flush(obj)
     struct file_io_object *obj;
{
#ifdef DIST
  if (obj->infile == stdin ||
      (obj->outfile == stdout || obj->outfile == stderr)) {
    return UserFflush();
  } else
#endif
    {
      return klic_fflush(obj->outfile);
    }
}

#define CheckInput()				    \
{						    \
  if (GC_SELF->infile == 0) goto message_error;	    \
  if (GC_SELF->outfile != 0) do_flush(GC_SELF);	    \
}

#define CheckOutput()				    \
{						    \
  if (GC_SELF->outfile == 0) goto message_error;    \
}

GCDEF_UNIFY()
{
  G_STD_DECL;
  q newvar;
  q reason;

  if (GC_SELF->stream != 0) {
    GC_TERM = GC_SELF->stream;
    GC_SELF->stream = 0;
  }
 top:
  if (G_ISCONS(GC_TERM)) {
    q message;
    GCSET_MESSAGE(message);
    if (G_ISINT(message)) {
      long c = G_INTVAL(message);
#ifdef DIST
      if (GC_SELF->outfile == stdout) {
	  UserPutc(c);
      } else if (GC_SELF->outfile == stderr) {
	  UserEPutc(c);
      } else
#endif
	{
	    if (klic_putc(c, GC_SELF->outfile) == EOF) {
		GC_FAIL("putc failed");
	    }
	}
    } else if (G_ISFUNCTOR(message)) {
      switch (G_SYMVAL(G_FUNCTOR_OF(message))) {
	/*** Input Messages ***/
      case functor_getc_1: {
	long c;
	CheckInput();
#ifdef DIST
	if (GC_SELF->infile == stdin) {
          c = UserGetc();
	} else
#endif
	  {
	    c = klic_getc(GC_SELF->infile);
	  }
	if (c=='\n') { GC_SELF->linecount++; }
	GC_UNIFY_VALUE(G_ARG(message,0), G_MAKEINT(c));
	break;
      }
      case functor_ungetc_1: {
	long c;
	CheckInput();
	GCSET_MESSAGE_INT_ARG(c, message, 0);
	if (c=='\n') { GC_SELF->linecount--; }
#ifdef DIST
	if (GC_SELF->infile == stdin) {
	  UserUngetc(c);
	} else
#endif
	  {
	      ungetc(c, GC_SELF->infile);
	  }
	break;
      }
      case functor_fread_2: {
	extern char *malloc_check();
	extern q convert_binary_c_string_to_klic_string();
	long n, k;
	char *buf;
	q string;
	long space_needed;
	CheckInput();
	GCSET_MESSAGE_INT_ARG(n, message, 0);
	if (n<0) goto message_error;
	space_needed = sizeof(struct byte_string_object)+n+sizeof(q);
	if ((char *)g_allocp+space_needed >= (char *)real_heaplimit) {
	  this_more_space += (space_needed+sizeof(q)-1)/sizeof(q);
	  goto gc_request;
	}
	buf = (char *)malloc_check(n);
#ifdef DIST
      if (GC_SELF->infile == stdin) {
	  size_t strlen();
          Read(buf); 
	  n = strlen(buf);
       } else
#endif
	 {
	     n = klic_fread(buf, 1, n, GC_SELF->infile);
	 }

	for (k=0; k<n; k++) {
	  if (buf[k] == '\n') { GC_SELF->linecount++; }
	}
	string = convert_binary_c_string_to_klic_string(buf, n, g_allocp);
	if (G_ISREF(string)) {
	  GC_FAIL("internal error: string allocation for fread");
	}
	g_allocp = heapp;
	free(buf);
	GC_UNIFY_VALUE(G_ARG(message,1), string);
	break;
      }
      case functor_linecount_1: {
	CheckInput();
	GC_UNIFY_VALUE(G_ARG(message,0), G_MAKEINT(GC_SELF->linecount));
	break;
      }
	/*** Output Messages ***/
      case functor_putc_1: {
	long c;
	CheckOutput();
	GCSET_MESSAGE_INT_ARG(c, message, 0);
#ifdef DIST
	if (GC_SELF->outfile == stdout) { UserPutc((int)c); }
	else if (GC_SELF->outfile == stderr) { UserEPutc((int)c); }
	else 
#endif
	  {
	      if (klic_putc(c, GC_SELF->outfile) == EOF) {
		  GC_FAIL("putc failed");
	      }
	  }
	break;
      }
      case functor_fwrite_2: {
	extern unsigned char *generic_string_body();
	struct byte_string_object *str;
	int size;
	int written;
	CheckOutput();
	GCSET_MESSAGE_STR_ARG(str, message, 0);
	size = generic_string_size(str);
#ifdef DIST
      if (GC_SELF->outfile == stdout) {
	  UserWrite(generic_string_body(str), size);
	  written = size;
      } else if (GC_SELF->outfile == stderr) {
	  UserEWrite(generic_string_body(str), size);
	  written = size;
      } else
#endif
	{
	    written = klic_fwrite(generic_string_body(str),
				  1, size, GC_SELF->outfile);
	}
	GC_UNIFY_VALUE(G_ARG(message,1), G_MAKEINT(written));
	break;
      }
      case functor_fwrite_1: {
	extern unsigned char *generic_string_body();
	struct byte_string_object *str;
	char *strbody;
	int size;
	int written = 0;
	CheckOutput();
	GCSET_MESSAGE_STR_ARG(str, message, 0);
	strbody = (char *)generic_string_body(str);
	size = generic_string_size(str);
#ifdef DIST
	if (GC_SELF->outfile == stdout) {
	    UserWrite(strbody, size);
	} else if (GC_SELF->outfile == stderr) {
	    UserEWrite(strbody, size);
	} else
#endif
	  {
	      while (size > 0) {
		  written = klic_fwrite(strbody+written, 1,
					size, GC_SELF->outfile);
		  size -= written;
	      }
	  }
	break;
      }
	/*** Common Messages ***/
      case functor_feof_1: {
	long iseof;
#ifdef DIST
	if (GC_SELF->infile == stdin ||
	    GC_SELF->outfile == stdout ||
	    GC_SELF->outfile == stderr) { iseof = 0; }
	else 
#endif
	  {
	    if (GC_SELF->infile != 0) {
	      iseof = feof(GC_SELF->infile);
	    } else {
	      iseof = feof(GC_SELF->outfile);
	    }
	  }
	GC_UNIFY_VALUE(G_ARG(message,0), G_MAKEINT(iseof));
	break;
      }
      case functor_fseek_3: {
	long offset, whence, result;
	GCSET_MESSAGE_INT_ARG(offset, message, 0);
	GCSET_MESSAGE_INT_ARG(whence, message, 1);
#ifdef DIST
      if (GC_SELF->infile == stdin ||
	  (GC_SELF->outfile == stdout || GC_SELF->outfile == stderr)) {
	result = 0;
      } else
#endif
	{
	  FILE *file =
	    (GC_SELF->infile == 0 ? GC_SELF->outfile : GC_SELF->infile);
	  result = fseek(file, offset, whence);
	}
	GC_UNIFY_VALUE(G_ARG(message,2), G_MAKEINT(result));
	break;
      }
      case functor_ftell_1: {
	long result;
#ifdef DIST
	if (GC_SELF->infile == stdin ||
	    (GC_SELF->outfile == stdout || GC_SELF->outfile == stderr)) {
	    result = 0;
	} else
#endif
	  {
	    FILE *file =
	      (GC_SELF->infile == 0 ? GC_SELF->outfile : GC_SELF->infile);
	    result = ftell(file);
	  }
	GC_UNIFY_VALUE(G_ARG(message,0), G_MAKEINT(result));
	break;
      }
      case functor_fflush_1: {
	long result = do_flush(GC_SELF);
	GC_UNIFY_VALUE(G_ARG(message,0), G_MAKEINT(result));
	break;
      }
      case functor_fclose_1: {
	long result;
#ifdef DIST
      if (GC_SELF->infile == stdin ||
	  GC_SELF->outfile == stdout || GC_SELF->outfile == stderr)
	result = 0;
      else
#endif
	{
	  if (GC_SELF->outfile != 0 &&
	      GC_SELF->outfile != stdout &&
	      GC_SELF->outfile != stderr) {
	    result = fclose(GC_SELF->outfile);
	    GC_SELF->outfile = 0;
	  } else {
	    result = 0;
	  }
	  if (result == 0) {
	    if (GC_SELF->infile != 0 &&
		GC_SELF->infile != stdin) {
	      result = fclose(GC_SELF->infile);
	      GC_SELF->infile = 0;
	    }
	  }
	  GC_UNIFY_VALUE(G_ARG(message,0), G_MAKEINT(result));
	  break;
	}
      }
      case functor_sync_1: {
	GC_UNIFY_VALUE(G_ARG(message,0), G_MAKEINT(0));
	break;
      }
      default: goto message_error;
      }
    } else {
      goto message_error;
    }
    GC_TERM = G_CDR_OF(GC_TERM);
    goto top;

  message_error:
    {
      extern unsigned char *generic_string_body();
      char *inname, *outname;
      inname = (char *)generic_string_body(G_FUNCTORP(GC_SELF->inname));
      if (inname == 0) inname = "";
      outname = (char *)generic_string_body(G_FUNCTORP(GC_SELF->outname));
      if (outname == 0) outname = "";
      debug_printf("Illegal message %k to I/O stream for %s%s%s\n",
		   message,
		   inname,
		   ((*inname !=0 && *outname !=0 &&
		     strcmp(inname,outname) != 0) ? "/" : ""),
		   outname);
      GC_FAIL("message error");
    }
  } else if (GC_TERM==NILATOM) {
#ifdef DIST
    if (GC_SELF->outfile == stdout) {
      UserFflush();
    } else if (GC_SELF->outfile == stderr) {
      UserEFflush();
    } else if (GC_SELF->infile == stdin) {
    } else
#endif
      {
	if (GC_SELF->infile != 0 && GC_SELF->infile != stdin) {
	  fclose(GC_SELF->infile);
	}
	if (GC_SELF->outfile != 0 &&
	    GC_SELF->outfile != stdout &&
	    GC_SELF->outfile != stderr) {
	  fclose(GC_SELF->outfile);
	}
      }
    GC_TERMINATE;
  } else if (G_ISREF(GC_TERM)) {
    q temp = G_DEREFONE(GC_TERM);
    if (G_ISREF(temp) &&
	(temp == GC_TERM || G_DEREFONE(temp) == GC_TERM)) {
      reason = GC_TERM;
      GC_SELF->stream = 0;
      goto suspend;
    } else {
      GC_TERM = temp;
      goto top;
    }
  }

 gc_request:
  G_MAKE_VAR(newvar);
  GC_KL1_UNIFY(GC_TERM,newvar);
  GC_RETURN_WITH_HOOK(newvar);

 suspend:
  GC_RETURN_WITH_HOOK(reason);
}

GCDEF_GC()
{
  G_STD_DECL;
  GC_OBJ_TYPE *newself;

  GCSET_NEWOBJ_IN_NEWGEN(newself);
  *newself = *GC_SELF;
  if (GC_SELF->stream != 0) {
    G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->stream,newself->stream);
  } else {
    newself->stream = 0;
  }
  G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->inname, newself->inname);
  G_COPY_KL1_TERM_TO_NEWGEN(GC_SELF->outname, newself->outname);
  GC_RETURN_FROM_GC(newself);
}

GCDEF_PRINT()
{
  G_STD_DECL;
  GC_PRINT("$$FILE I/O$");
  GC_RETURN_FROM_PRINT;
}

#define GCUSE_MY_UNIFY
#define GCUSE_MY_PRINT
#define GCUSE_MY_GC

/* define the method table structure of the merger */
#include <klic/gc_methtab.h>

GCDEF_NEW()
{
  GC_STD_DECL_FOR_NEW;
  GC_OBJ_TYPE *newobj;
  q infile, outfile, inpath, outpath;
  q var;
  struct pointer_object *inptr, *outptr;

  /*
    Arguments are:
      0: Input file object
      1: Its name
      2: Output file object
      3: Its name
  */
  if (GC_ARGC != 4) GC_ERROR_IN_NEW("Arity mismatch");

  infile = GC_ARGV[0];
  GC_DEREF_FOR_NEW(infile);
  if (infile != NILATOM &&
      (!G_ISGOBJ(infile) ||
       (struct data_object_method_table *)G_FUNCTOR_OF(infile) !=
       &pointer_g_data_method_table)) {
    GC_FAIL("First argument for file creation is not a pointer object");
  }
  inpath = GC_ARGV[1];
  GC_DEREF_FOR_NEW(inpath);

  outfile = GC_ARGV[2];
  GC_DEREF_FOR_NEW(outfile);
  if (outfile != NILATOM &&
      (!G_ISGOBJ(outfile) ||
       (struct data_object_method_table *)G_FUNCTOR_OF(outfile) !=
       &pointer_g_data_method_table)) {
    GC_FAIL("Third argument for file creation is not a pointer object");
  }
  outpath = GC_ARGV[3];
  GC_DEREF_FOR_NEW(outpath);

  GCSET_NEWOBJ_FOR_NEW(newobj,GC_OBJ_SIZE(newobj));
  if (infile == NILATOM) {
    newobj->infile = 0;
  } else {
    newobj->infile = (FILE *)
      ((struct pointer_object *)G_FUNCTORP(infile))->pointer;
  }
  if (outfile == NILATOM) {
    newobj->outfile = 0;
  } else {
    newobj->outfile = (FILE *)
      ((struct pointer_object *)G_FUNCTORP(outfile))->pointer;
  }
  newobj->linecount = 0;
  newobj->inname = inpath;
  newobj->outname = outpath;
  newobj->stream = 0;
  var = GC_MAKE_HOOK_VAR(newobj);
  GC_RETURN_FROM_NEW(var);
}