-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
203 lines (153 loc) · 3.14 KB
/
main.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "marignotti.h"
#include <memory.h>
#include <texttool.h>
#include <misctool.h>
#include <locator.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <gno/gno.h>
#pragma optimize 79
#pragma stacksize 768 // 406, so far.
int semID = 0;
Word MyID;
Word QuitFlag;
Word Debug;
#pragma databank 0
void signal_handler(int sig, int code)
{
WriteLine("\pWe got signal!");
QuitFlag = 1;
}
void DisplayMessage(const char *string)
{
if (string) WriteLine(string);
}
#pragma databank 0
// startup/shutdown flags.
enum {
kLoaded = 1,
kStarted = 2,
kConnected = 4
};
Word StartUp(displayPtr fx)
{
word status;
word flags = 0;
// TCPIP is an init, not a tool, so it should always
// be loaded.
status = TCPIPStatus();
if (_toolErr)
{
LoadOneTool(54, 0x0300);
if (_toolErr) return -1;
status = 0;
flags |= kLoaded;
}
#if 1
// require 3.0b3
if (TCPIPLongVersion() < 0x03006003)
{
if (fx) fx("\pMarinetti 3.0b3 is required.");
if (flags & kLoaded)
UnloadOneTool(54);
return -1;
}
#endif
if (!status)
{
TCPIPStartUp();
if (_toolErr) return -1;
flags |= kStarted;
}
status = TCPIPGetConnectStatus();
if (!status)
{
TCPIPConnect(fx);
flags |= kConnected;
}
return flags;
}
void ShutDown(word flags, Boolean force, displayPtr fx)
{
if (flags & kConnected)
{
TCPIPDisconnect(force, fx);
if (_toolErr) return;
}
if (flags & kStarted)
{
TCPIPShutDown();
if (_toolErr) return;
}
if (flags & kLoaded)
{
UnloadOneTool(54);
}
}
#define VERSION "0.4"
static void version(void)
{
fputs("Marignotti " VERSION "\n", stdout);
}
static void help(void)
{
fputs("Marignotti " VERSION "\n", stdout);
fputs("Marignotti is a TCP driver for GNO/ME 2.0.6\n", stdout);
fputs("Marinetti 3.0b3 or newer is required.\n", stdout);
fputs("\n", stdout);
fputs("Usage: marignotti [-h -d -v]\n", stdout);
fputs("-h show help information.\n", stdout);
fputs("-v show version information.\n", stdout);
fputs("-d increase debug level [Sweet-16 only].\n", stdout);
}
int main(int argc, char **argv)
{
int flags;
int ch;
_beginStackCheck();
Debug = 0;
QuitFlag = 0;
while ((ch = getopt(argc, argv, "dhv")) != EOF)
{
switch (ch)
{
case 'd':
Debug++;
break;
case 'h':
case '?':
help();
return 0;
break;
case 'v':
version();
return 0;
break;
}
}
MyID = MMStartUp();
flags = StartUp(DisplayMessage);
if (flags == -1) exit(1);
semID = screate(1);
InstallNetDriver(driver, 0);
signal(SIGQUIT, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGHUP, signal_handler);
// SIGUSR to dump table ala netstat?
while (!QuitFlag)
{
IncBusy();
TCPIPPoll();
DecBusy();
process_table();
Resched();
}
InstallNetDriver(NULL, 0);
sdelete(semID);
destroy_table();
ShutDown(flags, 0, DisplayMessage);
_reportStack();
return 0;
}