home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / print / post17s.lzh / post.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-13  |  74.8 KB  |  2,462 lines

  1. /* PostScript interpreter file "post.c" - the main program (Amiga)
  2.  * (C) Adrian Aylward 1989, 1992
  3.  *
  4.  * This file contains the code to parse the command line arguments, the
  5.  * WorkBench and intuition interface, and the printer screen or IFF file]
  6.  * output routines.  It is therfore heavily Amiga dependent.  It calls the
  7.  * interpreter library for all the PostScript stuff.
  8.  *
  9.  * There are also various Lattice dependencies: the startup code in _main,
  10.  * the menu task, the flush and copy functions, and the floating point trap
  11.  * and break handling.
  12.  *
  13.  * V1.6 First full source release
  14.  * V1.7 Treat "*" as interactive
  15.  */
  16.  
  17. # include <arpbase.h>
  18. # include <dos.h>
  19. # include <devices/printer.h>
  20. # include <devices/prtbase.h>
  21. # include <exec/exec.h>
  22. # include <exec/execbase.h>
  23. # include <exec/tasks.h>
  24. # include <graphics/gfxbase.h>
  25. # include <graphics/text.h>
  26. # include <intuition/intuition.h>
  27. # include <libraries/dosextens.h>
  28. # include <workbench/icon.h>
  29. # include <workbench/startup.h>
  30. # include <workbench/workbench.h>
  31. # include <proto/diskfont.h>
  32. # include <proto/dos.h>
  33. # include <proto/exec.h>
  34. # include <proto/graphics.h>
  35. # include <proto/icon.h>
  36. # include <proto/intuition.h>
  37. # include <stdio.h>
  38. # include <string.h>
  39.  
  40. # include "postlib.h"
  41.  
  42. # undef  POSTVERNO
  43. # define POSTVERNO 15  /* We need post.library version 1.5+ */
  44.  
  45. /* Assembler routines */
  46.  
  47. extern void insertbreak(void);
  48. extern void deletebreak(void);
  49. extern void insertftrap(void);
  50. extern void deleteftrap(void);
  51.  
  52. /* Routines defined and referenced only within this module */
  53.  
  54. extern void errmsg(char *string);
  55. extern void tidyup(void);
  56. extern void setprinter(void);
  57. extern void setprintden(void);
  58. extern void setreqgadgets(void);
  59. extern void getreqgadgets(void);
  60. extern void enablegadg(struct Gadget *gadget, int enable);
  61. extern void setgadgbool(struct Gadget *gadget, int value);
  62. extern int  getgadgbool(struct Gadget *gadget);
  63. extern void setgadgint(struct Gadget *gadget, int value);
  64. extern int  getgadgint(struct Gadget *gadget);
  65. extern int  getgadgabs(struct Gadget *gadget);
  66. extern int  strtoint(char **sp, int *ip);
  67. extern void sendmenu(int action);
  68. extern void __saveds menuproc(void);
  69. # ifndef STATICLINK
  70. extern void __saveds __asm flushpage(register __d0 int y1,
  71.                                      register __d1 int y2);
  72. extern void __saveds __asm copypage(register __d0 int num);
  73. # endif
  74. extern void printpage(void);
  75. extern void iffpage(void);
  76. extern int  igcd(int n, int m);
  77.  
  78. /* Message structure */
  79.  
  80. struct PSmessage
  81. {   struct Message ExecMessage;
  82.     long class;            /* Always zero */
  83.     short action;          /* Action - from Post to handler */
  84.     short command;         /* Command - from handler to Post */
  85.     struct BitMap *bitmap; /* The bitmap */
  86.     short y1, y2;          /* Min and max+1 y values to flush */
  87.     short result;          /* Result  (return code) */
  88.     short length;          /* Length of string */
  89.     char *string;          /* String */
  90.     long window;           /* Always zero */
  91.     short errnum;          /* Last error number */
  92.     short zero;            /* Reserved, presently always zero */
  93. };
  94.  
  95. /* Actions */
  96.  
  97. # define PSACTOPEN    1 /* Open */
  98. # define PSACTCLOSE   2 /* Close */
  99. # define PSACTFLUSH   3 /* Flush out the bitmap */
  100. # define PSACTPAUSE   4 /* Pause at the end of a page */
  101. # define PSACTCOMMAND 5 /* Get a command */
  102. # define PSACTEXIT    6 /* Exit */
  103.  
  104. /* Commands */
  105.  
  106. # define PSCOMQUIT    1 /* Quit */
  107. # define PSCOMRESTART 2 /* Restart */
  108. # define PSCOMFILEF   3 /* Load Font */
  109. # define PSCOMFILEL   4 /* Load File */
  110. # define PSCOMFILER   5 /* Run File */
  111. # define PSCOMINTER   6 /* Interactive */
  112.  
  113. /* Lattice startup */
  114.  
  115. extern struct ExecBase *SysBase;
  116. extern struct WBStartup *WBenchMsg;
  117.  
  118. /* External data (initialised to zero) */
  119.  
  120. int retcode, errcode, ioerror;
  121. int fatalerror, restarterror, requested;
  122.  
  123. int arec;
  124.  
  125. BPTR errfh, confh;
  126.  
  127. # ifndef STATICLINK
  128. struct library *PSbase;
  129. # endif
  130. struct PSparm parm;
  131. struct PSmessage menumsg;
  132.  
  133. struct ArpBase *ArpBase;
  134. struct FileRequester *filereq, *filereq1, *filereq2;
  135.  
  136. struct Process *process;
  137. struct MsgPort *mainport, *menuport;
  138. struct Task *menutask;
  139. struct Screen *screen;
  140. struct Window *syswindow, *reqwindow, *errwindow, *bitwindow, *intwindow;
  141. struct BitMap bitmap;
  142. struct ColorMap colormap;
  143. struct TextFont *textfont;
  144.  
  145. int propen, prden;
  146. struct IODRPReq prreq;
  147. struct PrinterData *prdata;
  148. struct PrinterExtendedData *prextdata;
  149. struct Preferences *prprefs;
  150. struct RastPort prrast;
  151. struct MsgPort *prport;
  152. ULONG prsig;
  153. UBYTE prstatus[2];
  154.  
  155. int breakset, ftrapset;
  156.  
  157. int winxbase, winybase, winxsize, winysize, winxpos, winypos;
  158.  
  159. /* Colour tables.  The default color map type is a vector or UWORD RGB
  160.  * values.  The colours are inverted as we set a bit in the bitmap whenever
  161.  * we want to place a drop of (black or cyan/magenta/yellow) ink */
  162.  
  163. static UWORD bcolors[2] =  /* Black and white */
  164. {   0xfff, 0x000                /* White   black */
  165. };
  166.  
  167. static UWORD ccolors[16] = /* Colour (RGB or CMYK) */
  168. {   0xfff, 0x0ff, 0xf0f, 0x00f, /* White   cyan    magenta blue */
  169.     0xff0, 0x0f0, 0xf00, 0x000, /* Yellow  green   red     black */
  170.     0x000, 0x000, 0x000, 0x000, /* Black */
  171.     0x000, 0x000, 0x000, 0x000  /* Black */
  172. };
  173.  
  174. char titlewait[]    = POSTVER " Copyright Adrian Aylward 1989, 1992";
  175. char titlestart[]   = POSTVER " Running startup file(s)";
  176. char titlerunning[] = POSTVER " Running";
  177. char titleinter[]   = POSTVER " Interactive";
  178. char titlepaused[]  = POSTVER " Paused";
  179.  
  180. char hailfilef[]    = POSTVER " Select font to load";
  181. char hailfilen[]    = POSTVER " Select file to load";
  182. char hailfiles[]    = POSTVER " Select file to run";
  183.  
  184. /* The default font and screen */
  185.  
  186. struct TextAttr topaz11 =       /* Font is Topaz 11 */
  187. {   "topaz.font", 11, FS_NORMAL, FPF_DISKFONT };
  188.  
  189. struct NewScreen newscreen =    /* Screen */
  190. {   0, 0, 640, 512, 1, 0, 15, LACE|HIRES, CUSTOMSCREEN, &topaz11,
  191.     (UBYTE *) &titlewait, NULL, NULL
  192. };
  193.  
  194. /* Stuff for the options requestor */
  195.  
  196. # define GADGOK    1  /* Gadget id's */
  197. # define GADGCAN   2
  198. # define GADGPRI   3
  199. # define GADGBW    4
  200. # define GADGC3    5
  201. # define GADGC4    6
  202. # define GADGPD1   7
  203. # define GADGPD2   8
  204. # define GADGPD3   9
  205. # define GADGPD4  10
  206. # define GADGPD5  11
  207. # define GADGPD6  12
  208. # define GADGPD7  13
  209. # define GADGABS  14  /* Integer gadget, absolute value */
  210. # define GADGRST  15  /* Dummy gadget to restart after error */
  211.  
  212. char undobuff[80];
  213.  
  214. SHORT bordervec1[] =
  215. {   0, 0, 67, 0, 67, 14, 0, 14, 0, 1 };
  216. struct Border border1 =
  217. {   -2, -2, 15, 0, JAM1, 5, bordervec1, NULL };
  218.  
  219. UBYTE mlgadgbuff[8];
  220. struct StringInfo mlgadginfo =
  221. {   mlgadgbuff, (UBYTE *)undobuff, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  222. struct Gadget mlgadg =
  223. {   NULL,    500, 212, 64, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  224.     (APTR)&border1, NULL, NULL, 0, (APTR)&mlgadginfo, GADGABS, NULL };
  225.  
  226. UBYTE mvgadgbuff[8];
  227. struct StringInfo mvgadginfo =
  228. {   mvgadgbuff, (UBYTE *)undobuff, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  229. struct Gadget mvgadg =
  230. {   &mlgadg, 500, 198, 64, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  231.     (APTR)&border1, NULL, NULL, 0, (APTR)&mvgadginfo, GADGABS, NULL };
  232.  
  233. UBYTE mhgadgbuff[8];
  234. struct StringInfo mhgadginfo =
  235. {   mhgadgbuff, (UBYTE *)undobuff, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  236. struct Gadget mhgadg =
  237. {   &mvgadg, 500, 184, 64, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  238.     (APTR)&border1, NULL, NULL, 0, (APTR)&mhgadginfo, GADGABS, NULL };
  239.  
  240. UBYTE mfgadgbuff[8];
  241. struct StringInfo mfgadginfo =
  242. {   mfgadgbuff, (UBYTE *)undobuff, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  243. struct Gadget mfgadg =
  244. {   &mhgadg, 500, 170, 64, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  245.     (APTR)&border1, NULL, NULL, 0, (APTR)&mfgadginfo, GADGABS, NULL };
  246.  
  247. SHORT bordervec2[] =
  248. {   0, 0, 43, 0, 43, 14, 0, 14, 0, 1 };
  249. struct Border border2 =
  250. {   -2, -2, 15, 0, JAM1, 5, bordervec2, NULL };
  251.  
  252.  
  253. UBYTE ydgadgbuff[5];
  254. struct StringInfo ydgadginfo =
  255. {   ydgadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  256. struct Gadget ydgadg =
  257. {   &mfgadg, 388, 198, 40, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  258.     (APTR)&border2, NULL, NULL, 0, (APTR)&ydgadginfo, GADGABS, NULL };
  259.  
  260. UBYTE xdgadgbuff[5];
  261. struct StringInfo xdgadginfo =
  262. {   xdgadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  263. struct Gadget xdgadg =
  264. {   &ydgadg, 345, 198, 40, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  265.     (APTR)&border2, NULL, NULL, 0, (APTR)&xdgadginfo, GADGABS, NULL };
  266.  
  267. UBYTE yogadgbuff[5];
  268. struct StringInfo yogadginfo =
  269. {   yogadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  270. struct Gadget yogadg =
  271. {   &xdgadg, 388, 184, 40, 13, 0, STRINGRIGHT|LONGINT, STRGADGET,
  272.     (APTR)&border2, NULL, NULL, 0, (APTR)&yogadginfo, 0, NULL };
  273.  
  274. UBYTE xogadgbuff[5];
  275. struct StringInfo xogadginfo =
  276. {   xogadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  277. struct Gadget xogadg =
  278. {   &yogadg, 345, 184, 40, 13, 0, STRINGRIGHT|LONGINT, STRGADGET,
  279.     (APTR)&border2, NULL, NULL, 0, (APTR)&xogadginfo, 0, NULL };
  280.  
  281. UBYTE ygadgbuff[5];
  282. struct StringInfo ygadginfo =
  283. {   ygadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  284. struct Gadget ygadg =
  285. {   &xogadg, 388, 170, 40, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  286.     (APTR)&border2, NULL, NULL, 0, (APTR)&ygadginfo, GADGABS, NULL };
  287.  
  288. UBYTE xgadgbuff[5];
  289. struct StringInfo xgadginfo =
  290. {   xgadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  291. struct Gadget xgadg =
  292. {   &ygadg, 345, 170, 40, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  293.     (APTR)&border2, NULL, NULL, 0, (APTR)&xgadginfo, GADGABS, NULL };
  294.  
  295. short __chip idata1[] =
  296. {   0xfffe, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002,
  297.     0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0xfffe,
  298.     0xfffe, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002,
  299.     0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0xfffe,
  300.     0xfffe, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002,
  301.     0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0xfffe,
  302.     0xfffe, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002,
  303.     0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0xfffe };
  304. struct Image image1 =
  305. {   -2, -2, 15, 15, 4, &idata1[0], 15, 0, NULL };
  306.  
  307. struct IntuiText itext30 =
  308. {   15, 0, JAM2, 1, 1, NULL, "7", NULL };
  309. struct IntuiText itext29 =
  310. {   15, 0, JAM2, 1, 1, NULL, "6", NULL };
  311. struct IntuiText itext28 =
  312. {   15, 0, JAM2, 1, 1, NULL, "5", NULL };
  313. struct IntuiText itext27 =
  314. {   15, 0, JAM2, 1, 1, NULL, "4", NULL };
  315. struct IntuiText itext26 =
  316. {   15, 0, JAM2, 1, 1, NULL, "3", NULL };
  317. struct IntuiText itext25 =
  318. {   15, 0, JAM2, 1, 1, NULL, "2", NULL };
  319. struct IntuiText itext24 =
  320. {   15, 0, JAM2, 1, 1, NULL, "1", NULL };
  321.  
  322. struct Gadget pd7gadg =
  323. {   &xgadg,   526, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  324.     (APTR)&image1, NULL, &itext30, 0, NULL, GADGPD7, NULL };
  325. struct Gadget pd6gadg =
  326. {   &pd7gadg, 512, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  327.     (APTR)&image1, NULL, &itext29, 0, NULL, GADGPD6, NULL };
  328. struct Gadget pd5gadg =
  329. {   &pd6gadg, 498, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  330.     (APTR)&image1, NULL, &itext28, 0, NULL, GADGPD5, NULL };
  331. struct Gadget pd4gadg =
  332. {   &pd5gadg, 484, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  333.     (APTR)&image1, NULL, &itext27, 0, NULL, GADGPD4, NULL };
  334. struct Gadget pd3gadg =
  335. {   &pd4gadg, 470, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  336.     (APTR)&image1, NULL, &itext26, 0, NULL, GADGPD3, NULL };
  337. struct Gadget pd2gadg =
  338. {   &pd3gadg, 456, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  339.     (APTR)&image1, NULL, &itext25, 0, NULL, GADGPD2, NULL };
  340. struct Gadget pd1gadg =
  341. {   &pd2gadg, 442, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  342.     (APTR)&image1, NULL, &itext24, 0, NULL, GADGPD1, NULL };
  343.  
  344. struct Gadget cwbgadg =
  345. {   &pd1gadg, 417, 226, 11, 11, GADGIMAGE, TOGGLESELECT, BOOLGADGET,
  346.     (APTR)&image1, NULL, NULL, 0, NULL, 0, NULL };
  347.  
  348. struct Gadget c4gadg =
  349. {   &cwbgadg, 553, 86, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  350.     (APTR)&image1, NULL, NULL, 0, NULL, GADGC4, NULL };
  351. struct Gadget c3gadg =
  352. {   &c4gadg, 553, 72, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  353.     (APTR)&image1, NULL, NULL, 0, NULL, GADGC3, NULL };
  354. struct Gadget bwgadg =
  355. {   &c3gadg, 553, 58, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  356.     (APTR)&image1, NULL, NULL, 0, NULL, GADGBW, NULL };
  357.  
  358. struct Gadget iffgadg =
  359. {   &bwgadg, 382, 86, 11, 11, GADGIMAGE, TOGGLESELECT, BOOLGADGET,
  360.     (APTR)&image1, NULL, NULL, 0, NULL, 0, NULL };
  361. struct Gadget scrgadg =
  362. {   &iffgadg, 382, 72, 11, 11, GADGIMAGE, TOGGLESELECT, BOOLGADGET,
  363.     (APTR)&image1, NULL, NULL, 0, NULL, 0, NULL };
  364. struct Gadget prigadg =
  365. {   &scrgadg, 382, 58, 11, 11, GADGIMAGE, TOGGLESELECT|RELVERIFY, BOOLGADGET,
  366.     (APTR)&image1, NULL, NULL, 0, NULL, GADGPRI, NULL };
  367.  
  368. SHORT bordervec3[] =
  369. {   0, 0, 101, 0, 101, 33, 0, 33, 0, 1 };
  370. struct Border border3 =
  371. {   -1, -1, 15, 0, JAM1, 5, bordervec3, NULL };
  372.  
  373. struct IntuiText itext1 =
  374. {   15, 0, JAM1, 26, 11, NULL, "CANCEL", NULL };
  375. struct Gadget cangadg =
  376. {   &prigadg, 174, 219, 100, 32, 0, RELVERIFY, BOOLGADGET,
  377.     (APTR)&border3, NULL, &itext1, 0, NULL, GADGCAN, NULL };
  378.  
  379. struct IntuiText itext2 =
  380. {   15, 0, JAM1, 43, 11, NULL, "OK", NULL };
  381. struct Gadget okgadg =
  382. {   &cangadg, 24, 219, 100, 32, 0, RELVERIFY, BOOLGADGET,
  383.     (APTR)&border3, NULL, &itext2, 0, NULL, GADGOK, NULL };
  384.  
  385. SHORT bordervec4[] =
  386. {   0, 0, 251, 0, 251, 14, 0, 14, 0, 1 };
  387. struct Border border4 =
  388. {   -2, -2, 15, 0, JAM1, 5, bordervec4, NULL };
  389.  
  390. UBYTE ifngadgbuff[80];
  391. struct StringInfo ifngadginfo =
  392. {   ifngadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  393. struct Gadget ifngadg =
  394. {   &okgadg, 25, 170, 250, 13, 0, 0, STRGADGET,
  395.     (APTR)&border4, NULL, NULL, 0, (APTR)&ifngadginfo, 0, NULL };
  396.  
  397. UBYTE if4gadgbuff[80];
  398. struct StringInfo if4gadginfo =
  399. {   if4gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  400. struct Gadget if4gadg =
  401. {   &ifngadg, 25, 114, 250, 13, 0, 0, STRGADGET,
  402.     (APTR)&border4, NULL, NULL, 0, (APTR)&if4gadginfo, 0, NULL };
  403.  
  404. UBYTE if3gadgbuff[80];
  405. struct StringInfo if3gadginfo =
  406. {   if3gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  407. struct Gadget if3gadg =
  408. {   &if4gadg, 25, 100, 250, 13, 0, 0, STRGADGET,
  409.     (APTR)&border4, NULL, NULL, 0, (APTR)&if3gadginfo, 0, NULL };
  410.  
  411. UBYTE if2gadgbuff[80];
  412. struct StringInfo if2gadginfo =
  413. {   if2gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  414. struct Gadget if2gadg =
  415. {   &if3gadg, 25, 86, 250, 13, 0, 0, STRGADGET,
  416.     (APTR)&border4, NULL, NULL, 0, (APTR)&if2gadginfo, 0, NULL };
  417.  
  418. UBYTE if1gadgbuff[80];
  419. struct StringInfo if1gadginfo =
  420. {   if1gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  421. struct Gadget if1gadg =
  422. {   &if2gadg, 25, 72, 250, 13, 0, 0, STRGADGET,
  423.     (APTR)&border4, NULL, NULL, 0, (APTR)&if1gadginfo, 0, NULL };
  424.  
  425. UBYTE if0gadgbuff[80];
  426. struct StringInfo if0gadginfo =
  427. {   if0gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  428. struct Gadget if0gadg =
  429. {   &if1gadg, 25, 58, 250, 13, 0, 0, STRGADGET,
  430.     (APTR)&border4, NULL, NULL, 0, (APTR)&if0gadginfo, 0, NULL };
  431.  
  432. struct IntuiText itext23 =
  433. {   15, 0, JAM1, 300, 226, NULL, "Close WBench", NULL };
  434. struct IntuiText itext22 =
  435. {   15, 0, JAM1, 448, 212, NULL, "Paths", &itext23 };
  436. struct IntuiText itext21 =
  437. {   15, 0, JAM1, 448, 198, NULL, "VM", &itext22 };
  438. struct IntuiText itext20 =
  439. {   15, 0, JAM1, 448, 184, NULL, "Htone", &itext21 };
  440. struct IntuiText itext19 =
  441. {   15, 0, JAM1, 448, 170, NULL, "Fonts", &itext20 };
  442. struct IntuiText itext18 =
  443. {   15, 0, JAM1, 448, 142, NULL, "Memory", &itext19 };
  444. struct IntuiText itext17 =
  445. {   15, 0, JAM1, 300, 198, NULL, "Dpi.", &itext18 };
  446. struct IntuiText itext16 =
  447. {   15, 0, JAM1, 300, 184, NULL, "Off.", &itext17 };
  448. struct IntuiText itext15 =
  449. {   15, 0, JAM1, 300, 170, NULL, "X, Y", &itext16 };
  450. struct IntuiText itext14 =
  451. {   15, 0, JAM1, 300, 142, NULL, "Page size", &itext15 };
  452. struct IntuiText itext13 =
  453. {   15, 0, JAM1, 300, 114, NULL, "Printer density", &itext14 };
  454. struct IntuiText itext12 =
  455. {   15, 0, JAM1, 416, 86, NULL, "4 Colour (CMYK)", &itext13 };
  456. struct IntuiText itext11 =
  457. {   15, 0, JAM1, 416, 72, NULL, "3 Colour (RGB)", &itext12 };
  458. struct IntuiText itext10 =
  459. {   15, 0, JAM1, 416, 58, NULL, "Black and white", &itext11 };
  460. struct IntuiText itext9 =
  461. {   15, 0, JAM1, 300, 86, NULL, "IFF file", &itext10 };
  462. struct IntuiText itext8 =
  463. {   15, 0, JAM1, 300, 72, NULL, "Screen", &itext9 };
  464. struct IntuiText itext7 =
  465. {   15, 0, JAM1, 300, 58, NULL, "Printer", &itext8 };
  466. struct IntuiText itext6 =
  467. {   15, 0, JAM1, 416, 30, NULL, "Colours", &itext7 };
  468. struct IntuiText itext5 =
  469. {   15, 0, JAM1, 300, 30, NULL, "Output", &itext6 };
  470. struct IntuiText itext4 =
  471. {   15, 0, JAM1, 25, 156, NULL, "IFF file name (pattern)", &itext5 };
  472. struct IntuiText itext3 =
  473. {   15, 0, JAM1, 25, 30, NULL, "Startup file name(s)", &itext4 };
  474.  
  475. struct NewWindow newreqwindow =
  476. {   25, 60, 590, 260, 0, 15,
  477.     GADGETUP|GADGETDOWN, ACTIVATE|SMART_REFRESH|NOCAREREFRESH,
  478.     &if0gadg, NULL, POSTVER, NULL, NULL, 0, 0, 0, 0, CUSTOMSCREEN
  479. };
  480.  
  481. struct NewWindow newerrwindow =
  482. {   0, 100, 640, 100, 0, 15,
  483.     0, SIMPLE_REFRESH|NOCAREREFRESH|BACKDROP|BORDERLESS,
  484.     NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, CUSTOMSCREEN
  485. };
  486.  
  487. /* Stuff for the windows */
  488.  
  489. struct PropInfo hscrollinfo =   /* Horizontal scroll proportional gadget */
  490. {   AUTOKNOB|FREEHORIZ, -1, -1, -1, -1 };
  491. struct Image hscrollimage;
  492. struct Gadget hscrollgadg =
  493. {   NULL, 0, -8, -14, 9,
  494.     GRELBOTTOM|GRELWIDTH, BOTTOMBORDER|RELVERIFY, PROPGADGET,
  495.     (APTR)&hscrollimage, NULL, NULL, 0, (APTR)&hscrollinfo, 0, NULL
  496. };
  497.  
  498. struct PropInfo vscrollinfo =   /* Vertical scroll proportional gadget */
  499. {   AUTOKNOB|FREEVERT, -1, -1, -1, -1 };
  500. struct Image vscrollimage;
  501. struct Gadget vscrollgadg =
  502. {   &hscrollgadg, -15, 0, 16, -8,
  503.     GRELRIGHT|GRELHEIGHT, RIGHTBORDER|RELVERIFY, PROPGADGET,
  504.     (APTR)&vscrollimage, NULL, NULL, 0, (APTR)&vscrollinfo, 0, NULL
  505. };
  506.  
  507. struct NewWindow newbitwindow = /* Bitmap window.  (Borders (4,2,18,10)) */
  508. {   0, 49, 640, 463, -1, -1,
  509.     0, SIMPLE_REFRESH,
  510.     &vscrollgadg, NULL, NULL, NULL, NULL,
  511.     0, 100, 0, 0, CUSTOMSCREEN
  512. };
  513.  
  514. struct NewWindow newintwindow = /* Interactive window */
  515. {   0, 12, 640, 37, -1, -1,
  516.     0, SIZEBRIGHT|WINDOWSIZING|SMART_REFRESH|ACTIVATE,
  517.     NULL, NULL, NULL, NULL, NULL,
  518.     0, 0, 0, 400, CUSTOMSCREEN
  519. };
  520.  
  521. struct IntuiText men2item3text = /* Menu strip */
  522. {   7, 0, COMPLEMENT, 0, 0, NULL, "Kill", NULL };
  523. struct MenuItem men2item3 =
  524. {   NULL, 0, 60, 200, 11, ITEMTEXT|ITEMENABLED|HIGHCOMP,
  525.     0, (APTR)&men2item3text, NULL, NULL, NULL, MENUNULL
  526. };
  527.  
  528. struct IntuiText men2item2text =
  529. {   7, 0, COMPLEMENT, 0, 0, NULL, "Interrupt", NULL };
  530. struct MenuItem men2item2 =
  531. {   &men2item3, 0, 40, 200, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  532.     0, (APTR)&men2item2text, NULL, 'I', NULL, MENUNULL
  533. };
  534.  
  535. struct IntuiText men2item1text =
  536. {   7, 0, COMPLEMENT, 0, 0, NULL, "Continue after pause", NULL };
  537. struct MenuItem men2item1 =
  538. {   &men2item2, 0, 20, 200, 11, ITEMTEXT|COMMSEQ|HIGHCOMP,
  539.     0, (APTR)&men2item1text, NULL, 'C', NULL, MENUNULL
  540. };
  541.  
  542. struct IntuiText men2item0text =
  543. {   7, 0, COMPLEMENT, 19, 0, NULL, "Pause every page", NULL };
  544. struct MenuItem men2item0 =
  545. {   &men2item1, 0, 0, 200,11,
  546.     CHECKIT|ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP|CHECKED,
  547.     0, (APTR)&men2item0text, NULL, 'P', NULL, MENUNULL
  548. };
  549.  
  550. struct Menu menu2 =
  551. {   NULL, 160, 0, 80, 0, MENUENABLED, "Control", &men2item0 };
  552.  
  553. struct IntuiText men1item3text =
  554. {   7, 0, COMPLEMENT, 0, 0, NULL, "Interactive", NULL };
  555. struct MenuItem men1item3 =
  556. {   NULL, 0, 60, 128, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  557.     0, (APTR)&men1item3text, NULL, 'W', NULL, MENUNULL
  558. };
  559.  
  560. struct IntuiText men1item2text =
  561. {   7, 0, COMPLEMENT, 0, 0, NULL, "Run file", NULL };
  562. struct MenuItem men1item2 =
  563. {   &men1item3, 0, 40, 128, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  564.     0, (APTR)&men1item2text, NULL, 'R', NULL, MENUNULL
  565. };
  566.  
  567. struct IntuiText men1item1text =
  568. {   7, 0, COMPLEMENT, 0, 0, NULL, "Load file", NULL };
  569. struct MenuItem men1item1 =
  570. {   &men1item2, 0, 20, 128, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  571.     0, (APTR)&men1item1text, NULL, 'L', NULL, MENUNULL
  572. };
  573.  
  574. struct IntuiText men1item0text =
  575. {   7, 0, COMPLEMENT, 0, 0, NULL, "Load font", NULL };
  576. struct MenuItem men1item0 =
  577. {   &men1item1, 0, 0, 128, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  578.     0, (APTR)&men1item0text, NULL, 'F', NULL, MENUNULL
  579. };
  580.  
  581. struct Menu menu1 =
  582. {   &menu2, 80, 0, 80, 0, 0, "File", &men1item0 };
  583.  
  584. struct IntuiText men0item1text =
  585. {   7, 0, COMPLEMENT, 0, 0, NULL, "Quit", NULL };
  586. struct MenuItem men0item1 =
  587. {   NULL, 0, 20, 100, 11, ITEMTEXT|ITEMENABLED|HIGHCOMP,
  588.     0, (APTR)&men0item1text, NULL, NULL, NULL, MENUNULL
  589. };
  590.  
  591. struct IntuiText men0item0text =
  592. {   7, 0, COMPLEMENT, 0, 0, NULL, "Restart", NULL };
  593. struct MenuItem men0item0 =
  594. {   &men0item1, 0, 0, 100, 11, ITEMTEXT|ITEMENABLED|HIGHCOMP,
  595.     0, (APTR)&men0item0text, NULL, NULL, NULL, MENUNULL
  596. };
  597.  
  598. struct Menu menu0 =
  599. {   &menu1, 0, 0, 80, 0, 0, "Project", &men0item0 };
  600.  
  601. /* Arguments */
  602.  
  603. int arglen, argwb, argfilec, argsizec, argmemc;
  604. int argprint, argscreen, argiff, argint, argclosewb;
  605. char *argbuf, *argsizev[5], *argmemv[5];
  606. char *argfilev[] =
  607. {   (char *) &if0gadgbuff[0],
  608.     (char *) &if1gadgbuff[0],
  609.     (char *) &if2gadgbuff[0],
  610.     (char *) &if3gadgbuff[0],
  611.     (char *) &if4gadgbuff[0]
  612. };
  613. char *argifn =
  614.     (char *) &ifngadgbuff[0];
  615. char *argcon;
  616. char *argkey[] =
  617. {   "IFF",
  618.     "SCREEN",
  619.     "PRINTER",
  620.     "INTERACTIVE",
  621.     "SIZE",
  622.     "MEM",
  623.     "CLOSEWB",
  624.     "CONDEV",
  625.     NULL
  626. };
  627.  
  628. /* Lattice stuff for using Arp FPrintf/SPrintf */
  629.  
  630. LONG ArpFPrintf(BPTR, char*,);
  631. LONG ArpSPrintf(char *, char*,);
  632.  
  633. # pragma libcall ArpBase ArpFPrintf 00EA 98003
  634. # pragma libcall ArpBase ArpSPrintf 0282 98003
  635.  
  636. LONG __stdargs FPrintf(BPTR fh, char *format,)
  637. {   return  ArpFPrintf(fh, format, (&format)+1);
  638. }
  639.  
  640. LONG __stdargs SPrintf(char *string, char *format,)
  641. {   return  ArpSPrintf(string, format, (&format)+1);
  642. }
  643.  
  644. /* Startup code */
  645.  
  646. extern void main(int argc, char **argv);
  647.  
  648. void _main(char *line)
  649. {   struct DiskObject *diskobj;
  650.     char *diskname;
  651.     char *argv[32];
  652.     int argc;
  653.  
  654.     /* If this is a workbench startup construct the arguments string.  We
  655.      * find the name of our icon from the first argument.  Then we laod the
  656.      * icon and look for an ARGS string in the tooltypes.  We concatenate
  657.      * the program name and the arguments into an Alloc'ed buffer */
  658.  
  659.     if (WBenchMsg && WBenchMsg->sm_NumArgs)
  660.     {   diskname = WBenchMsg->sm_ArgList[0].wa_Name;
  661.         line = NULL;
  662.         IconBase = (struct IconBase *) OpenLibrary("icon.library", 0);
  663.         if (IconBase)
  664.         {   diskobj = GetDiskObject(diskname);
  665.             line = FindToolType(diskobj->do_ToolTypes, "ARGS");
  666.         }
  667.         if (line)
  668.         {   arglen = strlen(diskname) + strlen(line) + 2;
  669.             argbuf = AllocMem(arglen, MEMF_CLEAR);
  670.         }
  671.         if (argbuf)
  672.         {   strcpy(argbuf, diskname);
  673.             strcat(argbuf, " ");
  674.             strcat(argbuf, line);
  675.         }
  676.         line = argbuf;
  677.         if (diskobj)
  678.             FreeDiskObject(diskobj);
  679.         if (IconBase)
  680.             CloseLibrary((struct Library *) IconBase);
  681.     }
  682.  
  683.     /* Parse the arguments to break words and strip quotes.  N.B. the
  684.      * main program can determine that the arument was quoted by inspecting
  685.      * the preceeding character */
  686.  
  687.     argc = 0;
  688.     if (line == NULL) goto endline;
  689.     for (;;)
  690.     {   while (*line == ' ' || *line == '\t' || *line == '\n') line++;
  691.         if (*line == 0) break;
  692.         if (argc == 32)
  693.         {   argc = 0;
  694.             goto endline;
  695.         }
  696.         if (*line == '"')
  697.         {   argv[argc] = ++line;
  698.             while (*line != '"')
  699.             {   if (*line == 0)
  700.                 {   argc = 0;
  701.                     goto endline;
  702.                 }
  703.                 line++;
  704.             }
  705.         }
  706.         else
  707.         {   argv[argc] = line;
  708.             while (*line != ' ' && *line != '\t' && *line != '\n')
  709.             {   if (*line == 0)
  710.                 {   argc++;
  711.                     goto endline;
  712.                 }
  713.                 line++;
  714.             }
  715.         }
  716.         *line++ = 0;
  717.         argc++;
  718.     }
  719. endline:
  720.  
  721.     /* Set up the standard input/output files */
  722.  
  723.     if (WBenchMsg) /* Workbench startup, no files */
  724.     {   argwb = 1;
  725.     }
  726.     else           /* CLI startup, open stderr */
  727.     {   errfh = Open("*", MODE_OLDFILE);
  728.         if (errfh == NULL)
  729.         {   retcode = 20;
  730.             goto tidyexit;
  731.         }
  732.     }
  733.  
  734.     /* Call the main program  */
  735.  
  736.     main(argc, argv);
  737.  
  738.     /* Tidy up and exit */
  739.  
  740. tidyexit:
  741.     if (argbuf) FreeMem(argbuf, arglen);
  742.     if (errfh) Close(errfh);
  743.  
  744.     XCEXIT(retcode);
  745. }
  746.  
  747. /* Main program */
  748.  
  749. void main(int argc, char **argv)
  750. {   struct Message *msg;
  751.     struct Gadget *gadget;
  752.     char *s;
  753.     int *ip, i, ch, l;
  754.     int xsize, ysize, ssize, xoff, yoff, xden, yden, pden;
  755.  
  756.     process = (struct Process *) FindTask(NULL);
  757.     syswindow = (struct Window *) process->pr_WindowPtr;
  758.  
  759.     /* Open the libraries */
  760.  
  761.     GfxBase =
  762.         (struct GfxBase *)       OpenLibrary("graphics.library", 0);
  763.     IntuitionBase =
  764.         (struct IntuitionBase *) OpenLibrary("intuition.library", 0);
  765.     ArpBase =
  766.         (struct ArpBase *)       OpenLibrary("arp.library", 39);
  767.     if (GfxBase == NULL || IntuitionBase == NULL || ArpBase == NULL)
  768.     {   errmsg("can't open libraries (you need arp.library V39+)");
  769.         goto errorexit;
  770.     }
  771. # ifndef STATICLINK
  772.     PSbase = OpenLibrary("post.library", POSTVERNO);
  773.     if (PSbase == NULL)
  774.     {   errmsg("can't open post.library");
  775.         goto errorexit;
  776.     }
  777. # endif
  778.  
  779.     /* Parse the arguments and keywords.  See the usage string below */
  780.  
  781.     if (argc == 0) goto badargs;
  782.     argc--;
  783.     argv++;
  784.     if (argc == 0 || argwb) argint = 1;
  785.     if (argc == 1 && strcmp(*argv, "?") == 0) goto usage;
  786.  
  787.     while (argc--)
  788.     {   s = *argv++;
  789.         i = -1;
  790.         if (s[-1] != '"')
  791.             for (;;)
  792.             {   i++;
  793.                 if (argkey[i] == NULL)
  794.                 {   i = -1;
  795.                     break;
  796.                 }
  797.                 if (stricmp(s, argkey[i]) == 0) break;
  798.             }
  799.         switch (i)
  800.         {   case 0:    /* IFF */
  801.                 if (argc == 0 || strlen(*argv) >= 80) goto badargs;
  802.                 argc--;
  803.                 argiff = 1;
  804.                 strcpy(argifn, *argv++);
  805.                 break;
  806.  
  807.             case 1:    /* SCREEN */
  808.                 argscreen = 1;
  809.                 break;
  810.  
  811.             case 2:    /* PRINTER */
  812.                 argprint = 1;
  813.                 break;
  814.  
  815.             case 3:    /* INTERACTIVE */
  816.                 argint = 1;
  817.                 break;
  818.  
  819.             case 4:    /* SIZE */
  820.                 if (argc == 0) goto badargs;
  821.                 argc--;
  822.                 if (argsizec == 5) goto badargs;
  823.                 argsizev[argsizec++] = *argv++;
  824.                 break;
  825.  
  826.             case 5:    /* MEM */
  827.                 if (argc == 0) goto badargs;
  828.                 argc--;
  829.                 if (argmemc == 5) goto badargs;
  830.                 argmemv[argmemc++] = *argv++;
  831.                 break;
  832.  
  833.             case 6:    /* CLOSEWB */
  834.                 argclosewb = 1;
  835.                 break;
  836.  
  837.             case 7:    /* CONDEV */
  838.                 if (argc == 0) goto badargs;
  839.                 argc--;
  840.                 argcon = *argv++;
  841.                 break;
  842.  
  843.             default:
  844.                 if (argfilec == 5 || strlen(s) >= 80) goto badargs;
  845.                 strcpy(argfilev[argfilec++], s);
  846.         }
  847.     }
  848.     if (!argscreen && !argiff && !argint)  argprint = 1;
  849.     if (argscreen) argint = 1;
  850.  
  851.     /* Set up the screen size, adjusting for interlace */
  852.  
  853.     newscreen.Width = GfxBase->NormalDisplayColumns;
  854.     newscreen.Height = GfxBase->NormalDisplayRows * 2;
  855.     if (newscreen.Width < 400) newscreen.Width = 400;
  856.     if (newscreen.Height < 300) newscreen.Height = 300;
  857.  
  858.     /* Set up the default page size.  For printer output we get the defaults
  859.      * from preferences; otherwise we default to an A4 inch page at 75 dpi.
  860.      * with 3 colour planes, trimming the width to fit onto the screen.  (We
  861.      * can't quite fit A4 onto a standard 640 pixel wide screen - 8.24 inches
  862.      * instead of 8.26). */
  863.  
  864.     bitmap.BytesPerRow = 1;
  865.     bitmap.Rows = 1;
  866.     bitmap.Flags = 0;
  867.     bitmap.Depth = parm.page.depth = 3;
  868.     if (argprint)
  869.     {   setprinter();
  870.         if (prport == NULL)
  871.         {   errmsg("can't open printer device");
  872.             goto errorexit;
  873.         }
  874.     }
  875.     else
  876.     {   parm.page.xsize = 824 * 75 / 100;
  877.         parm.page.ysize = 1169 * 75 / 100;
  878.         parm.page.xoff = parm.page.yoff = 0;
  879.         parm.page.xden = parm.page.yden = 75;
  880.         if (parm.page.xsize > newscreen.Width - 22)
  881.             parm.page.xsize = newscreen.Width - 22;
  882.     }
  883.     parm.page.ydir = -1;
  884.  
  885.     /* Parse the "SIZE xyod..s..p.bc." options */
  886.  
  887.     xsize = ysize = ssize = xden = yden = pden = 0;
  888.     for (i = 0; i < argsizec; i++)
  889.     {   s = argsizev[i];
  890.         for (;;)
  891.         {   ch = *s++;
  892.             if (ch == 0) break;
  893.             ch = tolower(ch);
  894.             switch (ch)
  895.             {   case 'x':
  896.                     if      (tolower(*s) == 'o')
  897.                     {   s++;
  898.                         if (!strtoint(&s, &xoff)) goto badvalue;
  899.                         parm.page.xoff = xoff;
  900.                         continue;
  901.                     }
  902.                     else if (tolower(*s) == 'd')
  903.                     {   s++;
  904.                         ip = &xden;
  905.                     }
  906.                     else
  907.                         ip = &xsize;
  908.                     break;
  909.  
  910.                 case 'y':
  911.                     if      (tolower(*s) == 'o')
  912.                     {   s++;
  913.                         if (!strtoint(&s, &yoff)) goto badvalue;
  914.                         parm.page.yoff = yoff;
  915.                         continue;
  916.                     }
  917.                     else if (tolower(*s) == 'd')
  918.                     {   s++;
  919.                         ip = &yden;
  920.                     }
  921.                     else
  922.                         ip = &ysize;
  923.                     break;
  924.  
  925.                 case 's':
  926.                     ip = &ssize;
  927.                     break;
  928.  
  929.                 case 'p':
  930.                     ip = &pden;
  931.                     break;
  932.  
  933.                 case 'd':
  934.                 {   if (!strtoint(&s, &xden)) goto badvalue;
  935.                     yden = xden;
  936.                     continue;
  937.                 }
  938.  
  939.                 case 'b':
  940.                     parm.page.depth = 1;
  941.                     continue;
  942.  
  943.                 case 'c':
  944.                     ch = *s;
  945.                     if      (ch == '3')
  946.                     {   s++;
  947.                         parm.page.depth = 3;
  948.                     }
  949.                     else if (ch == '4')
  950.                     {   s++;
  951.                         parm.page.depth  = 4;
  952.                     }
  953.                     else
  954.                         parm.page.depth = 3;
  955.                     continue;
  956.  
  957.                 default:
  958.                     goto badvalue;
  959.             }
  960.             if (!strtoint(&s, ip)) goto badvalue;
  961.         }
  962.     }
  963.     if (xden != 0) parm.page.xden = xden;
  964.     if (yden != 0) parm.page.yden = yden;
  965.     if (xsize != 0) parm.page.xsize = xsize;
  966.     if (ysize != 0) parm.page.ysize = ysize;
  967.     if (pden != 0)
  968.     {   prden = pden;
  969.         if (argprint) setprintden();
  970.     }
  971.  
  972.     /* Set up the default memory sizes */
  973.  
  974.     parm.memvlen = defmemvlen;
  975.     parm.memflen = defmemflen;
  976.     parm.memllen = defmemllen;
  977.     parm.memhlen = defmemhlen;
  978.  
  979.     /* Parse the "MEM fhlv.." options */
  980.  
  981.     for (i = 0; i < argmemc; i++)
  982.     {   s = argmemv[i];
  983.         for (;;)
  984.         {   ch = *s++;
  985.             if (ch == 0) break;
  986.             ch = tolower(ch);
  987.             switch (ch)
  988.             {   case 'f':
  989.                     ip = &parm.memflen;
  990.                     break;
  991.  
  992.                 case 'h':
  993.                     ip = &parm.memhlen;
  994.                     break;
  995.  
  996.                 case 'l':
  997.                     ip = &parm.memllen;
  998.                     break;
  999.  
  1000.                 case 'v':
  1001.                     ip = &parm.memvlen;
  1002.                     break;
  1003.  
  1004.                 default:
  1005.                     goto badvalue;
  1006.             }
  1007.             if (!strtoint(&s, ip)) goto badvalue;
  1008.         }
  1009.     }
  1010.  
  1011.     /* We only need the file requestors if we are interactive */
  1012.  
  1013.     if (argint)
  1014.     {   filereq1 = ArpAllocFreq();
  1015.         filereq2 = ArpAllocFreq();
  1016.         if (filereq1 == NULL || filereq2 == NULL)
  1017.         {   errmsg("can't get file requestors");
  1018.             goto errorexit;
  1019.         }
  1020.     }
  1021.  
  1022.     /* We seem to have to explicitly load the font from the disk before
  1023.      * Intuition can be depended upon to use it */
  1024.  
  1025.     if (argint)
  1026.     {  DiskfontBase = OpenLibrary("diskfont.library", 0);
  1027.        if (DiskfontBase)
  1028.        {   textfont = OpenDiskFont(&topaz11);
  1029.            CloseLibrary(DiskfontBase);
  1030.        }
  1031.        if (textfont == NULL)
  1032.        {   errmsg("can't find font topaz/11");
  1033.            goto errorexit;
  1034.        }
  1035.     }
  1036.  
  1037.     /* Set up the color map according to the number of bitplanes */
  1038.  
  1039. setcmap:
  1040.     restarterror = 0;
  1041.     if (!argint) newscreen.Depth = parm.page.depth;
  1042.     colormap.Count = 1 << newscreen.Depth;
  1043.     if      (newscreen.Depth == 1)
  1044.         colormap.ColorTable = (APTR) bcolors;
  1045.     else
  1046.         colormap.ColorTable = (APTR) ccolors;
  1047.  
  1048.     /* Open the screen and load the color map  */
  1049.  
  1050.     if (argint)
  1051.     {   screen = OpenScreen(&newscreen);
  1052.         if (screen == NULL)
  1053.         {   errmsg("can't open screen");
  1054.             goto errorexit;
  1055.         }
  1056.         newerrwindow.Screen = screen;
  1057.         newreqwindow.Screen = screen;
  1058.         newbitwindow.Screen = screen;
  1059.         newintwindow.Screen = screen;
  1060.         LoadRGB4(&screen->ViewPort,
  1061.                  (short *) colormap.ColorTable, colormap.Count);
  1062.         newerrwindow.Width = newscreen.Width;
  1063.         errwindow = OpenWindow(&newerrwindow);
  1064.         if (errwindow == NULL)
  1065.         {   errmsg("can't open error window");
  1066.             goto errorexit;
  1067.         }
  1068.         process->pr_WindowPtr = (APTR) errwindow;
  1069.     }
  1070.     restarterror = 1;
  1071.  
  1072. restart:
  1073.     if (fatalerror) goto tidyexit;
  1074.     retcode = 0;
  1075.     tidyup();
  1076.  
  1077.     if (argint)
  1078.     {   if (argclosewb) CloseWorkBench();
  1079.         if (requested == 0)
  1080.         {
  1081.             /* Put up the parameters requestor (window) */
  1082.  
  1083.             setreqgadgets();
  1084.             newreqwindow.IDCMPFlags = GADGETUP|GADGETDOWN;
  1085.             reqwindow = OpenWindow(&newreqwindow);
  1086.             if (reqwindow == NULL)
  1087.             {   errmsg("can't open parameters window");
  1088.                 goto restart;
  1089.             }
  1090.             PrintIText(reqwindow->RPort, &itext3, 0, 0);
  1091.  
  1092.             /* Loop handling gadget messages */
  1093.  
  1094.             for (;;)
  1095.             {   WaitPort(reqwindow->UserPort);
  1096.                 while (msg = GetMsg(reqwindow->UserPort))
  1097.                 {   gadget = (struct Gadget *)
  1098.                         ((struct IntuiMessage *) msg)->IAddress;
  1099.                     i = gadget->GadgetID;
  1100.                     switch (i)
  1101.                     {   case GADGOK:
  1102.                         case GADGCAN:
  1103.                             goto closereq;
  1104.  
  1105.                         case GADGPRI:
  1106.                             argprint = getgadgbool(gadget);
  1107.                             if (argprint)
  1108.                             {    setprinter();
  1109.                                  if (prport == NULL)
  1110.                                  {   argprint = 0;
  1111.                                      errmsg("can't open printer device");
  1112.                                      i = GADGRST;
  1113.                                      goto closereq;
  1114.                                  }
  1115.                             }
  1116.                             goto setgadg;
  1117.  
  1118.                         case GADGPD1: case GADGPD2: case GADGPD3:
  1119.                         case GADGPD4: case GADGPD5: case GADGPD6:
  1120.                         case GADGPD7:
  1121.                             if (argprint)
  1122.                             {   prden = i - (GADGPD1 - 1);
  1123.                                 setprintden();
  1124.                                 goto setgadg;
  1125.                             }
  1126.                             break;
  1127.  
  1128.                         case GADGBW:
  1129.                             parm.page.depth = 1;
  1130.                             goto setgadg;
  1131.  
  1132.                         case GADGC3:
  1133.                             parm.page.depth = 3;
  1134.                             goto setgadg;
  1135.  
  1136.                         case GADGC4:
  1137.                             parm.page.depth = 4;
  1138. setgadg:                    setreqgadgets();
  1139.                             break;
  1140.  
  1141.                         case GADGABS:
  1142.                             getgadgabs(gadget);
  1143.                     }
  1144.                     ReplyMsg(msg);
  1145.                 }
  1146.             }
  1147.  
  1148.             /* Close the requester window */
  1149.  
  1150. closereq:   ModifyIDCMP(reqwindow, CLOSEWINDOW);
  1151.             while (msg)
  1152.             {   ReplyMsg(msg);
  1153.                 msg = GetMsg(reqwindow->UserPort);
  1154.             }
  1155.             CloseWindow(reqwindow);
  1156.             reqwindow = NULL;
  1157.  
  1158.             if (i == GADGCAN) goto tidyexit;
  1159.             if (i == GADGRST) goto restart;
  1160.  
  1161.             getreqgadgets();
  1162.  
  1163.             /* If we have changed our mind about the number of bitplanes
  1164.              * close the screen and go back to reopen it with the new depth
  1165.              */
  1166.  
  1167.             if (parm.page.depth != newscreen.Depth)
  1168.             {   process->pr_WindowPtr = (APTR) syswindow;
  1169.                 CloseWindow(errwindow);
  1170.                 errwindow = NULL;
  1171.                 CloseScreen(screen);
  1172.                 screen = NULL;
  1173.                 newscreen.Depth = parm.page.depth;
  1174.                 requested = 1;
  1175.                 goto setcmap;
  1176.             }
  1177.         }
  1178.         requested = 0;
  1179.     }
  1180.  
  1181.     /* Set the size of the interactive windows */
  1182.  
  1183.     if (argint)
  1184.     {   newbitwindow.Width = newscreen.Width;
  1185.         newbitwindow.Height = newscreen.Height -
  1186.             newintwindow.TopEdge - newintwindow.Height;
  1187.         newbitwindow.TopEdge = newscreen.Height -
  1188.             newbitwindow.Height;
  1189.         newintwindow.Width = newscreen.Width;
  1190.         newintwindow.MaxHeight = newscreen.Height -
  1191.             newintwindow.TopEdge - newbitwindow.MinHeight;
  1192.  
  1193.     /* Locate the visible part of the bitmap within its window, adjusting
  1194.      * for the borders */
  1195.  
  1196.         winxbase = 4;
  1197.         winybase = 2;
  1198.         winxsize = newbitwindow.Width - 22;
  1199.         winysize = newbitwindow.Height - 12;
  1200.  
  1201.     /* Set the page size.  It must not be smaller than the interior of the
  1202.      * interactive window.  We may not need the horizontal scroll bar.  Each
  1203.      * bitmap row must start on a word boundary */
  1204.  
  1205.         if (parm.page.xsize <= winxsize)
  1206.         {   winysize += 8;
  1207.             vscrollgadg.NextGadget = 0;
  1208.             vscrollgadg.Height = 0;
  1209.         }
  1210.         else
  1211.         {   vscrollgadg.NextGadget = &hscrollgadg;
  1212.             vscrollgadg.Height = -8;
  1213.         }
  1214.         if (parm.page.xsize < winxsize) parm.page.xsize = winxsize;
  1215.         if (parm.page.ysize < winysize) parm.page.ysize = winysize;
  1216.     }
  1217.     parm.page.ybase = 0;
  1218.     parm.page.yheight = parm.page.ysize;
  1219.     if (!argint)
  1220.         if (ssize != 0 && ssize < parm.page.ysize) parm.page.ysize = ssize;
  1221.     if (parm.page.xsize == 0 || parm.page.ysize == 0)
  1222.     {   errmsg("page size not set in preferences");
  1223.         goto restart;
  1224.     }
  1225.     parm.page.xbytes = (parm.page.xsize + 15) >> 3 & 0xfffffffe;
  1226.     parm.page.len = parm.page.xbytes * parm.page.ysize;
  1227.  
  1228.     /* Allocate the page buffer.  It  must be in chip memory if we are
  1229.      * outputting to a screen */
  1230.  
  1231.     for (i = 0; i < parm.page.depth; i++)
  1232.     {   if ((parm.page.buf[i] = AllocMem(parm.page.len,
  1233.                  (argscreen ? MEMF_CHIP|MEMF_CLEAR :
  1234.                               MEMF_PUBLIC|MEMF_CLEAR))) == NULL)
  1235.         {   errmsg("can't get page buffer");
  1236.             goto restart;
  1237.         }
  1238.     }
  1239.  
  1240.     /* Initialise the bitmap */
  1241.  
  1242.     bitmap.BytesPerRow = parm.page.xbytes;
  1243.     bitmap.Rows = parm.page.ysize;
  1244.     bitmap.Flags = 0;
  1245.     bitmap.Depth = parm.page.depth;
  1246.     memcpy((char *) bitmap.Planes, (char *) parm.page.buf,
  1247.            sizeof bitmap.Planes);
  1248.  
  1249.     /* For interactive working, set up the windows */
  1250.  
  1251.     if (argint)
  1252.     {
  1253.         /* Finish initialising the gadgets and open the windows */
  1254.  
  1255.         hscrollinfo.HorizPot = 0;
  1256.         vscrollinfo.VertPot = 0xffff;
  1257.         hscrollinfo.HorizBody = (0xffff * winxsize) / parm.page.xsize;
  1258.         vscrollinfo.VertBody = (0xffff * winysize) / parm.page.ysize;
  1259.         winxpos = 0;
  1260.         winypos = parm.page.ysize - winysize;
  1261.         bitwindow = OpenWindow(&newbitwindow);
  1262.         intwindow = OpenWindow(&newintwindow);
  1263.         if (bitwindow == NULL || intwindow == NULL)
  1264.         {   errmsg("can't open windows");
  1265.             goto restart;
  1266.         }
  1267.  
  1268.         /* Set up new console streams.  Black on white characters */
  1269.  
  1270.         if (argcon == NULL)
  1271.             if (SysBase->LibNode.lib_Version < 36)
  1272.                 argcon = "CON:W";           /* Workbench 1.3, ConMan 1.3+ */
  1273.             else
  1274.                 argcon = "CON://///WINDOW"; /* Workbench 2.0 */
  1275.         SPrintf((char *) undobuff, "%s%lx", argcon, intwindow);
  1276.         confh = Open((char *) undobuff, MODE_OLDFILE);
  1277.         if (confh == NULL)
  1278.         {   errmsg("can't open console handler");
  1279.             goto restart;
  1280.         }
  1281.         FPrintf(confh, "\x9b\x33\x37\x6d");
  1282.     }
  1283.  
  1284.         /* Create the menu handler task */
  1285.  
  1286.     if (argint)
  1287.     {   mainport = CreatePort(NULL, 0);
  1288.         if (mainport)
  1289.         {   menumsg.ExecMessage.mn_ReplyPort = mainport;
  1290.             menumsg.action = PSACTOPEN;
  1291.             menumsg.bitmap = &bitmap;
  1292.             menutask = CreateTask("post.menu", 6, (APTR) menuproc, 2000);
  1293.         }
  1294.         if (menutask)
  1295.             sendmenu(PSACTOPEN);
  1296.         if (menutask == NULL)
  1297.         {   errmsg("can't create menu handler");
  1298.             goto restart;
  1299.         }
  1300.     }
  1301.  
  1302.     /* Initialise for interpretation */
  1303.  
  1304.     insertbreak();
  1305.     SetExcept(~0, SIGBREAKF_CTRL_C);
  1306.     breakset = 1;
  1307.     insertftrap();
  1308.     ftrapset = 1;
  1309.  
  1310. # ifndef STATICLINK
  1311.     parm.flushfunc = (APTR) flushpage;
  1312.     parm.copyfunc = (APTR) copypage;
  1313. # endif
  1314.  
  1315.     if (argint)
  1316.     {   parm.infh = confh;
  1317.         parm.outfh = confh;
  1318.         parm.errfh = confh;
  1319.     }
  1320.     else
  1321.     {   parm.infh = Input();
  1322.         parm.outfh = Output();
  1323.         parm.errfh = errfh;
  1324.     }
  1325.  
  1326.     arec = PScreateact(&parm);
  1327.     if (arec == 0)
  1328.     {   errmsg("can't get memory");
  1329.         retcode = 20;
  1330.         goto restart;
  1331.     }
  1332.     if ((unsigned) arec <= errmax) retcode = 10;
  1333.  
  1334.     /* Interpret the argument files */
  1335.  
  1336.     for (i = 0; i < 5; i++)
  1337.     {   s = argfilev[i];
  1338.         if (s[0] != 0)
  1339.         {   if (retcode != 0) break;
  1340.             errcode =
  1341.                 PSintstring(arec, s, -1, 
  1342.                     (strcmp(s , "*") == 0 ?
  1343.                         PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE|PSFLAGINTER :
  1344.                         PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE));
  1345.             if (errcode) retcode = 10;
  1346.         }
  1347.     }
  1348.  
  1349.     /* Execute menu commands */
  1350.  
  1351.     if (argint)
  1352.     {   for (;;)
  1353.         {   sendmenu(retcode == 0 ? PSACTCOMMAND : PSACTEXIT);
  1354.             s = menumsg.string;
  1355.             switch (menumsg.command)
  1356.             {   case PSCOMQUIT:
  1357.                     goto quit;
  1358.  
  1359.                 case PSCOMRESTART:
  1360.                     PSdeleteact(arec);
  1361.                     arec = 0;
  1362.                     goto restart;
  1363.  
  1364.                 case PSCOMFILEF:
  1365.                     i = PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE;
  1366.                     strcpy(filereq1->fr_Dir, "PSFonts:");
  1367.                     filereq = filereq1;
  1368.                     filereq->fr_Hail = hailfilef;
  1369.                     goto freq;
  1370.  
  1371.                 case PSCOMFILEL:
  1372.                     i = PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE;
  1373.                     filereq = filereq2;
  1374.                     filereq->fr_Hail = hailfilen;
  1375.                     goto freq;
  1376.  
  1377.                 case PSCOMFILER:
  1378.                     i = PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE|PSFLAGSAVE;
  1379.                     filereq = filereq2;
  1380.                     filereq->fr_Hail = hailfiles;
  1381. freq:               filereq->fr_Window = intwindow;
  1382.                     filereq->fr_FuncFlags |= (FRF_NewIDCMP|FRF_DoColor);
  1383.                     filereq->fr_LeftEdge = 170;
  1384.                     filereq->fr_TopEdge = 150;
  1385.                     if (FileRequest(filereq) == NULL) break;
  1386.                     l = strlen(filereq->fr_Dir);
  1387.                     TackOn(filereq->fr_Dir, filereq->fr_File);
  1388.                     PSintstring(arec, filereq->fr_Dir, -1, i);
  1389.                     filereq->fr_Dir[l] = 0;
  1390.                     break;
  1391.  
  1392.                 case PSCOMINTER:
  1393.                     PSintstring(arec, "%stdin", -1, 
  1394.                         PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE|PSFLAGSAVE|
  1395.                         PSFLAGINTER);
  1396.                     break;
  1397.             }
  1398.         }
  1399.     }
  1400.  
  1401. quit:
  1402.     PSdeleteact(arec);
  1403.     arec = 0;
  1404.     goto tidyexit;
  1405.  
  1406.     /* Argument errors and usage query */
  1407.  
  1408. badargs:
  1409.     errmsg("arguments bad, or value missing or too long");
  1410.     goto badusage;
  1411.  
  1412. badvalue:
  1413.     errmsg("argument bad value");
  1414.  
  1415. badusage:
  1416.     retcode = 20;
  1417.  
  1418. usage:
  1419.     if (!argwb) FPrintf(errfh, "post: usage:\n"
  1420.     "    post [files...] [IFF file] [SCREEN] [PRINTER] [INTERACTIVE]\n"
  1421.     "         [SIZE xyod..s..p.bc.] [MEM fhlv..] [CLOSEWB] [CONDEV con]\n");
  1422.     goto tidyexit;
  1423.  
  1424.     /* Tidy up and exit */
  1425.  
  1426. errorexit:
  1427.     retcode = 20;
  1428.  
  1429. tidyexit:
  1430.     tidyup();
  1431.  
  1432.     process->pr_WindowPtr = (APTR) syswindow;
  1433.     if (errwindow) CloseWindow(errwindow);
  1434.     if (screen) CloseScreen(screen);
  1435.  
  1436.     if (textfont) CloseFont(textfont);
  1437.  
  1438.     if (propen) CloseDevice((struct IORequest *) &prreq);
  1439.     if (prport) DeletePort(prport);
  1440.  
  1441. # ifndef STATICLINK
  1442.     if (PSbase)  CloseLibrary(PSbase);
  1443. # endif
  1444.     if (ArpBase) CloseLibrary((struct Library *) ArpBase);
  1445.     if (GfxBase) CloseLibrary((struct Library *) GfxBase);
  1446.     if (IntuitionBase)
  1447.     {   OpenWorkBench();
  1448.         CloseLibrary((struct Library *) IntuitionBase);
  1449.     }
  1450. }
  1451.  
  1452. /* Display an error requestor or message */
  1453.  
  1454. struct IntuiText bodytext2 =
  1455. {   AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  1456.     AUTOLEFTEDGE + 32, AUTOTOPEDGE + 11,
  1457.     AUTOITEXTFONT, NULL, NULL };
  1458. struct IntuiText bodytext =
  1459. {   AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  1460.     AUTOLEFTEDGE, AUTOTOPEDGE,
  1461.     AUTOITEXTFONT, POSTVER " Error", &bodytext2 };
  1462. struct IntuiText postext =
  1463. {   AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  1464.     AUTOLEFTEDGE, AUTOTOPEDGE,
  1465.     AUTOITEXTFONT, "RETRY", NULL };
  1466. struct IntuiText negtext =
  1467. {   AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  1468.     AUTOLEFTEDGE, AUTOTOPEDGE,
  1469.     AUTOITEXTFONT, "CANCEL", NULL };
  1470.  
  1471. void errmsg(char *string)
  1472. {   int width;
  1473.     if (IntuitionBase && (errwindow || argwb || ArpBase == NULL))
  1474.     {   bodytext2.IText = string;
  1475.         width = IntuiTextLength(&bodytext2) + 70;
  1476.         if (width < 200) width = 200;
  1477.         if (!AutoRequest(errwindow, &bodytext,
  1478.                 (restarterror ? &postext : NULL), &negtext,
  1479.                 0, 0, width, 70))
  1480.             fatalerror = 1;
  1481.         return;
  1482.     }
  1483.     if (ArpBase && !argwb)
  1484.     {   FPrintf(errfh, "post: %s\n", string);
  1485.         fatalerror = 1;
  1486.         return;
  1487.     }
  1488.     fatalerror = 1;
  1489. }
  1490.  
  1491. /* Tidy up */
  1492.  
  1493. void tidyup(void)
  1494. {   int i;
  1495.  
  1496.     if (breakset)
  1497.     {   SetExcept(0, SIGBREAKF_CTRL_C);
  1498.         deletebreak();
  1499.         breakset = 0;
  1500.     }
  1501.     if (ftrapset)
  1502.     {   deleteftrap();
  1503.         ftrapset = 0;
  1504.     }
  1505.  
  1506.     if (menuport)
  1507.         sendmenu(PSACTCLOSE);
  1508.     if (mainport)
  1509.     {   DeletePort(mainport);
  1510.         mainport = NULL;
  1511.     }
  1512.  
  1513.     if (confh)
  1514.     {   Close(confh);
  1515.         confh = NULL;
  1516.     }
  1517.  
  1518.     /* N.B. some versions of ConMan close the window themselves, so we
  1519.      * don't close the interactive window it it appears to have been
  1520.      * closed already */
  1521.  
  1522.     if (bitwindow)
  1523.     {   CloseWindow(bitwindow);
  1524.         bitwindow = NULL;
  1525.     }
  1526.     if (intwindow)
  1527.     {   if (screen->FirstWindow == intwindow ||
  1528.             errwindow->NextWindow == intwindow) CloseWindow(intwindow);
  1529.         intwindow = NULL;
  1530.     }
  1531.  
  1532.     for (i = 0; i < parm.page.depth; i++)
  1533.         if (parm.page.buf[i])
  1534.         {   FreeMem(parm.page.buf[i], parm.page.len);
  1535.             parm.page.buf[i] = NULL;
  1536.         }
  1537. }
  1538.  
  1539. /* Open the printer device and set up the page size */
  1540.  
  1541. void setprinter(void)
  1542. {   if (propen == 0)
  1543.     {   if (OpenDevice("printer.device", 0,
  1544.                        (struct IOReqest *) &prreq, 0) != 0)
  1545.             return;
  1546.         propen = 1;
  1547.     }
  1548.     if (prport == NULL)
  1549.     {   prport = CreatePort(NULL, 0);
  1550.         if (prport == NULL) return;
  1551.         prreq.io_Message.mn_ReplyPort = prport;
  1552.         prsig = 1 << prport->mp_SigBit;
  1553.         prdata = (struct PrinterData *) prreq.io_Device;
  1554.         prextdata = &prdata->pd_SegmentData->ps_PED;
  1555.         prprefs = &prdata->pd_Preferences;
  1556.         prden = prprefs->PrintDensity;
  1557.     }
  1558.     if ((prprefs->PrintShade & SHADE_COLOR) == 0) parm.page.depth = 1;
  1559.     setprintden();
  1560. }
  1561.  
  1562. /* Set the printer density */
  1563.  
  1564. void setprintden(void)
  1565. {   int pxsize, pysize, xy;
  1566.  
  1567.     /* New density, call the device driver to change its extended data.  No
  1568.      * error check */
  1569.  
  1570.     if (prden > 7) prden = 7;
  1571.     if (prden != prprefs->PrintDensity)
  1572.     {   prreq.io_Command = PRD_DUMPRPORT;
  1573.         prrast.BitMap = &bitmap;
  1574.         prreq.io_RastPort = &prrast;
  1575.         prreq.io_ColorMap = (struct ColorMap *) &colormap;
  1576.         prreq.io_Modes = 0;
  1577.         prreq.io_SrcX = 0;
  1578.         prreq.io_SrcY = 0;
  1579.         prreq.io_SrcWidth = 1;
  1580.         prreq.io_SrcHeight = 1;
  1581.         prreq.io_DestCols = 1;
  1582.         prreq.io_DestRows = 1;
  1583.         prreq.io_Special = (SPECIAL_DENSITY1 * prden) | SPECIAL_NOPRINT;
  1584.         prprefs->PrintDensity = prden;
  1585.         DoIO((struct IORequest *) &prreq);
  1586.     }
  1587.  
  1588.     /* Extract the page size and density from the printer device preferences
  1589.      * and extended data */
  1590.  
  1591.     parm.page.xden = prextdata->ped_XDotsInch;
  1592.     parm.page.yden = prextdata->ped_YDotsInch;
  1593.     if      (prprefs->PrintFlags & PIXEL_DIMENSIONS)
  1594.     {   pxsize = prprefs->PrintMaxWidth;
  1595.         pysize = prprefs->PrintMaxHeight;
  1596.     }
  1597.     else if (prprefs->PrintFlags &
  1598.                 (BOUNDED_DIMENSIONS|ABSOLUTE_DIMENSIONS))
  1599.     {   pxsize = prprefs->PrintMaxWidth * parm.page.xden / 10;
  1600.         pysize = prprefs->PrintMaxHeight * parm.page.yden / 10;
  1601.     }
  1602.     if (prprefs->PrintAspect & ASPECT_VERT)
  1603.     {   xy = pxsize;
  1604.         pxsize = pysize;
  1605.         pysize = xy;
  1606.     }
  1607.     if (pxsize != 0) parm.page.xsize = pxsize;
  1608.     if (pysize != 0) parm.page.ysize = pysize;
  1609.     parm.page.xoff = 0;
  1610.     parm.page.yoff = 0;
  1611. }
  1612.  
  1613. /* Set all requester gadgets */
  1614.  
  1615. void setreqgadgets(void)
  1616. {   setgadgbool(&bwgadg, (parm.page.depth == 1));
  1617.     setgadgbool(&c3gadg, (parm.page.depth == 3));
  1618.     setgadgbool(&c4gadg, (parm.page.depth == 4));
  1619.     setgadgbool(&pd1gadg, (prden == 1));
  1620.     setgadgbool(&pd2gadg, (prden == 2));
  1621.     setgadgbool(&pd3gadg, (prden == 3));
  1622.     setgadgbool(&pd4gadg, (prden == 4));
  1623.     setgadgbool(&pd5gadg, (prden == 5));
  1624.     setgadgbool(&pd6gadg, (prden == 6));
  1625.     setgadgbool(&pd7gadg, (prden == 7));
  1626.     setgadgint(&xgadg, parm.page.xsize);
  1627.     setgadgint(&ygadg, parm.page.ysize);
  1628.     setgadgint(&xogadg, parm.page.xoff);
  1629.     setgadgint(&yogadg, parm.page.yoff);
  1630.     setgadgint(&xdgadg, parm.page.xden);
  1631.     setgadgint(&ydgadg, parm.page.yden);
  1632.     if (reqwindow == NULL)
  1633.     {   setgadgbool(&prigadg, argprint);
  1634.         setgadgbool(&scrgadg, argscreen);
  1635.         setgadgbool(&iffgadg, argiff);
  1636.         setgadgbool(&cwbgadg, argclosewb);
  1637.         setgadgint(&mfgadg, parm.memflen);
  1638.         setgadgint(&mhgadg, parm.memhlen);
  1639.         setgadgint(&mvgadg, parm.memvlen);
  1640.         setgadgint(&mlgadg, parm.memllen);
  1641.     }
  1642.     enablegadg(&pd1gadg, argprint);
  1643.     enablegadg(&pd2gadg, argprint);
  1644.     enablegadg(&pd3gadg, argprint);
  1645.     enablegadg(&pd4gadg, argprint);
  1646.     enablegadg(&pd5gadg, argprint);
  1647.     enablegadg(&pd6gadg, argprint);
  1648.     enablegadg(&pd7gadg, argprint);
  1649. }
  1650.  
  1651. /* Get all requester gadgets */
  1652.  
  1653. void getreqgadgets(void)
  1654. {   argscreen = getgadgbool(&scrgadg);
  1655.     argiff = getgadgbool(&iffgadg);
  1656.     if (argifn[0] == 0) argiff = 0;
  1657.     parm.page.depth = getgadgbool(&bwgadg) ? 1 :
  1658.                       getgadgbool(&c3gadg) ? 3 : 4;
  1659.     argclosewb = getgadgbool(&cwbgadg);
  1660.     parm.page.xsize = getgadgabs(&xgadg);
  1661.     parm.page.ysize = getgadgabs(&ygadg);
  1662.     parm.page.xoff = getgadgint(&xogadg);
  1663.     parm.page.yoff = getgadgint(&yogadg);
  1664.     parm.page.xden = getgadgabs(&xdgadg);
  1665.     parm.page.yden = getgadgabs(&ydgadg);
  1666.     parm.memflen = getgadgabs(&mfgadg);
  1667.     parm.memhlen = getgadgabs(&mhgadg);
  1668.     parm.memvlen = getgadgabs(&mvgadg);
  1669.     parm.memllen = getgadgabs(&mlgadg);
  1670. }
  1671.  
  1672. /* Enable a gadget */
  1673.  
  1674. void enablegadg(struct Gadget *gadget, int enable)
  1675. {   if ((gadget->Flags & GADGDISABLED) != (enable ? 0 : GADGDISABLED))
  1676.     {   if (reqwindow)
  1677.             RemoveGadget(reqwindow, gadget);
  1678.         if (enable)
  1679.             gadget->Flags &= ~GADGDISABLED;
  1680.         else
  1681.             gadget->Flags |=  GADGDISABLED;
  1682.         if (reqwindow)
  1683.         {   AddGadget(reqwindow, gadget, 65535);
  1684.             RefreshGadgets(gadget, reqwindow, NULL);
  1685.         }
  1686.     }
  1687. }
  1688.  
  1689. /* Set the value of a boolean requester gadget */
  1690.  
  1691. void setgadgbool(struct Gadget *gadget, int value)
  1692. {   if (value == getgadgbool(gadget)) return;
  1693.     if (reqwindow)
  1694.         RemoveGadget(reqwindow, gadget);
  1695.     if (value)
  1696.         gadget->Flags |=  SELECTED;
  1697.     else
  1698.         gadget->Flags &= ~SELECTED;
  1699.     if (reqwindow)
  1700.     {   AddGadget(reqwindow, gadget, 65535);
  1701.         RefreshGadgets(gadget, reqwindow, NULL);
  1702.     }
  1703. }
  1704.  
  1705. /* Get the value of a boolean gadget */
  1706.  
  1707. int getgadgbool(struct Gadget *gadget)
  1708. {   return (gadget->Flags & SELECTED) != 0;
  1709. }
  1710.  
  1711. /* Set the value of an integer requester gadget */
  1712.  
  1713. void setgadgint(struct Gadget *gadget, int value)
  1714. {   struct StringInfo *info;
  1715.     if (reqwindow)
  1716.     {   if (value == getgadgint(gadget)) return;
  1717.         RemoveGadget(reqwindow, gadget);
  1718.     }
  1719.     info = (struct StringInfo *) gadget->SpecialInfo;
  1720.     info->LongInt = value;
  1721.     SPrintf((char *) info->Buffer, "%ld", value);
  1722.     if (reqwindow)
  1723.     {   AddGadget(reqwindow, gadget, 65535);
  1724.         RefreshGadgets(gadget, reqwindow, NULL);
  1725.     }
  1726. }
  1727.  
  1728. /* Get the value of an integer gadget */
  1729.  
  1730. int getgadgint(struct Gadget *gadget)
  1731. {   struct StringInfo *info = (struct StringInfo *) gadget->SpecialInfo;
  1732.     return (int) info->LongInt;
  1733. }
  1734.  
  1735. /* Get the absolute value of an integer gadget */
  1736.  
  1737. int getgadgabs(struct Gadget *gadget)
  1738. {   int value = getgadgint(gadget);
  1739.     if (value < 0)
  1740.     {   value = 0;
  1741.         setgadgint(gadget, value);
  1742.     }
  1743.     return value;
  1744. }
  1745.  
  1746. /* String to integer conversion; digits only, with error check */
  1747.  
  1748. int strtoint(char **sp, int *ip)
  1749. {   char *s = *sp;
  1750.     int i = 0;
  1751.     int ch;
  1752.     for (;;)
  1753.     {   ch = *s;
  1754.         if (ch < '0' || ch > '9') break;
  1755.         i = i * 10 + (ch - '0');
  1756.         s++;
  1757.     }
  1758.     if (s == *sp)
  1759.         return 0;
  1760.     else
  1761.     {   *sp = s;
  1762.         *ip = i;
  1763.         return 1;
  1764.     }
  1765. }
  1766.  
  1767. /* Send a mesage to the menu handler task */
  1768.  
  1769. void sendmenu(int action)
  1770. {   if (action != PSACTOPEN)
  1771.     {   menumsg.action = action;
  1772.         menumsg.result = retcode;
  1773.         menumsg.errnum = errcode;
  1774.         PutMsg(menuport, (struct Message *) &menumsg);
  1775.     }
  1776.     WaitPort(mainport);
  1777.     GetMsg(mainport);
  1778.     retcode = menumsg.result;
  1779. }
  1780.  
  1781. /* The menu handler task */
  1782.  
  1783. static int pause = 1;
  1784.  
  1785. void __saveds menuproc(void)
  1786. {   struct Message *msg;
  1787.     char *title, *oldtitle, *savetitle;
  1788.     int height, i, j;
  1789.  
  1790.     title = titlestart;
  1791.     height = newintwindow.Height;
  1792.     msg = (struct Message *) &menumsg;
  1793.  
  1794.     /* Loop handling messages.  We access the initial message directly,
  1795.      * rather than getting it from the menu port, as we havn't created the
  1796.      * port yet */
  1797.  
  1798.     for (;;)
  1799.     {
  1800.         /* Message from main program */
  1801.  
  1802.         if ((struct PSmessage *) msg == &menumsg)
  1803.         {   switch (menumsg.action)
  1804.             {
  1805.                 /* Open the windows and initialise */
  1806.  
  1807.                 case PSACTOPEN:
  1808.                     menuport = CreatePort(NULL, 0);
  1809.                     if (menuport == NULL)
  1810.                     {   menumsg.result = 20;
  1811.                         goto end;
  1812.                     }
  1813.                     bitwindow->UserPort = menuport;
  1814.                     intwindow->UserPort = menuport;
  1815.                     ModifyIDCMP(bitwindow,
  1816.                             REFRESHWINDOW|ACTIVEWINDOW|GADGETUP);
  1817.                     ModifyIDCMP(intwindow,
  1818.                             REFRESHWINDOW|MENUPICK);
  1819.                     menu2.Flags |= MENUENABLED;
  1820.                     SetMenuStrip(intwindow, &menu0);
  1821.                     SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  1822.                     ReplyMsg(msg);
  1823.                     break;
  1824.  
  1825.                 /* Close the windows and tidy up */
  1826.  
  1827.                 case PSACTCLOSE:
  1828.                     ClearMenuStrip(intwindow);
  1829.                     ModifyIDCMP(bitwindow, CLOSEWINDOW);
  1830.                     ModifyIDCMP(intwindow, CLOSEWINDOW);
  1831.                     bitwindow->UserPort = NULL;
  1832.                     intwindow->UserPort = NULL;
  1833.                     for (;;)
  1834.                     {   msg = GetMsg(menuport);
  1835.                         if (msg == NULL) break;
  1836.                         ReplyMsg(msg);
  1837.                     }
  1838.                     DeletePort(menuport);
  1839.                     menuport = NULL;
  1840.                     goto end;
  1841.  
  1842.                 /* Flush the bitmap to its window */
  1843.  
  1844.                 case PSACTFLUSH:
  1845.                     i = menumsg.y1;
  1846.                     j = menumsg.y2;
  1847.                     if (i < winypos) i = winypos;
  1848.                     if (j < winypos) j = winypos;
  1849.                     if (i > winypos + winysize) i = winypos + winysize;
  1850.                     if (j > winypos + winysize) j = winypos + winysize;
  1851.                     if (j > i && argscreen)
  1852.                     {   BltBitMapRastPort(&bitmap, winxpos, i,
  1853.                                           bitwindow->RPort,
  1854.                                           winxbase, winybase + i - winypos,
  1855.                                           winxsize, j - i, 0xC0);
  1856.                     }
  1857.                     ReplyMsg(msg);
  1858.                     break;
  1859.  
  1860.                 /* Pause at the end of a page */
  1861.  
  1862.                 case PSACTPAUSE:
  1863.                     if (pause)
  1864.                     {   savetitle = title;
  1865.                         title = titlepaused;
  1866.                         SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  1867.                         OnMenu(intwindow, 2 | SHIFTITEM(1));
  1868.                     }
  1869.                     else
  1870.                         ReplyMsg(msg);
  1871.                     break;
  1872.  
  1873.                 /* Get the next command */
  1874.  
  1875.                 case PSACTCOMMAND:
  1876.                     title = titlewait;
  1877.                     SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  1878.                     OnMenu(intwindow, 0 | SHIFTITEM(NOITEM));
  1879.                     OnMenu(intwindow, 1 | SHIFTITEM(NOITEM));
  1880.                     break;
  1881.  
  1882.                 /* Get a quit or restart command */
  1883.  
  1884.                 case PSACTEXIT:
  1885.                     title = titlewait;
  1886.                     SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  1887.                     OnMenu(intwindow, 0 | SHIFTITEM(NOITEM));
  1888.                     OffMenu(intwindow, 2 | SHIFTITEM(NOITEM));
  1889.                     break;
  1890.             }
  1891.         }
  1892.  
  1893.         /* Message from Intuition */
  1894.  
  1895.         else
  1896.         {   switch (((struct IntuiMessage *) msg)->Class)
  1897.             {
  1898.                 /* Make the interactive window the active one, but only if
  1899.                  * the scroll gadget knobs in the bitmapped window are not
  1900.                  * currently hit (so as not to prevent dragging them). (If
  1901.                  * someone is dragging them we will get a GADGETUP message
  1902.                  * when he lets go.) */
  1903.  
  1904.                 case ACTIVEWINDOW:
  1905.                     if (!(hscrollgadg.Flags & SELECTED) &&
  1906.                         !(vscrollgadg.Flags & SELECTED))
  1907.                         ActivateWindow(intwindow);
  1908.                     break;
  1909.  
  1910.                 /* Refresh a window */
  1911.  
  1912.                 case REFRESHWINDOW:
  1913.  
  1914.                     /* Refresh the bitmapped window */
  1915.  
  1916.                     if (((struct IntuiMessage *)msg)->IDCMPWindow ==
  1917.                                                                  bitwindow)
  1918.                     {   BeginRefresh(bitwindow);
  1919.                         if (argscreen)
  1920.                             BltBitMapRastPort(&bitmap, winxpos, winypos,
  1921.                                               bitwindow->RPort,
  1922.                                               winxbase, winybase,
  1923.                                               winxsize, winysize, 0xC0);
  1924.                         EndRefresh(bitwindow, TRUE);
  1925.                     }
  1926.  
  1927.                     /* We don't actually refresh the interactive window, but
  1928.                      * instead we use this event to tell us when it has
  1929.                      * changed size, so we can adjust the size and position
  1930.                      * of the bitmapped window to match.  (Despite what the
  1931.                      * manual says we seem to get the refresh message even
  1932.                      * when the window gets smaller */
  1933.  
  1934.                     else
  1935.                     {   i = intwindow->Height;
  1936.                         j = i - height;
  1937.                         height = i;
  1938.                         if      (j < 0)
  1939.                         {   MoveWindow(bitwindow, 0, j);
  1940.                             SizeWindow(bitwindow, 0, -j);
  1941.                         }
  1942.                         else if (j > 0)
  1943.                         {   SizeWindow(bitwindow, 0, -j);
  1944.                             MoveWindow(bitwindow, 0, j);
  1945.                         }
  1946.                         winypos += j;
  1947.                         if (winypos < 0) winypos = 0;
  1948.                         winysize -= j;
  1949.                         ModifyProp(&vscrollgadg, bitwindow, NULL,
  1950.                             AUTOKNOB|FREEVERT,
  1951.                             -1, (0xffff * winypos) /
  1952.                                     (parm.page.ysize - winysize),
  1953.                             -1, (0xffff * winysize) / parm.page.ysize);
  1954.                     }
  1955.  
  1956.                     break;
  1957.  
  1958.                 /* Scroll the bitmapped window.  Make sure we can scroll to
  1959.                  * the edges of the page, even after rounding erors */
  1960.  
  1961.                 case GADGETUP:
  1962.                     ActivateWindow(intwindow);
  1963.                     if      (((struct IntuiMessage *) msg)->IAddress ==
  1964.                                     (APTR) &hscrollgadg)
  1965.                     {   i = (parm.page.xsize - winxsize);
  1966.                         j = (i * hscrollinfo.HorizPot) / 65535;
  1967.                         if (j < i / 30) j = 0;
  1968.                         if (i - j < i / 30) j = i;
  1969.                         winxpos = j;
  1970.                     }
  1971.                     else if (((struct IntuiMessage *) msg)->IAddress ==
  1972.                                     (APTR) &vscrollgadg)
  1973.                     {   i = (parm.page.ysize - winysize);
  1974.                         j = (i * vscrollinfo.VertPot) / 65535;
  1975.                         if (j < i / 30) j = 0;
  1976.                         if (i - j < i / 30) j = i;
  1977.                         winypos = j;
  1978.                     }
  1979.                     if (argscreen)
  1980.                         BltBitMapRastPort(&bitmap, winxpos, winypos,
  1981.                                           bitwindow->RPort,
  1982.                                           winxbase, winybase,
  1983.                                           winxsize, winysize, 0xC0);
  1984.                     break;
  1985.  
  1986.                 /* Menu selection.  We don't implement extended selection
  1987.                  * to avoid real time problems, and it wouldn't be useful
  1988.                  * for our range of choices anyway */
  1989.  
  1990.                 case MENUPICK:
  1991.                     i = ((struct IntuiMessage *) msg)->Code;
  1992.                     j = ITEMNUM(i);
  1993.                     i = MENUNUM(i);
  1994.                     oldtitle = title;
  1995.                     if      (i == 0)     /* Project */
  1996.                     {   if      (j == 0) /*   Restart */
  1997.                             menumsg.command = PSCOMRESTART;
  1998.                         else if (j == 1) /*   Quit */
  1999.                             menumsg.command = PSCOMQUIT;
  2000.                     }
  2001.                     else if (i == 1)     /* File */
  2002.                     {   title = titlerunning;
  2003.                         if      (j == 0) /*   Load font */
  2004.                             menumsg.command = PSCOMFILEF;
  2005.                         else if (j == 1) /*   Load file */
  2006.                             menumsg.command = PSCOMFILEL;
  2007.                         else if (j == 2) /*   Run file */
  2008.                             menumsg.command = PSCOMFILER;
  2009.                         else if (j == 3) /*   Interactive */
  2010.                         {   menumsg.command = PSCOMINTER;
  2011.                             title = titleinter;
  2012.                         }
  2013.                     }
  2014.                     else if (i == 2)     /* Control */
  2015.                     {   if      (j == 0) /*   Pause every page */
  2016.                         {   pause = !pause;
  2017.                             if (pause)
  2018.                                 men2item0.Flags |=  CHECKED;
  2019.                             else
  2020.                                 men2item0.Flags &= ~CHECKED;
  2021.                             break;
  2022.                         }
  2023.                         else if (j == 1) /*   Continue after pause */
  2024.                         {   menumsg.command = 0;
  2025.                             title = savetitle;
  2026.                             OffMenu(intwindow, 2 | SHIFTITEM(1));
  2027.                         }
  2028.                         else if (j == 2) /*   Interrupt */
  2029.                         {   PSsignalint(arec, 1);
  2030.                             break;
  2031.                         }
  2032.                         else if (j == 3) /*   Kill */
  2033.                         {   PSsignalint(arec, 2);
  2034.                             break;
  2035.                         }
  2036.                     }
  2037.                     else                 /* NULL */
  2038.                         break;
  2039.                     OffMenu(intwindow, 0 | SHIFTITEM(NOITEM));
  2040.                     OffMenu(intwindow, 1 | SHIFTITEM(NOITEM));
  2041.                     if (title != oldtitle)
  2042.                         SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  2043.                     menumsg.length = -1;
  2044.                     menumsg.string = NULL;
  2045.                     ReplyMsg((struct Message *) &menumsg);
  2046.                     break;
  2047.             }
  2048.             ReplyMsg(msg);
  2049.         }
  2050.  
  2051.         /* Get next message */
  2052.  
  2053.         for (;;)
  2054.         {   msg = GetMsg(menuport);
  2055.             if (msg) break;
  2056.             WaitPort(menuport);
  2057.         }
  2058.     }
  2059.  
  2060.     /* Open failure or close.  Reply remove our task */
  2061.  
  2062. end:
  2063.     Forbid();
  2064.     ReplyMsg((struct Message *) &menumsg);
  2065.     menutask = NULL;
  2066.     RemTask(menutask);
  2067. }
  2068.  
  2069. /* Signal an interrupt */
  2070.  
  2071. void __saveds sigint()
  2072. {   PSsignalint(arec, 1);
  2073. }
  2074.  
  2075. /* Signal a floating point error */
  2076.  
  2077. void __saveds sigfpe()
  2078. {   PSsignalfpe(arec);
  2079. }
  2080.  
  2081. /* Call an external function (dummy) */
  2082.  
  2083. # ifdef STATICLINK
  2084. int callextfunc(APTR func, APTR aptr, int parms)
  2085. {   return 0;
  2086. }
  2087. # endif
  2088.  
  2089. /* Flush the page to the screen */
  2090.  
  2091. # ifdef STATICLINK
  2092. void flushpage(int y1, int y2)
  2093. # else
  2094. void __saveds __asm flushpage(register __d0 int y1, register __d1 int y2)
  2095. # endif
  2096. {   if (argscreen)
  2097.     {   menumsg.y1 = y1;
  2098.         menumsg.y2 = y2;
  2099.         sendmenu(PSACTFLUSH);
  2100.     }
  2101. }
  2102.  
  2103. /* Copy the page to the output */
  2104.  
  2105. # ifdef STATICLINK
  2106. void copypage(int num)
  2107. # else
  2108. void __saveds __asm copypage(register __d0 int num)
  2109. # endif
  2110. {   ioerror = 0;
  2111.     if (argprint)
  2112.         while (num--) printpage();
  2113.     if (argiff)
  2114.         iffpage();
  2115.     if (argscreen)
  2116.         sendmenu(PSACTPAUSE);
  2117.     if (ioerror) PSerror(arec, ioerror);
  2118. }
  2119.  
  2120. /* Print the page */
  2121.  
  2122. void printpage()
  2123. {   ULONG sig;
  2124.     UWORD prflags;
  2125.  
  2126.     /* Disable break exceptions so we can wait on the signal instead */
  2127.  
  2128.     SetExcept(0, SIGBREAKF_CTRL_C);
  2129.     breakset = 0;
  2130.  
  2131.     /* First check the printer is ready */
  2132.  
  2133.     prreq.io_Command = PRD_QUERY;
  2134.     ((struct IOStdReq *) &prreq)->io_Data = (APTR) prstatus;
  2135.     if (DoIO((struct IORequest *) &prreq))
  2136.     {   ioerror = errioerror;
  2137.         return;
  2138.     }
  2139.     if (((struct IOStdReq *) &prreq)->io_Actual == 1 && prstatus[0] & 3 != 0)
  2140.         FPrintf(argint ? confh : errfh,
  2141.                 "post: printer not ready (CTRL/C to abort)\n");
  2142.  
  2143.     /* Now dump the page */
  2144.  
  2145.     prrast.BitMap = &bitmap;
  2146.     prreq.io_Command = PRD_DUMPRPORT;
  2147.     prreq.io_RastPort = &prrast;
  2148.     prreq.io_ColorMap = (struct ColorMap *) &colormap;
  2149.     prreq.io_Modes = 0;
  2150.     prreq.io_SrcX = 0;
  2151.     prreq.io_SrcY = 0;
  2152.     prreq.io_SrcWidth = parm.page.xsize;
  2153.     prreq.io_SrcHeight = parm.page.ysize;
  2154.     prreq.io_Special = (SPECIAL_DENSITY1 * prden) | SPECIAL_TRUSTME;
  2155.     if (parm.page.ybase + parm.page.ysize >= parm.page.yheight)
  2156.         prreq.io_SrcHeight = parm.page.yheight - parm.page.ybase;
  2157.     else
  2158.         prreq.io_Special |= SPECIAL_NOFORMFEED;
  2159.     if (prextdata->ped_MaxXDots != 0)
  2160.         if (prreq.io_SrcWidth > prextdata->ped_MaxXDots)
  2161.             prreq.io_SrcWidth = prextdata->ped_MaxXDots;
  2162.     if (prextdata->ped_MaxYDots != 0)
  2163.         if (prreq.io_SrcHeight > prextdata->ped_MaxYDots)
  2164.             prreq.io_SrcHeight = prextdata->ped_MaxYDots;
  2165.     prreq.io_DestCols = prreq.io_SrcWidth;
  2166.     prreq.io_DestRows = prreq.io_SrcHeight;
  2167.     prflags = prprefs->PrintFlags;
  2168.     prprefs->PrintFlags = prflags & ~DIMENSIONS_MASK | IGNORE_DIMENSIONS;
  2169.  
  2170.     /* We use asynchronous IO so we can abort it with a CTRL/C */
  2171.  
  2172.     SendIO((struct IORequest *) &prreq);
  2173.  
  2174.     for (;;)
  2175.     {   sig = Wait(prsig | SIGBREAKF_CTRL_C);
  2176.         if (sig & SIGBREAKF_CTRL_C)
  2177.         {   AbortIO((struct IORequest *) &prreq);
  2178.             WaitIO((struct IORequest *) &prreq);
  2179.             ioerror = errioerror;
  2180.             break;
  2181.         }
  2182.         if (GetMsg(prport))
  2183.             break;
  2184.     }
  2185.     if (prreq.io_Error != 0) ioerror = errioerror;
  2186.  
  2187.     /* Restore break exceptions */
  2188.  
  2189.     SetExcept(~0, SIGBREAKF_CTRL_C);
  2190.     breakset = 1;
  2191.  
  2192.     prprefs->PrintFlags = prflags;
  2193. }
  2194.  
  2195. /* IFF ILBM routines */
  2196.  
  2197. static int iffseq = 0;
  2198.  
  2199. static int ifferr;
  2200.  
  2201. static FILE *ifffptr;
  2202.  
  2203. /* Put a byte */
  2204.  
  2205. static void iffputb(int b)
  2206. {   if (ifferr) return;
  2207.     if (putc((int) b, ifffptr) == EOF)
  2208.     {   ifferr = 1;
  2209.         return;
  2210.     }
  2211. }
  2212.  
  2213. /* Put a word */
  2214.  
  2215. static void iffputw(int w)
  2216. {   iffputb(w>>8);
  2217.     iffputb(w);
  2218. }
  2219.  
  2220. /* Put a long */
  2221.  
  2222. static void iffputl(int l)
  2223. {   iffputb(l>>24);
  2224.     iffputb(l>>16);
  2225.     iffputb(l>>8);
  2226.     iffputb(l);
  2227. }
  2228.  
  2229. /* Put a string */
  2230.  
  2231. static void iffputs(char *str)
  2232. {   while (*str) iffputb(*str++);
  2233. }
  2234.  
  2235. /* Pack a bitmap row */
  2236.  
  2237. static void iffpack(char *buf, int len)
  2238. {   int b, c, l;
  2239.     if (ifferr) return;
  2240.     l = 0;
  2241.     while (len--)
  2242.     {   b = *buf++;                  /* Pick up a byte */
  2243.         c = 1;
  2244.         while (len && *buf == b && c < 128)
  2245.         {   c++;
  2246.             buf++;
  2247.             len--;                   /* See if it begins a run */
  2248.         }
  2249.         if (c == 2 &&                /* If a two byte run */
  2250.             l > 0 &&                 /*    and preceeded by literals */
  2251.             l <= 125 &&              /*    and not more than 125 of them */
  2252.             (len <= 2 ||             /*    and no more than 2 bytes left */
  2253.              *buf != *(buf + 1)))    /*        or not followed by a run */
  2254.         {   c = 1;                   /* Then make it a literal */
  2255.             buf--;
  2256.             len++;
  2257.         }
  2258.         if (c == 1)                  /* If not a run */
  2259.         {   l++;                     /* Then it must be a literal */
  2260.             c = 0;
  2261.         }
  2262.         if (l > 0 &&                 /* If we have some literals */
  2263.             (c > 1 ||                /*    and beginning a run */
  2264.              l == 127 ||             /*    or reached 127 */
  2265.              len == 0))              /*    or no more bytes left */
  2266.         {   if (putc(l - 1, ifffptr) == EOF)
  2267.             {   ifferr = 1;
  2268.                 return;
  2269.             }
  2270.             while (l)              /* Then write out  the literals */
  2271.             {   if (putc(*(buf - c - l), ifffptr) == EOF)
  2272.                 {   ifferr = 1;
  2273.                     return;
  2274.                 }
  2275.                 l--;
  2276.             }
  2277.         }
  2278.         if (c > 1)                   /* If we have a run, write it */
  2279.         {   if (putc(1 - c, ifffptr) == EOF)
  2280.             {   ifferr = 1;
  2281.                 return;
  2282.             }
  2283.             if (putc(b, ifffptr) == EOF)
  2284.             {   ifferr = 1;
  2285.                 return;
  2286.             }
  2287.         }
  2288.     }
  2289. }
  2290.  
  2291. /* Determine the current address */
  2292.  
  2293. static long ifftell(void)
  2294. {   long addr;
  2295.     if (ifferr) return 0;
  2296.     if ((addr = ftell(ifffptr)) == -1)
  2297.     {   ifferr = 1;
  2298.         return 0;
  2299.     }
  2300.     return addr;
  2301. }
  2302.  
  2303. /* Fix up the length of a chunk */
  2304.  
  2305. static void ifffixup(long addr)
  2306. {   long size;
  2307.     if (ifferr) return;
  2308.     if ((size = ftell(ifffptr)) == -1)
  2309.     {   ifferr = 1;
  2310.         return;
  2311.     }
  2312.     if (size & 1) iffputb(0);
  2313.     size = size - addr - 8;
  2314.     if (fseek(ifffptr, addr + 4, 0) != 0)
  2315.     {   ifferr = 1;
  2316.         return;
  2317.     }
  2318.     iffputl(size);
  2319.     if (fseek(ifffptr, 0, 2) != 0)
  2320.     {   ifferr = 1;
  2321.         return;
  2322.     }
  2323. }
  2324.  
  2325. /* Write the page to the iff output file */
  2326.  
  2327. void iffpage(void)
  2328. {   long addr1, addr2;
  2329.     int xa, ya;
  2330.     int i, j, k, ch;
  2331.     UWORD w;
  2332.     int fslen;
  2333.     char fname[110], fsnum[10];
  2334.  
  2335.     /* Compute the aspect ratio.  Make sure the values fit into a byte */
  2336.  
  2337.     xa = parm.page.yden;
  2338.     ya = parm.page.xden;
  2339.     i = igcd(xa, ya);
  2340.     xa /= i;
  2341.     ya /= i;
  2342.     while (xa > 255 && ya > 255)
  2343.     {   xa /= 2;
  2344.         ya /= 2;
  2345.     }
  2346.  
  2347.     /* Construct the file name.  Copy it, replacing "*" by the sequence
  2348.      * number.  The scan it backwards replacing "?" by digits */
  2349.  
  2350.     iffseq++;
  2351.     fslen = 0;
  2352.     i = iffseq;
  2353.     while (i)
  2354.     {   fsnum[fslen++] = i % 10 + '0';
  2355.         i /= 10;
  2356.     }
  2357.     i = j = 0;
  2358.     for (;;)
  2359.     {   if (j > 100)
  2360.         {   ioerror = errioerror;
  2361.             return;
  2362.         }
  2363.         ch = argifn[i++];
  2364.         if (ch == '*')
  2365.         {   k = fslen;
  2366.             while (k--) fname[j++] = fsnum[k];
  2367.         }
  2368.         else
  2369.            fname[j++] = ch;
  2370.         if (ch == 0) break;
  2371.     }
  2372.     k = 0;
  2373.     while (--j)
  2374.     {   if (fname[j] == '?')
  2375.             fname[j] = (k < fslen) ? fsnum[k++] : '0';
  2376.     }
  2377.  
  2378.     /* Open the file, write a FORM ILBM, and close it again */
  2379.  
  2380.     ifferr = 0;
  2381.     ifffptr = fopen(fname, "wb");
  2382.     if (ifffptr == NULL) ifferr = 1;
  2383.  
  2384.     addr1 = ifftell();
  2385.     iffputs("FORM");          /* FORM ILBM */
  2386.     iffputl(0);
  2387.     iffputs("ILBM");
  2388.  
  2389.     iffputs("BMHD");          /* BMHD */
  2390.     iffputl(20);
  2391.     iffputw(parm.page.xsize); /* Width */
  2392.     iffputw(parm.page.ysize); /* Height */
  2393.     iffputw(0);               /* X position */
  2394.     iffputw(0);               /* Y position */
  2395.     iffputb(parm.page.depth); /* Number of bit planes */
  2396.     iffputb(0);               /* Masking:     None */
  2397.     iffputb(1);               /* Compression: ByteRun */
  2398.     iffputb(0);               /* Pad1 */
  2399.     iffputw(0);               /* Transparent colour */
  2400.     iffputb(xa);              /* X aspect */
  2401.     iffputb(ya);              /* Y aspect */
  2402.     iffputw(parm.page.xsize); /* Source width */
  2403.     iffputw(parm.page.ysize); /* Source height */
  2404.  
  2405.     addr2 = ifftell();
  2406.     iffputs("CMAP");          /* CMAP */
  2407.     iffputl(0);
  2408.     for (i = 0; i < colormap.Count; i++)
  2409.     {   w = ((UWORD *) colormap.ColorTable)[i];
  2410.         iffputb(((w >> 8) & 15) << 4);
  2411.         iffputb(((w >> 4) & 15) << 4);
  2412.         iffputb(( w       & 15) << 4);
  2413.     }
  2414.     ifffixup(addr2);
  2415.  
  2416.     if (argscreen)
  2417.     {   iffputs("CAMG");      /* CAMG */
  2418.         iffputl(4);
  2419.         iffputl(newscreen.ViewModes);
  2420.     }
  2421.  
  2422.     addr2 = ifftell();
  2423.     iffputs("BODY");          /* BODY */
  2424.     iffputl(0);
  2425.     for (j = 0; j < bitmap.Rows; j++)
  2426.         for (i = 0; i < bitmap.Depth; i++)
  2427.             iffpack((char *) bitmap.Planes[i] + j * bitmap.BytesPerRow,
  2428.                     bitmap.BytesPerRow);
  2429.     ifffixup(addr2);
  2430.  
  2431.     ifffixup(addr1);
  2432.  
  2433.     if (fclose(ifffptr) == EOF)
  2434.         ifferr = 1;
  2435.     if (ifferr) ioerror = errioerror;
  2436. }
  2437.  
  2438. /* Find the greatest common divisor of two positive integers.  If one is
  2439.  * zero the result is the other */
  2440.  
  2441. int igcd(int n, int m)
  2442. {   unsigned int n1, m1, r;
  2443.     if      (n > m)
  2444.     {   n1 = n;
  2445.         m1 = m;
  2446.     }
  2447.     else if (m > n)
  2448.     {   n1 = m;
  2449.         m1 = n;
  2450.     }
  2451.     else
  2452.         return n;
  2453.     while (m1 != 0)
  2454.     {   r = n1 % m1;
  2455.         n1 = m1;
  2456.         m1 = r;
  2457.     }
  2458.     return (int) n1;
  2459. }
  2460.  
  2461. /* End of file "post.c" */
  2462.