2 * Simple X11 edit-and-go frontend for the NASE A60 interpreter.
3 * Copyright (C) 1991,1992 Erik Schoenfelder (schoenfr@ibr.cs.tu-bs.de)
5 * This file is part of NASE A60.
7 * NASE A60 is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * NASE A60 is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with NASE A60; see the file COPYING. If not, write to the Free
19 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 * first steps to a simple X frontend for edit and go fun.
24 * (nothing serious - be warned)
31 /* commonly expected by X11R4: */
36 #include <X11/Intrinsic.h>
37 #include <X11/StringDefs.h>
38 #include <X11/Xaw/Cardinals.h>
39 #include <X11/Xaw/AsciiText.h>
40 #include <X11/Xaw/Command.h>
41 #include <X11/Xaw/Paned.h>
42 #include <X11/Xaw/Box.h>
44 #define VERSION "xa60 v0.11, June 1991"
50 * define as 1 to include a save button; this is not necessary,
51 * because the text is saved before every execution. (and upon exit)
56 static String fallback_resources [] = {
62 "*etext*editType: edit",
63 "*etext*scrollVertical: whenNeeded",
64 "*etext*autoFill: on",
67 "*otext*editType: append",
68 "*otext*scrollVertical: whenNeeded",
70 "*otext*displayCaret: False",
73 "*bexit*shapeStyle: oval",
74 "*bexit*borderWidth: 2",
76 "*clear*label: Clear",
77 "*clear*shapeStyle: oval",
80 "*save*shapeStyle: oval",
83 "*go*shapeStyle: oval",
85 "*fnlab*borderWidth: 0",
91 /* selected input file: */
92 static char *fname = (char *) 0;
94 /* temporary textbuffer (and its length): */
95 static char *txt_buf = (char *) 0;
96 static int txt_max = 0;
100 static Widget etext, otext;
103 static void have_a_run ();
107 * create a temporary file; a simple name is enough.
108 * static storage is ok.
114 static char tmp [128];
116 sprintf (tmp, "/tmp/nase-%d", getpid () % 999);
128 fp = fopen (fname, "r");
131 fp = fopen (fname, "w");
133 fprintf (stderr, "cannot open `%s' for writing...\n",
139 * insert some default text:
142 fprintf (fp, "begin\n vprint (\"Hi!\")\nend\n");
145 fp = fopen (fname, "r");
149 fprintf (stderr, "cannot open `%s' for reading...\n",
157 if (txt_len + 10 > txt_max) {
160 txt_buf = XtMalloc (txt_max);
162 txt_buf = XtRealloc (txt_buf, txt_max);
168 txt_buf [txt_len++] = ch;
184 tb.length = strlen (str);
188 point = XawTextGetInsertionPoint (w);
190 rc = XawTextReplace (w, point, point, &tb);
192 XawTextSetInsertionPoint (w, point + strlen (str));
194 if (rc == XawPositionError)
195 printf ("** xa60 internal error: XawPositionError ...\n");
196 else if (rc == XawEditError)
197 printf ("** xa60 internal error: XawEditError ...\n");
209 XtSetArg(args[0], XtNstring, &str);
210 XtGetValues(etext, args, ONE);
213 /* look for character position of line lno : */
215 for (n = 0, i = 1; str [n] && i < lno; n++)
216 i += str [n] == '\n';
218 XawTextSetInsertionPoint (etext, n);
222 * saving the text means: print the string (which contains the
224 * If this fails - abort.
233 fp = fopen (fname, "w");
235 fprintf (stderr, "cannot open `%s' for writing...\n",
240 fprintf (fp, "%s\n", txt);
246 * The callbacks for the Three buttons: Exit, Clear, Save and Go:
251 cb_clear (w, text_ptr, call_data)
253 XtPointer text_ptr, call_data;
255 Widget text = (Widget) text_ptr;
258 XtSetArg (args[0], XtNstring, "");
259 XtSetValues (text, args, ONE);
265 cb_save (w, text_ptr, call_data)
267 XtPointer text_ptr, call_data;
269 Widget text = (Widget) text_ptr;
273 XtSetArg(args[0], XtNstring, &str);
274 XtGetValues(text, args, ONE);
282 cb_bexit (w, text_ptr, call_data)
284 XtPointer text_ptr, call_data;
286 cb_save (w, text_ptr, call_data);
294 cb_go (w, text_ptr, call_data)
296 XtPointer text_ptr, call_data;
298 cb_save (w, text_ptr, call_data);
305 * Try to get a linenumber from this line: error lines are looking like:
306 * nase.a60: 12: parse error
307 * return the linenumber or return a 0.
316 int len = strlen (fname);
318 if (! strncmp (fname, s, len) && s [len] == ':')
319 lno = atoi (s + len + 1);
326 * read the lines from fp look about an error message (to set the
327 * caret [cursor]) and append the line to the output.
334 char buf [1024]; /* fixed length :-( */
335 int cursor_set = 0, lno;
337 while (buf == fgets (buf, 1024, fp)) {
338 append_text (otext, buf);
341 lno = is_error_line (buf);
352 * process the buffer through a60. a single directional pipe is enough
353 * and i'll use popen.
354 * (may be a fork providing a stdin from this process would be nice)
364 sprintf (cmd, "%s %s %s", A60PATH, A60FLAGS, fname);
365 fp = popen (cmd, "r");
368 sprintf (tmp, "cannot execute `%s'.\n", cmd);
369 append_text (otext, tmp);
372 sprintf (tmp, "*** a60 starting:\n");
373 append_text (otext, tmp);
376 append_text (otext, "*** a60 done.\n\n");
385 fprintf (stderr, "Use: xa60 [ -V ] | [ <source file> ]\n");
400 XtAppContext app_con;
401 Widget toplevel, paned, box, clear, bexit, go;
408 toplevel = XtAppInitialize (&app_con, "XA60", NULL, ZERO,
409 &argc, argv, fallback_resources, NULL, ZERO);
412 * Check to see that all arguments were processed, and if not then
413 * report an error and exit.
421 if (! strcmp (argv [1], "-V"))
423 printf ("Version: %s.\n", VERSION);
426 else if (! strcmp (argv [1], "-h"))
438 fn_str = XtMalloc (strlen (fname) + 20);
439 sprintf (fn_str, " File: %s", fname);
443 paned = XtCreateManagedWidget ("paned", panedWidgetClass, toplevel,
446 box = XtCreateManagedWidget ("box", boxWidgetClass, paned,
449 bexit = XtCreateManagedWidget ("bexit", commandWidgetClass, box,
452 clear = XtCreateManagedWidget ("clear", commandWidgetClass, box,
455 save = XtCreateManagedWidget ("save", commandWidgetClass, box,
458 go = XtCreateManagedWidget ("go", commandWidgetClass, box,
461 XtSetArg (args[0], XtNlabel, fn_str);
462 XtCreateManagedWidget ("fnlab", labelWidgetClass, box, args, ONE);
464 XtSetArg (args[0], XtNstring, txt_buf);
465 etext = XtCreateManagedWidget ("etext", asciiTextWidgetClass, paned,
468 XtSetArg (args[0], XtNstring, "See:\n");
469 otext = XtCreateManagedWidget ("otext", asciiTextWidgetClass, paned,
472 XtAddCallback (bexit, XtNcallback, cb_bexit, (XtPointer) etext);
473 XtAddCallback (clear, XtNcallback, cb_clear, (XtPointer) etext);
475 XtAddCallback (save, XtNcallback, cb_save, (XtPointer) etext);
477 XtAddCallback (go, XtNcallback, cb_go, (XtPointer) etext);
479 XtRealizeWidget (toplevel);
480 XtAppMainLoop (app_con);