Changeset 1207 in kBuild for trunk/src/kash
- Timestamp:
- Oct 7, 2007 5:09:24 PM (17 years ago)
- Location:
- trunk/src/kash
- Files:
-
- 22 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/Makefile.kmk
r1206 r1207 36 36 error.c \ 37 37 eval.c \ 38 exec.c \ 39 expand.c \ 38 40 histedit.c \ 41 input.c \ 42 jobs.c \ 39 43 mail.c \ 40 44 memalloc.c \ 41 45 mystring.c \ 42 46 options.c \ 47 output.c \ 43 48 show.c \ 44 49 syntax.c \ 45 output.c \46 input.c \47 exec.c \48 expand.c \49 50 miscbltin.c \ 50 51 bltin/echo.c \ 51 52 bltin/test.c \ 52 53 \ 53 jobs.c \54 54 parser.c \ 55 55 redir.c \ -
trunk/src/kash/bltin/kill.c
r1202 r1207 68 68 #define main killcmd 69 69 #include "bltin/bltin.h" 70 #include "jobs.h" 70 71 #endif /* SHELL */ 71 72 … … 153 154 for (errors = 0; argc; argc--, argv++) { 154 155 #ifdef SHELL 155 extern int getjobpgrp(shinstance *psh, const char *);156 156 if (*argv[0] == '%') { 157 157 pid = getjobpgrp(psh, *argv); -
trunk/src/kash/error.c
r1199 r1207 164 164 TRACE((psh, "exverror(%d, \"", cond)); 165 165 TRACEV((psh, msg, ap)); 166 TRACE((psh, "\") pid=%d\n", getpid()));166 TRACE((psh, "\") pid=%d\n", sh_getpid(psh))); 167 167 } else 168 TRACE((psh, "exverror(%d, NULL) pid=%d\n", cond, getpid()));168 TRACE((psh, "exverror(%d, NULL) pid=%d\n", cond, sh_getpid(psh))); 169 169 #endif 170 170 if (msg) -
trunk/src/kash/eval.c
r1203 r1207 234 234 #endif 235 235 TRACE((psh, "pid %d, evaltree(%p: %d, %d) called\n", 236 getpid(), n, n->type, flags));236 sh_getpid(psh), n, n->type, flags)); 237 237 switch (n->type) { 238 238 case NSEMI: -
trunk/src/kash/exec.c
r1203 r1207 676 676 /* XXX this code stops root executing stuff, and is buggy 677 677 if you need a group from the group list. */ 678 if (statb.st_uid == geteuid()) {678 if (statb.st_uid == sh_geteuid(psh)) { 679 679 if ((statb.st_mode & 0100) == 0) 680 680 goto loop; 681 } else if (statb.st_gid == getegid()) {681 } else if (statb.st_gid == sh_getegid(psh)) { 682 682 if ((statb.st_mode & 010) == 0) 683 683 goto loop; -
trunk/src/kash/expand.c
r1203 r1207 453 453 if (in.fd < 0) 454 454 break; 455 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);455 while ((i = shfile_read(&psh->fdtab, in.fd, buf, sizeof buf)) < 0 && errno == EINTR); 456 456 TRACE((psh, "expbackq: read returns %d\n", i)); 457 457 if (i <= 0) … … 474 474 475 475 if (in.fd >= 0) 476 close(in.fd);476 shfile_close(&psh->fdtab, in.fd); 477 477 if (in.buf) 478 478 ckfree(in.buf); … … 1192 1192 break; 1193 1193 } 1194 if (metaflag == 0 || lstat(psh->expdir, &statb) >= 0)1194 if (metaflag == 0 || shfile_lstat(&psh->fdtab, psh->expdir, &statb) >= 0) 1195 1195 addfname(psh, psh->expdir); 1196 1196 return; -
trunk/src/kash/histedit.c
r1203 r1207 389 389 error(psh, "can't create temporary file %s", editfile); 390 390 if ((efp = fdopen(fd, "w")) == NULL) { 391 close(fd);391 shfile_close(&psh->fdtab, fd); 392 392 error(psh, "can't allocate stdio buffer for temp"); 393 393 } -
trunk/src/kash/jobs.c
r1203 r1207 48 48 #include <fcntl.h> 49 49 #ifdef __sun__ 50 # define sys_siglist _sys_siglist50 # define sys_siglist _sys_siglist 51 51 #endif 52 52 #include <signal.h> … … 55 55 #include <stdlib.h> 56 56 #ifndef __sun__ 57 # include <paths.h>57 # include <paths.h> 58 58 #endif 59 59 #include <sys/types.h> 60 60 #include <sys/param.h> 61 61 #if defined(BSD) || defined(__sun__) 62 # include <sys/wait.h>63 # include <sys/time.h>64 # include <sys/resource.h>62 # include <sys/wait.h> 63 # include <sys/time.h> 64 # include <sys/resource.h> 65 65 #endif 66 66 #include <sys/ioctl.h> … … 68 68 #include "shell.h" 69 69 #if JOBS 70 #if OLD_TTY_DRIVER 71 #include "sgtty.h" 72 #else 73 #include <termios.h> 74 #endif 75 #undef CEOF /* syntax.h redefines this */ 70 # include <termios.h> 71 # undef CEOF /* syntax.h redefines this */ 76 72 #endif 77 73 #include "redir.h" … … 89 85 #include "error.h" 90 86 #include "mystring.h" 91 92 93 static struct job *jobtab; /* array of jobs */ 94 static int njobs; /* size of array */ 95 static int jobs_invalid; /* set in child */ 96 MKINIT pid_t psh->backgndpid = -1; /* pid of last background process */ 97 #if JOBS 98 int initialpgrp; /* pgrp of shell on invocation */ 99 static int curjob = -1; /* current job */ 100 #endif 101 static int ttyfd = -1; 102 103 STATIC void restartjob(struct job *); 104 STATIC void freejob(struct job *); 105 STATIC struct job *getjob(const char *, int); 106 STATIC int dowait(int, struct job *); 107 STATIC int onsigchild(void); 108 STATIC int waitproc(int, struct job *, int *); 109 STATIC void cmdtxt(union node *); 110 STATIC void cmdlist(union node *, int); 111 STATIC void cmdputs(const char *); 112 113 #ifdef OLD_TTY_DRIVER 114 static pid_t tcgetpgrp(int fd); 115 static int tcsetpgrp(int fd, pid_t pgrp); 116 117 static pid_t 118 tcgetpgrp(shinstance *psh, int fd) 119 { 120 pid_t pgrp; 121 if (shfile_ioctl(&psh->fdtab, fd, TIOCGPGRP, (char *)&pgrp) == -1) 122 return -1; 123 else 124 return pgrp; 125 } 126 127 static int 128 tcsetpgrp(shinstance *psh, int fd, pid_tpgrp) 129 { 130 return shfile_ioctl(&psh->fdtab, fd, TIOCSPGRP, (char *)&pgrp); 131 } 132 #endif 87 #include "shinstance.h" 88 89 //static struct job *jobtab; /* array of jobs */ 90 //static int njobs; /* size of array */ 91 //static int jobs_invalid; /* set in child */ 92 //MKINIT pid_t backgndpid = -1; /* pid of last background process */ 93 #if JOBS 94 //int initialpgrp; /* pgrp of shell on invocation */ 95 //static int curjob = -1; /* current job */ 96 #endif 97 //static int ttyfd = -1; 98 99 STATIC void restartjob(shinstance *, struct job *); 100 STATIC void freejob(shinstance *, struct job *); 101 STATIC struct job *getjob(shinstance *, const char *, int); 102 STATIC int dowait(shinstance *, int, struct job *); 103 STATIC int waitproc(shinstance *, int, struct job *, int *); 104 STATIC void cmdtxt(shinstance *, union node *); 105 STATIC void cmdlist(shinstance *, union node *, int); 106 STATIC void cmdputs(shinstance *, const char *); 107 133 108 134 109 /* … … 140 115 */ 141 116 142 MKINIT int jobctl;117 //MKINIT int jobctl; 143 118 144 119 void 145 setjobctl(int on) 146 { 147 #ifdef OLD_TTY_DRIVER 148 int ldisc; 149 #endif 150 151 if (on == jobctl || rootshell == 0) 120 setjobctl(shinstance *psh, int on) 121 { 122 if (on == psh->jobctl || psh->rootshell == 0) 152 123 return; 153 124 if (on) { … … 155 126 int err; 156 127 int i; 157 if ( ttyfd != -1)158 close(ttyfd);159 if (( ttyfd = open("/dev/tty", O_RDWR)) == -1) {128 if (psh->ttyfd != -1) 129 shfile_close(&psh->fdtab, psh->ttyfd); 130 if ((psh->ttyfd = shfile_open(&psh->fdtab, "/dev/tty", O_RDWR)) == -1) { 160 131 for (i = 0; i < 3; i++) { 161 if (isatty(i) && (ttyfd = dup(i)) != -1) 132 if (shfile_isatty(&psh->fdtab, i) 133 && (psh->ttyfd = shfile_dup(&psh->fdtab, i)) != -1) 162 134 break; 163 135 } … … 167 139 /* Move to a high fd */ 168 140 for (i = 10; i > 2; i--) { 169 if ((err = fcntl(ttyfd, F_DUPFD, (1 << i) - 1)) != -1)141 if ((err = shfile_fcntl(&psh->fdtab, psh->ttyfd, F_DUPFD, (1 << i) - 1)) != -1) 170 142 break; 171 143 } 172 144 if (err != -1) { 173 close(ttyfd);174 ttyfd = err;145 shfile_close(&psh->fdtab, psh->ttyfd); 146 psh->ttyfd = err; 175 147 } 176 148 #ifdef FIOCLEX 177 err = ioctl(ttyfd, FIOCLEX, 0);149 err = shfile_ioctl(&psh->fdtab, psh->ttyfd, FIOCLEX, 0); 178 150 #elif FD_CLOEXEC 179 err = fcntl(ttyfd, F_SETFD,180 fcntl(ttyfd, F_GETFD, 0) | FD_CLOEXEC);151 err = shfile_fcntl(&psh->fdtab, psh->ttyfd, F_SETFD, 152 shfile_fcntl(&psh->fdtab, psh->ttyfd, F_GETFD, 0) | FD_CLOEXEC); 181 153 #endif 182 154 if (err == -1) { 183 close(ttyfd);184 ttyfd = -1;155 shfile_close(&psh->fdtab, psh->ttyfd); 156 psh->ttyfd = -1; 185 157 goto out; 186 158 } … … 190 162 #endif 191 163 do { /* while we are in the background */ 192 if (( initialpgrp = tcgetpgrp(ttyfd)) < 0) {164 if ((psh->initialpgrp = sh_tcgetpgrp(psh, psh->ttyfd)) < 0) { 193 165 out: 194 166 out2str(psh, "sh: can't access tty; job control turned off\n"); … … 196 168 return; 197 169 } 198 if ( initialpgrp == -1)199 initialpgrp = getpgrp();200 else if ( initialpgrp != getpgrp()) {201 killpg(0, SIGTTIN);170 if (psh->initialpgrp == -1) 171 psh->initialpgrp = sh_getpgrp(psh); 172 else if (psh->initialpgrp != sh_getpgrp(psh)) { 173 sh_killpg(psh, 0, SIGTTIN); 202 174 continue; 203 175 } 204 176 } while (0); 205 177 206 #ifdef OLD_TTY_DRIVER207 if (ioctl(ttyfd, TIOCGETD, (char *)&ldisc) < 0208 || ldisc != NTTYDISC) {209 out2str(psh, "sh: need new tty driver to run job control; job control turned off\n");210 mflag(psh) = 0;211 return;212 }213 #endif214 178 setsignal(psh, SIGTSTP, 0); 215 179 setsignal(psh, SIGTTOU, 0); 216 180 setsignal(psh, SIGTTIN, 0); 217 if ( getpgid(0) != psh->rootpid && sh_setpgid(0, psh->rootpid) == -1)181 if (sh_getpgid(psh, 0) != psh->rootpid && sh_setpgid(psh, 0, psh->rootpid) == -1) 218 182 error(psh, "Cannot set process group (%s) at %d", 219 183 strerror(errno), __LINE__); 220 if ( tcsetpgrp(ttyfd, psh->rootpid) == -1)184 if (sh_tcsetpgrp(psh, psh->ttyfd, psh->rootpid) == -1) 221 185 error(psh, "Cannot set tty process group (%s) at %d", 222 186 strerror(errno), __LINE__); 223 187 } else { /* turning job control off */ 224 if ( getpgid(0) != initialpgrp && sh_setpgid(0,initialpgrp) == -1)188 if (sh_getpgid(psh, 0) != psh->initialpgrp && sh_setpgid(psh, 0, psh->initialpgrp) == -1) 225 189 error(psh, "Cannot set process group (%s) at %d", 226 190 strerror(errno), __LINE__); 227 if ( tcsetpgrp(ttyfd,initialpgrp) == -1)191 if (sh_tcsetpgrp(psh, psh->ttyfd, psh->initialpgrp) == -1) 228 192 error(psh, "Cannot set tty process group (%s) at %d", 229 193 strerror(errno), __LINE__); 230 close(ttyfd);231 ttyfd = -1;194 shfile_close(&psh->fdtab, psh->ttyfd); 195 psh->ttyfd = -1; 232 196 setsignal(psh, SIGTSTP, 0); 233 197 setsignal(psh, SIGTTOU, 0); 234 198 setsignal(psh, SIGTTIN, 0); 235 199 } 236 jobctl = on;200 psh->jobctl = on; 237 201 } 238 202 … … 261 225 262 226 nextopt(psh, ""); 263 jp = getjob( *psh->argptr, 0);227 jp = getjob(psh, *psh->argptr, 0); 264 228 if (jp->jobctl == 0) 265 229 error(psh, "job not created under job control"); … … 271 235 272 236 for (i = 0; i < jp->nprocs; i++) 273 if ( tcsetpgrp(psh,ttyfd, jp->ps[i].pid) != -1)237 if (sh_tcsetpgrp(psh, psh->ttyfd, jp->ps[i].pid) != -1) 274 238 break; 275 239 … … 286 250 287 251 static void 288 set_curjob(s truct job *jp, int mode)252 set_curjob(shinstance *psh, struct job *jp, int mode) 289 253 { 290 254 struct job *jp1, *jp2; 291 255 int i, ji; 292 256 293 ji = jp - jobtab;257 ji = (int)(jp - psh->jobtab); 294 258 295 259 /* first remove from list */ 296 if (ji == curjob)297 curjob = jp->prev_job;260 if (ji == psh->curjob) 261 psh->curjob = jp->prev_job; 298 262 else { 299 for (i = 0; i < njobs; i++) {300 if ( jobtab[i].prev_job != ji)263 for (i = 0; i < psh->njobs; i++) { 264 if (psh->jobtab[i].prev_job != ji) 301 265 continue; 302 jobtab[i].prev_job = jp->prev_job;266 psh->jobtab[i].prev_job = jp->prev_job; 303 267 break; 304 268 } … … 312 276 case 1: /* newly created job or backgrounded job, 313 277 put after all stopped jobs. */ 314 if ( curjob != -1 && jobtab[curjob].state == JOBSTOPPED) {315 for (jp1 = jobtab +curjob; ; jp1 = jp2) {278 if (psh->curjob != -1 && psh->jobtab[psh->curjob].state == JOBSTOPPED) { 279 for (jp1 = psh->jobtab + psh->curjob; ; jp1 = jp2) { 316 280 if (jp1->prev_job == -1) 317 281 break; 318 jp2 = jobtab + jp1->prev_job;282 jp2 = psh->jobtab + jp1->prev_job; 319 283 if (jp2->state != JOBSTOPPED) 320 284 break; … … 325 289 } 326 290 /* FALLTHROUGH */ 327 case 2: /* newly stopped job - becomes curjob */328 jp->prev_job = curjob;329 curjob = ji;291 case 2: /* newly stopped job - becomes psh->curjob */ 292 jp->prev_job = psh->curjob; 293 psh->curjob = ji; 330 294 break; 331 295 } … … 333 297 334 298 int 335 bgcmd( int argc, char **argv)299 bgcmd(shinstance *psh, int argc, char **argv) 336 300 { 337 301 struct job *jp; … … 343 307 if (jp->jobctl == 0) 344 308 error(psh, "job not created under job control"); 345 set_curjob( jp, 1);346 out1fmt(psh, "[%ld] %s", (long)(jp - jobtab + 1), jp->ps[0].cmd);309 set_curjob(psh, jp, 1); 310 out1fmt(psh, "[%ld] %s", (long)(jp - psh->jobtab + 1), jp->ps[0].cmd); 347 311 for (i = 1; i < jp->nprocs; i++) 348 312 out1fmt(psh, " | %s", jp->ps[i].cmd ); 349 313 out1c(psh, '\n'); 350 314 output_flushall(psh); 351 restartjob( jp);315 restartjob(psh, jp); 352 316 } while (*psh->argptr && *++psh->argptr); 353 317 return 0; … … 356 320 357 321 STATIC void 358 restartjob(s truct job *jp)322 restartjob(shinstance *psh, struct job *jp) 359 323 { 360 324 struct procstat *ps; … … 365 329 INTOFF; 366 330 for (i = 0; i < jp->nprocs; i++) 367 if ( killpg(jp->ps[i].pid, SIGCONT) != -1)331 if (sh_killpg(psh, jp->ps[i].pid, SIGCONT) != -1) 368 332 break; 369 333 if (i >= jp->nprocs) … … 380 344 381 345 static void 382 showjob(s truct output *out, struct job *jp, int mode)346 showjob(shinstance *psh, struct output *out, struct job *jp, int mode) 383 347 { 384 348 int procno; 385 349 int st; 386 350 struct procstat *ps; 387 int col;351 size_t col; 388 352 char s[64]; 389 353 … … 425 389 if (mode & SHOW_SIGNALLED && !(mode & SHOW_ISSIG)) { 426 390 if (jp->state == JOBDONE && !(mode & SHOW_NO_FREE)) { 427 TRACE((psh, "showjob: freeing job %d\n", jp - jobtab + 1));428 freejob( jp);391 TRACE((psh, "showjob: freeing job %d\n", jp - psh->jobtab + 1)); 392 freejob(psh, jp); 429 393 } 430 394 return; … … 434 398 if (ps == jp->ps) 435 399 fmtstr(s, 16, "[%ld] %c ", 436 (long)(jp - jobtab + 1),437 #if JOBS 438 jp == jobtab +curjob ? '+' :439 curjob != -1 && jp ==jobtab +440 jobtab[curjob].prev_job ? '-' :400 (long)(jp - psh->jobtab + 1), 401 #if JOBS 402 jp == psh->jobtab + psh->curjob ? '+' : 403 psh->curjob != -1 && jp == psh->jobtab + 404 psh->jobtab[psh->curjob].prev_job ? '-' : 441 405 #endif 442 406 ' '); … … 494 458 jp->changed = 0; 495 459 if (jp->state == JOBDONE && !(mode & SHOW_NO_FREE)) 496 freejob( jp);460 freejob(psh, jp); 497 461 } 498 462 499 463 500 464 int 501 jobscmd( int argc, char **argv)465 jobscmd(shinstance *psh, int argc, char **argv) 502 466 { 503 467 int mode, m; 504 int sv = jobs_invalid;505 506 jobs_invalid = 0;468 int sv = psh->jobs_invalid; 469 470 psh->jobs_invalid = 0; 507 471 mode = 0; 508 472 while ((m = nextopt(psh, "lp"))) … … 513 477 if (*psh->argptr) 514 478 do 515 showjob( out1, getjob(*psh->argptr,0), mode);479 showjob(psh, psh->out1, getjob(psh, *psh->argptr,0), mode); 516 480 while (*++psh->argptr); 517 481 else 518 showjobs(psh, out1, mode);519 jobs_invalid = sv;482 showjobs(psh, psh->out1, mode); 483 psh->jobs_invalid = sv; 520 484 return 0; 521 485 } … … 532 496 533 497 void 534 showjobs(s truct output *out, int mode)498 showjobs(shinstance *psh, struct output *out, int mode) 535 499 { 536 500 int jobno; … … 541 505 542 506 /* If not even one one job changed, there is nothing to do */ 543 gotpid = dowait( 0, NULL);544 while (dowait( 0, NULL) > 0)507 gotpid = dowait(psh, 0, NULL); 508 while (dowait(psh, 0, NULL) > 0) 545 509 continue; 546 510 #ifdef JOBS … … 549 513 * put us in it. 550 514 */ 551 if (mflag(psh) && gotpid != -1 && tcgetpgrp(ttyfd) != getpid()) {552 if ( tcsetpgrp(ttyfd, getpid()) == -1)515 if (mflag(psh) && gotpid != -1 && sh_tcgetpgrp(psh, psh->ttyfd) != sh_getpid(psh)) { 516 if (sh_tcsetpgrp(psh, psh->ttyfd, sh_getpid(psh)) == -1) 553 517 error(psh, "Cannot set tty process group (%s) at %d", 554 518 strerror(errno), __LINE__); … … 557 521 } 558 522 #endif 559 if ( jobs_invalid)523 if (psh->jobs_invalid) 560 524 return; 561 525 562 for (jobno = 1, jp = jobtab ; jobno <=njobs ; jobno++, jp++) {526 for (jobno = 1, jp = psh->jobtab ; jobno <= psh->njobs ; jobno++, jp++) { 563 527 if (!jp->used) 564 528 continue; 565 529 if (jp->nprocs == 0) { 566 freejob( jp);530 freejob(psh, jp); 567 531 continue; 568 532 } … … 573 537 continue; 574 538 } 575 showjob( out, jp, mode);539 showjob(psh, out, jp, mode); 576 540 } 577 541 } … … 582 546 583 547 STATIC void 584 freejob(s truct job *jp)548 freejob(shinstance *psh, struct job *jp) 585 549 { 586 550 INTOFF; … … 592 556 jp->used = 0; 593 557 #if JOBS 594 set_curjob( jp, 0);558 set_curjob(psh, jp, 0); 595 559 #endif 596 560 INTON; … … 600 564 601 565 int 602 waitcmd( int argc, char **argv)566 waitcmd(shinstance *psh, int argc, char **argv) 603 567 { 604 568 struct job *job; … … 610 574 if (!*psh->argptr) { 611 575 /* wait for all jobs */ 612 jp = jobtab;613 if ( jobs_invalid)576 jp = psh->jobtab; 577 if (psh->jobs_invalid) 614 578 return 0; 615 579 for (;;) { 616 if (jp >= jobtab +njobs) {580 if (jp >= psh->jobtab + psh->njobs) { 617 581 /* no running procs */ 618 582 return 0; … … 622 586 continue; 623 587 } 624 if (dowait( 1, (struct job *)NULL) == -1)588 if (dowait(psh, 1, (struct job *)NULL) == -1) 625 589 return 128 + SIGINT; 626 jp = jobtab;590 jp = psh->jobtab; 627 591 } 628 592 } … … 630 594 retval = 127; /* XXXGCC: -Wuninitialized */ 631 595 for (; *psh->argptr; psh->argptr++) { 632 job = getjob( *psh->argptr, 1);596 job = getjob(psh, *psh->argptr, 1); 633 597 if (!job) { 634 598 retval = 127; … … 637 601 /* loop until process terminated or stopped */ 638 602 while (job->state == JOBRUNNING) { 639 if (dowait( 1, (struct job *)NULL) == -1)603 if (dowait(psh, 1, (struct job *)NULL) == -1) 640 604 return 128 + SIGINT; 641 605 } … … 652 616 } 653 617 if (!iflag(psh)) 654 freejob( job);618 freejob(psh, job); 655 619 } 656 620 return retval; … … 660 624 661 625 int 662 jobidcmd( int argc, char **argv)626 jobidcmd(shinstance *psh, int argc, char **argv) 663 627 { 664 628 struct job *jp; … … 666 630 667 631 nextopt(psh, ""); 668 jp = getjob( *psh->argptr, 0);632 jp = getjob(psh, *psh->argptr, 0); 669 633 for (i = 0 ; i < jp->nprocs ; ) { 670 634 out1fmt(psh, "%ld", (long)jp->ps[i].pid); … … 675 639 676 640 int 677 getjobpgrp( const char *name)641 getjobpgrp(shinstance *psh, const char *name) 678 642 { 679 643 struct job *jp; 680 644 681 jp = getjob( name, 1);645 jp = getjob(psh, name, 1); 682 646 if (jp == 0) 683 647 return 0; … … 690 654 691 655 STATIC struct job * 692 getjob( const char *name, int noerror)656 getjob(shinstance *psh, const char *name, int noerror) 693 657 { 694 658 int jobno = -1; … … 700 664 if (name == NULL) { 701 665 #if JOBS 702 jobno = curjob;666 jobno = psh->curjob; 703 667 #endif 704 668 err_msg = "No current job"; … … 712 676 case '+': 713 677 case '%': 714 jobno = curjob;678 jobno = psh->curjob; 715 679 err_msg = "No current job"; 716 680 break; 717 681 case '-': 718 jobno = curjob;682 jobno = psh->curjob; 719 683 if (jobno != -1) 720 jobno = jobtab[jobno].prev_job;684 jobno = psh->jobtab[jobno].prev_job; 721 685 err_msg = "No previous job"; 722 686 break; … … 729 693 check_pattern: 730 694 found = NULL; 731 for (jp = jobtab, i =njobs ; --i >= 0 ; jp++) {695 for (jp = psh->jobtab, i = psh->njobs ; --i >= 0 ; jp++) { 732 696 if (!jp->used || jp->nprocs <= 0) 733 697 continue; … … 749 713 } else if (is_number(name)) { 750 714 pid = number(psh, name); 751 for (jp = jobtab, i =njobs ; --i >= 0 ; jp++) {715 for (jp = psh->jobtab, i = psh->njobs ; --i >= 0 ; jp++) { 752 716 if (jp->used && jp->nprocs > 0 753 717 && jp->ps[jp->nprocs - 1].pid == pid) … … 756 720 } 757 721 758 if (! jobs_invalid && jobno >= 0 && jobno <njobs) {759 jp = jobtab + jobno;722 if (!psh->jobs_invalid && jobno >= 0 && jobno < psh->njobs) { 723 jp = psh->jobtab + jobno; 760 724 if (jp->used) 761 725 return jp; … … 773 737 774 738 struct job * 775 makejob( union node *node, int nprocs)739 makejob(shinstance *psh, union node *node, int nprocs) 776 740 { 777 741 int i; 778 742 struct job *jp; 779 743 780 if ( jobs_invalid) {781 for (i = njobs, jp =jobtab ; --i >= 0 ; jp++) {744 if (psh->jobs_invalid) { 745 for (i = psh->njobs, jp = psh->jobtab ; --i >= 0 ; jp++) { 782 746 if (jp->used) 783 freejob( jp);784 } 785 jobs_invalid = 0;786 } 787 788 for (i = njobs, jp =jobtab ; ; jp++) {747 freejob(psh, jp); 748 } 749 psh->jobs_invalid = 0; 750 } 751 752 for (i = psh->njobs, jp = psh->jobtab ; ; jp++) { 789 753 if (--i < 0) { 790 754 INTOFF; 791 if ( njobs == 0) {792 jobtab = ckmalloc(4 * sizeofjobtab[0]);755 if (psh->njobs == 0) { 756 psh->jobtab = ckmalloc(4 * sizeof psh->jobtab[0]); 793 757 } else { 794 jp = ckmalloc(( njobs + 4) * sizeofjobtab[0]);795 memcpy(jp, jobtab,njobs * sizeof jp[0]);758 jp = ckmalloc((psh->njobs + 4) * sizeof psh->jobtab[0]); 759 memcpy(jp, psh->jobtab, psh->njobs * sizeof jp[0]); 796 760 /* Relocate `ps' pointers */ 797 for (i = 0; i < njobs; i++)798 if (jp[i].ps == & jobtab[i].ps0)761 for (i = 0; i < psh->njobs; i++) 762 if (jp[i].ps == &psh->jobtab[i].ps0) 799 763 jp[i].ps = &jp[i].ps0; 800 ckfree( jobtab);801 jobtab = jp;764 ckfree(psh->jobtab); 765 psh->jobtab = jp; 802 766 } 803 jp = jobtab +njobs;804 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);767 jp = psh->jobtab + psh->njobs; 768 for (i = 4 ; --i >= 0 ; psh->jobtab[psh->njobs++].used = 0); 805 769 INTON; 806 770 break; … … 815 779 jp->nprocs = 0; 816 780 #if JOBS 817 jp->jobctl = jobctl;818 set_curjob( jp, 1);781 jp->jobctl = psh->jobctl; 782 set_curjob(psh, jp, 1); 819 783 #endif 820 784 if (nprocs > 1) { … … 825 789 INTON; 826 790 TRACE((psh, "makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 827 jp - jobtab + 1));791 jp - psh->jobtab + 1)); 828 792 return jp; 829 793 } … … 846 810 847 811 int 848 forkshell(s truct job *jp, union node *n, int mode)812 forkshell(shinstance *psh, struct job *jp, union node *n, int mode) 849 813 { 850 814 int pid; 851 815 852 TRACE((psh, "forkshell(%%%d, %p, %d) called\n", jp - jobtab, n, mode));816 TRACE((psh, "forkshell(%%%d, %p, %d) called\n", jp - psh->jobtab, n, mode)); 853 817 switch ((pid = fork())) { 854 818 case -1: … … 856 820 INTON; 857 821 error(psh, "Cannot fork"); 858 break;822 return -1; /* won't get here */ 859 823 case 0: 860 824 forkchild(psh, jp, n, mode, 0); … … 866 830 867 831 int 868 forkparent(s truct job *jp, union node *n, int mode, pid_t pid)832 forkparent(shinstance *psh, struct job *jp, union node *n, int mode, pid_t pid) 869 833 { 870 834 int pgrp; 871 835 872 if ( rootshell && mode != FORK_NOJOB && mflag(psh)) {836 if (psh->rootshell && mode != FORK_NOJOB && mflag(psh)) { 873 837 if (jp == NULL || jp->nprocs == 0) 874 838 pgrp = pid; … … 876 840 pgrp = jp->ps[0].pid; 877 841 /* This can fail because we are doing it in the child also */ 878 (void)s etpgid(pid, pgrp);842 (void)sh_setpgid(psh, pid, pgrp); 879 843 } 880 844 if (mode == FORK_BG) 881 psh 0->backgndpid = pid; /* set $! */845 psh->backgndpid = pid; /* set $! */ 882 846 if (jp) { 883 847 struct procstat *ps = &jp->ps[jp->nprocs++]; … … 893 857 894 858 void 895 forkchild(s truct job *jp, union node *n, int mode, int vforked)859 forkchild(shinstance *psh, struct job *jp, union node *n, int mode, int vforked) 896 860 { 897 861 int wasroot; … … 900 864 const char *nullerr = "Can't open %s"; 901 865 902 wasroot = rootshell;903 TRACE((psh, "Child shell %d\n", getpid()));866 wasroot = psh->rootshell; 867 TRACE((psh, "Child shell %d\n", sh_getpid(psh))); 904 868 if (!vforked) 905 rootshell = 0;869 psh->rootshell = 0; 906 870 907 871 closescript(psh, vforked); … … 909 873 #if JOBS 910 874 if (!vforked) 911 jobctl = 0; /* do job control only in root shell */875 psh->jobctl = 0; /* do job control only in root shell */ 912 876 if (wasroot && mode != FORK_NOJOB && mflag(psh)) { 913 877 if (jp == NULL || jp->nprocs == 0) 914 pgrp = getpid();878 pgrp = sh_getpid(psh); 915 879 else 916 880 pgrp = jp->ps[0].pid; 917 881 /* This can fail because we are doing it in the parent also */ 918 (void)s etpgid(0, pgrp);882 (void)sh_setpgid(psh, 0, pgrp); 919 883 if (mode == FORK_FG) { 920 if ( tcsetpgrp(ttyfd, pgrp) == -1)884 if (sh_tcsetpgrp(psh, psh->ttyfd, pgrp) == -1) 921 885 error(psh, "Cannot set tty process group (%s) at %d", 922 886 strerror(errno), __LINE__); … … 929 893 if ((jp == NULL || jp->nprocs == 0) && 930 894 ! fd0_redirected_p(psh)) { 931 close(0);895 shfile_close(&psh->fdtab, 0); 932 896 if (open(devnull, O_RDONLY) != 0) 933 897 error(psh, nullerr, devnull); … … 940 904 if ((jp == NULL || jp->nprocs == 0) && 941 905 ! fd0_redirected_p(psh)) { 942 close(0);906 shfile_close(&psh->fdtab, 0); 943 907 if (open(devnull, O_RDONLY) != 0) 944 908 error(psh, nullerr, devnull); … … 953 917 954 918 if (!vforked) 955 jobs_invalid = 1;919 psh->jobs_invalid = 1; 956 920 } 957 921 … … 976 940 977 941 int 978 waitforjob(s truct job *jp)979 { 980 #if JOBS 981 int mypgrp = getpgrp();942 waitforjob(shinstance *psh, struct job *jp) 943 { 944 #if JOBS 945 int mypgrp = sh_getpgrp(psh); 982 946 #endif 983 947 int status; … … 985 949 986 950 INTOFF; 987 TRACE((psh, "waitforjob(%%%d) called\n", jp - jobtab + 1));951 TRACE((psh, "waitforjob(%%%d) called\n", jp - psh->jobtab + 1)); 988 952 while (jp->state == JOBRUNNING) { 989 dowait( 1, jp);953 dowait(psh, 1, jp); 990 954 } 991 955 #if JOBS 992 956 if (jp->jobctl) { 993 if ( tcsetpgrp(ttyfd, mypgrp) == -1)957 if (sh_tcsetpgrp(psh, psh->ttyfd, mypgrp) == -1) 994 958 error(psh, "Cannot set tty process group (%s) at %d", 995 959 strerror(errno), __LINE__); 996 960 } 997 if (jp->state == JOBSTOPPED && curjob != jp -jobtab)998 set_curjob( jp, 2);961 if (jp->state == JOBSTOPPED && psh->curjob != jp - psh->jobtab) 962 set_curjob(psh, jp, 2); 999 963 #endif 1000 964 status = jp->ps[jp->nprocs - 1].status; … … 1009 973 st = WTERMSIG(status) + 128; 1010 974 TRACE((psh, "waitforjob: job %d, nproc %d, status %x, st %x\n", 1011 jp - jobtab + 1, jp->nprocs, status, st ));975 jp - psh->jobtab + 1, jp->nprocs, status, st )); 1012 976 #if JOBS 1013 977 if (jp->jobctl) { … … 1025 989 #endif 1026 990 if (! JOBS || jp->state == JOBDONE) 1027 freejob( jp);991 freejob(psh, jp); 1028 992 INTON; 1029 993 return st; … … 1037 1001 1038 1002 STATIC int 1039 dowait( int block, struct job *job)1003 dowait(shinstance *psh, int block, struct job *job) 1040 1004 { 1041 1005 int pid; … … 1046 1010 int done; 1047 1011 int stopped; 1048 extern volatile char gotsig[];1049 1012 1050 1013 TRACE((psh, "dowait(%d) called\n", block)); 1051 1014 do { 1052 pid = waitproc( block, job, &status);1015 pid = waitproc(psh, block, job, &status); 1053 1016 TRACE((psh, "wait returns pid %d, status %d\n", pid, status)); 1054 } while (pid == -1 && errno == EINTR && gotsig[SIGINT - 1] == 0);1017 } while (pid == -1 && errno == EINTR && psh->gotsig[SIGINT - 1] == 0); 1055 1018 if (pid <= 0) 1056 1019 return pid; 1057 1020 INTOFF; 1058 1021 thisjob = NULL; 1059 for (jp = jobtab ; jp < jobtab +njobs ; jp++) {1022 for (jp = psh->jobtab ; jp < psh->jobtab + psh->njobs ; jp++) { 1060 1023 if (jp->used) { 1061 1024 done = 1; … … 1065 1028 continue; 1066 1029 if (sp->pid == pid) { 1067 TRACE((psh, "Job %d: changing status of proc %d from 0x%x to 0x%x\n", jp - jobtab + 1, pid, sp->status, status));1030 TRACE((psh, "Job %d: changing status of proc %d from 0x%x to 0x%x\n", jp - psh->jobtab + 1, pid, sp->status, status)); 1068 1031 sp->status = status; 1069 1032 thisjob = jp; … … 1077 1040 int state = done ? JOBDONE : JOBSTOPPED; 1078 1041 if (jp->state != state) { 1079 TRACE((psh, "Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));1042 TRACE((psh, "Job %d: changing state from %d to %d\n", jp - psh->jobtab + 1, jp->state, state)); 1080 1043 jp->state = state; 1081 1044 #if JOBS 1082 1045 if (done) 1083 set_curjob( jp, 0);1046 set_curjob(psh, jp, 0); 1084 1047 #endif 1085 1048 } … … 1090 1053 if (thisjob && thisjob->state != JOBRUNNING) { 1091 1054 int mode = 0; 1092 if (! rootshell || !iflag(psh))1055 if (!psh->rootshell || !iflag(psh)) 1093 1056 mode = SHOW_SIGNALLED; 1094 1057 if (job == thisjob) 1095 1058 mode = SHOW_SIGNALLED | SHOW_NO_FREE; 1096 1059 if (mode) 1097 showjob(psh ->out2, thisjob, mode);1060 showjob(psh, psh->out2, thisjob, mode); 1098 1061 else { 1099 1062 TRACE((psh, "Not printing status, rootshell=%d, job=%p\n", 1100 rootshell, job));1063 psh->rootshell, job)); 1101 1064 thisjob->changed = 1; 1102 1065 } … … 1113 1076 * stopped processes. If block is zero, we return a value of zero 1114 1077 * rather than blocking. 1115 *1116 * System V doesn't have a non-blocking wait system call. It does1117 * have a SIGCLD signal that is sent to a process when one of it's1118 * children dies. The obvious way to use SIGCLD would be to install1119 * a handler for SIGCLD which simply bumped a counter when a SIGCLD1120 * was received, and have waitproc bump another counter when it got1121 * the status of a process. Waitproc would then know that a wait1122 * system call would not block if the two counters were different.1123 * This approach doesn't work because if a process has children that1124 * have not been waited for, System V will send it a SIGCLD when it1125 * installs a signal handler for SIGCLD. What this means is that when1126 * a child exits, the shell will be sent SIGCLD signals continuously1127 * until is runs out of stack space, unless it does a wait call before1128 * restoring the signal handler. The code below takes advantage of1129 * this (mis)feature by installing a signal handler for SIGCLD and1130 * then checking to see whether it was called. If there are any1131 * children to be waited for, it will be.1132 *1133 * If neither SYSV nor BSD is defined, we don't implement nonblocking1134 * waits at all. In this case, the user will not be informed when1135 * a background process until the next time she runs a real program1136 * (as opposed to running a builtin command or just typing return),1137 * and the jobs command may give out of date information.1138 1078 */ 1139 1140 #ifdef SYSV1141 STATIC int gotsigchild;1142 1143 STATIC int onsigchild() {1144 gotsigchild = 1;1145 }1146 #endif1147 1148 1149 1079 STATIC int 1150 waitproc(int block, struct job *jp, int *status) 1151 { 1152 #ifdef BSD 1080 waitproc(shinstance *psh, int block, struct job *jp, int *status) 1081 { 1153 1082 int flags = 0; 1154 1083 … … 1159 1088 if (block == 0) 1160 1089 flags |= WNOHANG; 1161 return wait3(status, flags, (struct rusage *)NULL); 1162 #else 1163 #ifdef SYSV 1164 int (*save)(); 1165 1166 if (block == 0) { 1167 gotsigchild = 0; 1168 save = signal(SIGCLD, onsigchild); 1169 signal(SIGCLD, save); 1170 if (gotsigchild == 0) 1171 return 0; 1172 } 1173 return wait(status); 1174 #else 1175 if (block == 0) 1176 return 0; 1177 return wait(status); 1178 #endif 1179 #endif 1090 return sh_waitpid(psh, -1, status, flags); 1180 1091 } 1181 1092 … … 1183 1094 * return 1 if there are stopped jobs, otherwise 0 1184 1095 */ 1185 int job_warning = 0;1096 //int job_warning = 0; 1186 1097 int 1187 stoppedjobs( void)1098 stoppedjobs(shinstance *psh) 1188 1099 { 1189 1100 int jobno; 1190 1101 struct job *jp; 1191 1102 1192 if ( job_warning ||jobs_invalid)1103 if (psh->job_warning || psh->jobs_invalid) 1193 1104 return (0); 1194 for (jobno = 1, jp = jobtab; jobno <=njobs; jobno++, jp++) {1105 for (jobno = 1, jp = psh->jobtab; jobno <= psh->njobs; jobno++, jp++) { 1195 1106 if (jp->used == 0) 1196 1107 continue; 1197 1108 if (jp->state == JOBSTOPPED) { 1198 1109 out2str(psh, "You have stopped jobs.\n"); 1199 job_warning = 2;1110 psh->job_warning = 2; 1200 1111 return (1); 1201 1112 } … … 1210 1121 */ 1211 1122 1212 STATIC char *cmdnextc;1213 STATIC int cmdnleft;1123 //STATIC char *cmdnextc; 1124 //STATIC int cmdnleft; 1214 1125 1215 1126 void 1216 commandtext(s truct procstat *ps, union node *n)1127 commandtext(shinstance *psh, struct procstat *ps, union node *n) 1217 1128 { 1218 1129 int len; 1219 1130 1220 cmdnextc = ps->cmd;1221 if (iflag(psh) || mflag(psh) || sizeof ps->cmd< 100)1131 psh->cmdnextc = ps->cmd; 1132 if (iflag(psh) || mflag(psh) || sizeof(ps->cmd) < 100) 1222 1133 len = sizeof(ps->cmd); 1223 1134 else 1224 1135 len = sizeof(ps->cmd) / 10; 1225 cmdnleft = len;1226 cmdtxt( n);1227 if ( cmdnleft <= 0) {1136 psh->cmdnleft = len; 1137 cmdtxt(psh, n); 1138 if (psh->cmdnleft <= 0) { 1228 1139 char *p = ps->cmd + len - 4; 1229 1140 p[0] = '.'; … … 1232 1143 p[3] = 0; 1233 1144 } else 1234 * cmdnextc = '\0';1145 *psh->cmdnextc = '\0'; 1235 1146 TRACE((psh, "commandtext: ps->cmd %x, end %x, left %d\n\t\"%s\"\n", 1236 ps->cmd, cmdnextc,cmdnleft, ps->cmd));1147 ps->cmd, psh->cmdnextc, psh->cmdnleft, ps->cmd)); 1237 1148 } 1238 1149 1239 1150 1240 1151 STATIC void 1241 cmdtxt( union node *n)1152 cmdtxt(shinstance *psh, union node *n) 1242 1153 { 1243 1154 union node *np; … … 1247 1158 char s[2]; 1248 1159 1249 if (n == NULL || cmdnleft <= 0)1160 if (n == NULL || psh->cmdnleft <= 0) 1250 1161 return; 1251 1162 switch (n->type) { 1252 1163 case NSEMI: 1253 cmdtxt( n->nbinary.ch1);1254 cmdputs( "; ");1255 cmdtxt( n->nbinary.ch2);1164 cmdtxt(psh, n->nbinary.ch1); 1165 cmdputs(psh, "; "); 1166 cmdtxt(psh, n->nbinary.ch2); 1256 1167 break; 1257 1168 case NAND: 1258 cmdtxt( n->nbinary.ch1);1259 cmdputs( " && ");1260 cmdtxt( n->nbinary.ch2);1169 cmdtxt(psh, n->nbinary.ch1); 1170 cmdputs(psh, " && "); 1171 cmdtxt(psh, n->nbinary.ch2); 1261 1172 break; 1262 1173 case NOR: 1263 cmdtxt( n->nbinary.ch1);1264 cmdputs( " || ");1265 cmdtxt( n->nbinary.ch2);1174 cmdtxt(psh, n->nbinary.ch1); 1175 cmdputs(psh, " || "); 1176 cmdtxt(psh, n->nbinary.ch2); 1266 1177 break; 1267 1178 case NPIPE: 1268 1179 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 1269 cmdtxt( lp->n);1180 cmdtxt(psh, lp->n); 1270 1181 if (lp->next) 1271 cmdputs( " | ");1182 cmdputs(psh, " | "); 1272 1183 } 1273 1184 break; 1274 1185 case NSUBSHELL: 1275 cmdputs( "(");1276 cmdtxt( n->nredir.n);1277 cmdputs( ")");1186 cmdputs(psh, "("); 1187 cmdtxt(psh, n->nredir.n); 1188 cmdputs(psh, ")"); 1278 1189 break; 1279 1190 case NREDIR: 1280 1191 case NBACKGND: 1281 cmdtxt( n->nredir.n);1192 cmdtxt(psh, n->nredir.n); 1282 1193 break; 1283 1194 case NIF: 1284 cmdputs( "if ");1285 cmdtxt( n->nif.test);1286 cmdputs( "; then ");1287 cmdtxt( n->nif.ifpart);1195 cmdputs(psh, "if "); 1196 cmdtxt(psh, n->nif.test); 1197 cmdputs(psh, "; then "); 1198 cmdtxt(psh, n->nif.ifpart); 1288 1199 if (n->nif.elsepart) { 1289 cmdputs( "; else ");1290 cmdtxt( n->nif.elsepart);1291 } 1292 cmdputs( "; fi");1200 cmdputs(psh, "; else "); 1201 cmdtxt(psh, n->nif.elsepart); 1202 } 1203 cmdputs(psh, "; fi"); 1293 1204 break; 1294 1205 case NWHILE: 1295 cmdputs( "while ");1206 cmdputs(psh, "while "); 1296 1207 goto until; 1297 1208 case NUNTIL: 1298 cmdputs( "until ");1209 cmdputs(psh, "until "); 1299 1210 until: 1300 cmdtxt( n->nbinary.ch1);1301 cmdputs( "; do ");1302 cmdtxt( n->nbinary.ch2);1303 cmdputs( "; done");1211 cmdtxt(psh, n->nbinary.ch1); 1212 cmdputs(psh, "; do "); 1213 cmdtxt(psh, n->nbinary.ch2); 1214 cmdputs(psh, "; done"); 1304 1215 break; 1305 1216 case NFOR: 1306 cmdputs( "for ");1307 cmdputs( n->nfor.var);1308 cmdputs( " in ");1309 cmdlist( n->nfor.args, 1);1310 cmdputs( "; do ");1311 cmdtxt( n->nfor.body);1312 cmdputs( "; done");1217 cmdputs(psh, "for "); 1218 cmdputs(psh, n->nfor.var); 1219 cmdputs(psh, " in "); 1220 cmdlist(psh, n->nfor.args, 1); 1221 cmdputs(psh, "; do "); 1222 cmdtxt(psh, n->nfor.body); 1223 cmdputs(psh, "; done"); 1313 1224 break; 1314 1225 case NCASE: 1315 cmdputs( "case ");1316 cmdputs( n->ncase.expr->narg.text);1317 cmdputs( " in ");1226 cmdputs(psh, "case "); 1227 cmdputs(psh, n->ncase.expr->narg.text); 1228 cmdputs(psh, " in "); 1318 1229 for (np = n->ncase.cases; np; np = np->nclist.next) { 1319 cmdtxt( np->nclist.pattern);1320 cmdputs( ") ");1321 cmdtxt( np->nclist.body);1322 cmdputs( ";; ");1323 } 1324 cmdputs( "esac");1230 cmdtxt(psh, np->nclist.pattern); 1231 cmdputs(psh, ") "); 1232 cmdtxt(psh, np->nclist.body); 1233 cmdputs(psh, ";; "); 1234 } 1235 cmdputs(psh, "esac"); 1325 1236 break; 1326 1237 case NDEFUN: 1327 cmdputs( n->narg.text);1328 cmdputs( "() { ... }");1238 cmdputs(psh, n->narg.text); 1239 cmdputs(psh, "() { ... }"); 1329 1240 break; 1330 1241 case NCMD: 1331 cmdlist( n->ncmd.args, 1);1332 cmdlist( n->ncmd.redirect, 0);1242 cmdlist(psh, n->ncmd.args, 1); 1243 cmdlist(psh, n->ncmd.redirect, 0); 1333 1244 break; 1334 1245 case NARG: 1335 cmdputs( n->narg.text);1246 cmdputs(psh, n->narg.text); 1336 1247 break; 1337 1248 case NTO: … … 1353 1264 s[0] = n->nfile.fd + '0'; 1354 1265 s[1] = '\0'; 1355 cmdputs( s);1356 } 1357 cmdputs(p );1266 cmdputs(psh, s); 1267 } 1268 cmdputs(psh, p); 1358 1269 if (n->type == NTOFD || n->type == NFROMFD) { 1359 1270 s[0] = n->ndup.dupfd + '0'; 1360 1271 s[1] = '\0'; 1361 cmdputs( s);1272 cmdputs(psh, s); 1362 1273 } else { 1363 cmdtxt( n->nfile.fname);1274 cmdtxt(psh, n->nfile.fname); 1364 1275 } 1365 1276 break; 1366 1277 case NHERE: 1367 1278 case NXHERE: 1368 cmdputs( "<<...");1279 cmdputs(psh, "<<..."); 1369 1280 break; 1370 1281 default: 1371 cmdputs( "???");1282 cmdputs(psh, "???"); 1372 1283 break; 1373 1284 } … … 1375 1286 1376 1287 STATIC void 1377 cmdlist( union node *np, int sep)1288 cmdlist(shinstance *psh, union node *np, int sep) 1378 1289 { 1379 1290 for (; np; np = np->narg.next) { 1380 1291 if (!sep) 1381 cmdputs( " ");1382 cmdtxt( np);1292 cmdputs(psh, " "); 1293 cmdtxt(psh, np); 1383 1294 if (sep && np->narg.next) 1384 cmdputs( " ");1295 cmdputs(psh, " "); 1385 1296 } 1386 1297 } … … 1388 1299 1389 1300 STATIC void 1390 cmdputs( const char *s)1301 cmdputs(shinstance *psh, const char *s) 1391 1302 { 1392 1303 const char *p, *str = 0; … … 1400 1311 1401 1312 p = s; 1402 nextc = cmdnextc;1403 nleft = cmdnleft;1313 nextc = psh->cmdnextc; 1314 nleft = psh->cmdnleft; 1404 1315 while (nleft > 0 && (c = *p++) != 0) { 1405 1316 switch (c) { … … 1480 1391 nleft--; 1481 1392 } 1482 cmdnleft = nleft;1483 cmdnextc = nextc;1484 } 1393 psh->cmdnleft = nleft; 1394 psh->cmdnextc = nextc; 1395 } -
trunk/src/kash/main.c
r1199 r1207 222 222 state2: 223 223 state = 3; 224 if ( getuid() == geteuid() && getgid() == getegid()) {224 if (sh_getuid(psh) == sh_geteuid(psh) && sh_getgid(psh) == sh_getegid(psh)) { 225 225 if ((shinit = lookupvar(psh, "ENV")) != NULL && *shinit != '\0') { 226 226 state = 3; -
trunk/src/kash/miscbltin.c
r1204 r1207 129 129 STARTSTACKSTR(psh, p); 130 130 for (;;) { 131 if ( read(0, &c, 1) != 1) {131 if (shfile_read(&psh->fdtab, 0, &c, 1) != 1) { 132 132 status = 1; 133 133 break; … … 136 136 continue; 137 137 if (c == '\\' && !rflag) { 138 if ( read(0, &c, 1) != 1) {138 if (shfile_read(&psh->fdtab, 0, &c, 1) != 1) { 139 139 status = 1; 140 140 break; -
trunk/src/kash/mystring.c
r1198 r1207 79 79 80 80 void 81 scopyn(const char *from, char *to, int size)81 scopyn(const char *from, char *to, ssize_t size) 82 82 { 83 83 -
trunk/src/kash/mystring.h
r1198 r1207 38 38 #include "shinstance.h" 39 39 40 void scopyn(const char *, char *, int);40 void scopyn(const char *, char *, ssize_t); 41 41 int prefix(const char *, const char *); 42 42 int number(shinstance *, const char *); -
trunk/src/kash/output.c
r1199 r1207 308 308 #else 309 309 long l; 310 u _long num;310 unsigned long num; 311 311 #endif 312 312 unsigned base; -
trunk/src/kash/redir.c
r1202 r1207 138 138 INTOFF; 139 139 again: 140 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {140 if ((i = shfile_fcntl(&psh->fdtab, fd, F_DUPFD, 10)) == -1) { 141 141 switch (errno) { 142 142 case EBADF: … … 155 155 if (!try) { 156 156 sv->renamed[fd] = i; 157 close(fd);157 shfile_close(&psh->fdtab, fd); 158 158 } 159 159 INTON; 160 160 } else { 161 close(fd);161 shfile_close(&psh->fdtab, fd); 162 162 } 163 163 if (fd == 0) … … 198 198 goto eopen; 199 199 if (eflags) 200 (void) fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags);200 (void)shfile_fcntl(&psh->fdtab, f, F_SETFL, shfile_fcntl(&psh->fdtab, f, F_GETFL, 0) & ~eflags); 201 201 break; 202 202 case NFROMTO: … … 272 272 } 273 273 if (forkshell(psh, (struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { 274 close(pip[0]);274 shfile_close(&psh->fdtab, pip[0]); 275 275 signal(SIGINT, SIG_IGN); 276 276 signal(SIGQUIT, SIG_IGN); … … 287 287 } 288 288 out: 289 close(pip[1]);289 shfile_close(&psh->fdtab, pip[1]); 290 290 return pip[0]; 291 291 } … … 359 359 for (i = 0 ; i < 10 ; i++) { 360 360 if (rp->renamed[i] >= 0) { 361 close(rp->renamed[i]);361 shfile_close(&psh->fdtab, rp->renamed[i]); 362 362 } 363 363 if (!vforked) -
trunk/src/kash/shfile.h
r1206 r1207 107 107 long shfile_lseek(shfdtab *, int, long, int); 108 108 int shfile_fcntl(shfdtab *, int fd, int cmd, int arg); 109 int shfile_dup(shfdtab *, int fd); 109 110 110 111 int shfile_stat(shfdtab *, const char *, struct stat *); -
trunk/src/kash/shinstance.c
r1201 r1207 66 66 psh->out2 = &psh->errout; 67 67 68 /* jobs.c */ 69 psh->backgndpid = -1; 70 #if JOBS 71 psh->curjob = -1; 72 #endif 73 psh->ttyfd = -1; 74 68 75 } 69 76 return psh; -
trunk/src/kash/shinstance.h
r1206 r1207 29 29 30 30 #include <stdio.h> /* BUFSIZ */ 31 #include <signal.h> /* NSIG */ 31 32 32 33 #include "shtypes.h" … … 191 192 192 193 /* jobs.h */ 193 pid_t backgndpid ;/**< pid of last background process */194 pid_t backgndpid/* = -1 */; /**< pid of last background process */ 194 195 int job_warning; /**< user was warned about stopped jobs */ 196 197 /* jobs.c */ 198 struct job *jobtab; /**< array of jobs */ 199 int njobs; /**< size of array */ 200 int jobs_invalid; /**< set in child */ 201 #if JOBS 202 int initialpgrp; /**< pgrp of shell on invocation */ 203 int curjob/* = -1*/;/**< current job */ 204 #endif 205 int ttyfd/* = -1*/; 206 int jobctl; /**< job control enabled / disabled */ 207 char *cmdnextc; 208 int cmdnleft; 195 209 196 210 /* input.h */ … … 262 276 char **t_wp; 263 277 struct t_op const *t_wp_op; 278 279 /* trap.c */ 280 char gotsig[NSIG]; /**< indicates specified signal received */ 264 281 265 282 } shinstance; … … 331 348 gid_t sh_getgid(shinstance *); 332 349 gid_t sh_getegid(shinstance *); 333 334 #endif 350 pid_t sh_getpid(shinstance *); 351 pid_t sh_getpgrp(shinstance *); 352 pid_t sh_getpgid(shinstance *, pid_t); 353 int sh_setpgid(shinstance *, pid_t, pid_t); 354 int sh_killpg(shinstance *, pid_t, int); 355 356 /* tc* */ 357 pid_t sh_tcgetpgrp(shinstance *, int); 358 int sh_tcsetpgrp(shinstance *, int, pid_t); 359 360 #endif -
trunk/src/kash/show.c
r1206 r1207 290 290 if (debug(psh) != 1) 291 291 return; 292 fprintf(tracefile, "[%d] ", getpid());292 fprintf(tracefile, "[%d] ", sh_getpid(psh)); 293 293 va_start(va, fmt); 294 294 (void) vfprintf(tracefile, fmt, va); … … 303 303 if (debug(psh) != 1) 304 304 return; 305 fprintf(tracefile, "[%d] ", getpid());305 fprintf(tracefile, "[%d] ", sh_getpid(psh)); 306 306 (void) vfprintf(tracefile, fmt, va); 307 307 #endif … … 396 396 char *p; 397 397 if ((p = getenv("HOME")) == NULL) { 398 if ( geteuid() == 0)398 if (sh_geteuid(psh) == 0) 399 399 p = "/"; 400 400 else … … 421 421 } 422 422 #if defined(O_APPEND) && !defined(_MSC_VER) 423 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)424 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);423 if ((flags = shfile_fcntl(&psh->fdtab, fileno(tracefile), F_GETFL, 0)) >= 0) 424 shfile_fcntl(&psh->fdtab, fileno(tracefile), F_SETFL, flags | O_APPEND); 425 425 #endif 426 426 setvbuf(tracefile, (char *)NULL, _IOLBF, 0); //setlinebuf(tracefile); -
trunk/src/kash/shtypes.h
r1206 r1207 32 32 33 33 #ifdef _MSC_VER 34 #include <io.h> /* intptr_t and uintptr_t */ 34 # define setmode setmode_msc 35 # include <io.h> /* intptr_t and uintptr_t */ 36 # undef setmode 35 37 typedef signed char int8_t; 36 38 typedef unsigned char uint8_t; … … 72 74 typedef unsigned short uid_t; 73 75 typedef unsigned short gid_t; 76 typedef int mode_t; 77 typedef intptr_t ssize_t; 74 78 75 79 #else -
trunk/src/kash/trap.c
r1203 r1207 261 261 else 262 262 action = S_IGN; 263 if ( rootshell && !vforked && action == S_DFL) {263 if (psh->rootshell && !vforked && action == S_DFL) { 264 264 switch (signo) { 265 265 case SIGINT: … … 447 447 char *p; 448 448 449 TRACE((psh, "pid %d, exitshell(%d)\n", getpid(), status));449 TRACE((psh, "pid %d, exitshell(%d)\n", sh_getpid(psh), status)); 450 450 if (setjmp(loc1.loc)) { 451 451 goto l1; -
trunk/src/kash/var.c
r1203 r1207 272 272 vps1.next = *vpp; 273 273 *vpp = &vps1; 274 vps1.text = strdup( geteuid() ? "PS1=$ " : "PS1=# ");274 vps1.text = strdup(sh_geteuid(psh) ? "PS1=$ " : "PS1=# "); 275 275 vps1.flags = VSTRFIXED|VTEXTFIXED; 276 276 } -
trunk/src/kash/win/mscfakes.h
r1204 r1207 27 27 #ifdef _MSC_VER 28 28 29 #include "shtypes.h" 29 30 #define setmode setmode_msc 30 31 #include <sys/cdefs.h> … … 84 85 85 86 #define MAX(a,b) ((a) >= (b) ? (a) : (b)) 86 87 typedef int mode_t;88 typedef unsigned short nlink_t;89 typedef long ssize_t;90 typedef unsigned long u_long;91 typedef unsigned int u_int;92 typedef unsigned short u_short;93 87 94 88 #ifndef timerisset
Note:
See TracChangeset
for help on using the changeset viewer.