summaryrefslogtreecommitdiff
path: root/dviware/dviimp/diext.c
blob: 042c46975f1d785f283fd705d49a4b4ced3e6881 (plain)
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
/*
	External procedures for use with dviimp.
	Written by H. Trickey, 6/25/85.
	
	Implement the following procedures (as declared in Pascal)

	function gfbyte: integer;
	function gftwobytes: integer;
	function gfthreebytes: integer;
	function gfsignedquad: integer;
	function getbyte: integer;
	function signedbyte: integer;
	function gettwobytes: integer;
	function signedpair: integer;
	function getthreebytes: integer;
	function signedtrio: integer;
	function signedquad: integer;
	function curpos(var f:bytefile):integer;
	procedure imbyte(w:integer);
	procedure imhalfword(w:integer);
	procedure setpos(var f:bytefile;n:integer);
	procedure setpaths;
	function testaccess(accessmode:integer; filepath:integer):boolean;
	procedure setusername;
*/

#include <stdio.h>
#include <strings.h>
#include <pwd.h>
#include "h00vars.h" /* pc runtime structures */
#include "texpaths.h"

extern struct iorec dvifile,gffile,imfile; /* pc file structs */
extern int eofdvifile,eofgffile; /* need to be set true when these happens */
extern int curloc; /* needs to be incremented when a dvi byte is read */
extern int curgfloc; /* needs to be incremented when a gf byte is read */
extern int imbyteno; /* needs to be incremented when an im byte is written */

/* return unsigned version of next byte in gffile */
int gfbyte()
{
	register int c;
	
	if ((c = getc(gffile.fbuf))==EOF) {
		eofgffile = -1;
		return(0);
		}
	curgfloc++;
	return(c);
}

/* return unsigned version of next two bytes in gffile */
int gftwobytes()
{
	register int a,b;
	register FILE *iop = gffile.fbuf;
	a = getc(iop);
	eofgffile = ((b = getc(iop))==EOF) ? -1 : 0;
	curgfloc += 2;
	return((a << 8) | b);
}

/* return unsigned version of next three bytes in gffile */
int gfthreebytes()
{
	register int a,b,c;
	register FILE *iop = gffile.fbuf;
	a = getc(iop);
	b = getc(iop);
	eofgffile = ((c = getc(iop))==EOF) ? -1 : 0;
	curgfloc +=3;
	return((a << 16) | (b << 8) | c);
}

/* return signed version of next four bytes in gffile */
int gfsignedquad()
{
	register int a,b,c,d;
	register FILE *iop = gffile.fbuf;
	a = getc(iop);
	b = getc(iop);
	c = getc(iop);
	eofgffile = ((d = getc(iop))==EOF) ? -1 : 0;
	curgfloc += 4;
	return((a << 24) | (b << 16) | (c << 8) | d);
}

/* return unsigned version of next byte in dvifile */
int getbyte()
{
	register int c;
	
	if ((c = getc(dvifile.fbuf))==EOF) {
		eofdvifile = -1;
		return(0);
		}
	curloc++;
	return(c);
}

/* return signed version of next byte in dvifile */
int signedbyte()
{	register int c;
	if ((c = getc(dvifile.fbuf))==EOF) {
		eofdvifile = -1;
		return(0);
		}
	curloc++;
	if (c>127) return(c-256);
	return(c);
}

/* return unsigned value of next two bytes (high order first) */
int gettwobytes()
{
	register int a,b;
	register FILE *iop = dvifile.fbuf;
	a = getc(iop);
	eofdvifile = ((b = getc(iop))==EOF) ? -1 : 0;
	curloc += 2;
	return((a << 8) | b);
}

/* return signed value of next two bytes (high order first) */
int signedpair()
{
	register int a,b;
	register FILE *iop = dvifile.fbuf;
	if ( (a = getc(iop)) > 127) a -= 256; /* sign extend */
	eofdvifile = ((b = getc(iop))==EOF) ? -1 : 0;
	curloc += 2;
	return((a << 8) | b);
}

/* return unsigned value of next three bytes */
int getthreebytes()
{
	register int a,b,c;
	register FILE *iop = dvifile.fbuf;
	a = getc(iop);
	b = getc(iop);
	eofdvifile = ((c = getc(iop))==EOF) ? -1 : 0;
	curloc +=3;
	return((a << 16) | (b << 8) | c);
}

/* return signed value of next three bytes */
int signedtrio()
{
	register int a,b,c;
	register FILE *iop = dvifile.fbuf;
	if ( (a = getc(iop)) > 127) a -= 256;
	b = getc(iop);
	eofdvifile = ((c = getc(iop))==EOF) ? -1 : 0;
	curloc +=3;
	return((a << 16) | (b << 8) | c);
}

/* return value of next four bytes */
int signedquad()
{
	register int a,b,c,d;
	register FILE *iop = dvifile.fbuf;
	a = getc(iop);
	b = getc(iop);
	c = getc(iop);
	eofdvifile = ((d = getc(iop))==EOF) ? -1 : 0;
	curloc += 4;
	return((a << 24) | (b << 16) | (c << 8) | d);
}

/* put the low order byte of w on imfile */
imbyte(w)
	int w;
{
	putc(w, imfile.fbuf);
	imbyteno++;
}

