ao-tools: Add lots of compiler warning flags to ao-tools build
[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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "ao-dbg.h"
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <signal.h>
29 #include <stdarg.h>
30 #include <poll.h>
31 #include <getopt.h>
32
33 static int s51_port = 0;
34 static char *cpu = "8051";
35 static double freq = 11059200;
36 char *s51_prompt = "> ";
37 struct ccdbg *s51_dbg;
38 int s51_interrupted = 0;
39 int s51_monitor = 0;
40 char *s51_tty = NULL;
41 char *s51_device = NULL;
42
43 static FILE *s51_input;
44 static FILE *s51_output;
45
46 static void
47 usage(void)
48 {
49         fprintf(stderr, "You're doing it wrong.\n");
50         exit(1);
51 }
52
53 void s51_sigint()
54 {
55         s51_interrupted = 1;
56 }
57
58 static const struct option options[] = {
59         { .name = "tty", .has_arg = 1, .val = 'T' },
60         { .name = "device", .has_arg = 1, .val = 'D' },
61         { 0, 0, 0, 0 },
62 };
63
64 int
65 main(int argc, char **argv)
66 {
67         int flags, opt;
68         char *endptr;
69
70         while ((opt = getopt_long(argc, argv, "PVvHhmt:X:c:r:Z:s:S:p:T:", options, NULL)) != -1) {
71                 switch (opt) {
72                 case 't':
73                         cpu = optarg;
74                         break;
75                 case 'X':
76                         freq = strtod(optarg, &endptr);
77                         if (endptr == optarg)
78                                 usage();
79                         if (endptr[0] != '\0') {
80                                 if (!strcmp(endptr, "k"))
81                                         freq *= 1000;
82                                 else if (!strcmp(endptr, "M") )
83                                         freq *= 1000000;
84                                 else
85                                         usage ();
86                         }
87                         break;
88                 case 'c':
89                         break;
90                 case 'r':
91                 case 'Z':
92                         s51_port = strtol(optarg, &endptr, 0);
93                         if (endptr == optarg || strlen(endptr) != 0)
94                                 usage();
95                         break;
96                 case 's':
97                         break;
98                 case 'S':
99                         break;
100                 case 'p':
101                         s51_prompt = optarg;
102                         break;
103                 case 'P':
104                         s51_prompt = NULL;
105                         break;
106                 case 'V':
107                         break;
108                 case 'v':
109                         break;
110                 case 'H':
111                         exit (0);
112                         break;
113                 case 'h':
114                         usage ();
115                         break;
116                 case 'm':
117                         s51_monitor = 1;
118                         break;
119                 case 'T':
120                         s51_tty = optarg;
121                         break;
122                 case 'D':
123                         s51_device = optarg;
124                         break;
125                 }
126         }
127         if (s51_port) {
128                 int l, r, one = 1;
129                 int s;
130                 struct sockaddr_in in;
131
132                 l = socket(AF_INET, SOCK_STREAM, 0);
133                 if (l < 0) {
134                         perror ("socket");
135                         exit(1);
136                 }
137                 r = setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int));
138                 if (r) {
139                         perror("setsockopt");
140                         exit(1);
141                 }
142                 in.sin_family = AF_INET;
143                 in.sin_port = htons(s51_port);
144                 in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
145                 r = bind(l, (struct sockaddr *) &in, sizeof (in));
146                 if (r) {
147                         perror("bind");
148                         exit(1);
149                 }
150                 r = listen(l, 5);
151                 if (r) {
152                         perror("listen");
153                         exit(1);
154                 }
155                 for (;;) {
156                         struct sockaddr_in client_addr;
157                         socklen_t client_len = sizeof (struct sockaddr_in);
158
159                         s = accept(l, (struct sockaddr *)
160                                    &client_addr, &client_len);
161                         if (s < 0) {
162                                 perror("accept");
163                                 exit(1);
164                         }
165                         s51_input = fdopen(s, "r");
166                         s51_output = fdopen(s, "w");
167                         if (!s51_input || !s51_output) {
168                                 perror("fdopen");
169                                 exit(1);
170                         }
171                         signal(SIGINT, SIG_IGN);
172                         command_read();
173                         signal(SIGINT, SIG_DFL);
174                         fclose(s51_input);
175                         fclose(s51_output);
176                 }
177         } else {
178                 s51_input = stdin;
179                 s51_output = stdout;
180                 signal(SIGINT, s51_sigint);
181                 command_read();
182         }
183         exit(0);
184 }
185
186 void
187 s51_printf(char *format, ...)
188 {
189         va_list ap;
190
191         va_start(ap, format);
192         vfprintf(s51_output, format, ap);
193         if (s51_monitor)
194                 vfprintf(stdout, format, ap);
195         va_end(ap);
196 }
197
198 void
199 s51_putc(int c)
200 {
201         putc(c, s51_output);
202 }
203
204 #if HAVE_LIBREADLINE
205 #include <readline/readline.h>
206 #include <readline/history.h>
207 #endif
208
209 int
210 s51_read_line(char *line, int len)
211 {
212         int ret;
213 #if HAVE_LIBREADLINE
214         if (s51_output == stdout && s51_input == stdin && s51_prompt) {
215                 char *r;
216
217                 r = readline(s51_prompt);
218                 if (r == NULL)
219                         return 0;
220                 strncpy (line, r, len);
221                 line[len-1] = '\0';
222                 add_history(r);
223                 return 1;
224         } else
225 #endif
226         {
227                 if (s51_prompt)
228                         s51_printf("%s", s51_prompt);
229                 else
230                         s51_putc('\0');
231                 fflush(s51_output);
232                 ret = fgets(line, len, s51_input) != NULL;
233                 if (s51_monitor)
234                         printf("> %s", line);
235                 fflush(stdout);
236         }
237         return ret;
238 }
239
240 int
241 s51_check_input(void)
242 {
243         struct pollfd   input;
244         int r;
245         int c;
246
247         input.fd = fileno(s51_input);
248         input.events = POLLIN;
249         r = poll(&input, 1, 0);
250         if (r > 0) {
251                 char line[256];
252                 (void) s51_read_line(line, sizeof (line));
253                 return 1;
254         }
255         return 0;
256 }