home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume12 / postscript / part02 < prev    next >
Encoding:
Internet Message Format  |  1987-11-01  |  54.8 KB

  1. Subject:  v12i051:  A PostScript interpreter, Part02/18
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rs@uunet.UU.NET
  5.  
  6. Submitted-by: Crispin Goswell <caag@vd.rl.ac.uk>
  7. Posting-number: Volume 12, Issue 51
  8. Archive-name: postscript/part02
  9.  
  10.  
  11.  
  12. #! /bin/sh
  13. # This is a shell archive.  Remove anything before this line, then unpack
  14. # it by saving it into a file and typing "sh file".  To overwrite existing
  15. # files, type "sh file -c".  You can also feed this as standard input via
  16. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  17. # will see the following message at the end:
  18. #        "End of archive 2 (of 18)."
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'doc/byte-stream' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'doc/byte-stream'\"
  22. else
  23. echo shar: Extracting \"'doc/byte-stream'\" \(4082 characters\)
  24. sed "s/^X//" >'doc/byte-stream' <<'END_OF_FILE'
  25. XA byte Stream Protocol for low-level graphics operations in PostScript.
  26. X
  27. XThe PostScript interpreter does a great deal of work, so the actual
  28. Xgraphics primitives are not usually a bottle-neck. It thus makes sense
  29. Xto run the interpreter on a number-cruncher and watch the graphics on a
  30. Xbitmapped display.
  31. X
  32. XThis file describes a byte-stream protocol which has been used to
  33. Ximplement this.  It is clear that Remote Procedure Call is the correct
  34. Xway to solve this problem - this interface is described because it is
  35. Xportable and expedient. Most RPC mechanisms are intimately tied to a
  36. Xnetwork protocol of some description. This implementation works through
  37. Xpipes and can be sent accross a network with rsh(1) on 4.2BSD.
  38. X
  39. XA version may appear which uses only a simple command language with no
  40. Xenquiry. This unfortunately cannot provide the full functionality
  41. Xrequired. It would not be possible to read back cached fonts from the
  42. Xviewer for example, as they are held at that end.
  43. X
  44. XThe byte-stream is composed of requests of variable length. The first
  45. Xbyte in each request is a type byte and the format of the remaining
  46. Xbytes is determined by this.
  47. X
  48. XThe current formats are as follows. An indented following line
  49. Xindicates the expected reply (if any).
  50. X
  51. XNEW_WINDOW    hard channel; short width, height;
  52. X        /* create a displayed bitmap of the required size. The
  53. X        initial content is unimportant, as it will be cleared
  54. X        to user-white later */
  55. XNEW_BITMAP    hard channel; short width, height;
  56. X        /* create a non-displayed bitmap. These may vary from
  57. X        full-screen size down to character sizes and are used
  58. X        for caching fonts (among other things */
  59. XBITBLT        hard fromchan, tochan; point fromOrigin, toOrigin, extent;
  60. X        rop rasterOp;
  61. X        /* general purpose rasterop */
  62. XSEND_BITMAP    hard channel; short width, height; string data;
  63. X        /* create a bitmap of the given size and put the
  64. X        following bits in it. */
  65. XGET_BITMAP    hard channel;
  66. X        string data;
  67. X        /* return an encoded bitmap describing the given
  68. X        bitmap. The ability to dump from a window is not yet
  69. X        used, but may be in the future. */
  70. XDESTROY_HARDWARE    hard channel;
  71. X        /* release the bitmap or window associated with a channel */
  72. XLINE        hard channel; point from, to; rop rasterOp;
  73. X        /* draw a line as required */
  74. XGET_MATRIX    short width, height;
  75. X        float A, B, C, D, tx, ty;
  76. XGET_TRANSFERSIZE
  77. X        short size;
  78. XSET_TRANSFER    small tran[size];
  79. XPAINT        hard fromchan, tochan; point fromOrigin, toOrigin, extent;
  80. X        /* fromchan will be a bitmap, tochan will be a window.
  81. X        This is the only combination currently required */
  82. XPAINT_LINE    hard channel; point from, to; small hue, sat, bright;
  83. X        /* paint a line as required */
  84. X
  85. XHARD_FLUSH    /* flush hardware output buffer before user gets prompted
  86. X        (if necessary) */
  87. X
  88. XSCREEN_SIZE    float frequency, rotation;
  89. X        short size;
  90. X
  91. XBUILD_SCREEN    float frequency, rotation;
  92. X        short size; small *x, *y;
  93. X        /* returns XY pairs x[0], y[0], x[1], y[1], ... for
  94. X        spot function */
  95. X
  96. XSET_SCREEN    float frequency, rotation; small *thresh;
  97. X        /* send vector of thresholds from spot-function */
  98. X        
  99. X
  100. Xhard    = short
  101. Xshort     = low byte followed by high byte
  102. Xsmall    = short from float scaled by 16384 - the float is expected to be in
  103. X        the range -1 to +1
  104. Xfloat    = free format printable representation terminated by newline as
  105. X        generated by printf("%g\n", arg);
  106. Xpoint    = short x, y;    origin is expected to be top-left and units are
  107. X        device pixels.
  108. Xstring     = an encoding of a bitmap. msb is leftmost, rows are padded to
  109. X        byte-boundaries. Length is computed from the size of the
  110. X        relevant bitmap.
  111. Xrop    = byte    - the meanings are as follows:
  112. X
  113. X#define ROP_FALSE    0    /* F        */
  114. X#define ROP_AND        1    /* S & D    */
  115. X#define ROP_ANDNOT    2    /* S & ~D    */
  116. X#define ROP_SOURCE    3    /* S        */
  117. X#define ROP_NOTAND    4    /* ~S & D    */
  118. X#define ROP_DEST    5    /* D        */
  119. X#define ROP_XOR        6    /* S ^ D    */
  120. X#define ROP_OR        7    /* S | D    */
  121. X#define ROP_NOR        8    /* ~(S | D)    */
  122. X#define ROP_NXOR    9    /* ~(S ^ D)    */
  123. X#define ROP_NOTDEST    10    /* ~D        */
  124. X#define ROP_ORNOT    11    /* S | ~D    */
  125. X#define ROP_NOTSOURCE    12    /* ~S        */
  126. X#define ROP_NOTOR    13    /* ~S | D    */
  127. X#define ROP_NAND    14    /* ~(S & D)    */
  128. X#define ROP_TRUE    15    /* T        */
  129. END_OF_FILE
  130. if test 4082 -ne `wc -c <'doc/byte-stream'`; then
  131.     echo shar: \"'doc/byte-stream'\" unpacked with wrong size!
  132. fi
  133. # end of 'doc/byte-stream'
  134. fi
  135. if test -f 'source/boolean.c' -a "${1}" != "-c" ; then 
  136.   echo shar: Will not clobber existing file \"'source/boolean.c'\"
  137. else
  138. echo shar: Extracting \"'source/boolean.c'\" \(4214 characters\)
  139. sed "s/^X//" >'source/boolean.c' <<'END_OF_FILE'
  140. X/*
  141. X * Copyright (C) Rutherford Appleton Laboratory 1987
  142. X * 
  143. X * This source may be copied, distributed, altered or used, but not sold for profit
  144. X * or incorporated into a product except under licence from the author.
  145. X * It is not in the public domain.
  146. X * This notice should remain in the source unaltered, and any changes to the source
  147. X * made by persons other than the author should be marked as such.
  148. X * 
  149. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  150. X */
  151. X#include "main.h"
  152. X
  153. XObject True, False;
  154. Xstatic Object OpNot;
  155. Xstatic int Cvs (), And (), Or (), Not (), Xor ();
  156. Xstatic int PEq (), PNeq (), EqBoolean ();
  157. X
  158. XInitBoolean ()
  159. X {
  160. X    True =     MakeObject (Boolean);
  161. X    False = MakeObject (Boolean);
  162. X    
  163. X     True.u.Boolean = TRUE;
  164. X    False.u.Boolean = FALSE;
  165. X    
  166. X    Install ("true",    True);
  167. X    Install ("false",    False);
  168. X      
  169. X     TypeInstallOp (Boolean, "cvs",     Cvs,          2, 1, 0, 0, Boolean, String);
  170. X     TypeInstallOp (Boolean, "and",     And,          2, 1, 0, 0, Boolean, Boolean);
  171. X     TypeInstallOp (Boolean, "or",     Or,          2, 1, 0, 0, Boolean, Boolean);
  172. X     TypeInstallOp (Boolean, "xor",     Xor,          2, 1, 0, 0, Boolean, Boolean);
  173. X     TypeInstallOp (Boolean, "not",     Not,          1, 1, 0, 0, Boolean);
  174. X     TypeInstallOp (Boolean, "eq",     EqBoolean,      2, 1, 0, 0, Boolean, Boolean);
  175. X     
  176. X    OpNot = Lookup (Boolean, NameFrom ("not"));
  177. X    
  178. X     InstallOp ("eq",    PEq,          2, 1, 0, 0, Poly, Poly);
  179. X     InstallOp ("ne",    PNeq,         2, 1, 0, 0, Poly, Poly);
  180. X     InstallOp ("ge",     PolyPair,     2, 2, 0, 0, Poly, Poly);
  181. X     InstallOp ("gt",     PolyPair,     2, 2, 0, 0, Poly, Poly);
  182. X     InstallOp ("le",     PolyPair,     2, 2, 0, 0, Poly, Poly);
  183. X     InstallOp ("lt",     PolyPair,     2, 2, 0, 0, Poly, Poly);
  184. X     InstallOp ("not",     PolyFirst,     1, 1, 0, 0, Poly);
  185. X     InstallOp ("and",     PolyPair,     2, 2, 0, 0, Poly, Poly);
  186. X     InstallOp ("or",     PolyPair,     2, 2, 0, 0, Poly, Poly);
  187. X     InstallOp ("xor",     PolyPair,     2, 2, 0, 0, Poly, Poly);
  188. X     InstallOp ("bitshift",     PolyPair,     2, 2, 0, 0, Poly, Poly);
  189. X }
  190. X
  191. XObject MakeBoolean (b) int b;
  192. X {
  193. X    return b ? True : False;
  194. X }
  195. X
  196. Xint BodyBoolean (b) Object b;
  197. X {
  198. X     return b.u.Boolean;
  199. X }
  200. X
  201. Xstatic int Cvs (v, string) Object v, string;
  202. X {
  203. X     char *choice = BodyBoolean (v) ? "true" : "false";
  204. X     int length = strlen (choice);
  205. X     
  206. X    if (lengthString (string) < length)
  207. X        return Error (PRangeCheck);
  208. X    VOID Bcopy (BodyString (string), choice, length);
  209. X    return Push (OpStack, getIString (string, 0, length));
  210. X }
  211. X
  212. Xstatic int PEq (a, b) Object a, b;    /* any any --- boolean */
  213. X {
  214. X    if (!rCheck (a) || !rCheck (b))
  215. X         return Error (PInvAccess);
  216. X    if (TypeOf (a) == Name && TypeOf (b) == String)
  217. X         a = StringName (a);
  218. X     else if (TypeOf (b) == Name && TypeOf (a) == String)
  219. X         b = StringName (b);
  220. X    else if (TypeOf (a) == Real && TypeOf (b) == Integer)
  221. X            b = RealInteger (b);
  222. X    else if (TypeOf (a) == Integer && TypeOf (b) == Real)
  223. X            a = RealInteger (a);
  224. X     if (TypeOf (a) == TypeOf (b))
  225. X      {
  226. X          VOID Push (OpStack, a);
  227. X          VOID Push (OpStack, b);
  228. X          return Apply (TypeOf (a));
  229. X       }
  230. X     else
  231. X         return Push (OpStack, False);
  232. X }
  233. X
  234. Xstatic int PNeq (a, b) Object a, b;        /* any any --- boolean */
  235. X {
  236. X     if (!rCheck (a) || !rCheck (b))
  237. X          return Error (PInvAccess);
  238. X    if (TypeOf (a) == Name && TypeOf (b) == String)
  239. X         a = StringName (a);
  240. X     else if (TypeOf (b) == Name && TypeOf (a) == String)
  241. X         b = StringName (b);
  242. X    else if (TypeOf (a) == Real && TypeOf (b) == Integer)
  243. X            b = RealInteger (b);
  244. X    else if (TypeOf (a) == Integer && TypeOf (b) == Real)
  245. X            a = RealInteger (a);
  246. X     if (TypeOf (a) == TypeOf (b))
  247. X       {
  248. X          VOID Push (OpStack, a);
  249. X          VOID Push (OpStack, b);
  250. X          VOID Push (ExecStack, OpNot);
  251. X          Self = NameFrom ("eq");
  252. X          return Apply (TypeOf (a));
  253. X       }
  254. X     else
  255. X         return Push (OpStack, True);
  256. X }
  257. X
  258. Xstatic int EqBoolean (a, b) Object a, b;
  259. X {
  260. X      return Push (OpStack, MakeBoolean (BodyBoolean (a) == BodyBoolean (b)));
  261. X }
  262. X
  263. Xstatic int Not (bool) Object bool;
  264. X {
  265. X     return Push (OpStack, MakeBoolean (!BodyBoolean (bool)));
  266. X }
  267. X
  268. Xstatic int And (a, b) Object a, b;
  269. X {
  270. X     return Push (OpStack, MakeBoolean (BodyBoolean (a) && BodyBoolean (b)));
  271. X }
  272. X
  273. Xstatic int Or (a, b) Object a, b;
  274. X {
  275. X     return Push (OpStack, MakeBoolean (BodyBoolean (a) || BodyBoolean (b)));
  276. X }
  277. X
  278. Xstatic int Xor (a, b) Object a, b;
  279. X {
  280. X     return Push (OpStack, MakeBoolean (BodyBoolean (a) != BodyBoolean (b)));
  281. X }
  282. END_OF_FILE
  283. if test 4214 -ne `wc -c <'source/boolean.c'`; then
  284.     echo shar: \"'source/boolean.c'\" unpacked with wrong size!
  285. fi
  286. # end of 'source/boolean.c'
  287. fi
  288. if test -f 'source/colour.c' -a "${1}" != "-c" ; then 
  289.   echo shar: Will not clobber existing file \"'source/colour.c'\"
  290. else
  291. echo shar: Extracting \"'source/colour.c'\" \(4757 characters\)
  292. sed "s/^X//" >'source/colour.c' <<'END_OF_FILE'
  293. X/*
  294. X * Copyright (C) Rutherford Appleton Laboratory 1987
  295. X * 
  296. X * This source may be copied, distributed, altered or used, but not sold for profit
  297. X * or incorporated into a product except under licence from the author.
  298. X * It is not in the public domain.
  299. X * This notice should remain in the source unaltered, and any changes to the source
  300. X * made by persons other than the author should be marked as such.
  301. X * 
  302. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  303. X */
  304. X#include "main.h"
  305. X#include "graphics.h"
  306. X
  307. X
  308. XColour NewGray (level) float level;
  309. X {
  310. X     return NewHSBColour (0.0, 0.0, level);
  311. X }
  312. X
  313. XColour NewColour (h, s, b) float h, s, b;
  314. X {
  315. X     return NewHSBColour (h, s, b);
  316. X }
  317. X
  318. XColour NewHSBColour (h, s, b) float h, s, b;
  319. X {
  320. X    Colour res;
  321. X    
  322. X    res.hue = h;
  323. X    res.saturation = s;
  324. X    res.brightness = b;
  325. X    
  326. X    return res;
  327. X }
  328. X
  329. XColour NewRGBColour (R, G, B) float R, G, B;
  330. X {
  331. X     float H, S, L, m, M, r, g, b;
  332. X     
  333. X     M = R > G ? R : G; M = M > B ? M : B;
  334. X     m = R < G ? R : G; m = m < B ? m : B;
  335. X     if (M != m)
  336. X      {
  337. X          r = (M - R) / (M - m);
  338. X          g = (M - G) / (M - m);
  339. X          b = (M - B) / (M - m);
  340. X      }
  341. X     L = (M + m) / 2;
  342. X     
  343. X     if (M == m)
  344. X          S = 0;
  345. X     else if (L <= 0.5)
  346. X         S = (M - m) / (M + m);
  347. X     else
  348. X         S = (M - m) / (2 - M - m);
  349. X     
  350. X     if (S == 0)
  351. X         H = 0;
  352. X     else if (R == M)
  353. X         H = 2 + b - g;
  354. X     else if (G == M)
  355. X         H = 4 + r - b;
  356. X     else
  357. X         H = 6 + g - r;
  358. X     
  359. X     H /= 6;
  360. X    
  361. X    return NewHSBColour (H, S, L);
  362. X }
  363. X
  364. Xvoid ColourHSB (colour, h, s, b) Colour colour; float *h, *s, *b;
  365. X {
  366. X     *h = colour.hue;
  367. X     *s = colour.saturation;
  368. X     *b = colour.brightness;
  369. X }
  370. X
  371. Xfloat Value (m, M, hue) float m, M, hue;
  372. X {
  373. X     if (hue < 0.0)
  374. X         hue += 2 * PI;
  375. X     if (hue < PI / 3)
  376. X         return m + (M - m) * hue / (PI / 3);
  377. X     else if (hue < PI)
  378. X         return M;
  379. X     else if (hue < 4 * PI / 3)
  380. X         return m + (M - m) * (4 * PI / 3 - hue) / (PI / 3);
  381. X     else
  382. X         return m;
  383. X }
  384. X
  385. Xvoid ColourRGB (colour, r, g, b) Colour colour; float *r, *g, *b;
  386. X {
  387. X     float H = colour.hue, S = colour.saturation, L = colour.brightness;
  388. X     float m, M;
  389. X    
  390. X    if (L <= .5)
  391. X        M = L * (1 + S);
  392. X    else
  393. X        M = (L + S) - (L * S);
  394. X    m = 2 * L - M;
  395. X    H *= 2 * PI;
  396. X    *r = Value (m, M, H);
  397. X    *g = Value (m, M, H - 2 * PI / 3);
  398. X    *b = Value (m, M, H - 4 * PI / 3);
  399. X }
  400. X
  401. Xfloat Brightness (colour) Colour colour;
  402. X {
  403. X     return colour.brightness;
  404. X }
  405. X
  406. X
  407. X/* 
  408. X * The RGB colour model and Hue Saturation Brightness/Lightness model
  409. X * are derived from the paper:
  410. X * 
  411. X *     "Colour Gamut Transform Pairs"
  412. X *    by Alvy Ray Smith in Computer Graphics Volume 12 #3. August 1978
  413. X * 
  414. X * PostScript uses the NTSC video colour weights.
  415. X */
  416. X
  417. X#define RED_WEIGHT    0.3333    /* .3    */
  418. X#define GREEN_WEIGHT    0.3333    /* .59    */
  419. X#define BLUE_WEIGHT    0.3333    /* .11    */
  420. X
  421. X#define a0    120.0 * PI / 180        /* 156.58 * PI / 180 */
  422. X#define a1    120.0 * PI / 180        /* 115.68 * PI / 180 */
  423. X
  424. X#define A0    0.0        /* -21.60 * PI / 180 */
  425. X#define A1    0.0        /* 14.98 * PI / 180 */
  426. X#define A2    0.0        /* 10.65 * PI / 180 */
  427. X
  428. X
  429. X/*
  430. Xstatic int SetRGB (red, green, blue) Object red, green, blue;
  431. X {
  432. X     float R = BodyReal (red), G = BodyReal (green), B = BodyReal (blue);
  433. X     float r, g, b, r_, g_, b_, d, x, rr, gg, bb, wr_, k0, k1, min, H, S, L;
  434. X     
  435. X     if (R < 0 || R > 1 || G < 0 || G > 1 || B < 0 || B > 1)
  436. X         return Error (PRangeCheck);
  437. X     L = R * RED_WEIGHT + G * GREEN_WEIGHT + B * BLUE_WEIGHT;
  438. X     r_ = R/L; g_ = G/L; b_ = B/L;
  439. X     r = RED_WEIGHT * r_; g = GREEN_WEIGHT * g_; b = BLUE_WEIGHT * b_;
  440. X     rr = r - RED_WEIGHT; gg = g - GREEN_WEIGHT; bb = b - BLUE_WEIGHT;
  441. X     min = (r_ < g_ ? r_ : g_); min = min < b_ ? min : b_;
  442. X     S = 1 - min;
  443. X     if (S != 0)
  444. X      {
  445. X         k0 = sqrt (rr*rr + gg*gg + bb*bb);
  446. X         wr_ = 1 - RED_WEIGHT;
  447. X         d = wr_ * rr - GREEN_WEIGHT * gg + BLUE_WEIGHT * bb;
  448. X         k1 = sqrt (wr_ * wr_ + GREEN_WEIGHT * GREEN_WEIGHT - BLUE_WEIGHT * BLUE_WEIGHT);
  449. X         x = d / (k0 * k1);
  450. X         H = PI / 2 - atan2 (x, sqrt (1 - x*x));
  451. X         if (b_ > g_)
  452. X             H = 2 * PI - H;
  453. X         H /= 2 * PI;
  454. X      }
  455. X     if (H < 0 || H > 1 || S < 0 || S > 1 || L < 0 || L > 1)
  456. X         return Error (PRangeCheck);
  457. X    gstate->colour = NewHSLColour (H, S, L);
  458. X     return TRUE;
  459. X }
  460. X
  461. Xstatic int GetRGB ()
  462. X {
  463. X     float H = gstate->colour.hue, S = gstate->colour.saturation, L = gstate->colour.brightness;
  464. X     float r, g, b, Wr = RED_WEIGHT, Wg = GREEN_WEIGHT, Wb = BLUE_WEIGHT;
  465. X     
  466. X     H *= 2 * PI;
  467. X     if (0 <= H && H <= a0)
  468. X      {
  469. X          H -= A0;
  470. X          b = Wb * (1 - S);
  471. X          r = Wr + Wb * S * cos (H) / cos (PI / 3 - H);
  472. X          g = 1 - (r + b);
  473. X      }
  474. X     else if (a0 <= H  && H <= (a0 + a1))
  475. X      {
  476. X          H -= a0 + A1;
  477. X          r = Wr * (1 - S);
  478. X          g = Wg + Wr * S * cos (H) / cos (PI / 3 - H);
  479. X          b = 1 - (r + g);
  480. X      }
  481. X     else
  482. X      {
  483. X          H -= a0 + a1 + A2;
  484. X          g = Wg * (1 - S);
  485. X          b = Wb + Wg * S * cos (H) / cos (PI / 3 - H);
  486. X          r = 1 - (g + b);
  487. X      }
  488. X     
  489. X     VOID Push (OpStack, MakeReal (L* r / Wr));
  490. X     VOID Push (OpStack, MakeReal (L* g / Wg));
  491. X     VOID Push (OpStack, MakeReal (L* b / Wb));
  492. X    return TRUE;
  493. X }
  494. X
  495. X*/
  496. END_OF_FILE
  497. if test 4757 -ne `wc -c <'source/colour.c'`; then
  498.     echo shar: \"'source/colour.c'\" unpacked with wrong size!
  499. fi
  500. # end of 'source/colour.c'
  501. fi
  502. if test -f 'source/config.c' -a "${1}" != "-c" ; then 
  503.   echo shar: Will not clobber existing file \"'source/config.c'\"
  504. else
  505. echo shar: Extracting \"'source/config.c'\" \(3360 characters\)
  506. sed "s/^X//" >'source/config.c' <<'END_OF_FILE'
  507. X/*
  508. X * Copyright (C) Rutherford Appleton Laboratory 1987
  509. X * 
  510. X * This source may be copied, distributed, altered or used, but not sold for profit
  511. X * or incorporated into a product except under licence from the author.
  512. X * It is not in the public domain.
  513. X * This notice should remain in the source unaltered, and any changes to the source
  514. X * made by persons other than the author should be marked as such.
  515. X * 
  516. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  517. X */
  518. X#include "main.h"
  519. X
  520. Xstatic int SizeArray = 53, SizeMark = 19, SizeBoolean = 53, SizeNull = 19;
  521. Xstatic int SizeCondition = 19, SizeFile = 41, SizeString = 71, SizePoly = 19;
  522. Xstatic int SizeInteger = 71, SizeReal = 71, SizeName = 19, SizeOperator = 19;
  523. Xint SizeSysDict = 1001, SizeDictionary = 53, SizeFloat = 0, SizeFontID = 19;
  524. X
  525. XType Array, Mark, Boolean, Dictionary, Condition, Null, File, Integer;
  526. XType Real, Name, Operator, String, Poly, Float, FontID;
  527. X
  528. Xextern Object Absent, Nil, SysDict;
  529. X
  530. Xchar default_library[] = "/usr/ral/lib/postscript";
  531. X
  532. XInit ()
  533. X {    
  534. X    Nil     = MakeObject ((Type) 0);
  535. X    Null          = Nil.type = MakeType (SizeNull);
  536. X    EmptyDict (Null);    /* needed because of this recursion */
  537. X    
  538. X    Dictionary    = MakeType (SizeDictionary);
  539. X    Condition    = MakeType (SizeCondition);
  540. X     Name         = MakeType (SizeName);
  541. X    Operator    = MakeType (SizeOperator);
  542. X     Array         = MakeType (SizeArray);
  543. X     Mark         = MakeType (SizeMark);
  544. X     Boolean         = MakeType (SizeBoolean);
  545. X     File         = MakeType (SizeFile);
  546. X     Integer     = MakeType (SizeInteger);
  547. X     Real         = MakeType (SizeReal);
  548. X     String         = MakeType (SizeString);
  549. X     Poly         = MakeType (SizePoly);
  550. X     Float        = MakeType (SizeFloat);
  551. X    FontID        = MakeType (SizeFontID);
  552. X    
  553. X    Message ("InitDictionary");    InitDictionary ();
  554. X    Message ("InitOperator");    InitOperator ();
  555. X    Message ("InitName");    InitName ();
  556. X    Message ("InitPoly");    InitPoly ();
  557. X    Message ("InitArray");    InitArray ();
  558. X    Message ("InitStack");    InitStack ();
  559. X    Message ("InitFile");    InitFile ();
  560. X    Message ("InitMisc");    InitMisc ();
  561. X    Message ("InitBoolean");    InitBoolean ();
  562. X    Message ("InitInteger");    InitInteger ();
  563. X    Message ("InitReal");    InitReal ();
  564. X    Message ("InitMath");    InitMath ();
  565. X    Message ("InitString");    InitString ();
  566. X    Message ("InitProperty");    InitProperty ();
  567. X    Message ("InitControl");    InitControl ();
  568. X    
  569. X    Message ("InitMatrix");    InitMatrix ();
  570. X    Message ("InitPath");    InitPath ();
  571. X     Message ("InitFill");    InitFill ();
  572. X    Message ("InitStroke");    InitStroke ();
  573. X    Message ("InitGSave");    InitGSave ();
  574. X    Message ("InitDevices");    InitDevices ();
  575. X    Message ("InitCache");    InitCache ();
  576. X    Message ("InitImage");    InitImage ();
  577. X     Message ("InitState");    InitState ();
  578. X    Message ("InitFont");    InitFont ();
  579. X    Message ("InitUnix");    InitUnix ();
  580. X    
  581. X    Install ("nulltype",        DictFrom (Null));
  582. X    Install ("dicttype",        DictFrom (Dictionary));
  583. X    Install ("conditiontype",    DictFrom (Condition));
  584. X     Install ("nametype",        DictFrom (Name));
  585. X    Install ("operatortype",    DictFrom (Operator));
  586. X     Install ("arraytype",        DictFrom (Array));
  587. X     Install ("marktype",        DictFrom (Mark));
  588. X     Install ("booleantype",        DictFrom (Boolean));
  589. X     Install ("filetype",        DictFrom (File));
  590. X     Install ("integertype",        DictFrom (Integer));
  591. X     Install ("realtype",        DictFrom (Real));
  592. X     Install ("stringtype",        DictFrom (String));
  593. X     Install ("polytype",        DictFrom (Poly));
  594. X    Install ("fonttype",        DictFrom (FontID));
  595. X    
  596. X    Install ("version",    StringFrom ("Version 1.4"));
  597. X }
  598. END_OF_FILE
  599. if test 3360 -ne `wc -c <'source/config.c'`; then
  600.     echo shar: \"'source/config.c'\" unpacked with wrong size!
  601. fi
  602. # end of 'source/config.c'
  603. fi
  604. if test -f 'source/gsave.c' -a "${1}" != "-c" ; then 
  605.   echo shar: Will not clobber existing file \"'source/gsave.c'\"
  606. else
  607. echo shar: Extracting \"'source/gsave.c'\" \(2319 characters\)
  608. sed "s/^X//" >'source/gsave.c' <<'END_OF_FILE'
  609. X/*
  610. X * Copyright (C) Rutherford Appleton Laboratory 1987
  611. X * 
  612. X * This source may be copied, distributed, altered or used, but not sold for profit
  613. X * or incorporated into a product except under licence from the author.
  614. X * It is not in the public domain.
  615. X * This notice should remain in the source unaltered, and any changes to the source
  616. X * made by persons other than the author should be marked as such.
  617. X * 
  618. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  619. X */
  620. X#include "main.h"
  621. X#include "graphics.h"
  622. X
  623. Xstatic struct state gstate_stack [MAXGSAVES];
  624. X
  625. Xstruct state *gstate = gstate_stack;
  626. X
  627. Xstatic int gstate_height = 0;
  628. X
  629. Xint GSave (), GRestore (), GRestoreAll ();
  630. X
  631. XInitGSave ()
  632. X {
  633. X     InstallOp ("gsave",        GSave,         0, 0, 0, 0);
  634. X     InstallOp ("grestore",        GRestore,     0, 0, 0, 0);
  635. X     InstallOp ("grestoreall",    GRestoreAll,     0, 0, 0, 0);
  636. X     
  637. X     gstate->screen.count = 0;    /* stops thresh getting freed accidentally */
  638. X     gstate->transfer.tcount = 0;    /* stops tran getting freed accidentally */
  639. X }
  640. X
  641. Xint GSave ()
  642. X {
  643. X     if (gstate_height == MAXGSAVES - 1)
  644. X         return Error (PLimitCheck);
  645. X     gstate_stack [gstate_height + 1] = gstate_stack [gstate_height];
  646. X     ++gstate_height; ++gstate;
  647. X     gstate->path = PathCopy (gstate->path);
  648. X     gstate->clip = PathCopy (gstate->clip);
  649. X     LinkDevice (gstate->device);
  650. X     LinkDevice (gstate->clipdevice);
  651. X     ++gstate->screen.count;
  652. X     ++gstate->transfer.tcount;
  653. X     
  654. X     return TRUE;
  655. X }
  656. X
  657. Xint GRestore ()
  658. X {
  659. X     if (gstate_height == 0)
  660. X         VOID InitGraphics ();
  661. X     else
  662. X      {
  663. X          int sflag = FALSE, tflag = FALSE;
  664. X          
  665. X         UnlinkDevice (gstate->device);
  666. X          UnlinkDevice (gstate->clipdevice);
  667. X        PathFree (gstate->path);
  668. X         PathFree (gstate->clip);
  669. X         
  670. X         if (gstate->screen.count == 1)
  671. X          {
  672. X             Free ((char *) gstate->screen.thresh);
  673. X             sflag = TRUE;
  674. X          }
  675. X          if (gstate->transfer.tcount == 1)
  676. X          {
  677. X             Free ((char *) gstate->transfer.tran);
  678. X             tflag = TRUE;
  679. X          }
  680. X          --gstate_height;
  681. X         --gstate;
  682. X         if (sflag)
  683. X              SetScreen (gstate->screen.frequency, gstate->screen.rotation, gstate->screen.thresh);
  684. X          if (tflag)
  685. X              SetTransfer (gstate->transfer.tran);
  686. X          SetClipHardware (gstate->device->dev, (gstate->clipdevice ? gstate->clipdevice->dev : NULL));
  687. X     }
  688. X     
  689. X     return TRUE;
  690. X }
  691. X
  692. Xint GRestoreAll ()
  693. X {
  694. X    while (gstate != gstate_stack)
  695. X        VOID GRestore ();
  696. X     InitGraphics ();
  697. X     
  698. X     return TRUE;
  699. X }
  700. END_OF_FILE
  701. if test 2319 -ne `wc -c <'source/gsave.c'`; then
  702.     echo shar: \"'source/gsave.c'\" unpacked with wrong size!
  703. fi
  704. # end of 'source/gsave.c'
  705. fi
  706. if test -f 'source/main.h' -a "${1}" != "-c" ; then 
  707.   echo shar: Will not clobber existing file \"'source/main.h'\"
  708. else
  709. echo shar: Extracting \"'source/main.h'\" \(4101 characters\)
  710. sed "s/^X//" >'source/main.h' <<'END_OF_FILE'
  711. X/*
  712. X * Copyright (C) Crispin Goswell 1987, All Rights Reserved.
  713. X */
  714. X
  715. X#include <stdio.h>
  716. X#include <math.h>
  717. X#include <assert.h>
  718. X#include <setjmp.h>
  719. X#include <strings.h>
  720. X
  721. X#define NONE        (-2)
  722. X#define EOS        '\0'
  723. X#define TRUE        1
  724. X#define FALSE        0
  725. X
  726. X#define BUFSIZE         1000
  727. X
  728. X#define VOID    (void)
  729. X
  730. X#define READABLE     01
  731. X#define WRITEABLE     02
  732. X#define EXECUTABLE     04
  733. X#define PERMANENT    010
  734. X#define PIPED        020
  735. X
  736. Xtypedef struct dict_struct *Type;
  737. X
  738. Xtypedef struct object
  739. X {
  740. X     int flags;
  741. X    Type type;
  742. X    int Length;
  743. X    union
  744. X     {
  745. X        int Integer, Boolean, Font;
  746. X         float Real;
  747. X         Type Dictionary;
  748. X         unsigned char *String;
  749. X         struct name_struct *Name;
  750. X         struct op_struct *Operator;
  751. X         struct file_struct *File;
  752. X         struct object *Array;
  753. X     } u;
  754. X    
  755. X } Object;
  756. Xenum file_type { StringFile, StreamFile };
  757. X
  758. Xstruct file_struct
  759. X {
  760. X     enum file_type file_type;
  761. X     int available;
  762. X     union
  763. X      {
  764. X          unsigned char    *c_ptr;
  765. X          FILE    *f_ptr;
  766. X      } f;
  767. X };
  768. X
  769. Xstruct dict_entry
  770. X {
  771. X     Object entry_key, entry_value;
  772. X };
  773. X
  774. Xstruct dict_struct
  775. X {
  776. X     int dict_flags, dict_size, dict_fill;
  777. X     struct dict_entry *dict_body;
  778. X };
  779. X
  780. Xtypedef struct stack
  781. X {
  782. X     int stack_fill, stack_size;
  783. X     Object overflow, underflow, *stack_body;
  784. X } *Stack, StackOb;
  785. XObject SameFlags (), MakeObject (), Cvx (), Cvlit (), ReadOnly (), WriteOnly (), ExecOnly ();
  786. Xint OpCheck (), min (), rCheck (), wCheck (), xCheck ();
  787. XObject MakeArray (), ParseArray (), getArray (), getIArray (), *BodyArray ();
  788. XObject MakeBoolean ();
  789. X
  790. X
  791. XObject MakeDict (), DictLoad (), Lookup (), DictFrom (), Load ();
  792. XType MakeType (), TypeOf (), BodyDict ();
  793. X
  794. Xextern int EqTrue (), Equal ();
  795. X
  796. XObject FileFrom (), FileString ();
  797. X
  798. Xint Getch ();
  799. Xstruct file_struct *BodyFile ();
  800. XObject MakeInteger (), IntReal ();
  801. X
  802. XObject ParseNumber ();
  803. Xfloat Deg (), Rad ();
  804. XObject ParseId (), NameFrom (), MakeName (), Cvn (), StringName ();
  805. Xunsigned char *BodyName ();
  806. XObject MakeOp (), NameOperator ();
  807. XObject Parse ();
  808. X
  809. X
  810. Xint PolyFirst (), PolySecond (), PolyThird (), PolyPair ();
  811. X
  812. XObject MakeReal (), RealInteger ();
  813. X
  814. Xfloat BodyReal (), BodyFloat ();
  815. X
  816. Xchar *Malloc ();
  817. XObject Pop (), Top (), Where (), DictLookup ();
  818. Xint Push ();
  819. X
  820. Xextern Object MakeString (), StringFrom (), getIString (), ParseString (), ParseHexString ();
  821. Xunsigned char *BodyString ();
  822. Xint lengthString ();
  823. X
  824. Xextern Object PDictFull;
  825. Xextern Object PDictOverflow,        PInvFont,        PSyntaxError;
  826. Xextern Object PDictUnderflow,        PInvRestore,        PTypeCheck;
  827. Xextern Object PExecOverflow,        PIOError,        PUndefined;
  828. Xextern Object PExecUnderflow,        PLimitCheck,        PUnFilename;
  829. Xextern Object PInterrupt,        PNoCurrentPoint,    PUnResult;
  830. Xextern Object PInvAccess,        PRangeCheck,        PUnMatched;
  831. Xextern Object PInvExit,            POpOverflow,        PUnregistered;
  832. Xextern Object PInvFileAccess,        POpUnderflow,        PVMError;
  833. X
  834. Xextern Type Boolean, Mark, String, Real, Poly, Operator;
  835. Xextern Type Name, File, Dictionary, Condition, Null, Integer;
  836. Xextern Type Array, Mark, Condition, Null, Float, FontID;
  837. X
  838. Xextern Object SysDict, Absent, Nil;
  839. Xextern Object True, False, Marker, Self;
  840. Xextern Object OpInterp, Lbracket, Rbracket;
  841. Xextern Object StatementEdit, Fstdin, Fstdout, Fstderr;
  842. X
  843. Xextern Stack OpStack, ExecStack, DictStack;
  844. Xextern jmp_buf env;
  845. Xextern int interactive, verbose;
  846. Xextern char default_library[], *library;
  847. Xextern unsigned char *Bcopy ();
  848. Xextern FILE *vfp, *Fopen ();
  849. Xextern void Fclose ();
  850. X
  851. X
  852. X#define TypeOf(a)     ((a).type)
  853. X
  854. X#define Push(stack, object) (((stack)->stack_fill != (stack)->stack_size) ?    \
  855. X        ((stack)->stack_body[(stack)->stack_fill] = (object), (stack)->stack_fill++, TRUE) : FALSE)
  856. X
  857. X#define Pop(stack)     ((stack)->stack_body[--(stack)->stack_fill])
  858. X
  859. X#define Top(stack)    ((stack)->stack_body[(stack)->stack_fill - 1])
  860. X
  861. X#define Height(stack)    ((stack)->stack_fill)
  862. X
  863. X#define MaxStack(stack)        ((stack)->stack_size)
  864. X
  865. Xextern int getchbuf;
  866. X
  867. X#define BodyFile(file) ((file).u.File)
  868. X
  869. X#define StatusFile(file) (BodyFile(file)->available != 0)
  870. X
  871. X#define Getch(file) ((StatusFile(file) && BodyFile(file)->file_type == StreamFile) ?\
  872. X    ((getchbuf = getc (BodyFile(file)->f.f_ptr)), \
  873. X        ((getchbuf != EOF) ? getchbuf : ((BodyFile(file)->available = 0), Close (file), EOF))) \
  874. X    : GeneralGetch (file))
  875. END_OF_FILE
  876. if test 4101 -ne `wc -c <'source/main.h'`; then
  877.     echo shar: \"'source/main.h'\" unpacked with wrong size!
  878. fi
  879. # end of 'source/main.h'
  880. fi
  881. if test -f 'source/mat.c' -a "${1}" != "-c" ; then 
  882.   echo shar: Will not clobber existing file \"'source/mat.c'\"
  883. else
  884. echo shar: Extracting \"'source/mat.c'\" \(2868 characters\)
  885. sed "s/^X//" >'source/mat.c' <<'END_OF_FILE'
  886. X/*
  887. X * Copyright (C) Rutherford Appleton Laboratory 1987
  888. X * 
  889. X * This source may be copied, distributed, altered or used, but not sold for profit
  890. X * or incorporated into a product except under licence from the author.
  891. X * It is not in the public domain.
  892. X * This notice should remain in the source unaltered, and any changes to the source
  893. X * made by persons other than the author should be marked as such.
  894. X * 
  895. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  896. X */
  897. X#include "main.h"
  898. X#include "graphics.h"
  899. X
  900. XMatrix identity =   { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 };
  901. X
  902. XMatrix NewMatrix (A, B, C, D, tx, ty) float A, B, C, D, tx, ty;
  903. X {
  904. X     Matrix m;
  905. X     
  906. X     m.A = A; m.B = B; m.C = C; m.D = D; m.tx = tx; m.ty = ty;
  907. X     
  908. X     return m;
  909. X }
  910. X
  911. XVector NewVector (A, B, vt) float A, B, vt;
  912. X {
  913. X     Vector v;
  914. X     
  915. X     v.vx = A; v.vy = B; v.vt = vt;
  916. X     
  917. X     return v;
  918. X }
  919. X
  920. XMatrix Translate (m, x, y) Matrix m; float x, y;
  921. X {
  922. X     return NewMatrix (m.A,                   m.B,
  923. X                m.C,                   m.D,
  924. X                x * m.A + y * m.C + m.tx, x * m.B + y * m.D + m.ty);
  925. X }
  926. X
  927. XHereTranslate (pm, p) Matrix *pm; Point p;
  928. X {
  929. X     pm->tx += p.x * pm->A + p.y * pm->C;
  930. X     pm->ty += p.x * pm->B + p.y * pm->D;
  931. X }
  932. X
  933. XMatrix Scale (m, x, y) Matrix m; float x, y;
  934. X {
  935. X    return NewMatrix (m.A * x,     m.B * x,
  936. X               m.C * y,    m.D * y,
  937. X               m.tx,    m.ty);
  938. X }
  939. X
  940. XMatrix Rotate (m, a) Matrix m; float a;
  941. X {
  942. X     float ca = cos(a), sa = sin(a);
  943. X     
  944. X     return NewMatrix (m.A * ca + m.C * sa, m.B * ca + m.D * sa,
  945. X                m.C * ca - m.A * sa, m.D * ca - m.B * sa,
  946. X                m.tx,        m.ty);
  947. X }
  948. X
  949. XMatrix MatMult (a, b) Matrix a, b;
  950. X {
  951. X     return NewMatrix ( a.A  * b.A + a.B * b.C,        a.A * b.B + a.B * b.D,
  952. X                a.C  * b.A + a.D * b.C,        a.C * b.B + a.D * b.D,
  953. X                a.tx * b.A + a.ty * b.C + b.tx, a.tx * b.B + a.ty * b.D + b.ty);
  954. X }
  955. X
  956. XMatrix MatInvert (m) Matrix m;        /* know any good matrix inversion algorithms ? */
  957. X {                    /* this one will be simplistic */
  958. X     float det = m.A * m.D - m.B * m.C;
  959. X     
  960. X    return NewMatrix (m.D / det,               -m.B / det,
  961. X             -m.C  / det,                m.A / det,
  962. X             (m.C * m.ty - m.D * m.tx) / det, -(m.A * m.ty - m.B * m.tx) / det);
  963. X }
  964. X
  965. XVector Transform (v, m) Vector v; Matrix m;
  966. X {
  967. X     return NewVector (v.vx * m.A + v.vy * m.C + v.vt * m.tx,
  968. X                v.vx * m.B + v.vy * m.D + v.vt * m.ty,
  969. X                v.vt);
  970. X }
  971. X
  972. XVector DTransform (v, m) Vector v; Matrix m;
  973. X {
  974. X     return NewVector (v.vx * m.A + v.vy * m.C,
  975. X                v.vx * m.B + v.vy * m.D,
  976. X                v.vt);
  977. X }
  978. X
  979. XVector ITransform (v, mi) Vector v; Matrix mi;
  980. X {
  981. X     Matrix m;
  982. X     
  983. X     m = MatInvert (mi);
  984. X     return NewVector (v.vx * m.A + v.vy * m.C + v.vt * m.tx,
  985. X                v.vx * m.B + v.vy * m.D + v.vt * m.ty,
  986. X                v.vt);
  987. X }
  988. X
  989. XVector IDTransform (v, mi) Vector v; Matrix mi;
  990. X {
  991. X     Matrix m;
  992. X     
  993. X     m = MatInvert (mi);
  994. X     return NewVector (v.vx * m.A + v.vy * m.C,
  995. X                v.vx * m.B + v.vy * m.D,
  996. X                v.vt);
  997. X }
  998. X
  999. XVector DiffVector (a, o) Vector a, o;
  1000. X {
  1001. X     return NewVector (a.vx - o.vx, a.vy - o.vy, 1.0);
  1002. X }
  1003. END_OF_FILE
  1004. if test 2868 -ne `wc -c <'source/mat.c'`; then
  1005.     echo shar: \"'source/mat.c'\" unpacked with wrong size!
  1006. fi
  1007. # end of 'source/mat.c'
  1008. fi
  1009. if test -f 'source/name.c' -a "${1}" != "-c" ; then 
  1010.   echo shar: Will not clobber existing file \"'source/name.c'\"
  1011. else
  1012. echo shar: Extracting \"'source/name.c'\" \(4755 characters\)
  1013. sed "s/^X//" >'source/name.c' <<'END_OF_FILE'
  1014. X/*
  1015. X * Copyright (C) Rutherford Appleton Laboratory 1987
  1016. X * 
  1017. X * This source may be copied, distributed, altered or used, but not sold for profit
  1018. X * or incorporated into a product except under licence from the author.
  1019. X * It is not in the public domain.
  1020. X * This notice should remain in the source unaltered, and any changes to the source
  1021. X * made by persons other than the author should be marked as such.
  1022. X * 
  1023. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  1024. X */
  1025. X#include "main.h"
  1026. X
  1027. Xstruct name_struct
  1028. X {
  1029. X    struct name_struct *next_name, *prev_name;
  1030. X     int string_length;
  1031. X     unsigned char *string_body;
  1032. X };
  1033. X
  1034. Xstatic int Cvs (), EqEq (), Eq (), ExecName ();
  1035. Xstatic Object OpExecName;
  1036. X
  1037. XObject Self;
  1038. X
  1039. XInitName ()
  1040. X {
  1041. X     OpExecName = MakeOp ("exec", ExecName,    1, 1, 0, 4, Name);
  1042. X     
  1043. X    TypeInstall (Name, "exec",     OpExecName);
  1044. X    
  1045. X     TypeInstallOp (Name, "==",     EqEq,    1, 0, 0, 0, Name);
  1046. X      TypeInstallOp (Name, "cvs",     Cvs,    2, 1, 0, 0, Name, String);
  1047. X    TypeInstallOp (Name, "eq",     Eq,     2, 1, 0, 0, Name, Name);
  1048. X }
  1049. X
  1050. Xunsigned char *BodyName (item) Object item;
  1051. X {
  1052. X     return item.u.Name->string_body;
  1053. X }
  1054. X
  1055. Xint lengthName (item) Object item;
  1056. X {
  1057. X     return item.u.Name->string_length;
  1058. X }
  1059. X
  1060. Xstatic int Cvs (v, string) Object v, string;
  1061. X {
  1062. X     int length;
  1063. X     
  1064. X    if (lengthString (string) < (length = lengthName (v)))
  1065. X        return Error (PRangeCheck);
  1066. X    VOID Bcopy (BodyString (string), BodyName (v), length);
  1067. X    return Push (OpStack, getIString (string, 0, length));
  1068. X }
  1069. X
  1070. Xstatic int EqEq (v) Object v;
  1071. X {
  1072. X    if (!xCheck (v))
  1073. X        putchar ('/');
  1074. X    PrintName (v);
  1075. X    return TRUE;
  1076. X }
  1077. X
  1078. Xstatic int Eq (a, b) Object a, b;
  1079. X {
  1080. X    return Push (OpStack, MakeBoolean (BodyName (a) == BodyName (b) && lengthName (a) == lengthName (b)));
  1081. X }
  1082. X
  1083. Xstatic int ExecName (item) Object item;
  1084. X {
  1085. X     Object v;
  1086. X     
  1087. X     v = DictLookup (item);
  1088. X    if (TypeOf (v) == Condition)
  1089. X         return Error (PUndefined);
  1090. X    else
  1091. X        return Push (ExecStack, v);
  1092. X }
  1093. X
  1094. Xstatic struct name_struct *name_tree = NULL;
  1095. X
  1096. Xstatic struct name_struct *FindTreeName (s, length, root) unsigned char *s; int length; struct name_struct **root;
  1097. X {
  1098. X    if (*root)
  1099. X     {
  1100. X         int cmp = strncmp (s, (*root)->string_body, min (length, (*root)->string_length));
  1101. X         
  1102. X        if (cmp == 0 && (cmp = length - (*root)->string_length) == 0)
  1103. X            return *root;
  1104. X        else if (cmp < 0)
  1105. X            return FindTreeName (s, length, &(*root)->prev_name);
  1106. X        else
  1107. X            return FindTreeName (s, length, &(*root)->next_name);
  1108. X     }
  1109. X    else
  1110. X     {
  1111. X         struct name_struct *r = *root = (struct name_struct *) Malloc (sizeof (struct name_struct));
  1112. X         
  1113. X        r->next_name = r->prev_name = NULL;
  1114. X        r->string_body = Bcopy (Malloc ((unsigned) length), s, length);
  1115. X        r->string_length = length;
  1116. X        
  1117. X        return *root;
  1118. X     }
  1119. X }
  1120. X
  1121. Xstatic int HashName (s, length) unsigned char *s; int length;
  1122. X {
  1123. X    int i, res = 0;
  1124. X    
  1125. X    while (length--)
  1126. X        res += *s++;
  1127. X    return res;
  1128. X }
  1129. X
  1130. Xint name_tries = 0, name_hits = 0;
  1131. X
  1132. X#define HASH_NAME_SIZE    1024
  1133. X
  1134. Xstatic struct name_struct *hash_name [HASH_NAME_SIZE];
  1135. X
  1136. Xstatic struct name_struct *FindName (s, length) unsigned char *s; int length;
  1137. X {
  1138. X     int hash = HashName (s, length);
  1139. X     struct name_struct *p;
  1140. X     
  1141. X     hash &= (HASH_NAME_SIZE - 1);
  1142. X     
  1143. X     p = hash_name [hash];
  1144. X     ++name_tries;
  1145. X     ++name_hits;
  1146. X     
  1147. X     if (p == NULL || p->string_length != length || strncmp (s, p->string_body, length))
  1148. X      {
  1149. X        p = FindTreeName (s, length, &name_tree);
  1150. X          
  1151. X          if (p != NULL)
  1152. X           {
  1153. X              hash_name [hash] = p;
  1154. X              --name_hits;
  1155. X           }
  1156. X      }
  1157. X     return p;
  1158. X }
  1159. X
  1160. XObject MakeName (s, length) unsigned char *s; int length;
  1161. X {
  1162. X     Object res;
  1163. X     
  1164. X     res = MakeObject (Name);
  1165. X     res.u.Name = FindName (s, length);
  1166. X     
  1167. X     return res;
  1168. X }
  1169. X
  1170. XObject NameFrom (s) unsigned char *s;
  1171. X {
  1172. X     Object res;
  1173. X    
  1174. X     res = MakeObject (Name);
  1175. X     res.u.Name = FindName (s, strlen (s));
  1176. X     
  1177. X     return res;
  1178. X }
  1179. X
  1180. XObject Cvn (o) Object o;
  1181. X {
  1182. X    Object res;
  1183. X    
  1184. X    res = MakeObject (Name);
  1185. X    res.u.Name = FindName (BodyString (o), lengthString (o));
  1186. X    
  1187. X    return res;
  1188. X }
  1189. X
  1190. XObject StringName (o) Object o;
  1191. X {
  1192. X     return MakeString (BodyName (o), lengthName (o));
  1193. X }
  1194. X
  1195. XPrintName (n) Object n;
  1196. X {
  1197. X     printf ("%.*s", lengthName (n), BodyName (n));
  1198. X }
  1199. X
  1200. XObject ParseId (o) Object o;
  1201. X {
  1202. X    unsigned char buf[BUFSIZE], *p = buf;
  1203. X    int c, length = 0, immediate = FALSE;
  1204. X    Object number;
  1205. X    
  1206. X    if ((c = Getch (o)) == '/')
  1207. X        immediate = TRUE;
  1208. X    else
  1209. X        Ungetch (o, c);
  1210. X    
  1211. X    for (;;)
  1212. X     {
  1213. X         switch (c = Getch (o))
  1214. X         {
  1215. X             case EOF: case ' ': case '\t': case '\n':
  1216. X                 break;
  1217. X             
  1218. X             case '/': case '<': case '>': case '(': case ')':
  1219. X            case '%': case '{': case '}': case '[': case ']':
  1220. X                Ungetch (o, c);
  1221. X                break;
  1222. X            
  1223. X            default:
  1224. X                *p++ = c; ++length;
  1225. X                continue;
  1226. X         }
  1227. X        break;
  1228. X     }
  1229. X    if (length == 0)
  1230. X        return Absent;
  1231. X    
  1232. X    number = ParseNumber (buf, length);
  1233. X    if (TypeOf (number) != Null)
  1234. X        return number;
  1235. X    else if (immediate)
  1236. X         return Load (MakeName (buf, length));
  1237. X    else
  1238. X        return MakeName (buf, length);
  1239. X }
  1240. END_OF_FILE
  1241. if test 4755 -ne `wc -c <'source/name.c'`; then
  1242.     echo shar: \"'source/name.c'\" unpacked with wrong size!
  1243. fi
  1244. # end of 'source/name.c'
  1245. fi
  1246. if test -f 'source/poly.c' -a "${1}" != "-c" ; then 
  1247.   echo shar: Will not clobber existing file \"'source/poly.c'\"
  1248. else
  1249. echo shar: Extracting \"'source/poly.c'\" \(4311 characters\)
  1250. sed "s/^X//" >'source/poly.c' <<'END_OF_FILE'
  1251. X/*
  1252. X * Copyright (C) Rutherford Appleton Laboratory 1987
  1253. X * 
  1254. X * This source may be copied, distributed, altered or used, but not sold for profit
  1255. X * or incorporated into a product except under licence from the author.
  1256. X * It is not in the public domain.
  1257. X * This notice should remain in the source unaltered, and any changes to the source
  1258. X * made by persons other than the author should be marked as such.
  1259. X * 
  1260. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  1261. X */
  1262. X#include "main.h"
  1263. X
  1264. Xstatic int Copy ();
  1265. X
  1266. Xint PolyFirst (), PolySecond (), PolyThird (), PolyPair ();
  1267. X
  1268. XInitPoly ()
  1269. X {
  1270. X     Lbracket = Cvx (NameFrom("["));
  1271. X    Rbracket = Cvx (NameFrom("]"));
  1272. X    
  1273. X    InstallOp ("token",        PolyFirst,    1, 1, 0, 0, Poly);
  1274. X    InstallOp ("copy",        Copy,         0, 0, 0, 0);
  1275. X    InstallOp ("length",        PolyFirst,    1, 1, 0, 0, Poly);
  1276. X    InstallOp ("forall",        PolySecond,    2, 2, 0, 0, Poly, Poly);
  1277. X    InstallOp ("get",        PolySecond,    2, 2, 0, 0, Poly, Poly);
  1278. X    InstallOp ("put",        PolyThird,    3, 3, 0, 0, Poly, Poly, Poly);
  1279. X    InstallOp ("getinterval",    PolyThird,    3, 3, 0, 0, Poly, Poly, Poly);
  1280. X    InstallOp ("putinterval",    PolyThird,    3, 3, 0, 0, Poly, Poly, Poly);
  1281. X    InstallOp ("signature",        PolyFirst,    1, 0, 0, 0, Poly);
  1282. X }
  1283. X
  1284. XObject Parse (f) Object f;
  1285. X {
  1286. X     Object res;
  1287. X     int c;
  1288. X     
  1289. X    for (;;)
  1290. X     switch (c = Getch (f))
  1291. X      {
  1292. X         default:  Ungetch (f, c);
  1293. X             res = ParseId (f);
  1294. X             if (TypeOf (res) == Integer || TypeOf (res) == Real)
  1295. X                 return res;
  1296. X             else
  1297. X                 return Cvx (res);
  1298. X         
  1299. X        case '/': return ParseId (f);
  1300. X        case '{': return Cvx (ParseArray (f));
  1301. X        case '}': return True;
  1302. X        case '[': return Lbracket;
  1303. X        case ']': return Rbracket;
  1304. X        case '<': return ParseHexString (f);
  1305. X        case '(': return ParseString (f);
  1306. X        case ')': return Absent;
  1307. X        
  1308. X         case EOF: return False;
  1309. X         
  1310. X         case ' ':
  1311. X         case '\t':
  1312. X         case '\n':
  1313. X             /* nothing */
  1314. X             continue;
  1315. X         
  1316. X         case '%':
  1317. X             while ((c = Getch (f)) != '\n' && c != EOF)
  1318. X                 ;
  1319. X             Ungetch (f, c);
  1320. X             continue;
  1321. X      }
  1322. X }
  1323. X
  1324. X/*
  1325. X * The following are a few of the polymorphic generic routines which actually get called
  1326. X * by user PostScript.
  1327. X * They call type-checked routines on behalf of the types of their arguments.
  1328. X *
  1329. X */
  1330. X
  1331. Xint PolyFirst (arg1) Object arg1;    /* type dictionary choice determined by top of stack */
  1332. X {
  1333. X      if (!Apply (TypeOf (arg1)))
  1334. X         return Error (PTypeCheck);
  1335. X    return Push (OpStack, arg1);
  1336. X }
  1337. X
  1338. Xint PolySecond (arg1, arg2) Object arg1, arg2;    /* type dictionary choice determined by first from top of stack */
  1339. X {
  1340. X     if (!Apply (TypeOf (arg1)))
  1341. X         return Error (PTypeCheck);
  1342. X     return Push (OpStack, arg1), Push (OpStack, arg2);
  1343. X }
  1344. X
  1345. Xint PolyThird (arg1, arg2, arg3) Object arg1, arg2, arg3;    /* type dictionary choice determined by second from top of stack */
  1346. X {
  1347. X     if (!Apply (TypeOf (arg1)))
  1348. X         return Error (PTypeCheck);
  1349. X     return Push (OpStack, arg1), Push (OpStack, arg2), Push (OpStack, arg3);
  1350. X }
  1351. X
  1352. Xint PolyPair (arg1, arg2) Object arg1, arg2;
  1353. X {
  1354. X    if (TypeOf (arg1) == Integer && TypeOf (arg2) == Real)
  1355. X         arg1 = RealInteger (arg1);
  1356. X     else if (TypeOf (arg2) == Integer && TypeOf (arg1) == Real)
  1357. X         arg2 = RealInteger (arg2);
  1358. X     if (TypeOf (arg1) != TypeOf (arg2))
  1359. X        return Error (PTypeCheck);
  1360. X     if (!Apply (TypeOf (arg1)))
  1361. X         return Error (PTypeCheck);
  1362. X    return Push (OpStack, arg1), Push (OpStack, arg2);
  1363. X }
  1364. X
  1365. Xint Apply (type) Type type;
  1366. X {
  1367. X     Object fn;
  1368. X     
  1369. X     fn = Lookup (type, Self);
  1370. X     if (TypeOf (fn) == Condition)
  1371. X         return Error (PTypeCheck);
  1372. X     else
  1373. X         return Push (ExecStack, fn);
  1374. X }
  1375. X
  1376. Xstatic int Copy ()    /* any1 . . . anyn N    --- any1 . . . anyn any1 . . . anyn */
  1377. X        /* other1  other2    --- subother2 */
  1378. X {
  1379. X    Object object1, object2;
  1380. X    int h = Height (OpStack);
  1381. X    
  1382. X    object2 = Pop (OpStack);
  1383. X    if (h == 0)
  1384. X        return Error (POpUnderflow);
  1385. X    else if (TypeOf (object2) == Integer)
  1386. X     {
  1387. X          int n = BodyInteger (object2);
  1388. X          
  1389. X         if (n < 0 || n >= h)
  1390. X            return Error (PRangeCheck);
  1391. X         else if (h - 1 + n > MaxStack (OpStack))
  1392. X            return Error (POpOverflow);
  1393. X         else
  1394. X          {
  1395. X              int i;
  1396. X              
  1397. X              for (i = h - 1 - n; i < h; i++)
  1398. X                  OpStack->stack_body[i+n] = OpStack->stack_body[i];
  1399. X              OpStack->stack_fill += n;
  1400. X              return TRUE;
  1401. X          }
  1402. X      }
  1403. X     object1 = Top (OpStack);
  1404. X     VOID Push (OpStack, object2);
  1405. X     if (TypeOf (object1) != TypeOf (object2))
  1406. X        return Error (PTypeCheck);
  1407. X    else if (!rCheck (object1) || !wCheck (object2))
  1408. X        return Error (PInvAccess);
  1409. X    else
  1410. X         return Apply (TypeOf (object1));
  1411. X }
  1412. END_OF_FILE
  1413. if test 4311 -ne `wc -c <'source/poly.c'`; then
  1414.     echo shar: \"'source/poly.c'\" unpacked with wrong size!
  1415. fi
  1416. # end of 'source/poly.c'
  1417. fi
  1418. if test -f 'source/property.c' -a "${1}" != "-c" ; then 
  1419.   echo shar: Will not clobber existing file \"'source/property.c'\"
  1420. else
  1421. echo shar: Extracting \"'source/property.c'\" \(3240 characters\)
  1422. sed "s/^X//" >'source/property.c' <<'END_OF_FILE'
  1423. X/*
  1424. X * Copyright (C) Rutherford Appleton Laboratory 1987
  1425. X * 
  1426. X * This source may be copied, distributed, altered or used, but not sold for profit
  1427. X * or incorporated into a product except under licence from the author.
  1428. X * It is not in the public domain.
  1429. X * This notice should remain in the source unaltered, and any changes to the source
  1430. X * made by persons other than the author should be marked as such.
  1431. X * 
  1432. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  1433. X */
  1434. X#include "main.h"
  1435. X
  1436. Xstatic int PCvs (), PCvrs (), PCvn (), PType (), PCvlit(), PxCheck ();
  1437. Xint PCvx ();
  1438. X
  1439. XObject type;
  1440. X
  1441. XInitProperty ()
  1442. X {
  1443. X     type = NameFrom ("type");
  1444. X     
  1445. X     InstallOp ("type",        PType,        1, 1, 0, 0, Poly);
  1446. X     InstallOp ("cvlit",        PCvlit,        1, 1, 0, 0, Poly);
  1447. X     InstallOp ("cvn",        PCvn,        1, 1, 0, 0, String);
  1448. X     InstallOp ("cvrs",        PCvrs,        3, 1, 0, 0, Integer, Integer, String);
  1449. X     InstallOp ("cvs",        PCvs,        2, 1, 0, 0, Poly, String);
  1450. X     InstallOp ("cvx",        PCvx,        1, 1, 0, 0, Poly);
  1451. X     InstallOp ("xcheck",        PxCheck,    1, 1, 0, 0, Poly);
  1452. X    InstallOp ("cvi",        PolyFirst,     1, 1, 0, 0, Poly);
  1453. X    InstallOp ("cvr",        PolyFirst,     1, 1, 0, 0, Poly);
  1454. X    InstallOp ("readonly",        PolyFirst,     1, 1, 0, 0, Poly);
  1455. X    InstallOp ("rcheck",        PolyFirst,     1, 1, 0, 0, Poly);
  1456. X    InstallOp ("wcheck",        PolyFirst,     1, 1, 0, 0, Poly);
  1457. X    InstallOp ("executeonly",    PolyFirst,     1, 1, 0, 0, Poly);
  1458. X }
  1459. X
  1460. Xstatic int PType (item) Object item;
  1461. X {
  1462. X     return Push (OpStack, Lookup (TypeOf (item), type));
  1463. X }
  1464. X
  1465. Xstatic int PCvs (item, string) Object item, string;
  1466. X {
  1467. X     int l;
  1468. X     Object t;
  1469. X     
  1470. X     t = Lookup (TypeOf (item), NameFrom ("cvs"));
  1471. X    if (TypeOf (t) == Condition)
  1472. X        if (lengthString (string) < (l = strlen ("--nostringval--")))
  1473. X            return Error (PRangeCheck);
  1474. X        else
  1475. X         {
  1476. X             putIString (string, 0, StringFrom ("--nostringval--"));
  1477. X             return Push (OpStack, getIString (string, 0, l));
  1478. X         }
  1479. X    else
  1480. X     {
  1481. X        VOID Push (OpStack, item);
  1482. X        VOID Push (OpStack, string);
  1483. X         return Apply (TypeOf (item));
  1484. X      }
  1485. X }
  1486. X
  1487. Xstatic int PCvlit (item) Object item;
  1488. X {
  1489. X     return Push (OpStack, Cvlit (item));
  1490. X }
  1491. X
  1492. Xstatic int PCvn (string) Object string;
  1493. X {
  1494. X     return Push (OpStack, SameFlags (string, Cvn (string)));
  1495. X }
  1496. X
  1497. Xstatic int PCvrs (num, base, string) Object num, base, string;
  1498. X {
  1499. X     unsigned n = BodyInteger (num);
  1500. X    unsigned char buf [BUFSIZE], *p = buf, *q = BodyString (string);
  1501. X    int b, length;
  1502. X     
  1503. X     if (!wCheck (string))
  1504. X         return Error (PInvAccess);
  1505. X     else if ((b = BodyInteger (base)) < 2 || b > 36)
  1506. X         return Error (PRangeCheck);
  1507. X     do {
  1508. X         int dig_val = n % b;
  1509. X         
  1510. X         n /= b;
  1511. X         *p++ = dig_val >= 10 ? 'A' + dig_val - 10 : '0' + dig_val;
  1512. X     } while (n != 0);
  1513. X     
  1514. X     if ((length = p - buf) > lengthString (string))
  1515. X         return Error (PRangeCheck);
  1516. X     
  1517. X     while (--p >= buf)
  1518. X         *q++ = *p;
  1519. X     return Push (OpStack, getIString (string, 0, length));
  1520. X }
  1521. X
  1522. X/*ARGSUSED*/
  1523. Xint NoStringVal (v, string) Object v, string;
  1524. X {
  1525. X     char *mess = "--nostringval--";
  1526. X     int length = strlen (mess);
  1527. X     
  1528. X    if (lengthString (string) < length)
  1529. X        return Error (PRangeCheck);
  1530. X    VOID Bcopy (BodyString (string), mess, length);
  1531. X    return Push (OpStack, getIString (string, 0, length));
  1532. X }
  1533. X
  1534. Xint PCvx (item) Object item;
  1535. X {
  1536. X     return Push (OpStack, Cvx (item));
  1537. X }
  1538. X
  1539. Xstatic int PxCheck (item) Object item;
  1540. X {
  1541. X     return Push (OpStack, MakeBoolean (xCheck (item)));
  1542. X }
  1543. END_OF_FILE
  1544. if test 3240 -ne `wc -c <'source/property.c'`; then
  1545.     echo shar: \"'source/property.c'\" unpacked with wrong size!
  1546. fi
  1547. # end of 'source/property.c'
  1548. fi
  1549. if test -f 'source/protocol.c' -a "${1}" != "-c" ; then 
  1550.   echo shar: Will not clobber existing file \"'source/protocol.c'\"
  1551. else
  1552. echo shar: Extracting \"'source/protocol.c'\" \(3325 characters\)
  1553. sed "s/^X//" >'source/protocol.c' <<'END_OF_FILE'
  1554. X/*
  1555. X * Copyright (C) Rutherford Appleton Laboratory 1987
  1556. X * 
  1557. X * This source may be copied, distributed, altered or used, but not sold for profit
  1558. X * or incorporated into a product except under licence from the author.
  1559. X * It is not in the public domain.
  1560. X * This notice should remain in the source unaltered, and any changes to the source
  1561. X * made by persons other than the author should be marked as such.
  1562. X * 
  1563. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  1564. X */
  1565. X#include <stdio.h>
  1566. X
  1567. X#include "main.h"
  1568. X#include "graphics.h"
  1569. X#include "protocol.h"
  1570. X
  1571. X#define SCALE 16384
  1572. X
  1573. Xstatic FILE *devfpi, *devfpo;
  1574. Xchar *getenv (), *strcpy (), *strcat ();
  1575. Xunsigned char getb ();
  1576. X
  1577. Xvoid master_protocol ()
  1578. X {
  1579. X     char name [BUFSIZ], *e = getenv ("POSTSCRIPTDEVICE");
  1580. X     
  1581. X     if (e == NULL || *e == '\0')
  1582. X         VOID strcpy (name, "|viewer");
  1583. X     else
  1584. X         VOID strcpy (name, e);
  1585. X     if (*name == '|')
  1586. X      {
  1587. X          int ip[2], op[2];
  1588. X          
  1589. X          pipe (ip);
  1590. X          pipe (op);
  1591. X          devfpi = fdopen (ip[0], "r");
  1592. X          devfpo = fdopen (op[1], "w");
  1593. X          
  1594. X          if (fork () == 0)
  1595. X           {
  1596. X               dup2 (op[0], 0);
  1597. X               dup2 (ip[1], 1);
  1598. X               close (op[1]);
  1599. X               close (ip[0]);
  1600. X               
  1601. X               execl ("/bin/sh", "PSDEV", "-c", name + 1, NULL);
  1602. X               fprintf (stderr, "NO SHELL!\n");
  1603. X               exit (1);
  1604. X           }
  1605. X          else
  1606. X           {
  1607. X               close (op[0]);
  1608. X               close (ip[1]);
  1609. X           }
  1610. X      }
  1611. X     else if (*name == '%')
  1612. X      {
  1613. X          devfpi = fopen (name + 1, "r");
  1614. X          devfpo = fopen (name + 1, "w");
  1615. X      }
  1616. X     else
  1617. X      {
  1618. X          devfpi = NULL;
  1619. X          devfpo = fopen (name, "w");
  1620. X      }
  1621. X }
  1622. X
  1623. Xvoid slave_protocol ()
  1624. X {
  1625. X    devfpi = stdin;
  1626. X    devfpo = stdout;
  1627. X }
  1628. X
  1629. Xvoid send_colour (colour) Colour colour;
  1630. X {
  1631. X     send_small (colour.hue);
  1632. X     send_small (colour.saturation);
  1633. X     send_small (colour.brightness);
  1634. X }
  1635. X
  1636. XColour recv_colour ()
  1637. X {
  1638. X     Colour res;
  1639. X     
  1640. X     res.hue = recv_small ();
  1641. X     res.saturation = recv_small ();
  1642. X     res.brightness = recv_small ();
  1643. X     
  1644. X     return res;
  1645. X }
  1646. X
  1647. Xvoid send_small (f) float f;
  1648. X {
  1649. X     send_short ((int) (f * SCALE));
  1650. X }
  1651. X
  1652. Xfloat recv_small ()
  1653. X {
  1654. X    return (float) recv_short () / SCALE;
  1655. X }
  1656. X
  1657. Xvoid send_point (p) DevicePoint p;
  1658. X {
  1659. X    send_short (p.dx);
  1660. X    send_short (p.dy);
  1661. X }
  1662. X
  1663. XDevicePoint recv_point ()
  1664. X {
  1665. X    short r = recv_short ();
  1666. X    
  1667. X    return NewDevicePoint (r, recv_short ());
  1668. X }
  1669. X
  1670. Xvoid send_byte (b) unsigned char b;
  1671. X {
  1672. X    putc (b, devfpo);
  1673. X }
  1674. X
  1675. Xunsigned char recv_byte ()
  1676. X {
  1677. X    return getb (devfpi);
  1678. X }
  1679. X
  1680. Xvoid send_short (i) short i;
  1681. X {
  1682. X     send_byte (i & 0xff);
  1683. X     send_byte (i >> 8);
  1684. X }
  1685. X
  1686. Xshort recv_short ()
  1687. X {
  1688. X    short i;
  1689. X    
  1690. X    i = recv_byte () & 0xff;
  1691. X    i |= recv_byte () << 8;
  1692. X    
  1693. X    return i;
  1694. X }
  1695. X
  1696. Xvoid send_float (f) float f;
  1697. X {
  1698. X    fprintf (devfpo, "%g\n", f);
  1699. X }
  1700. X
  1701. Xfloat recv_float ()
  1702. X {
  1703. X     float f;
  1704. X     char buf [BUFSIZ], *p = buf;
  1705. X     
  1706. X     while ((*p++ = getb (devfpi)) != '\n')
  1707. X         ;
  1708. X     *p++ = '\0';
  1709. X     sscanf (buf, "%f\n", &f);
  1710. X     
  1711. X     return f;
  1712. X }
  1713. X
  1714. Xvoid send_string (s, len) char *s; int len;
  1715. X {
  1716. X     PanicIf (len != fwrite (s, 1, len, devfpo), "could not send bitmap from driver");
  1717. X }
  1718. X
  1719. Xvoid recv_string (s, len) char *s; int len;
  1720. X {
  1721. X     while (len--)
  1722. X         *s++ = getb (devfpi);
  1723. X }
  1724. X
  1725. Xvoid flush_protocol ()
  1726. X {
  1727. X     fflush (devfpo);
  1728. X }
  1729. X
  1730. Xint can_recv ()
  1731. X {
  1732. X     return devfpi != NULL;
  1733. X }
  1734. Xchar buffer [BUFSIZ], *p;
  1735. Xint remaining = 0;
  1736. X
  1737. Xunsigned char getb (fp) FILE *fp;
  1738. X {
  1739. X     if (remaining > 0)
  1740. X      {
  1741. X          --remaining;
  1742. X          return *p++;
  1743. X      }
  1744. X     if ((remaining = read (fp->_file, buffer, BUFSIZ)) <= 0)
  1745. X          exit (1);
  1746. X     p = buffer;
  1747. X     return getb (fp);
  1748. X }
  1749. END_OF_FILE
  1750. if test 3325 -ne `wc -c <'source/protocol.c'`; then
  1751.     echo shar: \"'source/protocol.c'\" unpacked with wrong size!
  1752. fi
  1753. # end of 'source/protocol.c'
  1754. fi
  1755. if test -f 'source/screen.c' -a "${1}" != "-c" ; then 
  1756.   echo shar: Will not clobber existing file \"'source/screen.c'\"
  1757. else
  1758. echo shar: Extracting \"'source/screen.c'\" \(3510 characters\)
  1759. sed "s/^X//" >'source/screen.c' <<'END_OF_FILE'
  1760. X/*
  1761. X * Copyright (C) Rutherford Appleton Laboratory 1987
  1762. X * 
  1763. X * This source may be copied, distributed, altered or used, but not sold for profit
  1764. X * or incorporated into a product except under licence from the author.
  1765. X * It is not in the public domain.
  1766. X * This notice should remain in the source unaltered, and any changes to the source
  1767. X * made by persons other than the author should be marked as such.
  1768. X * 
  1769. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  1770. X */
  1771. X#include "main.h"
  1772. X#include "graphics.h"
  1773. X#include "canon.h"
  1774. X
  1775. X/********************************** SET TRANSFER STUFF **********************************/
  1776. X
  1777. X#define TRANSFER_SIZE    256
  1778. X
  1779. Xstatic int transfer [TRANSFER_SIZE];
  1780. X
  1781. Xvoid InitTransfer (ppi) int ppi;
  1782. X {
  1783. X     int i;
  1784. X     
  1785. X     pixels_per_inch = ppi;
  1786. X     for (i = 0; i < TRANSFER_SIZE; i++)
  1787. X         transfer [i] = i;
  1788. X }
  1789. X
  1790. Xint HardColour (colour) Colour colour;
  1791. X {
  1792. X    return transfer [(int) ((TRANSFER_SIZE - 1) * colour.brightness + .5)];
  1793. X }
  1794. X
  1795. Xint TransferSize ()
  1796. X {
  1797. X    return TRANSFER_SIZE;
  1798. X }
  1799. X
  1800. Xvoid SetTransfer (tran) float *tran;
  1801. X {
  1802. X     int i;
  1803. X     
  1804. X     for (i = 0; i < TRANSFER_SIZE; i++)
  1805. X         transfer [i] = (TRANSFER_SIZE - 1) * tran[i] + .5;
  1806. X }
  1807. X
  1808. X/********************************** SET SCREEN STUFF *******************************/
  1809. X
  1810. Xstruct screen
  1811. X {
  1812. X     float val;
  1813. X     int sx, sy;
  1814. X     struct hardware *shade;
  1815. X } *screen = NULL;
  1816. X
  1817. Xstatic int screen_size, screen_side;
  1818. X
  1819. Xstatic int FreqSize (freq) float freq;
  1820. X {
  1821. X     int i = pixels_per_inch / freq + 0.5;
  1822. X     
  1823. X     if (i < 2)
  1824. X        return 2;
  1825. X    return i;
  1826. X }
  1827. X
  1828. Xstruct hardware *GraySync (col) int col;
  1829. X {
  1830. X     col = col * (float) screen_size / TRANSFER_SIZE + 0.5;
  1831. X     
  1832. X     return screen[col].shade;
  1833. X }
  1834. X
  1835. Xint ScreenSize (freq, rot) float freq, rot;
  1836. X {
  1837. X     int size = FreqSize (freq);
  1838. X     
  1839. X     return size * size;
  1840. X }
  1841. X
  1842. Xvoid BuildScreen (freq, rotation, x, y) float freq, rotation, *x, *y;
  1843. X {
  1844. X     int size = FreqSize (freq);
  1845. X     int i, j;
  1846. X     
  1847. X     for (i = 0; i < size; i++)
  1848. X         for (j = 0; j < size; j++)
  1849. X             *x++ = (2 * i - size + 1) / (float) size,
  1850. X             *y++ = (2 * j - size + 1) / (float) size;
  1851. X }
  1852. X
  1853. Xstatic sgn (a) float a;
  1854. X {
  1855. X    if (a == 0)
  1856. X        return 0;
  1857. X    else if (a < 0)
  1858. X        return -1;
  1859. X    else
  1860. X        return 1;
  1861. X }
  1862. X
  1863. Xstatic int screen_cmp (a, b) char *a, *b;
  1864. X {
  1865. X     struct screen *aa = (struct screen *) a, *bb = (struct screen *) b;
  1866. X     
  1867. X     return sgn (aa->val - bb->val);
  1868. X }
  1869. X
  1870. Xvoid SetScreen (freq, rotation, thresh) float freq, rotation, *thresh;
  1871. X {
  1872. X     struct hardware *temp;
  1873. X     int i, j, size = FreqSize (freq);
  1874. X     struct screen *p;
  1875. X     
  1876. X     if (screen)
  1877. X      {
  1878. X          for (i = 0; i < screen_size; i++)
  1879. X              DestroyHardware (screen [i].shade);
  1880. X          free ((char *) screen);
  1881. X      }
  1882. X     p = screen = (struct screen *) Malloc ((unsigned) (((screen_size = size * size) + 1) * sizeof (struct screen)));
  1883. X     screen_side = size;
  1884. X     for (i = 0; i < size; i++)
  1885. X         for (j = 0; j < size; j++)
  1886. X          {
  1887. X              p->val = *thresh++;
  1888. X              p->sx = i;
  1889. X              p->sy = j;
  1890. X              ++p;
  1891. X          }
  1892. X     qsort ((char *) screen, screen_size, sizeof (struct screen), screen_cmp);
  1893. X     temp = NewBitmapHardware (size, size);
  1894. X     BitBlt ((struct hardware *) NULL, temp, NewDevicePoint (0, 0), NewDevicePoint (0, 0), NewDevicePoint (size, size), ROP_TRUE);
  1895. X     
  1896. X     for (i = 0; i < screen_size; i++)
  1897. X      {
  1898. X          screen [i].shade = NewBitmapHardware (size, size);
  1899. X          BitBlt (temp, screen[i].shade,
  1900. X                  NewDevicePoint (0, 0), NewDevicePoint (0, 0),
  1901. X                  NewDevicePoint (size, size), ROP_SOURCE);
  1902. X          BitBlt ((struct hardware *) NULL, temp,
  1903. X                  NewDevicePoint (0, 0), NewDevicePoint (screen[i].sx, screen[i].sy),
  1904. X                  NewDevicePoint (1, 1), ROP_FALSE);
  1905. X      }
  1906. X     screen[screen_size].shade = temp;
  1907. X }
  1908. END_OF_FILE
  1909. if test 3510 -ne `wc -c <'source/screen.c'`; then
  1910.     echo shar: \"'source/screen.c'\" unpacked with wrong size!
  1911. fi
  1912. # end of 'source/screen.c'
  1913. fi
  1914. if test -f 'source/unix.c' -a "${1}" != "-c" ; then 
  1915.   echo shar: Will not clobber existing file \"'source/unix.c'\"
  1916. else
  1917. echo shar: Extracting \"'source/unix.c'\" \(3133 characters\)
  1918. sed "s/^X//" >'source/unix.c' <<'END_OF_FILE'
  1919. X/*
  1920. X * Copyright (C) Rutherford Appleton Laboratory 1987
  1921. X * 
  1922. X * This source may be copied, distributed, altered or used, but not sold for profit
  1923. X * or incorporated into a product except under licence from the author.
  1924. X * It is not in the public domain.
  1925. X * This notice should remain in the source unaltered, and any changes to the source
  1926. X * made by persons other than the author should be marked as such.
  1927. X * 
  1928. X *    Crispin Goswell @ Rutherford Appleton Laboratory caag@uk.ac.rl.vd
  1929. X */
  1930. X#include "main.h"
  1931. X#include <signal.h>
  1932. X
  1933. Xstatic int Chdir (), Fork (), Execv (), Wait (), Exit (), System (), Signal (), Kill ();
  1934. X
  1935. XInitUnix ()
  1936. X {
  1937. X     InstallOp ("chdir",    Chdir,    1, 1, 0, 0, String);
  1938. X     InstallOp ("fork",    Fork,    0, 1, 0, 0);
  1939. X      InstallOp ("wait",    Wait,    0, 5, 0, 0);
  1940. X      InstallOp ("uexit",    Exit,    1, 0, 0, 0, Integer);
  1941. X      InstallOp ("execv",    Execv,    2, 0, 0, 0, Array, String);
  1942. X      InstallOp ("system",    System,    1, 1, 0, 0, String);
  1943. X      InstallOp ("signal",     Signal, 2, 1, 0, 0, Integer, Integer);
  1944. X      InstallOp ("kill",    Kill,    2, 1, 0, 0, Integer, Integer);
  1945. X}
  1946. X
  1947. Xstatic int Chdir (dir) Object dir;
  1948. X {
  1949. X     char buffer [BUFSIZE];
  1950. X     int l = lengthString (dir);
  1951. X     
  1952. X     VOID strncpy (buffer, BodyString (dir), l);
  1953. X     buffer [l] = '\0';
  1954. X     
  1955. X     return Push (OpStack, MakeBoolean (!chdir (buffer)));
  1956. X }
  1957. X
  1958. Xstatic int Fork ()
  1959. X {
  1960. X     return Push (OpStack, MakeInteger (fork ()));
  1961. X }
  1962. X
  1963. Xstatic int Exit (status) Object status;
  1964. X {
  1965. X     exit (BodyInteger (status));
  1966. X     
  1967. X     return TRUE; /* shuts lint up */
  1968. X }
  1969. X
  1970. Xstatic int Wait ()
  1971. X {
  1972. X     int pid, status;
  1973. X     
  1974. X     if ((pid = wait (&status)) < 0)
  1975. X         return Push (OpStack, False);
  1976. X     return Push (OpStack, MakeBoolean (status & 0200))
  1977. X         && Push (OpStack, MakeInteger (status & 0177))
  1978. X         && Push (OpStack, MakeInteger (status >> 8))
  1979. X         && Push (OpStack, MakeInteger (pid))
  1980. X         && Push (OpStack, True);
  1981. X }
  1982. X
  1983. Xstatic int Execv (args, name) Object args, name;
  1984. X {
  1985. X     int i, nl, l = lengthArray (args);
  1986. X     char **av = (char **) Malloc ((unsigned) (l+1) * sizeof (char *));
  1987. X     char buffer [BUFSIZE];
  1988. X     
  1989. X     for (i = 0; i < l; i++)
  1990. X      {
  1991. X          Object elem;
  1992. X          
  1993. X          elem = getArray (args, i);
  1994. X         if (TypeOf (elem) != String)
  1995. X             return Error (PTypeCheck);
  1996. X         else
  1997. X          {
  1998. X              int l = lengthString (elem);
  1999. X              
  2000. X             av[i] = Malloc ((unsigned) l + 1);
  2001. X             VOID strncpy (av[i], BodyString (elem), l);
  2002. X             av[i][l] = '\0';
  2003. X          }
  2004. X      }
  2005. X     av[l] = NULL;
  2006. X     
  2007. X     nl = lengthString (name);
  2008. X     VOID strncpy (buffer, BodyString (name), nl);
  2009. X     buffer [nl] = '\0';
  2010. X     
  2011. X     if (execv (buffer, av) == -1)
  2012. X      {
  2013. X          for (i = 0; i < l; i++)
  2014. X              Free (av[i]);
  2015. X          Free ((char *) av);
  2016. X          
  2017. X          return Error (PInvFileAccess);
  2018. X      }
  2019. X     return TRUE;
  2020. X }
  2021. X
  2022. Xstatic int System (s) Object s;
  2023. X {
  2024. X     char buffer [BUFSIZE];
  2025. X     int l = lengthString (s);
  2026. X     
  2027. X     VOID strncpy (buffer, BodyString (s), l);
  2028. X     buffer [l] = '\0';
  2029. X     return Push (OpStack, MakeInteger (system (buffer)));
  2030. X }
  2031. X
  2032. Xstatic int Signal (n, s) Object n, s;
  2033. X {
  2034. X    int sn = BodyInteger (n);
  2035. X    
  2036. X     if (sn < 1 || sn > NSIG)
  2037. X         return Error (PRangeCheck);
  2038. X    return Push (OpStack, MakeInteger (signal (sn, BodyInteger (s))));
  2039. X }
  2040. X
  2041. Xstatic int Kill (n, s) Object n, s;
  2042. X {
  2043. X     return Push (OpStack, MakeBoolean (0 == kill (BodyInteger (n), BodyInteger (s))));
  2044. X }
  2045. END_OF_FILE
  2046. if test 3133 -ne `wc -c <'source/unix.c'`; then
  2047.     echo shar: \"'source/unix.c'\" unpacked with wrong size!
  2048. fi
  2049. # end of 'source/unix.c'
  2050. fi
  2051. echo shar: End of archive 2 \(of 18\).
  2052. cp /dev/null ark2isdone
  2053. MISSING=""
  2054. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ; do
  2055.     if test ! -f ark${I}isdone ; then
  2056.     MISSING="${MISSING} ${I}"
  2057.     fi
  2058. done
  2059. if test "${MISSING}" = "" ; then
  2060.     echo You have unpacked all 18 archives.
  2061.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2062. else
  2063.     echo You still need to unpack the following archives:
  2064.     echo "        " ${MISSING}
  2065. fi
  2066. ##  End of shell archive.
  2067. exit 0
  2068.