2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
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.
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.
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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
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;
41 char *s51_device = NULL;
43 static FILE *s51_input;
44 static FILE *s51_output;
49 fprintf(stderr, "You're doing it wrong.\n");
54 s51_sigint(int signum)
59 static const struct option options[] = {
60 { .name = "tty", .has_arg = 1, .val = 'T' },
61 { .name = "device", .has_arg = 1, .val = 'D' },
66 main(int argc, char **argv)
71 while ((opt = getopt_long(argc, argv, "PVvHhmt:X:c:r:Z:s:S:p:T:", options, NULL)) != -1) {
77 freq = strtod(optarg, &endptr);
80 if (endptr[0] != '\0') {
81 if (!strcmp(endptr, "k"))
83 else if (!strcmp(endptr, "M") )
93 s51_port = strtol(optarg, &endptr, 0);
94 if (endptr == optarg || strlen(endptr) != 0)
131 struct sockaddr_in in;
133 l = socket(AF_INET, SOCK_STREAM, 0);
138 r = setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int));
140 perror("setsockopt");
143 in.sin_family = AF_INET;
144 in.sin_port = htons(s51_port);
145 in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
146 r = bind(l, (struct sockaddr *) &in, sizeof (in));
157 struct sockaddr_in client_addr;
158 socklen_t client_len = sizeof (struct sockaddr_in);
160 s = accept(l, (struct sockaddr *)
161 &client_addr, &client_len);
166 s51_input = fdopen(s, "r");
167 s51_output = fdopen(s, "w");
168 if (!s51_input || !s51_output) {
172 signal(SIGINT, SIG_IGN);
174 signal(SIGINT, SIG_DFL);
181 signal(SIGINT, s51_sigint);
188 s51_printf(char *format, ...)
192 va_start(ap, format);
193 vfprintf(s51_output, format, ap);
195 vfprintf(stdout, format, ap);
206 #include <readline/readline.h>
207 #include <readline/history.h>
211 s51_read_line(char *line, int len)
215 if (s51_output == stdout && s51_input == stdin && s51_prompt) {
218 r = readline(s51_prompt);
221 strncpy (line, r, len);
229 s51_printf("%s", s51_prompt);
233 ret = fgets(line, len, s51_input) != NULL;
235 printf("> %s", line);
242 s51_check_input(void)
247 input.fd = fileno(s51_input);
248 input.events = POLLIN;
249 r = poll(&input, 1, 0);
252 (void) s51_read_line(line, sizeof (line));