tinform.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include<math.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<ctype.h>
  6. #include<stdarg.h>
  7. #define C char
  8. #define I int
  9. #define a for
  10. #define Z sizeof
  11. #define g goto
  12. #define J free
  13. #define Q g U
  14. #define W static
  15. #define m malloc
  16. #define mm realloc
  17. #define AA isalnum
  18. #define d strlen
  19. #define AS sscanf
  20. #define FC fclose
  21. #define k printf
  22. #define H index
  23. #define SS strstr
  24. #define RR return
  25. #define FG fgetc
  26. #define vd void
  27. #define u if
  28. #define V FILE
  29. #define z else
  30. #define BR break
  31. #define TI exit(-1);
  32. #define X() Y(&T, &j, &N)
  33. #define n(a,b) fprintf(stderr,a,b)
  34. W C KK[] = "br", e[] = "br", DO[] = "%d";
  35. W C R[] = "=**7*x7(=616?x}+R";
  36. I GG=2<<8, M =2<<14, q=0;
  37. vd E(C ***T, I *N, I *j, C c) {
  38. (*T)[*N][(*j)++] = c;
  39. u (*j % GG)
  40. (*T)[*N] = mm((*T)[*N],2*(d((*T)[*N])+1));
  41. }
  42. V* op(C*n) {
  43. V* P = fopen(n, KK);
  44. u (!P) n(R, n), TI;
  45. RR P;
  46. }
  47. W C PI[] = "0123456789";
  48. W I o=4711, S = 0, b = 0, i = 0, j = 0, l = 0,
  49. ch, G = 0, K, A=0,
  50. r;
  51. W C sr[] = "\03\05#%""pqctgb",
  52. ve[] = "\06w""sr}uedf""y~$",
  53. Hg[] = "<""9,9x,*-6"";9,""=<""x9,x}<R",
  54. Pe[] = "\06""e""x""~""~""e""x""$""$""e""x""w""e""x""s"
  55. "sexrex""}exuueex""ddex""ffex""ye",
  56. sp[] = "UxSQRT",
  57. TB[] = ",77x:1?bx}<",
  58. x[] = "RX",
  59. O[9]= {0};
  60. C* p;C c='\n', s='\n';
  61. vd ms(C*m,I c,I n) {
  62. a (I i=0; i<n; ) m[i++]=c;
  63. }
  64. I Y(C ***T, I *j, I *N) {
  65. u (*j) {
  66. (*T)[*N][(*j)++]=0;
  67. *j=0;
  68. ++*N;
  69. }
  70. u (*N>=M) {
  71. M += o;
  72. *T = mm(*T, M * Z(C*));
  73. a ( I p = M - o ; p < M ; ) {
  74. (*T)[p] = m(GG * Z(C));
  75. ms((*T)[p++], 0, GG);
  76. }
  77. }
  78. RR 1;
  79. }
  80. I main(I N, C ** T) {
  81. V* P=stdin;
  82. C F = 'r'^'b',
  83. f = r = 0, v = 0, t = 0, *B = 0, *go[]= {Hg,R,sp,Pe,ve,sr,TB,x,0} ,* w=(N>2) ? T[2] : 0;
  84. KK[0]^=F;
  85. KK[1]^=F;
  86. F^=F;
  87. a (I i=0; go[i]; i++)
  88. a (I j=0; j<d(i[go]); )
  89. go[i][j++] ^= x[1];
  90. u (N<2) TI;
  91. u (!SS("-", T[1]))
  92. P = op(T[1]);
  93. N = 0;
  94. T = m(M * Z(C*));
  95. a (; i < M; ) {
  96. T[i] = m(GG * Z(C));
  97. ms(T[i++], 0, GG);
  98. }
  99. a (i=0; (ch=FG(P)) ^ EOF;) {
  100. u (!(c=ch)) BR;
  101. b = 1;
  102. u (F & b) {
  103. F ^= b;
  104. u (!(F & 12) && c ^ s)
  105. E(&T, &N, &j, '\\'),
  106. E(&T, &N, &j, c);
  107. Q;
  108. }
  109. b <<= 1;
  110. u (c == '\\') {
  111. F |= b/2;
  112. Q;
  113. }
  114. b <<= 1;
  115. u (F & b) {
  116. u (c == s) {
  117. F ^= b;
  118. X();
  119. }
  120. Q;
  121. }
  122. b <<= 1;
  123. u (F & b) {
  124. u (c == '/' && S) {
  125. F ^= b;
  126. S = x[1];
  127. u (!(F & 2)) X();
  128. Q;
  129. }
  130. S = c == '*' ? 1 : S;
  131. Q;
  132. }
  133. b <<= 1;
  134. u (F & 48) {
  135. C y = F&b?'"':'\'';
  136. E(&T, &N, &j, c);
  137. u (c^y) Q;
  138. F ^= F&b?b:2*b;
  139. X();
  140. Q;
  141. }
  142. u (H(" ""\n", c) && F ^ 2) {
  143. X();
  144. Q;
  145. }
  146. u (c == s && F & 2) {
  147. F ^= 2;
  148. X();
  149. Q;
  150. }
  151. u (c == '#') {
  152. F |= 2;
  153. E(&T, &N, &j, c);
  154. Q;
  155. }
  156. u (c == '/' && v == c) {
  157. f = 0;
  158. F |= 4;
  159. Q;
  160. }
  161. u (c == '*' && v == '/') {
  162. f = 0;
  163. F |= 8;
  164. Q;
  165. }
  166. u (!H("/""\n",c) && F & 2) {
  167. E(&T, &N, &j, c);
  168. Q;
  169. }
  170. u (H(sr, c)) {
  171. u (f) {
  172. X();
  173. E(&T, &N, &j, f);
  174. f=0;
  175. }
  176. X();
  177. E(&T, &N, &j, c);
  178. X();
  179. Q;
  180. }
  181. u (f) {
  182. u (H("<>",f)) {
  183. u (t) {
  184. X();
  185. E(&T, &N, &j, t);
  186. E(&T, &N, &j, f);
  187. f = (t = 0);
  188. u (c^'=') {
  189. X();
  190. f = 0;
  191. u (H(ve, c))
  192. f = c;
  193. z
  194. E(&T, &N, &j, c);
  195. Q;
  196. }
  197. E(&T, &N, &j, c);
  198. X();
  199. Q;
  200. }
  201. u (f == c) {
  202. t = c;
  203. Q;
  204. }
  205. }
  206. e[0] = f;
  207. e[1] = c;
  208. p = SS(Pe, e);
  209. X();
  210. E(&T, &N, &j, f);
  211. f = 0;
  212. u (p) {
  213. E(&T, &N, &j, c);
  214. X();
  215. }
  216. z {
  217. X();
  218. u (c == '"')
  219. g dq;
  220. u (c == '\'')
  221. g sq;
  222. u (H(ve, c))
  223. f = c;
  224. z
  225. E(&T, &N, &j, c);
  226. }
  227. Q;
  228. }
  229. u (H(ve, c) && !f) {
  230. f = c;
  231. Q;
  232. }
  233. u (c == '"') {
  234. dq:
  235. F |= b;
  236. X();
  237. E(&T, &N, &j, c);
  238. Q;
  239. }
  240. u (c == '\'') {
  241. sq:
  242. F |= 2*b;
  243. X();
  244. E(&T, &N, &j, c);
  245. Q;
  246. }
  247. E(&T, &N, &j, c);
  248. U:
  249. b <<= 1;
  250. v = c;
  251. i++;
  252. }
  253. a (i=0; i<=N; )
  254. G += d(T[i++]);
  255. u (w) {
  256. V*Vv = op(w);
  257. I ch;
  258. F = 0;
  259. I w=0, h=0, G=0;
  260. C P[9]= {0};
  261. a(; (ch = FG(Vv)) ^ EOF;) {
  262. c = (C)(ch & 255);
  263. u (A==7) {
  264. u(B)
  265. J(B),
  266. B=0;
  267. BR;
  268. }
  269. r = A>=5 && F&4;
  270. u(!r) {
  271. u ((H(sp, c) && c ^ s && A ^ 3)
  272. || (c == s && A == 6)
  273. || F&1
  274. ) {
  275. F|=2;
  276. u ( F&1 && c == s) F ^=1;
  277. }
  278. F |= 3 * (H("#",c) ? 1 : 0);
  279. u (F&2) {
  280. F ^= 2;
  281. g L;
  282. }
  283. }
  284. u (!A++) {
  285. u (c^'P') A=7;
  286. g L;
  287. }
  288. A--;
  289. u (A++<2) {
  290. u (c=='4') F |= 4;
  291. z u (c^'1') A=7;
  292. g L;
  293. }
  294. A--;
  295. u (A++<3) {
  296. u (c^s) A=7;
  297. g L;
  298. }
  299. A--;
  300. u (A<4) {
  301. u (H(PI, c)) O[w++] = c;
  302. z u (H(sp, c)) A++;
  303. z A=7;
  304. g L;
  305. }
  306. u (A<5) {
  307. u (H(PI, c)) P[h++] = c;
  308. z u (c == s) A++;
  309. z A=7;
  310. g L;
  311. }
  312. u(A<6) {
  313. A++;
  314. AS(P, DO, &h);
  315. AS(O, DO, &w);
  316. S = (K=w)*h;
  317. u (S>1<<20) {
  318. n(TB,S);
  319. TI;
  320. }
  321. B = m(Z(C) * S);
  322. }
  323. u (F & 4) {
  324. a (j=7; j>=0; --j) {
  325. u (q > S) g D;
  326. B[q++] = (c & 1 << j) >> j;
  327. u (++G >= K) {
  328. G=0;
  329. g L;
  330. }
  331. }
  332. g L;
  333. }
  334. u (q > S) g D;
  335. B[q++] = c=='1';
  336. g L;
  337. D:
  338. n(Hg, q);
  339. A = 7;
  340. u(B) {
  341. J(B);
  342. B=0;
  343. }
  344. L: {
  345. }
  346. }
  347. u (q < S) {
  348. n(Hg, q);
  349. }
  350. FC(Vv);
  351. }
  352. u (!w || !B || A==7) {
  353. S = K = ceil(sqrt(G));
  354. B = m(S *= S * Z(C));
  355. ms(B, 1, S);
  356. }
  357. a (i=0; i<=N; ++i) {
  358. u (T[i][0] == '#') {
  359. u (r) k("\n");
  360. b = r;
  361. r = 0;
  362. k("%s""\n", T[i]);
  363. } z {
  364. a (; b; b--)
  365. r += k(" ");
  366. a (; l<=S && !B[l]; l++) {
  367. k(" ");
  368. u (++r >= K)
  369. r = k("\n") - 1;
  370. }
  371. q = i
  372. && AA(T[i-1][d(T[i-1])-1])
  373. && AA(T[i][0]) ? k(" ") : x[1];
  374. r += q += k("%s", T[i]);
  375. l += q;
  376. u (r >= K) {
  377. k("\n");
  378. q = r - K;
  379. u (q>(r=0))
  380. l -= q;
  381. }
  382. }
  383. }
  384. J(B);
  385. B=0;
  386. k("\n");
  387. a (i=0; i < M;)
  388. J(T[i++]);
  389. J(T);
  390. FC(P);
  391. RR 0;
  392. }