File: xa60.c

package info (click to toggle)
nase-a60 0.20a-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,580 kB
  • ctags: 1,111
  • sloc: ansic: 10,343; yacc: 965; sh: 360; lex: 336; makefile: 249
file content (489 lines) | stat: -rw-r--r-- 9,158 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * Simple X11 edit-and-go frontend for the NASE A60 interpreter.
 * Copyright (C) 1991-1996 Erik Schoenfelder (schoenfr@ibr.cs.tu-bs.de)
 *
 * This file is part of NASE A60.
 * 
 * NASE A60 is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * NASE A60 is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NASE A60; see the file COPYING.  If not, write to the Free
 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * xa60.c:						may 1991
 *
 * first steps to a simple X frontend for edit and go fun.
 * (nothing serious - be warned)
 */

#include <stdio.h>

#ifdef USG
#ifndef SYSV
/* commonly expected by X11R4: */
#define SYSV
#endif /* ! SYSV */
#endif /* USG */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Box.h>

#define VERSION		"xa60  v0.12,  March 1996"

#define A60PATH		"a60"
#define A60FLAGS	"-X"

/*
 * define as 1 to include a save button; this is not necessary,
 * because the text is saved before every execution. (and upon exit) 
 */
#define SAVE_BUTTEN	0


static String fallback_resources [] = { 
    "*input: 			True",
    "*showGrip: 		on",
    "*paned.width:		560",

    "*etext*height:		360", 
    "*etext*editType: 		edit",
    "*etext*scrollVertical: 	whenNeeded",
    "*etext*autoFill: 		on",

    "*otext*height:		240", 
    "*otext*editType:		append",
    "*otext*scrollVertical: 	whenNeeded",
    "*otext*wrap:		line",
    "*otext*displayCaret:	False",

    "*bexit*label: 		Exit",
    "*bexit*shapeStyle:		oval",
    "*bexit*borderWidth:	2",

    "*clear*label: 		Clear",
    "*clear*shapeStyle:		oval",
#if SAVE_BUTTEN
    "*save*label: 		Save",
    "*save*shapeStyle:		oval",
#endif
    "*go*label: 		Go",
    "*go*shapeStyle:		oval",

    "*fnlab*borderWidth:	0",

    NULL,
};


/* selected input file: */
static char *fname = (char *) 0;

/* temporary textbuffer (and its length): */
static char *txt_buf = (char *) 0;
static int txt_max = 0;
static int txt_len;

/* global ... */
static Widget etext, otext;

/* forward: */
static void have_a_run ();


/*
 * create a temporary file; a simple name is enough.
 * static storage is ok.
 */

static char *
mk_fname ()
{
	static char tmp [128];

	sprintf (tmp, "/tmp/nase-%d", getpid ());

	return tmp;
}


static void
read_file ()
{
	FILE *fp;
	int ch;
	
	fp = fopen (fname, "r");

	if (! fp) {
		fp = fopen (fname, "w");
		if (! fp) {
			fprintf (stderr, "cannot open `%s' for writing...\n",
				 fname);
			exit (1);
		}

		/*
		 * insert some default text:
		 */
	
		fprintf (fp, "begin\n    vprint (\"Hi!\")\nend\n");
		fclose (fp);

		fp = fopen (fname, "r");
	}

	if (! fp) {
		fprintf (stderr, "cannot open `%s' for reading...\n",
			 fname);
		exit (1);
	}

	txt_len = 0;

	do {
		if (txt_len + 10 > txt_max) {
			txt_max += 1000;
			if (! txt_buf)
				txt_buf = XtMalloc (txt_max);
			else
				txt_buf = XtRealloc (txt_buf, txt_max);
		}
		
		ch = fgetc (fp);

		if (ch != EOF)
			txt_buf [txt_len++] = ch;
	} while (ch != EOF);

	txt_buf [txt_len] = 0;

	fclose (fp);
}


static void
append_text (w, str)
Widget w;
char *str;
{
	XawTextBlock tb;
	int rc, point;
	
	tb.firstPos = 0;
	tb.length = strlen (str);
	tb.ptr = str;
	tb.format = FMT8BIT;
	
	point = XawTextGetInsertionPoint (w);

	rc = XawTextReplace (w, point, point, &tb);
	
	XawTextSetInsertionPoint (w, point + strlen (str));

	if (rc == XawPositionError)
		printf ("** xa60 internal error: XawPositionError ...\n");
	else if (rc == XawEditError)
		printf ("** xa60 internal error: XawEditError ...\n");
}


static void
set_caret (lno)
int lno;
{
	Arg args[1];
	String str;
	int i, n;

	XtSetArg(args[0], XtNstring, &str);
	XtGetValues(etext, args, ONE);


	/* look for character position of line lno : */

	for (n = 0, i = 1; str [n] && i < lno; n++)
		i += str [n] == '\n';

	XawTextSetInsertionPoint (etext, n);
}

/*
 * saving the text means: print the string (which contains the
 * complete text).
 * If this fails - abort.
 */

static void
save_file (txt)
char *txt;
{
	FILE *fp;

	fp = fopen (fname, "w");
	if (! fp) {
		fprintf (stderr, "cannot open `%s' for writing...\n",
			 fname);
		exit (1);
	}

	fprintf (fp, "%s\n", txt);
	fclose (fp);
}


/*
 * The callbacks for the Three buttons:  Exit, Clear, Save and Go:
 */

/* ARGSUSED */
static void
cb_clear (w, text_ptr, call_data)
Widget w;
XtPointer text_ptr, call_data;
{
	Widget text = (Widget) text_ptr;
	Arg args[1];
	
	XtSetArg (args[0], XtNstring, "");
	XtSetValues (text, args, ONE);
}