/* put the two low order bytes of w on imfile, high order first */
imhalfword(w)
	int w;
{
	putc(w >> 8, imfile.fbuf);
	putc(w, imfile.fbuf);
	imbyteno += 2;
}

/* seek to byte n of file f; maintain eof's if f is dvifile or gffile */
setpos(f,n)
	register struct iorec *f;
	int n;
{
	if (n>=0) {
	    fseek(f->fbuf,(long) n,0); 
	    if (f==&dvifile) eofdvifile = 0;
	    else if (f==&gffile) eofgffile =0;
	    }
	else {
	    fseek(f->fbuf, (long) n, 2);
	    if (f==&dvifile) eofdvifile = -1;
	    else if (f==&gffile) eofgffile = -1;
	    }
}

/* return current byte offset in file f */
int curpos(f)
	struct iorec *f;
{
	return((int) ftell(f->fbuf));
}

char *fontpath;

char *getenv();

/*
 * setpaths is called to set up the pointer fontpath
 * as follows:  if the user's environment has a value for TEXFONTS
 * then use it;  otherwise, use defaultfontpath.
 */
setpaths()
{
	register char *envpath;
	
	if ((envpath = getenv("TEXFONTS")) != NULL)
	    fontpath = envpath;
	else
	    fontpath = defaultfontpath;
}

#define namelength 100   /* should agree with dvitype.ch */
extern char curname[],realnameoffile[]; /* these have size namelength */

/*
 *	testaccess(amode,filepath)
 *
 *  Test whether or not the file whose name is in the global curname
 *  can be opened for reading (if mode=READACCESS)
 *  or writing (if mode=WRITEACCESS).
 *
 *  The filepath argument is one of the ...FILEPATH constants defined below.
 *  If the filename given in curname does not begin with '/', we try 
 *  prepending all the ':'-separated areanames in the appropriate path to the
 *  filename until access can be made, if it ever can.
 *
 *  The realnameoffile global array will contain the name that yielded an
 *  access success.
 */

#define READACCESS 4
#define WRITEACCESS 2

#define NOFILEPATH 0
#define FONTFILEPATH 3

bool
testaccess(amode,filepath)
    int amode,filepath;
{
    register bool ok;
    register char *p;
    char *curpathplace;
    int f;
    
    switch(filepath) {
	case NOFILEPATH: curpathplace = NULL; break;
	case FONTFILEPATH: curpathplace = fontpath; break;
	}
    if (curname[0]=='/')	/* file name has absolute path */
	curpathplace = NULL;
    do {
	packrealnameoffile(&curpathplace);
	if (amode==READACCESS)
	    /* use system call "access" to see if we could read it */
	    if (access(realnameoffile,READACCESS)==0) ok = TRUE;
	    else ok = FALSE;
	else {
	    /* WRITEACCESS: use creat to see if we could create it, but close
	    the file again if we're OK, to let pc open it for real */
	    f = creat(realnameoffile,0666);
	    if (f>=0) ok = TRUE;
	    else ok = FALSE;
	    if (ok)
		close(f);
	    }
    } while (!ok && curpathplace != NULL);
    if (ok) {  /* pad realnameoffile with blanks, as Pascal wants */
	for (p = realnameoffile; *p != '\0'; p++)
	    /* nothing: find end of string */ ;
	while (p < &(realnameoffile[namelength]))
	    *p++ = ' ';
	}
    return (ok ? -1 : 0);
}

/*
 * packrealnameoffile(cpp) makes realnameoffile contain the directory at *cpp,
 * followed by '/', followed by the characters in curname up until the
 * first blank there, and finally a '\0'.  The cpp pointer is left pointing
 * at the next directory in the path.
 * But: if *cpp == NULL, then we are supposed to use curname as is.
 */
packrealnameoffile(cpp)
    char **cpp;
{
    register char *p,*realname;
    
    realname = realnameoffile;
    if ((p = *cpp)!=NULL) {
	while ((*p != ':') && (*p != '\0')) {
	    *realname++ = *p++;
	    if (realname == &(realnameoffile[namelength-1]))
		break;
	    }
	if (*p == '\0') *cpp = NULL; /* at end of path now */
	else *cpp = p+1; /* else get past ':' */
	*realname++ = '/';  /* separate the area from the name to follow */
	}
    /* now append curname to realname... */
    p = curname;
    while (*p != ' ') {
	if (realname >= &(realnameoffile[namelength-1])) {
	    fprintf(stderr,"! Full file name is too long\n");
	    break;
	    }
	*realname++ = *p++;
	}
    *realname = '\0';
}

extern struct passwd * getpwuid();
extern char username[];
extern int lenusername;
/*
 * find out the caller's name, putting it into username[1..namelength]
 * and put the length used into lenusername
 */
setusername()
{
	int i;
	char *nam;
	
	nam = getpwuid(getuid())->pw_name;
	for (i=0; *nam!=0 && *nam!='(' && i<namelength; )
		username[i++] = *nam++;
	lenusername = i;
}

extern char hostname[];
extern int lenhostname;
sethostnam()
{
	int i;
	char nam[namelength];
	
	lenhostname = 0;
	if (gethostname(nam,255) != 0) return;
	for (i=0; nam[i]!=0 && nam[i]!='.' && i<namelength; ) {
		hostname[i] = nam[i];
		i++;
		}
	lenhostname = i;
}