main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5. #include "vt.h"
  6. #include "misc.h"
  7. #include "fdset.h"
  8. #include "xio.h"
  9. #include "vbi.h"
  10. #include "lang.h"
  11. #include "cache.h"
  12. #include "ui.h"
  13. static char *geometry;
  14. static char *dpy_name;
  15. static char *vbi_name = NULL;
  16. static struct xio *xio;
  17. static struct vbi *vbi;
  18. static int erc = 1;
  19. char *outfile = "";
  20. static char *channel;
  21. static int ttpid = -1;
  22. u_int16_t sid = 0;
  23. static void usage(FILE *fp, int exitval)
  24. {
  25. fprintf(fp, "\nUsage: %s [options]\n", prgname);
  26. fprintf(fp,
  27. "\n"
  28. " Valid options:\t\tDefault:\n"
  29. " -c <channel name>\t\t(none;dvb only)\n"
  30. " -ch -child <ppp.ss>\t\t(none)\n"
  31. " -cs -charset\t\tlatin-1\n"
  32. " <latin-1/2/koi8-r/iso8859-7>\n"
  33. " -h -help\n"
  34. " -o <outfile>\t\t(none;dvb only)\n"
  35. " -p -parent <ppp.ss>\t\t900\n"
  36. " -s -sid <sid>\t\t(none;dvb only)\n"
  37. " -t -ttpid <ttpid>\t\t(none;dvb only)\n"
  38. " -v -vbi <vbidev>\t\t/dev/vbi\n"
  39. " \t\t/dev/vbi0\n"
  40. " \t\t/dev/video0\n"
  41. " \t\t/dev/dvb/adapter0/demux0\n"
  42. "\n"
  43. " ppp.ss stands for a page number and an\n"
  44. " optional subpage number (Example: 123.4).\n"
  45. "\n"
  46. " The -child option requires a parent\n"
  47. " window. So it must be preceded by\n"
  48. " a parent or another child window.\n"
  49. );
  50. exit(exitval);
  51. }
  52. static int arg_pgno(char *p, int *subno)
  53. {
  54. char *end;
  55. int pgno;
  56. *subno = ANY_SUB;
  57. if (*p)
  58. {
  59. pgno = strtol(p, &end, 16);
  60. if ((*end == ':' || *end == '/' || *end == '.') && end[1])
  61. *subno = strtol(end + 1, &end, 16);
  62. if (*end == 0)
  63. if (pgno >= 0x100 && pgno <= 0x999)
  64. if (*subno == ANY_SUB || (*subno >= 0x00 && *subno <= 0x3f7f))
  65. return pgno;
  66. }
  67. fatal("%s: invalid page number", p);
  68. }
  69. static struct vtwin * start(int argc, char **argv, struct vtwin *parent,
  70. int pgno, int subno)
  71. {
  72. static int valid_vbi_name = 1;
  73. if (!valid_vbi_name)
  74. return parent;
  75. if (vbi == 0)
  76. vbi = vbi_open(vbi_name, cache_open(), channel, outfile, sid, ttpid);
  77. if (vbi == 0)
  78. {
  79. if (vbi_name)
  80. error("cannot open device: %s", vbi_name);
  81. valid_vbi_name = 0;
  82. vbi = open_null_vbi(cache_open());
  83. }
  84. if (vbi->cache)
  85. vbi->cache->op->mode(vbi->cache, CACHE_MODE_ERC, erc);
  86. if (xio == 0)
  87. xio = xio_open_dpy(dpy_name, argc, argv);
  88. if (xio == 0)
  89. fatal("cannot open display");
  90. parent = vtwin_new(xio, vbi, geometry, parent, pgno, subno);
  91. if (parent == 0)
  92. fatal("cannot create window");
  93. if (!valid_vbi_name)
  94. {
  95. if (vbi_name)
  96. send_errmsg(vbi, "cannot open device: %s", vbi_name);
  97. else
  98. send_errmsg(vbi, "cannot open any device", vbi_name);
  99. }
  100. return parent;
  101. }
  102. static int option(int argc, char **argv, int *ind, char **arg)
  103. {
  104. static struct { char *nam, *altnam; int arg; } opts[] = {
  105. { "-channel", "-c", 1 },
  106. { "-child", "-ch", 1 },
  107. { "-charset", "-cs", 1 },
  108. { "-help", "-h", 0 },
  109. { "-outfile", "-o", 1 },
  110. { "-parent", "-p", 1 },
  111. { "-sid", "-s", 1 },
  112. { "-ttpid", "-t", 1 },
  113. { "-vbi", "-v", 1 },
  114. };
  115. int i;
  116. if (*ind >= argc)
  117. return 0;
  118. *arg = argv[(*ind)++];
  119. for (i = 0; i < NELEM(opts); ++i)
  120. if (streq(*arg, opts[i].nam) || streq(*arg, opts[i].altnam))
  121. {
  122. if (opts[i].arg)
  123. if (*ind < argc)
  124. *arg = argv[(*ind)++];
  125. else
  126. fatal("option %s requires an argument", *arg);
  127. return i+1;
  128. }
  129. if (**arg == '-')
  130. {
  131. fatal("%s: invalid option", *arg);
  132. usage(stderr, 1);
  133. }
  134. return -1;
  135. }
  136. int main(int argc, char **argv)
  137. {
  138. struct vtwin *parent = 0;
  139. int pgno, subno;
  140. int opt, ind;
  141. char *arg;
  142. sid = 0;
  143. setprgname(argv[0]);
  144. fdset_init(fds);
  145. ind = 1;
  146. while (opt = option(argc, argv, &ind, &arg))
  147. switch (opt)
  148. {
  149. case 1: // channel
  150. channel = arg;
  151. break;
  152. case 2: // child
  153. if (parent == 0)
  154. fatal("-child requires a parent window");
  155. pgno = arg_pgno(arg, &subno);
  156. parent = start(argc, argv, parent, pgno, subno);
  157. geometry = 0;
  158. break;
  159. case 3: // charset
  160. if (streq(arg, "latin-1") || streq(arg, "1"))
  161. latin1 = LATIN1;
  162. else if (streq(arg, "latin-2") || streq(arg, "2"))
  163. latin1 = LATIN2;
  164. else if (streq(arg, "koi8-r") || streq(arg, "koi"))
  165. latin1 = KOI8;
  166. else if (streq(arg, "iso8859-7") || streq(arg, "el"))
  167. latin1 = GREEK;
  168. else
  169. fatal("bad charset (not latin-1/2/koi8-r/iso8859-7)");
  170. break;
  171. case 4: // help
  172. usage(stdout, 0);
  173. break;
  174. case 5: // outfile
  175. outfile = arg;
  176. break;
  177. case 6: // parent
  178. case -1: // non-option arg
  179. pgno = arg_pgno(arg, &subno);
  180. parent = start(argc, argv, 0, pgno, subno);
  181. geometry = 0;
  182. break;
  183. case 7: // sid
  184. sid = strtoul(arg, NULL, 0);
  185. break;
  186. case 8: // ttpid
  187. ttpid = strtoul(arg, NULL, 0);
  188. break;
  189. case 9: // vbi
  190. vbi_name = arg;
  191. vbi = 0;
  192. parent = 0;
  193. break;
  194. }
  195. if (parent == 0)
  196. start(argc, argv, 0, 0x900, ANY_SUB);
  197. xio_event_loop();
  198. exit(0);
  199. }