--- multiplexer/en300468ts.c 2010/09/29 21:31:24 1.16 +++ multiplexer/en300468ts.c 2010/09/29 21:33:03 1.17 @@ -210,6 +210,21 @@ const static unsigned char possible_desc #define LOOP_DEPTH 4 #define REALLOC_CHUNK 32 +static struct { + int tablen; + int itab; + int fd; + int isdescr; /* 0: main table, >0: descr loop depth */ + int descrtag; + int isyn; + int isyntab; + int numcount; + int loopbegin[LOOP_DEPTH]; + int loopcount[LOOP_DEPTH]; + int ibuf; + char buf[2048]; +} tabnew; + struct sitab { struct sitab *next; char typ; @@ -220,22 +235,6 @@ struct sitab { enum enumsi esi; unsigned long *tab; struct timeval soon; - union { - struct { - int tablen; - int itab; - int fd; - int isdescr; /* 0: main table, >0: descr loop depth */ - int descrtag; - int isyn; - int isyntab; - int numcount; - int loopbegin[LOOP_DEPTH]; - int loopcount[LOOP_DEPTH]; - int ibuf; - char buf[256]; - } a; - } u; unsigned char descrnum[DESCR_LAST-DESCR_FIRST+1]; }; @@ -1606,8 +1605,8 @@ static void maketab(char typ, long pid, t->esi = e; t->tab = NULL; memset(&t->descrnum[0], 0, sizeof(t->descrnum[0])); - memset(&t->u, 0, sizeof(t->u)); - t->u.a.fd = fd; + memset(&tabnew, 0, sizeof(tabnew)); + tabnew.fd = fd; newtab = t; } } @@ -1668,9 +1667,9 @@ static int tabline(char *b, int n) static int taballoc(struct sitab *st, int cnt) { - while (st->u.a.tablen < (st->u.a.itab + cnt)) { - st->u.a.tablen += REALLOC_CHUNK; - st->tab = realloc(st->tab, st->u.a.tablen * sizeof(unsigned long)); + while (tabnew.tablen < (tabnew.itab + cnt)) { + tabnew.tablen += REALLOC_CHUNK; + st->tab = realloc(st->tab, tabnew.tablen * sizeof(unsigned long)); if (st->tab == NULL) { return -ENOMEM; } @@ -1699,35 +1698,35 @@ static int siline(struct sitab *st, char char *e, *a, *z; long v; int s, i; - s = syntax[(st->u.a.isdescr > 0) ? 1 : 0] - [(st->u.a.isdescr > 0) ? st->u.a.descrtag : st->esi][st->u.a.isyn]; + s = syntax[(tabnew.isdescr > 0) ? 1 : 0] + [(tabnew.isdescr > 0) ? tabnew.descrtag : st->esi][tabnew.isyn]; if ((s == SYNTAX_END) || (s == SYNTAX_LOOPEND)) { int lc; #ifdef DEBUG fprintf(stderr, "end:\n"); #endif - if ((lc = --st->u.a.loopcount[0]) <= 0) { + if ((lc = --tabnew.loopcount[0]) <= 0) { if (lc == 0) { for (i = 0; i < LOOP_DEPTH-1; i++) { - st->u.a.loopbegin[i] = st->u.a.loopbegin[i+1]; - st->u.a.loopcount[i] = st->u.a.loopcount[i+1]; + tabnew.loopbegin[i] = tabnew.loopbegin[i+1]; + tabnew.loopcount[i] = tabnew.loopcount[i+1]; } - st->u.a.loopcount[LOOP_DEPTH-1] = 0; + tabnew.loopcount[LOOP_DEPTH-1] = 0; if (s == SYNTAX_LOOPEND) { - st->u.a.isyn += 1; - } else if (st->u.a.isdescr > 0) { - if (--st->u.a.isdescr == 0) { - st->u.a.isyn = st->u.a.isyntab; + tabnew.isyn += 1; + } else if (tabnew.isdescr > 0) { + if (--tabnew.isdescr == 0) { + tabnew.isyn = tabnew.isyntab; } } return 0; } else { - st->u.a.tablen = st->u.a.itab; - st->tab = realloc(st->tab, st->u.a.itab * sizeof(unsigned long)); + tabnew.tablen = tabnew.itab; + st->tab = realloc(st->tab, tabnew.itab * sizeof(unsigned long)); return -ENOBUFS; } } else { - st->u.a.isyn = st->u.a.loopbegin[0]; + tabnew.isyn = tabnew.loopbegin[0]; return 0; } } @@ -1749,8 +1748,8 @@ static int siline(struct sitab *st, char fprintf(stderr, "internal syntax error\n"); exit(1); } - if (st->u.a.numcount == 0) { - st->u.a.numcount = s; + if (tabnew.numcount == 0) { + tabnew.numcount = s; } v = strtoul(a, &z, 0); if (a == z) { @@ -1759,9 +1758,9 @@ static int siline(struct sitab *st, char #ifdef DEBUG fprintf(stderr, "number: %ld, %d..%d\n", v, a-b, z-b); #endif - st->tab[st->u.a.itab++] = v; - if (++st->u.a.numcount == 0) { - st->u.a.isyn += 1; + st->tab[tabnew.itab++] = v; + if (++tabnew.numcount == 0) { + tabnew.isyn += 1; } *e = '\n'; return z-b; @@ -1773,29 +1772,29 @@ static int siline(struct sitab *st, char #ifdef DEBUG fprintf(stderr, "loop: %ld, %d..%d\n", v, a-b, z-b); #endif - st->tab[st->u.a.itab++] = v; + st->tab[tabnew.itab++] = v; if (v != 0) { - if (st->u.a.isdescr > 0) { - st->u.a.isdescr += 1; + if (tabnew.isdescr > 0) { + tabnew.isdescr += 1; } for (i = LOOP_DEPTH-2; i >= 0; i--) { - st->u.a.loopbegin[i+1] = st->u.a.loopbegin[i]; - st->u.a.loopcount[i+1] = st->u.a.loopcount[i]; + tabnew.loopbegin[i+1] = tabnew.loopbegin[i]; + tabnew.loopcount[i+1] = tabnew.loopcount[i]; } - st->u.a.loopbegin[0] = ++st->u.a.isyn; - st->u.a.loopcount[0] = v; + tabnew.loopbegin[0] = ++tabnew.isyn; + tabnew.loopcount[0] = v; } else { do { - st->u.a.isyn += 1; - s = syntax[(st->u.a.isdescr > 0) ? 1 : 0] - [(st->u.a.isdescr > 0) ? st->u.a.descrtag : st->esi] - [st->u.a.isyn]; + tabnew.isyn += 1; + s = syntax[(tabnew.isdescr > 0) ? 1 : 0] + [(tabnew.isdescr > 0) ? tabnew.descrtag : st->esi] + [tabnew.isyn]; if (s == SYNTAX_LOOP) { v += 1; } } while ((s != SYNTAX_END) && ((s != SYNTAX_LOOPEND) || (--v >= 0))); if (s == SYNTAX_LOOPEND) { - st->u.a.isyn += 1; + tabnew.isyn += 1; } } *e = '\n'; @@ -1808,19 +1807,19 @@ static int siline(struct sitab *st, char #ifdef DEBUG fprintf(stderr, "descr: %ld, %d..%d\n", v, a-b, z-b); #endif - st->tab[st->u.a.itab++] = v; - st->u.a.isyn += 1; + st->tab[tabnew.itab++] = v; + tabnew.isyn += 1; if (v != 0) { - st->u.a.isdescr = 1; - st->u.a.descrtag = 0; + tabnew.isdescr = 1; + tabnew.descrtag = 0; for (i = LOOP_DEPTH-2; i >= 0; i--) { - st->u.a.loopbegin[i+1] = st->u.a.loopbegin[i]; - st->u.a.loopcount[i+1] = st->u.a.loopcount[i]; + tabnew.loopbegin[i+1] = tabnew.loopbegin[i]; + tabnew.loopcount[i+1] = tabnew.loopcount[i]; } - st->u.a.loopbegin[0] = 0; - st->u.a.loopcount[0] = v; - st->u.a.isyntab = st->u.a.isyn; - st->u.a.isyn = 0; + tabnew.loopbegin[0] = 0; + tabnew.loopcount[0] = v; + tabnew.isyntab = tabnew.isyn; + tabnew.isyn = 0; } *e = '\n'; return z-b; @@ -1832,7 +1831,7 @@ static int siline(struct sitab *st, char #ifdef DEBUG fprintf(stderr, "descrtag: %ld, %d..%d\n", v, a-b, z-b); #endif - st->tab[st->u.a.itab++] = v; + st->tab[tabnew.itab++] = v; v -= DESCR_FIRST; if ((v < 0) || (v > (DESCR_LAST - DESCR_FIRST))) { return -EINVAL; @@ -1841,8 +1840,8 @@ static int siline(struct sitab *st, char if (!((1 << st->esi) & possible_descr[v])) { return -EINVAL; } - st->u.a.descrtag = v; - st->u.a.isyn += 1; + tabnew.descrtag = v; + tabnew.isyn += 1; *e = '\n'; return z-b; case SYNTAX_DATETIME: @@ -1881,7 +1880,7 @@ static int siline(struct sitab *st, char return -EINVAL; } v += s - 678912; - st->tab[st->u.a.itab++] = v; + st->tab[tabnew.itab++] = v; v = 0; s = ' '; for (i = 2; i >= 0; i--) { @@ -1900,12 +1899,12 @@ static int siline(struct sitab *st, char v = (v<<4) + (s-'0'); s = ':'; } - st->tab[st->u.a.itab++] = v; + st->tab[tabnew.itab++] = v; #ifdef DEBUG fprintf(stderr, "datetime: %04lx %06lx, %d..%d\n", - st->tab[st->u.a.itab-2], v, a-b, z-b); + st->tab[tabnew.itab-2], v, a-b, z-b); #endif - st->u.a.isyn += 1; + tabnew.isyn += 1; *e = '\n'; return z-b; case SYNTAX_STRING: @@ -1920,7 +1919,7 @@ static int siline(struct sitab *st, char i = v; v += z-a; taballoc(st, 1 + (v + sizeof(long) - 1) / sizeof(long)); - memcpy(((char *)&st->tab[st->u.a.itab+1]) + i, a, z-a); + memcpy(((char *)&st->tab[tabnew.itab+1]) + i, a, z-a); z += 1; while (*z == '"') { a = z; @@ -1931,15 +1930,15 @@ static int siline(struct sitab *st, char i = v; v += z-a; taballoc(st, 1 + (v + sizeof(long) - 1) / sizeof(long)); - memcpy(((char *)&st->tab[st->u.a.itab+1]) + i, a, z-a); + memcpy(((char *)&st->tab[tabnew.itab+1]) + i, a, z-a); z += 1; } - st->tab[st->u.a.itab] = v; + st->tab[tabnew.itab] = v; #ifdef DEBUG fprintf(stderr, "string: %ld, %d..%d\n", v, a-b, z-b); #endif - st->u.a.itab += 1 + (v + sizeof(long) - 1) / sizeof(long); - st->u.a.isyn += 1; + tabnew.itab += 1 + (v + sizeof(long) - 1) / sizeof(long); + tabnew.isyn += 1; *e = '\n'; return z-b; } @@ -2018,7 +2017,7 @@ static void argloop(int f0) n0 = -1; nst = -1; if (newtab != NULL) { - nst = pollfd_add(newtab->u.a.fd, POLLIN); + nst = pollfd_add(tabnew.fd, POLLIN); } else if ((r = tabline(&buf0[o0], i0))) { o0 += r; i0 -= r; @@ -2171,32 +2170,32 @@ static void argloop(int f0) if ((n > 0) && (nst >= 0) && (r = pollfd_rev(nst))) { if (r & (POLLNVAL | POLLERR)) { fprintf(stderr, "poll error: %x\n", r); - close(newtab->u.a.fd); + close(tabnew.fd); free(newtab->tab); free(newtab); newtab = NULL; } else { - i = newtab->u.a.ibuf; - r = read(newtab->u.a.fd, &newtab->u.a.buf[i], sizeof(newtab->u.a.buf) - i - 1); + i = tabnew.ibuf; + r = read(tabnew.fd, &tabnew.buf[i], sizeof(tabnew.buf) - i - 1); if (r <= 0) { - fprintf(stderr, "read error(%d): %d\n", errno, newtab->u.a.fd); - close(newtab->u.a.fd); + fprintf(stderr, "read error(%d): %d\n", errno, tabnew.fd); + close(tabnew.fd); free(newtab->tab); free(newtab); newtab = NULL; } else { int j = 0; i += r; - while ((r = siline(newtab, &newtab->u.a.buf[j], i)) >= 0) { + while ((r = siline(newtab, &tabnew.buf[j], i)) >= 0) { j += r; i -= r; } switch (r) { case -ENOBUFS: - close(newtab->u.a.fd); + close(tabnew.fd); #ifdef DEBUG - fprintf(stderr, "done, itab=%d\n", newtab->u.a.itab); - for (i = 0; i < newtab->u.a.itab; i++) { + fprintf(stderr, "done, itab=%d\n", tabnew.itab); + for (i = 0; i < tabnew.itab; i++) { fprintf(stderr, "%lu,", newtab->tab[i]); } fprintf(stderr, "\n"); @@ -2218,13 +2217,13 @@ static void argloop(int f0) break; case -EAGAIN: if (i > 0) { - memmove(&newtab->u.a.buf[0], &newtab->u.a.buf[j], i); + memmove(&tabnew.buf[0], &tabnew.buf[j], i); } - newtab->u.a.ibuf = i; + tabnew.ibuf = i; break; default: fprintf(stderr, "eval error: %d\n", r); - close(newtab->u.a.fd); + close(tabnew.fd); free(newtab->tab); free(newtab); newtab = NULL;