win32.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #include <windows.h>
  2. #include "u.h"
  3. #include "lib.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include <libsec.h>
  7. typedef struct Oproc Oproc;
  8. struct Oproc {
  9. int tid;
  10. HANDLE *sema;
  11. };
  12. static int tlsx = TLS_OUT_OF_INDEXES;
  13. char *argv0;
  14. Proc*
  15. _getproc(void)
  16. {
  17. if(tlsx == TLS_OUT_OF_INDEXES)
  18. return nil;
  19. return TlsGetValue(tlsx);
  20. }
  21. void
  22. _setproc(Proc *p)
  23. {
  24. if(tlsx == TLS_OUT_OF_INDEXES){
  25. tlsx = TlsAlloc();
  26. if(tlsx == TLS_OUT_OF_INDEXES)
  27. panic("out of indexes");
  28. }
  29. TlsSetValue(tlsx, p);
  30. }
  31. void
  32. oserror(void)
  33. {
  34. oserrstr();
  35. nexterror();
  36. }
  37. void
  38. osinit(void)
  39. {
  40. Oproc *t;
  41. static Proc firstprocCTstore;
  42. _setproc(&firstprocCTstore);
  43. t = (Oproc*)firstprocCTstore.oproc;
  44. assert(t != 0);
  45. t->tid = GetCurrentThreadId();
  46. t->sema = CreateSemaphore(0, 0, 1000, 0);
  47. if(t->sema == 0) {
  48. oserror();
  49. panic("could not create semaphore: %r");
  50. }
  51. }
  52. void
  53. osnewproc(Proc *p)
  54. {
  55. Oproc *op;
  56. op = (Oproc*)p->oproc;
  57. op->sema = CreateSemaphore(0, 0, 1000, 0);
  58. if (op->sema == 0) {
  59. oserror();
  60. panic("could not create semaphore: %r");
  61. }
  62. }
  63. void
  64. osmsleep(int ms)
  65. {
  66. Sleep((DWORD) ms);
  67. }
  68. void
  69. osyield(void)
  70. {
  71. Sleep(0);
  72. }
  73. static DWORD WINAPI tramp(LPVOID vp);
  74. void
  75. osproc(Proc *p)
  76. {
  77. DWORD tid;
  78. if(CreateThread(0, 0, tramp, p, 0, &tid) == 0) {
  79. oserror();
  80. panic("osproc: %r");
  81. }
  82. Sleep(0);
  83. }
  84. static DWORD WINAPI
  85. tramp(LPVOID vp)
  86. {
  87. Proc *p = (Proc *) vp;
  88. Oproc *op = (Oproc*) p->oproc;
  89. _setproc(p);
  90. op->tid = GetCurrentThreadId();
  91. op->sema = CreateSemaphore(0, 0, 1000, 0);
  92. if(op->sema == 0) {
  93. oserror();
  94. panic("could not create semaphore: %r");
  95. }
  96. (*p->fn)(p->arg);
  97. ExitThread(0);
  98. return 0;
  99. }
  100. void
  101. procsleep(void)
  102. {
  103. Proc *p;
  104. Oproc *op;
  105. p = up;
  106. op = (Oproc*)p->oproc;
  107. WaitForSingleObject(op->sema, INFINITE);}
  108. void
  109. procwakeup(Proc *p)
  110. {
  111. Oproc *op;
  112. op = (Oproc*)p->oproc;
  113. ReleaseSemaphore(op->sema, 1, 0);
  114. }
  115. void
  116. random20(uchar *p)
  117. {
  118. LARGE_INTEGER ti;
  119. int i, j;
  120. FILETIME ft;
  121. DigestState ds;
  122. vlong tsc;
  123. GetSystemTimeAsFileTime(&ft);
  124. memset(&ds, 0, sizeof ds);
  125. sha1((uchar*)&ft, sizeof(ft), 0, &ds);
  126. for(i=0; i<50; i++) {
  127. for(j=0; j<10; j++) {
  128. QueryPerformanceCounter(&ti);
  129. sha1((uchar*)&ti, sizeof(ti), 0, &ds);
  130. tsc = GetTickCount();
  131. sha1((uchar*)&tsc, sizeof(tsc), 0, &ds);
  132. }
  133. Sleep(10);
  134. }
  135. sha1(0, 0, p, &ds);
  136. }
  137. void
  138. randominit(void)
  139. {
  140. }
  141. ulong
  142. randomread(void *v, ulong n)
  143. {
  144. int i;
  145. uchar p[20];
  146. for(i=0; i<n; i+=20){
  147. random20(p);
  148. if(i+20 <= n)
  149. memmove((char*)v+i, p, 20);
  150. else
  151. memmove((char*)v+i, p, n-i);
  152. }
  153. return n;
  154. }
  155. long
  156. seconds(void)
  157. {
  158. return time(0);
  159. }
  160. ulong
  161. ticks(void)
  162. {
  163. return GetTickCount();
  164. }
  165. #if 0
  166. uvlong
  167. fastticks(uvlong *v)
  168. {
  169. uvlong n;
  170. n = GetTickCount() * 1000 * 1000;
  171. if(v)
  172. *v = n;
  173. return n;
  174. }
  175. #endif
  176. extern int main(int, char*[]);
  177. int
  178. wstrutflen(Rune *s)
  179. {
  180. int n;
  181. for(n=0; *s; n+=runelen(*s),s++)
  182. ;
  183. return n;
  184. }
  185. int
  186. wstrtoutf(char *s, Rune *t, int n)
  187. {
  188. int i;
  189. char *s0;
  190. s0 = s;
  191. if(n <= 0)
  192. return wstrutflen(t)+1;
  193. while(*t) {
  194. if(n < UTFmax+1 && n < runelen(*t)+1) {
  195. *s = 0;
  196. return s-s0+wstrutflen(t)+1;
  197. }
  198. i = runetochar(s, t);
  199. s += i;
  200. n -= i;
  201. t++;
  202. }
  203. *s = 0;
  204. return s-s0;
  205. }
  206. int
  207. win_hasunicode(void)
  208. {
  209. OSVERSIONINFOA osinfo;
  210. int r;
  211. osinfo.dwOSVersionInfoSize = sizeof(osinfo);
  212. if(!GetVersionExA(&osinfo))
  213. panic("GetVersionEx failed");
  214. switch(osinfo.dwPlatformId) {
  215. default:
  216. panic("unknown PlatformId");
  217. case VER_PLATFORM_WIN32s:
  218. panic("Win32s not supported");
  219. case VER_PLATFORM_WIN32_WINDOWS:
  220. r = 0;
  221. break;
  222. case VER_PLATFORM_WIN32_NT:
  223. r = 1;
  224. break;
  225. }
  226. return r;
  227. }
  228. int
  229. wstrlen(Rune *s)
  230. {
  231. int n;
  232. for(n=0; *s; s++,n++)
  233. ;
  234. return n;
  235. }
  236. static int args(char *argv[], int n, char *p);
  237. int APIENTRY
  238. WinMain(HINSTANCE x, HINSTANCE y, LPSTR z, int w)
  239. {
  240. int argc, n;
  241. char *arg, *p, **argv;
  242. Rune *warg;
  243. if(0 && win_hasunicode()){
  244. warg = GetCommandLineW();
  245. n = (wstrlen(warg)+1)*UTFmax;
  246. arg = malloc(n);
  247. wstrtoutf(arg, warg, n);
  248. }else
  249. arg = GetCommandLineA();
  250. /* conservative guess at the number of args */
  251. for(argc=4,p=arg; *p; p++)
  252. if(*p == ' ' || *p == '\t')
  253. argc++;
  254. argv = malloc(argc*sizeof(char*));
  255. argc = args(argv, argc, arg);
  256. mymain(argc, argv);
  257. ExitThread(0);
  258. return 0;
  259. }
  260. /*
  261. * Break the command line into arguments
  262. * The rules for this are not documented but appear to be the following
  263. * according to the source for the microsoft C library.
  264. * Words are seperated by space or tab
  265. * Words containing a space or tab can be quoted using "
  266. * 2N backslashes + " ==> N backslashes and end quote
  267. * 2N+1 backslashes + " ==> N backslashes + literal "
  268. * N backslashes not followed by " ==> N backslashes
  269. */
  270. static int
  271. args(char *argv[], int n, char *p)
  272. {
  273. char *p2;
  274. int i, j, quote, nbs;
  275. for(i=0; *p && i<n-1; i++) {
  276. while(*p == ' ' || *p == '\t')
  277. p++;
  278. quote = 0;
  279. argv[i] = p2 = p;
  280. for(;*p; p++) {
  281. if(!quote && (*p == ' ' || *p == '\t'))
  282. break;
  283. for(nbs=0; *p == '\\'; p++,nbs++)
  284. ;
  285. if(*p == '"') {
  286. for(j=0; j<(nbs>>1); j++)
  287. *p2++ = '\\';
  288. if(nbs&1)
  289. *p2++ = *p;
  290. else
  291. quote = !quote;
  292. } else {
  293. for(j=0; j<nbs; j++)
  294. *p2++ = '\\';
  295. *p2++ = *p;
  296. }
  297. }
  298. /* move p up one to avoid pointing to null at end of p2 */
  299. if(*p)
  300. p++;
  301. *p2 = 0;
  302. }
  303. argv[i] = 0;
  304. return i;
  305. }
  306. /*
  307. * Windows socket error messages
  308. * There must be a way to get these strings out of the library.
  309. * This table is derived from the MSDN online help.
  310. */
  311. static struct {
  312. int e;
  313. char *s;
  314. } tab[] = {
  315. { 10004, "interrupted function call" },
  316. { 10013, "permission denied" },
  317. { 10014, "bad address" },
  318. { 10022, "invalid argument" },
  319. { 10024, "too many open files" },
  320. { 10035, "resource temporarily unavailable" },
  321. { 10036, "operation now in progress" },
  322. { 10037, "operation already in progress" },
  323. { 10038, "socket operation on nonsocket" },
  324. { 10039, "destination address required" },
  325. { 10040, "message too long" },
  326. { 10041, "protocol wrong type for socket" },
  327. { 10042, "bad protocol option" },
  328. { 10043, "protocol not supported" },
  329. { 10044, "socket type not supported" },
  330. { 10045, "operation not supported" },
  331. { 10046, "protocol family not supported" },
  332. { 10047, "address family not supported by protocol family" },
  333. { 10048, "address already in use" },
  334. { 10049, "cannot assign requested address" },
  335. { 10050, "network is down" },
  336. { 10051, "network is unreachable" },
  337. { 10052, "network dropped connection on reset" },
  338. { 10053, "software caused connection abort" },
  339. { 10054, "connection reset by peer" },
  340. { 10055, "no buffer space available" },
  341. { 10056, "socket is already connected" },
  342. { 10057, "socket is not connected" },
  343. { 10058, "cannot send after socket shutdown" },
  344. { 10060, "connection timed out" },
  345. { 10061, "connection refused" },
  346. { 10064, "host is down" },
  347. { 10065, "no route to host" },
  348. { 10067, "too many processes" },
  349. { 10091, "network subsystem is unavailable" },
  350. { 10092, "winsock.dll version out of range" },
  351. { 10093, "wsastartup not called" },
  352. { 10101, "graceful shutdown in progress" },
  353. { 10109, "class type not found" },
  354. { 11001, "host name not found" },
  355. { 11002, "host not found (non-authoritative)" },
  356. { 11003, "nonrecoverable error" },
  357. { 11004, "valid name, but no data record of requested type" },
  358. };
  359. void
  360. osrerrstr(char *buf, uint nbuf)
  361. {
  362. char *p, *q;
  363. int e, i, r;
  364. e = GetLastError();
  365. r = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
  366. 0, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  367. buf, nbuf, 0);
  368. if(r == 0){
  369. for(i=0; i<nelem(tab); i++)
  370. if(tab[i].e == e){
  371. strecpy(buf, buf+nbuf, tab[i].s);
  372. break;
  373. }
  374. if(i==nelem(tab))
  375. snprint(buf, nbuf, "windows error %d", e);
  376. }
  377. for(p=q=buf; *p; p++) {
  378. if(*p == '\r')
  379. continue;
  380. if(*p == '\n')
  381. *q++ = ' ';
  382. else
  383. *q++ = *p;
  384. }
  385. *q = '\0';
  386. }
  387. void
  388. oserrstr(void)
  389. {
  390. osrerrstr(up->errstr, ERRMAX);
  391. }
  392. long
  393. showfilewrite(char *a, int n)
  394. {
  395. Rune *action, *arg, *cmd;
  396. static Rune Lopen[] = { 'o', 'p', 'e', 'n', 0 };
  397. cmd = runesmprint("%.*s", n, a);
  398. if(cmd == nil)
  399. error("out of memory");
  400. if(cmd[runestrlen(cmd)-1] == '\n')
  401. cmd[runestrlen(cmd)] = 0;
  402. p = runestrchr(cmd, ' ');
  403. if(p){
  404. action = cmd;
  405. *p++ = 0;
  406. arg = p;
  407. }else{
  408. action = Lopen;
  409. arg = cmd;
  410. }
  411. ShellExecute(0, 0, action, arg, 0, SW_SHOWNORMAL);
  412. return n;
  413. }