From: SNYDER@d0sb10.fnal.gov
Newsgroups: comp.os.vms
Subject: Re: GNUPLOT_X11 for VMS???
Date: 30 Jul 92 04:03:20 GMT
Organization: Fermilab
In-reply-to: mckeever@cstp.umkc.edu's message of 30 Jul 92 00:31:32 GMT

In article <1992Jul29.183132.1@cstp.umkc.edu> mckeever@cstp.umkc.edu writes:
>...  Has some kind soul who knows what they are doing with X11 and
>C already got GNUPLOT_X11 running on a VMS machine?  And are you willing to
>share?


I hacked this up last week for gnuplot 3.2.  You should include `x11-vms.trm'
instead of `x11.trm' in term.c.  Compile and link gnuplot and gnuplot_x11,
and define the logical `gnuplot_dir' to point to the directory where gnuplot_x11.exe
is located.  Then you should be set!

Caveat:  I've only tried to compile this with gcc.  I don't think there's anything
which would cause problems for vaxc, though you may have to diddle
some header files.

scott snyder
snyder@d0sb10.fnal.gov


$! ------------------ CUT HERE -----------------------
$ v='f$verify(f$trnlnm("SHARE_VERIFY"))'
$!
$! This archive created by VMS_SHARE Version 7.2-010  25-Jun-1992
$!   On 29-JUL-1992 21:51:55.66   By user SNYDER 
$!
$! This VMS_SHARE Written by:
$!    Andy Harper, Kings College London UK
$!
$! Acknowledgements to:
$!    James Gray       - Original VMS_SHARE
$!    Michael Bednarek - Original Concept and implementation
$!
$! TO UNPACK THIS SHARE FILE, CONCATENATE ALL PARTS IN ORDER
$! AND EXECUTE AS A COMMAND PROCEDURE  (  @name  )
$!
$! THE FOLLOWING FILE(S) WILL BE CREATED AFTER UNPACKING:
$!       1. GNUPLOT_X11.C;7
$!       2. X11-VMS.TRM;5
$!
$set="set"
$set symbol/scope=(nolocal,noglobal)
$f=f$parse("SHARE_TEMP","SYS$SCRATCH:.TMP_"+f$getjpi("","PID"))
$e="write sys$error  ""%UNPACK"", "
$w="write sys$output ""%UNPACK"", "
$ if f$trnlnm("SHARE_LOG") then $ w = "!"
$ ve=f$getsyi("version")
$ if ve-f$extract(0,1,ve) .ges. "4.4" then $ goto START
$ e "-E-OLDVER, Must run at least VMS 4.4"
$ v=f$verify(v)
$ exit 44
$UNPACK: SUBROUTINE ! P1=filename, P2=checksum
$ x = P1 - f$parse(P1,,,"version")
$ y = f$search(x)
$ if y .eqs. "" then $ goto file_absent
$ x = f$integer(f$parse(P1,,,"version")-";")
$ y = f$integer(f$parse(y,,,"version")-";")
$ if x .gt. y then $ goto file_absent
$ if f$mode() .eqs. "INTERACTIVE" then $ goto file_interactive
$ if x .eq. y then e "-W-EXISTS, File ''P1' exists. Skipped."
$ if x .ne. y then e "-W-NEWERVERSION, of File ''P1' exists. Skipped."
$file_delete:
$ delete 'f'*
$ exit
$file_interactive:
$ if x .eq. y then e "-W-EXISTS, File ''P1' exists."
$ if x .ne. y then e "-W-NEWERVERSION, of File ''P1' exists."
$ read/error=file_delete/end=file_delete-
  /prompt="Create new version [y/n]: " -
  sys$command x
$ if .not. x then $ e "-W-SKIPPED, File ''P1' skipped."
$ if .not. x then $ goto file_delete
$ P1 = P1 - f$parse(P1,,,"version")
$file_absent:
$ if f$parse(P1) .nes. "" then $ goto dirok
$ dn=f$parse(P1,,,"DIRECTORY")
$ w "-I-CREDIR, Creating directory ''dn'."
$ create/dir 'dn'
$ if $status then $ goto dirok
$ e "-E-CREDIRFAIL, Unable to create ''dn'. File skipped."
$ delete 'f'*
$ exit
$dirok:
$ w "-I-PROCESS, Processing file ''P1'."
$ if .not. f$verify() then $ define/user sys$output nl:
$ EDIT/TPU/NOSEC/NODIS/COM=SYS$INPUT 'f'/OUT='P1'
PROCEDURE Unpacker ON_ERROR ENDON_ERROR;SET(FACILITY_NAME,"UNPACK");SET(
SUCCESS,OFF);SET(INFORMATIONAL,OFF);f:=GET_INFO(COMMAND_LINE,"file_name");b:=
CREATE_BUFFER(f,f);p:=SPAN(" ")@r&LINE_END;POSITION(BEGINNING_OF(b));
LOOP EXITIF SEARCH(p,FORWARD)=0;POSITION(r);ERASE(r);ENDLOOP;POSITION(
BEGINNING_OF(b));g:=0;LOOP EXITIF MARK(NONE)=END_OF(b);x:=ERASE_CHARACTER(1);
IF g=0 THEN IF x="X" THEN MOVE_VERTICAL(1);ENDIF;IF x="V" THEN APPEND_LINE;
MOVE_HORIZONTAL(-CURRENT_OFFSET);MOVE_VERTICAL(1);ENDIF;IF x="+" THEN g:=1;
ERASE_LINE;ENDIF;ELSE IF x="-" THEN IF INDEX(CURRENT_LINE,"+-+-+-+-+-+-+-+")=
1 THEN g:=0;ENDIF;ENDIF;ERASE_LINE;ENDIF;ENDLOOP;t:="0123456789ABCDEF";
POSITION(BEGINNING_OF(b));LOOP r:=SEARCH("`",FORWARD);EXITIF r=0;POSITION(r);
ERASE(r);x1:=INDEX(t,ERASE_CHARACTER(1))-1;x2:=INDEX(t,ERASE_CHARACTER(1))-1;
COPY_TEXT(ASCII(16*x1+x2));ENDLOOP;WRITE_FILE(b,GET_INFO(COMMAND_LINE,
"output_file"));ENDPROCEDURE;Unpacker;QUIT;
$ delete/nolog 'f'*
$ CHECKSUM 'P1'
$ IF CHECKSUM$CHECKSUM .eqs. P2 THEN $ EXIT
$ e "-E-CHKSMFAIL, Checksum of ''P1' failed."
$ ENDSUBROUTINE
$START:
$ create 'f'
X#ifndef lint
Xstatic char *RCSid = "$Id: gnuplot_x11.c,v 3.26 92/03/24 22:35:52 woo Exp Lo
Vcker: woo $";
X#endif
X
X/*--------------------------------------------------------------------------
V---
X *   gnuplot_x11 - X11 outboard terminal driver for gnuplot 3
X *
X *   Requires installation of companion inboard x11 driver in gnuplot/term.c
X *
X *   Acknowledgements:`20
X *      Chris Peterson (MIT)
X *      Dana Chee (Bellcore)`20
X *      Arthur Smith (Cornell)
X *      Hendri Hondorp (University of Twente, The Netherlands)
X *      Bill Kucharski (Solbourne)
X *      Charlie Kline (University of Illinois)
X *      O'Reilly & Associates: X Window System - Volumes 1 & 2
X *
X * vms stuff - sss
X *
X *   This code is provided as is and with no warranties of any kind.
X *      `20
X *   Ed Kubaitis (ejk@uiuc.edu)
X *   Computing & Communications Services Office`20
X *   University of Illinois, Urbana
X *--------------------------------------------------------------------------
V-*/
X
X#include <X11/Xos.h>
X#include <X11/Xlib.h>
X#include <X11/Xutil.h>
X#include <X11/Xatom.h>
X#include <X11/Xresource.h>
X`20
X#include <stdio.h>
X#include <signal.h>
X
X#ifndef VMS
X#ifndef FD_SET
X#ifndef OLD_SELECT
X#include <sys/select.h>
X#else   /* OLD_SELECT */
X#define FD_SET(n, p)    ((p)->fds_bits`5B0`5D `7C= (1 << ((n) % 32)))
X#define FD_CLR(n, p)    ((p)->fds_bits`5B0`5D &= `7E(1 << ((n) % 32)))
X#define FD_ISSET(n, p)  ((p)->fds_bits`5B0`5D & (1 << ((n) % 32)))
X#define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
X#endif  /* OLD_SELECT */
X#endif  /* FD_SET */
X#endif  /* VMS */
X
X#include <errno.h>
Xextern int errno;
X
X#define FallbackFont "fixed"
X#define Ncolors 13
Xunsigned long colors`5BNcolors`5D;
X
Xchar dashes`5B10`5D`5B5`5D = `7B `7B0`7D, `7B1,6,0`7D,`20
X   `7B0`7D, `7B4,2,0`7D, `7B1,3,0`7D, `7B4,4,0`7D, `7B1,5,0`7D, `7B4,4,4,1,0
V`7D, `7B4,2,0`7D, `7B1,3,0`7D
X   `7D;
X
XDisplay *dpy; int scr; Window win, root;
XVisual *vis; GC gc = (GC)0; Pixmap pixmap; XFontStruct *font;
Xunsigned int W = 640, H = 450; int D, gX = 100, gY = 100;
X
XBool Mono = 0, Gray = 0, Rv = 0, Clear = 0;
Xchar Name`5B64`5D = "gnuplot";
Xchar Class`5B64`5D = "Gnuplot";
X
Xint cx=0, cy=0, vchar, nc = 0, ncalloc = 0;
Xdouble xscale, yscale;
X#define X(x) (int) (x * xscale)
X#define Y(y) (int) ((4095-y) * yscale)
Xenum JUSTIFY `7B LEFT, CENTRE, RIGHT `7D jmode;
X
X#define Nbuf 1024
Xchar buf`5BNbuf`5D, **commands = (char **)0;
X
X#ifndef VMS
XFILE *X11_ipc = stdin;
Xchar X11_ipcpath`5B32`5D;
X#endif
X
X
X#ifdef VMS
X
XFILE *X11_ipc;
X
X#include <descrip.h>
X#include <iodef.h>
X#include <ssdef.h>
X
X/* channel to read from */
Xshort vms_input_chan;
X
X/* event flag used to signal completion of the read.
X   the flag must be in cluster 0, so $get_ef is kinda useless... */
Xconst int vms_input_efn = 4;
X
X/* flag set when we reach eof on the input channel */
Xint vms_input_eof = 0;
X
X/* an iosb for the read */
Xstruct vms_iosb
X`7B
X  short status;
X  short size;
X  int   dum;
X`7D vms_input_iosb;
X
X/* input buffer for read.
X   the length of this buffer is limited by the size of the mailbox
X   created in x11_vms.trm. */
Xchar vms_input_buf`5B160`5D;
X
X/* check a vms return status and signal if there was an error */
Xstatic int erck(int stat)
X`7B
X  if ((stat&1) == 0) lib$signal(stat);
X  return stat;
X`7D
X
X
X/* start a read on the input channel into VMS_INPUT_BUF.
X   event flag VMS_INPUT_EFN will be set when the read completes. */
X
Xvoid start_vms_input(void)
X`7B
X  erck( sys$qio(vms_input_efn, vms_input_chan, IO$_READVBLK, &vms_input_iosb
V,
X`09`090, 0, vms_input_buf, sizeof(vms_input_buf)-1,
X`09`090, 0, 0, 0) );
X`7D
X
X
X/* when exiting, close our input channel and commit suicide.
X   the somewhat drastic step of $delprc is taken to try to avoid the
X   possibility of stranded processes. */
X
Xvoid vms_exit()
X`7B
X  erck( sys$dassgn(vms_input_chan) );
X  erck( sys$delprc(0, 0) );
X`7D
X
X
X/* initialization: open a channel to sys$input, and start a read going on it
V.
X   also set up an exit handler which ensures that this process will die
X   when this program exits. */
X
Xvoid vms_init(void)
X`7B
X  static int dum;
X  static struct `7B
X    int flink;
X    void (*handler)();
X    int count;
X    int *condvalue;
X  `7D exhblk = `7B0, vms_exit, 0, &dum`7D;
X  $DESCRIPTOR(name_dsc, "SYS$INPUT");
X
X  erck( sys$assign(&name_dsc, &vms_input_chan, 0, 0) );
X  start_vms_input();
X  erck( sys$dclexh(&exhblk) );
X`7D
X
X
X/* this emulates fgets, but reads from our vms input channel instead of
X   a C stream */
X
Xint vms_fgets(char *buf, int bufsiz, FILE *dum)
X`7B
X  int len;
X
X  /* if we've already hit eof, return 0 */
X  if (vms_input_eof) return 0;
X
X  /* wait for the last read to complete */
X  erck( sys$waitfr(vms_input_efn) );
X
X  /* if it returned eof status, return 0 and set the eof flag */
X  if (vms_input_iosb.status == SS$_ENDOFFILE) `7B
X    vms_input_eof = 1;
X    return 0;
X  `7D
X  /* if it returned any other error, signal it */
X  if ((vms_input_iosb.status & 1) == 0) lib$signal(vms_input_iosb.status);
X
X  /* copy the input buffer to the caller's buffer
X     (or as much of it as will fit, anyway) */
X
X  len = vms_input_iosb.size;
X  if (len > bufsiz-1) len = bufsiz-1;
X  memcpy(buf, vms_input_buf, len);
X  buf`5Blen`5D = '\0';
X
X  /* start another read on the channel */
X  start_vms_input();
X  return len;
X`7D
X
X
Xint gethostname (char *buf, int buflen)
X`7B
X  static char system_name_saved`5B32`5D;
X  char *sp;
X  extern char *getenv ();
X  if ((sp = getenv ("SYS$NODE")) == 0)
X    sp = "vax-vms";
X
X  /* vms likes to stick a leading underscore in front of the node
X     name for some reason... */
X  if (*sp == '_') ++sp;
X  buf`5Bbuflen-1`5D = '\0';
X  strncpy (buf, sp, buflen);
X  if (strlen (sp) < buflen) buf`5Bstrlen (sp)`5D = '\0';
X `20
X  /* now hack off any trailing `60::' */
X  `7B
X    char *end;
X   `20
X    if ((end = index (buf, ':')) != 0)
X      *end = '\0';
X  `7D
X  return 0;
X`7D
X
X
X/* define some macros to map the C input routines to our asynchronous
X   vms input routines above */
X
X#define fgets vms_fgets
X#ifdef feof
X# undef feof
X#endif
X#define feof(dum) vms_input_eof
X#ifdef ferror
X# undef ferror
X#endif
X#define ferror(dum) 0
X
X#endif  /* VMS */
X
X/*--------------------------------------------------------------------------
V---
X *   main program`20
X *--------------------------------------------------------------------------
V-*/
X
Xmain(argc, argv) int argc; char *argv`5B`5D; `7B
X#ifdef VMS
X  vms_init ();
X#endif
X
X   preset(argc, argv);
X   mainloop();
X   exit(0);
X
X   `7D
X
X/*--------------------------------------------------------------------------
V---
X *   mainloop - process X events and input from gnuplot
X *
X *   On systems with a fully implemented select(), select is used (without
X *   timeout) to sense both input from the X server network connection and
X *   pipe input from gnuplot. On platforms with an incomplete or faulty`20
X *   select(), select (with timeout) is used for the server, and a temporary
V`20
X *   file rather than a pipe is used for gnuplot input.
X *--------------------------------------------------------------------------
V-*/
X
X#ifdef VMS
X
Xmainloop ()
X`7B
X  int ef_mask, stat, retval, rmask;
X  XEvent xe;
X
X  ef_mask = 1 << vms_input_efn;
X  while (1) `7B
X    stat = XMultiplexInput (1, &dpy, ef_mask, 1000000, 0, &retval);
X    while (XCheckMaskEvent (dpy, 0xffffffff, &xe)) `7B
X      if (xe.type == ConfigureNotify) resize (&xe);
X    `7D
X    sys$readef (0, &rmask);
X    if ((rmask & ef_mask) != 0) accept ();
X  `7D
X`7D
X
X#else /* not VMS */
X
Xmainloop() `7B
X   int nf, nfds, cn = ConnectionNumber(dpy), in = fileno(X11_ipc);
X   struct timeval timeout, *timer = (struct timeval *)0;
X   fd_set rset, tset;
X   unsigned long all = 0xffffffff;
X   XEvent xe;
X
X   FD_ZERO(&rset);
X   FD_SET(cn, &rset);
X
X#ifndef CRIPPLED_SELECT
X   FD_SET(in, &rset);
X   nfds = (cn > in) ? cn + 1 : in + 1;
X#else  /* CRIPPLED_SELECT */
X   timeout.tv_sec = 1;
X   timeout.tv_usec = 0;
X   timer = &timeout;
X   sprintf(X11_ipcpath, "/tmp/Gnuplot_%d", getppid());
X   nfds = cn + 1;
X#endif /* CRIPPLED_SELECT */
X
X   while(1) `7B
X      tset = rset;
X      nf = select(nfds, &tset, (fd_set *)0, (fd_set *)0, timer);
X      if (nf < 0) `7B
X`09 if (errno == EINTR) continue;
X`09 fprintf(stderr, "gnuplot: select failed. errno:%d\n", errno);
X`09 exit(1);
X`09 `7D
X      nf > 0 && XNoOp(dpy);
X      if (FD_ISSET(cn, &tset)) `7B
X`09 while (XCheckMaskEvent(dpy, all, &xe)) `7B
X`09    (xe.type == ConfigureNotify)  && resize(&xe);`20
X`09    `7D
X`09 `7D
X#ifndef CRIPPLED_SELECT
X      FD_ISSET(in, &tset) && accept();
X#else  /* CRIPPLED_SELECT */
X      if ((X11_ipc = fopen(X11_ipcpath, "r"))) `7B
X`09 unlink(X11_ipcpath);
X`09 accept();
X`09 fclose(X11_ipc);
X`09 `7D
X#endif /* CRIPPLED_SELECT */
X      `7D
X   `7D
X
X#endif /* not VMS */
X
X/*--------------------------------------------------------------------------
V---
X *   accept - accept & record new plot from gnuplot inboard X11 driver
X *--------------------------------------------------------------------------
V-*/
X
Xaccept() `7B
X
X   while (fgets(buf, Nbuf, X11_ipc)) `7B
X     if (*buf == 'G') `7B                           /* enter graphics mode *
V/
X`09 if (commands) `7B
X`09    int n; for (n=0; n<nc; n++) free(commands`5Bn`5D);
X`09    free(commands);
X`09    `7D
X`09 commands = (char **)0; nc = ncalloc = 0;
X         `7D
X      else if (*buf == 'E') `7B display(); break; `7D /* leave graphics mode
V */
X      else if (*buf == 'R') `7B exit(0); `7D          /* leave X11/x11 mode
V  */
X      else `7B                                      /* record command      *
V/
X`09 char *p;
X`09 if (nc >= ncalloc) `7B
X`09    ncalloc = ncalloc*2 + 1;
X`09    commands = (commands)
X`09       ? (char **)realloc(commands, ncalloc * sizeof(char *))
X`09       : (char **)malloc(sizeof(char *));
X`09    `7D
X`09 p = (char *)malloc((unsigned)strlen(buf)+1);
X`09 if (!commands `7C`7C !p) `7B
X`09    fprintf(stderr, "gnuplot: can't get memory. X11 aborted.\n");
X`09    exit(1);
X`09    `7D
X`09 commands`5Bnc++`5D = strcpy(p, buf);
X`09 `7D
X      `7D
X   if (feof(X11_ipc) `7C`7C ferror(X11_ipc)) exit(1);
X   `7D
X
X/*--------------------------------------------------------------------------
V---
X *   display - display last plot from gnuplot inboard X11 driver
X *--------------------------------------------------------------------------
V-*/
X
Xdisplay() `7B
X   int n, x, y, sw, sl, lt, width, type;
X   char *buf, *str;
X
X   if (!nc) return;
X
X   /* set scaling factor between internal driver & window geometry */
X   xscale = (double)W / 4096.;  yscale = (double)H / 4096.; `20
X
X   /* create new pixmap & GC */
X   if (gc) `7B XFreeGC(dpy, gc); XFreePixmap(dpy, pixmap); `7D
X   pixmap = XCreatePixmap(dpy, root, W, H, D);
X   gc = XCreateGC(dpy, pixmap, 0, (XGCValues *)0);
X   XSetFont(dpy, gc, font->fid);
X
X   /* set pixmap background */
X   XSetForeground(dpy, gc, colors`5B0`5D);
X   XFillRectangle(dpy, pixmap, gc, 0, 0, W, H);
X   XSetBackground(dpy, gc, colors`5B0`5D);
X
X   /* set new pixmap as window background */
X   XSetWindowBackgroundPixmap(dpy, win, pixmap);
X
X   /* momentarily clear the window first if requested */
X   if (Clear) `7B
X      XClearWindow(dpy, win);
X      XFlush(dpy);
X      `7D
X
X   /* loop over accumulated commands from inboard driver */
X   for (n=0; n<nc; n++) `7B
X      buf = commands`5Bn`5D;
X
X      /*   X11_vector(x,y) - draw vector  */
X      if (*buf == 'V') `7B`20
X`09 sscanf(buf, "V%4d%4d", &x, &y); `20
X`09 XDrawLine(dpy, pixmap, gc, X(cx), Y(cy), X(x), Y(y));
X`09 cx = x; cy = y;
X`09 `7D
X
X      /*   X11_move(x,y) - move  */
X      else if (*buf == 'M')`20
X`09 sscanf(buf, "M%4d%4d", &cx, &cy); `20
X
X      /*   X11_put_text(x,y,str) - draw text   */
X      else if (*buf == 'T') `7B`20
X`09 sscanf(buf, "T%4d%4d", &x, &y); `20
X`09 str = buf + 9; sl = strlen(str) - 1;
X`09 sw = XTextWidth(font, str, sl);
X`09 switch(jmode) `7B
X`09    case LEFT:   sw = 0;     break;
X`09    case CENTRE: sw = -sw/2; break;
X`09    case RIGHT:  sw = -sw;   break;
X`09    `7D
X`09 XSetForeground(dpy, gc, colors`5B2`5D);
X`09 XDrawString(dpy, pixmap, gc, X(x)+sw, Y(y)+vchar/3, str, sl);
X`09 XSetForeground(dpy, gc, colors`5Blt+3`5D);
X`09 `7D
X
X      /*   X11_justify_text(mode) - set text justification mode  */
X      else if (*buf == 'J')`20
X`09 sscanf(buf, "J%4d", &jmode);
X
X      /*   X11_linetype(type) - set line type  */
X      else if (*buf == 'L') `7B`20
X`09 sscanf(buf, "L%4d", &lt);
X`09 lt = (lt%8)+2;
X`09 width = (lt == 0) ? 2 : 0;
X`09 if (!Mono) `7B
X`09    if (lt != 1)`20
X`09       type = LineSolid;
X`09    else `7B
X`09       type = LineOnOffDash;
X`09       XSetDashes(dpy, gc, 0, dashes`5Blt`5D, strlen(dashes`5Blt`5D));
X`09       `7D
X`09    XSetForeground(dpy, gc, colors`5Blt+3`5D);
X`09    `7D
X`09 else `7B
X`09    type  = (lt == 0 `7C`7C lt == 2) ? LineSolid : LineOnOffDash;
X`09    if (dashes`5Blt`5D`5B0`5D)
X`09       XSetDashes(dpy, gc, 0, dashes`5Blt`5D, strlen(dashes`5Blt`5D));
X`09    `7D
X`09 XSetLineAttributes( dpy,gc, width, type, CapButt, JoinBevel);
X`09 `7D
X      `7D
X
X   /* trigger exposure of background pixmap */
X   XClearWindow(dpy,win);
X   XFlush(dpy);
X   `7D
X
X/*--------------------------------------------------------------------------
V---
X *   resize - rescale last plot if window resized
X *--------------------------------------------------------------------------
V-*/
X
XBool init = True;
X
Xresize(xce) XConfigureEvent *xce; `7B
X   if (!init `7C`7C xce->width != W `7C`7C xce->height != H) `7B
X      W = xce->width; H = xce->height;
X      display();
X      init = True;
X      `7D
X   `7D
X
X
X/*--------------------------------------------------------------------------
V---
X *   preset - determine options, open display, create window
X *--------------------------------------------------------------------------
V-*/
X
X#define On(v) ( !strcmp(v,"on") `7C`7C !strcmp(v,"true") `7C`7C \
X`09`09!strcmp(v,"On") `7C`7C !strcmp(v,"True") )
X
X#define AppDefDir "/usr/lib/X11/app-defaults"
X#ifndef MAXHOSTNAMELEN
X#define MAXHOSTNAMELEN 64
X#endif
X
Xstatic XrmDatabase dbCmd, dbApp, dbDef, dbEnv, db = (XrmDatabase)0;
X
Xchar *pr_GetR(), *getenv(), *type`5B20`5D;
XXrmValue value;
X
X#define Nopt 25
Xstatic XrmOptionDescRec options`5B`5D = `7B
X   `7B"-mono",             ".mono",             XrmoptionNoArg,   "on" `7D,
X   `7B"-gray",             ".gray",             XrmoptionNoArg,   "on" `7D,
X   `7B"-clear",            ".clear",            XrmoptionNoArg,   "on" `7D,
X   `7B"-display",          ".display",          XrmoptionSepArg,  NULL `7D,
X   `7B"-name",             ".name",             XrmoptionSepArg,  NULL `7D,
X   `7B"-geometry",         "*geometry",         XrmoptionSepArg,  NULL `7D,
X   `7B"-background",       "*background",       XrmoptionSepArg,  NULL `7D,
X   `7B"-bg",               "*background",       XrmoptionSepArg,  NULL `7D,
X   `7B"-foreground",       "*foreground",       XrmoptionSepArg,  NULL `7D,
X   `7B"-fg",               "*foreground",       XrmoptionSepArg,  NULL `7D,
X   `7B"-bordercolor",      "*bordercolor",      XrmoptionSepArg,  NULL `7D,
X   `7B"-bd",               "*bordercolor",      XrmoptionSepArg,  NULL `7D,
X   `7B"-borderwidth",      ".borderwidth",      XrmoptionSepArg,  NULL `7D,
X   `7B"-bw",               ".borderwidth",      XrmoptionSepArg,  NULL `7D,
X   `7B"-font",             "*font",             XrmoptionSepArg,  NULL `7D,
X   `7B"-fn",               "*font",             XrmoptionSepArg,  NULL `7D,
X   `7B"-reverse",          "*reverseVideo",     XrmoptionNoArg,   "on" `7D,
X   `7B"-rv",               "*reverseVideo",     XrmoptionNoArg,   "on" `7D,
X   `7B"+rv",               "*reverseVideo",     XrmoptionNoArg,   "off"`7D,
X   `7B"-iconic",           "*iconic",           XrmoptionNoArg,   "on" `7D,
X   `7B"-synchronous",      "*synchronous",      XrmoptionNoArg,   "on" `7D,
X   `7B"-xnllanguage",      "*xnllanguage",      XrmoptionSepArg,  NULL `7D,
X   `7B"-selectionTimeout", "*selectionTimeout", XrmoptionSepArg,  NULL `7D,
X   `7B"-title",            ".title",            XrmoptionSepArg,  NULL `7D,
X   `7B"-xrm",              NULL,                XrmoptionResArg,  NULL `7D,
X   `7D;
X
Xpreset(argc, argv) int argc; char *argv`5B`5D; `7B
X   int Argc = argc; char **Argv = argv;
X
X   char *display = getenv("DISPLAY"),  *home = getenv("HOME");
X   char *server_defaults, *env, buf`5B256`5D;
X
X   /*---set to ignore `5EC and `5EZ-----------------------------------------
V-----*/
X
X   signal(SIGINT, SIG_IGN);
X#ifdef SIGTSTP
X   signal(SIGTSTP, SIG_IGN);
X#endif
X
X   /*---prescan arguments for "-name"---------------------------------------
V-*/
X
X   while(++Argv, --Argc > 0) `7B
X      if (!strcmp(*Argv, "-name") && Argc > 1) `7B
X`09 strncpy(Name, Argv`5B1`5D, 64);
X`09 strncpy(Class, Argv`5B1`5D, 64);
X`09 if (Class`5B0`5D >= 'a' && Class`5B0`5D <= 'z') Class`5B0`5D -= 0x20;
X`09 `7D
X      `7D
X   Argc = argc; Argv = argv;
X
X   /*---parse command line--------------------------------------------------
V-*/
X
X   XrmInitialize();
X   XrmParseCommand(&dbCmd, options, Nopt, Name, &Argc, Argv);
X   if (Argc > 1) `7B
X      fprintf(stderr, "\ngnuplot: bad option: %s\n", Argv`5B1`5D);
X      fprintf(stderr, "gnuplot: X11 aborted.\n");
X      exit(1);
X      `7D
X   if (pr_GetR(dbCmd, ".display")) display = value.addr;
X
X   /*---open display--------------------------------------------------------
V-*/
X
X   dpy = XOpenDisplay(display);`20
X   if (!dpy) `7B
X      fprintf(stderr, "\ngnuplot: unable to open display '%s'\n", display);
X      fprintf(stderr, "gnuplot: X11 aborted.\n");
X      exit(1);
X      `7D
X   scr = DefaultScreen(dpy);
X   vis = DefaultVisual(dpy,scr);
X   D = DefaultDepth(dpy,scr);
X   root = DefaultRootWindow(dpy);
X   server_defaults = XResourceManagerString(dpy);
X
X   /*---get application defaults--(subset of Xt processing)-----------------
V-*/
X
X   sprintf(buf, "%s/%s", AppDefDir, "Gnuplot");
X   dbApp = XrmGetFileDatabase(buf);
X   XrmMergeDatabases(dbApp, &db);
X
X   /*---get server or `7E/.Xdefaults----------------------------------------
V---*/
X
X   if (server_defaults)
X      dbDef = XrmGetStringDatabase(server_defaults);
X   else `7B
X      sprintf(buf, "%s/.Xdefaults", home);
X      dbDef = XrmGetFileDatabase(buf);
X      `7D
X   XrmMergeDatabases(dbDef, &db);
X
X   /*---get XENVIRONMENT or  `7E/.Xdefaults-hostname------------------------
V---*/
X
X   if (env = getenv("XENVIRONMENT"))`20
X      dbEnv = XrmGetFileDatabase(env);
X   else `7B
X      char *p, host`5BMAXHOSTNAMELEN`5D;
X      if (gethostname(host, MAXHOSTNAMELEN) < 0) `7B
X`09 fprintf(stderr, "gnuplot: gethostname failed. X11 aborted.\n");
X`09 exit(1);
X`09 `7D
X      if (p = index(host, '.')) *p = '\0';
X      sprintf(buf, "%s/.Xdefaults-%s", home, host);
X      dbEnv = XrmGetFileDatabase(buf);
X      `7D
X   XrmMergeDatabases(dbEnv, &db);
X
X   /*---merge command line options------------------------------------------
V-*/
X
X   XrmMergeDatabases(dbCmd, &db);
X
X   /*---determine geometry, font and colors---------------------------------
V-*/
X
X   pr_geometry();
X   pr_font();
X   pr_color();
X
X   /*---create window-------------------------------------------------------
V-*/
X
X   pr_window();
X
X   `7D`20
X
X/*--------------------------------------------------------------------------
V---
X *   pr_GetR - get resource from database using "-name" option (if any)
X *--------------------------------------------------------------------------
V-*/
X
Xchar *
Xpr_GetR(db, resource) XrmDatabase db; char *resource; `7B
X   char name`5B128`5D, class`5B128`5D, *rc;
X
X   strcpy(name, Name); strcat(name, resource);
X   strcpy(class, Class); strcat(class, resource);
X   rc = XrmGetResource(db, name, class, type, &value)
X      ? (char *)value.addr`20
X      : (char *)0;
X   return(rc);
X   `7D
X
X/*--------------------------------------------------------------------------
V---
X *   pr_color - determine color values
X *--------------------------------------------------------------------------
V-*/
X
Xchar color_keys`5BNcolors`5D`5B30`5D =   `7B`20
X   "background", "bordercolor", "text", "border", "axis",`20
X   "line1", "line2", "line3",  "line4",`20
X   "line5", "line6", "line7",  "line8"`20
X   `7D;
Xchar color_values`5BNcolors`5D`5B30`5D = `7B`20
X   "white", "black",  "black",  "black",  "black",`20
X   "red",   "green",  "blue",   "magenta",`20
X   "cyan",  "sienna", "orange", "coral"`20
X   `7D;
Xchar gray_values`5BNcolors`5D`5B30`5D = `7B`20
X   "black",   "white",  "white",  "gray50", "gray50",
X   "gray100", "gray60", "gray80", "gray40",`20
X   "gray90",  "gray50", "gray70", "gray30"`20
X   `7D;
X
Xpr_color() `7B
X   unsigned long black = BlackPixel(dpy, scr), white = WhitePixel(dpy,scr);
X   char option`5B20`5D, *v, *type = (Gray) ? "Gray" : "Color";
X   XColor used, exact;
X   Colormap cmap;
X   int n;
X
X   pr_GetR(db, ".mono")         && On(value.addr) && Mono++;
X   pr_GetR(db, ".gray")         && On(value.addr) && Gray++;
X   pr_GetR(db, ".reverseVideo") && On(value.addr) && Rv++;
X
X   if (!Gray && (vis->class == GrayScale `7C`7C vis->class == StaticGray)) M
Vono++;
X
X   if (!Mono) `7B
X      cmap = DefaultColormap(dpy, scr);
X      for (n=0; n<Ncolors; n++) `7B
X`09 strcpy(option, ".");
X`09 strcat(option, color_keys`5Bn`5D);
X`09 (n > 1) && strcat(option, type);
X`09 v = pr_GetR(db, option)`20
X`09     ? value.addr
X`09     : ((Gray) ? gray_values`5Bn`5D : color_values`5Bn`5D);
X`09 if (XAllocNamedColor(dpy, cmap, v, &used, &exact))
X`09    colors`5Bn`5D = used.pixel;
X`09 else `7B
X`09    fprintf(stderr, "\ngnuplot: can't allocate %s:%s\n", option, v);
X`09    fprintf(stderr, "gnuplot: reverting to monochrome\n");
X`09    Mono++; break;
X`09    `7D
X`09 `7D
X      `7D
X   if (Mono) `7B
X      colors`5B0`5D = (Rv) ? black : white ;
X      for (n=1; n<Ncolors; n++)  colors`5Bn`5D = (Rv) ? white : black;
X      `7D
X   `7D
X
X/*--------------------------------------------------------------------------
V---
X *   pr_font - determine font         `20
X *--------------------------------------------------------------------------
V-*/
X
Xpr_font() `7B
X   char *fontname = pr_GetR(db, ".font");
X
X   if (!fontname) fontname = FallbackFont;
X   font = XLoadQueryFont(dpy, fontname);
X   if (!font) `7B
X      fprintf(stderr, "\ngnuplot: can't load font '%s'\n", fontname);
X      fprintf(stderr, "gnuplot: using font '%s' instead.\n", FallbackFont);
X      font = XLoadQueryFont(dpy, FallbackFont);
X      if (!font) `7B
X`09 fprintf(stderr, "gnuplot: can't load font '%s'\n", FallbackFont);
X`09 fprintf(stderr, "gnuplot: no useable font - X11 aborted.\n");
X         exit(1);
X`09 `7D
X      `7D
X   vchar = font->ascent + font->descent;
X   `7D
X
X/*--------------------------------------------------------------------------
V---
X *   pr_geometry - determine window geometry     `20
X *--------------------------------------------------------------------------
V-*/
X
Xpr_geometry() `7B
X   char *geometry = pr_GetR(db, ".geometry");
X   int x, y, flags;
X   unsigned int w, h;`20
X
X   if (geometry) `7B
X      flags = XParseGeometry(geometry, &x, &y, &w, &h);
X
X      if (flags & WidthValue)  W = w;
X      if (flags & HeightValue) H = h;
X      if (flags & XValue) `7B
X`09 if (flags & XNegative) x += DisplayWidth(dpy,scr);
X`09 gX = x;
X`09 `7D
X      if (flags & YValue) `7B
X`09 if (flags & YNegative) y += DisplayHeight(dpy,scr);
X`09 gY = y;
X`09 `7D
X      `7D
X   `7D
X
X/*--------------------------------------------------------------------------
V---
X *   pr_window - create window`20
X *--------------------------------------------------------------------------
V-*/
X
Xpr_window() `7B
X   char *title =  pr_GetR(db, ".title");
X   XSizeHints hints;
X
X   win = XCreateSimpleWindow(dpy, root, gX, gY, W, H, 2, colors`5B1`5D, colo
Vrs`5B0`5D);
X
X   pr_GetR(db, ".clear") && On(value.addr) && Clear++;
X
X   hints.flags = PPosition;
X   hints.x = gX; hints.y = gY;
X   XSetNormalHints(dpy, win, &hints);
X
X   if (pr_GetR(db, ".iconic") && On(value.addr)) `7B
X      XWMHints wmh;
X
X      wmh.flags = StateHint ;
X      wmh.initial_state = IconicState;
X      XSetWMHints(dpy, win, &wmh);
X      `7D`20
X
X   XStoreName(dpy, win, ((title) ? title : Class));
X
X   XSelectInput(dpy, win, StructureNotifyMask);
X   XMapWindow(dpy, win);
X  `20
X   `7D
$ CALL UNPACK GNUPLOT_X11.C;7 1210028340
$ create 'f'
X/*
X *    x11-vms.trm  --- inboard terminal driver for X11 (VMS version) - sss
X */
X
X#define X11_XMAX 4096
X#define X11_YMAX 4096
X
X/* approximations for typical font/screen sizes */
X#define X11_VCHAR (X11_YMAX/25)`20
X#define X11_HCHAR (X11_XMAX/100)`20
X#define X11_VTIC (X11_YMAX/100)
X#define X11_HTIC (X11_XMAX/150)
X
X#define X11_nopts 25
Xchar X11_opts`5BX11_nopts`5D`5B20`5D = `7B
X   "-mono", "-gray", "-clear",
X   "-iconic", "-rv", "-reverse", "+rv", "-synchronous",`20
X   "-display", "-geometry", "-bg", "-background", "-bd", "-bordercolor", "-b
Vw",
X   "-borderwidth", "-fg", "-foreground", "-fn", "-font", "-name",`20
X   "-selectionTimeout", "-title", "-xnllanguage", "-xrm"`20
X   `7D;
Xint X11_optarg`5BX11_nopts`5D = `7B`20
X   0, 0, 0,
X   0, 0, 0, 0, 0,
X   1, 1, 1, 1, 1, 1, 1,
X   1, 1, 1, 1, 1, 1,`20
X   1, 1, 1, 1
X   `7D;
X
X/* command to start the outboard driver */
Xchar X11_command`5B1024`5D= "mcr gnuplot_dir:gnuplot_x11 -name gnuplot";
X
X
X/*   X11_args - scan gnuplot command line for standard X Toolkit options */
X
XX11_args(argc, argv) int argc; char *argv`5B`5D; `7B
X   int nx11 = 0, n;
X
X   while(++argv, --argc > 0) `7B
X      for (n=0; n<X11_nopts; n++) `7B
X`09 if (!strcmp(*argv, X11_opts`5Bn`5D)) `7B
X`09    strcat(X11_command, " ");
X`09    strcat(X11_command, *argv);`20
X`09    if (X11_optarg`5Bn`5D) `7B
X`09       if (--argc <= 0) return(nx11);
X`09       strcat(X11_command, " \"");
X`09       strcat(X11_command, *++argv);`20
X`09       strcat(X11_command, "\"");
X`09       nx11++;
X`09       `7D
X`09    nx11++; break;
X`09    `7D
X`09 `7D
X      if (n == X11_nopts) break;`20
X      `7D
X   return(nx11);
X   `7D
X
X
X#include <dvidef.h>
X#include <iodef.h>
X#include <descrip.h>
X#include <clidef.h>
X
X#define MSGSIZE 160
Xstatic int mbx_chan;
Xstatic int x11_on = 0;
X
Xint X11_reset ();
X
Xstatic int exh_retstat;
Xstatic struct `7B
X  int flink;
X  int (*handler)();
X  int count;
X  int *condvalue;
X`7D exhblk = `7B0, X11_reset, 0, &exh_retstat`7D;
X
Xint erck(int stat)
X`7B
X  if ((stat&1) == 0) lib$signal(stat);
X  return stat;
X`7D
X
Xvoid mbx_close(void)
X`7B
X  erck( sys$qio(0, mbx_chan, IO$_WRITEOF,
X`09`090, 0, 0, 0, 0, 0, 0, 0, 0) );
X  erck( sys$dassgn(mbx_chan) );
X`7D
X
Xvoid create_mbx (int *chan)
X`7B
X  erck( sys$crembx (0, chan, MSGSIZE, 2 * MSGSIZE, 0, 0, 0) );
X`7D
X
Xvoid vms_get_device_name(short chan, struct dsc$descriptor_s *dsc)
X`7B
X  int dum_DVI$_DEVNAM = DVI$_DEVNAM;
X
X  erck( lib$getdvi (&dum_DVI$_DEVNAM, &chan, 0, 0, dsc, &dsc->dsc$w_length)
V );
X`7D
X
XX11_init()`20
X`7B
X  int dum_CLI$M_NOWAIT = CLI$M_NOWAIT;
X  char inbuf`5B32`5D;
X  $DESCRIPTOR(indsc, inbuf);
X  $DESCRIPTOR(outdsc, "sys$output");
X  $DESCRIPTOR(cmddsc, "");
X  create_mbx(&mbx_chan);
X  vms_get_device_name(mbx_chan, &indsc);
X
X  cmddsc.dsc$w_length = strlen(X11_command);
X  cmddsc.dsc$a_pointer = X11_command;
X
X  erck( lib$spawn(&cmddsc, &indsc, &outdsc, &dum_CLI$M_NOWAIT) );
X
X  erck( sys$dclexh(&exhblk) );
X  x11_on = 1;
X`7D
X
Xvoid mbx_write(char *buf)
X`7B
X  erck( sys$qiow(0, mbx_chan, IO$_WRITEVBLK `7C IO$M_NOW,
X`09`09 0, 0, 0, buf, strlen(buf), 0, 0, 0, 0) );
X`7D
X
X
Xint X11_reset()
X`7B
X  if (x11_on) `7B
X    x11_on = 0;
X    erck( sys$canexh(&exhblk) );
X    mbx_write("R\n");
X    mbx_close();
X  `7D
X`7D
X
XX11_text() `7B mbx_write("E\n"); `7D
X
XX11_graphics() `7B mbx_write("G\n"); `7D
X
XX11_move(x,y) unsigned int x,y;
X`7B
X  char buf`5B12`5D;
X  sprintf(buf, "M%04d%04d\n", x, y);
X  mbx_write(buf);
X`7D
X
XX11_vector(x,y) unsigned int x,y;
X`7B
X  char buf`5B12`5D;
X  sprintf(buf, "V%04d%04d\n", x, y);
X  mbx_write(buf);
X`7D
X
XX11_linetype(lt) int lt;`20
X`7B
X  char buf`5B12`5D;
X  sprintf(buf, "L%04d\n", lt);
X  mbx_write(buf);
X`7D
X
XX11_put_text(x,y,str) unsigned int x,y; char str`5B`5D;
X`7B
X  extern void *malloc();
X  char *buf = (char *)malloc(strlen(str) + 12);
X  if (buf == NULL) abort();
X  sprintf(buf, "T%04d%04d%s\n", x, y, str);
X  mbx_write(buf);
X  free(buf);
X`7D
X
XX11_justify_text(mode) enum JUSTIFY mode;
X`7B
X  char buf`5B12`5D;
X  sprintf(buf, "J%04d\n", mode);
X  mbx_write(buf);
X  return(TRUE);
X`7D
$ CALL UNPACK X11-VMS.TRM;5 192512603
$ v=f$verify(v)
$ EXIT
Newsgroups: comp.os.vms