|
|
1.1 ! root 1: /* fifo_open.c -- Routines implementing internally typed fifos. */ ! 2: #include <sys/typed.h> ! 3: ! 4: /* Open a typed space as a fifo. ! 5: * ! 6: * Takes a typed_space that is already allocated, and a mode. The type of ! 7: * the typed space must be a FIFO. Only T_FIFO_SIC has been implimented ! 8: * (static, in-core fifo). ! 9: * ! 10: * The mode indicates whether to open for reading or writing. ! 11: * mode == 0 means read only. ! 12: * mode == 1 means write only. ! 13: * Other values are illegal. ! 14: * ! 15: * Returns a pointer to an initialized FIFO structure. FIFO structures are ! 16: * allocated from a pre-allocated array. Returns F_NULL if it can't open ! 17: * the fifo. ! 18: */ ! 19: FIFO * ! 20: fifo_open(fifo_space, mode) ! 21: typed_space *fifo_space; ! 22: int mode; ! 23: { ! 24: /* ff_table is a table of FIFO structures which can be allocated on ! 25: * demand. It is functionally similiar to the file descritor table ! 26: * in the kernel. ! 27: */ ! 28: static FIFO ff_table[NFIFOS]; ! 29: static int inited = (1==2); /* Has ff_table been initialized? */ ! 30: ! 31: int i; /* A handy counter. */ ! 32: FIFO *the_fifo; /* The fifo we are going to allocate. */ ! 33: ! 34: /* Initilize ff_table the first time we get called. */ ! 35: if (!inited) { ! 36: for (i = 0; i < NFIFOS; ++i) { ! 37: ff_table[i].f_space = F_NULL; ! 38: ff_table[i].f_flags = 0; ! 39: } ! 40: inited = (1==1); ! 41: } ! 42: ! 43: /* Check the type of the space we were passed. */ ! 44: switch (fifo_space->ts_type) { ! 45: case T_FIFO: /* Overly general type, assuming SIC. */ ! 46: fifo_space->ts_type = T_FIFO_SIC; ! 47: break; ! 48: case T_FIFO_SIC:/* Static In-core Fifo. */ ! 49: break; ! 50: case T_FIFO_DIC:/* Dynamic In-core Fifo (can grow). */ ! 51: return(F_NULL); /* Unimplimented. */ ! 52: case T_FIFO_SP: /* Static Permanent Fifo (fixed size file). */ ! 53: return(F_NULL); /* Unimplimented. */ ! 54: case T_FIFO_DP: /* Dynamic Permanent Fifo (ordinary file). */ ! 55: return(F_NULL); /* Unimplimented. */ ! 56: default: ! 57: return(F_NULL); /* Illegal type encountered. */ ! 58: } ! 59: ! 60: /* ASSERTION: fifo_space is a valid and implimented FIFO. */ ! 61: ! 62: /* Find the first free FIFO structure. */ ! 63: ! 64: /* This should be re-implimented using a malloc-based scheme. ! 65: * At the moment, the tertiary boot libraries do not include a ! 66: * malloc. ! 67: */ ! 68: for (i = 0; (i < NFIFOS) && (0 != ff_table[i].f_flags); ++i) { ! 69: /* Do nothing else. */ ! 70: } ! 71: ! 72: if (NFIFOS == i) { ! 73: return(F_NULL); /* No more free fifo structs. */ ! 74: } ! 75: ! 76: the_fifo = &(ff_table[i]); ! 77: ! 78: /* ASSERTION: the_fifo points at a FIFO we can take. */ ! 79: ! 80: /* Initialize the FIFO struct. */ ! 81: the_fifo->f_space = fifo_space; ! 82: the_fifo->f_offset = fifo_space->ts_data; ! 83: ! 84: /* Initilize the flags. */ ! 85: switch(mode) { ! 86: case 0: /* read */ ! 87: the_fifo->f_flags |= F_READ; ! 88: break; ! 89: case 1: /* write */ ! 90: the_fifo->f_flags |= F_WRITE; ! 91: break; ! 92: default: ! 93: return(F_NULL); /* Illegal mode flag. */ ! 94: } ! 95: ! 96: return(the_fifo); ! 97: } /* fifo_open() */ ! 98: ! 99: ! 100: #ifdef TEST ! 101: #include <stdio.h> ! 102: ! 103: /* This is the typed space we will use for our FIFO operations. */ ! 104: TYPED_SPACE(global_space, 128, T_FIFO_SIC); /* Static In-Core Fifo. */ ! 105: ! 106: int ! 107: main() ! 108: { ! 109: FIFO *ffp; /* Fifo pointer for a handle. */ ! 110: char line[1024]; /* Place to put input lines. */ ! 111: long size; /* Length for line. (Sizes are all long.) */ ! 112: int i; ! 113: ! 114: typed_space *local_space; ! 115: ! 116: /* Open the fifo for writing. */ ! 117: if (F_NULL == (ffp = fifo_open(&global_space, 1))) { ! 118: fprintf(stderr, "Can't open global_space for writing.\n"); ! 119: exit(1); ! 120: } ! 121: ! 122: do { ! 123: printf("Feed me: "); ! 124: gets(line); ! 125: size = (long) (strlen(line) + 1); ! 126: } while (T_NULL != fifo_write_untyped(ffp, line, size, T_STR_STR)); ! 127: ! 128: if (0 == fifo_close(ffp)) { ! 129: fprintf(stderr, "Failed to close global_space.\n"); ! 130: exit(1); ! 131: } ! 132: ! 133: printf("OK, global_space is now full.\n"); ! 134: /* ASSERTION: We've filled global_space with strings. */ ! 135: ! 136: /* Open the fifo for reading. */ ! 137: if (F_NULL == (ffp = fifo_open(&global_space, 0))) { ! 138: fprintf(stderr, "Can't open global_space for reading.\n"); ! 139: exit(1); ! 140: } ! 141: ! 142: /* Dump the contents of this FIFO. */ ! 143: for (i = 1; T_NULL != (local_space = fifo_read(ffp)); ++i) { ! 144: printf("%d: size: %ld: type: 0x%x\n", i, ! 145: local_space->ts_size, ! 146: local_space->ts_type); ! 147: ! 148: /* Assume everything is a NUL terminated string. */ ! 149: printf("datum: %s\n\n", local_space->ts_data); ! 150: printf("Hit <RETURN>"); ! 151: gets(line); ! 152: } ! 153: ! 154: ! 155: /* Rewind the file, and dump it out again. */ ! 156: printf("Rewinding.\n"); ! 157: if (0 == fifo_rewind(ffp)) { ! 158: fprintf("Can't rewind global_space.\n"); ! 159: exit(1); ! 160: } ! 161: ! 162: for (i = 1; T_NULL != (local_space = fifo_read(ffp)); ++i) { ! 163: printf("%d: size: %ld: type: 0x%x\n", i, ! 164: local_space->ts_size, ! 165: local_space->ts_type); ! 166: ! 167: /* Assume everything is a NUL terminated string. */ ! 168: printf("datum: %s\n\n", local_space->ts_data); ! 169: } ! 170: ! 171: if (0 == fifo_close(ffp)) { ! 172: fprintf(stderr, "Failed to close global_space.\n"); ! 173: exit(1); ! 174: } ! 175: ! 176: exit(0); ! 177: } /* main() */ ! 178: ! 179: #endif /* TEST */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.