home *** CD-ROM | disk | FTP | other *** search
- /* This source file is part of the LynxLib miscellaneous library by
- Robert Fischer, and is Copyright 1990 by Robert Fischer. It costs no
- money, and you may not make money off of it, but you may redistribute
- it. It comes with ABSOLUTELY NO WARRANTY. See the file LYNXLIB.DOC
- for more details.
- To contact the author:
- Robert Fischer \\80 Killdeer Rd \\Hamden, CT 06517 USA
- (203) 288-9599 fischer-robert@cs.yale.edu */
-
- /* By Robert Fischer, August 1988 */
- /*
- This is another file driver for VFILE It reads and writes via the
- rendezvous coroutine function. The lower 16 bits of the message it
- sends is the character it is sending. Hopefully, when co_getc is
- called, the lower 16 bits of the thing it's getting is the character. */
-
- #include <co.h>
- #include <vfile.h>
- #include <stdio.h>
- /* -------------------------------------------------- */
- typedef struct { /* A slave's end of the the cofile */
- long size;
- BOOLEAN eof;
- } SLAVE_FILE;
- typedef struct {
- pcb f; /* The function which the cofile is made of */
- long size; /* Current size of file (amount read or written) */
- BOOLEAN eof; /* TRUE if end of file has been reached */
- SLAVE_FILE *slave;
- } COFILE;
-
- #define ASK_EOF 1
- #define ASK_GETC 2
-
- /* -------------------------------------------------- */
- /* Master's cofile driver */
-
- WORD co_getc(f)
- COFILE *f;
- {
- LONG c; /* Character being gotten */
- if (f->eof) return EOF;
- if (resume_co(&f->f, NULL, &c)) { /* A character was found */
- f->size++;
- return (WORD)c;
- }
-
- /* A character was NOT found -- set eof */
- f->eof = TRUE;
- return EOF;
- }
-
- co_eof(f) /* Looks @ end of file */
- COFILE *f;
- {
- return f->eof;
- }
-
- co_putc(c, f) /* Puts a character */
- int c; /* Character to put */
- COFILE *f;
- {
- LONG dum;
- if (f->eof) return; /* The slave has quit already, something which shouldn't happen */
- if (!resume_co(&f->f, (LONG)c, &dum)) /* slave quit */
- f->eof = TRUE;
- }
-
- co_close(f)
- COFILE *f;
- {
- kill_co(&f->f);
- free(f->slave);
- free(f);
- }
-
- long co_curpos(f)
- COFILE *f;
- {
- return f->size;
- }
-
- /* -------------------------------------------------- */
- /* Slave's cofile driver */
-
- WORD slave_getc(f)
- SLAVE_FILE *f;
- {
- if (!f->eof) {
- f->size++;
- return (WORD)to_master(NULL);
- }
- }
-
- BOOLEAN slave_eof(f)
- SLAVE_FILE *f;
- {
- return f->eof;
- }
-
- WORD slave_putc(c, f)
- int c;
- SLAVE_FILE *f;
- {
- if (!f->eof) {
- f->size++;
- to_master((LONG)c);
- }
- }
-
- slave_close(f)
- SLAVE_FILE *f;
- {
- f->eof = TRUE;
- }
-
- long slave_curpos(f)
- SLAVE_FILE *f;
- {
- return f->size;
- }
- /* -------------------------------------------------- */
- VFILE *open_cofile(f, x, stacksize, slave_vfile)
- func *f; /* Function to make a cofile of */
- LONG x; /* Argument to f */
- long stacksize; /* Size to make cofile's stack */
- VFILE **slave_vfile; /* Slave's cofile handle */
- {
- VFILE *v; /* Master's cofile */
- VFILE *s; /* Slave's cofile */
- COFILE *c;
- SLAVE_FILE *d;
-
- /* Init the master's cofile */
- v = malloc(sizeof(*v));
- v->curpos = &co_curpos;
- v->do_eof = &co_eof;
- v->do_getc = &co_getc;
- v->do_putc = &co_putc;
- v->do_close = &co_close;
- c = v->p = malloc(sizeof(COFILE));
- c->size = 0;
- c->eof = FALSE;
- init_co(f, x, stacksize, &(c->f));
-
- /* Init the slave's cofile */
- c->slave = s = malloc(sizeof(*s));
- s->curpos = &slave_curpos;
- s->do_eof = &slave_eof;
- s->do_getc = &slave_getc;
- s->do_putc = &slave_putc;
- s->do_close = &slave_close;
- d = s->p = malloc(sizeof(*d));
- d->size = 0;
- d->eof = FALSE;
-
- /* Return cofiles */
- *slave_vfile = s;
- return v;
- }
- /* -------------------------------------------------- */
- /* -------------------------------------------------- */
-