--- xplot.c.orig	Wed Nov 19 18:35:43 2003
+++ xplot.c	Wed Nov 19 18:35:42 2003
@@ -46,7 +46,7 @@
 #include <X11/cursorfont.h>
 #include <math.h>
 #include <ctype.h>
-#include <malloc.h>
+#include <stdlib.h>
 #include "xplot.h"
 #include "coord.h"
 
@@ -813,9 +813,7 @@
     static struct dpy_info {
       unsigned long line_plane_mask;
       Colormap clr_map;
-      XColor clr;
-      unsigned long pixel[NCOLORS];
-      int Colors[NCOLORS];
+      unsigned long Colors[NCOLORS];
       int virgin;
       Atom xplot_nagle_atom;
       Display *saved_dpy;
@@ -842,38 +840,37 @@
     /* Allocate some color cells */
       
     if (d_i[d].virgin ) {
+      int usewhite;
+
       d_i[d].virgin = 0;
       
       d_i[d].xplot_nagle_atom = XInternAtom(pl->dpy, "XPLOT_NAGLE", False);
 
       d_i[d].clr_map = DefaultColormap(pl->dpy, DefaultScreen(pl->dpy));
       
-      if ( option_mono ||
+      if ( option_mono ) {
+	usewhite = 1;
+      } else {
+	usewhite = 0;
 
-	   ! XAllocColorCells(pl->dpy, 
-			      d_i[d].clr_map, 0,
-			      &d_i[d].line_plane_mask, 1,
-			      d_i[d].pixel, NColors) )
-	{
-	  /* probably only one bit plane, or all the color cells are taken */
-#if 1
-	  if (! option_mono)
-	    fputs("XAllocColorCells failed, will only have one plot color\n",
-		  stderr);
-#endif
-	  for ( i = 0; i < NColors; i++) {
-	    d_i[d].Colors[i] = WhitePixelOfScreen(pl->screen);
-	  }
-	} else {
-	  for (i = 0; i < NColors; i++)  {
-	    XParseColor(pl->dpy, d_i[d].clr_map, ColorNames[i], &d_i[d].clr);
-	    d_i[d].clr.pixel = d_i[d].pixel[i];
-	    XStoreColor (pl->dpy, d_i[d].clr_map, &d_i[d].clr);
-	    d_i[d].clr.pixel |= d_i[d].line_plane_mask;
-	    XStoreColor (pl->dpy, d_i[d].clr_map, &d_i[d].clr);
-	    d_i[d].Colors[i] = d_i[d].clr.pixel;
+	for (i = 0; i < NColors; i++)  {
+	  XColor alloc, exact;
+
+	  if (XAllocNamedColor(pl->dpy, d_i[d].clr_map, ColorNames[i],
+		      &alloc, &exact) == 0) {
+	    XFreeColors(pl->dpy, d_i[d].clr_map, d_i[d].Colors, i, NULL);
+	    usewhite = 1;
+	    fprintf(stderr, "unable to allocate color \"%s\", falling back to only one plot color\n", ColorNames[i]);
+	    break;
 	  }
+	  d_i[d].Colors[i] = alloc.pixel;
 	}
+      }
+      if ( usewhite ) {
+	for ( i = 0; i < NColors; i++) {
+	  d_i[d].Colors[i] = WhitePixelOfScreen(pl->screen);
+	}
+      }
     }
     
     for (i = 0; i < NColors; i++) {
@@ -3154,11 +3151,7 @@
    * GDT: 950310: put dot font hack in.
    */
 
-#ifdef SDO
   fputs("%!PS\n", fp);
-#else
-  fputs("%!PostScript\n", fp);
-#endif
   /* Bracket the PS program with gsave/grestore so these page descriptions
      can be concatenated, then printed. */
   fputs("%%BoundingBox: ", fp);
@@ -3168,146 +3161,146 @@
   fputs("gsave\n", fp);
 
   /* Set up scale */
-  fputs("
-%/sign { dup 0 gt { pop 1 } { 0 lt { -1 } { 0 } ifelse } ifelse } def
-
-%matrix currentmatrix
-%aload pop
-%6 2 roll sign
-%6 1 roll sign
-%6 1 roll sign
-%6 1 roll sign
-%6 1 roll
-%matrix astore setmatrix
-
-0.12 dup scale
-
+  fputs("\n\
+%/sign { dup 0 gt { pop 1 } { 0 lt { -1 } { 0 } ifelse } ifelse } def\n\
+\n\
+%matrix currentmatrix\n\
+%aload pop\n\
+%6 2 roll sign\n\
+%6 1 roll sign\n\
+%6 1 roll sign\n\
+%6 1 roll sign\n\
+%6 1 roll\n\
+%matrix astore setmatrix\n\
+\n\
+0.12 dup scale\n\
+\n\
 ", fp);
 
   fprintf(fp, "/theta {%d mul} def\n", ( (state == PRINTING) ? 4 : 2));
 
   /* Set up units of measurement. */
   fprintf(fp, "/inch {%d mul} def\n", PER_INCH);
-  fputs("/pt {inch 72 div} def
-%
-%
-/tfont /Times-Bold findfont 12 pt scalefont def
-%
-/lfont /Times-Roman findfont 10 pt scalefont def
-%
-%tfont /FontBBox get
-%  aload pop 
-%  tfont /FontMatrix get dtransform pop /tascent exch def
-%  tfont /FontMatrix get dtransform pop neg /tdescent exch def
-lfont /FontBBox get
-  aload pop 
-  lfont /FontMatrix get dtransform pop 0.65 mul /lascent exch def
-  lfont /FontMatrix get dtransform pop neg /ldescent exch def
-% begin gdt mod
-% define font for xplot characters
-/BuildCharDict 10 dict def
-/Xplotfontdict 7 dict def
-Xplotfontdict begin
-  /FontType 3 def
-  /FontMatrix [1 0 0 1 0 0] def
-  /FontBBox [-1 -1 1 1]def
-  /Encoding 256 array def
-  0 1 255 {Encoding exch /.notdef put} for
-  Encoding (.) 0 get /xplotfontdot put
-  /CharacterDefs 3 dict def
-  CharacterDefs /.notdef {} put
-  CharacterDefs /xplotfontdot
-    { newpath
-	0 0 1 0 360 arc fill
-    } put
-  /BuildChar
-    { BuildCharDict begin
-	/char exch def
-	/fontdict exch def
-	/charname fontdict /Encoding get
-	char get def
-	/charproc fontdict /CharacterDefs
-        get charname get def
-	1 0 -1 -1 1 1 setcachedevice
-	gsave charproc grestore
-      end
-    } def
-end
-/XplotFont Xplotfontdict definefont pop
-% scale font according to theta
-/dotsetup { /dotfont /XplotFont findfont 4 theta scalefont def } def
-% DONE gdt mod
-%define procedures for each xplot primitive.
-% x y x --
-/x {moveto 8 8 rlineto -16 -16 rlineto
-    8 8 rmoveto
-    -8 8 rlineto 16 -16 rlineto} def
-% x y + --
-/+ {moveto 0 8 rlineto 0 -16 rlineto
-    0 8 rmoveto
-    -8 0 rlineto 16 0 rlineto} def
-% x y ?arrow --
-/darrow {moveto 8 theta 8 theta rmoveto -8 theta -8 theta rlineto
-         -8 theta 8 theta rlineto } def
-/uarrow {moveto -8 theta -8 theta rmoveto 8 theta 8 theta rlineto
-         8 theta -8 theta rlineto } def
-/rarrow {moveto -8 theta 8 theta rmoveto 8 theta -8 theta rlineto
-         -8 theta -8 theta rlineto } def
-/larrow {moveto 8 theta 8 theta rmoveto -8 theta -8 theta rlineto
-         8 theta -8 theta rlineto } def
-%x y x y line --
-/line {moveto lineto} def
-%x y dot --
-% begin gdt mod
-/dot { moveto dotfont setfont (.) show } def
-%/dot {stroke 8 theta 0 360 arc fill} def
-% end gdt mod
-%x y plus --
-/plus {moveto -16 theta 0 rmoveto 16 theta 0 rlineto 
-       -16 theta -16 theta rmoveto 0 16 theta rlineto} def
-%x y box --
-/box {moveto -8 theta -8 theta rmoveto
-      16 theta 0 rlineto 
-      0 16 theta rlineto
-      -16 theta 0 rlineto
-      0 -16 theta rlineto} def
-%x y diamond --
-/diamond { moveto 0 theta 24 theta rmoveto
-           -24 theta -24 theta rlineto
-            24 theta -24 theta rlineto
-            24 theta  24 theta rlineto
-           -24 theta  24 theta rlineto} def
-%x y ?tick --
-/utick {moveto 0 6 theta rlineto} def
-/dtick {moveto 0 -6 theta rlineto} def
-/ltick {moveto -6 theta 0 rlineto} def
-/rtick {moveto 6 theta 0 rlineto} def
-/htick {moveto -6 theta 0 rmoveto 12 theta 0 rlineto} def
-/vtick {moveto 0 -6 theta rmoveto 0 12 theta rlineto} def
-%Separate functions for each text position.
-%x y string ?text --
-/space 6 pt def
-% Set the font, figure out the width.
-% x y string tsetup string x width y
-/tsetup {lfont setfont dup stringwidth pop exch
-         4 1 roll exch} def
-%CENTER
-/ctext {tsetup lascent 2 div sub
-        3 1 roll 2 div sub exch
-% stack should now be string x y
-        moveto show} def
-%ABOVE
-/atext {tsetup space ldescent add add
-        3 1 roll 2 div sub exch moveto show} def
-%BELOW
-/btext {tsetup space lascent add sub
-        3 1 roll 2 div sub exch moveto show} def
-%TO_THE_LEFT
-/ltext {tsetup lascent 2 div sub
-        3 1 roll space add sub exch moveto show} def
-%TO_THE_RIGHT
-/rtext {tsetup lascent 2 div sub
-        3 1 roll pop space add exch moveto show} def
+  fputs("/pt {inch 72 div} def\n\
+%\n\
+%\n\
+/tfont /Times-Bold findfont 12 pt scalefont def\n\
+%\n\
+/lfont /Times-Roman findfont 10 pt scalefont def\n\
+%\n\
+%tfont /FontBBox get\n\
+%  aload pop\n\
+%  tfont /FontMatrix get dtransform pop /tascent exch def\n\
+%  tfont /FontMatrix get dtransform pop neg /tdescent exch def\n\
+lfont /FontBBox get\n\
+  aload pop\n\
+  lfont /FontMatrix get dtransform pop 0.65 mul /lascent exch def\n\
+  lfont /FontMatrix get dtransform pop neg /ldescent exch def\n\
+% begin gdt mod\n\
+% define font for xplot characters\n\
+/BuildCharDict 10 dict def\n\
+/Xplotfontdict 7 dict def\n\
+Xplotfontdict begin\n\
+  /FontType 3 def\n\
+  /FontMatrix [1 0 0 1 0 0] def\n\
+  /FontBBox [-1 -1 1 1]def\n\
+  /Encoding 256 array def\n\
+  0 1 255 {Encoding exch /.notdef put} for\n\
+  Encoding (.) 0 get /xplotfontdot put\n\
+  /CharacterDefs 3 dict def\n\
+  CharacterDefs /.notdef {} put\n\
+  CharacterDefs /xplotfontdot\n\
+    { newpath\n\
+	0 0 1 0 360 arc fill\n\
+    } put\n\
+  /BuildChar\n\
+    { BuildCharDict begin\n\
+	/char exch def\n\
+	/fontdict exch def\n\
+	/charname fontdict /Encoding get\n\
+	char get def\n\
+	/charproc fontdict /CharacterDefs\n\
+        get charname get def\n\
+	1 0 -1 -1 1 1 setcachedevice\n\
+	gsave charproc grestore\n\
+      end\n\
+    } defn\n\
+end\n\
+/XplotFont Xplotfontdict definefont pop\n\
+% scale font according to theta\n\
+/dotsetup { /dotfont /XplotFont findfont 4 theta scalefont def } def\n\
+% DONE gdt mod\n\
+%define procedures for each xplot primitive.\n\
+% x y x --\n\
+/x {moveto 8 8 rlineto -16 -16 rlineto\n\
+    8 8 rmoveto\n\
+    -8 8 rlineto 16 -16 rlineto} def\n\
+% x y + --\n\
+/+ {moveto 0 8 rlineto 0 -16 rlineto\n\
+    0 8 rmoveto\n\
+    -8 0 rlineto 16 0 rlineto} def\n\
+% x y ?arrow --\n\
+/darrow {moveto 8 theta 8 theta rmoveto -8 theta -8 theta rlineto\n\
+         -8 theta 8 theta rlineto } def\n\
+/uarrow {moveto -8 theta -8 theta rmoveto 8 theta 8 theta rlineto\n\
+         8 theta -8 theta rlineto } def\n\
+/rarrow {moveto -8 theta 8 theta rmoveto 8 theta -8 theta rlineto\n\
+         -8 theta -8 theta rlineto } def\n\
+/larrow {moveto 8 theta 8 theta rmoveto -8 theta -8 theta rlineto\n\
+         8 theta -8 theta rlineto } def\n\
+%x y x y line --\n\
+/line {moveto lineto} def\n\
+%x y dot --\n\
+% begin gdt mod\n\
+/dot { moveto dotfont setfont (.) show } def\n\
+%/dot {stroke 8 theta 0 360 arc fill} def\n\
+% end gdt mod\n\
+%x y plus --\n\
+/plus {moveto -16 theta 0 rmoveto 16 theta 0 rlineto\n\
+       -16 theta -16 theta rmoveto 0 16 theta rlineto} def\n\
+%x y box --\n\
+/box {moveto -8 theta -8 theta rmoveto\n\
+      16 theta 0 rlineto\n\
+      0 16 theta rlineto\n\
+      -16 theta 0 rlineto\n\
+      0 -16 theta rlineto} def\n\
+%x y diamond --\n\
+/diamond { moveto 0 theta 24 theta rmoveto\n\
+           -24 theta -24 theta rlineto\n\
+            24 theta -24 theta rlineto\n\
+            24 theta  24 theta rlineto\n\
+           -24 theta  24 theta rlineto} def\n\
+%x y ?tick --\n\
+/utick {moveto 0 6 theta rlineto} def\n\
+/dtick {moveto 0 -6 theta rlineto} def\n\
+/ltick {moveto -6 theta 0 rlineto} def\n\
+/rtick {moveto 6 theta 0 rlineto} def\n\
+/htick {moveto -6 theta 0 rmoveto 12 theta 0 rlineto} def\n\
+/vtick {moveto 0 -6 theta rmoveto 0 12 theta rlineto} def\n\
+%Separate functions for each text position.\n\
+%x y string ?text --\n\
+/space 6 pt def\n\
+% Set the font, figure out the width.\n\
+% x y string tsetup string x width y\n\
+/tsetup {lfont setfont dup stringwidth pop exch\n\
+         4 1 roll exch} def\n\
+%CENTER\n\
+/ctext {tsetup lascent 2 div sub\n\
+        3 1 roll 2 div sub exch\n\
+% stack should now be string x y\n\
+        moveto show} def\n\
+%ABOVE\n\
+/atext {tsetup space ldescent add add\n\
+        3 1 roll 2 div sub exch moveto show} def\n\
+%BELOW\n\
+/btext {tsetup space lascent add sub\n\
+        3 1 roll 2 div sub exch moveto show} def\n\
+%TO_THE_LEFT\n\
+/ltext {tsetup lascent 2 div sub\n\
+        3 1 roll space add sub exch moveto show} def\n\
+%TO_THE_RIGHT\n\
+/rtext {tsetup lascent 2 div sub\n\
+        3 1 roll pop space add exch moveto show} def\n\
 ", fp);
 
   {
@@ -3354,9 +3347,9 @@
   if (state == PRINTING) {
     fputs("-90 rotate -11 inch 0 inch translate\n", fp);
   } else {
-    fputs("
-/notintex { currentdict userdict eq } def
-notintex { 1.5 inch 5.0 inch translate } if
+    fputs("\n\
+/notintex { currentdict userdict eq } def\n\
+notintex { 1.5 inch 5.0 inch translate } if\n\
 ", fp);
   }
   /* Move origin to create left & bottom margins. */
