File: tek2plot.c

package info (click to toggle)
plotutils 2.4.1-15
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,072 kB
  • ctags: 6,952
  • sloc: ansic: 76,305; cpp: 12,402; sh: 8,475; yacc: 2,604; makefile: 894; lex: 144
file content (1434 lines) | stat: -rw-r--r-- 40,465 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
/* This file is the main routine for GNU tek2plot.  It reads a stream of
   Tektronix commands and draws graphics in real time by calling the
   appropriate routines in GNU libplot.  Written by Robert S. Maier
   <rsm@math.arizona.edu>.  Based on earlier work by Rich Murphey and by
   Edward Moy <moy@parc.xerox.com>.

   The table-driven parser is based on the one written by Ed at Berkeley in
   the mid-'80s.  The parsing tables in Tektable.c are essentially the same
   as the ones he designed for his `tek2ps' utility and for the Tektronix
   emulator included in the X10 and X11 versions of xterm(1).  All GNU
   extensions to his work copyright (C) 1989-1998 Free Software Foundation,
   Inc. */

/* The basic reference on the features of the Tektronix 4014 with extended
   graphics module (EGM), which is what we emulate, is the 4014 Service
   Manual (Tektronix Part #070-1648-00, dated 8/74; there is also a User
   Manual [Part #070-1647-00]).  
   
   The code below emulates the non-interactive features of a Tektronix 4014
   with EGM [Extended Graphics Module], though not the interactive ones
   such as GIN mode or status inquiry.  It also doesn't support
   write-through mode or beam defocusing.  It does support the ANSI color
   extensions (ISO-6429) recognized by the MS-DOS Kermit v2.31 Tektronix
   emulator.  It also recognizes, and ignores, the VT340-style control
   sequences ESC [ ?38h (switch to Tektronix mode), ESC [ ?38l (switch to
   native mode), and ESC ^C (switch to native mode), which are used by some
   Tektronix emulators. */

#include "sys-defines.h"
#include "plot.h"
#include "getopt.h"
#include "Tekparse.h"

const char *progname = "tek2plot"; /* name of this program */

const char *usage_appendage = " [FILE]...\n\
With no FILE, or when FILE is -, read standard input.\n";

/* Default font, ideally monospaced.  Each character in the font should
   ideally have a width equal to CHAR_WIDTH em, i.e. a width equal to
   CHAR_WIDTH times the font size. */
#define CHAR_WIDTH 0.6		/* valid for Courier family, at least */
#define DEFAULT_PS_FONT_NAME "Courier"
#define DEFAULT_PCL_FONT_NAME "Courier"
#define DEFAULT_HERSHEY_FONT_NAME "HersheySerif" /* not monospaced */

/* Coordinates in Tek file should be in range [0..4095]x[0..3119].  So to
   center points within a virtual graphics display of size
   [0..4095]x[0..4095], we add 488 to each y coordinate. */
#define TEK_WIDTH 4096
#define YOFFSET 488

/* Font size of the libplot marker symbol used to represent a `point', in
   Tek units.  We represent a Tektronix point by marker symbol #1, i.e. a
   dot (filled circle), which by convention has diameter 3/32 times the
   font size.  [See libplot/g_mark.c.]  So to get a diameter of 1 Tek unit,
   we choose 10 here. */
#define DOT_SIZE 10

/* parse tables in Tektable.c */
extern int Talptable[];
extern int Tbestable[];
extern int Tbyptable[];
extern int Tesctable[];
extern int Tipltable[];
extern int Tplttable[];
extern int Tpttable[];
extern int Tspttable[];

/* maximum size ANSI escape sequence we can handle */
#define BUFFER_SIZE 128

/* metrics for the four Tektronix fonts */
struct Tek_Char
{
  int hsize;	/* in Tek units */
  int vsize;	/* in Tek units */
  int charsperline;
  int nlines;
};

static const struct Tek_Char TekChar[4] = 
{ 
  {56, 88, 74, 35},	/* large */
  {51, 82, 81, 38},	/* #2 */
  {34, 53, 121, 58},	/* #3 */
  {31, 48, 133, 64},	/* small */
};

#define TEXT_BUFFER_SIZE 256	/* must be able to handle a full line */

/* Tektronix line types */
const char *linemodes[8] =
{
  "solid", "dotted", "dotdashed", "shortdashed", "longdashed",
  "solid", "solid", "solid"	/* final three treated as solid */
};

#define	TEKHOME		((TekChar[fontsize].nlines - 1)\
			 * TekChar[fontsize].vsize)
#define	MARGIN1		0	/* i.e. left edge */
#define	MARGIN2		1	/* i.e. half-way across page */

enum { PENDOWN, PENUP };
enum { NORTH = 04, SOUTH = 010, EAST = 01, WEST = 02 };

#define PRINTABLE_ASCII(c) ((c >= 0x20) && (c <= 0x7E))
#define	BEL		07

/* masks for coordinate-reading DFA */
#define ONE_BIT (0x1)
#define TWO_BITS (0x3)
#define FOUR_BITS (0x0f)
#define FIVE_BITS (0x1f)
#define TEN_BITS (0x3ff)

/* Long options we recognize */

#define	ARG_NONE	0
#define	ARG_REQUIRED	1
#define	ARG_OPTIONAL	2

struct option long_options[] = 
{
  /* The most important option */
  {"display-type",	ARG_REQUIRED,	NULL, 'T'},
  /* Other frequently used options */
  { "bg-color",		ARG_REQUIRED,	NULL, 'q' << 8 },
  { "bitmap-size",	ARG_REQUIRED,	NULL, 'B' << 8 },
  { "emulate-color",	ARG_REQUIRED,	NULL, 'e' << 8 },
  { "font-name",	ARG_REQUIRED,	NULL, 'F' },
  { "line-width",	ARG_REQUIRED,	NULL, 'W' },
  { "pen-color",	ARG_REQUIRED,	NULL, 'C' << 8 },
  { "max-line-length",	ARG_REQUIRED,	NULL, 'M' << 8 },
  { "page-number",	ARG_REQUIRED,	NULL, 'p' },
  { "page-size",	ARG_REQUIRED,	NULL, 'P' << 8 },
  { "position-chars",	ARG_NONE,	NULL, 'S' << 8 },
  { "rotation",		ARG_REQUIRED,	NULL, 'r' << 8},
  { "use-tek-fonts",	ARG_NONE,	NULL, 't' << 8 },
  /* Options relevant only to raw tek2plot (refers to metafile output) */
  { "portable-output",	ARG_NONE,	NULL, 'O' },
  /* Documentation options */
  { "help-fonts",	ARG_NONE,	NULL, 'f' << 8 },
  { "list-fonts",	ARG_NONE,	NULL, 'l' << 8 },
  { "version",		ARG_NONE,	NULL, 'V' << 8 },
  { "help",		ARG_NONE,	NULL, 'h' << 8 },
  { NULL,		0,		NULL, 0}
};

