Fix ao-bitbang examples to not have . in the first column
[fw/altos] / ao-tools / ao-dbg / ao-dbg-main.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao-dbg.h"
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <poll.h>
27 #include <getopt.h>
28
29 static int s51_port = 0;
30 static char *cpu = "8051";
31 static double freq = 11059200;
32 char *s51_prompt = "> ";
33 struct ccdbg *s51_dbg;
34 int s51_interrupted = 0;
35 int s51_monitor = 0;
36 char *s51_tty = NULL;
37
38 static FILE *s51_input;
39 static FILE *s51_output;
40
41 static void
42 usage(void)
43 {
44         fprintf(stderr, "You're doing it wrong.\n");
45         exit(1);
46 }
47
48 void s51_sigint()
49 {
50         s51_interrupted = 1;
51 }
52
53 static const struct option options[] = {
54         { .name = "tty", .has_arg = 1, .val = 'T' },
55         { 0, 0, 0, 0 },
56 };
57
58 int
59 main(int argc, char **argv)
60 {
61         int flags, opt;
62         char *endptr;
63         struct sigvec vec, ovec;
64
65         while ((opt = getopt_long(argc, argv, "PVvHhmt:X:c:r:Z:s:S:p:T:", options, NULL)) != -1) {
66                 switch (opt) {
67                 case 't':
68                         cpu = optarg;
69                         break;
70                 case 'X':
71                         freq = strtod(optarg, &endptr);
72                         if (endptr == optarg)
73                                 usage();
74                         if (endptr[0] != '\0') {
75                                 if (!strcmp(endptr, "k"))
76                                         freq *= 1000;
77                                 else if (!strcmp(endptr, "M") )
78                                         freq *= 1000000;
79                                 else
80                                         usage ();
81                         }
82                         break;
83                 case 'c':
84                         break;
85                 case 'r':
86                 case 'Z':
87                         s51_port = strtol(optarg, &endptr, 0);
88                         if (endptr == optarg || strlen(endptr) != 0)
89                                 usage();
90                         break;
91                 case 's':
92                         break;
93                 case 'S':
94                         break;
95                 case 'p':
96                         s51_prompt = optarg;
97                         break;
98                 case 'P':
99                         s51_prompt = NULL;
100                         break;
101                 case 'V':
102                         break;
103                 case 'v':
104                         break;
105                 case 'H':
106                         exit (0);
107                         break;
108                 case 'h':
109                         usage ();
110                         break;
111                 case 'm':
112                         s51_monitor = 1;
113                         break;
114                 case 'T':
115                         s51_tty = optarg;
116                         break;
117                 }
118         }
119         if (s51_port) {
120                 int l, r, one = 1;
121                 int s;
122                 struct sockaddr_in in;
123
124                 l = socket(AF_INET, SOCK_STREAM, 0);
125                 if (l < 0) {
126                         perror ("socket");
127                         exit(1);
128                 }
129                 r = setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int));
130                 if (r) {
131                         perror("setsockopt");
132                         exit(1);
133                 }
134                 in.sin_family = AF_INET;
135                 in.sin_port = htons(s51_port);
136                 in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
137                 r = bind(l, (struct sockaddr *) &in, sizeof (in));
138                 if (r) {
139                         perror("bind");
140                         exit(1);
141                 }
142                 r = listen(l, 5);
143                 if (r) {
144                         perror("listen");
145                         exit(1);
146                 }
147                 for (;;) {
148                         struct sockaddr_in client_addr;
149                         socklen_t client_len = sizeof (struct sockaddr_in);
150
151                         s = accept(l, (struct sockaddr *)
152                                    &client_addr, &client_len);
153                         if (s < 0) {
154                                 perror("accept");
155                                 exit(1);
156                         }
157                         s51_input = fdopen(s, "r");
158                         s51_output = fdopen(s, "w");
159                         if (!s51_input || !s51_output) {
160                                 perror("fdopen");
161                                 exit(1);
162                         }
163                         vec.sv_handler = SIG_IGN;
164                         vec.sv_mask = 0;
165                         vec.sv_flags = 0;
166                         sigvec(SIGINT, &vec, &ovec);
167                         command_read();
168                         sigvec(SIGINT, &ovec, NULL);
169                         fclose(s51_input);
170                         fclose(s51_output);
171                 }
172         } else {
173                 s51_input = stdin;
174                 s51_output = stdout;
175                 vec.sv_handler = s51_sigint;
176                 vec.sv_mask = 0;
177                 vec.sv_flags = 0;
178                 sigvec(SIGINT, &vec, &ovec);
179                 command_read();
180         }
181         exit(0);
182 }
183
184 void
185 s51_printf(char *format, ...)
186 {
187         va_list ap;
188
189         va_start(ap, format);
190         vfprintf(s51_output, format, ap);
191         if (s51_monitor)
192                 vfprintf(stdout, format, ap);
193         va_end(ap);
194 }
195
196 void
197 s51_putc(int c)
198 {
199         putc(c, s51_output);
200 }
201
202 #include <readline/readline.h>
203 #include <readline/history.h>
204
205 int
206 s51_read_line(char *line, int len)
207 {
208         int ret;
209         if (s51_output == stdout && s51_input == stdin && s51_prompt) {
210                 char *r;
211
212                 r = readline(s51_prompt);
213                 if (r == NULL)
214                         return 0;
215                 strncpy (line, r, len);
216                 line[len-1] = '\0';
217                 add_history(r);
218                 return 1;
219         } else {
220                 if (s51_prompt)
221                         s51_printf("%s", s51_prompt);
222                 else
223                         s51_putc('\0');
224                 fflush(s51_output);
225                 ret = fgets(line, len, s51_input) != NULL;
226                 if (s51_monitor)
227                         printf("> %s", line);
228                 fflush(stdout);
229         }
230         return ret;
231 }
232
233 int
234 s51_check_input(void)
235 {
236         struct pollfd   input;
237         int r;
238         int c;
239
240         input.fd = fileno(s51_input);
241         input.events = POLLIN;
242         r = poll(&input, 1, 0);
243         if (r > 0) {
244                 char line[256];
245                 (void) s51_read_line(line, sizeof (line));
246                 return 1;
247         }
248         return 0;
249 }