handle blank lines in spice3 ascii format - as produced by recent ngspice
[gwave-svn.git] / remote / gwaverepl.c
blob7992085f20fc52fe136dd0cc677eb2729faf3c31
1 /*
2 * gwaverepl - read-evaluate-print loop connected to running gwave process.
3 * by Steve Tell
4 * November 6 10, 2000
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this software; see the file COPYING.GPL. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307 USA
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <glib.h>
28 #include "xgexec.h"
30 Display *display;
31 char *dispName;
32 Window w;
34 extern char *split_at(char **to_split, int i);
37 int
38 init_display()
40 if (!(dispName = getenv("DISPLAY")))
41 return 0;
42 if (!(display = XOpenDisplay(dispName)))
43 return 0;
44 return 1;
48 void die(char *str)
50 fputs(str,stderr);
51 exit(1);
54 main(int argc, char **argv)
56 extern char *optarg;
57 extern int optind;
58 int ch, fd;
59 char *msg;
60 char rbuf[32768];
61 int port;
62 int len;
63 char *result, *out, *err;
65 int splitpoint;
66 char *expr;
67 int done = 0;
68 char *gather = g_new(char, 1);
71 if (argc != 1)
72 die("Usage: gwaverepl\n");
73 if (!init_display())
74 die("Could not connect to gwave server. Check your DISPLAY environment variable.\n");
76 w=xgexec_init(display);
78 if (w==None)
79 die("Unable to establish gwave-exec connection.\n");
81 #ifdef HAVE_READLINE
82 init_readline();
83 #endif
84 while (!done) {
85 if ((splitpoint = check_balance(gather))) {
86 char *result, *error, *output;
87 expr = split_at(&gather,splitpoint);
89 result = xgexec_exec_full(display, w, expr, &output, &error);
90 fputs (output, stdout);
91 if (strlen(error)!=0) {
92 fputs(error, stderr);
93 } else {
94 fputs(result, stdout);
96 putchar('\n');
98 if (result) { g_free(result); result = NULL; }
99 if (output) { g_free(output); output = NULL; }
100 if (error) { g_free(error); error = NULL; }
101 free(expr);
102 expr = NULL;
103 } else {
104 done = !appending_fgets(&gather);
108 XCloseDisplay (display);
110 exit(0);