/* null-terminated list of options that we don't show to the user */
int hidden_options[] = { 0 };

typedef struct
{
  int red;
  int green;
  int blue;
} Color;

/* ANSI (ISO-6429) color extensions.  Scheme is essentially:
	0 = normal, 1 = bright
        foreground color (30-37) = 30 + colors
                where colors are   1=red, 2=green, 4=blue
	background color is similar, with `40' replacing `30'. */

const Color ansi_color[16] =
{
  {0x0000, 0x0000, 0x0000},		/* black, \033[0;30m */
  {0x8b8b, 0x0000, 0x0000},		/* red4 \033[0;31m */
  {0x0000, 0x8b8b, 0x0000},		/* green4 \033[0;32m */
  {0x8b8b, 0x8b8b, 0x0000},		/* yellow4 \033[0;33m */
  {0x0000, 0x0000, 0x8b8b},		/* blue4 \033[0;34m */
  {0x8b8b, 0x0000, 0x8b8b},		/* magenta4 \033[0;35m */
  {0x0000, 0x8b8b, 0x8b8b},		/* cyan4, \033[0;36m */
  {0x8b8b, 0x8b8b, 0x8b8b},		/* gray55 \033[0;37m */
  {0x4d4d, 0x4d4d, 0x4d4d},		/* gray30 \033[1;30m */
  {0xffff, 0x0000, 0x0000},		/* red \033[1;31m */
  {0x0000, 0xffff, 0x0000},		/* green \033[1;32m */
  {0xffff, 0xffff, 0x0000},		/* yellow \033[1;33m */
  {0x0000, 0x0000, 0xffff},		/* blue \033[1;34m */
  {0xffff, 0x0000, 0xffff},		/* magenta \033[1;35m */
  {0x0000, 0xffff, 0xffff},		/* cyan \033[1;36m */
  {0xffff, 0xffff, 0xffff}		/* white \033[1;37m */
};

/* global variables set on command line, used in Tek parsing routine */
bool position_indiv_chars = false; /* user may set this */
bool single_page_is_requested = false; /* set if user uses -p option */
bool use_tek_fonts = false;	/* fonts tekfont0..tekfont3 available? */
bool force_hershey_default = false; /* default font sh'd be Hershey? [kludge]*/
char *font_name = NULL;		/* initial font name, can be spec'd by user */
char *pen_color = NULL;		/* initial pen color, can be spec'd by user */
double line_width = -1.0;	/* initial line width, <0 means default */
int requested_page = 0;		/* user sets this via -p option */

/* variables used in parser */
bool plotter_open = false;
bool plotter_opened = false;
int cur_X = 0, cur_Y = 0;	/* graphics cursor position in Tek coors */
int current_page = 0;		/* page count */

/* forward references */
bool getpoint ____P((int *xcoor, int *ycoor, FILE *stream, int *badstatus, int *margin));
bool read_plot ____P((plPlotter *plotter, FILE *in_stream));
int read_byte ____P((FILE *stream, int *badstatus));
void begin_page ____P((plPlotter *plotter));
void end_page ____P((plPlotter *plotter));
void set_font_size ____P ((plPlotter *plotter, int new_fontsize));
void unread_byte ____P((int byte, FILE *in_stream, int *badstatus));
/* from libcommon */
extern int display_fonts ____P((const char *display_type, const char *progname));
extern int list_fonts ____P((const char *display_type, const char *progname));
extern void display_usage ____P((const char *progname, const int *omit_vals, const char *appendage, bool fonts));
extern void display_version ____P((const char *progname)); 
extern voidptr_t xcalloc ____P ((size_t nmemb, size_t size));
extern voidptr_t xmalloc ____P ((size_t size));
extern char *xstrdup ____P ((const char *s));

int
#ifdef _HAVE_PROTOS
main (int argc, char *argv[])
#else
main (argc, argv)
     int argc;
     char *argv[];
