common.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include "common.h"
  6. #include "comments.h"
  7. #include "path.h"
  8. struct strtab charcode[FONTSIZE] = {
  9. {4, "\\000"}, {4, "\\001"}, {4, "\\002"}, {4, "\\003"},
  10. {4, "\\004"}, {4, "\\005"}, {4, "\\006"}, {4, "\\007"},
  11. {4, "\\010"}, {4, "\\011"}, {4, "\\012"}, {4, "\\013"},
  12. {4, "\\014"}, {4, "\\015"}, {4, "\\016"}, {4, "\\017"},
  13. {4, "\\020"}, {4, "\\021"}, {4, "\\022"}, {4, "\\023"},
  14. {4, "\\024"}, {4, "\\025"}, {4, "\\026"}, {4, "\\027"},
  15. {4, "\\030"}, {4, "\\031"}, {4, "\\032"}, {4, "\\033"},
  16. {4, "\\034"}, {4, "\\035"}, {4, "\\036"}, {4, "\\037"},
  17. {1, " "}, {1, "!"}, {1, "\""}, {1, "#"},
  18. {1, "$"}, {1, "%"}, {1, "&"}, {1, "'"},
  19. {2, "\\("}, {2, "\\)"}, {1, "*"}, {1, "+"},
  20. {1, ","}, {1, "-"}, {1, "."}, {1, "/"},
  21. {1, "0"}, {1, "1"}, {1, "2"}, {1, "3"},
  22. {1, "4"}, {1, "5"}, {1, "6"}, {1, "7"},
  23. {1, "8"}, {1, "9"}, {1, ":"}, {1, ";"},
  24. {1, "<"}, {1, "="}, {1, ">"}, {1, "?"},
  25. {1, "@"}, {1, "A"}, {1, "B"}, {1, "C"},
  26. {1, "D"}, {1, "E"}, {1, "F"}, {1, "G"},
  27. {1, "H"}, {1, "I"}, {1, "J"}, {1, "K"},
  28. {1, "L"}, {1, "M"}, {1, "N"}, {1, "O"},
  29. {1, "P"}, {1, "Q"}, {1, "R"}, {1, "S"},
  30. {1, "T"}, {1, "U"}, {1, "V"}, {1, "W"},
  31. {1, "X"}, {1, "Y"}, {1, "Z"}, {1, "["},
  32. {2, "\\\\"}, {1, "]"}, {1, "^"}, {1, "_"},
  33. {1, "`"}, {1, "a"}, {1, "b"}, {1, "c"},
  34. {1, "d"}, {1, "e"}, {1, "f"}, {1, "g"},
  35. {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"},
  36. {1, "l"}, {1, "m"}, {1, "n"}, {1, "o"},
  37. {1, "p"}, {1, "q"}, {1, "r"}, {1, "s"},
  38. {1, "t"}, {1, "u"}, {1, "v"}, {1, "w"},
  39. {1, "x"}, {1, "y"}, {1, "z"}, {1, "{"},
  40. {1, "|"}, {1, "}"}, {1, "~"}, {4, "\\177"},
  41. {4, "\\200"}, {4, "\\201"}, {4, "\\202"}, {4, "\\203"},
  42. {4, "\\204"}, {4, "\\205"}, {4, "\\206"}, {4, "\\207"},
  43. {4, "\\210"}, {4, "\\211"}, {4, "\\212"}, {4, "\\213"},
  44. {4, "\\214"}, {4, "\\215"}, {4, "\\216"}, {4, "\\217"},
  45. {4, "\\220"}, {4, "\\221"}, {4, "\\222"}, {4, "\\223"},
  46. {4, "\\224"}, {4, "\\225"}, {4, "\\226"}, {4, "\\227"},
  47. {4, "\\230"}, {4, "\\231"}, {4, "\\232"}, {4, "\\233"},
  48. {4, "\\234"}, {4, "\\235"}, {4, "\\236"}, {4, "\\237"},
  49. {4, "\\240"}, {4, "\\241"}, {4, "\\242"}, {4, "\\243"},
  50. {4, "\\244"}, {4, "\\245"}, {4, "\\246"}, {4, "\\247"},
  51. {4, "\\250"}, {4, "\\251"}, {4, "\\252"}, {4, "\\253"},
  52. {4, "\\254"}, {4, "\\255"}, {4, "\\256"}, {4, "\\257"},
  53. {4, "\\260"}, {4, "\\261"}, {4, "\\262"}, {4, "\\263"},
  54. {4, "\\264"}, {4, "\\265"}, {4, "\\266"}, {4, "\\267"},
  55. {4, "\\270"}, {4, "\\271"}, {4, "\\272"}, {4, "\\273"},
  56. {4, "\\274"}, {4, "\\275"}, {4, "\\276"}, {4, "\\277"},
  57. {4, "\\300"}, {4, "\\301"}, {4, "\\302"}, {4, "\\303"},
  58. {4, "\\304"}, {4, "\\305"}, {4, "\\306"}, {4, "\\307"},
  59. {4, "\\310"}, {4, "\\311"}, {4, "\\312"}, {4, "\\313"},
  60. {4, "\\314"}, {4, "\\315"}, {4, "\\316"}, {4, "\\317"},
  61. {4, "\\320"}, {4, "\\321"}, {4, "\\322"}, {4, "\\323"},
  62. {4, "\\324"}, {4, "\\325"}, {4, "\\326"}, {4, "\\327"},
  63. {4, "\\330"}, {4, "\\331"}, {4, "\\332"}, {4, "\\333"},
  64. {4, "\\334"}, {4, "\\335"}, {4, "\\336"}, {4, "\\337"},
  65. {4, "\\340"}, {4, "\\341"}, {4, "\\342"}, {4, "\\343"},
  66. {4, "\\344"}, {4, "\\345"}, {4, "\\346"}, {4, "\\347"},
  67. {4, "\\350"}, {4, "\\351"}, {4, "\\352"}, {4, "\\353"},
  68. {4, "\\354"}, {4, "\\355"}, {4, "\\356"}, {4, "\\357"},
  69. {4, "\\360"}, {4, "\\361"}, {4, "\\362"}, {4, "\\363"},
  70. {4, "\\364"}, {4, "\\365"}, {4, "\\366"}, {4, "\\367"},
  71. {4, "\\370"}, {4, "\\371"}, {4, "\\372"}, {4, "\\373"},
  72. {4, "\\374"}, {4, "\\375"}, {4, "\\376"}, {4, "\\377"}
  73. };
  74. static BOOLEAN in_string = FALSE;
  75. int char_no = 0;
  76. int line_no = 0;
  77. int page_no = 0; /* page number in a document */
  78. int pages_printed = 0;
  79. static int pplistmaxsize=0;
  80. static unsigned char *pplist=0; /* bitmap list for storing pages to print */
  81. void
  82. pagelist(char *list) {
  83. char c;
  84. int n, m;
  85. int state, start;
  86. if (list == 0) return;
  87. state = 1;
  88. start = 0;
  89. while ((c=*list) != '\0') {
  90. n = 0;
  91. while (isdigit(c)) {
  92. n = n * 10 + c - '0';
  93. c = *++list;
  94. }
  95. switch (state) {
  96. case 1:
  97. start = n;
  98. case 2:
  99. if (n/8+1 > pplistmaxsize) {
  100. pplistmaxsize = n/8+1;
  101. pplist = galloc(pplist, n/8+1, "page list");
  102. }
  103. for (m=start; m<=n; m++)
  104. pplist[m/8] |= 1<<(m%8);
  105. break;
  106. }
  107. switch (c) {
  108. case '-':
  109. state = 2;
  110. list++;
  111. break;
  112. case ',':
  113. state = 1;
  114. list++;
  115. break;
  116. case '\0':
  117. break;
  118. }
  119. }
  120. }
  121. BOOLEAN
  122. pageon(void) {
  123. extern BOOLEAN debug;
  124. static BOOLEAN privdebug = FALSE;
  125. if (pplist == 0 && page_no != 0) {
  126. if (privdebug && !debug) {
  127. privdebug = FALSE;
  128. debug = TRUE;
  129. }
  130. return(TRUE); /* no page list, print all pages */
  131. }
  132. if (page_no/8 < pplistmaxsize && (pplist[page_no/8] & 1<<(page_no%8))) {
  133. if (privdebug && !debug) {
  134. privdebug = FALSE;
  135. debug = TRUE;
  136. }
  137. return(TRUE);
  138. } else {
  139. if (!privdebug && debug) {
  140. privdebug = TRUE;
  141. debug = FALSE;
  142. }
  143. return(FALSE);
  144. }
  145. }
  146. static int stringhpos, stringvpos;
  147. void
  148. startstring(void) {
  149. if (!in_string) {
  150. stringhpos = hpos;
  151. stringvpos = vpos;
  152. if (pageon()) Bprint(Bstdout, "(");
  153. in_string = 1;
  154. }
  155. }
  156. void
  157. endstring(void) {
  158. if (in_string) {
  159. if (pageon()) Bprint(Bstdout, ") %d %d w\n", stringhpos, stringvpos);
  160. in_string = 0;
  161. }
  162. }
  163. BOOLEAN
  164. isinstring(void) {
  165. return(in_string);
  166. }
  167. void
  168. startpage(void) {
  169. ++char_no;
  170. ++line_no;
  171. ++page_no;
  172. if (pageon()) {
  173. ++pages_printed;
  174. Bprint(Bstdout, "%s %d %d\n", PAGE, page_no, pages_printed);
  175. Bprint(Bstdout, "/saveobj save def\n");
  176. Bprint(Bstdout, "mark\n");
  177. Bprint(Bstdout, "%d pagesetup\n", pages_printed);
  178. }
  179. }
  180. void
  181. endpage(void) {
  182. endstring();
  183. curpostfontid = -1;
  184. line_no = 0;
  185. char_no = 0;
  186. if (pageon()) {
  187. Bprint(Bstdout, "cleartomark\n");
  188. Bprint(Bstdout, "showpage\n");
  189. Bprint(Bstdout, "saveobj restore\n");
  190. Bprint(Bstdout, "%s %d %d\n", ENDPAGE, page_no, pages_printed);
  191. }
  192. }
  193. /* This was taken from postprint */
  194. int
  195. cat(char *filename) {
  196. Biobuf *bfile;
  197. Biobufhdr *Bfile;
  198. int n;
  199. static char buf[Bsize];
  200. if ((bfile = Bopen(filename, OREAD)) == 0) {
  201. return(1);
  202. }
  203. Bfile = &(bfile->Biobufhdr);
  204. while ((n=Bread(Bfile, buf, Bsize)) > 0) {
  205. if (Bwrite(Bstdout, buf, n) != n)
  206. break;
  207. }
  208. Bterm(Bfile);
  209. if (n != 0) {
  210. return(1);
  211. }
  212. return(0);
  213. }
  214. extern int debug;
  215. void *
  216. galloc(void *ptr, int size, char *perstr) {
  217. void *x;
  218. if ((x=realloc(ptr, size)) == 0) {
  219. perror(perstr);
  220. exits("malloc");
  221. }
  222. return(x);
  223. }
  224. static char *errorstrings[] = {
  225. {""}, /* NONE */
  226. {"WARNING"},
  227. {"FATAL"}
  228. };
  229. char *programname;
  230. char *inputfilename = "<stdin>";
  231. int inputlineno;
  232. void
  233. error(int errtype, char *fmt, ...) {
  234. va_list arg;
  235. Bflush(Bstdout);
  236. Bflush(Bstderr);
  237. fprint(2, "%s: %s:%d :%s: ", programname, inputfilename, inputlineno, errorstrings[errtype]);
  238. va_start(arg, fmt);
  239. vfprint(2, fmt, arg);
  240. va_end(arg);
  241. if (errtype == FATAL)
  242. exits("fatal error");
  243. }