fossil.c 2.5 KB

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