#endif
{
  plPlotter *plotter;
  plPlotterParams *plotter_params;
  bool do_list_fonts = false;	/* show a list of fonts? */
  bool show_fonts = false;	/* supply help on fonts? */
  bool show_usage = false;	/* show usage message? */
  bool show_version = false;	/* show version message? */
  char *display_type = (char *)"meta"; /* default libplot output format */
  double local_line_width;	/* temporary storage */
  int errcnt = 0;		/* errors encountered */
  int local_page_number;	/* temporary storage */
  int opt_index;		/* long option index */
  int option;			/* option character */
  int retval;			/* return value */

  plotter_params = pl_newplparams ();
  while ((option = getopt_long (argc, argv, "Op:F:W:T:", long_options, &opt_index)) != EOF) 
    {
      if (option == 0)
	option = long_options[opt_index].val;
      
      switch (option)
	{
	case 'T':		/* Display type, ARG REQUIRED      */
	  display_type = (char *)xmalloc (strlen (optarg) + 1);
	  strcpy (display_type, optarg);

	  /* Kludge: if HP-GL[/2] output is requested, be sure to use a
	     Hershey font as the default font, even though the Plotter
	     nominally supports PS fonts.  Reason: nominal != real. */

	  if (strcasecmp (display_type, "hpgl") == 0)
	    force_hershey_default = true;
	  else
	    force_hershey_default = false;
	  break;
	case 'F':		/* set the initial font */
	  font_name = (char *)xmalloc (strlen (optarg) + 1);
	  strcpy (font_name, optarg);
	  break;
	case 'p':		/* page number */
	  if (sscanf (optarg, "%d", &local_page_number) <= 0
	      || local_page_number < 0)
	    {
	      fprintf (stderr,
		       "%s: error: page number must be a nonnegative integer, was `%s'\n",
		       progname, optarg);
	      errcnt++;
	    }
	  else
	    {
	      requested_page = local_page_number;
	      single_page_is_requested = true;
	    }
	  break;
	case 'W':		/* set the initial line width */
	  if (sscanf (optarg, "%lf", &local_line_width) <= 0)
	    {
	      fprintf (stderr,
		       "%s: error: line thickness must be a number, was `%s'\n",
		       progname, optarg);
	      errcnt++;
	      break;
	    }
	  if (local_line_width < 0.0)
	    fprintf (stderr, "%s: ignoring negative line thickness `%f'\n",
		     progname, local_line_width);
	  else
	    line_width = local_line_width;
	  break;
	case 'O':		/* Portable version of metafile output */
	  pl_setplparam (plotter_params, "META_PORTABLE", (voidptr_t)"yes");
	  break;

	  /*---------------- Long options below here ----------------*/
	case 'e' << 8:		/* Emulate color via grayscale */
	  pl_setplparam (plotter_params, "EMULATE_COLOR", (voidptr_t)optarg);
	  break;
	case 'q' << 8:		/* Set the initial background color */
	  pl_setplparam (plotter_params, "BG_COLOR", (voidptr_t)optarg);
	  break;
	case 'B' << 8:		/* Bitmap size */
	  pl_setplparam (plotter_params, "BITMAPSIZE", (voidptr_t)optarg);
	  break;
	case 'C' << 8:		/* Set the initial pen color */
	  pen_color = (char *)xmalloc (strlen (optarg) + 1);
	  strcpy (pen_color, optarg);
	  break;
	case 'M' << 8:		/* Max line length */
	  pl_setplparam (plotter_params, "MAX_LINE_LENGTH", (voidptr_t)optarg);
	  break;
	case 'P' << 8:		/* Page size */
	  pl_setplparam (plotter_params, "PAGESIZE", (voidptr_t)optarg);
	  break;
	case 'S' << 8:        /* Position chars in text strings individually */
	  position_indiv_chars = true;
	  break;
	case 'r' << 8:		/* Rotation angle */
	  pl_setplparam (plotter_params, "ROTATION", (voidptr_t)optarg);
	  break;
	case 't' << 8:		/* Use Tektronix fonts (must be installed) */
	  if (strcmp (display_type, "X") == 0)
	    use_tek_fonts = true;
	  break;

	case 'f' << 8:		/* Fonts */
	  show_fonts = true;
	  break;
	case 'l' << 8:		/* Fonts */
	  do_list_fonts = true;
	  break;
	case 'h' << 8:		/* Help */
	  show_usage = true;
	  break;
	case 'V' << 8:		/* Version */
	  show_version = true;
	  break;

	default:
	  errcnt++;
	  break;
	}
    }
  
  if (errcnt > 0)
    {
      fprintf (stderr, "Try `%s --help' for more information\n", progname);
      return EXIT_FAILURE;
    }
  if (show_version)
    {
      display_version (progname);
      return EXIT_SUCCESS;
    }
  if (do_list_fonts)
    {
      int success;

      success = list_fonts (display_type, progname);
      if (success)
	return EXIT_SUCCESS;
      else
	return EXIT_FAILURE;
    }
  if (show_fonts)
    {
      int success;

      success = display_fonts (display_type, progname);
      if (success)
	return EXIT_SUCCESS;
      else
	return EXIT_FAILURE;
    }
  if (show_usage)
    {
      display_usage (progname, hidden_options, usage_appendage, true);
      return EXIT_SUCCESS;
    }

  /* turn off special interpretation of `erase' in GIF Plotters */
  pl_setplparam (plotter_params, "GIF_ANIMATION", (voidptr_t)"no");

  if ((plotter = pl_newpl_r (display_type, NULL, stdout, stderr,
			     plotter_params)) == NULL)
    {
      fprintf (stderr, "%s: error: could not create plot device\n", progname);
      return EXIT_FAILURE;
    }

  retval = EXIT_SUCCESS;
  if (optind < argc)
    /* input files (or stdin) named explicitly on the command line */
    {
      for (; optind < argc; optind++)
	{
	  FILE *data_file;
	  
	  if (strcmp (argv[optind], "-") == 0)
	    data_file = stdin;
	  else
	    {
	      data_file = fopen (argv[optind], "r");
	      if (data_file == NULL)
		{
		  fprintf (stderr, "%s: %s: %s\n", progname, argv[optind], strerror(errno));
		  fprintf (stderr, "%s: ignoring this file\n", progname);
		  errno = 0;	/* not quite fatal */
		  retval = EXIT_FAILURE;
		  continue;	/* back to top of for loop */
		}
	    }
	  if (read_plot (plotter, data_file) == false)
	    {
		  fprintf (stderr, "%s: could not parse input file `%s'\n",
			   progname, argv[optind]);
		  retval = EXIT_FAILURE;
		  continue;	/* back to top of for loop */
	    }

	  if (data_file != stdin) /* Don't close stdin */
	    if (fclose (data_file) < 0)
	      {
		fprintf (stderr, 
			 "%s: error: could not close input file `%s'\n",
			 progname, argv[optind]);
		return EXIT_FAILURE; /* exit immediately */
	      }
	}
    } /* endfor */
  else
    /* no files/streams spec'd on the command line, just read stdin */
    {
      if (read_plot (plotter, stdin) == false)
	{
	  fprintf (stderr, "%s: could not parse input\n", progname);
	  retval = EXIT_FAILURE;
	}
    }

  /* if nothing was emitted ... */
  if (plotter_opened == false)
    {
      if (single_page_is_requested == false)
	/* output a blank page */
	{
	  begin_page (plotter);
	  end_page (plotter);
	}
      else
	{
	  if (requested_page >= current_page)
	    {
	      fprintf (stderr, "%s: requested page does not exist\n", progname);
	      retval = EXIT_FAILURE;
	    }
	  else
	    /* page must have been seen, but was empty; output a blank page */
	    {
	      begin_page (plotter);
	      end_page (plotter);
	    }
	}
    }

  if (pl_deletepl_r (plotter) < 0)
    {
      fprintf (stderr, "%s: error: could not delete plot device\n", progname);
      retval = EXIT_FAILURE;
    }
  pl_deleteplparams (plotter_params);

  return retval;
}

