v04i045: Xhpgl hpgl 7470 for X10, Part01/01

Dan Heller argv at island.uu.net
Mon Jul 17 16:31:03 AEST 1989


Submitted-by: Randy L. Yach <ryach at sedona.intel.com>
Posting-number: Volume 4, Issue 45
Archive-name: xhpgl/part01

[ Not tested --X10 stuff. --argv ]


A while ago I submitted a request to the net for any piece of software that
will read hpgl syntax and display it on an X display.  I never got any responce
so I wrote my own.  Here is a crude version that works unsing X10.3 and X10.4.
It has been tested on a sun 386i running sunos 4.0.0 and on ultrix 2.2.  I 
take no responsibility for the code but I am open to comments and suggestions.


#!/bin/sh
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
# If all goes well, you will see the message "No problems found."
# This archive was produced by Randys imitation shar program.
# The real shar was not available.
# Please inform Randy of any problems.
# Exit status; set to 1 on "wc" errors or if would overwrite.
STATUS=0
# Contents:  black.bitmap hpcolors hpgl.l hpgl.y pc.xw xhpgl.c xhpgl.man Makefile README

echo x - black.bitmap
if test -f black.bitmap ; then
    echo black.bitmap exists, putting output in $$black.bitmap
    OUT=$$black.bitmap
    STATUS=1
else
    OUT=black.bitmap
fi
sed 's/^X//' > $OUT <<'@//E*O*F black.bitmap//'
X#define black_width 16
X#define black_height 16
Xstatic short black_bits[] = {
X   0xffff, 0xffff, 0xffff, 0xffff,
X   0xffff, 0xffff, 0xffff, 0xffff,
X   0xffff, 0xffff, 0xffff, 0xffff,
X   0xffff, 0xffff, 0xffff, 0xffff};
@//E*O*F black.bitmap//
chmod u=rw,g=rw,o=rw $OUT

echo x - hpcolors
if test -f hpcolors ; then
    echo hpcolors exists, putting output in $$hpcolors
    OUT=$$hpcolors
    STATUS=1
else
    OUT=hpcolors
fi
sed 's/^X//' > $OUT <<'@//E*O*F hpcolors//'
X1 red
X2 green
X3 yellow
X4 blue
X5 cyan
X6 magenta
X7 white
X8 orange
@//E*O*F hpcolors//
chmod u=rw,g=rw,o=rw $OUT

echo x - hpgl.l
if test -f hpgl.l ; then
    echo hpgl.l exists, putting output in $$hpgl.l
    OUT=$$hpgl.l
    STATUS=1
else
    OUT=hpgl.l
