summaryrefslogtreecommitdiff
path: root/s51/s51-main.c
blob: e8bf2d7dbf65d0020ffa511b2426ffc1ded748df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright © 2008 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "s51.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

static int s51_port = 0;
static char *cpu = "8051";
static double freq = 11059200;
char *s51_prompt = "> ";
struct ccdbg *s51_dbg;

static void
usage(void)
{
	fprintf(stderr, "You're doing it wrong.\n");
	exit(1);
}

int
main(int argc, char **argv)
{
	int flags, opt;
	FILE *console_in = stdin;
	FILE *console_out = stdout;
	char *endptr;

	while ((opt = getopt(argc, argv, "PVvHht:X:c:Z:s:S:p:")) != -1) {
		switch (opt) {
		case 't':
			cpu = optarg;
			break;
		case 'X':
			freq = strtod(optarg, &endptr);
			if (endptr == optarg)
				usage();
			if (endptr[0] != '\0') {
				if (!strcmp(endptr, "k"))
					freq *= 1000;
				else if (!strcmp(endptr, "M") )
					freq *= 1000000;
				else
					usage ();
			}
			break;
		case 'c':
			break;
		case 'Z':
			s51_port = strtol(optarg, &endptr, 0);
			if (endptr == optarg || strlen(endptr) != 0)
				usage();
			break;
		case 's':
			break;
		case 'S':
			break;
		case 'p':
			s51_prompt = optarg;
			break;
		case 'P':
			s51_prompt = NULL;
			break;
		case 'V':
			break;
		case 'v':
			break;
		case 'H':
			exit (0);
			break;
		case 'h':
			usage ();
			break;
		}
	}
	if (s51_port) {
		int l, r, one = 1;
		int s;
		struct sockaddr_in in;

		l = socket(AF_INET, SOCK_STREAM, 0);
		if (l < 0) {
			perror ("socket");
			exit(1);
		}
		in.sin_family = AF_INET;
		in.sin_port = htons(s51_port);
		in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
		r = bind(l, (struct sockaddr *) &in, sizeof (in));
		if (r) {
			perror("bind");
			exit(1);
		}
		r = setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int));
		if (r) {
			perror("setsockopt");
			exit(1);
		}
		r = listen(l, 5);
		if (r) {
			perror("listen");
			exit(1);
		}
		for (;;) {
			struct sockaddr_in client_addr;
			socklen_t client_len = sizeof (struct sockaddr_in);
			FILE *client;
			
			s = accept(r, (struct sockaddr *)
				   &client_addr, &client_len);
			if (s < 0) {
				perror("accept");
				exit(1);
			}
			client = fdopen(s, "rw");
			if (!client) {
				perror("fdopen");
				exit(1);
			}
			command_read(client, client);
			fclose(client);
		}
	} else
		command_read(console_in, console_out);
	exit(0);
}