void
#ifdef _HAVE_PROTOS
unread_byte (int c, FILE *in_stream, int *badstatus)
#else
unread_byte (c, in_stream, badstatus)
     int c;
     FILE *in_stream;
     int *badstatus;
#endif
{
  if (*badstatus == 0)
    {
      if (ungetc (c, in_stream) == EOF)	/* means error, not EOF */
	*badstatus = 2;		/* treat as EOF anyway */
    }
}

int 
#ifdef _HAVE_PROTOS
read_byte (FILE *in_stream, int *badstatus)
#else
read_byte (in_stream, badstatus)
     FILE *in_stream;
     int *badstatus;
#endif
{
  int i;

  if (*badstatus == 1)		/* status = parse error */
    return 0;
  i = getc (in_stream);
  if (i == EOF)
    {
      *badstatus = 2;		/* status = eof */
      return 0;
    }
  return (i & 0x7f);		/* high bit ignored */
}


/* getpoint() reads a point (x,y) from the input stream, in Tektronix
   format, and returns it.  A point is a pair of coordinates in the range
   0..4095.  Reading a point will normally require reading anywhere between
   1 and 5 bytes from the input stream.  This function contains internal
   state: several static variables.

   Return value indicates whether a point is successfully read.  Failure to
   return a point may occur on account of a parsing problem or because of
   eof.  In either of these two cases an error code is returned through
   `badstatus', signalling that parsing of the input stream should
   terminate.

   A point may also fail to be returned if the first byte that is read from
   the input stream is not a byte in the 0x20..0xff range.  Normally, no
   byte in the range 0x00..0x1f range may be part of a point.  So if such a
   byte is seen, it is pushed back on the stream and point-reading is
   aborted (no error code is returned through badstatus).  Note that if the
   very first byte that is read is in this range, this function may return
   having read, in all, 0 bytes.

   An exception to the last rule: if any of the bytes CR, LF, or NUL is
   seen during the reading of a point, it is discarded and reading
   continues.  So it is also possible that >5 bytes may be read in all.

   A possible side effect of calling getpoint(): if the MSB of the egm
   byte, which is one of the bytes that make up the point, is set, then the
   left-hand margin will be set to MARGIN2, i.e. to 2048.  */

bool
#ifdef _HAVE_PROTOS
getpoint (int *xcoor, int *ycoor, FILE *in_stream, int *badstatus, int *margin)
#else
getpoint (xcoor, ycoor, in_stream, badstatus, margin)
     int *xcoor, *ycoor;
     FILE *in_stream;
     int *badstatus;
     int *margin;
