Annotation of multiplexer/en300468ts.c, revision 1.10
1.1 oskar 1: /*
2: * SI table generator (EN 300468)
1.3 oskar 3: * Copyright (C) 2004,2005 Oskar Schirmer (schirmer@scara.com)
1.1 oskar 4: *
5: * This program is free software; you can redistribute it and/or modify
6: * it under the terms of the GNU General Public License as published by
7: * the Free Software Foundation; either version 2 of the License, or
8: * (at your option) any later version.
9: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * You should have received a copy of the GNU General Public License
16: * along with this program; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19:
20: /*
21: * Module: SI table generator
1.5 oskar 22: * Purpose: From a list of descriptive files generate SI tables and feed
1.1 oskar 23: * them to stdout.
24: * known bug: missing check for section length <= 1021
25: */
26:
27: //#define DEBUG
28:
29: #include <limits.h>
30: #include <unistd.h>
31: #include <stdio.h>
32: #include <stdlib.h>
1.6 oskar 33: #include <stdint.h>
1.1 oskar 34: #include <sys/stat.h>
35: #include <fcntl.h>
36: #include <signal.h>
37: #include <sys/poll.h>
38: #include <errno.h>
39: #include <string.h>
40: #include <ctype.h>
41: #include <sys/time.h>
42:
43: #include "crc32.h"
44:
45: #ifdef PATH_MAX
46: #define MY_PATH_MAX PATH_MAX
47: #else
48: #define MY_PATH_MAX 4096
49: #endif
50:
51: static struct pollfd *pfd = NULL;
52: static int npfd = 0;
53: static int ipfd;
54:
55: #define TABLE_PID_FIRST 0x10
56: #define TABLE_PID_LAST 0x1F
57: #define TABBUF_SIZE (1<<15)
58: #define MAX_PSI_SIZE (4096+1)
59:
60: static unsigned int tabin[TABLE_PID_LAST-TABLE_PID_FIRST+1];
61: static unsigned int tabinold[TABLE_PID_LAST-TABLE_PID_FIRST+1];
62: static unsigned int tabout[TABLE_PID_LAST-TABLE_PID_FIRST+1];
63: static unsigned char conticnt[TABLE_PID_LAST-TABLE_PID_FIRST+1];
64: static unsigned char *tabbuf[TABLE_PID_LAST-TABLE_PID_FIRST+1];
65:
66: #define TABLEID_FIRST 0x40
67: #define TABLEID_LAST 0x7F
68:
69: static unsigned char nextversion[TABLEID_LAST-TABLEID_FIRST+1];
70:
71: #define TS_PACKET_SIZE 188
72: #define TS_HEADSLEN 3
73: #define TS_PACKET_HEADSIZE 4
1.4 oskar 74: #define TS_PFIELDLEN 1
1.1 oskar 75: #define TS_SYNC_BYTE 0x47
76: #define OUTBUF_PACKETS 256
77: #define OUTBUF_SIZE (TS_PACKET_SIZE*OUTBUF_PACKETS)
78:
79: static unsigned int outin = 0;
80: static unsigned int outout = 0;
81: static unsigned char outbuf[OUTBUF_SIZE];
82:
83: static void pollfd_init()
84: {
85: ipfd = 0;
86: }
87:
88: static int pollfd_add(int f, short e)
89: {
90: if (npfd <= ipfd) {
91: npfd = 2*npfd+1;
92: pfd = realloc(pfd, npfd * sizeof(struct pollfd));
93: }
94: pfd[ipfd].fd = f;
95: pfd[ipfd].events = e;
96: return ipfd++;
97: }
98:
99: static int pollfd_poll(int timeout)
100: {
101: return poll(pfd, ipfd, timeout);
102: }
103:
104: static int pollfd_rev(int i)
105: {
106: return pfd[i].revents;
107: }
108:
109: static void signalhandler(int sig)
110: {
111: exit(0);
112: }
113:
114: static void system_init()
115: {
116: signal(SIGINT, (void *) (*signalhandler));
117: signal(SIGPIPE, SIG_IGN);
118: }
119:
120: static void unblockf(int f)
121: {
122: int r;
123: r = fcntl(f, F_GETFL);
124: if (r >= 0) {
125: r = fcntl(f, F_SETFL, r | O_NONBLOCK);
126: }
127: if (r < 0) {
128: fprintf(stderr, "fcntl failed(%d): %d\n", errno, f);
129: }
130: }
131:
132: enum enumsi {
1.5 oskar 133: #define ENDEF_BEGIN(name,pid,tableid) name ,
134: #define ENDEF_END
135: #define ENDEF_NUMBER0(name)
136: #define ENDEF_NUMBER1(name)
137: #define ENDEF_DESCR
138: #define ENDEF_DATETIME
139: #define ENDEF_LOOP
140: #define ENDEF_LOOPEND0
141: #include "en300468ts.table"
142: #undef ENDEF_BEGIN
143: #undef ENDEF_END
144: #undef ENDEF_NUMBER0
145: #undef ENDEF_NUMBER1
146: #undef ENDEF_DESCR
147: #undef ENDEF_DATETIME
148: #undef ENDEF_LOOP
149: #undef ENDEF_LOOPEND0
1.1 oskar 150: num_si,
151: si_none = -1
152: };
153:
154: #define DESCR_FIRST 0x40
155: #define DESCR_LAST 0x6E
156: #define PMT_POSS 0
157:
158: static unsigned char descrcnt[DESCR_LAST-DESCR_FIRST+1];
159:
160: const static unsigned char possible_descr[DESCR_LAST-DESCR_FIRST+1] = {
161: (1<<nit),
162: (1<<nit) | (1<<bat),
163: 0 /* (1<<nit) | (1<<bat) | (1<<sdt) | (1<<eit) | (1<<sit) */,
164: (1<<nit),
165: (1<<nit),
166: PMT_POSS,
167: PMT_POSS,
168: (1<<bat) | (1<<sdt) | (1<<sit),
169: (1<<sdt) | (1<<sit),
170: (1<<bat) | (1<<sdt) | (1<<sit),
171: (1<<nit) | (1<<bat) | (1<<sdt) | (1<<eit) | (1<<sit),
172: (1<<sdt) | (1<<sit),
173: (1<<sdt) | (1<<sit),
174: (1<<eit) | (1<<sit),
175: (1<<eit) | (1<<sit),
176: (1<<eit) | (1<<sit),
177: (1<<eit) | (1<<sit),
178: (1<<sdt) | PMT_POSS | (1<<sit),
179: PMT_POSS,
180: (1<<bat) | (1<<sdt) | (1<<eit) | (1<<sit),
181: (1<<eit) | (1<<sit),
182: (1<<eit) | (1<<sit),
183: PMT_POSS,
184: (1<<sdt) | (1<<eit) | (1<<sit),
185: (1<<tot),
186: PMT_POSS,
187: (1<<nit),
188: (1<<nit),
189: (1<<bat),
190: (1<<sdt) | (1<<sit),
191: (1<<eit) | (1<<sit),
192: (1<<nit) | (1<<bat) | (1<<sdt) | (1<<eit) | PMT_POSS | (1<<sit),
193: PMT_POSS,
194: (1<<eit) | (1<<sit),
195: (1<<nit),
196: (1<<sit),
197: (1<<sdt) | (1<<eit) | (1<<sit),
198: PMT_POSS,
199: PMT_POSS,
200: 0,
201: 0,
202: (1<<eit),
203: PMT_POSS,
204: PMT_POSS,
205: (1<<nit),
206: (1<<nit),
207: (1<<nit)
208: };
209:
210: #define LOOP_DEPTH 4
211: #define REALLOC_CHUNK 32
212:
213: struct sitab {
214: struct sitab *next;
215: char typ;
216: unsigned char version;
217: unsigned char tableid;
218: long pid;
219: long freqmsec;
220: enum enumsi esi;
221: unsigned long *tab;
222: union {
223: struct {
224: int tablen;
225: int itab;
226: int fd;
227: int isdescr; /* 0: main table, >0: descr loop depth */
228: int descrtag;
229: int isyn;
230: int isyntab;
231: int numcount;
232: int loopbegin[LOOP_DEPTH];
233: int loopcount[LOOP_DEPTH];
234: int ibuf;
235: char buf[256];
236: } a;
237: struct {
238: struct timeval next;
239: } s;
240: } u;
241: unsigned char descrnum[DESCR_LAST-DESCR_FIRST+1];
242: };
243:
244: static struct sitab *newtab = NULL;
245: static struct sitab *runtab = NULL;
246:
247: #define SYNTAX_END 0
248: #define SYNTAX_LOOPEND 1
249: #define SYNTAX_LOOP 2
250: #define SYNTAX_DESCR 3
251: #define SYNTAX_DESCRTAG 4
252: #define SYNTAX_DATETIME 5
253: #define SYNTAX_STRING 6
1.5 oskar 254: #define SYNTAX_NUMBER -1
1.1 oskar 255:
1.5 oskar 256: #define ENDEF_BEGIN(name,tag) \
257: const static signed char syntax_##name [] = {SYNTAX_DESCRTAG,
258: #define ENDEF_END SYNTAX_END};
259: #define ENDEF_NUMBER0(name) SYNTAX_NUMBER
260: #define ENDEF_NUMBER1(name) SYNTAX_NUMBER,
261: #define ENDEF_DATETIME SYNTAX_DATETIME,
262: #define ENDEF_LOOP SYNTAX_LOOP,
263: #define ENDEF_LOOPEND0
264: #define ENDEF_LOOPEND1 SYNTAX_LOOPEND,
265: #define ENDEF_STRING(name) SYNTAX_STRING,
266: #define ENDEF_STRING3(name) SYNTAX_STRING,
267: #include "en300468ts.descr"
268: #undef ENDEF_BEGIN
269: #undef ENDEF_END
270: #undef ENDEF_NUMBER0
271: #undef ENDEF_NUMBER1
272: #undef ENDEF_DATETIME
273: #undef ENDEF_LOOP
274: #undef ENDEF_LOOPEND0
275: #undef ENDEF_LOOPEND1
276: #undef ENDEF_STRING
277: #undef ENDEF_STRING3
1.1 oskar 278:
279: static unsigned char *gen_network_name(struct sitab *st, unsigned char *p,
280: unsigned long **tt)
281: {
282: unsigned char *d = p;
283: unsigned long *t = *tt;
284: int i;
285: i = *t++;
286: memcpy(d, t, i);
287: d += i;
288: t += (i + sizeof(long) - 1) / sizeof(long);
289: *tt = t;
290: return d;
291: }
292:
293: static unsigned char *gen_service_list(struct sitab *st, unsigned char *p,
294: unsigned long **tt)
295: {
296: unsigned char *d = p;
297: unsigned long *t = *tt;
298: int i;
299: i = *t++;
300: while (i > 0) {
301: *d++ = *t >> 8;
302: *d++ = *t++;
303: *d++ = *t++;
304: i -= 1;
305: }
306: *tt = t;
307: return d;
308: }
309:
310: static unsigned char *gen_satellite_delivery_system(struct sitab *st,
311: unsigned char *p, unsigned long **tt)
312: {
313: unsigned char *d = p;
314: unsigned long *t = *tt;
315: unsigned char c;
316: *d++ = *t >> 24;
317: *d++ = *t >> 16;
318: *d++ = *t >> 8;
319: *d++ = *t++;
320: *d++ = *t >> 8;
321: *d++ = *t++;
322: c = *t++ << 7;
323: c |= (*t++ & 0x03) << 5;
324: *d++ = c | (*t++ & 0x1F);
325: *d++ = *t >> 20;
326: *d++ = *t >> 12;
327: *d++ = *t >> 4;
328: c = *t++ << 4;
329: *d++ = c | (*t++ & 0x0F);
330: *tt = t;
331: return d;
332: }
333:
334: static unsigned char *gen_cable_delivery_system(struct sitab *st,
335: unsigned char *p, unsigned long **tt)
336: {
337: unsigned char *d = p;
338: unsigned long *t = *tt;
339: unsigned char c;
340: *d++ = *t >> 24;
341: *d++ = *t >> 16;
342: *d++ = *t >> 8;
343: *d++ = *t++;
344: *d++ = 0xFF;
345: *d++ = 0xF0 | *t++;
346: *d++ = *t++;
347: *d++ = *t >> 20;
348: *d++ = *t >> 12;
349: *d++ = *t >> 4;
350: c = *t++ << 4;
351: *d++ = c | (*t++ & 0x0F);
352: *tt = t;
353: return d;
354: }
355:
356: static unsigned char *gen_bouquet_name(struct sitab *st, unsigned char *p,
357: unsigned long **tt)
358: {
359: unsigned char *d = p;
360: unsigned long *t = *tt;
361: int i;
362: i = *t++;
363: memcpy(d, t, i);
364: d += i;
365: t += (i + sizeof(long) - 1) / sizeof(long);
366: *tt = t;
367: return d;
368: }
369:
370: static unsigned char *gen_service(struct sitab *st, unsigned char *p,
371: unsigned long **tt)
372: {
373: unsigned char *d = p;
374: unsigned long *t = *tt;
375: int i;
376: *d++ = *t++;
377: *d++ = i = *t++;
378: memcpy(d, t, i);
379: d += i;
380: t += (i + sizeof(long) - 1) / sizeof(long);
381: *d++ = i = *t++;
382: memcpy(d, t, i);
383: d += i;
384: t += (i + sizeof(long) - 1) / sizeof(long);
385: *tt = t;
386: return d;
387: }
388:
389: static unsigned char *gen_country_availability(struct sitab *st,
390: unsigned char *p, unsigned long **tt)
391: {
392: unsigned char *d = p;
393: unsigned long *t = *tt;
394: int i, j;
395: *d++ = (*t++ << 7) | 0x7F;
396: i = *t++;
397: while (i > 0) {
398: j = *t++;
399: memcpy(d, t, 3);
400: d += 3;
401: t += (j + sizeof(long) - 1) / sizeof(long);
402: i -= 1;
403: }
404: *tt = t;
405: return d;
406: }
407:
408: static unsigned char *gen_linkage(struct sitab *st, unsigned char *p,
409: unsigned long **tt)
410: {
411: unsigned char *d = p;
412: unsigned long *t = *tt;
413: int i;
414: unsigned char l, h, o;
415: unsigned long n, s;
416: *d++ = *t >> 8;
417: *d++ = *t++;
418: *d++ = *t >> 8;
419: *d++ = *t++;
420: *d++ = *t >> 8;
421: *d++ = *t++;
422: *d++ = l = *t++;
423: h = *t++ & 0x0F;
424: o = *t++ & 0x01;
425: n = *t++;
426: s = *t++;
427: if (l == 8) {
428: *d++ = (h << 4) | 0x0E | o;
429: if ((h >= 1) && (h <= 3)) {
430: *d++ = n >> 8;
431: *d++ = n;
432: }
433: if (o == 0) {
434: *d++ = s >> 8;
435: *d++ = s;
436: }
437: }
438: i = *t++;
439: while (i > 0) {
440: *d++ = *t++;
441: i -= 1;
442: }
443: *tt = t;
444: return d;
445: }
446:
447: static unsigned char *gen_nvod_reference(struct sitab *st, unsigned char *p,
448: unsigned long **tt)
449: {
450: unsigned char *d = p;
451: unsigned long *t = *tt;
452: int i;
453: i = *t++;
454: while (i > 0) {
455: *d++ = *t >> 8;
456: *d++ = *t++;
457: *d++ = *t >> 8;
458: *d++ = *t++;
459: *d++ = *t >> 8;
460: *d++ = *t++;
461: i -= 1;
462: }
463: *tt = t;
464: return d;
465: }
466:
467: static unsigned char *gen_time_shifted_service(struct sitab *st,
468: unsigned char *p, unsigned long **tt)
469: {
470: unsigned char *d = p;
471: unsigned long *t = *tt;
472: *d++ = *t >> 8;
473: *d++ = *t++;
474: *tt = t;
475: return d;
476: }
477:
478: static unsigned char *gen_short_event(struct sitab *st, unsigned char *p,
479: unsigned long **tt)
480: {
481: unsigned char *d = p;
482: unsigned long *t = *tt;
483: int i;
484: i = *t++;
485: memcpy(d, t, 3);
486: d += 3;
487: t += (i + sizeof(long) - 1) / sizeof(long);
488: *d++ = i = *t++;
489: memcpy(d, t, i);
490: d += i;
491: t += (i + sizeof(long) - 1) / sizeof(long);
492: *d++ = i = *t++;
493: memcpy(d, t, i);
494: d += i;
495: t += (i + sizeof(long) - 1) / sizeof(long);
496: *tt = t;
497: return d;
498: }
499:
500: static unsigned char *gen_extended_event(struct sitab *st, unsigned char *p,
501: unsigned long **tt)
502: {
503: unsigned char *d = p;
504: unsigned long *t = *tt;
505: int i, j;
506: *d++ = (descrcnt[0x4E - DESCR_FIRST] << 4)
507: | (st->descrnum[0x4E - DESCR_FIRST] & 0x0F);
508: i = *t++;
509: memcpy(d, t, 3);
510: d += 4;
511: t += (i + sizeof(long) - 1) / sizeof(long);
512: i = *t++;
513: while (i > 0) {
514: *d++ = j = *t++;
515: memcpy(d, t, j);
516: d += j;
517: t += (j + sizeof(long) - 1) / sizeof(long);
518: *d++ = j = *t++;
519: memcpy(d, t, j);
520: d += j;
521: t += (j + sizeof(long) - 1) / sizeof(long);
522: i -= 1;
523: }
524: p[4] = d-p-5;
525: *d++ = i = *t++;
526: memcpy(d, t, i);
527: d += i;
528: t += (i + sizeof(long) - 1) / sizeof(long);
529: *tt = t;
530: return d;
531: }
532:
533: static unsigned char *gen_time_shifted_event(struct sitab *st, unsigned char *p,
534: unsigned long **tt)
535: {
536: unsigned char *d = p;
537: unsigned long *t = *tt;
538: *d++ = *t >> 8;
539: *d++ = *t++;
540: *d++ = *t >> 8;
541: *d++ = *t++;
542: *tt = t;
543: return d;
544: }
545:
546: static unsigned char *gen_component(struct sitab *st, unsigned char *p,
547: unsigned long **tt)
548: {
549: unsigned char *d = p;
550: unsigned long *t = *tt;
551: int i;
552: *d++ = 0xF0 | *t++;
553: *d++ = *t++;
554: *d++ = *t++;
555: i = *t++;
556: memcpy(d, t, 3);
557: d += 3;
558: t += (i + sizeof(long) - 1) / sizeof(long);
559: i = *t++;
560: memcpy(d, t, i);
561: d += i;
562: t += (i + sizeof(long) - 1) / sizeof(long);
563: *tt = t;
564: return d;
565: }
566:
567: static unsigned char *gen_mosaic(struct sitab *st, unsigned char *p,
568: unsigned long **tt)
569: {
570: unsigned char *d = p;
571: unsigned long *t = *tt;
572: unsigned char *e;
573: unsigned char c;
574: int i, j;
575: c = *t++ << 7;
576: c = c | ((*t++ & 0x07) << 4);
577: *d++ = c | 0x08 | (*t++ & 0x07);
578: i = *t++;
579: while (i > 0) {
580: *d++ = (*t++ << 2) | 0x03;
581: *d++ = 0xF8 | *t++;
582: e = d++;
583: j = *t++;
584: while (j > 0) {
585: *d++ = 0xC0 | *t++;
586: j -= 1;
587: }
588: *e = d-e-1;
589: switch (*d++ = *t++) {
590: case 0x01:
591: *d++ = t[0] >> 8;
592: *d++ = t[0];
593: break;
594: case 0x02:
595: case 0x03:
596: *d++ = t[1] >> 8;
597: *d++ = t[1];
598: *d++ = t[2] >> 8;
599: *d++ = t[2];
600: *d++ = t[3] >> 8;
601: *d++ = t[3];
602: break;
603: case 0x04:
604: *d++ = t[1] >> 8;
605: *d++ = t[1];
606: *d++ = t[2] >> 8;
607: *d++ = t[2];
608: *d++ = t[3] >> 8;
609: *d++ = t[3];
610: *d++ = t[4] >> 8;
611: *d++ = t[4];
612: break;
613: }
614: t += 5;
615: i -= 1;
616: }
617: *tt = t;
618: return d;
619: }
620:
621: static unsigned char *gen_ca_identifier(struct sitab *st, unsigned char *p,
622: unsigned long **tt)
623: {
624: unsigned char *d = p;
625: unsigned long *t = *tt;
626: int i;
627: i = *t++;
628: while (i > 0) {
629: *d++ = *t >> 8;
630: *d++ = *t++;
631: i -= 1;
632: }
633: *tt = t;
634: return d;
635: }
636:
637: static unsigned char *gen_content(struct sitab *st, unsigned char *p,
638: unsigned long **tt)
639: {
640: unsigned char *d = p;
641: unsigned long *t = *tt;
642: int i;
643: i = *t++;
644: while (i > 0) {
645: *d++ = *t++;
646: *d++ = *t++;
647: i -= 1;
648: }
649: *tt = t;
650: return d;
651: }
652:
653: static unsigned char *gen_parental_rating(struct sitab *st, unsigned char *p,
654: unsigned long **tt)
655: {
656: unsigned char *d = p;
657: unsigned long *t = *tt;
658: int i, j;
659: i = *t++;
660: while (i > 0) {
661: j = *t++;
662: memcpy(d, t, 3);
663: d += 3;
664: t += (j + sizeof(long) - 1) / sizeof(long);
665: *d++ = *t++;
666: i -= 1;
667: }
668: *tt = t;
669: return d;
670: }
671:
672: static unsigned char *gen_telephone(struct sitab *st, unsigned char *p,
673: unsigned long **tt)
674: {
675: unsigned char *d = p;
676: unsigned long *t = *tt;
677: unsigned char c;
678: int i;
679: c = *t++ << 5;
680: *d = 0xC0 | c | (*t++ & 0x1F);
681: d += 3;
682: i = *t++;
683: c = i << 5;
684: memcpy(d, t, i);
685: d += i;
686: t += (i + sizeof(long) - 1) / sizeof(long);
687: i = *t++;
688: c |= (i & 0x07) << 2;
689: memcpy(d, t, i);
690: d += i;
691: t += (i + sizeof(long) - 1) / sizeof(long);
692: i = *t++;
693: p[1] = 0x80 | c | (i & 0x03);
694: memcpy(d, t, i);
695: d += i;
696: t += (i + sizeof(long) - 1) / sizeof(long);
697: i = *t++;
698: c = i << 4;
699: memcpy(d, t, i);
700: d += i;
701: t += (i + sizeof(long) - 1) / sizeof(long);
702: i = *t++;
703: p[2] = 0x80 | c | (i & 0x0F);
704: memcpy(d, t, i);
705: d += i;
706: t += (i + sizeof(long) - 1) / sizeof(long);
707: *tt = t;
708: return d;
709: }
710:
711: static unsigned char *gen_local_time_offset(struct sitab *st, unsigned char *p,
712: unsigned long **tt)
713: {
714: unsigned char *d = p;
715: unsigned long *t = *tt;
716: unsigned char c;
717: int i, j;
718: i = *t++;
719: while (i > 0) {
720: j = *t++;
721: memcpy(d, t, 3);
722: d += 3;
723: t += (j + sizeof(long) - 1) / sizeof(long);
724: c = *t++ << 2;
725: *d++ = c | 0x02 | (*t++ & 0x01);
726: *d++ = *t >> 8;
727: *d++ = *t++;
728: *d++ = *t >> 8;
729: *d++ = *t++;
730: *d++ = *t >> 16;
731: *d++ = *t >> 8;
732: *d++ = *t++;
733: *d++ = *t >> 8;
734: *d++ = *t++;
735: i -= 1;
736: }
737: *tt = t;
738: return d;
739: }
740:
741: static unsigned char *gen_terrestrial_delivery_system(struct sitab *st,
742: unsigned char *p, unsigned long **tt)
743: {
744: unsigned char *d = p;
745: unsigned long *t = *tt;
746: unsigned char c;
747: *d++ = *t >> 24;
748: *d++ = *t >> 16;
749: *d++ = *t >> 8;
750: *d++ = *t++;
751: *d++ = (*t++ << 5) | 0x1F;
752: c = *t++ << 6;
753: c |= (*t++ & 0x07) << 3;
754: *d++ = c | (*t++ & 0x07);
755: c = *t++ << 5;
756: c |= (*t++ & 0x03) << 3;
757: c |= (*t++ & 0x03) << 1;
758: *d++ = c | (*t++ & 0x01);
759: *d++ = 0xFF;
760: *d++ = 0xFF;
761: *d++ = 0xFF;
762: *d++ = 0xFF;
763: *tt = t;
764: return d;
765: }
766:
767: static unsigned char *gen_multilingual_network_name(struct sitab *st,
768: unsigned char *p, unsigned long **tt)
769: {
770: unsigned char *d = p;
771: unsigned long *t = *tt;
772: int i, j;
773: i = *t++;
774: while (i > 0) {
775: j = *t++;
776: memcpy(d, t, 3);
777: d += 3;
778: t += (j + sizeof(long) - 1) / sizeof(long);
779: *d++ = j = *t++;
780: memcpy(d, t, j);
781: d += j;
782: t += (j + sizeof(long) - 1) / sizeof(long);
783: i -= 1;
784: }
785: *tt = t;
786: return d;
787: }
788:
789: static unsigned char *gen_multilingual_bouquet_name(struct sitab *st,
790: unsigned char *p, unsigned long **tt)
791: {
792: unsigned char *d = p;
793: unsigned long *t = *tt;
794: int i, j;
795: i = *t++;
796: while (i > 0) {
797: j = *t++;
798: memcpy(d, t, 3);
799: d += 3;
800: t += (j + sizeof(long) - 1) / sizeof(long);
801: *d++ = j = *t++;
802: memcpy(d, t, j);
803: d += j;
804: t += (j + sizeof(long) - 1) / sizeof(long);
805: i -= 1;
806: }
807: *tt = t;
808: return d;
809: }
810:
811: static unsigned char *gen_multilingual_service_name(struct sitab *st,
812: unsigned char *p, unsigned long **tt)
813: {
814: unsigned char *d = p;
815: unsigned long *t = *tt;
816: int i, j;
817: i = *t++;
818: while (i > 0) {
819: j = *t++;
820: memcpy(d, t, 3);
821: d += 3;
822: t += (j + sizeof(long) - 1) / sizeof(long);
823: *d++ = j = *t++;
824: memcpy(d, t, j);
825: d += j;
826: t += (j + sizeof(long) - 1) / sizeof(long);
827: *d++ = j = *t++;
828: memcpy(d, t, j);
829: d += j;
830: t += (j + sizeof(long) - 1) / sizeof(long);
831: i -= 1;
832: }
833: *tt = t;
834: return d;
835: }
836:
837: static unsigned char *gen_multilingual_component(struct sitab *st,
838: unsigned char *p, unsigned long **tt)
839: {
840: unsigned char *d = p;
841: unsigned long *t = *tt;
842: int i, j;
843: *d++ = *t++;
844: i = *t++;
845: while (i > 0) {
846: j = *t++;
847: memcpy(d, t, 3);
848: d += 3;
849: t += (j + sizeof(long) - 1) / sizeof(long);
850: *d++ = j = *t++;
851: memcpy(d, t, j);
852: d += j;
853: t += (j + sizeof(long) - 1) / sizeof(long);
854: i -= 1;
855: }
856: *tt = t;
857: return d;
858: }
859:
860: static unsigned char *gen_private_data_specifier(struct sitab *st,
861: unsigned char *p, unsigned long **tt)
862: {
863: unsigned char *d = p;
864: unsigned long *t = *tt;
865: *d++ = *t >> 24;
866: *d++ = *t >> 16;
867: *d++ = *t >> 8;
868: *d++ = *t++;
869: *tt = t;
870: return d;
871: }
872:
873: static unsigned char *gen_short_smoothing_buffer(struct sitab *st,
874: unsigned char *p, unsigned long **tt)
875: {
876: unsigned char *d = p;
877: unsigned long *t = *tt;
878: unsigned char c;
879: c = *t++ << 6;
880: *d++ = c | (*t++ & 0x3F);
881: *tt = t;
882: return d;
883: }
884:
885: static unsigned char *gen_frequency_list(struct sitab *st, unsigned char *p,
886: unsigned long **tt)
887: {
888: unsigned char *d = p;
889: unsigned long *t = *tt;
890: int i;
891: *d++ = 0xFC | *t++;
892: i = *t++;
893: while (i > 0) {
894: *d++ = *t >> 24;
895: *d++ = *t >> 16;
896: *d++ = *t >> 8;
897: *d++ = *t++;
898: i -= 1;
899: }
900: *tt = t;
901: return d;
902: }
903:
904: static unsigned char *gen_partial_transport_stream(struct sitab *st,
905: unsigned char *p, unsigned long **tt)
906: {
907: unsigned char *d = p;
908: unsigned long *t = *tt;
909: *d++ = 0xC0 | (*t >> 16);
910: *d++ = *t >> 8;
911: *d++ = *t++;
912: *d++ = 0xC0 | (*t >> 16);
913: *d++ = *t >> 8;
914: *d++ = *t++;
915: *d++ = 0xC0 | (*t >> 8);
916: *d++ = *t++;
917: *tt = t;
918: return d;
919: }
920:
921: static unsigned char *gen_data_broadcast(struct sitab *st, unsigned char *p,
922: unsigned long **tt)
923: {
924: unsigned char *d = p;
925: unsigned long *t = *tt;
926: int i;
927: *d++ = *t >> 8;
928: *d++ = *t++;
929: *d++ = *t++;
930: *d++ = i = *t++;
931: while (i > 0) {
932: *d++ = *t++;
933: i -= 1;
934: }
935: i = *t++;
936: memcpy(d, t, 3);
937: d += 3;
938: t += (i + sizeof(long) - 1) / sizeof(long);
939: *d++ = i = *t++;
940: memcpy(d, t, i);
941: d += i;
942: t += (i + sizeof(long) - 1) / sizeof(long);
943: *tt = t;
944: return d;
945: }
946:
947: static unsigned char *gen_pdc(struct sitab *st, unsigned char *p,
948: unsigned long **tt)
949: {
950: unsigned char *d = p;
951: unsigned long *t = *tt;
952: *d++ = 0xF0 | (*t >> 16);
953: *d++ = *t >> 8;
954: *d++ = *t++;
955: *tt = t;
956: return d;
957: }
958:
959: static unsigned char *gen_cell_list(struct sitab *st, unsigned char *p,
960: unsigned long **tt)
961: {
962: unsigned char *d = p;
963: unsigned long *t = *tt;
964: unsigned char c;
965: int i, j;
966: i = *t++;
967: while (i > 0) {
968: *d++ = *t >> 8;
969: *d++ = *t++;
970: *d++ = *t >> 8;
971: *d++ = *t++;
972: *d++ = *t >> 8;
973: *d++ = *t++;
974: *d++ = *t >> 4;
975: c = *t++ << 4;
976: *d++ = c | ((*t >> 8) & 0x0F);
977: *d++ = *t++;
978: j = *t++;
979: *d++ = 8 * j;
980: while (j > 0) {
981: *d++ = *t++;
982: *d++ = *t >> 8;
983: *d++ = *t++;
984: *d++ = *t >> 8;
985: *d++ = *t++;
986: *d++ = *t >> 4;
987: c = *t++ << 4;
988: *d++ = c | ((*t >> 8) & 0x0F);
989: *d++ = *t++;
990: j -= 1;
991: }
992: i -= 1;
993: }
994: *tt = t;
995: return d;
996: }
997:
998: static unsigned char *gen_cell_frequency_link(struct sitab *st,
999: unsigned char *p, unsigned long **tt)
1000: {
1001: unsigned char *d = p;
1002: unsigned long *t = *tt;
1003: int i, j;
1004: i = *t++;
1005: while (i > 0) {
1006: *d++ = *t >> 8;
1007: *d++ = *t++;
1008: *d++ = *t >> 24;
1009: *d++ = *t >> 16;
1010: *d++ = *t >> 8;
1011: *d++ = *t++;
1012: j = *t++;
1013: *d++ = 5 * j;
1014: while (j > 0) {
1015: *d++ = *t++;
1016: *d++ = *t >> 24;
1017: *d++ = *t >> 16;
1018: *d++ = *t >> 8;
1019: *d++ = *t++;
1020: j -= 1;
1021: }
1022: i -= 1;
1023: }
1024: *tt = t;
1025: return d;
1026: }
1027:
1028: static unsigned char *gen_announcement_support(struct sitab *st,
1029: unsigned char *p, unsigned long **tt)
1030: {
1031: unsigned char *d = p;
1032: unsigned long *t = *tt;
1033: unsigned char c, v;
1034: int i;
1035: *d++ = *t >> 8;
1036: *d++ = *t++;
1037: i = *t++;
1038: while (i > 0) {
1039: c = *t++ << 4;
1040: v = *t++ & 0x07;
1041: *d++ = c | 0x08 | v;
1042: switch (v) {
1043: case 0x01:
1044: case 0x02:
1045: case 0x03:
1046: *d++ = *t >> 8;
1047: *d++ = *t++;
1048: *d++ = *t >> 8;
1049: *d++ = *t++;
1050: *d++ = *t >> 8;
1051: *d++ = *t++;
1052: *d++ = *t++;
1053: break;
1054: }
1055: i -= 1;
1056: }
1057: *tt = t;
1058: return d;
1059: }
1060:
1061: const static signed char *const descr_syntax[DESCR_LAST-DESCR_FIRST+1] = {
1062: &syntax_network_name[0],
1063: &syntax_service_list[0],
1064: NULL,
1065: &syntax_satellite_delivery_system[0],
1066: &syntax_cable_delivery_system[0],
1067: NULL,
1068: NULL,
1069: &syntax_bouquet_name[0],
1070: &syntax_service[0],
1071: &syntax_country_availability[0],
1072: &syntax_linkage[0],
1073: &syntax_nvod_reference[0],
1074: &syntax_time_shifted_service[0],
1075: &syntax_short_event[0],
1076: &syntax_extended_event[0],
1077: &syntax_time_shifted_event[0],
1078: &syntax_component[0],
1079: &syntax_mosaic[0],
1080: NULL,
1081: &syntax_ca_identifier[0],
1082: &syntax_content[0],
1083: &syntax_parental_rating[0],
1084: NULL,
1085: &syntax_telephone[0],
1086: &syntax_local_time_offset[0],
1087: NULL,
1088: &syntax_terrestrial_delivery_system[0],
1089: &syntax_multilingual_network_name[0],
1090: &syntax_multilingual_bouquet_name[0],
1091: &syntax_multilingual_service_name[0],
1092: &syntax_multilingual_component[0],
1093: &syntax_private_data_specifier[0],
1094: NULL,
1095: &syntax_short_smoothing_buffer[0],
1096: &syntax_frequency_list[0],
1097: &syntax_partial_transport_stream[0],
1098: &syntax_data_broadcast[0],
1099: NULL,
1100: NULL,
1101: NULL,
1102: NULL,
1103: &syntax_pdc[0],
1104: NULL,
1105: NULL,
1106: &syntax_cell_list[0],
1107: &syntax_cell_frequency_link[0],
1108: &syntax_announcement_support[0]
1109: };
1110:
1111: static unsigned char *gendescr(struct sitab *st, unsigned char *p,
1112: unsigned long **tt, unsigned char msb)
1113: {
1114: unsigned char *d = p+2;
1115: unsigned long *t = *tt;
1116: unsigned char *b;
1117: unsigned char v;
1118: int i;
1119: i = *t++;
1120: while (i > 0) {
1121: b = d+2;
1122: switch (*d = v = *t++) {
1.5 oskar 1123: #define ENDEF_BEGIN(name,tag) case tag : d = gen_##name (st, b, &t); break;
1124: #define ENDEF_END
1125: #define ENDEF_NUMBER0(name)
1126: #define ENDEF_NUMBER1(name)
1127: #define ENDEF_DATETIME
1128: #define ENDEF_LOOP
1129: #define ENDEF_LOOPEND0
1130: #define ENDEF_LOOPEND1
1131: #define ENDEF_STRING(name)
1132: #define ENDEF_STRING3(name)
1133: #include "en300468ts.descr"
1134: #undef ENDEF_BEGIN
1135: #undef ENDEF_END
1136: #undef ENDEF_NUMBER0
1137: #undef ENDEF_NUMBER1
1138: #undef ENDEF_DATETIME
1139: #undef ENDEF_LOOP
1140: #undef ENDEF_LOOPEND0
1141: #undef ENDEF_LOOPEND1
1142: #undef ENDEF_STRING
1143: #undef ENDEF_STRING3
1.1 oskar 1144: default:
1145: fprintf(stderr, "error: descr not implemented (%02x)\n", *d);
1146: exit(1);
1147: }
1148: descrcnt[v-DESCR_FIRST] += 1;
1149: b[-1] = d-b;
1150: i -= 1;
1151: }
1152: i = d-p-2;
1153: p[0] = msb | (i >> 8);
1154: p[1] = i;
1155: *tt = t;
1156: return d;
1157: }
1158:
1.5 oskar 1159: #define ENDEF_BEGIN(name,pid,tableid) \
1160: const static signed char syntax_##name [] = {
1161: #define ENDEF_END SYNTAX_END};
1162: #define ENDEF_NUMBER0(name) SYNTAX_NUMBER
1163: #define ENDEF_NUMBER1(name) SYNTAX_NUMBER,
1164: #define ENDEF_DESCR SYNTAX_DESCR,
1165: #define ENDEF_DATETIME SYNTAX_DATETIME,
1166: #define ENDEF_LOOP SYNTAX_LOOP,
1167: #define ENDEF_LOOPEND0
1168: #include "en300468ts.table"
1169: #undef ENDEF_BEGIN
1170: #undef ENDEF_END
1171: #undef ENDEF_NUMBER0
1172: #undef ENDEF_NUMBER1
1173: #undef ENDEF_DESCR
1174: #undef ENDEF_DATETIME
1175: #undef ENDEF_LOOP
1176: #undef ENDEF_LOOPEND0
1.1 oskar 1177:
1178: static int gentab_nit(struct sitab *st, unsigned char *b)
1179: {
1180: unsigned char *p = b;
1181: unsigned long *t = st->tab;
1182: unsigned char *q;
1183: int i;
1184: *p = st->tableid;
1185: p += 3;
1186: *p++ = *t >> 8;
1187: *p++ = *t++;
1188: *p++ = 0xC0 | (st->version << 1) | 0x01;
1189: *p++ = 0;
1190: *p++ = 0;
1191: p = gendescr(st, p, &t, 0xF0);
1192: q = p;
1193: p += 2;
1194: i = *t++;
1195: while (i > 0) {
1196: *p++ = *t >> 8;
1197: *p++ = *t++;
1198: *p++ = *t >> 8;
1199: *p++ = *t++;
1200: p = gendescr(st, p, &t, 0xF0);
1201: i -= 1;
1202: }
1203: i = p-q-2;
1204: q[0] = 0xF0 | (i >> 8);
1205: q[1] = i;
1206: i = p-b+1;
1207: b[1] = 0xF0 | (i >> 8);
1208: b[2] = i;
1209: crc32_calc(b, i-1, p);
1210: return p-b+4;
1211: }
1212:
1213: static int gentab_sdt(struct sitab *st, unsigned char *b)
1214: {
1215: unsigned char *p = b;
1216: unsigned long *t = st->tab;
1217: int i;
1218: unsigned char c;
1219: *p = st->tableid;
1220: p += 3;
1221: *p++ = *t >> 8;
1222: *p++ = *t++;
1223: *p++ = 0xC0 | (st->version << 1) | 0x01;
1224: *p++ = 0;
1225: *p++ = 0;
1226: *p++ = *t >> 8;
1227: *p++ = *t++;
1228: *p++ = 0xFF;
1229: i = *t++;
1230: while (i > 0) {
1231: *p++ = *t >> 8;
1232: *p++ = *t++;
1233: c = *t++ << 1;
1234: *p++ = 0xFC | c | (*t++ & 1);
1235: c = *t++ << 5;
1236: p = gendescr(st, p, &t, c);
1237: i -= 1;
1238: }
1239: i = p-b+1;
1240: b[1] = 0xF0 | (i >> 8);
1241: b[2] = i;
1242: crc32_calc(b, i-1, p);
1243: return p-b+4;
1244: }
1245:
1246: static int gentab_bat(struct sitab *st, unsigned char *b)
1247: {
1248: unsigned char *p = b;
1249: unsigned long *t = st->tab;
1250: unsigned char *q;
1251: int i;
1252: *p = st->tableid;
1253: p += 3;
1254: *p++ = *t >> 8;
1255: *p++ = *t++;
1256: *p++ = 0xC0 | (st->version << 1) | 0x01;
1257: *p++ = 0;
1258: *p++ = 0;
1259: p = gendescr(st, p, &t, 0xF0);
1260: q = p;
1261: p += 2;
1262: i = *t++;
1263: while (i > 0) {
1264: *p++ = *t >> 8;
1265: *p++ = *t++;
1266: *p++ = *t >> 8;
1267: *p++ = *t++;
1268: p = gendescr(st, p, &t, 0xF0);
1269: i -= 1;
1270: }
1271: i = p-q-2;
1272: q[0] = 0xF0 | (i >> 8);
1273: q[1] = i;
1274: i = p-b+1;
1275: b[1] = 0xF0 | (i >> 8);
1276: b[2] = i;
1277: crc32_calc(b, i-1, p);
1278: return p-b+4;
1279: }
1280:
1281: static int gentab_eit(struct sitab *st, unsigned char *b)
1282: {
1283: unsigned char *p = b;
1284: unsigned long *t = st->tab;
1285: int i;
1.8 oskar 1286: unsigned char c;
1.1 oskar 1287: struct sitab *gt;
1288: *p = i = st->tableid;
1289: p += 3;
1290: *p++ = *t >> 8;
1291: *p++ = *t++;
1292: *p++ = 0xC0 | (st->version << 1) | 0x01;
1293: *p++ = 0;
1294: *p++ = 0;
1295: *p++ = *t >> 8;
1296: *p++ = *t++;
1297: *p++ = *t >> 8;
1298: *p++ = *t++;
1299: *p++ = 0;
1300: gt = runtab;
1301: while (gt != NULL) {
1302: if ((gt->esi == eit) && (gt->tableid > i)) {
1303: i = gt->tableid;
1304: }
1305: gt = gt->next;
1306: }
1307: *p++ = i;
1308: i = *t++;
1.8 oskar 1309: while (i > 0) {
1310: *p++ = *t >> 8;
1311: *p++ = *t++;
1312: *p++ = *t >> 8;
1313: *p++ = *t++;
1314: *p++ = *t >> 16;
1315: *p++ = *t >> 8;
1316: *p++ = *t++;
1317: *p++ = *t >> 16;
1318: *p++ = *t >> 8;
1319: *p++ = *t++;
1320: c = *t++ << 5;
1321: p = gendescr(st, p, &t, c);
1322: i -= 1;
1323: }
1.1 oskar 1324: i = p-b+1;
1325: b[1] = 0xF0 | (i >> 8);
1326: b[2] = i;
1327: crc32_calc(b, i-1, p);
1328: return p-b+4;
1329: }
1330:
1331: static int gentab_rst(struct sitab *st, unsigned char *b)
1332: {
1333: unsigned char *p = b;
1334: unsigned long *t = st->tab;
1335: int i;
1336: *p = st->tableid;
1337: p += 3;
1338: i = *t++;
1339: while (i > 0) {
1340: *p++ = *t >> 8;
1341: *p++ = *t++;
1342: *p++ = *t >> 8;
1343: *p++ = *t++;
1344: *p++ = *t >> 8;
1345: *p++ = *t++;
1346: *p++ = *t >> 8;
1347: *p++ = *t++;
1348: *p++ = 0xF8 | *t++;
1349: i -= 1;
1350: }
1351: i = p-b-3;
1352: b[1] = 0xF0 | (i >> 8);
1353: b[2] = i;
1354: return p-b;
1355: }
1356:
1357: static unsigned char *gentvdatetime(unsigned char *p, struct timeval *tv)
1358: {
1359: unsigned long d, s;
1360: s = tv->tv_sec;
1361: d = s / 86400;
1362: s = s % 86400;
1363: d += 40587;
1364: *p++ = d >> 8;
1365: *p++ = d;
1366: d = s / 3600;
1367: s = s % 3600;
1368: d += (d / 10) * 6;
1369: *p++ = d;
1370: d = s / 60;
1371: s = s % 60;
1372: d += (d / 10) * 6;
1373: *p++ = d;
1374: s += (s / 10) * 6;
1375: *p++ = s;
1376: return p;
1377: }
1378:
1379: static int gentab_tdt(struct sitab *st, unsigned char *b, struct timeval *tv)
1380: {
1381: unsigned char *p = b;
1382: *p++ = st->tableid;
1383: *p++ = 0xF0;
1384: *p++ = 0x05;
1385: p = gentvdatetime(p, tv);
1386: return p-b;
1387: }
1388:
1389: static int gentab_tot(struct sitab *st, unsigned char *b, struct timeval *tv)
1390: {
1391: unsigned char *p = b;
1392: unsigned long *t = st->tab;
1393: int i;
1394: *p = st->tableid;
1395: p += 3;
1396: p = gentvdatetime(p, tv);
1397: p = gendescr(st, p, &t, 0xF0);
1398: i = p-b+1;
1399: b[1] = 0xF0 | (i >> 8);
1400: b[2] = i;
1401: crc32_calc(b, i-1, p);
1402: return p-b+4;
1403: }
1404:
1405: static int gentab_sit(struct sitab *st, unsigned char *b)
1406: {
1407: unsigned char *p = b;
1408: unsigned long *t = st->tab;
1.3 oskar 1409: unsigned char c;
1410: int i;
1.1 oskar 1411: *p = st->tableid;
1.3 oskar 1412: p += 3;
1413: *p++ = 0xFF;
1414: *p++ = 0xFF;
1415: *p++ = 0xC0 | (st->version << 1) | 0x01;
1416: *p++ = 0;
1417: *p++ = 0;
1418: p = gendescr(st, p, &t, 0xF0);
1419: i = *t++;
1420: while (i > 0) {
1421: *p++ = *t >> 8;
1422: *p++ = *t++;
1423: c = *t++ << 4;
1424: p = gendescr(st, p, &t, 0x80 | c);
1425: i -= 1;
1426: }
1427: i = p-b+1;
1428: b[1] = 0xF0 | (i >> 8);
1429: b[2] = i;
1430: crc32_calc(b, i-1, p);
1431: return p-b+4;
1.1 oskar 1432: }
1433:
1434: static int gentab_dit(struct sitab *st, unsigned char *b)
1435: {
1436: unsigned char *p = b;
1437: unsigned long *t = st->tab;
1.3 oskar 1438: *p++ = st->tableid;
1439: *p++ = 0xF0;
1440: *p++ = 0x01;
1441: *p++ = (*t++ << 7) | 0x7F;
1.1 oskar 1442: return p-b;
1443: }
1444:
1445: const static signed char *const tab_syntax[num_si] = {
1.5 oskar 1446: #define ENDEF_BEGIN(name,pid,tableid) &syntax_##name [0],
1447: #define ENDEF_END
1448: #define ENDEF_NUMBER0(name)
1449: #define ENDEF_NUMBER1(name)
1450: #define ENDEF_DESCR
1451: #define ENDEF_DATETIME
1452: #define ENDEF_LOOP
1453: #define ENDEF_LOOPEND0
1454: #include "en300468ts.table"
1455: #undef ENDEF_BEGIN
1456: #undef ENDEF_END
1457: #undef ENDEF_NUMBER0
1458: #undef ENDEF_NUMBER1
1459: #undef ENDEF_DESCR
1460: #undef ENDEF_DATETIME
1461: #undef ENDEF_LOOP
1462: #undef ENDEF_LOOPEND0
1.1 oskar 1463: };
1464:
1465: const static signed char *const *const syntax[2] = {
1466: &tab_syntax[0],
1467: &descr_syntax[0]
1468: };
1469:
1470: static void gentab(struct sitab *st, struct timeval *tv)
1471: {
1.10 ! oskar 1472: int i, l;
1.1 oskar 1473: unsigned char *b;
1474: memset(&descrcnt[0], 0, sizeof(descrcnt));
1.10 ! oskar 1475: i = st->pid - TABLE_PID_FIRST;
! 1476: b = &tabbuf[i][tabin[i]];
1.1 oskar 1477: *b++ = st->pid >> 8;
1478: *b++ = st->pid;
1479: switch (st->esi) {
1480: case nit: l = gentab_nit(st, b); break;
1481: case sdt: l = gentab_sdt(st, b); break;
1482: case bat: l = gentab_bat(st, b); break;
1483: case eit: l = gentab_eit(st, b); break;
1484: case rst: l = gentab_rst(st, b); break;
1485: case tdt: l = gentab_tdt(st, b, tv); break;
1486: case tot: l = gentab_tot(st, b, tv); break;
1487: case sit: l = gentab_sit(st, b); break;
1488: case dit: l = gentab_dit(st, b); break;
1489: default:
1490: fprintf(stderr, "internal error (gentab, %d)\n", st->esi);
1491: exit(1);
1492: }
1.10 ! oskar 1493: tabin[i] += l+2;
1.1 oskar 1494: }
1495:
1496: static enum enumsi alloctab(long pid, long tid)
1497: {
1498: enum enumsi e = si_none;
1499: switch (pid) {
1500: case 0x0010:
1501: if ((tid == 0x40) || (tid == 0x41)) {
1502: e = nit;
1503: } else {
1504: fprintf(stderr, "bad tableid: 0x%02lx (pid: 0x%04lx)\n", tid, pid);
1505: }
1506: break;
1507: case 0x0011:
1508: if ((tid == 0x42) || (tid == 0x46)) {
1509: e = sdt;
1510: } else if (tid == 0x4A) {
1511: e = bat;
1512: } else {
1513: fprintf(stderr, "bad tableid: 0x%02lx (pid: 0x%04lx)\n", tid, pid);
1514: }
1515: break;
1516: case 0x0012:
1517: if ((tid >= 0x4E) && (tid <= 0x6F)) {
1518: e = eit;
1519: } else {
1520: fprintf(stderr, "bad tableid: 0x%02lx (pid: 0x%04lx)\n", tid, pid);
1521: }
1522: break;
1523: case 0x0013:
1524: if (tid == 0x71) {
1525: e = rst;
1526: } else {
1527: fprintf(stderr, "bad tableid: 0x%02lx (pid: 0x%04lx)\n", tid, pid);
1528: }
1529: break;
1530: case 0x0014:
1531: if (tid == 0x70) {
1532: e = tdt;
1533: } else if (tid == 0x73) {
1534: e = tot;
1535: } else {
1536: fprintf(stderr, "bad tableid: 0x%02lx (pid: 0x%04lx)\n", tid, pid);
1537: }
1538: break;
1539: case 0x001E:
1540: if (tid == 0x7E) {
1541: e = dit;
1542: } else {
1543: fprintf(stderr, "bad tableid: 0x%02lx (pid: 0x%04lx)\n", tid, pid);
1544: }
1545: break;
1546: case 0x001F:
1547: if (tid == 0x7F) {
1548: e = sit;
1549: } else {
1550: fprintf(stderr, "bad tableid: 0x%02lx (pid: 0x%04lx)\n", tid, pid);
1551: }
1552: break;
1553: default:
1554: fprintf(stderr, "bad pid: 0x%04lx\n", pid);
1555: break;
1556: }
1557: return e;
1558: }
1559:
1560: static struct sitab **findtab(struct sitab **pst, long pid, long tableid)
1561: {
1562: struct sitab *st = *pst;
1563: while (st != NULL) {
1564: if ((st->pid == pid) && (st->tableid == tableid)) {
1565: return pst;
1566: }
1567: pst = &st->next;
1568: st = *pst;
1569: }
1570: return NULL;
1571: }
1572:
1573: static void droptab(long pid, long tableid)
1574: {
1575: struct sitab **pst;
1576: struct sitab *st;
1577: pst = &newtab;
1578: while ((pst = findtab(pst, pid, tableid)) != NULL) {
1579: st = *pst;
1580: *pst = st->next;
1581: close(st->u.a.fd);
1582: free(st->tab);
1583: free(st);
1584: }
1585: pst = &runtab;
1586: while ((pst = findtab(pst, pid, tableid)) != NULL) {
1587: st = *pst;
1588: *pst = st->next;
1589: nextversion[st->tableid - TABLEID_FIRST] = (st->version + 1) & 0x1F;
1590: free(st->tab);
1591: free(st);
1592: }
1593: }
1594:
1595: static void maketab(char typ, long pid, long tableid, long freqmsec, int fd)
1596: {
1597: struct sitab **pt;
1598: struct sitab *t;
1599: enum enumsi e;
1600: e = alloctab(pid, tableid);
1601: if (e >= 0) {
1602: pt = findtab(&newtab, pid, tableid);
1603: if (pt == NULL) {
1604: pt = findtab(&runtab, pid, tableid);
1605: } else {
1606: t = *pt;
1.2 oskar 1607: #ifdef DEBUG
1608: fprintf(stderr, "close old fd: %d\n", t->u.a.fd);
1609: #endif
1.1 oskar 1610: close(t->u.a.fd);
1611: }
1612: if (pt == NULL) {
1613: t = malloc(sizeof(struct sitab));
1614: t->pid = pid;
1615: t->tableid = tableid;
1616: t->version = nextversion[tableid - TABLEID_FIRST];
1617: } else {
1618: t = *pt;
1619: *pt = t->next;
1620: free(t->tab);
1621: t->version = (t->version + 1) & 0x1F;
1622: }
1623: t->typ = typ;
1624: t->freqmsec = freqmsec;
1625: t->esi = e;
1626: t->tab = NULL;
1627: memset(&t->descrnum[0], 0, sizeof(t->descrnum[0]));
1.2 oskar 1628: memset(&t->u, 0, sizeof(t->u));
1.1 oskar 1629: t->u.a.fd = fd;
1630: t->next = newtab;
1631: newtab = t;
1632: }
1633: }
1634:
1635: static int tabline(char *b, int n)
1636: {
1637: char *e, *a, *z;
1638: int i;
1639: long v[3];
1640: char c;
1641: if ((n <= 0) || ((e = memchr(b, '\n', n)) == NULL)) {
1642: return 0;
1643: }
1644: *e = 0;
1645: if (b >= e) {
1646: fprintf(stderr, "incomplete line: %s\n", b);
1647: } else {
1648: z = b+1;
1649: i = 0;
1650: c = toupper(*b);
1651: switch (c) {
1652: case '-':
1653: do {
1654: a = z;
1655: v[i] = strtol(a, &z, 0);
1656: } while ((a != z) && (++i < 2));
1657: if (a == z) {
1658: fprintf(stderr, "invalid line(%d): %s\n", a-b, b);
1659: } else {
1660: droptab(v[0], v[1]);
1661: }
1662: break;
1663: case 'S':
1664: do {
1665: a = z;
1666: v[i] = strtol(a, &z, 0);
1667: } while ((a != z) && (++i < 3));
1668: if (a == z) {
1669: fprintf(stderr, "invalid line(%d): %s\n", a-b, b);
1670: } else {
1671: while (isspace(*z)) {
1672: z += 1;
1673: }
1674: i = open(z, O_RDONLY | O_NONBLOCK);
1675: if (i < 0) {
1676: fprintf(stderr, "open failed(%d): %s\n", errno, z);
1677: } else {
1678: #ifdef DEBUG
1679: fprintf(stderr, "table: %c, %ld, %ld, %ld, <%s>\n",
1680: c, v[0], v[1], v[2], z);
1681: #endif
1682: maketab(c, v[0], v[1], v[2], i);
1683: }
1684: }
1685: break;
1686: }
1687: }
1688: return e-b+1;
1689: }
1690:
1691: static int taballoc(struct sitab *st, int cnt)
1692: {
1693: while (st->u.a.tablen < (st->u.a.itab + cnt)) {
1694: st->u.a.tablen += REALLOC_CHUNK;
1695: st->tab = realloc(st->tab, st->u.a.tablen * sizeof(unsigned long));
1696: if (st->tab == NULL) {
1697: return -ENOMEM;
1698: }
1699: }
1700: return 0;
1701: }
1702:
1703: static long longval(char **n, int d)
1704: {
1705: long v;
1706: int i;
1707: char c;
1708: v = 0;
1709: for (i = 0; i < d; i++) {
1710: c = *(*n)++;
1711: if (!isdigit(c)) {
1712: return -ENOMEM;
1713: }
1714: v = 10*v+c-'0';
1715: }
1716: return v;
1717: }
1718:
1719: static int siline(struct sitab *st, char *b, int n)
1720: {
1721: char *e, *a, *z;
1722: long v;
1723: int s, i;
1724: s = syntax[(st->u.a.isdescr > 0) ? 1 : 0]
1725: [(st->u.a.isdescr > 0) ? st->u.a.descrtag : st->esi][st->u.a.isyn];
1726: if ((s == SYNTAX_END) || (s == SYNTAX_LOOPEND)) {
1727: int lc;
1728: #ifdef DEBUG
1729: fprintf(stderr, "end:\n");
1730: #endif
1731: if ((lc = --st->u.a.loopcount[0]) <= 0) {
1732: if (lc == 0) {
1733: for (i = 0; i < LOOP_DEPTH-1; i++) {
1734: st->u.a.loopbegin[i] = st->u.a.loopbegin[i+1];
1735: st->u.a.loopcount[i] = st->u.a.loopcount[i+1];
1736: }
1737: st->u.a.loopcount[LOOP_DEPTH-1] = 0;
1738: if (s == SYNTAX_LOOPEND) {
1739: st->u.a.isyn += 1;
1740: } else if (st->u.a.isdescr > 0) {
1741: if (--st->u.a.isdescr == 0) {
1742: st->u.a.isyn = st->u.a.isyntab;
1743: }
1744: }
1745: return 0;
1746: } else {
1747: st->u.a.tablen = st->u.a.itab;
1748: st->tab = realloc(st->tab, st->u.a.itab * sizeof(unsigned long));
1749: return -ENOBUFS;
1750: }
1751: } else {
1752: st->u.a.isyn = st->u.a.loopbegin[0];
1753: return 0;
1754: }
1755: }
1756: if ((n <= 0) || ((e = memchr(b, '\n', n)) == NULL)) {
1757: return -EAGAIN;
1758: }
1759: *e = 0;
1760: a = b;
1761: while (isspace(*a)) {
1762: a += 1;
1763: }
1764: if (taballoc(st, 1)) {
1765: return -ENOMEM;
1766: }
1767: if (a != e) {
1768: switch (s) {
1769: default:
1770: if (s > 0) {
1771: fprintf(stderr, "internal syntax error\n");
1772: exit(1);
1773: }
1774: if (st->u.a.numcount == 0) {
1775: st->u.a.numcount = s;
1776: }
1777: v = strtoul(a, &z, 0);
1778: if (a == z) {
1779: return -EINVAL;
1780: }
1781: #ifdef DEBUG
1782: fprintf(stderr, "number: %ld, %d..%d\n", v, a-b, z-b);
1783: #endif
1784: st->tab[st->u.a.itab++] = v;
1785: if (++st->u.a.numcount == 0) {
1786: st->u.a.isyn += 1;
1787: }
1788: *e = '\n';
1789: return z-b;
1790: case SYNTAX_LOOP:
1791: v = strtol(a, &z, 0);
1792: if (a == z) {
1793: return -EINVAL;
1794: }
1795: #ifdef DEBUG
1796: fprintf(stderr, "loop: %ld, %d..%d\n", v, a-b, z-b);
1797: #endif
1798: st->tab[st->u.a.itab++] = v;
1799: if (v != 0) {
1800: if (st->u.a.isdescr > 0) {
1801: st->u.a.isdescr += 1;
1802: }
1.7 oskar 1803: for (i = LOOP_DEPTH-2; i >= 0; i--) {
1.1 oskar 1804: st->u.a.loopbegin[i+1] = st->u.a.loopbegin[i];
1805: st->u.a.loopcount[i+1] = st->u.a.loopcount[i];
1806: }
1807: st->u.a.loopbegin[0] = ++st->u.a.isyn;
1808: st->u.a.loopcount[0] = v;
1809: } else {
1810: do {
1811: st->u.a.isyn += 1;
1812: s = syntax[(st->u.a.isdescr > 0) ? 1 : 0]
1813: [(st->u.a.isdescr > 0) ? st->u.a.descrtag : st->esi]
1814: [st->u.a.isyn];
1815: if (s == SYNTAX_LOOP) {
1816: v += 1;
1817: }
1818: } while ((s != SYNTAX_END) && ((s != SYNTAX_LOOPEND) || (--v >= 0)));
1819: if (s == SYNTAX_LOOPEND) {
1820: st->u.a.isyn += 1;
1821: }
1822: }
1823: *e = '\n';
1824: return z-b;
1825: case SYNTAX_DESCR:
1826: v = strtol(a, &z, 0);
1827: if (a == z) {
1828: return -EINVAL;
1829: }
1830: #ifdef DEBUG
1831: fprintf(stderr, "descr: %ld, %d..%d\n", v, a-b, z-b);
1832: #endif
1833: st->tab[st->u.a.itab++] = v;
1834: st->u.a.isyn += 1;
1835: if (v != 0) {
1836: st->u.a.isdescr = 1;
1837: st->u.a.descrtag = 0;
1.7 oskar 1838: for (i = LOOP_DEPTH-2; i >= 0; i--) {
1.1 oskar 1839: st->u.a.loopbegin[i+1] = st->u.a.loopbegin[i];
1840: st->u.a.loopcount[i+1] = st->u.a.loopcount[i];
1841: }
1842: st->u.a.loopbegin[0] = 0;
1843: st->u.a.loopcount[0] = v;
1844: st->u.a.isyntab = st->u.a.isyn;
1845: st->u.a.isyn = 0;
1846: }
1847: *e = '\n';
1848: return z-b;
1849: case SYNTAX_DESCRTAG:
1850: v = strtol(a, &z, 0);
1851: if (a == z) {
1852: return -EINVAL;
1853: }
1854: #ifdef DEBUG
1855: fprintf(stderr, "descrtag: %ld, %d..%d\n", v, a-b, z-b);
1856: #endif
1857: st->tab[st->u.a.itab++] = v;
1858: v -= DESCR_FIRST;
1859: if ((v < 0) || (v > (DESCR_LAST - DESCR_FIRST))) {
1860: return -EINVAL;
1861: }
1862: st->descrnum[v] += 1;
1863: if (!((1 << st->esi) & possible_descr[v])) {
1864: return -EINVAL;
1865: }
1866: st->u.a.descrtag = v;
1867: st->u.a.isyn += 1;
1868: *e = '\n';
1869: return z-b;
1870: case SYNTAX_DATETIME:
1871: /* yyyy/mm/dd hh:mm:ss */
1872: z = a;
1873: i = e-z;
1874: if (i < 19) {
1875: return -EINVAL;
1876: }
1877: if (taballoc(st, 2)) {
1878: return -ENOMEM;
1879: }
1880: if ((v = longval(&z, 4)) < 0) {
1881: return v;
1882: }
1883: if ((v < 1582) || (*z++ != '/')) {
1884: return -EINVAL;
1885: }
1886: if ((s = longval(&z, 2)) < 0) {
1887: return s;
1888: }
1889: s -= 1;
1890: if ((s < 0) || (s > 11) || (*z++ != '/')) {
1891: return -EINVAL;
1892: }
1893: s -= 2;
1894: if (s < 0) {
1895: s += 12;
1896: v -= 1;
1897: }
1898: v = (1461*v)/4 - ((v/100+1)*3)/4 + (153*s)/5;
1899: if ((s = longval(&z, 2)) < 0) {
1900: return s;
1901: }
1902: if ((s <= 0) || (s > 31)) {
1903: return -EINVAL;
1904: }
1905: v += s - 678912;
1906: st->tab[st->u.a.itab++] = v;
1907: v = 0;
1908: s = ' ';
1909: for (i = 2; i >= 0; i--) {
1910: if (*z++ != s) {
1911: return -EINVAL;
1912: }
1913: s = *z++;
1914: if (!isdigit(s)) {
1915: return -EINVAL;
1916: }
1917: v = (v<<4) + (s-'0');
1918: s = *z++;
1919: if (!isdigit(s)) {
1920: return -EINVAL;
1921: }
1922: v = (v<<4) + (s-'0');
1923: s = ':';
1924: }
1925: st->tab[st->u.a.itab++] = v;
1926: #ifdef DEBUG
1927: fprintf(stderr, "datetime: %04lx %06lx, %d..%d\n",
1928: st->tab[st->u.a.itab-2], v, a-b, z-b);
1929: #endif
1930: st->u.a.isyn += 1;
1931: *e = '\n';
1932: return z-b;
1933: case SYNTAX_STRING:
1934: if (*a++ != '"') {
1935: return -EINVAL;
1936: }
1937: v = 0;
1938: z = strchr(a, '"');
1939: if (z == NULL) {
1940: return -EINVAL;
1941: }
1942: i = v;
1943: v += z-a;
1944: taballoc(st, 1 + (v + sizeof(long) - 1) / sizeof(long));
1945: memcpy(((char *)&st->tab[st->u.a.itab+1]) + i, a, z-a);
1946: z += 1;
1947: while (*z == '"') {
1948: a = z;
1949: z = strchr(a+1, '"');
1950: if (z == NULL) {
1951: return -EINVAL;
1952: }
1953: i = v;
1954: v += z-a;
1955: taballoc(st, 1 + (v + sizeof(long) - 1) / sizeof(long));
1956: memcpy(((char *)&st->tab[st->u.a.itab+1]) + i, a, z-a);
1957: z += 1;
1958: }
1959: st->tab[st->u.a.itab] = v;
1960: #ifdef DEBUG
1961: fprintf(stderr, "string: %ld, %d..%d\n", v, a-b, z-b);
1962: #endif
1963: st->u.a.itab += 1 + (v + sizeof(long) - 1) / sizeof(long);
1964: st->u.a.isyn += 1;
1965: *e = '\n';
1966: return z-b;
1967: }
1968: } else {
1969: return e-b+1;
1970: }
1971: }
1972:
1973: static unsigned int tab2ts(unsigned char *t, unsigned char *conticnt)
1974: {
1975: unsigned int l, d;
1976: unsigned char *i = &t[2];
1977: unsigned char *o;
1978: unsigned char c = *conticnt;
1.4 oskar 1979: l = ((t[3] & 0x0F) << 8) + t[4] + TS_HEADSLEN + TS_PFIELDLEN;
1.1 oskar 1980: d = (l-1) % (TS_PACKET_SIZE - TS_PACKET_HEADSIZE) + 1;
1981: if (outin >= OUTBUF_SIZE) {
1982: outin = 0;
1983: }
1.9 oskar 1984: #ifdef DEBUG
1985: fprintf(stderr, "tab2ts(%02x,%02x,%02x,%02x,%02x; %2d), l=%d, d=%d, o:%d\n",
1986: t[0], t[1], t[2], t[3], t[4], c, l, d, outin);
1987: #endif
1.1 oskar 1988: o = &outbuf[outin];
1989: if (d <= (TS_PACKET_SIZE - TS_PACKET_HEADSIZE - 1)) {
1990: if (d < (TS_PACKET_SIZE - TS_PACKET_HEADSIZE - 1)) {
1991: o[5] = 0; /* no indicators, no flags, padding: */
1992: memset(&o[6], -1, TS_PACKET_SIZE - TS_PACKET_HEADSIZE - 2 - d);
1993: }
1994: o[4] = TS_PACKET_SIZE - TS_PACKET_HEADSIZE - 1 - d;
1995: o[3] = (0x00 << 6) | (0x03 << 4) | c;
1996: } else {
1997: o[3] = (0x00 << 6) | (0x01 << 4) | c;
1998: }
1.4 oskar 1999: o[TS_PACKET_SIZE - d] = 0; /* pointer_field */
2000: d -= TS_PFIELDLEN;
1.1 oskar 2001: memcpy(&o[TS_PACKET_SIZE - d], i, d);
2002: i += d;
1.4 oskar 2003: d = l - d - TS_PFIELDLEN;
1.1 oskar 2004: o[1] = (0 << 7) | (1 << 6) | (0 << 5) | t[0];
2005: o[2] = t[1];
2006: o[0] = TS_SYNC_BYTE;
2007: c = (c + 1) & 0x0F;
2008: outin += TS_PACKET_SIZE;
2009: while (d > 0) {
2010: if (outin >= OUTBUF_SIZE) {
2011: outin = 0;
2012: }
2013: o = &outbuf[outin];
2014: o[3] = (0x00 << 6) | (0x01 << 4) | c;
2015: memcpy(&o[4], i, TS_PACKET_SIZE-TS_PACKET_HEADSIZE);
2016: i += (TS_PACKET_SIZE - TS_PACKET_HEADSIZE);
2017: d -= (TS_PACKET_SIZE - TS_PACKET_HEADSIZE);
2018: o[1] = (0 << 7) | (0 << 6) | (0 << 5) | t[0];
2019: o[2] = t[1];
2020: o[0] = TS_SYNC_BYTE;
2021: c = (c + 1) & 0x0F;
2022: outin += TS_PACKET_SIZE;
2023: }
2024: *conticnt = c;
2025: return l+2;
2026: }
2027:
2028: static void argloop(int f0)
2029: {
2030: int i0 = 0;
2031: char buf0[PATH_MAX];
2032: do {
2033: int i, n, r, n0, n1, nst, tmo;
2034: struct timeval tv;
2035: struct sitab *st;
2036: struct sitab **pst;
2037: pollfd_init();
2038: if (f0 >= 0) {
2039: n0 = pollfd_add(f0, POLLIN);
2040: } else {
2041: n0 = -1;
2042: }
2043: if (newtab != NULL) {
2044: st = newtab;
2045: nst = pollfd_add(st->u.a.fd, POLLIN);
2046: while ((st = st->next) != NULL) {
2047: pollfd_add(st->u.a.fd, POLLIN);
2048: }
2049: } else {
2050: nst = -1;
2051: }
2052: tmo = -1;
2053: if (outin == 0) {
2054: r = OUTBUF_SIZE;
2055: n1 = -1;
2056: } else {
2057: r = outout - outin;
2058: if (r < 0) {
2059: r += OUTBUF_SIZE;
2060: }
2061: n1 = pollfd_add(STDOUT_FILENO, POLLOUT);
2062: }
2063: for (i = 0; i <= TABLE_PID_LAST-TABLE_PID_FIRST; i++) {
2064: if ((tabin[i] > tabout[i])
2065: && (r >= (((((tabbuf[i][tabout[i]+3] & 0x0F) << 8)
1.4 oskar 2066: + tabbuf[i][tabout[i]+4] + TS_HEADSLEN + TS_PFIELDLEN
1.1 oskar 2067: + TS_PACKET_SIZE - TS_PACKET_HEADSIZE)
2068: * 131) / 128))) {
2069: tmo = 0;
1.9 oskar 2070: #ifdef DEBUG
2071: {
2072: int x;
2073: fprintf(stderr, "tabbuf[%d..%d-1]:\n", tabout[i], tabin[i]);
2074: for (x = tabout[i]; x < tabin[i]; x++) {
2075: fprintf(stderr, "%02x ", tabbuf[i][x]);
2076: }
2077: fprintf(stderr, "\n");
2078: }
2079: #endif
1.1 oskar 2080: break;
2081: }
2082: }
2083: gettimeofday(&tv, NULL);
2084: st = runtab;
2085: while (st != NULL) {
2086: i = st->pid - TABLE_PID_FIRST;
2087: if ((i < 0) || (i >= (TABLE_PID_LAST-TABLE_PID_FIRST))) {
2088: fprintf(stderr, "internal error (pid)\n");
2089: exit(1);
2090: }
2091: tabinold[i] = tabin[i];
2092: if (tmo != 0) {
2093: if (tabin[i] == 0) {
2094: i = (st->u.s.next.tv_sec - tv.tv_sec) * 1000
2095: + (st->u.s.next.tv_usec - tv.tv_usec) / 1000;
2096: if (i <= 0) {
2097: tmo = 0;
2098: } else if ((tmo < 0) || (i < tmo)) {
2099: tmo = i;
2100: }
2101: }
2102: }
2103: st = st->next;
2104: }
2105: n = pollfd_poll(tmo);
2106: gettimeofday(&tv, NULL);
2107: for (i = 0; i <= TABLE_PID_LAST-TABLE_PID_FIRST; i++) {
2108: while ((tabin[i] > tabout[i])
2109: && (r >= (((((tabbuf[i][tabout[i]+3] & 0x0F) << 8)
1.4 oskar 2110: + tabbuf[i][tabout[i]+4] + TS_HEADSLEN + TS_PFIELDLEN
1.1 oskar 2111: + TS_PACKET_SIZE - TS_PACKET_HEADSIZE)
2112: * 131) / 128))) {
2113: tabout[i] += tab2ts(&tabbuf[i][tabout[i]], &conticnt[i]);
2114: r = outout - outin;
2115: if (r < 0) {
2116: r += OUTBUF_SIZE;
2117: }
2118: }
2119: if (tabin[i] <= tabout[i]) {
2120: tabin[i] = tabout[i] = 0;
2121: }
2122: }
2123: if ((n > 0) && (n1 >= 0) && (r = pollfd_rev(n1))) {
2124: if (r & (POLLNVAL | POLLERR)) {
2125: fprintf(stderr, "poll error: %x\n", r);
2126: return;
2127: }
2128: if (outout >= OUTBUF_SIZE) {
2129: outout = 0;
2130: }
2131: r = ((outin > outout) ? outin : OUTBUF_SIZE) - outout;
2132: r = write(STDOUT_FILENO, &outbuf[outout], r);
2133: if (r < 0) {
2134: fprintf(stderr, "write error(%d)\n", errno);
2135: return;
2136: }
2137: if (r == 0) {
2138: exit(0);
2139: }
2140: outout += r;
2141: if (outout == outin) {
2142: outin = outout = 0;
2143: }
2144: }
2145: pst = &runtab;
2146: while ((st = *pst) != NULL) {
2147: i = st->pid - TABLE_PID_FIRST;
2148: if ((tabinold[i] == 0)
2149: && (tabin[i] < (TABBUF_SIZE-MAX_PSI_SIZE+1-2))
2150: && ((st->u.s.next.tv_sec < tv.tv_sec)
2151: || ((st->u.s.next.tv_sec == tv.tv_sec)
2152: && (st->u.s.next.tv_usec <= tv.tv_usec)))) {
2153: if (st->freqmsec > 0) {
2154: i = (st->u.s.next.tv_sec - tv.tv_sec) * 1000
2155: + (st->u.s.next.tv_usec - tv.tv_usec) / 1000;
2156: if (i < -st->freqmsec) {
2157: st->u.s.next = tv;
2158: } else {
2159: st->u.s.next.tv_sec += st->freqmsec / 1000;
2160: st->u.s.next.tv_usec += (st->freqmsec % 1000) * 1000;
2161: if (st->u.s.next.tv_usec > 1000000) {
2162: st->u.s.next.tv_usec -= 1000000;
2163: st->u.s.next.tv_sec += 1;
2164: }
2165: }
2166: }
2167: #ifdef DEBUG
2168: fprintf(stderr, "do tab: %ld.%06ld: %ld, %u\n", tv.tv_sec, tv.tv_usec,
2169: st->pid, st->tableid);
2170: #endif
2171: gentab(st, &tv);
2172: if (st->freqmsec <= 0) {
2173: *pst = st->next;
2174: nextversion[st->tableid - TABLEID_FIRST] = (st->version + 1) & 0x1F;
2175: free(st->tab);
2176: free(st);
2177: } else {
2178: pst = &st->next;
2179: }
2180: } else {
2181: pst = &st->next;
2182: }
2183: }
2184: if ((n > 0) && (nst >= 0)) {
2185: pst = &newtab;
2186: do {
2187: st = *pst;
2188: if ((r = pollfd_rev(nst))) {
2189: if (r & (POLLNVAL | POLLERR)) {
2190: fprintf(stderr, "poll error: %x\n", r);
2191: *pst = st->next;
2192: close(st->u.a.fd);
2193: free(st->tab);
2194: free(st);
2195: } else {
2196: i = st->u.a.ibuf;
2197: r = read(st->u.a.fd, &st->u.a.buf[i], sizeof(st->u.a.buf) - i - 1);
2198: if (r <= 0) {
2199: fprintf(stderr, "read error(%d): %d\n", errno, st->u.a.fd);
2200: *pst = st->next;
2201: close(st->u.a.fd);
2202: free(st->tab);
2203: free(st);
2204: } else {
2205: int j = 0;
2206: i += r;
2207: while ((r = siline(st, &st->u.a.buf[j], i)) >= 0) {
2208: j += r;
2209: i -= r;
2210: }
2211: switch (r) {
2212: case -ENOBUFS:
2213: *pst = st->next;
2214: close(st->u.a.fd);
2215: #ifdef DEBUG
2216: fprintf(stderr, "done, itab=%d\n", st->u.a.itab);
2217: for (i = 0; i < st->u.a.itab; i++) {
2218: fprintf(stderr, "%lu,", st->tab[i]);
2219: }
2220: fprintf(stderr, "\n");
2221: #endif
2222: i = st->pid - TABLE_PID_FIRST;
2223: if ((tabbuf[i] == NULL)
2224: && ((tabbuf[i] = malloc(TABBUF_SIZE)) == NULL)) {
2225: fprintf(stderr, "malloc failed for table buffer pid=%02lx\n",
2226: st->pid);
2227: free(st->tab);
2228: free(st);
2229: }
2230: st->next = runtab;
2231: runtab = st;
2232: st->u.s.next = tv;
2233: break;
2234: case -EAGAIN:
2235: if (i > 0) {
2236: memmove(&st->u.a.buf[0], &st->u.a.buf[j], i);
2237: }
2238: st->u.a.ibuf = i;
2239: pst = &st->next;
2240: break;
2241: default:
2242: fprintf(stderr, "eval error: %d\n", r);
2243: *pst = st->next;
2244: close(st->u.a.fd);
2245: free(st->tab);
2246: free(st);
2247: break;
2248: }
2249: }
2250: }
2251: n -= 1;
2252: } else {
2253: pst = &st->next;
2254: }
2255: nst += 1;
2256: } while ((*pst != NULL) && (n > 0));
2257: }
2258: if ((n > 0) && (n0 >= 0) && (r = pollfd_rev(n0))) {
2259: if (r & (POLLNVAL | POLLERR)) {
2260: fprintf(stderr, "poll error: %x\n", r);
2261: return;
2262: }
2263: r = read(f0, &buf0[i0], sizeof(buf0) - i0 - 1);
2264: if (r < 0) {
2265: fprintf(stderr, "read error(%d): %d\n", errno, f0);
2266: return;
2267: }
2268: if (r == 0) {
2269: return;
2270: }
2271: i0 += r;
2272: if (i0 == sizeof(buf0)-1) {
2273: buf0[sizeof(buf0)-1] = '\n';
2274: i0 += 1;
2275: }
2276: i = 0;
2277: while ((r = tabline(&buf0[i], i0))) {
2278: i += r;
2279: i0 -= r;
2280: }
2281: if (i0 > 0) {
2282: memmove(&buf0[0], &buf0[i], i0);
2283: }
2284: n -= 1;
2285: }
2286: } while (1);
2287: }
2288:
2289: int main(int argc, char *argv[])
2290: {
2291: int f = -1;
2292: int a;
2293: if (sizeof(unsigned) != sizeof(struct loop_descr *)) {
2294: fprintf(stderr, "data type prerequisites not met\n");
2295: return 1;
2296: }
2297: gen_crc32_table();
2298: system_init();
2299: unblockf(STDIN_FILENO);
2300: unblockf(STDOUT_FILENO);
2301: memset(&tabin[0], 0, sizeof(tabin));
2302: memset(&tabout[0], 0, sizeof(tabout));
2303: memset(&conticnt[0], 0, sizeof(conticnt));
2304: memset(&nextversion[0], 0, sizeof(nextversion));
2305: for (a = TABLE_PID_LAST-TABLE_PID_FIRST; a >= 0; a--) {
2306: tabbuf[a] = NULL;
2307: }
2308: a = 1;
2309: do {
2310: if ((a < argc) && (strcmp(argv[a], "-"))) {
2311: f = open(argv[a], O_RDONLY | O_NONBLOCK);
2312: if (f < 0) {
2313: fprintf(stderr, "open failed(%d): %s\n", errno, argv[a]);
2314: }
2315: } else {
2316: f = STDIN_FILENO;
2317: }
2318: argloop(f);
2319: } while (++a < argc);
2320: argloop(-1);
2321: return 0;
2322: }
LinuxTV legacy CVS <linuxtv.org/cvs>