/* ARGSUSED */
static void
cb_save (w, text_ptr, call_data)
Widget w;
XtPointer text_ptr, call_data;
{
	Widget text = (Widget) text_ptr;
	Arg args[1];
	String str;

	XtSetArg(args[0], XtNstring, &str);
	XtGetValues(text, args, ONE);

	save_file (str);
}


/* ARGSUSED */
static void
cb_bexit (w, text_ptr, call_data)
Widget w;
XtPointer text_ptr, call_data;
{
	cb_save (w, text_ptr, call_data);
	
	exit (0);
}


/* ARGSUSED */
static void
cb_go (w, text_ptr, call_data)
Widget w;
XtPointer text_ptr, call_data;
{
	cb_save (w, text_ptr, call_data);

	have_a_run ();
}


/*
 * Try to get a linenumber from this line: error lines are looking like:
 * nase.a60: 12: parse error
 * return the linenumber or return a 0.
 */

static int
is_error_line (s)
char *s;
{
	int lno = 0;

	int len = strlen (fname);

	if (! strncmp (fname, s, len) && s [len] == ':')
		lno = atoi (s + len + 1);

	return lno;
}


/*
 * read the lines from fp look about an error message (to set the
 * caret [cursor]) and append the line to the output.
 */

static void
process_a60 (fp)
FILE *fp;
{
	char buf [1024];			/* fixed length :-( */
	int cursor_set = 0, lno;

	while (buf == fgets (buf, 1024, fp)) {
		append_text (otext, buf);

		if (! cursor_set) {
			lno = is_error_line (buf);
			if (lno) {
				set_caret (lno);
				cursor_set = 1;
			}
		}
	}
}


/*
 * process the buffer through a60. a single directional pipe is enough
 * and i'll use popen.
 * (may be a fork providing a stdin from this process would be nice)
 */

static void
have_a_run ()
{
	FILE *fp;
	char cmd [100];
	char tmp [100];

	sprintf (cmd, "%s %s %s", A60PATH, A60FLAGS, fname);
	fp = popen (cmd, "r");

	if (! fp) {
		sprintf (tmp, "cannot execute `%s'.\n", cmd);
		append_text (otext, tmp);
	}
	else {
		sprintf (tmp, "*** a60 starting:\n");
		append_text (otext, tmp);
		process_a60 (fp);
		pclose (fp);
		append_text (otext, "*** a60 done.\n\n");
	}
}



static void 
usage ()
{
	fprintf (stderr, "Use:  xa60  [ -V ] | [ <source file> ]\n");
	
	exit (1);
}


/*
 * M A I N
 */

int
main (argc, argv)
int argc;
char *argv[];
{
	XtAppContext app_con;
	Widget toplevel, paned, box, clear, bexit, go;
	Arg args[1];
	char *fn_str;
#if SAVE_BUTTEN
	Widget save;
#endif

	toplevel = XtAppInitialize (&app_con, "XA60", NULL, ZERO,
			   &argc, argv, fallback_resources, NULL, ZERO);

	/*
	 * Check to see that all arguments were processed, and if not then
	 * report an error and exit.
	 */
	
	if (argc > 2)
		usage ();

 	if (argc == 2) 
	{
		if (! strcmp (argv [1], "-V")) 
		{
			printf ("Version:  %s.\n", VERSION);
			exit (0);
		}
		else if (! strcmp (argv [1], "-h")) 
		{
			usage ();
		}
		else {
			fname = argv [1];
		}
	}
	else {
		fname = mk_fname ();
	}

	fn_str = XtMalloc (strlen (fname) + 20);
	sprintf (fn_str, " File: %s", fname);

	read_file ();
	
	paned = XtCreateManagedWidget ("paned", panedWidgetClass, toplevel, 
				       NULL, ZERO);
	
	box = XtCreateManagedWidget ("box", boxWidgetClass, paned, 
				     NULL, ZERO);
	
	bexit = XtCreateManagedWidget ("bexit", commandWidgetClass, box, 
				       NULL, ZERO);
	
	clear = XtCreateManagedWidget ("clear", commandWidgetClass, box, 
				       NULL, ZERO);
#if SAVE_BUTTEN
	save = XtCreateManagedWidget ("save", commandWidgetClass, box, 
				      NULL, ZERO);
#endif
	go = XtCreateManagedWidget ("go", commandWidgetClass, box, 
				    NULL, ZERO);

	XtSetArg (args[0], XtNlabel, fn_str);
	XtCreateManagedWidget ("fnlab", labelWidgetClass, box, args, ONE);

	XtSetArg (args[0], XtNstring, txt_buf);
	etext = XtCreateManagedWidget ("etext", asciiTextWidgetClass, paned, 
				     args, ONE);

	XtSetArg (args[0], XtNstring, "See:\n");
	otext = XtCreateManagedWidget ("otext", asciiTextWidgetClass, paned, 
				      args, ONE);

	XtAddCallback (bexit, XtNcallback, cb_bexit, (XtPointer) etext);
	XtAddCallback (clear, XtNcallback, cb_clear, (XtPointer) etext);
#if SAVE_BUTTEN
	XtAddCallback (save, XtNcallback, cb_save, (XtPointer) etext);
#endif
	XtAddCallback (go, XtNcallback, cb_go, (XtPointer) etext);

	XtRealizeWidget (toplevel);
	XtAppMainLoop (app_con);

	/* not reached */
	return 0;
}

/* end of xa60.c */