Add more commands to s51 assembly-language debugger
[fw/altos] / s51 / s51-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 "s51.h"
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24
25 static int s51_port = 0;
26 static char *cpu = "8051";
27 static double freq = 11059200;
28 char *s51_prompt = "> ";
29 struct ccdbg *s51_dbg;
30
31 static void
32 usage(void)
33 {
34         fprintf(stderr, "You're doing it wrong.\n");
35         exit(1);
36 }
37
38 int
39 main(int argc, char **argv)
40 {
41         int flags, opt;
42         FILE *console_in = stdin;
43         FILE *console_out = stdout;
44         char *endptr;
45
46         while ((opt = getopt(argc, argv, "PVvHht:X:c:r:Z:s:S:p:")) != -1) {
47                 switch (opt) {
48                 case 't':
49                         cpu = optarg;
50                         break;
51                 case 'X':
52                         freq = strtod(optarg, &endptr);
53                         if (endptr == optarg)
54                                 usage();
55                         if (endptr[0] != '\0') {
56                                 if (!strcmp(endptr, "k"))
57                                         freq *= 1000;
58                                 else if (!strcmp(endptr, "M") )
59                                         freq *= 1000000;
60                                 else
61                                         usage ();
62                         }
63                         break;
64                 case 'c':
65                         break;
66                 case 'r':
67                 case 'Z':
68                         s51_port = strtol(optarg, &endptr, 0);
69                         if (endptr == optarg || strlen(endptr) != 0)
70                                 usage();
71                         break;
72                 case 's':
73                         break;
74                 case 'S':
75                         break;
76                 case 'p':
77                         s51_prompt = optarg;
78                         break;
79                 case 'P':
80                         s51_prompt = NULL;
81                         break;
82                 case 'V':
83                         break;
84                 case 'v':
85                         break;
86                 case 'H':
87                         exit (0);
88                         break;
89                 case 'h':
90                         usage ();
91                         break;
92                 }
93         }
94         if (s51_port) {
95                 int l, r, one = 1;
96                 int s;
97                 struct sockaddr_in in;
98
99                 l = socket(AF_INET, SOCK_STREAM, 0);
100                 if (l < 0) {
101                         perror ("socket");
102                         exit(1);
103                 }
104                 r = setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int));
105                 if (r) {
106                         perror("setsockopt");
107                         exit(1);
108                 }
109                 in.sin_family = AF_INET;
110                 in.sin_port = htons(s51_port);
111                 in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
112                 r = bind(l, (struct sockaddr *) &in, sizeof (in));
113                 if (r) {
114                         perror("bind");
115                         exit(1);
116                 }
117                 r = listen(l, 5);
118                 if (r) {
119                         perror("listen");
120                         exit(1);
121                 }
122                 for (;;) {
123                         struct sockaddr_in client_addr;
124                         socklen_t client_len = sizeof (struct sockaddr_in);
125                         FILE *client_in, *client_out;
126                         
127                         s = accept(l, (struct sockaddr *)
128                                    &client_addr, &client_len);
129                         if (s < 0) {
130                                 perror("accept");
131                                 exit(1);
132                         }
133                         client_in = fdopen(s, "r");
134                         client_out = fdopen(s, "w");
135                         if (!client_in || !client_out) {
136                                 perror("fdopen");
137                                 exit(1);
138                         }
139                         command_read(client_in, client_out);
140                         fclose(client_in);
141                         fclose(client_out);
142                 }
143         } else
144                 command_read(console_in, console_out);
145         exit(0);
146 }