xref: /inferno-os/utils/acid/os-Nt.c (revision 6e425a9de8c003b5a733621a6b6730ec3cc902b8)
1 /*
2  * Windows Nt
3  */
4 
5 #include <lib9.h>
6 #include <bio.h>
7 #include <ctype.h>
8 #include "mach.h"
9 #define Extern extern
10 #include "acid.h"
11 #include <signal.h>
12 
13 #include <windows.h>
14 
15 #define MAXBUFSIZ 16640 /* 2 STYX messages plus headers  */
16 #define NT_DEBUG
17 int	nt_debug = 0;
18 
19 int
opentty(char * tty,int baud)20 opentty(char *tty, int baud)
21 {
22 	HANDLE	comport;
23 	DCB	dcb;
24 	COMMTIMEOUTS	timeouts;
25 
26 	comport = CreateFile(tty, GENERIC_READ|GENERIC_WRITE,
27 				0, 0, OPEN_EXISTING,
28 				FILE_ATTRIBUTE_NORMAL, 0);
29 	if (comport == INVALID_HANDLE_VALUE) {
30 		werrstr("could not create port %s", tty);
31 		return -1;
32 	}
33 
34 	if (SetupComm(comport, MAXBUFSIZ, MAXBUFSIZ) != TRUE) {
35 		werrstr("could not set up %s Comm port", tty);
36 		CloseHandle(comport);
37 		return -1;
38 	}
39 
40 	if (GetCommState(comport, &dcb) != TRUE) {
41 		werrstr("could not get %s comstate", tty);
42 		CloseHandle(comport);
43 		return -1;
44 	}
45 
46 	if (baud == 0) {
47 		dcb.BaudRate = 19200;
48 	} else {
49 		dcb.BaudRate = baud;
50 	}
51 	dcb.ByteSize = 8;
52 	dcb.fParity = 0;
53 	dcb.Parity = NOPARITY;
54 	dcb.StopBits = ONESTOPBIT;
55 	dcb.fInX = 0;
56 	dcb.fOutX = 0;
57 	dcb.fAbortOnError = 1;
58 
59 	if (SetCommState(comport, &dcb) != TRUE) {
60 		werrstr("could not set %s comstate", tty);
61 		CloseHandle(comport);
62 		return -1;
63 	}
64 
65 	timeouts.ReadIntervalTimeout = 2;
66 	/* char time in milliseconds, at 19.2K char time is .4 ms */
67 	timeouts.ReadTotalTimeoutMultiplier = 0; /* was 100; */
68 	timeouts.ReadTotalTimeoutConstant = 200; /* was 500; */
69 	timeouts.WriteTotalTimeoutMultiplier = 0; /* was 10; */
70 	timeouts.WriteTotalTimeoutConstant = 400; /* was 20; */
71 
72 	SetCommTimeouts(comport, &timeouts);
73 
74 	EscapeCommFunction(comport, SETDTR);
75 
76 	return (int) comport;
77 }
78 
79 int
remote_read(int fd,char * buf,int bytes)80 remote_read(int fd, char *buf, int bytes)
81 {
82 	DWORD numread = 0;
83 	BOOL rtn;
84 
85 #ifdef NT_DEBUG
86 	if (nt_debug) {
87 		print("NT:rread fd %x bytes: %d", fd, bytes);
88 	}
89 #endif
90 	rtn = ReadFile((HANDLE) fd, buf, bytes, &numread, 0);
91 #ifdef NT_DEBUG
92 	if (nt_debug) {
93 		print(" numread: %d rtn: %x\n", numread, rtn);
94 		if (numread) {
95 			char *cp;
96 			int i;
97 
98 			cp = (char *) buf;
99 			for (i=0; i < numread; i++) {
100 				print(" %2.2x", *cp++);
101 			}
102 			print("\n");
103 		}
104 	}
105 #endif
106 	if (!rtn)
107 		return -1;
108 	else
109 		return numread;
110 }
111 
112 int
remote_write(int fd,char * buf,int bytes)113 remote_write(int fd, char *buf, int bytes)
114 {
115 	DWORD numwrt = 0;
116 	BOOL	rtn;
117 	char	*cp;
118 	int	i;
119 
120 #ifdef NT_DEBUG
121 	if (nt_debug) {
122 		print("NT:rwrite fd %x bytes: %d", fd, bytes);
123 		print("\n");
124 		cp = (char *) buf;
125 		for (i=0; i < bytes; i++) {
126 			print(" %2.2x", *cp++);
127 		}
128 		print("\n");
129 	}
130 #endif
131 	while (bytes > 0) {
132 		rtn = WriteFile((HANDLE) fd, buf, bytes, &numwrt, 0);
133 		if (!rtn) {
134 			break;
135 		}
136 		buf += numwrt;
137 		bytes -= numwrt;
138 	}
139 	return numwrt;
140 }
141 
142 void
detach(void)143 detach(void)
144 {
145 	/* ??? */
146 }
147 
148 char *
waitfor(int pid)149 waitfor(int pid)
150 {
151 	fprint(2, "wait unimplemented");
152 	return 0;
153 }
154 
155 int
fork(void)156 fork(void)
157 {
158 	fprint(2, "fork unimplemented");
159 	return -1;
160 }
161 
162 char *
runcmd(char * cmd)163 runcmd(char *cmd)
164 {
165 	fprint(2, "runcmd unimplemented");
166 	return 0;
167 }
168 
169 void (*notefunc)(int);
170 
171 void
os_notify(void (* func)(int))172 os_notify(void (*func)(int))
173 {
174 	notefunc = func;
175 	signal(SIGINT, func);
176 	return 0;
177 }
178 
179 void
catcher(int sig)180 catcher(int sig)
181 {
182 	if (sig == SIGINT) {
183 		gotint = 1;
184 		signal(SIGINT, notefunc);
185 	}
186 }
187 
188 void
setup_os_notify(void)189 setup_os_notify(void)
190 {
191 	os_notify(catcher);
192 }
193 
194 int
nproc(char ** argv)195 nproc(char **argv)
196 {
197 	fprint(2, "nproc not implemented\n");
198 	return -1;
199 }
200