movie.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "dict.h"
  13. /* Possible tags */
  14. enum {
  15. BEG, /* beginning of entry */
  16. AB, /* abstract */
  17. AN, /* database serial number */
  18. AS, /* author (one at a time) */
  19. AU, /* all authors */
  20. AW, /* award_awardee */
  21. BW, /* bw or c */
  22. CA, /* cast: character_actor */
  23. CN, /* cinematography */
  24. CO, /* country */
  25. CR, /* miscellaneous job_name */
  26. DE, /* topic keyword */
  27. DR, /* director */
  28. ED, /* editor */
  29. MP, /* MPAA rating (R, PG, etc.) */
  30. NT, /* note */
  31. PR, /* producer and for ...*/
  32. PS, /* producer (repeats info in PR) */
  33. RA, /* rating (letter) */
  34. RD, /* release date */
  35. RT, /* running time */
  36. RV, /* review citation */
  37. ST, /* production or release company (repeats info in PR) */
  38. TI, /* title[; original foreign title] */
  39. TX, /* paragraph of descriptive text */
  40. VD, /* video information (format_time_company; or "Not Avail.") */
  41. NTAG /* number of tags */
  42. };
  43. /* Assoc tables must be sorted on first field */
  44. static char *tagtab[] = {
  45. [BEG] "$$",
  46. [AB] "AB",
  47. [AN] "AN",
  48. [AS] "AS",
  49. [AU] "AU",
  50. [AW] "AW",
  51. [BW] "BW",
  52. [CA] "CA",
  53. [CN] "CN",
  54. [CO] "CO",
  55. [CR] "CR",
  56. [DE] "DE",
  57. [DR] "DR",
  58. [ED] "ED",
  59. [MP] "MP",
  60. [NT] "NT",
  61. [PR] "PR",
  62. [PS] "PS",
  63. [RA] "RA",
  64. [RD] "RD",
  65. [RT] "RT",
  66. [RV] "RV",
  67. [ST] "ST",
  68. [TI] "TI",
  69. [TX] "TX",
  70. [VD] "VD",
  71. };
  72. static char *mget(int, char *, char *, char **);
  73. static void moutall(int, char *, char *);
  74. static void moutall2(int, char *, char *);
  75. void
  76. movieprintentry(Entry ent, int cmd)
  77. {
  78. char *p, *e, *ps, *pe, *pn;
  79. int n;
  80. ps = ent.start;
  81. pe = ent.end;
  82. if(cmd == 'r') {
  83. Bwrite(bout, ps, pe-ps);
  84. return;
  85. }
  86. p = mget(TI, ps, pe, &e);
  87. if(p) {
  88. outpiece(p, e);
  89. outnl(0);
  90. }
  91. if(cmd == 'h')
  92. return;
  93. outnl(2);
  94. n = 0;
  95. p = mget(RD, ps, pe, &e);
  96. if(p) {
  97. outchars("Released: ");
  98. outpiece(p, e);
  99. n++;
  100. }
  101. p = mget(CO, ps, pe, &e);
  102. if(p) {
  103. if(n)
  104. outchars(", ");
  105. outpiece(p, e);
  106. n++;
  107. }
  108. p = mget(RT, ps, pe, &e);
  109. if(p) {
  110. if(n)
  111. outchars(", ");
  112. outchars("Running time: ");
  113. outpiece(p, e);
  114. n++;
  115. }
  116. p = mget(MP, ps, pe, &e);
  117. if(p) {
  118. if(n)
  119. outchars(", ");
  120. outpiece(p, e);
  121. n++;
  122. }
  123. p = mget(BW, ps, pe, &e);
  124. if(p) {
  125. if(n)
  126. outchars(", ");
  127. if(*p == 'c' || *p == 'C')
  128. outchars("Color");
  129. else
  130. outchars("B&W");
  131. n++;
  132. }
  133. if(n) {
  134. outchar('.');
  135. outnl(1);
  136. }
  137. p = mget(VD, ps, pe, &e);
  138. if(p) {
  139. outchars("Video: ");
  140. outpiece(p, e);
  141. outnl(1);
  142. }
  143. p = mget(AU, ps, pe, &e);
  144. if(p) {
  145. outchars("By: ");
  146. moutall2(AU, ps, pe);
  147. outnl(1);
  148. }
  149. p = mget(DR, ps, pe, &e);
  150. if(p) {
  151. outchars("Director: ");
  152. outpiece(p, e);
  153. outnl(1);
  154. }
  155. p = mget(PR, ps, pe, &e);
  156. if(p) {
  157. outchars("Producer: ");
  158. outpiece(p, e);
  159. outnl(1);
  160. }
  161. p = mget(CN, ps, pe, &e);
  162. if(p) {
  163. outchars("Cinematograpy: ");
  164. outpiece(p, e);
  165. outnl(1);
  166. }
  167. p = mget(CR, ps, pe, &e);
  168. if(p) {
  169. outchars("Other Credits: ");
  170. moutall2(CR, ps, pe);
  171. }
  172. outnl(2);
  173. p = mget(CA, ps, pe, &e);
  174. if(p) {
  175. outchars("Cast: ");
  176. moutall2(CA, ps, pe);
  177. }
  178. outnl(2);
  179. p = mget(AW, ps, pe, &e);
  180. if(p) {
  181. outchars("Awards: ");
  182. moutall2(AW, ps, pe);
  183. outnl(2);
  184. }
  185. p = mget(NT, ps, pe, &e);
  186. if(p) {
  187. outpiece(p, e);
  188. outnl(2);
  189. }
  190. p = mget(AB, ps, pe, &e);
  191. if(p) {
  192. outpiece(p, e);
  193. outnl(2);
  194. }
  195. pn = ps;
  196. n = 0;
  197. while((p = mget(TX, pn, pe, &pn)) != 0) {
  198. if(n++)
  199. outnl(1);
  200. outpiece(p, pn);
  201. }
  202. outnl(0);
  203. }
  204. int32_t
  205. movienextoff(int32_t fromoff)
  206. {
  207. int32_t a;
  208. char *p;
  209. a = Bseek(bdict, fromoff, 0);
  210. if(a < 0)
  211. return -1;
  212. for(;;) {
  213. p = Brdline(bdict, '\n');
  214. if(!p)
  215. break;
  216. if(p[0] == '$' && p[1] == '$')
  217. return (Boffset(bdict)-Blinelen(bdict));
  218. }
  219. return -1;
  220. }
  221. void
  222. movieprintkey(void)
  223. {
  224. Bprint(bout, "No key\n");
  225. }
  226. /*
  227. * write a comma-separated list of all tag values between b and e
  228. */
  229. static void
  230. moutall(int tag, char *b, char *e)
  231. {
  232. char *p, *pn;
  233. int n;
  234. n = 0;
  235. pn = b;
  236. while((p = mget(tag, pn, e, &pn)) != 0) {
  237. if(n++)
  238. outchars(", ");
  239. outpiece(p, pn);
  240. }
  241. }
  242. /*
  243. * like moutall, but values are expected to have form:
  244. * field1_field2
  245. * and we are to output 'field2 (field1)' for each
  246. * (sometimes field1 has underscores, so search from end)
  247. */
  248. static void
  249. moutall2(int tag, char *b, char *e)
  250. {
  251. char *p, *pn, *us, *q;
  252. int n;
  253. n = 0;
  254. pn = b;
  255. while((p = mget(tag, pn, e, &pn)) != 0) {
  256. if(n++)
  257. outchars(", ");
  258. us = 0;
  259. for(q = pn-1; q >= p; q--)
  260. if(*q == '_') {
  261. us = q;
  262. break;
  263. }
  264. if(us) {
  265. /*
  266. * Hack to fix cast list Himself/Herself
  267. */
  268. if(strncmp(us+1, "Himself", 7) == 0 ||
  269. strncmp(us+1, "Herself", 7) == 0) {
  270. outpiece(p, us);
  271. outchars(" (");
  272. outpiece(us+1, pn);
  273. outchar(')');
  274. } else {
  275. outpiece(us+1, pn);
  276. outchars(" (");
  277. outpiece(p, us);
  278. outchar(')');
  279. }
  280. } else {
  281. outpiece(p, pn);
  282. }
  283. }
  284. }
  285. /*
  286. * Starting from b, find next line beginning with tagtab[tag].
  287. * Don't go past e, but assume *e==0.
  288. * Return pointer to beginning of value (after tag), and set
  289. * eptr to point at newline that ends the value
  290. */
  291. static char *
  292. mget(int tag, char *b, char *e, char **eptr)
  293. {
  294. char *p, *t, *ans;
  295. if(tag < 0 || tag >= NTAG)
  296. return 0;
  297. t = tagtab[tag];
  298. ans = 0;
  299. for(p = b;;) {
  300. p = strchr(p, '\n');
  301. if(!p || ++p >= e) {
  302. if(ans)
  303. *eptr = e-1;
  304. break;
  305. }
  306. if(!ans) {
  307. if(p[0] == t[0] && p[1] == t[1])
  308. ans = p+3;
  309. } else {
  310. if(p[0] != ' ') {
  311. *eptr = p-1;
  312. break;
  313. }
  314. }
  315. }
  316. return ans;
  317. }