#endif
{
  /* variables for the point-reading DFA, initialized */
  int status_one = 0, status_three = 0;	/* 0=none, 1=seen one, 2=finished */
  bool got_lo_y = false;
  bool got_hi_x = false, got_hi_y = false;
  int lo_x = 0, lo_y = 0, hi_x = 0, hi_y = 0;
  bool got_egm = false;
  int egm = 0;
  int temp_three = 0;

  /* following variables are saved from point to point */
  static int saved_lo_y = 0, saved_hi_x = 0, saved_hi_y = 0;
  static bool margin_reset = false;

  int byte_read, type;

  if (*badstatus)
    return false;

  for ( ; ; )
    {
      byte_read = read_byte (in_stream, badstatus);
      if (*badstatus)
	return false;

      /* Ignore high bit (bit 8); bit pattern of next two bits (bits 7/6)
	 determines what sort of coordinate byte we have.  1 = Hi_X or
	 Hi_Y, 2 = Lo_X, 3 = Lo_Y or EGM; 0 usually means abort point.
	 Coordinate bytes appear in order

	 	[Hi_Y] [EGM] [Lo_Y] [Hi_X] Lo_X.  

		  1      3     3       1    2

	 All save last are optional, except that if EGM or Hi_X is
	 transmitted, also need need a Lo_Y.  We remember old values of
	 Hi_Y, Lo_Y, Hi_X, although not EGM or Lo_X, in our DFA. */

      type = (byte_read>>5) & TWO_BITS; /* type of byte */
      byte_read &= FIVE_BITS;	/* mask off 5 relevant bits */
      
      switch (type)
	{
	case 0:			/* interruption of point-reading (parse error?) */
	  fprintf (stderr, 
		   "%s: ignoring incomplete point in input\n",
		   progname);
	  if (byte_read == '\n' || byte_read == '\r' || byte_read == '\0')
	    continue;		/* discard, on to next byte */
	  else
	    /* put unread byte back on stream; hope we can parse it later */
	    unread_byte (byte_read, in_stream, badstatus);
	    return false;
	case 1:		/* Hi_Y or Hi_X */
	  switch (status_one)
	    {
	    case 0:
	      if (status_three)
		{
		  hi_x = byte_read; /* 2nd = Hi_X */
		  got_hi_x = true;
		  if (status_three == 1) 
		    {
		      lo_y = temp_three; /* Lo_Y */
		      got_lo_y = true;
		    }
		  
		  status_one = 2; /* no more 1's */
		  status_three = 2; /* no more 3's */
		}
	      else
		{
		  hi_y = byte_read; /* 1st = Hi_Y */
		  got_hi_y = true;
		  status_one = 1;
		}
	      break;
	    case 1:
	      if (status_three == 0)
		{
		  fprintf (stderr, 
			   "%s: error: point in input has Hi_Y, Hi_X bytes with no Lo_Y between\n",
			   progname);
		  *badstatus = 1; /* parse error */
		  return false;
		}
	      if (status_three == 1) 
		{
		  
		  lo_y = temp_three; /* Lo_Y */
		  got_lo_y = true;
		}
	      hi_x = byte_read; /* 2nd = Hi_X */
	      got_hi_x = true;
	      status_one = 2; /* no more 1's */
	      status_three = 2; /* no more 3's */
	      break;
	    case 2:
	      fprintf (stderr, 
		       "%s: error: point in input contains too many Hi_Y/Hi_X bytes\n",
		       progname);
	      *badstatus = 1; /* parse error */
	      return false;
	    }
	  break;
	case 3:		/* EGM or Lo_Y */
	  switch (status_three)
	    {
	    case 0:
	      if (status_one == 2)
		{
		  fprintf (stderr, 
			   "%s: error: point in input has EGM/Lo_Y byte after 2 Hi_X/Hi_Y bytes\n",
			   progname);
		  *badstatus = 1; /* parse error */
		  return false;
		}
	      else
		{
		  temp_three = byte_read;
		  status_three = 1;
		}
	      break;
	    case 1:
	      if (status_one == 2)
		{
		  fprintf (stderr, 
			   "%s: error: point in input has EGM/Lo_Y byte after 2 Hi_X/Hi_Y bytes\n",
			   progname);
		  *badstatus = 1; /* parse error */
		  return false;
		}
	      egm = temp_three; /* 1st = EGM */
	      got_egm = true;
	      lo_y = byte_read; /* 2nd = Lo_Y */
	      got_lo_y = true;
	      status_three = 2;
	      break;
	    case 2:
	      fprintf (stderr, 
		       "%s: error: point in input has too many EGM/Lo_Y bytes\n",
		       progname);
	      *badstatus = 1; /* parse error */
	      return false;
	    }
	  break;
	case 2:		/* Lo_X, final byte */
	  {
	    int low_res_x, low_res_y;
	    int x, y;
	    
	    if (status_three == 1)
	      {
		lo_y = temp_three; /* Lo_Y */
		got_lo_y = true;
	      }
	    lo_x = byte_read; /* Lo_X */
	    
	    lo_y = got_lo_y ? lo_y : saved_lo_y;	      
	    hi_x = got_hi_x ? hi_x : saved_hi_x;
	    hi_y = got_hi_y ? hi_y : saved_hi_y;
	    
	    saved_lo_y = lo_y;
	    saved_hi_x = hi_x;
	    saved_hi_y = hi_y;	      
	    
	    /* On a genuine Tektronix 4014, the MSB of the 5-bit EGM
	       byte sets the margin to equal Margin2 (2048) */
	    if ((egm >> 4) & ONE_BIT)
	      {
		*margin = MARGIN2;
		if (margin_reset == false)
		  fprintf (stderr,
			   "%s: Tektronix left margin reset by input file(s)\n",
			   progname);
		margin_reset = true;
	      }
	    
	    /* low_res is what we'd use on a pre-EGM Tektronix */
	    low_res_x = (hi_x << 5) | lo_x;
	    low_res_y = (hi_y << 5) | lo_y;
	    x = (low_res_x << 2) | (egm & TWO_BITS);
	    y = (low_res_y << 2) | ((egm >> 2) & TWO_BITS);
	    
	    *xcoor = x;
	    *ycoor = y;
	    
	    return true;	/* end of `case 2' in switch: success */
	  }
	} /* end of switch */ 

    } /* end of while loop */
  /* NOTREACHED */
}


/* Parse a Tektronix stream and make appropriate libplot calls, paying
   attention to several global variables.  Will output at least one
   openpl()..closepl(). */
bool 
#ifdef _HAVE_PROTOS
read_plot (plPlotter *plotter, FILE *in_stream)
#else
read_plot (plotter, in_stream)
     plPlotter *plotter;
     FILE *in_stream;
