main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #include "dat.h"
  2. #include "fns.h"
  3. #include "error.h"
  4. #include "interp.h"
  5. #include "kernel.h"
  6. #include "draw.h"
  7. #include "version.h"
  8. int rebootargc = 0;
  9. char** rebootargv;
  10. static char *imod = "/dis/emuinit.dis";
  11. extern char* hosttype;
  12. char* tkfont; /* for libtk/utils.c */
  13. int tkstylus; /* libinterp/tk.c */
  14. extern int mflag;
  15. int dflag;
  16. int vflag;
  17. int vflag;
  18. Procs procs;
  19. char *eve;
  20. int Xsize = 640;
  21. int Ysize = 480;
  22. int bflag = 1;
  23. int sflag;
  24. int qflag;
  25. int xtblbit;
  26. ulong displaychan;
  27. char *cputype;
  28. static void
  29. usage(void)
  30. {
  31. fprint(2, "Usage: emu [options...] [file.dis [args...]]\n"
  32. "\t-gXxY\n"
  33. "\t-c[0-9]\n"
  34. "\t-d file.dis\n"
  35. "\t-s\n"
  36. "\t-v\n"
  37. "\t-p<poolname>=maxsize\n"
  38. "\t-f<fontpath>\n"
  39. "\t-r<rootpath>\n"
  40. "\t-7\n"
  41. "\t-B\n"
  42. "\t-C<channel string>\n"
  43. "\t-S\n");
  44. exits("usage");
  45. }
  46. static void
  47. envusage(void)
  48. {
  49. fprint(2, "emu: bad option in EMU environment variable (%s)\n", getenv("EMU"));
  50. usage();
  51. }
  52. static int
  53. isnum(char *p)
  54. {
  55. if (*p == 0) return 0;
  56. while (*p) {
  57. if (*p < '0' || *p > '9') return 0;
  58. p++;
  59. }
  60. return 1;
  61. }
  62. static int
  63. geom(char *val)
  64. {
  65. char *p;
  66. int x, y;
  67. if (val == '\0' || (*val < '0' || *val > '9'))
  68. return 0;
  69. x = strtoul(val, &p, 0);
  70. if(x >= 64)
  71. Xsize = x;
  72. if (*p++ != 'x' || !isnum(p))
  73. return 0;
  74. y = strtoul(p, &p, 0);
  75. if(y >= 48)
  76. Ysize = y;
  77. if (*p != '\0') return 0;
  78. return 1;
  79. }
  80. static void
  81. poolopt(char *str)
  82. {
  83. char *var;
  84. int n;
  85. ulong x;
  86. var = str;
  87. while(*str && *str != '=')
  88. str++;
  89. if(*str != '=' || str[1] == '\0')
  90. usage();
  91. *str++ = '\0';
  92. n = strlen(str);
  93. x = atoi(str);
  94. switch(str[n - 1]){
  95. case 'k':
  96. case 'K':
  97. x *= 1024;
  98. break;
  99. case 'm':
  100. case 'M':
  101. x *= 1024*1024;
  102. break;
  103. }
  104. if(poolsetsize(var, x) == 0)
  105. usage();
  106. }
  107. static void
  108. option(int argc, char *argv[], void (*badusage)(void))
  109. {
  110. char *cp;
  111. ARGBEGIN {
  112. default:
  113. badusage();
  114. case 'g': /* Window geometry */
  115. if (geom(EARGF(badusage())) == 0)
  116. badusage();
  117. break;
  118. case 'b': /* jit array bounds checking (obsolete, now on by default) */
  119. break;
  120. case 'B': /* suppress jit array bounds checks */
  121. bflag = 0;
  122. break;
  123. case 'c': /* Compile on the fly */
  124. cp = EARGF(badusage());
  125. if (!isnum(cp))
  126. badusage();
  127. cflag = atoi(cp);
  128. if(cflag < 0|| cflag > 9)
  129. usage();
  130. break;
  131. case 'I': /* (temporary option) run without cons */
  132. dflag++;
  133. break;
  134. case 'd': /* run as a daemon */
  135. dflag++;
  136. imod = EARGF(badusage());
  137. break;
  138. case 's': /* No trap handling */
  139. sflag++;
  140. break;
  141. case 'm': /* gc mark and sweep */
  142. cp = EARGF(badusage());
  143. if (!isnum(cp))
  144. badusage();
  145. mflag = atoi(cp);
  146. if(mflag < 0|| mflag > 9)
  147. usage();
  148. break;
  149. case 'p': /* pool option */
  150. poolopt(EARGF(badusage()));
  151. break;
  152. case 'f': /* Set font path */
  153. tkfont = EARGF(badusage());
  154. break;
  155. case 'r': /* Set inferno root */
  156. strecpy(rootdir, rootdir+sizeof(rootdir), EARGF(badusage()));
  157. break;
  158. case '7': /* use 7 bit colormap in X */
  159. xtblbit = 1;
  160. break;
  161. case 'G': /* allow global access to file system (obsolete) */
  162. break;
  163. case 'C': /* channel specification for display */
  164. cp = EARGF(badusage());
  165. displaychan = strtochan(cp);
  166. if(displaychan == 0){
  167. fprint(2, "emu: invalid channel specifier (-C): %q\n", cp);
  168. exits("usage");
  169. }
  170. break;
  171. case 'S':
  172. tkstylus = 1;
  173. break;
  174. case 'v':
  175. vflag = 1; /* print startup messages */
  176. break;
  177. } ARGEND
  178. }
  179. static void
  180. savestartup(int argc, char *argv[])
  181. {
  182. int i;
  183. rebootargc = argc;
  184. rebootargv = malloc((argc+1)*sizeof(char*));
  185. if(rebootargv == nil)
  186. panic("can't save startup args");
  187. for(i = 0; i < argc; i++) {
  188. rebootargv[i] = strdup(argv[i]);
  189. if(rebootargv[i] == nil)
  190. panic("can't save startup args");
  191. }
  192. rebootargv[i] = nil;
  193. }
  194. void
  195. putenvq(char *name, char *val, int conf)
  196. {
  197. val = smprint("%q", val);
  198. ksetenv(name, val, conf);
  199. free(val);
  200. }
  201. void
  202. putenvqv(char *name, char **v, int n, int conf)
  203. {
  204. Fmt f;
  205. int i;
  206. char *val;
  207. fmtstrinit(&f);
  208. for(i=0; i<n; i++)
  209. fmtprint(&f, "%s%q", i?" ":"", v[i]);
  210. val = fmtstrflush(&f);
  211. ksetenv(name, val, conf);
  212. free(val);
  213. }
  214. void
  215. nofence(void)
  216. {
  217. }
  218. void
  219. main(int argc, char *argv[])
  220. {
  221. char *opt, *p;
  222. char *enva[20];
  223. int envc;
  224. if(coherence == nil)
  225. coherence = nofence;
  226. quotefmtinstall();
  227. savestartup(argc, argv);
  228. /* set default root now, so either $EMU or -r can override it later */
  229. if((p = getenv("INFERNO")) != nil || (p = getenv("ROOT")) != nil)
  230. strecpy(rootdir, rootdir+sizeof(rootdir), p);
  231. opt = getenv("EMU");
  232. if(opt != nil && *opt != '\0') {
  233. enva[0] = "emu";
  234. envc = tokenize(opt, &enva[1], sizeof(enva)-1) + 1;
  235. enva[envc] = 0;
  236. option(envc, enva, envusage);
  237. }
  238. option(argc, argv, usage);
  239. eve = strdup("inferno");
  240. opt = "interp";
  241. if(cflag)
  242. opt = "compile";
  243. if(vflag)
  244. print("Inferno %s main (pid=%d) %s\n", VERSION, getpid(), opt);
  245. libinit(imod);
  246. }
  247. void
  248. emuinit(void *imod)
  249. {
  250. Osenv *e;
  251. char *wdir;
  252. e = up->env;
  253. e->pgrp = newpgrp();
  254. e->fgrp = newfgrp(nil);
  255. e->egrp = newegrp();
  256. e->errstr = e->errbuf0;
  257. e->syserrstr = e->errbuf1;
  258. e->user = strdup("");
  259. links();
  260. chandevinit();
  261. if(waserror())
  262. panic("setting root and dot");
  263. e->pgrp->slash = namec("#/", Atodir, 0, 0);
  264. cnameclose(e->pgrp->slash->name);
  265. e->pgrp->slash->name = newcname("/");
  266. e->pgrp->dot = cclone(e->pgrp->slash);
  267. poperror();
  268. strcpy(up->text, "main");
  269. if(kopen("#c/cons", OREAD) != 0)
  270. fprint(2, "failed to make fd0 from #c/cons: %r\n");
  271. kopen("#c/cons", OWRITE);
  272. kopen("#c/cons", OWRITE);
  273. /* the setid cannot precede the bind of #U */
  274. kbind("#U", "/", MAFTER|MCREATE);
  275. setid(eve, 0);
  276. kbind("#^", "/dev", MBEFORE); /* snarf */
  277. kbind("#^", "/chan", MBEFORE);
  278. kbind("#m", "/dev", MBEFORE); /* pointer */
  279. kbind("#c", "/dev", MBEFORE);
  280. kbind("#p", "/prog", MREPL);
  281. kbind("#d", "/fd", MREPL);
  282. kbind("#I", "/net", MAFTER); /* will fail on Plan 9 */
  283. /* BUG: we actually only need to do these on Plan 9 */
  284. kbind("#U/dev", "/dev", MAFTER);
  285. kbind("#U/net", "/net", MAFTER);
  286. kbind("#U/net.alt", "/net.alt", MAFTER);
  287. if(cputype != nil)
  288. ksetenv("cputype", cputype, 1);
  289. putenvqv("emuargs", rebootargv, rebootargc, 1);
  290. putenvq("emuroot", rootdir, 1);
  291. ksetenv("emuhost", hosttype, 1);
  292. wdir = malloc(1024);
  293. if(wdir != nil){
  294. if(getwd(wdir, 1024) != nil)
  295. putenvq("emuwdir", wdir, 1);
  296. free(wdir);
  297. }
  298. kproc("main", disinit, imod, KPDUPFDG|KPDUPPG|KPDUPENVG);
  299. for(;;)
  300. ospause();
  301. }
  302. void
  303. errorf(char *fmt, ...)
  304. {
  305. va_list arg;
  306. char buf[PRINTSIZE];
  307. va_start(arg, fmt);
  308. vseprint(buf, buf+sizeof(buf), fmt, arg);
  309. va_end(arg);
  310. error(buf);
  311. }
  312. void
  313. error(char *err)
  314. {
  315. if(err != up->env->errstr && up->env->errstr != nil)
  316. kstrcpy(up->env->errstr, err, ERRMAX);
  317. // ossetjmp(up->estack[NERR-1]);
  318. nexterror();
  319. }
  320. void
  321. exhausted(char *resource)
  322. {
  323. char buf[64];
  324. int n;
  325. n = snprint(buf, sizeof(buf), "no free %s\n", resource);
  326. iprint(buf);
  327. buf[n-1] = 0;
  328. error(buf);
  329. }
  330. void
  331. nexterror(void)
  332. {
  333. oslongjmp(nil, up->estack[--up->nerr], 1);
  334. }
  335. /* for dynamic modules - functions not macros */
  336. void*
  337. waserr(void)
  338. {
  339. up->nerr++;
  340. return up->estack[up->nerr-1];
  341. }
  342. void
  343. poperr(void)
  344. {
  345. up->nerr--;
  346. }
  347. char*
  348. enverror(void)
  349. {
  350. return up->env->errstr;
  351. }
  352. void
  353. panic(char *fmt, ...)
  354. {
  355. va_list arg;
  356. char buf[512];
  357. va_start(arg, fmt);
  358. vseprint(buf, buf+sizeof(buf), fmt, arg);
  359. va_end(arg);
  360. fprint(2, "panic: %s\n", buf);
  361. if(sflag)
  362. abort();
  363. cleanexit(0);
  364. }
  365. int
  366. iprint(char *fmt, ...)
  367. {
  368. int n;
  369. va_list va;
  370. char buf[1024];
  371. va_start(va, fmt);
  372. n = vseprint(buf, buf+sizeof buf, fmt, va) - buf;
  373. va_end(va);
  374. write(1, buf, n);
  375. return 1;
  376. }
  377. void
  378. _assert(char *fmt)
  379. {
  380. panic("assert failed: %s", fmt);
  381. }
  382. /*
  383. * mainly for libmp
  384. */
  385. void
  386. sysfatal(char *fmt, ...)
  387. {
  388. va_list arg;
  389. char buf[64];
  390. va_start(arg, fmt);
  391. vsnprint(buf, sizeof(buf), fmt, arg);
  392. va_end(arg);
  393. error(buf);
  394. }
  395. void
  396. oserror(void)
  397. {
  398. oserrstr(up->env->errstr, ERRMAX);
  399. error(up->env->errstr);
  400. }