t5.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /* t5.c: read data for table */
  10. # include "t.h"
  11. void
  12. gettbl(void)
  13. {
  14. int icol, ch;
  15. cstore = cspace = chspace();
  16. textflg = 0;
  17. for (nlin = nslin = 0; gets1(cstore, MAXCHS - (cstore - cspace)); nlin++) {
  18. stynum[nlin] = nslin;
  19. if (prefix(".TE", cstore)) {
  20. leftover = 0;
  21. break;
  22. }
  23. if (prefix(".TC", cstore) || prefix(".T&", cstore)) {
  24. readspec();
  25. nslin++;
  26. }
  27. if (nlin >= MAXLIN) {
  28. leftover = cstore;
  29. break;
  30. }
  31. fullbot[nlin] = 0;
  32. if (cstore[0] == '.' && !isdigit(cstore[1])) {
  33. instead[nlin] = cstore;
  34. while (*cstore++)
  35. ;
  36. continue;
  37. } else
  38. instead[nlin] = 0;
  39. if (nodata(nlin)) {
  40. if ((ch = oneh(nlin)))
  41. fullbot[nlin] = ch;
  42. table[nlin] = (struct colstr *) alocv((ncol + 2) * sizeof(table[0][0]));
  43. for (icol = 0; icol < ncol; icol++) {
  44. table[nlin][icol].rcol = "";
  45. table[nlin][icol].col = "";
  46. }
  47. nlin++;
  48. nslin++;
  49. fullbot[nlin] = 0;
  50. instead[nlin] = (char *) 0;
  51. }
  52. table[nlin] = (struct colstr *) alocv((ncol + 2) * sizeof(table[0][0]));
  53. if (cstore[1] == 0)
  54. switch (cstore[0]) {
  55. case '_':
  56. fullbot[nlin] = '-';
  57. continue;
  58. case '=':
  59. fullbot[nlin] = '=';
  60. continue;
  61. }
  62. stynum[nlin] = nslin;
  63. nslin = min(nslin + 1, nclin - 1);
  64. for (icol = 0; icol < ncol; icol++) {
  65. table[nlin][icol].col = cstore;
  66. table[nlin][icol].rcol = 0;
  67. ch = 1;
  68. if (match(cstore, "T{")) { /* text follows */
  69. table[nlin][icol].col =
  70. (char *)(isize)gettext(cstore, nlin, icol,
  71. font[icol][stynum[nlin]],
  72. csize[icol][stynum[nlin]]);
  73. } else
  74. {
  75. for (; (ch = *cstore) != '\0' && ch != tab; cstore++)
  76. ;
  77. *cstore++ = '\0';
  78. switch (ctype(nlin, icol)) /* numerical or alpha, subcol */ {
  79. case 'n':
  80. table[nlin][icol].rcol = maknew(table[nlin][icol].col);
  81. break;
  82. case 'a':
  83. table[nlin][icol].rcol = table[nlin][icol].col;
  84. table[nlin][icol].col = "";
  85. break;
  86. }
  87. }
  88. while (ctype(nlin, icol + 1) == 's') /* spanning */
  89. table[nlin][++icol].col = "";
  90. if (ch == '\0')
  91. break;
  92. }
  93. while (++icol < ncol + 2) {
  94. table[nlin][icol].col = "";
  95. table [nlin][icol].rcol = 0;
  96. }
  97. while (*cstore != '\0')
  98. cstore++;
  99. if (cstore - cspace + MAXLINLEN > MAXCHS)
  100. cstore = cspace = chspace();
  101. }
  102. last = cstore;
  103. permute();
  104. if (textflg)
  105. untext();
  106. }
  107. int
  108. nodata(int il)
  109. {
  110. int c;
  111. for (c = 0; c < ncol; c++) {
  112. switch (ctype(il, c)) {
  113. case 'c':
  114. case 'n':
  115. case 'r':
  116. case 'l':
  117. case 's':
  118. case 'a':
  119. return(0);
  120. }
  121. }
  122. return(1);
  123. }
  124. int
  125. oneh(int lin)
  126. {
  127. int k, icol;
  128. k = ctype(lin, 0);
  129. for (icol = 1; icol < ncol; icol++) {
  130. if (k != ctype(lin, icol))
  131. return(0);
  132. }
  133. return(k);
  134. }
  135. # define SPAN "\\^"
  136. void
  137. permute(void)
  138. {
  139. int irow, jcol, is;
  140. char *start, *strig;
  141. for (jcol = 0; jcol < ncol; jcol++) {
  142. for (irow = 1; irow < nlin; irow++) {
  143. if (vspand(irow, jcol, 0)) {
  144. is = prev(irow);
  145. if (is < 0)
  146. error("Vertical spanning in first row not allowed");
  147. start = table[is][jcol].col;
  148. strig = table[is][jcol].rcol;
  149. while (irow < nlin && vspand(irow, jcol, 0))
  150. irow++;
  151. table[--irow][jcol].col = start;
  152. table[irow][jcol].rcol = strig;
  153. while (is < irow) {
  154. table[is][jcol].rcol = 0;
  155. table[is][jcol].col = SPAN;
  156. is = next(is);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. int
  163. vspand(int ir, int ij, int ifform)
  164. {
  165. if (ir < 0)
  166. return(0);
  167. if (ir >= nlin)
  168. return(0);
  169. if (instead[ir])
  170. return(0);
  171. if (ifform == 0 && ctype(ir, ij) == '^')
  172. return(1);
  173. if (table[ir][ij].rcol != 0)
  174. return(0);
  175. if (fullbot[ir])
  176. return(0);
  177. return(vspen(table[ir][ij].col));
  178. }
  179. int
  180. vspen(char *s)
  181. {
  182. if (s == 0)
  183. return(0);
  184. if (!point(s))
  185. return(0);
  186. return(match(s, SPAN));
  187. }