File: pltek.c

package info (click to toggle)
plplot 5.3.1-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 26,248 kB
  • ctags: 11,687
  • sloc: ansic: 86,045; xml: 17,249; sh: 12,400; tcl: 8,113; cpp: 6,824; perl: 4,383; python: 3,915; makefile: 2,899; java: 2,788; fortran: 290; sed: 5; awk: 1
file content (231 lines) | stat: -rw-r--r-- 5,371 bytes parent folder | download | duplicates (2)
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
/* $Id: pltek.c,v 1.10 2001/01/10 06:45:32 mlebrun Exp $
 *
 *  Review a Tektronix vector file.
 *  from 'scan', by Clair Nielsen, LANL.
 *  Modifications by Maurice LeBrun, IFS.
 *
 *  This version works well with xterm and at least one vt100/tek emulator
 *  I've tried.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>

static void describe(void);
static void tek_mode(int mode);

#define ESC  27				/* ESCape character */

#define ALPHA_MODE	0		/* switch to alpha mode */
#define GRAPH_MODE	1 		/* switch to graph mode */
#define BUFSZ	1024
#define MAXPAGES 1024

enum { unknown, xterm, tekterm } termtype = unknown;


int
main(int argc, char *argv[])
{
    FILE *fd;
    int i, j, nb, npage, ipage, ifirst, oldpage;
    int istop;
    long start[MAXPAGES];		/* start (offset) of each page */
    char buf[BUFSZ], xtra, lastchar = '\0';
    char c, ibuf[128], *t;

    if (argc < 2) {
	describe();
	exit(1);
    }
    if ((fd = fopen(argv[1], "r")) == NULL) {
	printf("Cannot open %s\n", argv[1]);
	exit(1);
    }
   
    if ( (t = getenv("TERM")) != NULL ) {      
	if ( strcmp("xterm", t) == 0 ) { 
	    termtype = xterm;	 
	} else if (!strncmp("tek",t,3) ||
		   !strncmp("401",t,3) ||
		   !strncmp("410",t,3) ) {
	    termtype = tekterm;
	}
    }

/* Find out how many pages there are in file. */

    ipage = 0;
    start[0] = 0;
    for (i = 0; i < MAXPAGES; i++) {
	nb = fread(buf, 1, BUFSZ, fd);
	if (nb <= 0)
	    break;
	ifirst = 0;
	for (j = 0; j < nb; j++) {
	    if ((lastchar = buf[j]) == '\f') {
		ifirst = j - 1;
		ipage++;
		start[ipage] = BUFSZ * i + ifirst + 2;
	    }
	}
    }

/* don't count a FF at the end of the file as a separate page */

    if (lastchar == '\f')
	ipage--;

    npage = ipage + 1;

/* Loop until the user quits */

    ipage = 0;
    while (1) {
	oldpage = ipage;
	printf("Page %d/%d> ", ipage, npage);

	if (!fgets(ibuf, 128, stdin))
	    break;

	c = ibuf[0];

/* User input a page number or a return */
/* A carriage return in response to the prompt proceeds to the next page. */

	if (c == '\n') {	 	/* CR = increment by one page */
	    ipage++;
	} else if (c == '+') {		/* +<n> = increment by <n> pages */
	    ipage += atoi(ibuf+1);
	} else if (c == '-') {		/* -<n> = decrement by <n> pages */
	    ipage -= atoi(ibuf+1);
	} else if (isdigit(c)) {	/* <n> = goto page <n> */
	    ipage = atoi(ibuf);
	} else if (c == '\f') {
	    ;				/* FF = redraw the last plot */
	} else if (c == 'q') {		/* q = quit */
	    break;
	} else {			/* h, ? or garbage = give help */
	    tek_mode(ALPHA_MODE);
	    describe();    
	    continue;
	}

/* Bounds checking */

	if (ipage > npage) {
	    ipage = npage;
	    if (ipage == oldpage)
		continue;
	} else if (ipage < 0) {
	    ipage = 1;
	} else if (ipage == 0) {
	    continue;			/* page 0 does not exist */
	}

/* Time to actually plot something. */

	tek_mode(GRAPH_MODE);
	istop = fseek(fd, start[ipage - 1], 0);
	xtra = '\0';
	istop = 0;
	for (i = 0; i < 8196; i++) {	/* less than 8MB per page! */
	    if (xtra) {
		fwrite(&xtra, 1, 1, stdout);
		xtra = '\0';
	    }
	    nb = fread(buf, 1, BUFSZ, fd);
	    if (nb <= 0)
		break;
	    ifirst = 0;
	    for (j = 0; j < nb; j++) {
		if (buf[j] == '\f') {
		    fwrite(&buf[ifirst], 1, j - ifirst, stdout);
		    fflush(stdout);
		    istop = 1;
		    break;
		}
	    }
	    if (istop)
		break;
	    if (buf[nb] == ESC) {
		xtra = ESC;
		j--;
	    }
	    fwrite(&buf[ifirst], 1, j - ifirst, stdout);
	}
	if ( termtype == xterm ) 
	    tek_mode(ALPHA_MODE);
    }
    tek_mode(ALPHA_MODE);		/* how to kill an xterm Tek window? */
    fclose(fd);
    exit(0);
}

/*----------------------------------------------------------------------*\
 *  tek_mode()
 *
 *  switch to alpha/graph mode
\*----------------------------------------------------------------------*/

static int currentmode = ALPHA_MODE;
static void 
tek_mode (int mode)
{
    if ( mode == GRAPH_MODE ) {
	if ( currentmode == ALPHA_MODE ) {
	    switch(termtype) {
	    case tekterm:
		break;
	    case xterm:
		printf("\033[?38h");		/* open graphics window */
		break;
	    default:
		printf("\033[?38h");		/* open graphics window */
	    }
	    printf("\035");			/* Enter Vector mode = GS */
	}
	printf("\033\f");			/* clear screen = ESC FF */
	fflush(stdout);      
	currentmode = mode;
    } else if ( mode == ALPHA_MODE ) {
	switch(termtype) {
	case tekterm:
	    printf("\037\030");		/* Enter Alpha mode = US CAN */
	    break;
	case xterm:
	    printf("\033\003");		/* VT mode (xterm) = ESC ETX */
	    break;
	default:
	    printf("\033\003");		/* VT mode (xterm) = ESC ETX */
	}
	fflush(stdout);      
	currentmode = mode;
    }
}

/*----------------------------------------------------------------------*\
 *  describe()
 *
 *  Print help message.
 *  Note: if this message starts to exceed 512 bytes, may need to split
 *  since some compilers can't handle strings that long.
\*----------------------------------------------------------------------*/

static void 
describe (void)
{
    fputs("\
Usage: pltek filename \n\
At the prompt, the following replies are recognized:\n\
   h,?	  Give this help message.\n\
    q	  Quit program.\n\
   <n>	  Go to the specified page number.\n\
   -<n>   Go back <n> pages.\n\
   +<n>   Go forward <n> pages.\n\
 <Return> Go to the next page.\n\n",
	  stdout);
}