File: pltek.c

package info (click to toggle)
plplot 5.14.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 30,424 kB
  • sloc: ansic: 79,613; xml: 28,583; cpp: 20,037; ada: 19,456; tcl: 12,081; f90: 11,423; ml: 7,276; java: 6,863; python: 6,792; sh: 3,185; perl: 828; lisp: 75; makefile: 48; sed: 33; fortran: 5
file content (280 lines) | stat: -rw-r--r-- 7,326 bytes parent folder | download | duplicates (4)
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
//  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, npage, ipage, ifirst, oldpage;
    size_t j, nb;
    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 = (int) 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 )
            {
                if ( fwrite( &xtra, 1, 1, stdout ) != 1 )
                {
                    fprintf( stderr, "Error writing to stdout\n" );
                    exit( 1 );
                }
                xtra = '\0';
            }
            nb = fread( buf, 1, BUFSZ, fd );
            if ( nb == 0 )
                break;
            for ( j = 0; j < nb; j++ )
            {
                if ( buf[j] == '\f' )
                {
                    if ( fwrite( &buf[0], 1, j, stdout ) != j )
                    {
                        fprintf( stderr, "Error writing to stdout\n" );
                        exit( 1 );
                    }
                    fflush( stdout );
                    istop = 1;
                    break;
                }
            }
            if ( istop )
                break;
            if ( buf[nb] == ESC )
            {
                xtra = ESC;
                j--;
            }
            if ( fwrite( &buf[0], 1, j, stdout ) != j )
            {
                fprintf( stderr, "Error writing to stdout\n" );
                exit( 1 );
            }
        }
        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 );
}