#endif
{
  /* variables for DFA */
  int *Tparsestate = Talptable;	/* start in ALPHA mode */
  int *curstate = Talptable;	/* for temporary storage (for `bypass' mode) */
  int pen = PENUP;		/* pen up or pen down */
  int linetype = 0;		/* in range 0..7, 0 means "solid" */
  int fontsize = 0;		/* in range 0..3, 0 means large  */
  int margin = MARGIN1;		/* MARGIN1=left side, MARGIN2=halfway across */
  char text[TEXT_BUFFER_SIZE];	/* for storage of text strings */
  int badstatus = 0;		/* 0=OK, 1=parse error, 2=eof */

  while (!badstatus)		/* exit from loop only on parse error or eof */
    {
      int c;
      int x, y;

      c = read_byte (in_stream, &badstatus);
      if (badstatus)
	break;			/* parse error or eof; exit from while loop */

      switch (Tparsestate[c])
	{
	  /* Switch among 5 basic states: ALPHA, PLOT, POINT PLOT, 
	     SPECIAL POINT PLOT and INCREMENTAL PLOT. */

	case CASE_ALP_STATE:	/* Enter ALPHA mode */
	  Tparsestate = curstate = Talptable;
	  break;
	  
	case CASE_PLT_STATE:	/* Enter PLOT mode */
	  Tparsestate = curstate = Tplttable;
	  c = read_byte (in_stream, &badstatus);
	  /* do lookahead */
	  if (c == BEL)		
	    /* no initial dark vector */
	    pen = PENDOWN;
	  else 
	    {
	      pen = PENUP;
	      unread_byte (c, in_stream, &badstatus);
	    }
	  break;

	case CASE_PT_STATE:	/* Enter POINT PLOT mode */
	  Tparsestate = curstate = Tpttable;
	  break;
	  
	case CASE_SPT_STATE:	/* enter SPECIAL POINT PLOT mode */
	  Tparsestate = curstate = Tspttable;
	  break;
	  
	case CASE_IPL_STATE:	/* enter INCREMENTAL PLOT mode */
	  Tparsestate = curstate = Tipltable;
	  break;
	  
	  /*****************************************/

	  /* ALPHA mode commands */

	case CASE_PRINT:	/* printable character */
	  {
	    char *cp = text;
	    int x_here, y_here, n;
	    
	    x_here = cur_X, y_here = cur_Y;
	    
	    /* push back, so we can read string as a unit */
	    unread_byte (c, in_stream, &badstatus);
	    if (badstatus)
	      break;
	    
	    n = (position_indiv_chars ? 1 : TEXT_BUFFER_SIZE - 1);
	    y = cur_Y;
	    while (!badstatus && n-- > 0 && y == cur_Y) 
	      {
		c = read_byte (in_stream, &badstatus);
		if (badstatus)
		  {
		    break;	/* end label on eof or read error */
		  }
		
		if (!PRINTABLE_ASCII (c))
		  {
		    /* push back */
		    unread_byte (c, in_stream, &badstatus);
		    break;	/* end label on non-ascii character */
		  }
		*cp++ = c;
		
		/* following block is merely `cursor right' (cf. below) */
		{
		  const struct Tek_Char *t = &TekChar[fontsize];
		  int l;
		  
		  cur_X += t->hsize;
		  if (cur_X > TEK_WIDTH) 
		    {
		      l = cur_Y / t->vsize - 1;
		      if (l < 0)
			{
			  margin = !margin;
			  l = t->nlines - 1;
			}
		      cur_Y = l * t->vsize;
		      cur_X = (margin == MARGIN1 ? 0 : TEK_WIDTH / 2);
		    }
		}

	      } /* end of string-reading while loop */

	    *cp = '\0';		/* null-terminate string, and output it */
	    if (current_page == requested_page || !single_page_is_requested)
	      {
		if (plotter_open == false)
		  begin_page (plotter);
		if (position_indiv_chars)
		  /* string consists of a single char */
		  {
		    int halfwidth = TekChar[fontsize].hsize / 2;

		    pl_move_r (plotter, x_here + halfwidth, y_here + YOFFSET);
		    pl_alabel_r (plotter, 'c', 'b', text);
		  }
		else
		  {
		    pl_move_r (plotter, x_here, y_here + YOFFSET);
		    pl_alabel_r (plotter, 'l', 'b', text);
		  }
		pl_move_r (plotter, cur_X, cur_Y);
	      }

	  } /* end of CASE_PRINT */
	  break;

	  /* PLOT mode commands */

	case CASE_PLT_VEC:	/* PLT: vector */
	  /* push back, so we can read vector as a unit */
	  unread_byte (c, in_stream, &badstatus);
	  if (getpoint (&x, &y, in_stream, &badstatus, &margin)
	      && !badstatus)
	    /* N.B. getpoint returns w/o having read c only if c=0x00..0x1f,
	       so there's no chance of a infinite loop (see parsetable) */
	    {
	      if (current_page == requested_page || !single_page_is_requested)
		{
		  if (pen == PENDOWN)
		    {
		      if (plotter_open == false)
			begin_page (plotter);
		      pl_cont_r (plotter, x, y + YOFFSET);
		    }
		  else
		    {
		      /* N.B. Don't begin a new page just for a move() */ 
		      if (plotter_open == true)
			pl_move_r (plotter, x, y + YOFFSET);
		    }
		}
	      cur_X = x;
	      cur_Y = y;
	      pen = PENDOWN;
	    }
	  break;
	  
	  /* POINT PLOT mode commands */

	case CASE_PT_POINT:	/* PT: point */
	  /* push back, so we can read vector as a unit */
	  unread_byte (c, in_stream, &badstatus);
	  if (getpoint (&x, &y, in_stream, &badstatus, &margin)
	      && !badstatus)
	    /* N.B. getpoint returns w/o having read c only if c=0x00..0x1f,
	       so there's no chance of a infinite loop (see parsetable) */
	    {
	      if (current_page == requested_page || !single_page_is_requested)
		{
		  if (plotter_open == false)
		    begin_page (plotter);
		  pl_fmarker_r (plotter, 
				(double)x, (double)(y + YOFFSET), 
				M_DOT, (double)DOT_SIZE);
		}
	      cur_X = x;
	      cur_Y = y;
	    }
	  break;
	  
	  /* SPECIAL POINT PLOT mode commands */

	case CASE_SPT_POINT:	/* SPT: point */
	  /* don't push back c (ignore intensity byte) */
	  if (getpoint (&x, &y, in_stream, &badstatus, &margin)
	      && !badstatus)
	    /* N.B. getpoint returns w/o having read c only if c=0x00..0x1f,
	       so there's no chance of a infinite loop (see parsetable) */
	    {
	      /* assume intensity is > 0 */
	      if (current_page == requested_page || !single_page_is_requested)
		{
		  if (plotter_open == false)
		    begin_page (plotter);
		  pl_fmarker_r (plotter, 
				(double)x, (double)(y + YOFFSET), 
				M_DOT, (double)(DOT_SIZE));
		}
	      cur_X = x;
	      cur_Y = y;
	    }
	  break;
	  
	  /* INCREMENTAL PLOT mode commands */

	case CASE_PENUP:	/* IPL: penup */
	  pen = PENUP;
	  break;
	  
	case CASE_PENDOWN:	/* IPL: pendown */
	  pen = PENDOWN;
	  break;
	  
	case CASE_IPL_POINT:	/* IPL: point */
	  x = cur_X;
	  y = cur_Y;
	  if (c & NORTH)
	    y++;
	  else if (c & SOUTH)
	    y--;
	  if (c & EAST)
	    x++;
	  else if (c & WEST)
	    x--;
	  if (current_page == requested_page || !single_page_is_requested)
	    {
	      if (pen == PENDOWN)
		{
		  if (plotter_open == false)
		    begin_page (plotter);
		  pl_cont_r (plotter, x, y + YOFFSET);
		}
	      else
		{
		  /* N.B. Don't begin a new page just for a move() */
		  if (plotter_open == true)
		    pl_move_r (plotter, x, y + YOFFSET);
		}
	    }
	  cur_X = x;
	  cur_Y = y;
	  break;
	  
	  /****************************************/

	  /* These switch the parsetable temporarily to one of three
             pseudo-states, while stashing the current state. */

	case CASE_BES_STATE:	/* Byp: an escape char */
	  Tparsestate = Tbestable;
	  break;
	  
	case CASE_BYP_STATE:	/* set bypass condition */
	  Tparsestate = Tbyptable;
	  break;
	  
	case CASE_ESC_STATE:	/* ESC */
	  Tparsestate = Tesctable;
	  break;
	  
	  /*****************************************/

	  /* Cursor motion, useful mostly in ALPHA mode.  Some of these
	     restore the stashed state (if any) and some do not. */

	case CASE_CR:		/* CR */
	  cur_X = (margin == MARGIN1 ? 0 : TEK_WIDTH / 2);
	  Tparsestate = curstate = Talptable; /* switch to ALPHA mode */
	  break;
	  
	case CASE_BS:		/* BS, cursor left */
	  Tparsestate = curstate; /* clear bypass condition if any */
	  {
	    const struct Tek_Char *t;
	    int x, l;
	    
	    x = (cur_X -= (t = &TekChar[fontsize])->hsize);
	    if ((margin == MARGIN1 && x < 0)
		|| (margin == MARGIN2 && x < TEK_WIDTH / 2))
	      {
		if ((l = (cur_Y + (t->vsize - 1)) / t->vsize + 1) >=
		    t->nlines) 
		  {
		    margin = !margin;
		    l = 0;
		  }
		cur_Y = l * t->vsize;
		cur_X = (t->charsperline - 1) * t->hsize;
	      }
	  }
	  break;
	  
	case CASE_TAB:		/* HT */
	  Tparsestate = curstate; /* clear bypass condition if any */
	  /* FALL THROUGH */

	case CASE_SP:		/* SP, cursor right */
	  {
	    const struct Tek_Char *t = &TekChar[fontsize];
	    int l;

	    cur_X += t->hsize;
	    if (cur_X > TEK_WIDTH) 
	      {
		l = cur_Y / t->vsize - 1;
		if (l < 0) 
		  {
		    margin = !margin;
		    l = t->nlines - 1;
		  }
		cur_Y = l * t->vsize;
		cur_X = (margin == MARGIN1 ? 0 : TEK_WIDTH / 2);
	      }
	  }
	  break;
	  
	case CASE_LF:		/* LF, cursor down */
	  {
	    const struct Tek_Char *t;
	    int l;
	    
	    t = &TekChar[fontsize];
	    if ((l = cur_Y / t->vsize - 1) < 0) 
	      {
		l = t->nlines - 1;
		if ((margin = !margin) != MARGIN1) 
		  {
		    if (cur_X < TEK_WIDTH / 2)
		      cur_X += TEK_WIDTH / 2;
		  } 
		else if (cur_X >= TEK_WIDTH / 2)
		  cur_X -= TEK_WIDTH / 2;
	      }
	    cur_Y = l * t->vsize;
	  }
	  break;
	  
	case CASE_UP:		/* cursor up */
	  Tparsestate = curstate; /* clear bypass condition if any */
	  {
	    const struct Tek_Char *t;
	    int l;
	    
	    t = &TekChar[fontsize];
	    if ((l = (cur_Y + (t->vsize - 1)) / t->vsize + 1) >= t->nlines) 
	      {
		l = 0;
		if ((margin = !margin) != MARGIN1) 
		  {
		    if (cur_X < TEK_WIDTH / 2)
		      cur_X += TEK_WIDTH / 2;
		  } 
		else if (cur_X >= TEK_WIDTH / 2)
		  cur_X -= TEK_WIDTH / 2;
	      }
	    cur_Y = l * t->vsize;
	  }
	  break;

	  /****************************************/

	  /* Miscellaneous: functions we interpret as `next page',
	     `set font size', and `set line type'. */

	case CASE_PAGE:		/* page function, clears bypass cond. */
	  /* do closepl to flush out page (if nonempty) */
	  if (plotter_open == true)
	    end_page (plotter);

	  if (single_page_is_requested && current_page == requested_page)
	    {
	      badstatus = 2;	/* requested page is finished, so signal eof */
	      break;		/* exit from while loop */
	    }

	  /* now beginning parse of a new Tektronix page */
	  current_page++;

	  /* special case: if only a single page is requested, and line
	     mode and font size have changed due to commands on previous
	     Tek pages, output them */
	  if (single_page_is_requested && current_page == requested_page)
	    {
	      if (linetype != 0)
		{
		  if (plotter_open == false)
		    begin_page (plotter);
		  pl_linemod_r (plotter, linemodes[linetype]);
		}
	      if (fontsize != 0)
		{
		  if (plotter_open == false)
		    begin_page (plotter);
		  set_font_size (plotter, fontsize);
		}
	    }
	  cur_X = 0;
	  cur_Y = TEKHOME;		/* home pos. depends on fontsize */
	  break;
	  
	case CASE_CHAR_SIZE:	/* select font size */
	  fontsize = c & 03;
	  if (current_page == requested_page || !single_page_is_requested)
	    {
	      if (plotter_open == false)
		begin_page (plotter);
	      set_font_size (plotter, fontsize);
	    }
	  Tparsestate = curstate;
	  break;
	  
	case CASE_BEAM_VEC:	/* select beam and vector types */
	  /* disregard beam type */
	  c &= 07;
	  if (c != linetype)
	    if (current_page == requested_page || !single_page_is_requested)
	      {
		if (plotter_open == false)
		  begin_page (plotter);
		linetype = c;
		pl_linemod_r (plotter, linemodes[linetype]);
	      }
	  Tparsestate = curstate;
	  break;

	  /****************************************/

	  /* Things we ignore. */

	case CASE_OSC:		/* do osc escape */
	  /* ignore all bytes up to and including first non-ascii byte
	     (presumably BEL) */
	  do
	    c = read_byte (in_stream, &badstatus);
	  while (!badstatus && PRINTABLE_ASCII(c));
	  Tparsestate = curstate;
	  break;

	case CASE_ANSI:		/* parse an ANSI-style escape sequence */
	  {
	    char ansi[BUFFER_SIZE]; /* buffer for escape sequence */
	    char type = 0;	/* `type' (i.e. final byte) */
	    int i;		/* length of arg bytes, incl. separators */
	    
	    i = 0;
	    for ( ; ; )
	      {
		c = read_byte (in_stream, &badstatus);
		if (badstatus)
		  break;
		if ((c >= '0' && c <= '9') || c == ';'
		    || (i == 0 && c == '?'))
		  /* an arg byte, or a separator byte */
		  ansi[i++] = c;
		else
		  {
		    type = c;
		    if (!(PRINTABLE_ASCII(type)))
		      badstatus = 1; /* parse error */
		    break;
		  }
		if (i == BUFFER_SIZE)
		  {
		    fprintf (stderr, 
			     "%s: error: overly long ANSI escape sequence\n",
			     progname);
		    badstatus = 1; /* parse error */
		    break;
		  }
	      }
	    Tparsestate = curstate;
	    if (badstatus)
	      break;
	    
	    if (i == 3 && (type == 'h' || type == 'l')
		&& (ansi[0] == '?' && ansi[1] == '3' && ansi[2] == '8'))
	      /* switch to Tek or VT100 mode, ignore */
	      break;

	    if (i == 4 && type == 'm' 
		&& (ansi[0] == '0' || ansi[0] == '1') 
		&& ansi[1] == ';' && ansi[2] == '3'
		&& ansi[3] >= '0' && ansi[3] <= '7')
	      /* set ANSI foreground color */
	      {
		int intensity, color_index;

		if (plotter_open == false)
		  begin_page (plotter);
		intensity = ansi[0] - '0';
		color_index = ansi[3] - '0';
		pl_pencolor_r (plotter, 
			       ansi_color[8 * intensity + color_index].red,
			       ansi_color[8 * intensity + color_index].green,
			       ansi_color[8 * intensity + color_index].blue);
		break;
	      }
	  }
	  break;

	case CASE_IGNORE:	/* Esc: totally ignore CR, ESC, LF, ~ */
	  break;
	  
	  /****************************************/

	  /* We interpret these as just restoring the stashed 
	     state (if any). */

	case CASE_REPORT:	/* report address */
	case CASE_VT_MODE:	/* special return to vt102 mode */
	case CASE_BEL:		/* BEL */
	case CASE_COPY:		/* make copy */
	case CASE_CURSTATE:
	  Tparsestate = curstate; /* clear bypass condition */
	  break;
	  
	case CASE_ASCII:	/* select ASCII char set */
	  /* ignore for now */
	  Tparsestate = curstate;
	  break;
	  
	case CASE_APL:		/* select APL char set */
	  /* ignore for now */
	  Tparsestate = curstate;
	  break;
	  
	case CASE_GIN:		/* do Tek GIN mode */
	  Tparsestate = Tbyptable; /* Bypass mode */
	  break;

	}
    }

  /* end parsing of this Tektronix stream */

  if (plotter_open == true)	/* close plotter, i.e. end page if any */
    end_page (plotter);

  current_page++;		/* bump page count for next file if any */

  return (badstatus == 2 ? true : false); /* OK to end parse at EOF */
}


