|
|
1.1 ! root 1: #include "mk.h" ! 2: ! 3: bquote(fd, src, dest) ! 4: register char *src, *dest; ! 5: { ! 6: int childin[2], childout[2], pid, i; ! 7: int quotedone = 1; ! 8: char *ldest, *t; ! 9: char *newline = "\n"; ! 10: int wearedone = 0; ! 11: int initdone = 0; ! 12: extern char **environ; ! 13: ! 14: while(wearedone == 0) ! 15: switch(*src) ! 16: { ! 17: default: ! 18: *dest++ = *src++; ! 19: break; ! 20: case 0: ! 21: if(quotedone) ! 22: wearedone = 1; ! 23: else { ! 24: if(t = Frdline(fd)){ ! 25: inline++; ! 26: *dest++ = '\n'|EBIT; ! 27: strcpy(src, t); ! 28: continue; ! 29: } else { ! 30: SYNERR(-1); Fprint(2, "missing closing `\n"); ! 31: break; ! 32: } ! 33: } ! 34: break; ! 35: case '\\': ! 36: if(*src) ! 37: *dest++ = *src++; ! 38: else ! 39: if(t = Frdline(fd)){ ! 40: inline++; ! 41: strcpy(src, t); ! 42: } ! 43: break; ! 44: case '`': ! 45: if(quotedone = 1-quotedone){ ! 46: if(initdone == 0){ ! 47: execinit(); ! 48: initdone = 1; ! 49: } ! 50: Fflush(1); ! 51: Fflush(2); ! 52: if(pipe(childin) < 0){ ! 53: SYNERR(-1); perror("pipe1"); ! 54: Exit(); ! 55: } ! 56: if(pipe(childout) < 0){ ! 57: SYNERR(-1); perror("pipe2"); ! 58: Exit(); ! 59: } ! 60: if((pid = fork()) < 0){ ! 61: SYNERR(-1); perror("fork"); ! 62: Exit(); ! 63: } ! 64: if(pid){ /* parent */ ! 65: close(childin[0]); ! 66: close(childout[1]); ! 67: if(ldest < dest){ ! 68: writeout(childin[1], ldest, dest); ! 69: writeout(childin[1], newline, newline+1); ! 70: } ! 71: close(childin[1]); ! 72: dest = ldest; ! 73: while((i = read(childout[0], dest, 512)) > 0) ! 74: dest += i; ! 75: if((dest > ldest) && (dest[-1] == '\n')) ! 76: dest--; ! 77: close(childout[0]); ! 78: } else { ! 79: dup2(childin[0], 0); ! 80: dup2(childout[1], 1); ! 81: close(childin[0]); ! 82: close(childin[1]); ! 83: close(childout[0]); ! 84: close(childout[1]); ! 85: if(execle(SHELL, "sh", (char *)0, environ) < 0) ! 86: perror("exec"); ! 87: _exit(1); ! 88: } ! 89: } else ! 90: ldest = dest; ! 91: src++; ! 92: break; ! 93: } ! 94: *dest = 0; ! 95: } ! 96: ! 97: writeout(fd, b, e) ! 98: register char *b, *e; ! 99: { ! 100: char buf[BIGBLOCK]; ! 101: register char *s; ! 102: ! 103: for(s = buf; b < e;){ ! 104: if(s >= &buf[BIGBLOCK-1]){ ! 105: SYNERR(-1); Fprint(2, "too much text (%d chars) for `expr`\n", s-buf); ! 106: Exit(); ! 107: } ! 108: if(*b&EBIT){ ! 109: *s++ = '\\'; ! 110: *s++ = (~EBIT) & *b++; ! 111: } else ! 112: *s++ = *b++; ! 113: } ! 114: write(fd, buf, s-buf); ! 115: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.