fossil.c 3.0 KB

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