void
#ifdef _HAVE_PROTOS
set_font_size (plPlotter *plotter, int new_fontsize)
#else
set_font_size (plotter, new_fontsize)
     plPlotter *plotter;
     int new_fontsize;
#endif
{
  if (use_tek_fonts)
    /* switch among Tektronix fonts (may not be available on all X servers) */
    {
      switch (new_fontsize)
	{
	case 0:
	default:
	  pl_fontname_r (plotter, "tekfont0");
	  break;
	case 1:
	  pl_fontname_r (plotter, "tekfont1");
	  break;
	case 2:
	  pl_fontname_r (plotter, "tekfont2");
	  break;
	case 3:
	  pl_fontname_r (plotter, "tekfont3");
	  break;
	}
    }
  else
    /* we presumably are using a scalable font */
    pl_ffontsize_r (plotter,
		    (double)(TekChar[new_fontsize].hsize) / CHAR_WIDTH);
}

void
#ifdef _HAVE_PROTOS
begin_page (plPlotter *plotter)
#else
begin_page (plotter)
     plPlotter *plotter;
#endif
{
  if (pl_openpl_r (plotter) < 0)
    {
      fprintf (stderr, 
	       "%s: error: could not open plot device\n", 
	       progname);
      exit (EXIT_FAILURE);
    }
  plotter_open = true;
  plotter_opened = true;

  /* set background color, set affine map from user frame to device frame */
  pl_erase_r (plotter);
  pl_space_r (plotter, 0, 0, TEK_WIDTH - 1, TEK_WIDTH - 1);

  /* improve smoothness of plotted curves */
  pl_joinmod_r (plotter, "round");
  /* may be necessary if zero-length lines are to display as points */
  pl_capmod_r (plotter, "round");

  /* optionally initialize pen color, font, fontsize, line width */
  if (pen_color)
    pl_pencolorname_r (plotter, pen_color);
  if (use_tek_fonts)
    pl_fontname_r (plotter, "tekfont0");
  else
    {
      if (font_name)
	pl_fontname_r (plotter, font_name);
      else
	{
	  if (!force_hershey_default)
	    /* normal case */
	    {
	      if (pl_havecap_r (plotter, "PS_FONTS") == 1)
		pl_fontname_r (plotter, DEFAULT_PS_FONT_NAME);
	      else if (pl_havecap_r (plotter, "PCL_FONTS") == 1)
		pl_fontname_r (plotter, DEFAULT_PCL_FONT_NAME);
	      else
		/* use Hershey font as a default */
		pl_fontname_r (plotter, DEFAULT_HERSHEY_FONT_NAME);
	    }
	  else
	    /* forced to use Hershey font as a default, even if other fonts
	       are available (this happens for `-T hpgl'; see above) */
	    pl_fontname_r (plotter, DEFAULT_HERSHEY_FONT_NAME);
	}
      /* `large' is default size */
      pl_ffontsize_r (plotter, (double)(TekChar[0].hsize) / CHAR_WIDTH);
    }
  if (line_width >= 0.0)
    pl_flinewidth_r (plotter, line_width * TEK_WIDTH);

  /* move to current position on page */
  pl_move_r (plotter, cur_X, cur_Y + YOFFSET);
}

void
#ifdef _HAVE_PROTOS
end_page (plPlotter *plotter)
#else
end_page (plotter)
     plPlotter *plotter;
#endif
{
  if (pl_closepl_r (plotter) < 0)
    {
      fprintf (stderr, 
	       "%s: error: could not close plot device\n",
	       progname);
      exit (EXIT_FAILURE);
    }
  plotter_open = false;
}