fossil.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "stdinc.h"
  2. #include <ctype.h>
  3. #include "9.h"
  4. int Dflag;
  5. char* none = "none";
  6. static void
  7. usage(void)
  8. {
  9. fprint(2, "usage: %s"
  10. " [-Dt]"
  11. " [-c cmd]"
  12. " [-f partition]\n"
  13. , argv0);
  14. exits("usage");
  15. }
  16. static void
  17. readCmdPart(char *file, char ***pcmd, int *pncmd)
  18. {
  19. char buf[1024+1], *f[1024];
  20. char tbuf[1024];
  21. int nf;
  22. int i, fd, n;
  23. char **cmd, *p;
  24. int ncmd;
  25. cmd = *pcmd;
  26. ncmd = *pncmd;
  27. if((fd = open(file, OREAD)) < 0)
  28. sysfatal("open %s: %r", file);
  29. if(seek(fd, 127*1024, 0) != 127*1024)
  30. sysfatal("seek %s 127kB: %r", file);
  31. n = readn(fd, buf, sizeof buf-1);
  32. if(n == 0)
  33. sysfatal("short read of %s at 127kB", file);
  34. if(n < 0)
  35. sysfatal("read %s: %r", file);
  36. buf[n] = 0;
  37. if(memcmp(buf, "fossil config\n", 6+1+6+1) != 0)
  38. sysfatal("bad config magic in %s", file);
  39. nf = getfields(buf+6+1+6+1, f, nelem(f), 1, "\n");
  40. for(i=0; i<nf; i++){
  41. if(f[i][0] == '#')
  42. continue;
  43. cmd = vtMemRealloc(cmd, (ncmd+1)*sizeof(char*));
  44. /* expand argument '*' to mean current file */
  45. if((p = strchr(f[i], '*')) && (p==f[i]||isspace(p[-1])) && (p[1]==0||isspace(p[1]))){
  46. memmove(tbuf, f[i], p-f[i]);
  47. strecpy(tbuf+(p-f[i]), tbuf+sizeof tbuf, file);
  48. strecpy(tbuf+strlen(tbuf), tbuf+sizeof tbuf, p+1);
  49. f[i] = tbuf;
  50. }
  51. cmd[ncmd++] = vtStrDup(f[i]);
  52. }
  53. close(fd);
  54. *pcmd = cmd;
  55. *pncmd = ncmd;
  56. }
  57. void
  58. main(int argc, char* argv[])
  59. {
  60. char **cmd, *p;
  61. int i, ncmd, tflag;
  62. fmtinstall('D', dirfmt);
  63. fmtinstall('F', fcallfmt);
  64. fmtinstall('M', dirmodefmt);
  65. quotefmtinstall();
  66. /*
  67. * Insulate from the invoker's environment.
  68. */
  69. if(rfork(RFREND|RFNOTEG|RFNAMEG) < 0)
  70. sysfatal("rfork: %r");
  71. close(0);
  72. open("/dev/null", OREAD);
  73. close(1);
  74. open("/dev/null", OWRITE);
  75. cmd = nil;
  76. ncmd = tflag = 0;
  77. vtAttach();
  78. ARGBEGIN{
  79. case '?':
  80. default:
  81. usage();
  82. break;
  83. case 'D':
  84. Dflag ^= 1;
  85. break;
  86. case 'f':
  87. p = EARGF(usage());
  88. readCmdPart(p, &cmd, &ncmd);
  89. break;
  90. case 'c':
  91. p = EARGF(usage());
  92. cmd = vtMemRealloc(cmd, (ncmd+1)*sizeof(char*));
  93. cmd[ncmd++] = p;
  94. break;
  95. case 't':
  96. tflag = 1;
  97. break;
  98. }ARGEND
  99. if(argc != 0)
  100. usage();
  101. consInit();
  102. cliInit();
  103. msgInit();
  104. conInit();
  105. cmdInit();
  106. fsysInit();
  107. exclInit();
  108. fidInit();
  109. srvInit();
  110. lstnInit();
  111. usersInit();
  112. for(i = 0; i < ncmd; i++)
  113. if(cliExec(cmd[i]) == 0)
  114. fprint(2, "%s: %R\n", cmd[i]);
  115. vtMemFree(cmd);
  116. if(tflag && consTTY() == 0)
  117. consPrint("%s\n", vtGetError());
  118. vtDetach();
  119. exits(0);
  120. }