fi
sed 's/^X//' > $OUT <<'@//E*O*F hpgl.l//'
X%{
X/*****************************************************************\
X * FILE: hpgl.l   part of hpgl to X windows                      *
X *                                                               *
X * Written by Randy L. Yach aid by Jackie Harrison               *
X *                                                               *
X * Description:                                                  *
X *    this is the lexical analizer of HPGL syntax.  All action   *
X *    tokens are covered first.                                  *
X\*****************************************************************/
X
X#include <stdio.h>
X#include "y.tab.h"
X#include <X/Xlib.h>
X
X#ifdef ULTRIX
X#    include <math.h>
X#endif
X
X/* need this because the sun 386i has a funny location of the atof
X   definition */
X
X#ifdef UI38
X#    include <floatingpoint.h>
X#endif
X
X/* need this to keep count of the input lines for error messages */
Xint hline = 1;
X%}
XD	[0-9]
XR       [0-9\.]
XA	[A-Z]
XLBL     [a-z\-\, \.A-Z0-9\:\;\(\)\&\^\%\$\#\@\!\"\'\~\`|?\/\<\>\_]
XDOT     [\.]
XCOMMA   [\,]
XALT     [A-Z\(\)\@]
XG       [A-Z0-9\;]
X%%
XLT	                        {
X                                return(LT);
X                                }
XLB{LBL}*\                     {
X                                /* strip off the LB and the ending ^C
X                                   from the input line and return it */
X			        strcpy(yylval.sb,&yytext[2]);
X				yylval.sb[strlen(yylval.sb)-1] = '\0';
X				return(LB);
X				}
XSC                              {
X				return(SC);
X				}
XSP{D}                           {
X                                /* return the pen number */
X				yylval.i = yytext[2] - '0';
X				return(SP);
X				}
XPA              		{
X				return(PA);
X				}
XPR              		{
X				return(PR);
X				}
XPU              		{
X				return(PU);
X				}
XPD              		{
X				return(PD);
X				}
X\-?{D}+                         {
X                                /* integer digit routine */
X				yylval.i = atoi(yytext);
X				return(DIGIT);
X				}
X\-?{R}+                         {
X                                /* real digit routine */
X				yylval.f = atof(yytext);
X				return(NUMBER);
X				}
X\;				{
X				return(SEMICOLON);
X                                }
X\,                              {
X				return(COMMA);
X                                }
XIN\;                            return(RESET);
XDF\;                            return(RESET);
X[ \t]*			        ;
X[\n]                            {
X                                hline++;
X				}
X\{DOT}{ALT}({G}+\:)?          ;
XC{A}{D}*({COMMA}{D}*)?\;        ;
XDI{D}*({COMMA}{D}*)?\;          ;
XDR{D}*({COMMA}{D}*)?\;          ;
XDT{A}\;                         ;
XDC\;                            ;
XDP\;                            ;
XO{A}\;                          ;
XIM{D}*({COMMA}{D}*)*\;          ;
XIP{D}*({COMMA}{D}*)*\;          ;
XIW{D}*({COMMA}{D}*)*\;          ;
XSA\;                            ;
XSI{D}*{COMMA}{D}*\;             ;
XSL{D}*\;                        ;
XSM{A}\;                         ;
XSR{R}*{COMMA}{R}*\;             ;
XSS\;                            ;
XSA\;                            ;
XTL{D}*({COMMA}{D}+)?\;          ;
XVS{D}*\;                        ;
XXT\;                            ;
XYT\;                            ;
X%%
@//E*O*F hpgl.l//
chmod u=rw,g=rw,o=rw $OUT

echo x - hpgl.y
if test -f hpgl.y ; then
    echo hpgl.y exists, putting output in $$hpgl.y
    OUT=$$hpgl.y
    STATUS=1
else
    OUT=hpgl.y
fi
sed 's/^X//' > $OUT <<'@//E*O*F hpgl.y//'
X%{
X/*****************************************************************\
X * FILE: hpgl.y   part of hpgl to X windows                      *
X *                                                               *
X * Written by Randy L. Yach aid by Jackie Harrison               *
X *                                                               *
X * Description:                                                  *
X *    this is the yacc parser of HPGL syntax.  All plotting      *
X *    line drawing and labeling are done in this procedure       *
X\*****************************************************************/
X
X#include <stdio.h>
X#include <X/Xlib.h>
X#include <math.h>
X
X/* need all these external global variables to interface with the many
X   X windows variables. */
Xextern int hline;
Xextern char yytext[];
Xextern double xMin,xMax,yMin,yMax;
Xextern int last_x, last_y;
Xextern int x,y;
Xextern int cur_pen,no_pen;
Xextern int pen_down;
Xextern int absolute;
Xextern Color pen[];
Xextern Window main_window;
Xextern Font text_font;
Xextern short font_offset;
Xextern int minWidth,minHeight;
Xextern Pattern line_type;
Xextern line;
X
Xextern initialize_plotter();
X
X/* line points used for line drawing */
XVertex coordinate_points[2] =
X    {
X    0, 0, 0,
X    0, 0, 0
X    };
X
X/* binary pattern definitions for drawing dashed lines */
X/* default |________|  */
X/* line 0  |        |  */
X/* line 1  |_       |  */
X/* line 2  |____    |  */
X/* line 3  |______  |  */
X/* line 4  |______ _|  */
X/* line 5  |_____ __|  */
X/* line 6  |____ _ _|  */
Xunsigned short line_pattern[7] = {
X    0x00, 0x80, 0xf0, 0xfc, 0xfd, 0xfb, 0xf5
X    };
X
X/* these variables are needed to calculate the default length
X   of the patterns used in the dashed lines */
Xint pat_len;
Xdouble pat_perc,tot_dist;
X
X%}
X
X%token LT LB PA PR PU PD SP SC DIGIT SEMICOLON COMMA NUMBER RESET
X
X%start xhpgl_start
X
X%union {
Xint i;
Xchar sb[BUFSIZ];
Xfloat f;
X}
X
X%type <i> DIGIT SP
X%type <sb> LB
X%type <f> NUMBER
X
X%%
X
Xxhpgl_start    : /* empty statement */
X	       | xhpgl_start xhpgl_commands
X	       ;
X
Xxhpgl_commands : plot_coordinates cordinate_list SEMICOLON
X	       | SP SEMICOLON
X		 { /* begin select pen */
X		 /* set pen color from the SP command */
X		 cur_pen = pen[$1].pixel;
X		 } /* end select pen */
X	       | SC scale_coordinates SEMICOLON
X	       | text_labels
X	       | LT line_type_change SEMICOLON
X	       | RESET
X		 { /* begin reset */
X		 initialize_plotter();
X		 } /* end reset */
X	       ;
X
Xline_type_change : /* empty statement */ 
X                   { /* begin solid line */
X		   /* set up solid line type */
X		   line_type = XMakePattern(0xff,8,1);
X		   } /* end solid line */
X		 | DIGIT COMMA NUMBER
X                   { /* begin dashed line */
X		   /* set up line pattern */
X		   pat_perc=($3/100.0);
X		   tot_dist=sqrt(pow((float)minWidth,(float)2)
X				+pow((float)minHeight,(float)2));
X		   pat_len=(int)((pat_perc*tot_dist)/8.0);
X		   if (pat_len < 1) pat_len = 1;
X		   line_type = XMakePattern(line_pattern[$1],8,pat_len);
X		   } /* end dashed line */
X		 | DIGIT COMMA DIGIT
X                   { /* begin dashed line */
X		   /* set up line pattern */
X		   pat_perc=($3/100.0);
X		   tot_dist=sqrt(pow((float)minWidth,(float)2)
X				+pow((float)minHeight,(float)2));
X		   pat_len=(int)((pat_perc*tot_dist)/8.0);
X		   if (pat_len < 1) pat_len = 1;
X		   line_type = XMakePattern(line_pattern[$1],8,pat_len);
X		   } /* end dashed line */
X		 ;
X
Xtext_labels    : LB
X                 { /* begin plot label */
X		 /* plot the text given at the current X,Y locations */
X		 XText(main_window,x,y-font_offset,$1,strlen($1),text_font,
X		       cur_pen,no_pen);
X		 } /* end plot label */
X	       ;	
X
Xplot_coordinates : PA
X	           { /* begin pen absolute coordinates */
X		   absolute = 1;
X		   } /* end pen absolute coordinates */
X		 | PD
X		   { /* begin pen relitave coordinates */
X		   pen_down = 1;
X		   } /* end pen relitave coordinates */
X		 | PU
X		   { /* begin pen up coordinates */
X		   pen_down = 0;
X		   } /* end pen up coordinates */
X		 | PR
X		   { /* begin pen down coordinates */
X		   absolute = 0;
X		   } /* end pen down coordinates */
X		 ;
X
Xscale_coordinates : DIGIT COMMA DIGIT COMMA DIGIT COMMA DIGIT
X                    { /* begin user scale */
X		    /* set up the user coodinate boundaries */
X		    xMin = $1;
X		    xMax = $3;
X		    yMin = $5;
X		    yMax = $7;
X                    } /* end user scale */
X                  ;
X
Xcordinate_list : /* empty statement */
X	       | cordinate_list cordinate_def comma_opt
X	       ;
X
Xcomma_opt      : /* empty statement */
X	       | COMMA
X	       ;
X
Xcordinate_def  : DIGIT COMMA DIGIT
X                 { /* begin cordinate change */
X		 /* get the new x,y points and convert then to absolute
X                    X window coodinates based on the user coordinates and 
X		    plot scale. */
X		 last_x = x;
X		 last_y = y;
X		 if (absolute)
X		     { /* begin abs coordinate */
X		     x=(double)(($1 - xMin)*(minWidth/(xMax - xMin)));
X		     y=(double)(($3 - yMin)*(minHeight/(yMax - yMin)));
X		     } /* end abs coordinate */
X		 else
X		     { /* begin relitave coordinate */
X		     x=last_x+(double)(($1 - xMin)*(minWidth/(xMax - xMin)));
X		     y=last_y+(double)(($3 - yMin)*(minHeight/(yMax - yMin)));
X		     } /* end relitave coordinate */
X		 y = minHeight - y;
X		 
X                 /* if the pen is down, draw a line of the appropriate line
X		    type between the last x,y and the current x,y
X		    coordinates */
X                 if (pen_down)
X		     { /* begin draw line */
X		     coordinate_points[0].x = last_x;
X		     coordinate_points[0].y = last_y;
X		     coordinate_points[1].x = x;
X		     coordinate_points[1].y = y;
X		     XDrawDashed(main_window,coordinate_points,
X			   2,1,1,cur_pen,line_type,GXcopy,AllPlanes);
X		     } /* end draw line */
X		 } /* end cordinate change */
X	       ;
X
X%%
Xvoid yyerror(mess)
Xchar *mess;
X{ /* begin parsing error */
X    fprintf(stderr,"Syntax error line %d token %s.\n",hline,yytext); 
X    exit();
X} /* end parsing error */
X
Xint yywrap()
X{ /* begin */
X	return(1);
X} /* end */
X
@//E*O*F hpgl.y//
chmod u=rw,g=rw,o=rw $OUT

echo x - pc.xw
if test -f pc.xw ; then
    echo pc.xw exists, putting output in $$pc.xw
    OUT=$$pc.xw
    STATUS=1
else
    OUT=pc.xw
fi
sed 's/^X//' > $OUT <<'@//E*O*F pc.xw//'
X.(.@;0:.I40;;17:.N;19:DF;SC0,1999,0,1439;.(
XSR0.733333,1.354167;DI1,0;PU;SP7;SL0;LT;.).(
XPU;SP1;PU;PA820,604;PD;PA1112,604;
XPD;PA1108,650;
XPD;PA1098,694;
XPD;PA1080,737;
XPD;PA1056,776;
XPD;PA820,604;
XPU;SP2;PD;PA1056,776;
XPD;PA1026,810;
XPD;PA992,840;
XPD;PA953,864;
XPD;PA910,882;
XPD;PA866,892;
XPD;PA820,896;
XPD;PA774,892;
XPD;PA730,882;
XPD;PA820,604;
XPU;PA731,877;PD;PA738,884;
XPU;PA736,862;PD;PA765,891;
XPU;PA741,847;PD;PA788,894;
XPU;PA746,832;PD;PA810,896;
XPU;PA751,817;PD;PA830,896;
XPU;PA756,802;PD;PA849,895;
XPU;PA761,787;PD;PA866,892;
XPU;PA766,772;PD;PA883,889;
XPU;PA771,757;PD;PA899,885;
XPU;PA775,741;PD;PA914,880;
XPU;PA780,726;PD;PA929,875;
XPU;PA785,711;PD;PA943,869;
XPU;PA790,696;PD;PA956,862;
XPU;PA795,681;PD;PA969,855;
XPU;PA800,666;PD;PA981,847;
XPU;PA805,651;PD;PA993,839;
XPU;PA810,636;PD;PA1004,830;
XPU;PA815,621;PD;PA1015,821;
XPU;PA820,606;PD;PA1025,811;
XPU;PA886,652;PD;PA1035,801;
XPU;PA960,706;PD;PA1045,791;
XPU;PA1034,760;PD;PA1053,779;
XPU;SP3;PU;PA820,604;PD;PA730,882;
XPD;PA687,864;
XPD;PA648,840;
XPD;PA614,810;
XPD;PA584,776;
XPD;PA560,737;
XPD;PA542,694;
XPD;PA532,650;
XPD;PA528,604;
XPD;PA532,558;
XPD;PA542,514;
XPD;PA560,471;
XPD;PA584,432;
XPD;PA820,604;
XPU;PA577,442;PD;PA586,433;
XPU;PA553,486;PD;PA597,442;
XPU;PA541,518;PD;PA609,450;
XPU;PA534,545;PD;PA620,459;
XPU;PA530,569;PD;PA632,467;
XPU;PA528,591;PD;PA644,475;
XPU;PA528,611;PD;PA655,484;
XPU;PA529,630;PD;PA667,492;
XPU;PA531,648;PD;PA678,501;
XPU;PA534,665;PD;PA690,509;
XPU;PA538,681;PD;PA701,518;
XPU;PA543,696;PD;PA713,526;
XPU;PA548,711;PD;PA725,534;
XPU;PA554,725;PD;PA736,543;
XPU;PA561,738;PD;PA748,551;
XPU;PA568,751;PD;PA759,560;
XPU;PA575,764;PD;PA771,568;
XPU;PA584,775;PD;PA782,577;
XPU;PA592,787;PD;PA794,585;
XPU;PA601,798;PD;PA806,593;
XPU;PA611,808;PD;PA817,602;
XPU;PA621,818;PD;PA813,626;
XPU;PA632,827;PD;PA803,656;
XPU;PA643,836;PD;PA794,685;
XPU;PA654,845;PD;PA784,715;
XPU;PA667,852;PD;PA775,744;
XPU;PA679,860;PD;PA765,774;
XPU;PA692,867;PD;PA755,804;
XPU;PA706,873;PD;PA746,833;
XPU;PA720,879;PD;PA736,863;
XPU;SP4;PU;PA820,604;PD;PA584,432;
XPD;PA614,398;
XPD;PA648,368;
XPD;PA687,344;
XPD;PA730,326;
XPD;PA774,316;
XPD;PA820,312;
XPD;PA866,316;
XPD;PA910,326;
XPD;PA953,344;
XPD;PA992,368;
XPD;PA1026,398;
XPD;PA1056,432;
XPD;PA1080,471;
XPD;PA1098,514;
XPD;PA1108,558;
XPD;PA1112,604;
XPD;PA820,604;
XPU;PA600,444;PD;PA600,412;
XPU;PA620,458;PD;PA620,391;
XPU;PA640,473;PD;PA640,374;
XPU;PA660,487;PD;PA660,360;
XPU;PA680,502;PD;PA680,348;
XPU;PA700,517;PD;PA700,338;
XPU;PA720,531;PD;PA720,330;
XPU;PA740,546;PD;PA740,323;
XPU;PA760,560;PD;PA760,318;
XPU;PA780,575;PD;PA780,315;
XPU;PA800,589;PD;PA800,313;
XPU;PA820,604;PD;PA820,312;
XPU;PA840,604;PD;PA840,313;
XPU;PA860,604;PD;PA860,315;
XPU;PA880,604;PD;PA880,318;
XPU;PA900,604;PD;PA900,323;
XPU;PA920,604;PD;PA920,330;
XPU;PA940,604;PD;PA940,338;
XPU;PA960,604;PD;PA960,348;
XPU;PA980,604;PD;PA980,360;
XPU;PA1000,604;PD;PA1000,374;
XPU;PA1020,604;PD;PA1020,391;
XPU;PA1040,604;PD;PA1040,412;
XPU;PA1060,604;PD;PA1060,438;
XPU;PA1080,604;PD;PA1080,471;
XPU;PA1100,604;PD;PA1100,521;
XPU;SP7;PU;PA644,1311;
XPU;LBDATA FROM @PIEME
XPU;PA748,1233;
XPU;LBPIE 1
XPU;SP1;PU;PA1706,740;PD;PA1823,740;
XPD;PA1823,778;
XPD;PA1706,778;
XPD;PA1706,740;
XPU;SP7;PU;PA1846,747;
XPU;LBSLICE 1
XPU;SP2;PU;PA1706,662;PD;PA1823,662;
XPD;PA1823,700;
XPD;PA1706,700;
XPD;PA1706,662;
XPU;PA1706,685;PD;PA1721,700;
XPU;PA1706,665;PD;PA1741,700;
XPU;PA1723,662;PD;PA1761,700;
XPU;PA1743,662;PD;PA1781,700;
XPU;PA1763,662;PD;PA1801,700;
XPU;PA1783,662;PD;PA1821,700;
XPU;PA1803,662;PD;PA1823,682;
XPU;PA1823,662;PD;PA1823,662;
XPU;SP7;PU;PA1846,669;
XPU;LBSLICE 2
XPU;SP3;PU;PA1706,584;PD;PA1823,584;
XPD;PA1823,622;
XPD;PA1706,622;
XPD;PA1706,584;
XPU;PA1706,593;PD;PA1715,584;
XPU;PA1706,613;PD;PA1735,584;
XPU;PA1717,622;PD;PA1755,584;
XPU;PA1737,622;PD;PA1775,584;
XPU;PA1757,622;PD;PA1795,584;
XPU;PA1777,622;PD;PA1815,584;
XPU;PA1797,622;PD;PA1823,597;
XPU;PA1817,622;PD;PA1823,616;
XPU;SP7;PU;PA1846,591;
XPU;LBSLICE 3
XPU;SP4;PU;PA1706,506;PD;PA1823,506;
XPD;PA1823,544;
XPD;PA1706,544;
XPD;PA1706,506;
XPU;PA1720,506;PD;PA1720,544;
XPU;PA1740,506;PD;PA1740,544;
XPU;PA1760,506;PD;PA1760,544;
XPU;PA1780,506;PD;PA1780,544;
XPU;PA1800,506;PD;PA1800,544;
XPU;PA1820,506;PD;PA1820,544;
XPU;SP7;PU;PA1846,513;
XPU;LBSLICE 4
XPU;PA0,1389;
XPU;LBPC                                                                 11-JUL-89 12:06 Page 1 
XPU;SP0;.).(PU;PA0,1439;DF;SP0;.)
@//E*O*F pc.xw//
chmod u=rw,g=rw,o=rw $OUT

echo x - xhpgl.c
if test -f xhpgl.c ; then
    echo xhpgl.c exists, putting output in $$xhpgl.c
    OUT=$$xhpgl.c
    STATUS=1
else
    OUT=xhpgl.c
fi
sed 's/^X//' > $OUT <<'@//E*O*F xhpgl.c//'
X/******************************************************************\
X *                                                                *
X *  File Name:  xhpgl.c    part of hpgl to Xwindows program       *
X *                                                                *
X *                                                                *
X *  Author:  Randy L. Yach                                        *
X *                                                                *
X *  Functional description:                                       *
X *                                                                *
X *     This program will read a 7470a syntax HPGL file and        *
X *     display it on an Xwindow display.                          *
X *                                                                *
X\******************************************************************/
X
X
X#include <X/Xlib.h>
X#include <stdio.h>
X#include "black.bitmap"
X
X#define DISCARD 1
X#define MAXX 1000
X#define MAXY 773
X
Xdouble xMin,xMax,yMin,yMax; /* user min/max coordinate boundaries */
Xint last_x, last_y; /* last x and y coordinates for line drawing */
Xint x,y;           /* current x and y coordinates for line drawing */
Xint cur_pen,no_pen,line; /* current pen color */
Xint pen_down;            /* pen status */
Xint absolute;            /* ploter status */
XFont text_font;          /* LB font selection */
Xshort font_offset;       /* distance from lower left to upper left of font */
XWindow main_window;      /* main window for plotting */
XColor pen[10];            /* all pen colors for plotting */
Xint minWidth,minHeight;  /* size of X window for plotting */
XPattern line_type;       /* line dash pattern */
Xchar font_name[32]; /* font name of user supplied font */
X
X/**/
X/******************************************************************\
X * prodedure select_font(x,y)                                     *
X ******************************************************************
X *    will pick a font based on the window size                   *
X\******************************************************************/
X
Xvoid get_text_font(user_font)
Xint user_font;
X{
X     if (!user_font)
X	 {
X         if ((minWidth < 350) || (minHeight < 350)) 
X	     strcpy(font_name,"nil2");
X         else if ((minWidth < 700) || (minHeight < 700)) 
X	     strcpy(font_name,"6x10");
X         else if ((minWidth < 800) || (minHeight < 800)) 
X	     strcpy(font_name,"8x13");
X         else 
X	     strcpy(font_name,"9x15");
X	 }
X
X    /* get the font and check if it is valid */
X    text_font = XGetFont(font_name);
X
X}
X
X/**/
X/******************************************************************\
X * procedure reverse                                              *
X ******************************************************************
X *    used to revers a string s                                   *
X\******************************************************************/
Xvoid reverse(s)
X/* reverse string s in place */
Xchar s[];
X{ /* begin reverse */
Xint c,i,j;
X
X   for (i = 0, j = strlen(s)-1; i < j; i++, j--) 
X       { /* begin for string */
X       c = s[i];
X       s[i] = s[j];
X       s[j] = c;
X       } /* end for string */
X    } /* end reverse */
X
X/**/
X/******************************************************************\
X * procedure itoa                                                 *
X ******************************************************************
X *    converts an integer to a string.                            *
X\******************************************************************/
Xvoid itoa(s,n) 
X/* convert n to characters in s */
Xint n;
Xchar s[];
X{ /* begin itoa */
Xint i, sign;
X
X   if ((sign = n) < 0)  /* record sign */
X      n = -n;
X   i = 0;
X   do {  /* generate digits in reverse order */
X      s[i++] = n % 10 + '0';  /* get next digit */
X   } while ((n /= 10) > 0);  /* delete it */
X   if (sign < 0)
X      s[i++] = '-';
X   s[i] = '\0';
X   reverse(s);
X} /* end itoa */
X
X/**/
X/******************************************************************\
X * procedure initialize_plotter                                   *
X ******************************************************************
X *    used to set up the default plotter conditions.              *
X\******************************************************************/
Xvoid initialize_plotter()
X{ /* begin initialize_plotter */
X    x=0;                  /* set current x plotter coordinate */
X    y=0;                  /* set current y plotter coordinate */
X    last_x=0;             /* set last x plotter coordinate */
X    last_y=0;             /* set last y plotter coordinate */
X    
X    xMin = 0;             /* set default x and y plotter scale */
X    yMin = 0;             /* user coordinates.  This assumes */
X    xMax = 10300;         /* that the 7470a is in US mode */
X    yMax = 7650;
X    
X    pen_down=0;           /* pot the pen in up position */
X    
X    cur_pen=pen[0].pixel; /* select no pen or no color */
X    no_pen=pen[0].pixel;  /* need a reference background color for text */
X    
X    line_type = XMakePattern(0xff,8,1); /* set up solid line pattern */
X    } /* end initialize_plotter */
X/**/
X/******************************************************************\
X * procedure assign_color                                         *
X ******************************************************************
X *    used to assign a color to each plotter pen.                 *
X\******************************************************************/
Xvoid assign_color(pen_num,color)
Xint pen_num;
Xchar color[];
X{ /* begin assign_color */
X    if ((pen_num <=0) || (pen_num >= 9))
X        { /* begin invalid pen */
X        fprintf(stderr,"pen numbers must be 1 to 8.\n");
X	exit(-1);
X	} /* end invalid pen */
X    else
X	{ /* begin valid pen */
X        if (strcmp(color,"black")==0) 
X            { /* begin black */
X            pen[pen_num].red=0; 
X	    pen[pen_num].green=0; pen[pen_num].blue=0;
X            } /* end black */
X        else if (strcmp(color,"red")==0) 
X            { /* begin red */
X            pen[pen_num].red=65535; 
X	    pen[pen_num].green=0; pen[pen_num].blue=0;
X            } /* end red */
X        else if (strcmp(color,"gold")==0) 
X            { /* begin gold */
X            pen[pen_num].red=61500;pen[pen_num].green=36750; 
X	    pen[pen_num].blue=25000;
X            } /* end gold */
X        else if (strcmp(color,"firebrick")==0) 
X            { /* begin firebrick */
X            pen[pen_num].red=42600;pen[pen_num].green=10500; 
X	    pen[pen_num].blue=10500;
X            } /* end firebrick */
X        else if (strcmp(color,"maroon")==0) 
X            { /* begin maroon */
X            pen[pen_num].red=42600; pen[pen_num].green=10500; 
X	    pen[pen_num].blue=32100;
X            } /* end maroon */
X        else if (strcmp(color,"orange")==0) 
X            { /* begin orange */
X            pen[pen_num].red=61200; pen[pen_num].green=15000; 
X	    pen[pen_num].blue=15000;
X            } /* end orange */
X        else if (strcmp(color,"pink")==0) 
X            { /* begin pink */
X            pen[pen_num].red=56400; pen[pen_num].green=42900; 
X	    pen[pen_num].blue=42900;
X            } /* end pink */
X        else if (strcmp(color,"turquoise")==0) 
X            { /* begin turquoise */
X            pen[pen_num].red=51900; pen[pen_num].green=65535; 
X	    pen[pen_num].blue=65535;
X            } /* end turquoise */
X        else if (strcmp(color,"violet")==0) 
X            { /* begin violet */
X            pen[pen_num].red=23700; pen[pen_num].green=14100; 
X	    pen[pen_num].blue=23700;
X            } /* end violet */
X        else if (strcmp(color,"green")==0) 
X            { /* begin green */
X            pen[pen_num].red=0; 
X	    pen[pen_num].green=65535; pen[pen_num].blue=0;
X            } /* end green */
X        else if (strcmp(color,"blue")==0) 
X            { /* begin blue */
X            pen[pen_num].red=0; 
X	    pen[pen_num].green=0; pen[pen_num].blue=65535;
X            } /* end blue */
X        else if (strcmp(color,"yellow")==0) 
X            { /* begin yellow */
X            pen[pen_num].red=65535; 
X	    pen[pen_num].green=65535; pen[pen_num].blue=0;
X            } /* end yellow */
X        else if (strcmp(color,"cyan")==0) 
X            { /* begin cyan */
X            pen[pen_num].red=0; 
X	    pen[pen_num].green=65535; pen[pen_num].blue=65535;
X            } /* end cyan */
X        else if (strcmp(color,"magenta")==0) 
X            { /* begin magenta */
X            pen[pen_num].red=65535; 
X	    pen[pen_num].green=0; pen[pen_num].blue=65535;
X            } /* end magenta */
X        else if (strcmp(color,"white")==0) 
X            { /* begin white */
X            pen[pen_num].red=65535; 
X	    pen[pen_num].green=65535; pen[pen_num].blue=65535;
X            } /* end white */
X        else 
X            { /* begin default white */
X	    fprintf(stderr,"Color %s not supported, using white.\n",color);
X            pen[pen_num].red=65535; 
X	    pen[pen_num].green=65535; pen[pen_num].blue=65535;
X	    } /* begin default white */
X        } /* end valid pen */
X    } /* end assign_color */
X
X/**/
Xmain (argc, argv)
Xint argc;
Xchar *argv[];
X{ /* begin main */
X
X    /* variable declerations */
X    Pixmap pixmap,bpix; /* Used for frame and background of main window */
X    Bitmap blackB;      /* All 1's located in a 16 x 16 area for tiles */
X    OpaqueFrame frame;  /* Used for creating the frame for the window */
X    XEvent event;       /* used for watching for keys in main window */
X    WindowInfo window_info[1]; /* window information from XQueryWindow */
X    FontInfo text_font_info[1]; /* font information structure */
X    
X    char file_name[32]; /* file name of hpgl input file */
X    extern FILE *yyin;  /* File pointer for hpgl input file */
X    
X    char size_def[22];  /* is the default size used for creating the window */
X    char temp[22];      /* temporary string holder */
X    int user_font = 0;  /* need to know if the user entered a font */
X    
X    int i; /* general puropse counter */
X    char user_color[10]; /* string for user color name */
X    char *color_file_path; /* path to user color file */
X    char color_file[65]; /* color file name */
X    FILE *color_file_pointer; /* color file pointer */
X    
X    
X    /* set up default font_name and file_name */
X    *font_name='\0';
X    *file_name='\0';
X    
X    /* set up the default color file path and name */
X    color_file_path = (char *) getenv("HOME");
X    if (color_file_path == NULL)
X	*color_file_path = '\0';
X    strcpy(color_file,color_file_path);
X    strcat(color_file,"/.hpcolors");
X    
X    /* set up the default pixel size of the display window.  all plot
X       coordinates will be scaled to these coordinates */
X    minWidth=MAXX;
X    minHeight=MAXY;
X    strcpy(size_def,"=");
X    itoa(temp,MAXX);
X    strcat(size_def,temp);
X    strcat(size_def,"x");
X    itoa(temp,MAXY);
X    strcat(size_def,temp);
X    strcat(size_def,"+0+0");
X    
X    /* initialize all the pen colors to default value */
X    pen[1].red=65535; pen[1].green=0; pen[1].blue=0;
X    pen[2].red=0; pen[2].green=65535; pen[2].blue=0;
X    pen[3].red=0; pen[3].green=0; pen[3].blue=65535;
X    pen[4].red=0; pen[4].green=65535; pen[4].blue=65535;
X    pen[5].red=65535; pen[5].green=0; pen[5].blue=65535;
X    pen[6].red=65535; pen[6].green=65535; pen[6].blue=0;
X    pen[7].red=65535; pen[7].green=65535; pen[7].blue=65535;
X    pen[8].red=65535; pen[8].green=65535; pen[8].blue=65535;
X
X    /* define the window black and white colors.  let the user
X       defint all the rest.  pen 0 is background black and pen 9
X       is frame white.  pens 1-8 are user definable */
X    pen[0].red=0; pen[0].green=0; pen[0].blue=0; /* background black */
X    pen[9].red=65535; pen[9].green=65535; pen[9].blue=65535; /* white */
X    
X    /* check to see if the user did not enter a file name and print
X       and error message */
X    if(argc == 1)
X        { /* begin error print */
X        fprintf(stderr,"\nusage: xhpgl -fn [font] file\n\n");
X        exit(-1);
X        } /* end error print */
X    
X    /* parse the input arguments and get the file name and font name.
X       all arguments passed that are not -fn font or -cf color_file
X       are assumed to be files.
X       This program will use the last arg sent as the file to plot. */
X    for(argc--,argv++;argc;argc--,argv++)
X        { /* begin input arg parseing */
X        if (!strcmp(*argv,"-fn"))
X            { /* begin arg fn */
X            argc--;
X            argv++;
X            strcpy(font_name,*argv);
X	    user_font = 1;
X            } /* end arg fn */
X	if (!strcmp(*argv,"-cf"))
X            { /* begin color file */
X	    argc--;
X	    argv++;
X	    strcpy(color_file,*argv);
X	    } /* end color file */
X        else
X            strcpy(file_name,*argv);
X        } /* end input arg parseing */
X    
X    /* open the hpgl file and print error if not able to */
X    if((yyin=fopen(file_name,"r"))==NULL)
X        { /* begin open file error */
X        fprintf(stderr,"Could not open hpgl file -%s- for read.\n",
X                file_name);
X        exit(-1);
X        } /* end open file error */
X    
X
X    /* open the color definition file and print message of not found */
X    /* assign colors if file is found */
X    if((color_file_pointer=fopen(color_file,"r"))==NULL)
X	{ /* begin no color file */
X	fprintf(stderr,"Could not open color file -%s- for read.\n",
X		color_file);
X	fprintf(stderr,"Using default internal values...\n");
X	} /* end no color file */
X    else
X        { /* begin assign colors */
X        while(fscanf(color_file_pointer,"%d %s",&i,user_color) != EOF)
X	    assign_color(i,user_color);
X        fclose(color_file_pointer);
X        } /* begin assign colors */
X	
X    
X    /* connect to the Display as a client */
X    if (XOpenDisplay(NULL)==NULL)
X	{
X        fprintf(stderr,"Could not open Display\n");
X	exit(-1);
X	}
X    
X    /* allocate and create a hardware color for each of the pens */
X    for (i=0;i<10;i++)
X        if (!(XGetHardwareColor(&pen[i])))
X	    {
X            fprintf(stderr,"Could not obtain color for pen %d\n",i);
X	    exit(-1);
X	    }
X    
X    
X    /* set up the pixmap and bitmaps for the background of the main
X       window and its frame. */
X    blackB = XStoreBitmap (black_width,black_height,black_bits);
X    pixmap = XMakePixmap (blackB,pen[0].pixel,pen[9].pixel);
X    bpix = XMakePixmap (blackB,pen[9].pixel,pen[0].pixel);
X    
X    /* set up the frame info for the default window */
X    frame.self=main_window;
X    frame.x=0;
X    frame.y=0;
X    frame.bdrwidth=3;
X    frame.border=bpix;
X    frame.background=pixmap;
X    frame.width=minWidth;
X    frame.height=minHeight;
X    
X    /* create the main window */
X    main_window = XCreate ("xhpgl",
X                           "xhpgl window",
X                           size_def,
X                           size_def,
X                           &frame,
X                           minWidth/10,minHeight/10);
X   
X    /* query window for size to see how big the actual window was
X       that was created. */
X    XQueryWindow (main_window,window_info);
X
X    /* set the the window coordinate size to the actual window size */
X    minWidth = window_info[0].width;
X    minHeight = window_info[0].height;
X
X    /* select the proper font for the window size.  minWidth and minHeight
X       must be assigned to the current window size before this call is made,
X       or the font will be the wrong size */
X    get_text_font(user_font);
X
X    /* check to make sure that the font selected exists on the 
X       hardware */
X    if (text_font == 0)
X        {/* begin get font error */
X        fprintf(stderr,"Could not read font %s\n",font_name);
X        exit(-1);
X        }/* end get font error */
X
X    /* get size of the font */
X    XQueryFont(text_font,text_font_info);
X    font_offset = text_font_info[0].height;
X
X    /* map the window and flush all events */
X    XMapWindow(main_window);
X    XFlush();
X    
X    /* initialize the plotter and set it up to default conditions */
X    initialize_plotter();
X    
X    /* call the yacc parser to parse the hpgl file.  All plotting and line
X       drawing are done in the yacc file */
X    yyparse();
X    
X    /* close the hpgl file */
X    fclose(yyin);
X    
X    /* set up the window to watch key events */
X    XSelectInput(main_window,KeyPressed);
X    
X    while(1) 
X        { /* begin event handeling */
X	/* get the next pending event in the queue */
X        XNextEvent(&event);
X	
X	/* switch on the event type */
X        switch (event.type)
X            { /* begin event switch */
X            case KeyPressed:
X                { /* begin key pressed */
X                char *key_char;
X                XKeyEvent *key_event = (XKeyEvent *) &event;
X
X                key_char = XLookupMapping(key_event,&i);
X
X                for (;i>0;i--) 
X                switch(*key_char++)
X                    { /* begin key matching */
X                    case 'Q':
X                    case 'q': 
X                        { /* begin keys q and Q */
X                        exit(0);
X                        break;
X                        }/* end keys q and Q */
X                    case 'r':
X                    case 'R':
X                        { /* begin keys r and R */
X			/* if a redraw occures, open the file again
X                           and call the yacc parser.  then flush and
X			   discard all other events.  this keeps multiple
X			   events from clogging the queue while a long
X			   plot is painting. */
X                        if((yyin=fopen(file_name,"r"))==NULL)
X			    { /* beign file error */
X                            fprintf(stderr,
X                                "Could not open file %s for read access.\n"
X                                ,file_name);
X			    exit(-1);
X			    } /* end file error */
X
X			/* clear the display to get rid of any opbjects
X			   that are not drawn to proper scale if the window
X			   was resized */
X                        XClear(main_window);
X
X			/* get window size information */
X		        XQueryWindow(main_window,window_info);
X		        minWidth = window_info[0].width;
X		        minHeight = window_info[0].height;
X
X			/* select an appropriate font */
X			get_text_font(user_font);
X
X                        if (text_font == 0)
X                            {/* begin get font error */
X                            fprintf(stderr,"Could not read font %s\n",
X				    font_name);
X                            exit(-1);
X                            }/* end get font error */
X
X                        /* get size of the font */
X                        XQueryFont(text_font,text_font_info);
X                        font_offset = text_font_info[0].height;
X
X			initialize_plotter();
X                        yyparse();
X                        fclose(yyin);
X                        XFlush();
X                        XSync(DISCARD);
X                        break;
X                        } /* end keys r and R */
X                    default: break;
X                    } /* end key matching */
X                } /* end key pressed */
X            default:
X                break;
X            } /* end event switch */
X        } /* end event handeling */
X    } /* end main */
@//E*O*F xhpgl.c//
chmod u=rw,g=rw,o=rw $OUT

echo x - xhpgl.man
if test -f xhpgl.man ; then
    echo xhpgl.man exists, putting output in $$xhpgl.man
    OUT=$$xhpgl.man
    STATUS=1
else
    OUT=xhpgl.man
fi
sed 's/^X//' > $OUT <<'@//E*O*F xhpgl.man//'
X.TH XHPGL 1 "Jul 1989" "Version 1.0"
X.SH NAME
Xxhpgl \- HPGL version 7470a Xwindow display program
X.SH SYNOPSIS
X.B xhpgl
X-cf [color_file] -fn [font] file
X.SH DESCRIPTION
X.I xhpgl
Xis an Xwindow display program that will read a file in HPGL syntax and
Xdisplay it on a X server DISPLAY.  
X.I xhpgl
Xis compatible with X10.3 and X10.4.  The HPGL Syntax is restricted to that
Xdescribed in the 7470A manual.  Most of the commands are supported,
Xbut some are not (see supported commands).
X
X.I xhpgl 
XAlso has some added features.
X.I xhpgl
Xsupports up to 8 different pens where the 7470A routine only supports 2.
XThe color/pen assignments are user definable.  All pen colors are read from
Xa file called $HOME/.hpcolors.  The colors supported are: black, red, green,
Xblue, yellow, cyan, magenta, gold, maroon, turquoise, violet, orange and white.
XThe .hpcolors file is of the form "pen# color".  Only pens 1 to 8 are used.
Xan error will occur if a pen number larger than 8 is given.  If no color
Xfile is found the program will display a message and use internal values.
XThe -cf color_file option may be used to over-ride the default color file
Xon a case by case bases.
X
XThe text used in
X.I xhpgl
Xis of fixed size.  That means that there is no scaling or slanting of text
Xallowed.  This is a temporary problem and will be incorporated in future
Xversions that are compatible with X11.  To help make text more usable 
X.I xhpgl
Xwill select a font based on the window size.  Also, the -fn
Xoption may be used to specify any user font instead of the default selection.
X
X.I xhpgl
Xhas two simple commands built into it for controlling the display window.
Xthe first is a 'q' which will quit the program, and an 'r' will redraw 
Xthe display.  All other keys and mouse activity are ignored.
XWhen part or all of the window is messed up due to X events and window
Xoverlaps,
X.I xhpgl
Xwill not automatically regenerate the window image.  The "r" command must be
Xused.  This will save some long painting times.
X
X.SH OPTIONS
X.I xhpgl 
Xwill take any font supplied by the user.  The -fn parameter is used for this
Xpurpose.
X.I xhpgl
Xwill take only one file in HPGL syntax at a time and display it.
XThe -cf color_file option is used to instruct
X.I xhpgl
Xto use a different color file other than $HOME/.hpcolors.
X
X.SH SUPPORTED COMMANDS
X.I xhpgl 
Xwill recognize and execute the following HPGL commands:
XLT, LB, PA, PR, PU, PD, IN, DF, SP, and SC.  
XAll others are read and accepted however no action will be taken.
X
X.SH AUTHOR
XRandy L. Yach (ryach at sedona.intel.com)
@//E*O*F xhpgl.man//
chmod u=rw,g=rw,o=rw $OUT

echo x - Makefile
if test -f Makefile ; then
    echo Makefile exists, putting output in $$Makefile
    OUT=$$Makefile
    STATUS=1
else
    OUT=Makefile
fi
sed 's/^X//' > $OUT <<'@//E*O*F Makefile//'
XOBJS  = xhpgl.o \
X	y.tab.o \
X	lex.yy.o
X
XSRC   = xhpgl.c
X
XPARSER = hpgl.l \
X	 hpgl.y
X
XCOMPPARS = lex.yy.c \
X	   y.tab.c \
X	   y.tab.h \
X	   y.output
X
XFILES = Makefile
XHEADERS = 
X
XBITMAPS = black.bitmap
X
XLINTFLAGS =
XCFLGS = -O -DUI38
X#CFLGS = -O -DULTRIX
XLLIB = -lX -lm
XLEXFLAGS = 
XYACCFLAGS = -dv
X
X#BINDIR = /eng/eng4/ryach/local/uvax
XBINDIR = /eng/eng4/ryach/local/ui38
XMANDIR = /eng/eng4/ryach/local/man
XCHAPTER = 1
X
Xxhpgl:   $(OBJS) xhpgl.$(CHAPTER)
X	  /bin/cc $(CFLGS) $(OBJS) -o xhpgl $(LLIB)
X
Xy.tab.o:  y.tab.c
X	  /bin/cc $(CFLGS) -c y.tab.c
X
Xlex.yy.o: lex.yy.c
X	  /bin/cc $(CFLGS) -c lex.yy.c
X
Xy.tab.c: hpgl.y $(FILES)
X	  yacc $(YACCFLAGS) hpgl.y
X
Xlex.yy.c: hpgl.l y.tab.h $(FILES)
X	  lex $(LEXFLAGS) hpgl.l
X
Xxhpgl.o:  xhpgl.c $(FILES) $(BITMAPS) $(HEADERS) 
X	  /bin/cc $(CFLGS) -c xhpgl.c
X
X$(SRC):
X	  sccs get $(SRC)
X
X$(PARSER):
X	  sccs get $(PARSER)
X
X$(BITMAPS):
X	  sccs get $(BITMAPS)
X
Xxhpgl.$(CHAPTER): xhpgl.man
X	  sed 's/XHPGL 1/XHPGL $(CHAPTER)/' xhpgl.man > xhpgl.$(CHAPTER)
X
Xxhpgl.man:
X	  sccs get xhpgl.man
X
Xinstall:  
X	  cp xhpgl $(BINDIR)
X	  cp xhpgl.$(CHAPTER) $(MANDIR)
X	  chmod 755 $(BINDIR)/xhpgl
X	  chmod 644 $(MANDIR)/xhpgl.$(CHAPTER)
X	  
Xclean:
X	  /bin/rm $(OBJS) $(COMPPARS) xhpgl xhpgl.$(CHAPTER)
X	  
Xlint:	  
X	  lint $(LINTFLAGS) $(SRC) > lint.log
@//E*O*F Makefile//
chmod u=rw,g=rw,o=rw $OUT

echo x - README
if test -f README ; then
    echo README exists, putting output in $$README
    OUT=$$README
    STATUS=1
else
    OUT=README
fi
sed 's/^X//' > $OUT <<'@//E*O*F README//'
Xxhpgl is a quick and dirty 7470a hpgl display program.  It was written on 
Xa Sun 382i and tested on Sunos 4.0 and ultrix 3.2.  All it takes to compile
Xthe code is to modify the BINDIR pointer and MANDIR pointer and CHAPTER in the
XMakefile and type make and make install.  It should have no problems.  Sorry,
XI still do not have X11 so it is written for X10 and was tested for X10.3 and
XX10.4.
X
XIf xhpgl is being compiled on ultrix change -DUI38 in the Makefile to
X-DULTRIX.  -DUI38 is for the sun 386i.
X
XThe file pc.xw is a sample hpgl format file to use to test the software once
Xit has been compiled.  Other files are up to the user to generate.
X
XThe file hpcolors is a default color file to be placed in the users home
Xdirectory as .hpcolors.
X
XAny problems may be sent to:
X
X	ryach at sedona.intel.com
@//E*O*F README//
chmod u=rw,g=rw,o=rw $OUT
echo Inspecting for damage in transit...
temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
cat > $temp <<!!!
       7      27     218 black.bitmap
       8      16      64 hpcolors
     113     272    3251 hpgl.l
     215     793    6026 hpgl.y
     181     192    4297 pc.xw
     519    2043   19055 xhpgl.c
      65     452    2532 xhpgl.man
      75     156    1276 Makefile
      19     151     801 README
    1202    4102   37520 total
!!!
wc black.bitmap hpcolors hpgl.l hpgl.y pc.xw xhpgl.c xhpgl.man Makefile README | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
if test -s $dtemp ; then
    echo "Ouch [diff of wc output]:"
    cat $dtemp
    STATUS=1
elif test $STATUS = 0 ; then
    echo "No problems found."
else
    echo "WARNING -- PROBLEMS WERE FOUND..."
fi
exit $STATUS
---                    :     __
Randy L. Yach          :   /__/  __   __   __/       /  /  __  __  /__   
Intel Corporation      :  /  \_/__/_/  /_/__/_/__/   \_/_/__/_/___/  /   
ryach at sedona.intel.com :                        /     /



More information about the Comp.sources.x mailing list