-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpft.c
284 lines (255 loc) · 7.01 KB
/
pft.c
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
/*
pft.c
A program to communicate with the PostScript server.
trey
September 18,1986
Copyright 1986, NeXT, Inc.
Modified:
23Sep86 Leo Added command line option for port number
14Dec86 Trey updated to reflect psprim changes in streams
14Apr87 Trey totally changed to use psprims procs
05May87 Leo Added command line option for host
*/
#include <stdlib.h>
#include <dpsclient/dpsclient.h>
#include <objc/error.h>
#include <string.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
extern int read();
static char *Host = NULL; /* the host to hookup to */
static char *Name = 0; /* the server name to hookup to */
static char *File = NULL; /* a file to run on startup */
static int justSource = FALSE; /* source a file and leave? */
static int loadMacros = TRUE; /* load special = macros? */
DPSContext Ctxt;
static void stdinProc();
static int eventProc();
static void asciiProc();
static void usage();
static void gobbleArgs();
static void parseArgs();
void main(argc, argv)
int argc;
char *argv[];
{
NXEvent e;
parseArgs( &argc, argv );
NX_DURING
Ctxt = DPSCreateContext(Host, Name, asciiProc, NULL);
printf("Connection to PostScript established.\n");
if( loadMacros ) {
DPSPrintf(Ctxt, "/= [ /= load /exec cvx /flush cvx ] cvx def\n");
DPSPrintf(Ctxt, "/== [ /== load /exec cvx /flush cvx ] cvx def\n");
DPSPrintf(Ctxt,
"/pstack [ /pstack load /exec cvx /flush cvx ] cvx def\n");
}
/* source a file for them if they wish */
if( File ) {
printf("running file %s\n", File );
DPSPrintf( Ctxt, "(%s) run\n", File );
}
if( justSource ) {
DPSWaitContext(Ctxt);
DPSDestroyContext(Ctxt);
exit(0);
}
DPSAddFD( 0, stdinProc, NULL, 1 );
DPSSetEventFunc( Ctxt, eventProc );
NX_HANDLER
switch( NXLocalHandler.code ) {
case dps_err_ps:
fprintf( stderr, "pft: PostScript error while connecting:\n" );
DPSPrintError( stdout, NXLocalHandler.data2 );
break;
case dps_err_cantConnect:
fprintf( stderr, "pft: Could not form connection.\n" );
break;
default:
fprintf( stderr, "pft: Fatal error #%d while connecting.\n",
NXLocalHandler.code );
}
exit(-1);
NX_ENDHANDLER
restartLoop:
NX_DURING
DPSGetEvent( DPS_ALLCONTEXTS, &e, -1, NX_FOREVER, 0 );
fprintf(stderr, "pft: DPSGetEvent returned unexpectedly.\n");
exit(-1);
NX_HANDLER
switch( NXLocalHandler.code ) {
case dps_err_ps:
DPSPrintError( stderr, NXLocalHandler.data2 );
goto restartLoop;
case dps_err_resultTagCheck:
case dps_err_resultTypeCheck:
fprintf( stderr, "Binary Object returned\n" );
goto restartLoop;
case dps_err_write:
case dps_err_connectionClosed:
fprintf( stderr, "pft: Connection to Window Server closed\n" );
exit(-1);
default:
fprintf( stderr, "pft: Fatal exception raised #%d\n",
NXLocalHandler.code );
exit(-1);
}
NX_ENDHANDLER
}
/* a little proc to handle activity on stdin */
void stdinProc( fd, data )
int fd;
char *data;
{
char buf[256];
int num_chars;
if ((num_chars = read(0, buf, 255)) > 0) {
DPSWriteData(Ctxt, buf, num_chars);
DPSFlushContext(Ctxt);
} else {
DPSDestroyContext(Ctxt);
exit(0);
}
}
static void
asciiProc( ctxt, buf, count )
DPSContext ctxt;
char *buf;
long unsigned int count;
{
fwrite( buf, sizeof(char), count, stdout );
}
/* little procs to handle events, errors, and tokens */
int eventProc( e )
NXEvent *e;
{
FILE *fp = stdout;
static char *eNames[NX_LASTEVENT+1] = {
"NullEvent",
"LMouseDown",
"LMouseUp",
"RMouseDown",
"RMouseUp",
"MouseMoved",
"LMouseDragged",
"RMouseDragged",
"MouseEntered",
"MouseExited",
"KeyDown",
"KeyUp",
"FlagsChanged",
"WinChanged",
"SysDefined",
"AppDefined",
"Timer",
"CursorUpdate"
};
if( e->type < sizeof(eNames)/sizeof(char*) )
fprintf( fp, "%s", eNames[e->type] );
else
fprintf( fp, "Unknown" );
fprintf( fp, " at: %.1f,%.1f", e->location.x, e->location.y );
fprintf( fp, " time: %d", e->time );
fprintf( fp, " flags: %#x", e->flags );
fprintf( fp, " win: %hu", e->window );
fprintf( fp, " cxn: %x", e->ctxt );
switch(e->type) {
case NX_LMOUSEDOWN:
case NX_LMOUSEUP:
case NX_RMOUSEDOWN:
case NX_RMOUSEUP:
fprintf( fp, " data: %hd,%d\n", e->data.mouse.eventNum,
e->data.mouse.click);
break;
case NX_KEYDOWN:
case NX_KEYUP:
fprintf( fp, " data: %hd,%hu,%hu,%hu,%hd\n", e->data.key.repeat,
e->data.key.charSet, e->data.key.charCode,
e->data.key.keyCode, e->data.key.keyData );
break;
case NX_MOUSEENTERED:
case NX_MOUSEEXITED:
fprintf( fp, " data: %hd,%d,%x\n", e->data.tracking.eventNum,
e->data.tracking.trackingNum,
e->data.tracking.userData);
break;
case NX_MOUSEMOVEDMASK: /* these dont use the data union */
case NX_LMOUSEDRAGGEDMASK:
case NX_RMOUSEDRAGGEDMASK:
case NX_FLAGSCHANGEDMASK:
break;
default:
fprintf( fp, " data: %hd,%x,%x\n", e->data.compound.subtype,
e->data.compound.misc.L[0],
e->data.compound.misc.L[1] );
break;
}
return 0;
}
/* routine to parse all the command line args */
static void
parseArgs( argc, argv )
int *argc;
char **argv;
{
argv++; /* skip the command itself */
while( *argv ) /* while there are more args */
if( **argv == '-' ) { /* if its a flag (starts with a '-') */
if( !strcmp( *argv, "-f" )) {
File = *(argv+1);
gobbleArgs( 2, argc, argv );
} else if( !strcmp( *argv, "-NXHost" ) ||
!strcmp( *argv, "-Host" ) ||
!strcmp( *argv, "-host" )) {
Host = *(argv+1);
gobbleArgs( 2, argc, argv );
} else if( !strcmp( *argv, "-r" )) {
loadMacros = FALSE;
gobbleArgs( 1, argc, argv );
} else if( !strcmp( *argv, "-NXPSName" )) {
Name = *(argv+1);
gobbleArgs( 2, argc, argv );
} else if( !strcmp( *argv, "-s" )) {
gobbleArgs( 1, argc, argv );
justSource = TRUE;
} else /* else its one of our flags */
usage();
} else /* else its not flag at all */
usage();
}
/* a little accessory routine to the command line arg parsing that
removes some args from the argv vector.
*/
static void
gobbleArgs( numToEat, argc, argv )
register int numToEat;
int *argc;
register char **argv;
{
register char **copySrc;
/* copy the other args down, overwriting those we wish to gobble */
for( copySrc = argv+numToEat; *copySrc; *argv++ = *copySrc++ );
*argv = NULL; /* leave the list NULL terminated */
*argc -= numToEat;
}
/* print out some help and bail out */
static void
usage()
{
fprintf( stderr, "Usage: pft [optional args...]\n");
fprintf( stderr,
" -NXHost sets the machine where the PS server is located\n");
fprintf( stderr,
" -f <file> sources a file of PostScript commands\n");
fprintf( stderr,
" -s means to source the file and exit\n");
fprintf( stderr,
" -NXPSName sets the name to lookup for the PS server\n");
fprintf( stderr,
" -r suppresses loading new definitions for =, == and pstack\n");
exit(-1);
}