summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvi2tty/dvi2tty-5.3.4/UNIXtoMacLib/unix-to-macintosh.c
blob: f29b03342565990bae651a0762f8f2046a9ef9f5 (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
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
/* unix-to-macintosh.c  24.2.1996

 * Macintosh THINK-C file system and user interface
 * library for use by dvi2tty, disdvi, and rtf2LaTeX.
 *
 * Written and copyright (c) 1996 by Alex Viskovatoff
 * (av@mixcom.com). Permission is granted to distribute
 * and modify this file.
 */

#include <MacHeaders>
#include <string.h>
#include "macintosh.h"


static char home_pathname_buf[PATHNAME_LENGTH];

static char *make_pathname(char *q,long dir_id, int vol_num)
{
	CInfoPBRec	directory;
	Str31		dir_name;
	int			n;
	directory.dirInfo.ioDrDirID = dir_id;
	directory.dirInfo.ioVRefNum = vol_num;
	directory.dirInfo.ioFDirIndex = -1;
	directory.dirInfo.ioNamePtr = dir_name;
	for (;;directory.dirInfo.ioDrDirID = directory.dirInfo.ioDrParID) {
		*q-- = ':';
		PBGetCatInfoSync(&directory);
		for (n=*dir_name; n; n--) *q-- = (char) dir_name[n];
		if (directory.dirInfo.ioDrDirID == 2) break;
	}
	return q+1;
}	

char *get_home_dir()
{
	FCBPBRec	home_dir;
	Str31		dir_name;
	Str31		apName;
	int			pathRefNum;
	Handle		apParam;
	char 	   *p = home_pathname_buf+PATHNAME_LENGTH-1;
	GetAppParms(&apName,&pathRefNum,&apParam);
	home_dir.ioRefNum = pathRefNum;
	home_dir.ioFCBIndx = 0;
	home_dir.ioNamePtr = dir_name;
	PBGetFCBInfoSync(&home_dir);
	*p-- = 0;
	return make_pathname(p,home_dir.ioFCBParID,home_dir.ioFCBVRefNum);
}

pascal unsigned char not_rtf_file_p(void *parms)
{
	StringPtr name = ((IOParam*)parms)->ioNamePtr;
	char *p, *q;
	static char extension[] = ".rtf";
	int i;
	if (name[0] < 5) return TRUE;
	for (i=4, p = (char*) name + name[0] - 3, q=extension; i; i--)
		if (*p++ != *q++) return TRUE;
	return FALSE;
}

void set_creator(const unsigned char *outfilename)
{
	FInfo	finfo;
	Str255	filename;
	StringHandle h;
	strcpy((char*)filename,(char*)outfilename);
	CtoPstr(filename);
	if (!GetFInfo(filename,0,&finfo)) {
        finfo.fdCreator = *((OSType*)*(h=GetString(128)));
        ReleaseResource(h);
	SetFInfo(filename,0,&finfo);
	}
}



/* The rest of file is a modification of the file ccomand.c (part of
 * Think C 5.0), with the function ccommand renamed to "docommand",
 * and changed to take three more arguments: a filter procedure to use
 * when displaying files in the open input file dialog, the length of
 * a list of signatures of file types that should be displayed in that
 * dialog, and a pointer to this list. Furthermore, the file creator
 * signature for the output file, which is of type 'TEXT', is now set
 * by a call to the new function set_creator. The signature is stored
 * as a string resource, and so can be modified by means of ResEdit.
 * (In the distribution, the signature used is '*TEX', that of Textures.)
 */


/*
 *  ccommand.c
 *
 *  Copyright (c) 1991 Symantec Corporation.  All rights reserved.
 *
 *  This routine should be called from "main" as follows:
 *
 *		main(int argc, char **argv)
 *		{
 *			argc = ccommand(&argv);
 *			...
 *
 *  A dialog is presented allowing entry of command line arguments
 *  and redirection of stdin/stdout.
 *
 */

#include <MacHeaders>

#include "stdlib.h"
#include "errno.h"
#include "console.h"
#include "ansi_private.h"

#define NARGS		25

static char *argv[NARGS+1];
static char argbuf[256];

static void create_dialog(void);
static Handle ditem(int);
static void radio(int, int, int);
static pascal void drawRing(DialogPtr, short);

static Point getWhere = { 90, 82 };
static Point putWhere = { 106, 104 };

static void setfile(char *, SFReply *);

static void init_command_line(char *, char *);
static int parse(char *, char *);

enum {
	cmdLine = 3,
	inCon, inFile,
	outCon, outFile, outEcho, outPrint,
	okRing,
	labelIn, labelOut, labelLine
};

static struct {
	short			count;
	struct {
		Handle			h;
		Rect			box;
		char			kind;
	}				item[13];
} itemList = { 12,

		/*  OK  */
	0, { 176, 115, 196, 175 }, ctrlItem+btnCtrl,

		/*  Cancel  */
	0, { 176, 225, 196, 285 }, ctrlItem+btnCtrl,
	
		/*  command line  */
	0, { 141, 34, 157, 376 }, editText+itemDisable,
	
		/*  (stdin) console  */
	0, { 34, 30, 50, 180 }, ctrlItem+radCtrl,
	
		/*  (stdin) file  */
	0, { 54, 30, 70, 180 }, ctrlItem+radCtrl,
	
		/*  (stdout) console  */
	0, { 34, 230, 50, 380 }, ctrlItem+radCtrl,
	
		/*  (stdout) file  */
	0, { 54, 230, 70, 380 }, ctrlItem+radCtrl,
	
		/*  (stdout) console+file  */
	0, { 74, 230, 90, 380 }, ctrlItem+radCtrl,
	
		/*  (stdout) console+printer  */
	0, { 94, 230, 110, 380 }, ctrlItem+radCtrl,

		/*  ring around OK button  */
	(Handle) drawRing, { 172, 111, 200, 179 }, userItem+itemDisable,
	
		/*  "Standard Input:"  */
	0, { 10, 20, 26, 170 }, statText+itemDisable,
	
		/*  "Standard Output:"  */
	0, { 10, 220, 26, 370 }, statText+itemDisable,
	
		/*  "Command Line:"  */
	0, { 114, 20, 130, 170 }, statText+itemDisable,
};

static DialogPtr dp;
static Rect bounds = { 60, 51, 270, 461 };
static int inChoice = inCon, outChoice = outCon;


/*
 *  ccommand - process "command line"
 *
 */

int
docommand(char ***av, ProcPtr file_filter, int num_types, SFTypeList type_list)
{
	short i, argc;
	SFReply input, output, scratch;
	char buf[256];
	
		/*  present dialog  */
		
	cshow(stdin);
	create_dialog();
	init_command_line(buf, argbuf);
	SetCtlValue(ditem(inCon), 1);
	SetCtlValue(ditem(outCon), 1);
	ShowWindow(dp);
	
		/*  engage in dialog  */
		
	do {
		ModalDialog(0, &i);
		switch (i) {
			case cancel:
				abort();
			case inFile:
				SFGetFile(getWhere, "", file_filter, num_types, type_list, NULL, &scratch);
				if (!scratch.good)
					break;
				input = scratch;
				/* ... */
			case inCon:
				radio(inChoice = i, inCon, 2);
				break;
			case outFile:
			case outEcho:
				SFPutFile(putWhere, "", "", NULL, &scratch);
				if (!scratch.good)
					break;
				output = scratch;
				/* ... */
			case outCon:
			case outPrint:
				radio(outChoice = i, outCon, 4);
				break;
		}
	} while (i != ok);
	
		/*  parse command line  */
		
	GetIText(ditem(cmdLine), argbuf);
	sprintf(buf, "%#s", argbuf);
	argc = parse(buf, argbuf);
	*av = argv;
	DisposDialog(dp);
	
		/*  redirect stdout  */
		
	if (outChoice == outPrint)
		cecho2printer(stdout);
	else if (outChoice != outCon) {
		setfile((char *) scratch.fName, &output);
		if (outChoice == outFile)
			freopen((char *) scratch.fName, "w", stdout);
		else cecho2file((char *) scratch.fName, 0, stdout);
		set_creator(scratch.fName);              /*** Added by av ***/
	}
	
		/*  redirect stdin  */
		
	if (inChoice == inFile) {
		setfile((char *) scratch.fName, &input);
		freopen((char *) scratch.fName, "r", stdin);
	}
	
		/*  done  */
	
	errno = 0;
	return(argc);
}


/* ---------- dialog utilities ---------- */


/*
 *  create_dialog - build dialog in memory
 *
 */

static void
create_dialog(void)
{
	Handle items;
	
	asm {
		lea		itemList,a0
		move.l	#sizeof itemList,d0
		_PtrToHand
		move.l	a0,items
	}
	dp = NewDialog(0, &bounds, "", 0, 1, (Ptr) -1, 0, 0, items);
	SetCTitle(ditem(ok), "\pOK");
	SetCTitle(ditem(cancel), "\pCancel");
	SetCTitle(ditem(inCon), "\pconsole");
	SetCTitle(ditem(inFile), "\pfile");
	SetCTitle(ditem(outCon), "\pconsole");
	SetCTitle(ditem(outFile), "\pfile");
	SetCTitle(ditem(outEcho), "\pconsole+file");
	SetCTitle(ditem(outPrint), "\pconsole+printer");
	SetIText(ditem(labelIn), "\pStandard Input:");
	SetIText(ditem(labelOut), "\pStandard Output:");
	SetIText(ditem(labelLine), "\pCommand Line:");
}


/*
 *  ditem - return item handle
 *
 */

static Handle
ditem(int i)
{
	short kind;
	Handle item;
	Rect box;
	
	GetDItem(dp, i, &kind, &item, &box);
	return(item);
}


/*
 *  radio - adjust a cluster of radio buttons
 *
 */

static void
radio(int i, int j, int n)
{
	for (; n--; j++)
		SetCtlValue(ditem(j), i == j);
}


/*
 *  drawRing - (user-item proc) draw ring around OK button
 *
 */

static pascal void
drawRing(DialogPtr dp, short i)
{
	PenNormal();
	PenSize(3, 3);
	FrameRoundRect(&itemList.item[okRing-1].box, 16, 16);
	PenNormal();
}


/* ---------- file utilities ---------- */


/*
 *  setfile - prepare to open file
 *
 */

static void
setfile(char *buf, SFReply *reply)
{
	IOParam pb;
	
	pb.ioNamePtr = 0;
	pb.ioVRefNum = reply->vRefNum;
	PBSetVolSync(&pb);
	sprintf(buf, "%#s", reply->fName);
}


/* ---------- string utilities ---------- */


/*
 *  init_command_line - prepare initial command line
 *
 *  The command line is preset to show the name of the program.
 *  The program name is quoted as necessary.
 *
 */

static void
init_command_line(char *buf1, char *buf2)
{
	register char *s, *t = buf2;
	int c, space = 0, dquote = 0, squote = 0, quote = 0;
	
	sprintf(s = buf1, "%#s", CurApName);
	while (c = *s++) {
		if (c == ' ')
			space = 1;
		else if (c == '"')
			dquote = 1;
		else if (c == '\'')
			squote = 1;
	}
	if (space || dquote || squote)
		*t++ = quote = dquote && !squote ? '\'' : '"';
	for (s = buf1; c = *s++; *t++ = c) {
		if (c == quote || c == '\\')
			*t++ = '\\';
	}
	if (quote)
		*t++ = quote;
	*t++ = ' ';
	*t++ = 0;
	SetIText(ditem(cmdLine), __c2p(buf2, buf1));
	SelIText(dp, cmdLine, 9999, 9999);
}


/*
 *  parse - divide command line into "words"
 *
 *  Words are delimited by one or more spaces.  Any characters within
 *  matching single (') or double (") quotes are taken literally.  Any
 *  character preceded by a backslash (\) is taken literally.
 *
 */

static int
parse(char *s, char *t)
{
	int c, quote = 0, argc = 0;
		
	while (c = *s++) {
		if (c == ' ')
			continue;
		if (argc < NARGS)
			argv[argc++] = t;
		do {
			if (c == '\\' && *s)
				c = *s++;
			else if (c == '"' || c == '\'') {
				if (!quote) {
					quote = c;
					continue;
				}
				if (c == quote) {
					quote = 0;
					continue;
				}
			}
			*t++ = c;
		} while (*s && ((c = *s++) != ' ' || quote));
		*t++ = 0;
	}
	return(argc);
}