kiss.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* This is the Keep It Simple, Stupid shell. It is written
  10. * to have as little c library usage as possible. It's handy
  11. * for bootstrapping broken harvey ports. You can make it the
  12. * boot command in your config. It tries to open /dev/cons and
  13. * if that fails #P/cons, which is useful if your namespace
  14. * is really not working. */
  15. #include <u.h>
  16. #include <libc.h>
  17. static char line[512];
  18. static char *argv[512];
  19. static int argc;
  20. static char err[] = "Can't open /dev/cons or #P/cons; continuing\n";
  21. void
  22. main(int _, char *__)
  23. {
  24. int i, amt;
  25. int fd;
  26. fd = open("#P/cons", ORDWR);
  27. if (fd < 0)
  28. write(1, err, sizeof(err)-1);
  29. else {
  30. if (fd != 0)
  31. dup(fd, 0);
  32. dup(fd, 1);
  33. dup(fd, 2);
  34. }
  35. argv0 = "kiss";
  36. i = 0;
  37. argv[0] = line;
  38. while (1) {
  39. if ((amt = read(0, &line[i], 1)) < 1) {
  40. print("read: %r\n");
  41. break;
  42. }
  43. if (line[i] == '\n') {
  44. line[i] = 0;
  45. switch(fork()) {
  46. default:
  47. wait();
  48. break;
  49. case 0:
  50. exec(argv[0], argv);
  51. exits("exec failed");
  52. break;
  53. case -1:
  54. print("rfork failed: %r\n");
  55. break;
  56. }
  57. i = 0;
  58. argc = 0;
  59. memset(argv, 0, sizeof(argv));
  60. memset(line, 0, sizeof(line));
  61. argv[0] = line;
  62. } else if (line[i] == ' ') {
  63. line[i] = 0;
  64. argc++, i++;
  65. argv[argc] = &line[i];
  66. } else {
  67. i++;
  68. }
  69. }
  70. exits(0);
  71. }