home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / xdvik / xdvi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-28  |  49.0 KB  |  1,641 lines

  1. /*
  2.  * Copyright (c) 1994 Paul Vojta.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 1. Redistributions of source code must retain the above copyright
  8.  *    notice, this list of conditions and the following disclaimer.
  9.  * 2. Redistributions in binary form must reproduce the above copyright
  10.  *    notice, this list of conditions and the following disclaimer in the
  11.  *    documentation and/or other materials provided with the distribution.
  12.  *
  13.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23.  * SUCH DAMAGE.
  24.  *
  25.  * NOTE:
  26.  *    xdvi is based on prior work as noted in the modification history, below.
  27.  */
  28.  
  29. /*
  30.  * DVI previewer for X.
  31.  *
  32.  * Eric Cooper, CMU, September 1985.
  33.  *
  34.  * Code derived from dvi-imagen.c.
  35.  *
  36.  * Modification history:
  37.  * 1/1986    Modified for X.10    --Bob Scheifler, MIT LCS.
  38.  * 7/1988    Modified for X.11    --Mark Eichin, MIT
  39.  * 12/1988    Added 'R' option, toolkit, magnifying glass
  40.  *                    --Paul Vojta, UC Berkeley.
  41.  * 2/1989    Added tpic support    --Jeffrey Lee, U of Toronto
  42.  * 4/1989    Modified for System V    --Donald Richardson, Clarkson Univ.
  43.  * 3/1990    Added VMS support    --Scott Allendorf, U of Iowa
  44.  * 7/1990    Added reflection mode    --Michael Pak, Hebrew U of Jerusalem
  45.  * 1/1992    Added greyscale code    --Till Brychcy, Techn. Univ. Muenchen
  46.  *                      and Lee Hetherington, MIT
  47.  * 4/1994    Added DPS support, bounding box
  48.  *                    --Ricardo Telichevesky
  49.  *                      and Luis Miguel Silveira, MIT RLE.
  50.  *
  51.  *    Compilation options:
  52.  *    SYSV    compile for System V
  53.  *    VMS    compile for VMS
  54.  *    NOTOOL    compile without toolkit (X11 only)
  55.  *    BUTTONS    compile with buttons on the side of the window (needs toolkit)
  56.  *    MSBITFIRST    store bitmaps internally with most significant bit first
  57.  *    BMSHORT    store bitmaps in shorts instead of bytes
  58.  *    BMLONG    store bitmaps in longs instead of bytes
  59.  *    ALTFONT    default for -altfont option
  60.  *    A4    use European size paper
  61.  *    TEXXET    support reflection dvi codes (right-to-left typesetting)
  62.  *    GREY    use grey levels to shrink fonts
  63.  *    PS_DPS    use display postscript to render pictures/bounding boxes
  64.  *    PS_NEWS    use the NeWS server to render pictures/bounding boxes
  65.  *    PS_GS    use GhostScript to render pictures/bounding boxes
  66.  */
  67.  
  68. #if 0
  69. static    char    copyright[] =
  70. "@(#) Copyright (c) 1994 Paul Vojta.  All rights reserved.\n";
  71. #endif
  72.  
  73. #ifndef    ALTFONT
  74. #define    ALTFONT    "cmr10"
  75. #endif
  76.  
  77. #ifndef    A4
  78. #define    DEFAULT_PAPER        "us"
  79. #else
  80. #define    DEFAULT_PAPER        "a4"
  81. #endif
  82.  
  83. #define    EXTERN
  84. #define    INIT(x)    =x
  85.  
  86. #include "config.h"
  87. #include "c-openmx.h"
  88. #include <kpathsea/c-ctype.h>
  89. #include <kpathsea/c-fopen.h>
  90. #include <kpathsea/c-pathch.h>
  91. #include <kpathsea/proginit.h>
  92. #include <kpathsea/progname.h>
  93. #include <kpathsea/tex-file.h>
  94.  
  95. #include "patchlevel.h"
  96.  
  97. /* Xlib and Xutil are already included */
  98. #include <X11/cursorfont.h>
  99. #include <X11/keysym.h>
  100. #include "xdvi.icon"
  101.  
  102. #ifdef    TOOLKIT
  103. #ifdef    OLD_X11_TOOLKIT
  104. #include <X11/Atoms.h>
  105. #else /* not OLD_X11_TOOLKIT */
  106. #include <X11/Xatom.h>
  107. #include <X11/StringDefs.h>
  108. #endif /* not OLD_X11_TOOLKIT */
  109. #include <X11/Shell.h>    /* needed for def. of XtNiconX */
  110. #ifndef    XtSpecificationRelease
  111. #define    XtSpecificationRelease    0
  112. #endif
  113. #if    XtSpecificationRelease >= 4
  114. #include <X11/Xaw/Viewport.h>
  115. #ifdef    BUTTONS
  116. #include <X11/Xaw/Command.h>
  117. #endif
  118. #else    /* XtSpecificationRelease < 4 */
  119. #define    XtPointer caddr_t
  120. #include <X11/Viewport.h>
  121. #ifdef    BUTTONS
  122. #include <X11/Command.h>
  123. #endif
  124. #endif    /* XtSpecificationRelease */
  125. #else    /* !TOOLKIT */
  126. typedef    int        Position;
  127. #endif    /* TOOLKIT */
  128.  
  129. #ifdef    VMS
  130. /*
  131.  * Magnifying glass cursor
  132.  *
  133.  * Developed by Tom Sawyer, April 1990
  134.  * Contibuted by Hunter Goatley, January 1991
  135.  *
  136.  */
  137.  
  138. #define mag_glass_width 16
  139. #define mag_glass_height 16
  140. #define mag_glass_x_hot 6
  141. #define mag_glass_y_hot 6
  142. static char mag_glass_bits[] = {
  143.     0xf8, 0x03, 0x0c, 0x06, 0xe2, 0x09, 0x13, 0x1a, 0x01, 0x14, 0x01, 0x14,
  144.     0x01, 0x10, 0x01, 0x10, 0x01, 0x10, 0x03, 0x10, 0x02, 0x18, 0x0c, 0x34,
  145.     0xf8, 0x6f, 0x00, 0xd8, 0x00, 0xb0, 0x00, 0xe0
  146. };
  147. #include <decw$cursor.h>    /* Include the DECWindows cursor symbols */
  148. static    int    DECWCursorFont;    /* Space for the DECWindows cursor font  */
  149. static    Pixmap    MagnifyPixmap;    /* Pixmap to hold our special mag-glass  */
  150. #include <X11/Xresource.h>      /* Motif apparently needs this one */
  151. #endif    /* VMS */
  152.  
  153. /*
  154.  * Command line flags.
  155.  */
  156.  
  157. static    Dimension    bwidth    = 2;
  158.  
  159. #define    fore_Pixel    resource._fore_Pixel
  160. #define    back_Pixel    resource._back_Pixel
  161. #ifdef    TOOLKIT
  162. struct _resource    resource;
  163. #define    brdr_Pixel    resource._brdr_Pixel
  164. #define    hl_Pixel    resource._hl_Pixel
  165. #define    cr_Pixel    resource._cr_Pixel
  166. #else    /* TOOLKIT */
  167. static    Pixel        hl_Pixel, cr_Pixel;
  168. #endif    /* TOOLKIT */
  169.  
  170. struct    mg_size_rec    mg_size[5]    = {{200, 150}, {400, 250}, {700, 500},
  171.                        {1000, 800}, {1200, 1200}};
  172.  
  173. static    char    *curr_page;
  174.  
  175. struct WindowRec mane    = {(Window) 0, 3, 0, 0, 0, 0, MAXDIM, 0, MAXDIM, 0};
  176. struct WindowRec alt    = {(Window) 0, 1, 0, 0, 0, 0, MAXDIM, 0, MAXDIM, 0};
  177. /*    currwin is temporary storage except for within redraw() */
  178. struct WindowRec currwin = {(Window) 0, 3, 0, 0, 0, 0, MAXDIM, 0, MAXDIM, 0};
  179.  
  180. #ifdef    lint
  181. #ifdef    TOOLKIT
  182. WidgetClass    viewportWidgetClass, widgetClass;
  183. #ifdef    BUTTONS
  184. WidgetClass    formWidgetClass, compositeWidgetClass, commandWidgetClass;
  185. #endif    /* BUTTONS */
  186. #endif    /* TOOLKIT */
  187. #endif    /* lint */
  188.  
  189. /*
  190.  *    Data for options processing
  191.  */
  192.  
  193. static    _Xconst    char    silent[] = "";    /* flag value for usage() */
  194.  
  195. static    _Xconst    char    subst[]    = "x";    /* another flag value */
  196.  
  197. static    _Xconst char    *subst_val[] = {"-mgs[n] <size>"};
  198.  
  199. #ifdef    TOOLKIT
  200.  
  201. static    XrmOptionDescRec    options[] = {
  202. {"-d",        ".debugLevel",    XrmoptionSepArg,    (caddr_t) NULL},
  203. {"-s",        ".shrinkFactor", XrmoptionSepArg,    (caddr_t) NULL},
  204. #ifndef    VMS
  205. {"-S",        ".densityPercent", XrmoptionSepArg,    (caddr_t) NULL},
  206. #endif
  207. {"-density",    ".densityPercent", XrmoptionSepArg,    (caddr_t) NULL},
  208. #ifdef    GREY
  209. {"-nogrey",    ".grey",    XrmoptionNoArg,        (caddr_t) "off"},
  210. {"+nogrey",    ".grey",    XrmoptionNoArg,        (caddr_t) "on"},
  211. {"-gamma",    ".gamma",    XrmoptionSepArg,    (caddr_t) NULL},
  212. #endif
  213. {"-p",        ".pixelsPerInch", XrmoptionSepArg,    (caddr_t) NULL},
  214. {"-margins",    ".Margin",    XrmoptionSepArg,    (caddr_t) NULL},
  215. {"-sidemargin",    ".sideMargin",    XrmoptionSepArg,    (caddr_t) NULL},
  216. {"-topmargin",    ".topMargin",    XrmoptionSepArg,    (caddr_t) NULL},
  217. {"-offsets",    ".Offset",    XrmoptionSepArg,    (caddr_t) NULL},
  218. {"-xoffset",    ".xOffset",    XrmoptionSepArg,    (caddr_t) NULL},
  219. {"-yoffset",    ".yOffset",    XrmoptionSepArg,    (caddr_t) NULL},
  220. {"-paper",    ".paper",    XrmoptionSepArg,    (caddr_t) NULL},
  221. {"-altfont",    ".altFont",    XrmoptionSepArg,    (caddr_t) NULL},
  222. {"-l",        ".listFonts",    XrmoptionNoArg,        (caddr_t) "on"},
  223. {"+l",        ".listFonts",    XrmoptionNoArg,        (caddr_t) "off"},
  224. #ifdef    BUTTONS
  225. {"-expert",    ".expert",    XrmoptionNoArg,        (caddr_t) "on"},
  226. {"+expert",    ".expert",    XrmoptionNoArg,        (caddr_t) "off"},
  227. #endif
  228. {"-mgs",    ".magnifierSize1",XrmoptionSepArg,    (caddr_t) NULL},
  229. {"-mgs1",    ".magnifierSize1",XrmoptionSepArg,    (caddr_t) NULL},
  230. {"-mgs2",    ".magnifierSize2",XrmoptionSepArg,    (caddr_t) NULL},
  231. {"-mgs3",    ".magnifierSize3",XrmoptionSepArg,    (caddr_t) NULL},
  232. {"-mgs4",    ".magnifierSize4",XrmoptionSepArg,    (caddr_t) NULL},
  233. {"-mgs5",    ".magnifierSize5",XrmoptionSepArg,    (caddr_t) NULL},
  234. {"-hush",    ".Hush",    XrmoptionNoArg,        (caddr_t) "on"},
  235. {"+hush",    ".Hush",    XrmoptionNoArg,        (caddr_t) "off"},
  236. {"-hushspecials", ".hushSpecials", XrmoptionNoArg,    (caddr_t) "on"},
  237. {"+hushspecials", ".hushSpecials", XrmoptionNoArg,    (caddr_t) "off"},
  238. {"-hushchars",    ".hushLostChars", XrmoptionNoArg,    (caddr_t) "on"},
  239. {"+hushchars",    ".hushLostChars", XrmoptionNoArg,    (caddr_t) "off"},
  240. {"-hushchecksums", ".hushChecksums", XrmoptionNoArg,    (caddr_t) "on"},
  241. {"+hushchecksums", ".hushChecksums", XrmoptionNoArg,    (caddr_t) "off"},
  242. {"-fg",        ".foreground",    XrmoptionSepArg,    (caddr_t) NULL},
  243. {"-foreground",    ".foreground",    XrmoptionSepArg,    (caddr_t) NULL},
  244. {"-bg",        ".background",    XrmoptionSepArg,    (caddr_t) NULL},
  245. {"-background",    ".background",    XrmoptionSepArg,    (caddr_t) NULL},
  246. {"-hl",        ".highlight",    XrmoptionSepArg,    (caddr_t) NULL},
  247. {"-cr",        ".cursorColor",    XrmoptionSepArg,    (caddr_t) NULL},
  248. {"-icongeometry",".iconGeometry",XrmoptionSepArg,    (caddr_t) NULL},
  249. {"-keep",    ".keepPosition",XrmoptionNoArg,        (caddr_t) "on"},
  250. {"+keep",    ".keepPosition",XrmoptionNoArg,        (caddr_t) "off"},
  251. {"-copy",    ".copy",    XrmoptionNoArg,        (caddr_t) "on"},
  252. {"+copy",    ".copy",    XrmoptionNoArg,        (caddr_t) "off"},
  253. {"-thorough",    ".thorough",    XrmoptionNoArg,        (caddr_t) "on"},
  254. {"+thorough",    ".thorough",    XrmoptionNoArg,        (caddr_t) "off"},
  255. #if    PS
  256. {"-nopostscript",".postscript",    XrmoptionNoArg,        (caddr_t) "off"},
  257. {"+nopostscript",".postscript",    XrmoptionNoArg,        (caddr_t) "on"},
  258. #ifdef    PS_DPS
  259. {"-nodps",    ".dps",        XrmoptionNoArg,        (caddr_t) "off"},
  260. {"+nodps",    ".dps",        XrmoptionNoArg,        (caddr_t) "on"},
  261. #endif
  262. #ifdef    PS_NEWS
  263. {"-nonews",    ".news",    XrmoptionNoArg,        (caddr_t) "off"},
  264. {"+nonews",    ".news",    XrmoptionNoArg,        (caddr_t) "on"},
  265. #endif
  266. #ifdef    PS_GS
  267. {"-noghostscript",".ghostscript", XrmoptionNoArg,    (caddr_t) "off"},
  268. {"+noghostscript",".ghostscript", XrmoptionNoArg,    (caddr_t) "on"},
  269. #endif
  270. #endif    /* PS */
  271. {"-version",    ".version",    XrmoptionNoArg,        (caddr_t) "on"},
  272. {"+version",    ".version",    XrmoptionNoArg,        (caddr_t) "off"},
  273. {"-maketexpk",    ".maketexpk",    XrmoptionNoArg,        (caddr_t) "off"},
  274. {"+maketexpk",    ".maketexpk",    XrmoptionNoArg,        (caddr_t) "on"},
  275. {"-mfmode",    ".mfmode",    XrmoptionSepArg,    (caddr_t) "cx"},
  276. };
  277.  
  278. #define    offset(field)    XtOffsetOf(struct _resource, field)
  279.  
  280. static    int    basedpi    = BDPI;        /* default value for -p option */
  281.  
  282. static    XtResource    application_resources[] = {
  283. {"debugLevel", "DebugLevel", XtRString, sizeof(char *),
  284.   offset(debug_arg), XtRString, (caddr_t) NULL},
  285. {"shrinkFactor", "ShrinkFactor", XtRInt, sizeof(int),
  286.   offset(shrinkfactor), XtRString, "3"},
  287. {"densityPercent", "DensityPercent", XtRInt, sizeof(int),
  288.   offset(_density), XtRString, "40"},
  289. #ifdef    GREY
  290. {"gamma", "Gamma", XtRFloat, sizeof(float),
  291.   offset(_gamma), XtRString, "1"},
  292. #endif
  293. {"pixelsPerInch", "PixelsPerInch", XtRInt, sizeof(int),
  294.   offset(_pixels_per_inch), XtRInt, (caddr_t) &basedpi},
  295. {"sideMargin", "Margin", XtRString, sizeof(char *),
  296.   offset(sidemargin), XtRString, (caddr_t) NULL},
  297. {"topMargin", "Margin", XtRString, sizeof(char *),
  298.   offset(topmargin), XtRString, (caddr_t) NULL},
  299. {"xOffset", "Offset", XtRString, sizeof(char *),
  300.   offset(xoffset), XtRString, (caddr_t) NULL},
  301. {"yOffset", "Offset", XtRString, sizeof(char *),
  302.   offset(yoffset), XtRString, (caddr_t) NULL},
  303. {"paper", "Paper", XtRString, sizeof(char *),
  304.   offset(paper), XtRString, (caddr_t) DEFAULT_PAPER},
  305. {"altFont", "AltFont", XtRString, sizeof(char *),
  306.   offset(_alt_font), XtRString, (caddr_t) ALTFONT},
  307. {"listFonts", "ListFonts", XtRBoolean, sizeof(Boolean),
  308.   offset(_list_fonts), XtRString, "false"},
  309. {"reverseVideo", "ReverseVideo", XtRBoolean, sizeof(Boolean),
  310.   offset(reverse), XtRString, "false"},
  311. {"hushSpecials", "Hush", XtRBoolean, sizeof(Boolean),
  312.   offset(_hush_spec), XtRString, "false"},
  313. {"hushLostChars", "Hush", XtRBoolean, sizeof(Boolean),
  314.   offset(_hush_chars), XtRString, "false"},
  315. {"hushChecksums", "Hush", XtRBoolean, sizeof(Boolean),
  316.   offset(_hush_chk), XtRString, "false"},
  317. {"foreground", "Foreground", XtRPixel, sizeof(Pixel),
  318.   offset(_fore_Pixel), XtRPixel, (caddr_t) &resource._fore_Pixel},
  319. {"foreground", "Foreground", XtRString, sizeof(char *),
  320.   offset(fore_color), XtRString, (caddr_t) NULL},
  321. {"background", "Background", XtRPixel, sizeof(Pixel),
  322.   offset(_back_Pixel), XtRPixel, (caddr_t) &resource._back_Pixel},
  323. {"background", "Background", XtRString, sizeof(char *),
  324.   offset(back_color), XtRString, (caddr_t) NULL},
  325. {"borderColor", "BorderColor", XtRPixel, sizeof(Pixel),
  326.   offset(_brdr_Pixel), XtRPixel, (caddr_t) &resource._brdr_Pixel},
  327. {"borderColor", "BorderColor", XtRString, sizeof(char *),
  328.   offset(brdr_color), XtRString, (caddr_t) NULL},
  329. {"highlight", "Highlight", XtRPixel, sizeof(Pixel),
  330.   offset(_hl_Pixel), XtRPixel, (caddr_t) &resource._hl_Pixel},
  331. {"highlight", "Highlight", XtRString, sizeof(char *),
  332.   offset(high_color), XtRString, (caddr_t) NULL},
  333. {"cursorColor", "CursorColor", XtRPixel, sizeof(Pixel),
  334.   offset(_cr_Pixel), XtRPixel, (caddr_t) &resource._cr_Pixel},
  335. {"cursorColor", "CursorColor", XtRString, sizeof(char *),
  336.   offset(curs_color), XtRString, (caddr_t) NULL},
  337. {"iconGeometry", "IconGeometry", XtRString, sizeof(char *),
  338.   offset(icon_geometry), XtRString, (caddr_t) NULL},
  339. {"keepPosition", "KeepPosition", XtRBoolean, sizeof(Boolean),
  340.   offset(keep_flag), XtRString, "false"},
  341. #if    PS
  342. {"postscript", "Postscript", XtRBoolean, sizeof(Boolean),
  343.   offset(_postscript), XtRString, "true"},
  344. #ifdef    PS_DPS
  345. {"dps", "DPS", XtRBoolean, sizeof(Boolean),
  346.   offset(useDPS), XtRString, "true"},
  347. #endif
  348. #ifdef    PS_NEWS
  349. {"news", "News", XtRBoolean, sizeof(Boolean),
  350.   offset(useNeWS), XtRString, "true"},
  351. #endif
  352. #ifdef    PS_GS
  353. {"ghostscript", "Ghostscript", XtRBoolean, sizeof(Boolean),
  354.   offset(useGS), XtRString, "true"},
  355. #endif
  356. #endif    /* PS */
  357. {"copy", "Copy", XtRString, sizeof(char *),
  358.   offset(copy_arg), XtRString, (caddr_t) NULL},
  359. {"copy", "Copy", XtRBoolean, sizeof(Boolean),
  360.   offset(copy), XtRString, "false"},
  361. {"thorough", "Thorough", XtRBoolean, sizeof(Boolean),
  362.   offset(thorough), XtRString, "false"},
  363. {"version", "Version", XtRBoolean, sizeof(Boolean),
  364.   offset(version_flag), XtRString, "false"},
  365. #ifdef    BUTTONS
  366. {"expert", "Expert", XtRBoolean, sizeof(Boolean),
  367.   offset(expert), XtRString, "false"},
  368. #endif
  369. {"magnifierSize1", "MagnifierSize", XtRString, sizeof(char *),
  370.   offset(mg_arg[0]), XtRString, (caddr_t) NULL},
  371. {"magnifierSize2", "MagnifierSize", XtRString, sizeof(char *),
  372.   offset(mg_arg[1]), XtRString, (caddr_t) NULL},
  373. {"magnifierSize3", "MagnifierSize", XtRString, sizeof(char *),
  374.   offset(mg_arg[2]), XtRString, (caddr_t) NULL},
  375. {"magnifierSize4", "MagnifierSize", XtRString, sizeof(char *),
  376.   offset(mg_arg[3]), XtRString, (caddr_t) NULL},
  377. {"magnifierSize5", "MagnifierSize", XtRString, sizeof(char *),
  378.   offset(mg_arg[4]), XtRString, (caddr_t) NULL},
  379. #ifdef    GREY
  380. {"grey", "Grey", XtRBoolean, sizeof (Boolean),
  381.  offset(_use_grey), XtRString, "true"},
  382. #endif
  383. {"maketexpk", "MakeTeXPK", XtRBoolean, sizeof (Boolean),
  384.  offset(_use_makepk), XtRString, MAKEPK_BY_DEFAULT_STRING},
  385. {"mfmode", "MFmode", XtRString, sizeof (char *),
  386.  offset(_mf_mode), XtRString, NULL},
  387. };
  388.  
  389. #undef    offset
  390.  
  391. static    _Xconst    char    *usagestr[] = {
  392.     /* d */            "debugnum",
  393.     /* shrinkFactor */    "shrink",
  394. #ifndef    VMS
  395.     /* S */            "density",
  396.     /* density */        silent,
  397. #else
  398.     /* density */        "density",
  399. #endif
  400. #ifdef    GREY
  401.     /* gamma */        "g",
  402. #endif
  403.     /* p */            "pixels",
  404.     /* margins */        "dimen",
  405.     /* sidemargin */    "dimen",
  406.     /* topmargin */        "dimen",
  407.     /* offsets */        "dimen",
  408.     /* xoffset */        "dimen",
  409.     /* yoffset */        "dimen",
  410.     /* paper */        "papertype",
  411.     /* altfont */        "font",
  412.     /* rv */        "^-l", "-rv",
  413.     /* mgs */        subst,
  414.     /* msg1 */        silent,
  415.     /* msg2 */        silent,
  416.     /* msg3 */        silent,
  417.     /* msg4 */        silent,
  418.     /* msg5 */        silent,
  419.     /* fg */        "color",
  420.     /* foreground */    silent,
  421.     /* bg */        "color",
  422.     /* background */    silent,
  423.     /* hl */        "color",
  424.     /* bd */        "^-hl", "-bd <color>",
  425.     /* cr */        "color",
  426.     /* bw */        "^-cr", "-bw <width>",
  427. #ifndef VMS
  428.     /* display */        "^-cr", "-display <host:display>",
  429. #else
  430.     /* display */        "^-cr", "-display <host::display>",
  431. #endif
  432.     /* geometry */        "^-cr", "-geometry <geometry>",
  433.     /* icongeometry */    "geometry",
  434.     /* iconic */        "^-icongeometry", "-iconic",
  435.     /* mfmode */        "string",
  436. };
  437.  
  438. #ifdef    NOQUERY
  439. #define    drawWidgetClass    widgetClass
  440. #else
  441.  
  442. /* ARGSUSED */
  443. static    XtGeometryResult
  444. QueryGeometry(w, constraints, reply)
  445.     Widget    w;
  446.     XtWidgetGeometry *constraints, *reply;
  447. {
  448.     reply->request_mode = CWWidth | CWHeight;
  449.     reply->width = page_w;
  450.     reply->height = page_h;
  451.     return XtGeometryAlmost;
  452. }
  453.  
  454. #include <X11/IntrinsicP.h>
  455. #include <X11/CoreP.h>
  456.  
  457. #ifdef    lint
  458. WidgetClassRec    widgetClassRec;
  459. #endif
  460.  
  461.     /* if the following gives you trouble, just compile with -DNOQUERY */
  462. static    WidgetClassRec    drawingWidgetClass = {
  463.   {
  464.     /* superclass         */    &widgetClassRec,
  465.     /* class_name         */    "Draw",
  466.     /* widget_size        */    sizeof(WidgetRec),
  467.     /* class_initialize   */    NULL,
  468.     /* class_part_initialize*/  NULL,
  469.     /* class_inited       */    FALSE,
  470.     /* initialize         */    NULL,
  471.     /* initialize_hook    */    NULL,
  472.     /* realize            */    XtInheritRealize,
  473.     /* actions            */    NULL,
  474.     /* num_actions        */    0,
  475.     /* resources          */    NULL,
  476.     /* num_resources      */    0,
  477.     /* xrm_class          */    NULLQUARK,
  478.     /* compress_motion    */    FALSE,
  479.     /* compress_exposure  */    TRUE,
  480.     /* compress_enterleave*/    FALSE,
  481.     /* visible_interest   */    FALSE,
  482.     /* destroy            */    NULL,
  483.     /* resize             */    XtInheritResize,
  484.     /* expose             */    XtInheritExpose,
  485.     /* set_values         */    NULL,
  486.     /* set_values_hook    */    NULL,
  487.     /* set_values_almost  */    XtInheritSetValuesAlmost,
  488.     /* get_values_hook    */    NULL,
  489.     /* accept_focus       */    XtInheritAcceptFocus,
  490.     /* version            */    XtVersion,
  491.     /* callback_offsets   */    NULL,
  492.     /* tm_table           */    XtInheritTranslations,
  493.     /* query_geometry       */  QueryGeometry,
  494.     /* display_accelerator  */  XtInheritDisplayAccelerator,
  495.     /* extension            */  NULL
  496.   }
  497. };
  498.  
  499. #define    drawWidgetClass    &drawingWidgetClass
  500.  
  501. #endif    /* NOQUERY */
  502.  
  503. static    Arg    vport_args[] = {
  504. #ifdef    BUTTONS
  505.     {XtNborderWidth, (XtArgVal) 0},
  506.     {XtNtop,    (XtArgVal) XtChainTop},
  507.     {XtNbottom,    (XtArgVal) XtChainBottom},
  508.     {XtNleft,    (XtArgVal) XtChainLeft},
  509.     {XtNright,    (XtArgVal) XtChainRight},
  510. #endif
  511.     {XtNallowHoriz,    (XtArgVal) True},
  512.     {XtNallowVert,    (XtArgVal) True},
  513. };
  514.  
  515. static    Arg    draw_args[] = {
  516.     {XtNwidth,    (XtArgVal) 0},
  517.     {XtNheight,    (XtArgVal) 0},
  518. #ifdef    GREY
  519.     {XtNbackground,    (XtArgVal) 0},
  520. #endif
  521.     {XtNx,        (XtArgVal) 0},
  522.     {XtNy,        (XtArgVal) 0},
  523.     {XtNlabel,    (XtArgVal) ""},
  524. };
  525.  
  526. #ifdef    BUTTONS
  527. static    Arg    form_args[] = {
  528.     {XtNdefaultDistance, (XtArgVal) 0},
  529. };
  530. #endif
  531.  
  532. #else    /* !TOOLKIT */
  533.  
  534. static    char    *display;
  535. static    char    *geometry;
  536. static    char    *margins;
  537. static    char    *offsets;
  538. static    Boolean    hush;
  539. static    Boolean    iconic    = False;
  540.  
  541. #define    ADDR(x)    (caddr_t) &resource.x
  542.  
  543. static    struct option {
  544.     _Xconst    char    *name;
  545.     _Xconst    char    *resource;
  546.     enum {FalseArg, TrueArg, StickyArg, SepArg}
  547.             argclass;
  548.     enum {BooleanArg, StringArg, NumberArg, FloatArg}
  549.             argtype;
  550.     int        classcount;
  551.     _Xconst    char    *usagestr;
  552.     caddr_t        address;
  553. }    options[] = {
  554. {"+",        NULL,        StickyArg, StringArg, 1,
  555.   NULL,        (caddr_t) &curr_page},
  556. {"-s",        "shrinkFactor", SepArg, NumberArg, 1,
  557.   "shrink",    (caddr_t)&shrink_factor},
  558. #ifndef VMS
  559. {"-S",        NULL,        SepArg, NumberArg, 2,
  560.   "density",    ADDR(_density)},
  561. {"-density",    "densityPercent", SepArg, NumberArg, 1,
  562.   silent,    ADDR(_density)},
  563. #else
  564. {"-density",    "densityPercent", SepArg, NumberArg, 1,
  565.   "density",    ADDR(_density)},
  566. #endif
  567. #ifdef    GREY
  568. {"-nogrey",    NULL,        FalseArg, BooleanArg, 2,
  569.   NULL,        ADDR(_use_grey)},
  570. {"+nogrey",    "grey",        TrueArg, BooleanArg, 1,
  571.   NULL,        ADDR(_use_grey)},
  572. {"-gamma",    "gamma",    SepArg,    FloatArg, 1,
  573.   "g",        ADDR(_gamma)},
  574. #endif
  575. {"-p",        "pixelsPerInch", SepArg, NumberArg, 1,
  576.   "pixels",    ADDR(_pixels_per_inch)},
  577. {"-margins",    "Margin",    SepArg,    StringArg, 3,
  578.   "dimen",    (caddr_t) &margins},
  579. {"-sidemargin",    "sideMargin",    SepArg,    StringArg, 1,
  580.   "dimen",    ADDR(sidemargin)},
  581. {"-topmargin",    "topMargin",    SepArg,    StringArg, 1,
  582.   "dimen",    ADDR(topmargin)},
  583. {"-offsets",    "Offset",    SepArg,    StringArg, 3,
  584.   "dimen",    (caddr_t) &offsets},
  585. {"-xoffset",    "xOffset",    SepArg,    StringArg, 1,
  586.   "dimen",    ADDR(xoffset)},
  587. {"-yoffset",    "yOffset",    SepArg,    StringArg, 1,
  588.   "dimen",    ADDR(yoffset)},
  589. {"-paper",    "paper",    SepArg,    StringArg, 1,
  590.   "papertype",    ADDR(paper)},
  591. {"-altfont",    "altFont",    SepArg,    StringArg, 1,
  592.   "font",    ADDR(_alt_font)},
  593. {"-l",        NULL,        TrueArg, BooleanArg, 2,
  594.   NULL,        ADDR(_list_fonts)},
  595. {"+l",        "listFonts",    FalseArg, BooleanArg, 1,
  596.   NULL,        ADDR(_list_fonts)},
  597. {"-rv",        NULL,        TrueArg, BooleanArg, 2,
  598.   NULL,        ADDR(reverse)},
  599. {"+rv",        "reverseVideo",    FalseArg, BooleanArg, 1,
  600.   NULL,        ADDR(reverse)},
  601. {"-mgs",    NULL,        SepArg, StringArg, 2,
  602.   subst,    ADDR(mg_arg[0])},
  603. {"-mgs1",    "magnifierSize1",SepArg, StringArg, 1,
  604.   silent,    ADDR(mg_arg[0])},
  605. {"-mgs2",    "magnifierSize2",SepArg, StringArg, 1,
  606.   silent,    ADDR(mg_arg[1])},
  607. {"-mgs3",    "magnifierSize3",SepArg, StringArg, 1,
  608.   silent,    ADDR(mg_arg[2])},
  609. {"-mgs4",    "magnifierSize4",SepArg, StringArg, 1,
  610.   silent,    ADDR(mg_arg[3])},
  611. {"-mgs5",    "magnifierSize5",SepArg, StringArg, 1,
  612.   silent,    ADDR(mg_arg[4])},
  613. {"-hush",    NULL,        TrueArg, BooleanArg, 6,
  614.   NULL,        (caddr_t) &hush},
  615. {"+hush",    "Hush",        FalseArg, BooleanArg, 5,
  616.   NULL,        (caddr_t) &hush},
  617. {"-hushspecials", NULL,        TrueArg, BooleanArg, 2,
  618.   NULL,        ADDR(_hush_spec)},
  619. {"+hushspecials", "hushSpecials", FalseArg, BooleanArg, 1,
  620.   NULL,        ADDR(_hush_spec)},
  621. {"-hushchars",    NULL,        TrueArg, BooleanArg, 2,
  622.   NULL,        ADDR(_hush_chars)},
  623. {"+hushchars",    "hushLostChars", FalseArg, BooleanArg, 1,
  624.   NULL,        ADDR(_hush_chars)},
  625. {"-hushchecksums", NULL,    TrueArg, BooleanArg, 2,
  626.   NULL,        ADDR(_hush_chk)},
  627. {"+hushchecksums","hushChecksums", FalseArg, BooleanArg, 1,
  628.   NULL,        ADDR(_hush_chk)},
  629. {"-borderwidth", "borderWidth",    SepArg,    NumberArg, 1,
  630.   silent,    (caddr_t) &bwidth},
  631. {"-fg",        NULL,        SepArg,    StringArg, 2,
  632.   "color",    ADDR(fore_color)},
  633. {"-foreground",    "foreground",    SepArg,    StringArg, 1,
  634.   silent,    ADDR(fore_color)},
  635. {"-bg",        NULL,        SepArg,    StringArg, 2,
  636.   "color",    ADDR(back_color)},
  637. {"-background",    "background",    SepArg,    StringArg, 1,
  638.   silent,    ADDR(back_color)},
  639. {"-hl",        "highlight",    SepArg,    StringArg, 1,
  640.   "color",    ADDR(high_color)},
  641. {"-bd",        NULL,        SepArg,    StringArg, 2,
  642.   "color",    ADDR(brdr_color)},
  643. {"-bordercolor","borderColor",    SepArg,    StringArg, 1,
  644.   silent,    ADDR(brdr_color)},
  645. {"-cr",        "cursorColor",    SepArg,    StringArg, 1,
  646.   "color",    ADDR(curs_color)},
  647. {"-bw",        NULL,        SepArg,    NumberArg, 2,
  648.   "width",    (caddr_t) &bwidth},
  649. #ifndef VMS
  650. {"-display",    NULL,        SepArg,    StringArg, 1,
  651.   "host:display", (caddr_t) &display},
  652. #else
  653. {"-display",    NULL,        SepArg,    StringArg, 1,
  654.   "host::display", (caddr_t) &display},
  655. #endif
  656. {"-geometry",    "geometry",    SepArg,    StringArg, 1,
  657.   "geometry",    (caddr_t) &geometry},
  658. {"-icongeometry","iconGeometry",StickyArg, StringArg, 1,
  659.   "geometry",    ADDR(icon_geometry)},
  660. {"-iconic",    NULL,        TrueArg, BooleanArg, 2,
  661.   NULL,        (caddr_t) &iconic},
  662. {"+iconic",    "iconic",    FalseArg, BooleanArg, 1,
  663.   NULL,        (caddr_t) &iconic},
  664. {"-keep",    NULL,        TrueArg, BooleanArg, 2,
  665.   NULL,        ADDR(keep_flag)},
  666. {"+keep",    "keepPosition",    FalseArg, BooleanArg, 1,
  667.   NULL,        ADDR(keep_flag)},
  668. {"-copy",    NULL,        TrueArg, BooleanArg, 2,
  669.   NULL,        ADDR(copy)},
  670. {"+copy",    "copy",        FalseArg, BooleanArg, 1,
  671.   NULL,        ADDR(copy)},
  672. {"-thorough",    NULL,        TrueArg, BooleanArg, 2,
  673.   NULL,        ADDR(thorough)},
  674. {"+thorough",    "thorough",    FalseArg, BooleanArg, 1,
  675.   NULL,        ADDR(thorough)},
  676. #if    PS
  677. {"-nopostscript", NULL,        FalseArg, BooleanArg, 2,
  678.   NULL,        ADDR(_postscript)},
  679. {"+nopostscript", "postscript",    TrueArg, BooleanArg, 1,
  680.   NULL,        ADDR(_postscript)},
  681. #ifdef    PS_DPS
  682. {"-nodps",    NULL,        FalseArg, BooleanArg, 2,
  683.   NULL,        ADDR(useDPS)},
  684. {"+nodps",    "dps",        TrueArg, BooleanArg, 1,
  685.   NULL,        ADDR(useDPS)},
  686. #endif
  687. #ifdef    PS_NEWS
  688. {"-nonews",    NULL,        FalseArg, BooleanArg, 2,
  689.   NULL,        ADDR(useNeWS)},
  690. {"+nonews",    "news",        TrueArg, BooleanArg, 1,
  691.   NULL,        ADDR(useNeWS)},
  692. #endif
  693. #ifdef    PS_GS
  694. {"-noghostscript",NULL,        FalseArg, BooleanArg, 2,
  695.   NULL,        ADDR(useGS)},
  696. {"+noghostscript","ghostscript", TrueArg, BooleanArg, 1,
  697.   NULL,        ADDR(useGS)},
  698. #endif
  699. #endif    /* PS */
  700. {"-version",    NULL,        TrueArg, BooleanArg, 2,
  701.   NULL,        ADDR(version_flag)},
  702. {"+version",    "version",    FalseArg, BooleanArg, 1,
  703.   NULL,        ADDR(version_flag)},
  704. {"-d",        "debugLevel",    SepArg,    StringArg, 1,
  705.   silent,    ADDR(debug_arg)},
  706. {"-maketexpk",    NULL,        FalseArg, BooleanArg, 2,
  707.  NULL,        ADDR(_use_makepk)},
  708. {"+maketexpk",    NULL,        TrueArg, BooleanArg, 1,
  709.  NULL,        ADDR(_use_makepk)},
  710. {"-mfmode",    "mfmode",       SepArg, StringArg, 1,
  711.  "cx",        ADDR(_mf_mode)},
  712. };
  713.  
  714. #endif    /* !TOOLKIT */
  715.  
  716. static    NORETURN void
  717. usage() {
  718. #ifdef    TOOLKIT
  719.     XrmOptionDescRec *opt;
  720.     _Xconst    char    **usageptr = usagestr;
  721. #else
  722.     struct option    *opt;
  723. #endif
  724.     _Xconst char    **sv    = subst_val;
  725.     _Xconst char    *str1;
  726.     _Xconst char    *str2;
  727.     int        col    = 23;
  728.     int        n;
  729.  
  730.     Fputs("Usage: xdvi [+[<page>]]", stderr);
  731.     for (opt = options; opt < options + XtNumber(options); ++opt) {
  732. #ifdef    TOOLKIT
  733.         str1 = opt->option;
  734.         if (*str1 != '-') continue;
  735.         str2 = NULL;
  736.         if (opt->argKind != XrmoptionNoArg) {
  737.         str2 = *usageptr++;
  738.         if (str2 == silent) continue;
  739.         if (str2 == subst) {
  740.             str1 = *sv++;
  741.             str2 = NULL;
  742.         }
  743.         }
  744.         for (;;) {
  745.         n = strlen(str1) + 3;
  746.         if (str2 != NULL) n += strlen(str2) + 3;
  747.         if (col + n < 80) Putc(' ', stderr);
  748.         else {
  749.             Fputs("\n\t", stderr);
  750.             col = 8 - 1;
  751.         }
  752.         if (str2 == NULL)
  753.             Fprintf(stderr, "[%s]", str1);
  754.         else
  755.             Fprintf(stderr, "[%s <%s>]", str1, str2);
  756.         col += n;
  757.         if (**usageptr != '^'
  758.              || strcmp(*usageptr + 1, opt->option) != 0) break;
  759.         ++usageptr;
  760.         str1 = *usageptr++;
  761.         str2 = NULL;
  762.         }
  763. #else     /* !TOOLKIT */
  764.         str1 = opt->name;
  765.         str2 = opt->usagestr;
  766.         if (*str1 != '-' || str2 == silent) continue;
  767.         if (str2 == subst) {
  768.         str1 = *sv++;
  769.         str2 = NULL;
  770.         }
  771.         n = strlen(str1) + 3;
  772.         if (str2 != NULL) n += strlen(str2) + 3;
  773.         if (col + n < 80) Putc(' ', stderr);
  774.         else {
  775.         Fputs("\n\t", stderr);
  776.         col = 8 - 1;
  777.         }
  778.         if (str2 == NULL)
  779.         Fprintf(stderr, "[%s]", str1);
  780.         else
  781.         Fprintf(stderr, "[%s <%s>]", str1, str2);
  782.         col += n;
  783. #endif
  784.     }
  785.     if (col + 9 < 80) Putc(' ', stderr);
  786.     else Fputs("\n\t", stderr);
  787. #ifdef SELFILE
  788.     Fputs("[dvi_file]\n", stderr);
  789. #else
  790.     Fputs("dvi_file\n", stderr);
  791. #endif
  792.     exit(1);
  793. }
  794.  
  795. static    int
  796. atopix(arg)
  797.     _Xconst    char    *arg;
  798. {
  799.     int    len    = strlen(arg);
  800.  
  801.     return (len > 2 && arg[len - 2] == 'c' && arg[len - 1] == 'm' ?
  802.         1.0 / 2.54 : 1.0) * atof(arg) * pixels_per_inch + 0.5;
  803. }
  804.  
  805. /**
  806.  **    Main programs start here.
  807.  **/
  808.  
  809. #ifdef    TOOLKIT
  810.  
  811. static    Arg    temp_args1[] = {
  812.     {XtNiconX,    (XtArgVal) 0},
  813.     {XtNiconY,    (XtArgVal) 0},
  814. };
  815.  
  816. static    Arg    temp_args2 = {XtNborderWidth,    (XtArgVal) &bwidth};
  817.  
  818. static    Pixmap    icon_pm;
  819.  
  820. static    Arg    temp_args3[] = {
  821.     {XtNiconPixmap,    (XtArgVal) &icon_pm},
  822. };
  823.  
  824. static    Arg    temp_args4[] = {
  825.     {XtNtitle,    (XtArgVal) 0},
  826.     {XtNinput,    (XtArgVal) True},
  827. };
  828.  
  829. static    Arg    set_wh_args[] = {
  830.     {XtNwidth,    (XtArgVal) 0},
  831.     {XtNheight,    (XtArgVal) 0},
  832. };
  833.  
  834. #else    /* !TOOLKIT */
  835.  
  836. struct _resource    resource = {
  837.     /* debug_arg        */    NULL,
  838.     /* density        */    40,
  839. #ifdef    GREY
  840.     /* gamma        */    1.0,
  841. #endif
  842.     /* pixels_per_inch    */    BDPI,
  843.     /* sidemargin        */    NULL,
  844.     /* topmargin        */    NULL,
  845.     /* xoffset        */    NULL,
  846.     /* yoffset        */    NULL,
  847.     /* paper        */    DEFAULT_PAPER,
  848.     /* alt_font        */    ALTFONT,
  849.     /* list_fonts        */    False,
  850.     /* reverse        */    False,
  851.     /* hush_spec        */    False,
  852.     /* hush_chars        */    False,
  853.     /* hush_chk        */    False,
  854.     /* fore_color        */    NULL,
  855.     /* back_color        */    NULL,
  856.     /* brdr_color        */    NULL,
  857.     /* high_color        */    NULL,
  858.     /* curs_color        */    NULL,
  859.     /* fore_Pixel        */    (Pixel) 0,
  860.     /* back_Pixel        */    (Pixel) 0,
  861.     /* icon_geometry    */    NULL,
  862.     /* keep_flag        */    False,
  863.     /* copy            */    2,
  864.     /* thorough        */    False,
  865. #if    PS
  866.     /* postscript        */    True,
  867. #ifdef    PS_DPS
  868.     /* useDPS        */    True,
  869. #endif
  870. #ifdef    PS_NEWS
  871.     /* useNeWS        */    True,
  872. #endif
  873. #ifdef    PS_GS
  874.     /* useGS        */    True,
  875. #endif
  876. #endif    /* PS */
  877.     /* version_flag        */    False,
  878.     /* mg_arg        */    {NULL, NULL, NULL, NULL, NULL},
  879. #ifdef    GREY
  880.     /* use_grey        */    True,
  881. #endif
  882.     /* use_makepk        */    MAKEPK_BY_DEFAULT_BOOL,
  883.     /* mf_mode        */    "cx",
  884. };
  885.  
  886. static    Pixel
  887. string_to_pixel(strp)        /* adapted from the toolkit */
  888.     char    **strp;
  889. {
  890.     char    *str = *strp;
  891.     Status    status;
  892.     XColor    color, junk;
  893.  
  894.     if (*str == '#') {    /* an rgb definition */
  895.         status = XParseColor(DISP, DefaultColormapOfScreen(SCRN),
  896.         str, &color);
  897.         if (status != 0)
  898.         status = XAllocColor(DISP, DefaultColormapOfScreen(SCRN),
  899.             &color);
  900.     }
  901.     else    /* a name */
  902.         status = XAllocNamedColor(DISP, DefaultColormapOfScreen(SCRN),
  903.         str, &color, &junk);
  904.     if (status == 0) {
  905.         Fprintf(stderr, "Cannot allocate colormap entry for \"%s\"\n", str);
  906.         *strp = NULL;
  907.         return (Pixel) 0;
  908.     }
  909.     return color.pixel;
  910. }
  911.  
  912. /*
  913.  *    Process the option table.  This is not guaranteed for all possible
  914.  *    option tables, but at least it works for this one.
  915.  */
  916.  
  917. static    void
  918. parse_options(argc, argv)
  919.     int argc;
  920.     char **argv;
  921. {
  922.     char    **arg;
  923.     char    **argvend = argv + argc;
  924.     char    *optstring;
  925.     caddr_t    addr;
  926.     struct option *opt, *lastopt, *candidate;
  927.     int    len1, len2, matchlen;
  928.  
  929.     /*
  930.      * Step 1.  Process command line options.
  931.      */
  932.     for (arg = argv + 1; arg < argvend; ++arg) {
  933.         len1 = strlen(*arg);
  934.         candidate = NULL;
  935.         matchlen = 0;
  936.         for (opt = options; opt < options + XtNumber(options); ++opt) {
  937.         len2 = strlen(opt->name);
  938.         if (opt->argclass == StickyArg) {
  939.             if (matchlen <= len2 && !strncmp(*arg, opt->name, len2)) {
  940.             candidate = opt;
  941.             matchlen = len2;
  942.             }
  943.         }
  944.         else if (len1 <= len2 && matchlen <= len1 &&
  945.             !strncmp(*arg, opt->name, len1)) {
  946.             if (len1 == len2) {
  947.             candidate = opt;
  948.             break;
  949.             }
  950.             if (matchlen < len1) candidate = opt;
  951.             else if (candidate && candidate->argclass != StickyArg)
  952.             candidate = NULL;
  953.             matchlen = len1;
  954.         }
  955.         }
  956.         if (candidate == NULL) {
  957.         if (**arg == '-' || dvi_name) usage();
  958.         else {
  959.             dvi_name = *arg;
  960.             continue;
  961.         }
  962.         }
  963.         /* flag it for subsequent processing */
  964.         candidate->resource = (char *) candidate;
  965.         /* store the value */
  966.         addr = candidate->address;
  967.         switch (candidate->argclass) {
  968.         case FalseArg:    *((Boolean *) addr) = False; continue;
  969.         case TrueArg:    *((Boolean *) addr) = True; continue;
  970.         case StickyArg:    optstring = *arg + strlen(candidate->name);
  971.             break;
  972.         case SepArg:
  973.             ++arg;
  974.             if (arg >= argvend) usage();
  975.             optstring = *arg;
  976.             break;
  977.         }
  978.         switch (candidate->argtype) {
  979.         case StringArg:    *((char **) addr) = optstring; break;
  980.         case NumberArg:    *((int *) addr) = atoi(optstring); break;
  981.         case FloatArg:  *((float *) addr) = atof(optstring); break;
  982.         default:  ;
  983.         }
  984.     }
  985.     /*
  986.      * Step 2.  Propagate classes for command line arguments.  Backwards.
  987.      */
  988.     for (opt = options + XtNumber(options) - 1; opt >= options; --opt)
  989.         if (opt->resource == (char *) opt) {
  990.         addr = opt->address;
  991.         lastopt = opt + opt->classcount;
  992.         for (candidate = opt; candidate < lastopt; ++candidate) {
  993.             if (candidate->resource != NULL) {
  994.             switch (opt->argtype) {
  995.                 case BooleanArg:
  996.                 *((Boolean *) candidate->address) =
  997.                     *((Boolean *) addr);
  998.                 break;
  999.                 case StringArg:
  1000.                 *((char **) candidate->address) =
  1001.                     *((char **) addr);
  1002.                 break;
  1003.                 case NumberArg:
  1004.                 *((int *) candidate->address) = *((int *) addr);
  1005.                 break;
  1006.                 case FloatArg:
  1007.                 *((float *) candidate->address) =
  1008.                     *((float *) addr);
  1009.                 break;
  1010.             }
  1011.             candidate->resource = NULL;
  1012.             }
  1013.         }
  1014.         }
  1015.  
  1016.     if ((DISP = XOpenDisplay(display)) == NULL)
  1017.         oops("Can't open display");
  1018.     SCRN = DefaultScreenOfDisplay(DISP);
  1019.     /*
  1020.      * Step 3.  Handle resources (including classes).
  1021.      */
  1022.     for (opt = options; opt < options + XtNumber(options); ++opt)
  1023.         if (opt->resource &&
  1024.             ((optstring = XGetDefault(DISP, prog, opt->resource)) ||
  1025.             (optstring = XGetDefault(DISP, "XDvi", opt->resource))))
  1026.         {
  1027.             lastopt = opt + opt->classcount;
  1028.             for (candidate = opt; candidate < lastopt; ++candidate)
  1029.             if (candidate->resource != NULL) switch (opt->argtype) {
  1030.                 case BooleanArg:
  1031.                 *((Boolean *) candidate->address) =
  1032.                     (strcmp(optstring, "on") == 0);
  1033.                 break;
  1034.                 case StringArg:
  1035.                 *((char **) candidate->address) = optstring;
  1036.                 break;
  1037.                 case NumberArg:
  1038.                 *((int *) candidate->address) = atoi(optstring);
  1039.                 break;
  1040.                 case FloatArg:
  1041.                 *((float *) candidate->address) =
  1042.                     atof(optstring);
  1043.             }
  1044.         }
  1045. }
  1046.  
  1047. #endif    /* TOOLKIT */
  1048.  
  1049. static    _Xconst    char    *paper_types[] = {
  1050.     "us",        "8.5x11",
  1051.     "usr",        "11x8.5",
  1052.     "legal",    "8.5x14",
  1053.     "foolscap",    "13.5x17.0",    /* ??? */
  1054.  
  1055.     /* ISO `A' formats, Portrait */
  1056.     "a1",        "59.4x84.0cm",
  1057.     "a2",        "42.0x59.4cm",
  1058.     "a3",        "29.7x42.0cm",
  1059.     "a4",        "21.0x29.7cm",
  1060.     "a5",        "14.85x21.0cm",
  1061.     "a6",        "10.5x14.85cm",
  1062.     "a7",        "7.42x10.5cm",
  1063.  
  1064.     /* ISO `A' formats, Landscape */
  1065.     "a1r",        "84.0x59.4cm",
  1066.     "a2r",        "59.4x42.0cm",
  1067.     "a3r",        "42.0x29.7cm",
  1068.     "a4r",        "29.7x21.0cm",
  1069.     "a5r",        "21.0x14.85cm",
  1070.     "a6r",        "14.85x10.5cm",
  1071.     "a7r",        "10.5x7.42cm",
  1072.  
  1073.     /* ISO `B' formats, Portrait */
  1074.     "b1",        "70.6x100.0cm",
  1075.     "b2",        "50.0x70.6cm",
  1076.     "b3",        "35.3x50.0cm",
  1077.     "b4",        "25.0x35.3cm",
  1078.     "b5",        "17.6x25.0cm",
  1079.     "b6",        "13.5x17.6cm",
  1080.     "b7",        "8.8x13.5cm",
  1081.  
  1082.     /* ISO `B' formats, Landscape */
  1083.     "b1r",        "100.0x70.6cm",
  1084.     "b2r",        "70.6x50.0cm",
  1085.     "b3r",        "50.0x35.3cm",
  1086.     "b4r",        "35.3x25.0cm",
  1087.     "b5r",        "25.0x17.6cm",
  1088.     "b6r",        "17.6x13.5cm",
  1089.     "b7r",        "13.5x8.8cm",
  1090.  
  1091.     /* ISO `C' formats, Portrait */
  1092.     "c1",        "64.8x91.6cm",
  1093.     "c2",        "45.8x64.8cm",
  1094.     "c3",        "32.4x45.8cm",
  1095.     "c4",        "22.9x32.4cm",
  1096.     "c5",        "16.2x22.9cm",
  1097.     "c6",        "11.46x16.2cm",
  1098.     "c7",        "8.1x11.46cm",
  1099.  
  1100.     /* ISO `C' formats, Landscape */
  1101.     "c1r",        "91.6x64.8cm",
  1102.     "c2r",        "64.8x45.8cm",
  1103.     "c3r",        "45.8x32.4cm",
  1104.     "c4r",        "32.4x22.9cm",
  1105.     "c5r",        "22.9x16.2cm",
  1106.     "c6r",        "16.2x11.46cm",
  1107.     "c7r",        "11.46x8.1cm",
  1108. };
  1109.  
  1110. static    Boolean
  1111. set_paper_type() {
  1112.     _Xconst    char    *arg, *arg1;
  1113.     char    temp[21];
  1114.     _Xconst    char    **p;
  1115.     char    *q;
  1116.  
  1117.     if (strlen(resource.paper) > sizeof(temp) - 1) return False;
  1118.     arg = resource.paper;
  1119.     q = temp;
  1120.     for (;;) {    /* convert to lower case */
  1121.         char c = *arg++;
  1122.         if (c >= 'A' && c <= 'Z') c ^= ('a' ^ 'A');
  1123.         *q++ = c;
  1124.         if (c == '\0') break;
  1125.     }
  1126.     arg = temp;
  1127.     /* perform substitutions */
  1128.     for (p = paper_types; p < paper_types + XtNumber(paper_types); p += 2)
  1129.         if (strcmp(temp, *p) == 0) {
  1130.         arg = p[1];
  1131.         break;
  1132.         }
  1133.     arg1 = index(arg, 'x');
  1134.     if (arg1 == NULL) return False;
  1135.     unshrunk_paper_w = atopix(arg);
  1136.     unshrunk_paper_h = atopix(arg1 + 1);
  1137.     return (unshrunk_paper_w != 0 && unshrunk_paper_h != 0);
  1138. }
  1139.  
  1140. /* Set the icon name and title name standard properties on `top_level'
  1141.    (which we don't pass in because it is a different type for TOOLKIT
  1142.    !and TOOLKIT).  We use the basename of the DVI file (without the
  1143.    .dvi), so different xdvi invocations can be distinguished, yet we
  1144.    don't use up too much real estate.  */
  1145.  
  1146. void
  1147. set_icon_and_title (dvi_name)
  1148.     char *dvi_name;
  1149. {
  1150.   /* Use basename of DVI file for name in icon and title.  */
  1151.   unsigned baselen;
  1152.   char *icon_name, *title_name;
  1153.   Window top_window =
  1154. #ifdef TOOLKIT
  1155.     XtWindow (top_level);
  1156. #else
  1157.     top_level;
  1158. #endif
  1159.   char *basename = strrchr (dvi_name, '/');
  1160.   basename = basename ? basename + 1 : dvi_name;
  1161.  
  1162.   baselen = strlen (basename);
  1163.   icon_name = xmalloc (baselen + 1, "icon name");
  1164.  
  1165.   baselen -= sizeof ("dvi"); /* subtract off the suffix */
  1166.   if (strcmp(basename + baselen, ".dvi") == 0) {
  1167.     strncpy (icon_name, basename, baselen);
  1168.     icon_name[baselen] = 0;
  1169.   } else {
  1170.     strcpy (icon_name, basename);
  1171.   }
  1172.  
  1173.   title_name = xmalloc (strlen (icon_name) + sizeof ("Xdvi: ") + 1,
  1174.                         "title name");
  1175.   strcpy (title_name, "Xdvi: ");
  1176.   strcat (title_name, icon_name);
  1177.  
  1178.   XSetStandardProperties(DISP, top_window, title_name, icon_name,
  1179.                          (Pixmap) 0, NULL, 0, NULL);
  1180. }
  1181.  
  1182. /*
  1183.  *    main program
  1184.  */
  1185.  
  1186. int
  1187. main(argc, argv)
  1188.     int argc;
  1189.     char **argv;
  1190. {
  1191. #ifndef    TOOLKIT
  1192.     XSizeHints    size_hints;
  1193.     XWMHints    wmhints;
  1194. #endif    /* TOOLKIT */
  1195.     Dimension    screen_w, screen_h;
  1196.     int    i;
  1197.  
  1198.         /* Have to special-case this to make just `xdvi -version' not
  1199.            give a usage message. */
  1200.     if (argc == 2 && ((strcmp (argv[1], "-version") == 0)
  1201.                          || (strcmp (argv[1], "+version") == 0)
  1202.                          || (strcmp (argv[1], "--version") == 0)
  1203.                          ))
  1204.           {
  1205.             Puts((_Xconst char *) version);
  1206.         exit (EXIT_SUCCESS); 
  1207.           }
  1208.     if (argc == 2 && ((strcmp (argv[1], "-help") == 0)
  1209.                          || (strcmp (argv[1], "+help") == 0)
  1210.                          || (strcmp (argv[1], "--help") == 0)
  1211.                          ))
  1212.           {
  1213.             Puts((_Xconst char *) version);
  1214.             usage ();
  1215.         exit (EXIT_SUCCESS); 
  1216.           }
  1217.  
  1218.         /* We can't initialize this at compile time, because it might be
  1219.            the result of a function call under POSIX. */
  1220.         n_files_left = OPEN_MAX;
  1221.  
  1222. #ifndef    VMS
  1223.     prog = rindex(*argv, '/');
  1224. #else
  1225.     prog = rindex(*argv, ']');
  1226. #endif
  1227.     if (prog != NULL) ++prog; else prog = *argv;
  1228.  
  1229. #ifdef    VMS
  1230.     if (index(prog, '.') != NULL) *index(prog, '.') = '\0';
  1231. #endif
  1232.     kpse_set_progname (argv[0]);
  1233. #ifdef    TOOLKIT
  1234.     top_level = XtInitialize(prog, "XDvi", options, XtNumber(options),
  1235.         &argc, argv);
  1236.     while (--argc > 0) {
  1237.         if (*(*++argv) == '+')
  1238.         if (curr_page != NULL) usage();
  1239.         else curr_page = *argv + 1;
  1240.         else if (dvi_name != NULL) usage();
  1241.         else dvi_name = *argv;
  1242.     }
  1243.  
  1244.     XtGetApplicationResources(top_level, (XtPointer) &resource,
  1245.         application_resources, XtNumber(application_resources),
  1246.         (ArgList) NULL, 0);
  1247.     DISP = XtDisplay(top_level);
  1248.     SCRN = XtScreen(top_level);
  1249.     shrink_factor = resource.shrinkfactor;
  1250.  
  1251. #else    /* !TOOLKIT */
  1252.  
  1253.     parse_options(argc, argv);
  1254.     if (resource.fore_color)
  1255.         fore_Pixel = string_to_pixel(&resource.fore_color);
  1256.     if (resource.back_color)
  1257.         back_Pixel = string_to_pixel(&resource.back_color);
  1258.     if (resource.brdr_color)
  1259.         brdr_Pixel = string_to_pixel(&resource.brdr_color);
  1260.     if (resource.high_color)
  1261.         hl_Pixel = string_to_pixel(&resource.high_color);
  1262.     if (resource.curs_color)
  1263.         cr_Pixel = string_to_pixel(&resource.curs_color);
  1264.  
  1265. #endif    /* TOOLKIT */
  1266.  
  1267. #ifdef SELFILE
  1268.     /* If no filename by here, so pop up a selection box. */
  1269.     if (dvi_name == NULL)
  1270.       {
  1271.         extern FILE *select_filename ();
  1272.         (void) select_filename(False, False) ;
  1273.       }
  1274. #endif  /* SELFILE */
  1275.  
  1276.     if (shrink_factor <= 0 || density <= 0 || pixels_per_inch <= 0 ||
  1277.         dvi_name == NULL) usage();
  1278.     if (shrink_factor != 1) bak_shrink = shrink_factor;
  1279.     mane.shrinkfactor = shrink_factor;
  1280.     if (resource.debug_arg != NULL)
  1281.       {
  1282.         debug = isdigit(*resource.debug_arg) ? atoi(resource.debug_arg)
  1283.         : DBG_ALL;
  1284.         if (debug & DBG_OPEN) KPSE_DEBUG_SET (KPSE_DEBUG_FOPEN);
  1285.         if (debug & DBG_STAT) KPSE_DEBUG_SET (KPSE_DEBUG_STAT);
  1286.         if (debug & DBG_HASH) KPSE_DEBUG_SET (KPSE_DEBUG_HASH);
  1287.         if (debug & DBG_PATHS) KPSE_DEBUG_SET (KPSE_DEBUG_PATHS);
  1288.         if (debug & DBG_EXPAND) KPSE_DEBUG_SET (KPSE_DEBUG_EXPAND);
  1289.         if (debug & DBG_SEARCH) KPSE_DEBUG_SET (KPSE_DEBUG_SEARCH);
  1290.       }
  1291.     if (resource.sidemargin) home_x = atopix(resource.sidemargin);
  1292.     if (resource.topmargin) home_y = atopix(resource.topmargin);
  1293.     offset_x = resource.xoffset ? atopix(resource.xoffset)
  1294.         : pixels_per_inch;
  1295.     offset_y = resource.yoffset ? atopix(resource.yoffset)
  1296.         : pixels_per_inch;
  1297.     if (!set_paper_type()) oops("Don't recognize paper type %s",
  1298.         resource.paper);
  1299.     for (i = 0; i < 5; ++i)
  1300.         if (resource.mg_arg[i] != NULL) {
  1301.         char    *s;
  1302.  
  1303.         mg_size[i].w = mg_size[i].h = atoi(resource.mg_arg[i]);
  1304.         s = index(resource.mg_arg[i], 'x');
  1305.         if (s != NULL) {
  1306.             mg_size[i].h = atoi(s + 1);
  1307.             if (mg_size[i].h <= 0) mg_size[i].w = 0;
  1308.         }
  1309.         }
  1310.  
  1311.     kpse_init_prog ("XDVI", pixels_per_inch, mf_mode, use_makepk,alt_font);
  1312.     open_dvi_file();
  1313.     if (curr_page) {
  1314.         current_page = (*curr_page ? atoi(curr_page) : total_pages) - 1;
  1315.         if (current_page < 0 || current_page >= total_pages) usage();
  1316.     }
  1317.     if (resource.version_flag) Puts((_Xconst char *) &version);
  1318.  
  1319.     /*
  1320.      *    Colors
  1321.      */
  1322.  
  1323.     if (resource.reverse) {
  1324.         if (!resource.fore_color) fore_Pixel = WhitePixelOfScreen(SCRN);
  1325.         if (!resource.back_color) back_Pixel = BlackPixelOfScreen(SCRN);
  1326.         /* Set them nonzero */
  1327.         resource.fore_color = resource.back_color = (char *) &version;
  1328.     } else {
  1329.         if (!resource.fore_color) fore_Pixel = BlackPixelOfScreen(SCRN);
  1330.         if (!resource.back_color) back_Pixel = WhitePixelOfScreen(SCRN);
  1331.     }
  1332.  
  1333. #ifdef    GREY
  1334.     if (DefaultDepthOfScreen(SCRN) == 1)
  1335.         use_grey = False;
  1336. #endif
  1337.  
  1338. #ifdef    TOOLKIT
  1339.     if (!resource.copy_arg)
  1340. #else
  1341.     if (resource.copy == 2)
  1342. #endif
  1343. #ifdef    GREY
  1344.         resource.copy = (!resource.thorough && !use_grey
  1345.         && DefaultDepthOfScreen(SCRN) > 1);
  1346. #else
  1347.         resource.copy = (!resource.thorough
  1348.         && DefaultDepthOfScreen(SCRN) > 1);
  1349. #endif
  1350.  
  1351. #ifdef    GREY
  1352.     if (use_grey)
  1353.         init_pix(True);
  1354.     else
  1355. #endif
  1356.     {
  1357.         XGCValues    values;
  1358.         Pixel    set_bits = (Pixel) (fore_Pixel & ~back_Pixel);
  1359.         Pixel    clr_bits = (Pixel) (back_Pixel & ~fore_Pixel);
  1360.  
  1361. #define    MakeGC(fcn, fg, bg)    (values.function = fcn, values.foreground=fg,\
  1362.         values.background=bg,\
  1363.         XCreateGC(DISP, RootWindowOfScreen(SCRN),\
  1364.             GCFunction|GCForeground|GCBackground, &values))
  1365.  
  1366.         if (resource.copy || (set_bits && clr_bits))
  1367.         ruleGC = MakeGC(GXcopy, fore_Pixel, back_Pixel);
  1368.         if (resource.copy) foreGC = ruleGC;
  1369.         else if (!resource.thorough && ruleGC) {
  1370.         foreGC = ruleGC;
  1371.         Puts("Note:  overstrike characters may be incorrect.");
  1372.         }
  1373.         else {
  1374.         if (set_bits) foreGC = MakeGC(GXor, set_bits, 0);
  1375.         if (clr_bits || !set_bits)
  1376.             *(foreGC ? &foreGC2 : &foreGC) =
  1377.             MakeGC(GXandInverted, clr_bits, 0);
  1378.         if (!ruleGC) ruleGC = foreGC;
  1379.         }
  1380.     }
  1381.  
  1382.     {
  1383.         XGCValues    values;
  1384.  
  1385.         highGC = ruleGC;
  1386.         if (resource.high_color)
  1387.         highGC = MakeGC(GXcopy, hl_Pixel, back_Pixel);
  1388.     }
  1389.  
  1390.     if (!resource.brdr_color) brdr_Pixel = fore_Pixel;
  1391.     if (!resource.high_color) hl_Pixel = fore_Pixel;
  1392.  
  1393. #ifndef    VMS
  1394.     ready_cursor = XCreateFontCursor(DISP, XC_cross);
  1395.     redraw_cursor = XCreateFontCursor(DISP, XC_watch);
  1396. #else
  1397.     DECWCursorFont = XLoadFont(DISP, "DECW$CURSOR");
  1398.     XSetFont(DISP, foreGC, DECWCursorFont);
  1399.     redraw_cursor = XCreateGlyphCursor(DISP, DECWCursorFont, DECWCursorFont,
  1400.         decw$c_wait_cursor, decw$c_wait_cursor + 1,
  1401.         &resource.fore_color, &resource.back_color);
  1402.     MagnifyPixmap = XCreateBitmapFromData (DISP, RootWindowOfScreen(SCRN),
  1403.         mag_glass_bits, mag_glass_width, mag_glass_height);
  1404.     ready_cursor = XCreatePixmapCursor(DISP, MagnifyPixmap, MagnifyPixmap,
  1405.         &resource.back_color, &resource.fore_color,
  1406.         mag_glass_x_hot, mag_glass_y_hot);
  1407. #endif    /* VMS */
  1408.  
  1409.     if (!resource.curs_color) cr_Pixel = hl_Pixel;
  1410.     {
  1411.         XColor bg_Color, cr_Color;
  1412.  
  1413.         bg_Color.pixel = back_Pixel;
  1414.         XQueryColor(DISP, DefaultColormapOfScreen(SCRN), &bg_Color);
  1415.         cr_Color.pixel = cr_Pixel;
  1416.         XQueryColor(DISP, DefaultColormapOfScreen(SCRN), &cr_Color);
  1417.         XRecolorCursor(DISP, ready_cursor, &cr_Color, &bg_Color);
  1418.         XRecolorCursor(DISP, redraw_cursor, &cr_Color, &bg_Color);
  1419.     }
  1420.  
  1421. #ifdef    TOOLKIT
  1422.  
  1423.     /*
  1424.      *    Windows (toolkit)
  1425.      */
  1426.  
  1427.         /* The following code is lifted from Xterm */
  1428.     if (resource.icon_geometry != NULL) {
  1429.         int scr, junk;
  1430.  
  1431.         for(scr = 0;    /* yyuucchh */
  1432.         SCRN != ScreenOfDisplay(DISP, scr);
  1433.         scr++);
  1434.  
  1435.         (void) XGeometry(DISP, scr, resource.icon_geometry, "",
  1436.             0, 0, 0, 0, 0,
  1437.             (int *) &temp_args1[0].value,
  1438.             (int *) &temp_args1[1].value, &junk, &junk);
  1439.         XtSetValues(top_level, temp_args1, XtNumber(temp_args1));
  1440.     }
  1441.         /* Set icon pixmap */
  1442.     XtGetValues(top_level, temp_args3, XtNumber(temp_args3));
  1443.     if (icon_pm == (Pixmap) 0) {
  1444.         temp_args3[0].value = (XtArgVal) (XCreateBitmapFromData(DISP,
  1445.         RootWindowOfScreen(SCRN), (_Xconst char *) xdvi_bits,
  1446.         xdvi_width, xdvi_height));
  1447.         XtSetValues(top_level, temp_args3, XtNumber(temp_args3));
  1448.     }
  1449.     temp_args4[0].value = (XtArgVal) dvi_name;
  1450.     XtSetValues(top_level, temp_args4, XtNumber(temp_args4));
  1451.  
  1452. #ifdef    BUTTONS
  1453.     form_widget = XtCreateManagedWidget("form", formWidgetClass,
  1454.         top_level, form_args, XtNumber(form_args));
  1455.  
  1456. #else    /* !BUTTONS */
  1457. #define    form_widget    top_level    /* for calls to XtAddEventHandler */
  1458. #endif    /* BUTTONS */
  1459.     vport_widget = XtCreateManagedWidget("vport", viewportWidgetClass,
  1460.         form_widget, vport_args, XtNumber(vport_args));
  1461.     clip_widget = XtNameToWidget(vport_widget, "clip");
  1462.     draw_args[0].value = (XtArgVal) page_w;
  1463.     draw_args[1].value = (XtArgVal) page_h;
  1464. #ifdef    GREY
  1465.     draw_args[2].value = (XtArgVal) back_Pixel;
  1466. #endif
  1467.     draw_widget = XtCreateManagedWidget("drawing", drawWidgetClass,
  1468.         vport_widget, draw_args, XtNumber(draw_args));
  1469.     {    /* set default window size */
  1470. #ifdef    BUTTONS
  1471.         int xtra_wid = resource.expert ? 0 : XTRA_WID;
  1472. #else
  1473. #define    xtra_wid    0
  1474. #endif
  1475.         XtWidgetGeometry constraints;
  1476.         XtWidgetGeometry reply;
  1477.  
  1478.         XtGetValues(top_level, &temp_args2, 1);    /* get border width */
  1479.         screen_w = WidthOfScreen(SCRN) - 2 * bwidth - xtra_wid;
  1480.         screen_h = HeightOfScreen(SCRN) - 2 * bwidth;
  1481.         constraints.request_mode = reply.request_mode = 0;
  1482.         constraints.width = page_w;
  1483.         if (page_w > screen_w) {
  1484.         constraints.request_mode = CWWidth;
  1485.         constraints.width = screen_w;
  1486.         }
  1487.         constraints.height = page_h;
  1488.         if (page_h > screen_h) {
  1489.         constraints.request_mode = CWHeight;
  1490.         constraints.height = screen_h;
  1491.         }
  1492.         if (constraints.request_mode != 0
  1493.             && constraints.request_mode != (CWWidth | CWHeight))
  1494.         (void) XtQueryGeometry(vport_widget, &constraints, &reply);
  1495.         if (!(reply.request_mode & CWWidth))
  1496.         reply.width = constraints.width;
  1497.         set_wh_args[0].value = (XtArgVal) ((reply.width < screen_w
  1498.                 ? reply.width : screen_w) + xtra_wid);
  1499.         if (!(reply.request_mode & CWHeight))
  1500.         reply.height = constraints.height;
  1501.         set_wh_args[1].value = (XtArgVal) (reply.height < screen_h
  1502.                     ? reply.height : screen_h);
  1503.         XtSetValues(top_level, set_wh_args, XtNumber(set_wh_args));
  1504. #ifdef    BUTTONS
  1505.         set_wh_args[0].value -= xtra_wid;
  1506.         XtSetValues(vport_widget, set_wh_args, XtNumber(set_wh_args));
  1507.         if (!resource.expert) create_buttons(set_wh_args[1].value);
  1508. #endif    /* BUTTONS */
  1509.     }
  1510.     if (resource.fore_color) {
  1511.         static Arg fore_args = {XtNforeground, (XtArgVal) 0};
  1512.  
  1513.         fore_args.value = fore_Pixel;
  1514.         XtSetValues(draw_widget, &fore_args, 1);
  1515.     }
  1516.     if (resource.back_color) {
  1517.         static Arg back_args = {XtNbackground, (XtArgVal) 0};
  1518.  
  1519.         back_args.value = back_Pixel;
  1520.         XtSetValues(draw_widget, &back_args, 1);
  1521.         XtSetValues(clip_widget, &back_args, 1);
  1522.     }
  1523.     XtAddEventHandler(form_widget, KeyPressMask, False, handle_key,
  1524.         (caddr_t) NULL);
  1525.     XtAddEventHandler(vport_widget, StructureNotifyMask, False,
  1526.         handle_resize, (caddr_t) NULL);
  1527.     XtAddEventHandler(draw_widget, ExposureMask, False, handle_exp,
  1528.         (caddr_t) &mane);
  1529.     XtAddEventHandler(draw_widget, ButtonPressMask, False, handle_button,
  1530.         (caddr_t) NULL);
  1531.     XtAddEventHandler(draw_widget, ButtonMotionMask, False, handle_motion,
  1532.         (caddr_t) NULL);
  1533.     XtAddEventHandler(draw_widget, ButtonReleaseMask, False, handle_release,
  1534.         (caddr_t) NULL);
  1535.     XtRealizeWidget(top_level);
  1536.  
  1537.     currwin.win = mane.win = XtWindow(draw_widget);
  1538.  
  1539.     {
  1540.         XWindowAttributes attrs;
  1541.  
  1542.         (void) XGetWindowAttributes(DISP, mane.win, &attrs);
  1543.         backing_store = attrs.backing_store;
  1544.     }
  1545.  
  1546. #else    /* !TOOLKIT */
  1547.  
  1548.     /*
  1549.      *    Windows (non toolkit)
  1550.      */
  1551.  
  1552.     screen_w = WidthOfScreen(SCRN) - 2*bwidth;
  1553.     screen_h = HeightOfScreen(SCRN) - 2*bwidth;
  1554.     size_hints.flags = PMinSize;
  1555.     size_hints.min_width = size_hints.min_height = 50;
  1556.     size_hints.x = size_hints.y = 0;
  1557.     if (geometry != NULL) {
  1558.         int flag = XParseGeometry(geometry, &size_hints.x, &size_hints.y,
  1559.         &window_w, &window_h);
  1560.  
  1561.         if (flag & (XValue | YValue))
  1562.         size_hints.flags |= USPosition;
  1563.         if (flag & (WidthValue | HeightValue))
  1564.         size_hints.flags |= USSize;
  1565.         if (flag & XNegative) size_hints.x += screen_w - window_w;
  1566.         if (flag & YNegative) size_hints.y += screen_h - window_h;
  1567.     }
  1568.     if (!(size_hints.flags & USSize)) {
  1569.         int x_thick = 0;
  1570.         int y_thick = 0;
  1571.         if (screen_w < page_w) x_thick = BAR_THICK;
  1572.         if (screen_h < page_h + x_thick) y_thick = BAR_THICK;
  1573.         window_w = page_w + y_thick;
  1574.         if (window_w > screen_w) {
  1575.         x_thick = BAR_THICK;
  1576.         window_w = screen_w;
  1577.         }
  1578.         window_h = page_h + x_thick;
  1579.         if (window_h > screen_h) window_h = screen_h;
  1580.         size_hints.flags |= PSize;
  1581.     }
  1582.     size_hints.width = window_w;
  1583.     size_hints.height = window_h;
  1584.     top_level = XCreateSimpleWindow(DISP, RootWindowOfScreen(SCRN),
  1585.         size_hints.x, size_hints.y, window_w, window_h, bwidth,
  1586.         brdr_Pixel, back_Pixel);
  1587.     XSetStandardProperties(DISP, top_level, NULL, NULL, (Pixmap) 0,
  1588.         argv, argc, &size_hints);
  1589.  
  1590.     wmhints.flags = InputHint | StateHint | IconPixmapHint;
  1591.     wmhints.input = True;    /* window manager must direct input */
  1592.     wmhints.initial_state = iconic ? IconicState : NormalState;
  1593.     wmhints.icon_pixmap = XCreateBitmapFromData(DISP,
  1594.         RootWindowOfScreen(SCRN), (_Xconst char *) xdvi_bits,
  1595.         xdvi_width, xdvi_height);
  1596.     if (resource.icon_geometry != NULL) {
  1597.         int junk;
  1598.  
  1599.         wmhints.flags |= IconPositionHint;
  1600.         (void) XGeometry(DISP, DefaultScreen(DISP), resource.icon_geometry,
  1601.         "", 0, 0, 0, 0, 0, &wmhints.icon_x, &wmhints.icon_y,
  1602.         &junk, &junk);
  1603.     }
  1604.     XSetWMHints(DISP, top_level, &wmhints);
  1605.  
  1606.     XSelectInput(DISP, top_level, KeyPressMask | StructureNotifyMask);
  1607.     XMapWindow(DISP, top_level);
  1608.     XFlush(DISP);
  1609.  
  1610. #endif    /* TOOLKIT */
  1611.  
  1612.     set_icon_and_title (dvi_name);
  1613.     
  1614. #define    rebindkey(ks, str)    XRebindKeysym(DISP, (KeySym) ks, \
  1615.         (KeySym *) NULL, 0, (_Xconst ubyte *) str, 1)
  1616.     rebindkey(XK_Home, "^");
  1617.     rebindkey(XK_Left, "l");
  1618.     rebindkey(XK_Up, "u");
  1619.     rebindkey(XK_Right, "r");
  1620.     rebindkey(XK_Down, "d");
  1621.     rebindkey(XK_Prior, "b");
  1622.     rebindkey(XK_Next, "f");
  1623. #undef    rebindkey
  1624.  
  1625.     image = XCreateImage(DISP, DefaultVisualOfScreen(SCRN), 1, XYBitmap, 0,
  1626.                  (char *)NULL, 0, 0, BITS_PER_BMUNIT, 0);
  1627.     image->bitmap_unit = BITS_PER_BMUNIT;
  1628. #ifndef    MSBITFIRST
  1629.     image->bitmap_bit_order = LSBFirst;
  1630. #else
  1631.     image->bitmap_bit_order = MSBFirst;
  1632. #endif
  1633.     {
  1634.         short endian = MSBFirst << 8 | LSBFirst;
  1635.         image->byte_order = *((char *) &endian);
  1636.     }
  1637.  
  1638.     do_pages();
  1639.     return 0;    /* do_pages() returns if DBG_BATCH is specified */
  1640. }
  1641.