|
|
1.1 root 1: /*
2: * File: fifo.c
3: *
4: * Purpose: allow kernel to fetch data from real-mode bootstrap data area
5: *
6: * $Log: fifo.c,v $
7: * Revision 1.2 92/08/04 12:30:58 bin
8: * changed for kernel 59
9: *
10: * Revision 1.2 92/01/06 11:59:11 hal
11: * Compile with cc.mwc.
12: *
13: */
14:
15: /*
16: * Includes.
17: */
18: #define KERNEL
19: #include <sys/typed.h>
20:
21: /*
22: * Definitions.
23: * Constants.
24: * Macros with argument lists.
25: * Typedefs.
26: * Enums.
27: */
28:
29: typedef unsigned char uchar;
30: typedef unsigned int uint;
31: typedef unsigned long ulong;
32:
33: /*
34: * Functions.
35: * Import Functions.
36: * Export Functions.
37: * Local Functions.
38: */
39: FIFO * fifo_open();
40: int fifo_close();
41: typed_space * fifo_read();
42:
43: /*
44: * Global Data.
45: * Import Variables.
46: * Export Variables.
47: * Local Variables.
48: *
49: * Arguments are passed into the kernel through boot_gift.
50: * If you start getting "Not enough room for all arguments." messages
51: * at boot time, just increase the BG_LEN to whatever you need.
52: * This structure is EXACTLY BG_LEN bytes long.
53: */
54: TYPED_SPACE(boot_gift, BG_LEN, T_FIFO_SIC);
55:
56: /*
57: * fifo_open()
58: *
59: * Open a typed space as a fifo.
60: *
61: * Takes a typed_space that is already allocated, and a mode. The type of
62: * the typed space must be a FIFO. Only T_FIFO_SIC has been implimented
63: * (static, in-core fifo).
64: *
65: * The mode indicates whether to open for reading or writing.
66: * mode == 0 means read only.
67: * mode == 1 means write only.
68: * Other values are illegal.
69: *
70: * Returns a pointer to an initialized FIFO structure. FIFO structures are
71: * allocated from a pre-allocated array. Returns F_NULL if it can't open
72: * the fifo.
73: */
74: FIFO *
75: fifo_open(fifo_space, mode)
76: typed_space *fifo_space;
77: int mode;
78: {
79: /* ff_table is a table of FIFO structures which can be allocated on
80: * demand. It is functionally similiar to the file descritor table
81: * in the kernel.
82: */
83: static FIFO ff_table[NFIFOS];
84: static int inited = (1==2); /* Has ff_table been initialized? */
85:
86: int i; /* A handy counter. */
87: FIFO *the_fifo; /* The fifo we are going to allocate. */
88:
89: /* Initilize ff_table the first time we get called. */
90: if (!inited) {
91: for (i = 0; i < NFIFOS; ++i) {
92: ff_table[i].f_space = F_NULL;
93: ff_table[i].f_flags = 0;
94: }
95: inited = 1;
96: }
97:
98: /* Check the type of the space we were passed. */
99: switch (fifo_space->ts_type) {
100: case T_FIFO: /* Overly general type, assuming SIC. */
101: fifo_space->ts_type = T_FIFO_SIC;
102: break;
103: case T_FIFO_SIC: /* Static In-core Fifo. */
104: break;
105: case T_FIFO_DIC: /* Dynamic In-core Fifo (can grow). */
106: return(F_NULL); /* Unimplimented. */
107: case T_FIFO_SP: /* Static Permanent Fifo (fixed size file). */
108: return(F_NULL); /* Unimplimented. */
109: case T_FIFO_DP: /* Dynamic Permanent Fifo (ordinary file). */
110: return(F_NULL); /* Unimplimented. */
111: default:
112: return(F_NULL); /* Illegal type encountered. */
113: }
114:
115: /* ASSERTION: fifo_space is a valid and implimented FIFO. */
116:
117: /* Find the first free FIFO structure. */
118:
119: /* This should be re-implimented using a malloc-based scheme.
120: * At the moment, the tertiary boot libraries do not include a
121: * malloc.
122: */
123: for (i = 0; (i < NFIFOS) && (0 != ff_table[i].f_flags); ++i) {
124: /* Do nothing else. */
125: }
126:
127: if (NFIFOS == i) {
128: return(F_NULL); /* No more free fifo structs. */
129: }
130:
131: the_fifo = &(ff_table[i]);
132:
133: /* ASSERTION: the_fifo points at a FIFO we can take. */
134:
135: /* Initialize the FIFO struct. */
136: the_fifo->f_space = fifo_space;
137: the_fifo->f_offset = fifo_space->ts_data;
138:
139: /* Initilize the flags. */
140: switch(mode) {
141: case 0: /* read */
142: the_fifo->f_flags |= F_READ;
143: break;
144: case 1: /* write */
145: the_fifo->f_flags |= F_WRITE;
146: break;
147: default:
148: return(F_NULL); /* Illegal mode flag. */
149: }
150:
151: return(the_fifo);
152: } /* fifo_open() */
153:
154: /*
155: * fifo_close()
156: *
157: * Finish with using a typed space as a fifo.
158: * Free up FIFO structure associated with a typed space.
159: * Returns 0 if ffp was not open, 1 otherwise.
160: */
161: int
162: fifo_close(ffp)
163: FIFO *ffp;
164: {
165: if (0 == ffp->f_flags) {
166: return(0); /* This ffp is not open. */
167: }
168: ffp->f_space = F_NULL;
169: ffp->f_offset = T_NULL;
170: ffp->f_flags = 0;
171:
172: return(1);
173: } /* fifo_close() */
174:
175: /*
176: * fifo_read()
177: *
178: * Read a typed space from a fifo.
179: * Return a pointer to the next typed space in the fifo ffp. Returns
180: * NULL on end of fifo.
181: *
182: * This read assumes that ffp->f_space has type T_FIFO_SIC.
183: */
184: typed_space *
185: fifo_read(ffp)
186: register FIFO *ffp;
187: {
188: typed_space *retval;
189:
190: /* Read MUST be set. */
191: if (F_READ != F_READ & ffp->f_flags ) {
192: return(T_NULL); /* This ffp is not open for reading. */
193: }
194:
195: /* From here to the end of fifo_read is really fifo_read_sic(). */
196:
197:
198: /* Space of size 0 marks EOFIFO. */
199: if ((long)0 == ffp->f_offset->ts_size) {
200: retval = T_NULL;
201: } else {
202: /* Return the next space. */
203: retval = ffp->f_offset;
204: /* Advance to the next space. */
205: (char *) ffp->f_offset += ffp->f_offset->ts_size;
206: }
207:
208: return(retval);
209: } /* fifo_read() */
210:
211: #ifdef TEST
212: #include <stdio.h>
213:
214: /* This is the typed space we will use for our FIFO operations. */
215: TYPED_SPACE(global_space, 128, T_FIFO_SIC); /* Static In-Core Fifo. */
216:
217: int
218: main()
219: {
220: FIFO *ffp; /* Fifo pointer for a handle. */
221: char line[1024]; /* Place to put input lines. */
222: long size; /* Length for line. (Sizes are all long.) */
223: int i;
224:
225: typed_space *local_space;
226:
227: /* Open the fifo for writing. */
228: if (F_NULL == (ffp = fifo_open(&global_space, 1))) {
229: fprintf(stderr, "Can't open global_space for writing.\n");
230: exit(1);
231: }
232:
233: do {
234: printf("Feed me: ");
235: gets(line);
236: size = (long) (strlen(line) + 1);
237: } while (T_NULL != fifo_write_untyped(ffp, line, size, T_STR_STR));
238:
239: if (0 == fifo_close(ffp)) {
240: fprintf(stderr, "Failed to close global_space.\n");
241: exit(1);
242: }
243:
244: printf("OK, global_space is now full.\n");
245: /* ASSERTION: We've filled global_space with strings. */
246:
247: /* Open the fifo for reading. */
248: if (F_NULL == (ffp = fifo_open(&global_space, 0))) {
249: fprintf(stderr, "Can't open global_space for reading.\n");
250: exit(1);
251: }
252:
253: /* Dump the contents of this FIFO. */
254: for (i = 1; T_NULL != (local_space = fifo_read(ffp)); ++i) {
255: printf("%d: size: %ld: type: 0x%x\n", i,
256: local_space->ts_size,
257: local_space->ts_type);
258:
259: /* Assume everything is a NUL terminated string. */
260: printf("datum: %s\n\n", local_space->ts_data);
261: printf("Hit <RETURN>");
262: gets(line);
263: }
264:
265:
266: /* Rewind the file, and dump it out again. */
267: printf("Rewinding.\n");
268: if (0 == fifo_rewind(ffp)) {
269: fprintf("Can't rewind global_space.\n");
270: exit(1);
271: }
272:
273: for (i = 1; T_NULL != (local_space = fifo_read(ffp)); ++i) {
274: printf("%d: size: %ld: type: 0x%x\n", i,
275: local_space->ts_size,
276: local_space->ts_type);
277:
278: /* Assume everything is a NUL terminated string. */
279: printf("datum: %s\n\n", local_space->ts_data);
280: }
281:
282: if (0 == fifo_close(ffp)) {
283: fprintf(stderr, "Failed to close global_space.\n");
284: exit(1);
285: }
286:
287: exit(0);
288: } /* main() */
289:
290: #endif /* TEST */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.