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.
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
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;
37 char *s51_device = NULL;
39 static FILE *s51_input;
40 static FILE *s51_output;
45 fprintf(stderr, "You're doing it wrong.\n");
54 static const struct option options[] = {
55 { .name = "tty", .has_arg = 1, .val = 'T' },
56 { .name = "device", .has_arg = 1, .val = 'D' },
61 main(int argc, char **argv)
65 struct sigvec vec, ovec;
67 while ((opt = getopt_long(argc, argv, "PVvHhmt:X:c:r:Z:s:S:p:T:", options, NULL)) != -1) {
73 freq = strtod(optarg, &endptr);
76 if (endptr[0] != '\0') {
77 if (!strcmp(endptr, "k"))
79 else if (!strcmp(endptr, "M") )
89 s51_port = strtol(optarg, &endptr, 0);
90 if (endptr == optarg || strlen(endptr) != 0)
127 struct sockaddr_in in;
129 l = socket(AF_INET, SOCK_STREAM, 0);
134 r = setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int));
136 perror("setsockopt");
139 in.sin_family = AF_INET;
140 in.sin_port = htons(s51_port);
141 in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
142 r = bind(l, (struct sockaddr *) &in, sizeof (in));
153 struct sockaddr_in client_addr;
154 socklen_t client_len = sizeof (struct sockaddr_in);
156 s = accept(l, (struct sockaddr *)
157 &client_addr, &client_len);
162 s51_input = fdopen(s, "r");
163 s51_output = fdopen(s, "w");
164 if (!s51_input || !s51_output) {
168 vec.sv_handler = SIG_IGN;
171 sigvec(SIGINT, &vec, &ovec);
173 sigvec(SIGINT, &ovec, NULL);
180 vec.sv_handler = s51_sigint;
183 sigvec(SIGINT, &vec, &ovec);
190 s51_printf(char *format, ...)
194 va_start(ap, format);
195 vfprintf(s51_output, format, ap);
197 vfprintf(stdout, format, ap);
207 #include <readline/readline.h>
208 #include <readline/history.h>
211 s51_read_line(char *line, int len)
214 if (s51_output == stdout && s51_input == stdin && s51_prompt) {
217 r = readline(s51_prompt);
220 strncpy (line, r, len);
226 s51_printf("%s", s51_prompt);
230 ret = fgets(line, len, s51_input) != NULL;
232 printf("> %s", line);
239 s51_check_input(void)
245 input.fd = fileno(s51_input);
246 input.events = POLLIN;
247 r = poll(&input, 1, 0);
250 (void) s51_read_line(line, sizeof (line));