env.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "mk.h"
  10. enum {
  11. ENVQUANTA=10
  12. };
  13. Envy *envy;
  14. static int nextv;
  15. static char *myenv[] =
  16. {
  17. "target",
  18. "stem",
  19. "prereq",
  20. "pid",
  21. "nproc",
  22. "newprereq",
  23. "alltarget",
  24. "newmember",
  25. "stem0", /* must be in order from here */
  26. "stem1",
  27. "stem2",
  28. "stem3",
  29. "stem4",
  30. "stem5",
  31. "stem6",
  32. "stem7",
  33. "stem8",
  34. "stem9",
  35. 0,
  36. };
  37. void
  38. initenv(void)
  39. {
  40. char **p;
  41. for(p = myenv; *p; p++)
  42. symlook(*p, S_INTERNAL, (void *)"");
  43. readenv(); /* o.s. dependent */
  44. }
  45. static void
  46. envinsert(char *name, Word *value)
  47. {
  48. static int envsize;
  49. if (nextv >= envsize) {
  50. envsize += ENVQUANTA;
  51. envy = (Envy *) Realloc((char *) envy, envsize*sizeof(Envy));
  52. }
  53. envy[nextv].name = name;
  54. envy[nextv++].values = value;
  55. }
  56. static void
  57. envupd(char *name, Word *value)
  58. {
  59. Envy *e;
  60. for(e = envy; e->name; e++)
  61. if(strcmp(name, e->name) == 0){
  62. delword(e->values);
  63. e->values = value;
  64. return;
  65. }
  66. e->name = name;
  67. e->values = value;
  68. envinsert(0,0);
  69. }
  70. static void
  71. ecopy(Symtab *s)
  72. {
  73. char **p;
  74. if(symlook(s->name, S_NOEXPORT, 0))
  75. return;
  76. for(p = myenv; *p; p++)
  77. if(strcmp(*p, s->name) == 0)
  78. return;
  79. envinsert(s->name, s->u.ptr);
  80. }
  81. void
  82. execinit(void)
  83. {
  84. char **p;
  85. nextv = 0;
  86. for(p = myenv; *p; p++)
  87. envinsert(*p, stow(""));
  88. symtraverse(S_VAR, ecopy);
  89. envinsert(0, 0);
  90. }
  91. Envy*
  92. buildenv(Job *j, int slot)
  93. {
  94. char **p, *cp, *qp;
  95. Word *w, *v, **l;
  96. int i;
  97. char buf[256];
  98. envupd("target", wdup(j->t));
  99. if(j->r->attr&REGEXP)
  100. envupd("stem",newword(""));
  101. else
  102. envupd("stem", newword(j->stem));
  103. envupd("prereq", wdup(j->p));
  104. snprint(buf, sizeof buf, "%d", getpid());
  105. envupd("pid", newword(buf));
  106. snprint(buf, sizeof buf, "%d", slot);
  107. envupd("nproc", newword(buf));
  108. envupd("newprereq", wdup(j->np));
  109. envupd("alltarget", wdup(j->at));
  110. l = &v;
  111. v = w = wdup(j->np);
  112. while(w){
  113. cp = strchr(w->s, '(');
  114. if(cp){
  115. qp = strchr(cp+1, ')');
  116. if(qp){
  117. *qp = 0;
  118. strcpy(w->s, cp+1);
  119. l = &w->next;
  120. w = w->next;
  121. continue;
  122. }
  123. }
  124. *l = w->next;
  125. free(w->s);
  126. free(w);
  127. w = *l;
  128. }
  129. envupd("newmember", v);
  130. /* update stem0 -> stem9 */
  131. for(p = myenv; *p; p++)
  132. if(strcmp(*p, "stem0") == 0)
  133. break;
  134. for(i = 0; *p; i++, p++){
  135. if((j->r->attr&REGEXP) && j->match[i])
  136. envupd(*p, newword(j->match[i]));
  137. else
  138. envupd(*p, newword(""));
  139. }
  140. return envy;
  141. }