Changeset 1203 in kBuild for trunk/src/kash
- Timestamp:
- Oct 7, 2007 1:39:01 AM (17 years ago)
- Location:
- trunk/src/kash
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/Makefile.kmk
r1201 r1203 45 45 output.c \ 46 46 input.c \ 47 \48 47 exec.c \ 49 48 expand.c \ 49 \ 50 50 jobs.c \ 51 51 miscbltin.c \ -
trunk/src/kash/bltin/bltin.h
r1202 r1203 72 72 #define exit sh_exit 73 73 #define setprogname(s) 74 #define getprogname() commandname74 #define getprogname() psh->commandname 75 75 #define setlocate(l,s) 0 76 76 -
trunk/src/kash/eval.c
r1199 r1203 881 881 sh__exit(psh, 0); 882 882 } else { 883 sh__exit(psh, exerrno);883 sh__exit(psh, psh->exerrno); 884 884 } 885 885 } -
trunk/src/kash/exec.c
r1202 r1203 86 86 #endif 87 87 88 89 #define CMDTABLESIZE 31 /* should be prime */ 90 #define ARB 1 /* actual size determined at run time */ 91 92 93 94 struct tblentry { 95 struct tblentry *next; /* next entry in hash chain */ 96 union param param; /* definition of builtin function */ 97 short cmdtype; /* index identifying command */ 98 char rehash; /* if set, cd done since entry created */ 99 char cmdname[ARB]; /* name of command */ 100 }; 101 102 103 STATIC struct tblentry *cmdtable[CMDTABLESIZE]; 104 STATIC int builtinloc = -1; /* index in path of %builtin, or -1 */ 105 int exerrno = 0; /* Last exec error */ 106 107 108 STATIC void tryexec(char *, char **, char **, int, int); 109 STATIC void execinterp(char **, char **); 110 STATIC void printentry(struct tblentry *, int); 111 STATIC void clearcmdentry(int); 112 STATIC struct tblentry *cmdlookup(const char *, int); 113 STATIC void delete_cmd_entry(void); 88 #include "shinstance.h" 89 90 //#define CMDTABLESIZE 31 /* should be prime */ 91 //#define ARB 1 /* actual size determined at run time */ 92 // 93 // 94 // 95 //struct tblentry { 96 // struct tblentry *next; /* next entry in hash chain */ 97 // union param param; /* definition of builtin function */ 98 // short cmdtype; /* index identifying command */ 99 // char rehash; /* if set, cd done since entry created */ 100 // char cmdname[ARB]; /* name of command */ 101 //}; 102 // 103 // 104 //STATIC struct tblentry *cmdtable[CMDTABLESIZE]; 105 //STATIC int builtinloc = -1; /* index in path of %builtin, or -1 */ 106 //int exerrno = 0; /* Last exec error */ 107 108 109 STATIC void tryexec(shinstance *, char *, char **, char **, int, int); 110 STATIC void execinterp(shinstance *, char **, char **); 111 STATIC void printentry(shinstance *, struct tblentry *, int); 112 STATIC void clearcmdentry(shinstance *, int); 113 STATIC struct tblentry *cmdlookup(shinstance *, const char *, int); 114 STATIC void delete_cmd_entry(shinstance *); 114 115 #ifdef PC_EXE_EXTS 115 STATIC int stat_pc_exec_exts( char *fullname, struct stat *st, int has_ext);116 STATIC int stat_pc_exec_exts(shinstance *, char *fullname, struct stat *st, int has_ext); 116 117 #endif 117 118 … … 125 126 126 127 void 127 shellexec( char **argv, char **envp, const char *path, int idx, int vforked)128 shellexec(shinstance *psh, char **argv, char **envp, const char *path, int idx, int vforked) 128 129 { 129 130 char *cmdname; 130 131 int e; 131 132 #ifdef PC_EXE_EXTS 132 int has_ext = strlen(argv[0]) - 4;133 int has_ext = (int)strlen(argv[0]) - 4; 133 134 has_ext = has_ext > 0 134 135 && argv[0][has_ext] == '.' … … 148 149 cmdname = stalloc(psh, strlen(argv[0]) + 5); 149 150 strcpy(cmdname, argv[0]); 150 tryexec( cmdname, argv, envp, vforked, has_ext);151 tryexec(psh, cmdname, argv, envp, vforked, has_ext); 151 152 TRACE((psh, "shellexec: cmdname=%s\n", cmdname)); 152 153 stunalloc(psh, cmdname); … … 155 156 e = ENOENT; 156 157 while ((cmdname = padvance(psh, &path, argv[0])) != NULL) { 157 if (--idx < 0 && p athopt == NULL) {158 tryexec( cmdname, argv, envp, vforked, has_ext);158 if (--idx < 0 && psh->pathopt == NULL) { 159 tryexec(psh, cmdname, argv, envp, vforked, has_ext); 159 160 if (errno != ENOENT && errno != ENOTDIR) 160 161 e = errno; … … 167 168 switch (e) { 168 169 case EACCES: 169 exerrno = 126;170 psh->exerrno = 126; 170 171 break; 171 172 case ENOENT: 172 exerrno = 127;173 psh->exerrno = 127; 173 174 break; 174 175 default: 175 exerrno = 2;176 psh->exerrno = 2; 176 177 break; 177 178 } 178 179 TRACE((psh, "shellexec failed for '%s', errno %d, vforked %d, suppressint %d\n", 179 argv[0], e, vforked, suppressint ));180 argv[0], e, vforked, psh->suppressint )); 180 181 exerror(psh, EXEXEC, "%s: %s", argv[0], errmsg(psh, e, E_EXEC)); 181 182 /* NOTREACHED */ … … 184 185 185 186 STATIC void 186 tryexec( char *cmd, char **argv, char **envp, int vforked, int has_ext)187 tryexec(shinstance *psh, char *cmd, char **argv, char **envp, int vforked, int has_ext) 187 188 { 188 189 int e; … … 196 197 struct stat st; 197 198 if (!has_ext) 198 stat_pc_exec_exts( cmd, &st, 0);199 stat_pc_exec_exts(psh, cmd, &st, 0); 199 200 #endif 200 201 #if defined __INNOTEK_LIBC__ && defined EXEC_HASH_BANG_SCRIPT … … 204 205 #ifdef SYSV 205 206 do { 206 execve(cmd, argv, envp);207 sh_execve(psh, cmd, argv, envp); 207 208 } while (errno == EINTR); 208 209 #else 209 execve(cmd, argv, envp);210 sh_execve(psh, cmd, argv, envp); 210 211 #endif 211 212 e = errno; … … 220 221 initshellproc(psh); 221 222 setinputfile(psh, cmd, 0); 222 commandname =arg0 = savestr(argv[0]);223 psh->commandname = psh->arg0 = savestr(argv[0]); 223 224 #ifdef EXEC_HASH_BANG_SCRIPT 224 225 pgetc(psh); pungetc(psh); /* fill up input buffer */ 225 p = p arsenextc;226 if (p arsenleft > 2 && p[0] == '#' && p[1] == '!') {226 p = psh->parsenextc; 227 if (psh->parsenleft > 2 && p[0] == '#' && p[1] == '!') { 227 228 argv[0] = cmd; 228 execinterp( argv, envp);229 execinterp(psh, argv, envp); 229 230 } 230 231 #endif … … 250 251 251 252 STATIC void 252 execinterp( char **argv, char **envp)253 execinterp(shinstance *psh, char **argv, char **envp) 253 254 { 254 255 int n; … … 263 264 char **new; 264 265 265 n = p arsenleft - 2;266 inp = p arsenextc + 2;266 n = psh->parsenleft - 2; 267 inp = psh->parsenextc + 2; 267 268 ap = newargs; 268 269 for (;;) { … … 310 311 while (*ap2++ = *ap++); 311 312 TRACE((psh, "hash bang '%s'\n", new[0])); 312 shellexec(psh, new, envp, pathval( ), 0, 0);313 shellexec(psh, new, envp, pathval(psh), 0, 0); 313 314 /* NOTREACHED */ 314 315 } … … 323 324 * the possible path expansions in sequence. If an option (indicated by 324 325 * a percent sign) appears in the path entry then the global variable 325 * p athopt will be set to point to it; otherwisepathopt will be set to326 * psh->pathopt will be set to point to it; otherwise psh->pathopt will be set to 326 327 * NULL. 327 328 */ 328 329 329 const char *pathopt;330 //const char *pathopt; 330 331 331 332 char * 332 padvance( const char **path, const char *name)333 padvance(shinstance *psh, const char **path, const char *name) 333 334 { 334 335 const char *p; … … 345 346 for (p = start ; *p && *p != ':' && *p != '%' ; p++); 346 347 #endif 347 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */348 len = (int)(p - start + strlen(name) + 2); /* "2" is for '/' and '\0' */ 348 349 #ifdef PC_EXE_EXTS 349 350 len += 4; /* "4" is for .exe/.com/.cmd/.bat/.btm */ … … 358 359 } 359 360 strcpy(q, name); 360 p athopt = NULL;361 psh->pathopt = NULL; 361 362 if (*p == '%') { 362 p athopt = ++p;363 psh->pathopt = ++p; 363 364 #ifdef PC_PATH_SEP 364 365 while (*p && *p != ';') p++; … … 380 381 381 382 #ifdef PC_EXE_EXTS 382 STATIC int stat_pc_exec_exts( char *fullname, struct stat *st, int has_ext)383 STATIC int stat_pc_exec_exts(shinstance *psh, char *fullname, struct stat *st, int has_ext) 383 384 { 384 385 /* skip the SYSV crap */ 385 if (s tat(fullname, st) >= 0)386 if (shfile_stat(&psh->fdtab, fullname, st) >= 0) 386 387 return 0; 387 388 if (!has_ext && errno == ENOENT) … … 389 390 char *psz = strchr(fullname, '\0'); 390 391 memcpy(psz, ".exe", 5); 391 if (s tat(fullname, st) >= 0)392 if (shfile_stat(&psh->fdtab, fullname, st) >= 0) 392 393 return 0; 393 394 if (errno != ENOENT && errno != ENOTDIR) … … 395 396 396 397 memcpy(psz, ".cmd", 5); 397 if (s tat(fullname, st) >= 0)398 if (shfile_stat(&psh->fdtab, fullname, st) >= 0) 398 399 return 0; 399 400 if (errno != ENOENT && errno != ENOTDIR) … … 401 402 402 403 memcpy(psz, ".bat", 5); 403 if (s tat(fullname, st) >= 0)404 if (shfile_stat(&psh->fdtab, fullname, st) >= 0) 404 405 return 0; 405 406 if (errno != ENOENT && errno != ENOTDIR) … … 407 408 408 409 memcpy(psz, ".com", 5); 409 if (s tat(fullname, st) >= 0)410 if (shfile_stat(&psh->fdtab, fullname, st) >= 0) 410 411 return 0; 411 412 if (errno != ENOENT && errno != ENOTDIR) … … 413 414 414 415 memcpy(psz, ".btm", 5); 415 if (s tat(fullname, st) >= 0)416 if (shfile_stat(&psh->fdtab, fullname, st) >= 0) 416 417 return 0; 417 418 *psz = '\0'; … … 427 428 428 429 int 429 hashcmd( int argc, char **argv)430 hashcmd(shinstance *psh, int argc, char **argv) 430 431 { 431 432 struct tblentry **pp; … … 439 440 while ((c = nextopt(psh, "rv")) != '\0') { 440 441 if (c == 'r') { 441 clearcmdentry( 0);442 clearcmdentry(psh, 0); 442 443 } else if (c == 'v') { 443 444 verbose++; 444 445 } 445 446 } 446 if (* argptr == NULL) {447 for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {447 if (*psh->argptr == NULL) { 448 for (pp = psh->cmdtable ; pp < &psh->cmdtable[CMDTABLESIZE] ; pp++) { 448 449 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { 449 450 if (verbose || cmdp->cmdtype == CMDNORMAL) 450 printentry( cmdp, verbose);451 printentry(psh, cmdp, verbose); 451 452 } 452 453 } 453 454 return 0; 454 455 } 455 while ((name = * argptr) != NULL) {456 if ((cmdp = cmdlookup( name, 0)) != NULL456 while ((name = *psh->argptr) != NULL) { 457 if ((cmdp = cmdlookup(psh, name, 0)) != NULL 457 458 && (cmdp->cmdtype == CMDNORMAL 458 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))459 delete_cmd_entry( );460 find_command(psh, name, &entry, DO_ERR, pathval( ));459 || (cmdp->cmdtype == CMDBUILTIN && psh->builtinloc >= 0))) 460 delete_cmd_entry(psh); 461 find_command(psh, name, &entry, DO_ERR, pathval(psh)); 461 462 if (verbose) { 462 463 if (entry.cmdtype != CMDUNKNOWN) { /* if no error msg */ 463 cmdp = cmdlookup( name, 0);464 printentry( cmdp, verbose);464 cmdp = cmdlookup(psh, name, 0); 465 printentry(psh, cmdp, verbose); 465 466 } 466 467 output_flushall(psh); 467 468 } 468 argptr++;469 psh->argptr++; 469 470 } 470 471 return 0; … … 473 474 474 475 STATIC void 475 printentry(s truct tblentry *cmdp, int verbose)476 printentry(shinstance *psh, struct tblentry *cmdp, int verbose) 476 477 { 477 478 int idx; … … 482 483 case CMDNORMAL: 483 484 idx = cmdp->param.index; 484 path = pathval( );485 path = pathval(psh); 485 486 do { 486 487 name = padvance(psh, &path, cmdp->cmdname); … … 523 524 524 525 void 525 find_command( char *name, struct cmdentry *entry, int act, const char *path)526 find_command(shinstance *psh, char *name, struct cmdentry *entry, int act, const char *path) 526 527 { 527 528 struct tblentry *cmdp, loc_cmd; … … 531 532 struct stat statb; 532 533 int e; 533 int (*bltin)( int,char **);534 int (*bltin)(shinstance*,int,char **); 534 535 535 536 #ifdef PC_EXE_EXTS 536 int has_ext = strlen(name) - 4;537 int has_ext = (int)(strlen(name) - 4); 537 538 has_ext = has_ext > 0 538 539 && name[has_ext] == '.' … … 550 551 if (strchr(name, '/') != NULL) { 551 552 if (act & DO_ABS) { 552 while (s tat(name, &statb) < 0) {553 while (shfile_stat(&psh->fdtab, name, &statb) < 0) { 553 554 #ifdef SYSV 554 555 if (errno == EINTR) … … 570 571 } 571 572 572 if (path != pathval( ))573 if (path != pathval(psh)) 573 574 act |= DO_ALTPATH; 574 575 … … 577 578 578 579 /* If name is in the table, check answer will be ok */ 579 if ((cmdp = cmdlookup( name, 0)) != NULL) {580 if ((cmdp = cmdlookup(psh, name, 0)) != NULL) { 580 581 do { 581 582 switch (cmdp->cmdtype) { … … 593 594 break; 594 595 case CMDBUILTIN: 595 if ((act & DO_ALTBLTIN) || builtinloc >= 0) {596 if ((act & DO_ALTBLTIN) || psh->builtinloc >= 0) { 596 597 cmdp = NULL; 597 598 continue; … … 606 607 607 608 /* If %builtin not in path, check for builtin next */ 608 if ((act & DO_ALTPATH ? !(act & DO_ALTBLTIN) : builtinloc < 0) &&609 if ((act & DO_ALTPATH ? !(act & DO_ALTBLTIN) : psh->builtinloc < 0) && 609 610 (bltin = find_builtin(psh, name)) != 0) 610 611 goto builtin_success; … … 614 615 if (cmdp) { /* doing a rehash */ 615 616 if (cmdp->cmdtype == CMDBUILTIN) 616 prev = builtinloc;617 prev = psh->builtinloc; 617 618 else 618 619 prev = cmdp->param.index; … … 625 626 stunalloc(psh, fullname); 626 627 idx++; 627 if (p athopt) {628 if (prefix("builtin", p athopt)) {628 if (psh->pathopt) { 629 if (prefix("builtin", psh->pathopt)) { 629 630 if ((bltin = find_builtin(psh, name)) == 0) 630 631 goto loop; 631 632 goto builtin_success; 632 } else if (prefix("func", p athopt)) {633 } else if (prefix("func", psh->pathopt)) { 633 634 /* handled below */ 634 635 } else { … … 645 646 } 646 647 #ifdef PC_EXE_EXTS 647 while (stat_pc_exec_exts( fullname, &statb, has_ext) < 0) {648 while (stat_pc_exec_exts(psh, fullname, &statb, has_ext) < 0) { 648 649 #else 649 while (s tat(fullname, &statb) < 0) {650 while (shfile_stat(&psh->fdtab, fullname, &statb) < 0) { 650 651 #endif 651 652 #ifdef SYSV … … 661 662 if (!S_ISREG(statb.st_mode)) 662 663 goto loop; 663 if (p athopt) { /* this is a %func directory */664 if (psh->pathopt) { /* this is a %func directory */ 664 665 if (act & DO_NOFUNC) 665 666 goto loop; 666 667 stalloc(psh, strlen(fullname) + 1); 667 668 readcmdfile(psh, fullname); 668 if ((cmdp = cmdlookup( name, 0)) == NULL ||669 if ((cmdp = cmdlookup(psh, name, 0)) == NULL || 669 670 cmdp->cmdtype != CMDFUNCTION) 670 671 error(psh, "%s not defined in %s", name, fullname); … … 692 693 cmdp = &loc_cmd; 693 694 } else 694 cmdp = cmdlookup( name, 1);695 cmdp = cmdlookup(psh, name, 1); 695 696 cmdp->cmdtype = CMDNORMAL; 696 697 cmdp->param.index = idx; … … 701 702 /* We failed. If there was an entry for this command, delete it */ 702 703 if (cmdp) 703 delete_cmd_entry( );704 delete_cmd_entry(psh); 704 705 if (act & DO_ERR) 705 706 outfmt(psh->out2, "%s: %s\n", name, errmsg(psh, e, E_EXEC)); … … 712 713 cmdp = &loc_cmd; 713 714 else 714 cmdp = cmdlookup( name, 1);715 cmdp = cmdlookup(psh, name, 1); 715 716 if (cmdp->cmdtype == CMDFUNCTION) 716 717 /* DO_NOFUNC must have been set */ … … 732 733 733 734 int 734 (*find_builtin(name))(int, char **) 735 char *name; 735 (*find_builtin(shinstance *psh, char *name))(shinstance *psh, int, char **) 736 736 { 737 737 const struct builtincmd *bp; … … 745 745 746 746 int 747 (*find_splbltin(name))(int, char **) 748 char *name; 747 (*find_splbltin(shinstance *psh, char *name))(shinstance *psh, int, char **) 749 748 { 750 749 const struct builtincmd *bp; … … 765 764 766 765 void 767 hash_special_builtins( void)766 hash_special_builtins(shinstance *psh) 768 767 { 769 768 const struct builtincmd *bp; … … 771 770 772 771 for (bp = splbltincmd ; bp->name ; bp++) { 773 cmdp = cmdlookup( bp->name, 1);772 cmdp = cmdlookup(psh, bp->name, 1); 774 773 cmdp->cmdtype = CMDSPLBLTIN; 775 774 cmdp->param.bltin = bp->builtin; … … 785 784 786 785 void 787 hashcd( void)786 hashcd(shinstance *psh) 788 787 { 789 788 struct tblentry **pp; 790 789 struct tblentry *cmdp; 791 790 792 for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {791 for (pp = psh->cmdtable ; pp < &psh->cmdtable[CMDTABLESIZE] ; pp++) { 793 792 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { 794 793 if (cmdp->cmdtype == CMDNORMAL 795 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))794 || (cmdp->cmdtype == CMDBUILTIN && psh->builtinloc >= 0)) 796 795 cmdp->rehash = 1; 797 796 } … … 804 803 * Fix command hash table when PATH changed. 805 804 * Called before PATH is changed. The argument is the new value of PATH; 806 * pathval( ) still returns the old value at this point.805 * pathval(psh) still returns the old value at this point. 807 806 * Called with interrupts off. 808 807 */ 809 808 810 809 void 811 changepath( const char *newval)810 changepath(shinstance *psh, const char *newval) 812 811 { 813 812 const char *old, *new; … … 816 815 int bltin; 817 816 818 old = pathval( );817 old = pathval(psh); 819 818 new = newval; 820 819 firstchange = 9999; /* assume no change */ … … 847 846 new++, old++; 848 847 } 849 if ( builtinloc < 0 && bltin >= 0)850 builtinloc = bltin; /* zap builtins */851 if ( builtinloc >= 0 && bltin < 0)848 if (psh->builtinloc < 0 && bltin >= 0) 849 psh->builtinloc = bltin; /* zap builtins */ 850 if (psh->builtinloc >= 0 && bltin < 0) 852 851 firstchange = 0; 853 clearcmdentry( firstchange);854 builtinloc = bltin;852 clearcmdentry(psh, firstchange); 853 psh->builtinloc = bltin; 855 854 } 856 855 … … 862 861 863 862 STATIC void 864 clearcmdentry( int firstchange)863 clearcmdentry(shinstance *psh, int firstchange) 865 864 { 866 865 struct tblentry **tblp; … … 869 868 870 869 INTOFF; 871 for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {870 for (tblp = psh->cmdtable ; tblp < &psh->cmdtable[CMDTABLESIZE] ; tblp++) { 872 871 pp = tblp; 873 872 while ((cmdp = *pp) != NULL) { … … 875 874 cmdp->param.index >= firstchange) 876 875 || (cmdp->cmdtype == CMDBUILTIN && 877 builtinloc >= firstchange)) {876 psh->builtinloc >= firstchange)) { 878 877 *pp = cmdp->next; 879 878 ckfree(cmdp); … … 905 904 906 905 void 907 deletefuncs( void)906 deletefuncs(shinstance *psh) 908 907 { 909 908 struct tblentry **tblp; … … 912 911 913 912 INTOFF; 914 for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {913 for (tblp = psh->cmdtable ; tblp < &psh->cmdtable[CMDTABLESIZE] ; tblp++) { 915 914 pp = tblp; 916 915 while ((cmdp = *pp) != NULL) { … … 941 940 942 941 STATIC struct tblentry * 943 cmdlookup( const char *name, int add)942 cmdlookup(shinstance *psh, const char *name, int add) 944 943 { 945 944 int hashval; … … 953 952 hashval += *p++; 954 953 hashval &= 0x7FFF; 955 pp = & cmdtable[hashval % CMDTABLESIZE];954 pp = &psh->cmdtable[hashval % CMDTABLESIZE]; 956 955 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { 957 956 if (equal(cmdp->cmdname, name)) … … 978 977 979 978 STATIC void 980 delete_cmd_entry( void)979 delete_cmd_entry(shinstance *psh) 981 980 { 982 981 struct tblentry *cmdp; … … 993 992 #ifdef notdef 994 993 void 995 getcmdentry( char *name, struct cmdentry *entry)996 { 997 struct tblentry *cmdp = cmdlookup( name, 0);994 getcmdentry(shinstance *psh, char *name, struct cmdentry *entry) 995 { 996 struct tblentry *cmdp = cmdlookup(psh, name, 0); 998 997 999 998 if (cmdp) { … … 1014 1013 1015 1014 STATIC void 1016 addcmdentry( char *name, struct cmdentry *entry)1015 addcmdentry(shinstance *psh, char *name, struct cmdentry *entry) 1017 1016 { 1018 1017 struct tblentry *cmdp; 1019 1018 1020 1019 INTOFF; 1021 cmdp = cmdlookup( name, 1);1020 cmdp = cmdlookup(psh, name, 1); 1022 1021 if (cmdp->cmdtype != CMDSPLBLTIN) { 1023 1022 if (cmdp->cmdtype == CMDFUNCTION) { … … 1036 1035 1037 1036 void 1038 defun( char *name, union node *func)1037 defun(shinstance *psh,char *name, union node *func) 1039 1038 { 1040 1039 struct cmdentry entry; … … 1053 1052 1054 1053 int 1055 unsetfunc( char *name)1054 unsetfunc(shinstance *psh, char *name) 1056 1055 { 1057 1056 struct tblentry *cmdp; 1058 1057 1059 if ((cmdp = cmdlookup( name, 0)) != NULL &&1058 if ((cmdp = cmdlookup(psh, name, 0)) != NULL && 1060 1059 cmdp->cmdtype == CMDFUNCTION) { 1061 1060 freefunc(cmdp->param.func); 1062 delete_cmd_entry( );1061 delete_cmd_entry(psh); 1063 1062 return (0); 1064 1063 } … … 1072 1071 1073 1072 int 1074 typecmd( int argc, char **argv)1073 typecmd(shinstance *psh, int argc, char **argv) 1075 1074 { 1076 1075 struct cmdentry entry; … … 1096 1095 error(psh, "cannot specify -p with -v or -V"); 1097 1096 1098 while ((arg = * argptr++)) {1097 while ((arg = *psh->argptr++)) { 1099 1098 if (!v_flag) 1100 1099 out1str(psh, arg); … … 1121 1120 1122 1121 /* Then check if it is a tracked alias */ 1123 if ((cmdp = cmdlookup( arg, 0)) != NULL) {1122 if ((cmdp = cmdlookup(psh, arg, 0)) != NULL) { 1124 1123 entry.cmdtype = cmdp->cmdtype; 1125 1124 entry.u = cmdp->param; 1126 1125 } else { 1127 1126 /* Finally use brute force */ 1128 find_command(psh, arg, &entry, DO_ABS, pathval( ));1127 find_command(psh, arg, &entry, DO_ABS, pathval(psh)); 1129 1128 } 1130 1129 … … 1132 1131 case CMDNORMAL: { 1133 1132 if (strchr(arg, '/') == NULL) { 1134 const char *path = pathval( );1133 const char *path = pathval(psh); 1135 1134 char *name; 1136 1135 int j = entry.u.index; -
trunk/src/kash/exec.h
r884 r1203 34 34 * @(#)exec.h 8.3 (Berkeley) 6/8/95 35 35 */ 36 37 #ifndef ___exec_h 38 #define ___exec_h 36 39 37 40 /* values of cmdtype */ … … 78 81 int typecmd(struct shinstance *, int, char **); 79 82 void hash_special_builtins(struct shinstance *); 83 84 #endif -
trunk/src/kash/expand.c
r1202 r1203 78 78 #include "mystring.h" 79 79 #include "show.h" 80 81 /* 82 * Structure specifying which parts of the string should be searched 83 * for IFS characters. 84 */ 85 86 struct ifsregion { 87 struct ifsregion *next; /* next region in list */ 88 int begoff; /* offset of start of region */ 89 int endoff; /* offset of end of region */ 90 int inquotes; /* search for nul bytes only */ 91 }; 92 93 94 char *expdest; /* output of current string */ 95 struct nodelist *argbackq; /* list of back quote expressions */ 96 struct ifsregion ifsfirst; /* first struct in list of ifs regions */ 97 struct ifsregion *ifslastp; /* last struct in list */ 98 struct arglist exparg; /* holds expanded arg list */ 99 100 STATIC void argstr(char *, int); 101 STATIC char *exptilde(char *, int); 102 STATIC void expbackq(union node *, int, int); 103 STATIC int subevalvar(char *, char *, int, int, int, int); 104 STATIC char *evalvar(char *, int); 105 STATIC int varisset(char *, int); 106 STATIC void varvalue(char *, int, int, int); 107 STATIC void recordregion(int, int, int); 108 STATIC void removerecordregions(int); 109 STATIC void ifsbreakup(char *, struct arglist *); 110 STATIC void ifsfree(void); 111 STATIC void expandmeta(struct strlist *, int); 112 STATIC void expmeta(char *, char *); 113 STATIC void addfname(char *); 80 #include "shinstance.h" 81 82 ///* 83 // * Structure specifying which parts of the string should be searched 84 // * for IFS characters. 85 // */ 86 // 87 //struct ifsregion { 88 // struct ifsregion *next; /* next region in list */ 89 // int begoff; /* offset of start of region */ 90 // int endoff; /* offset of end of region */ 91 // int inquotes; /* search for nul bytes only */ 92 //}; 93 // 94 // 95 //char *expdest; /* output of current string */ 96 //struct nodelist *argbackq; /* list of back quote expressions */ 97 //struct ifsregion ifsfirst; /* first struct in list of ifs regions */ 98 //struct ifsregion *ifslastp; /* last struct in list */ 99 //struct arglist exparg; /* holds expanded arg list */ 100 101 STATIC void argstr(shinstance *, char *, int); 102 STATIC char *exptilde(shinstance *, char *, int); 103 STATIC void expbackq(shinstance *, union node *, int, int); 104 STATIC int subevalvar(shinstance *, char *, char *, int, int, int, int); 105 STATIC char *evalvar(shinstance *, char *, int); 106 STATIC int varisset(shinstance *, char *, int); 107 STATIC void varvalue(shinstance *, char *, int, int, int); 108 STATIC void recordregion(shinstance *, int, int, int); 109 STATIC void removerecordregions(shinstance *, int); 110 STATIC void ifsbreakup(shinstance *, char *, struct arglist *); 111 STATIC void ifsfree(shinstance *); 112 STATIC void expandmeta(shinstance *, struct strlist *, int); 113 STATIC void expmeta(shinstance *, char *, char *); 114 STATIC void addfname(shinstance *, char *); 114 115 STATIC struct strlist *expsort(struct strlist *); 115 116 STATIC struct strlist *msort(struct strlist *, int); 116 117 STATIC int pmatch(char *, char *, int); 117 STATIC char *cvtnum( int, char *);118 STATIC char *cvtnum(shinstance *, int, char *); 118 119 119 120 /* … … 138 139 139 140 void 140 expandarg( union node *arg, struct arglist *arglist, int flag)141 expandarg(shinstance *psh, union node *arg, struct arglist *arglist, int flag) 141 142 { 142 143 struct strlist *sp; 143 144 char *p; 144 145 145 argbackq = arg->narg.backquote;146 STARTSTACKSTR(psh, expdest);147 ifsfirst.next = NULL;148 ifslastp = NULL;149 argstr( arg->narg.text, flag);146 psh->argbackq = arg->narg.backquote; 147 STARTSTACKSTR(psh, psh->expdest); 148 psh->ifsfirst.next = NULL; 149 psh->ifslastp = NULL; 150 argstr(psh, arg->narg.text, flag); 150 151 if (arglist == NULL) { 151 152 return; /* here document expanded */ 152 153 } 153 STPUTC(psh, '\0', expdest);154 p = grabstackstr(psh, expdest);155 exparg.lastp = &exparg.list;154 STPUTC(psh, '\0', psh->expdest); 155 p = grabstackstr(psh, psh->expdest); 156 psh->exparg.lastp = &psh->exparg.list; 156 157 /* 157 158 * TODO - EXP_REDIR 158 159 */ 159 160 if (flag & EXP_FULL) { 160 ifsbreakup(p , &exparg);161 * exparg.lastp = NULL;162 exparg.lastp = &exparg.list;163 expandmeta( exparg.list, flag);161 ifsbreakup(psh, p, &psh->exparg); 162 *psh->exparg.lastp = NULL; 163 psh->exparg.lastp = &psh->exparg.list; 164 expandmeta(psh, psh->exparg.list, flag); 164 165 } else { 165 166 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ … … 167 168 sp = (struct strlist *)stalloc(psh, sizeof (struct strlist)); 168 169 sp->text = p; 169 * exparg.lastp = sp;170 exparg.lastp = &sp->next;171 } 172 ifsfree( );173 * exparg.lastp = NULL;174 if ( exparg.list) {175 *arglist->lastp = exparg.list;176 arglist->lastp = exparg.lastp;170 *psh->exparg.lastp = sp; 171 psh->exparg.lastp = &sp->next; 172 } 173 ifsfree(psh); 174 *psh->exparg.lastp = NULL; 175 if (psh->exparg.list) { 176 *arglist->lastp = psh->exparg.list; 177 arglist->lastp = psh->exparg.lastp; 177 178 } 178 179 } … … 187 188 188 189 STATIC void 189 argstr( char *p, int flag)190 argstr(shinstance *psh, char *p, int flag) 190 191 { 191 192 char c; … … 196 197 197 198 if (flag & EXP_IFS_SPLIT) 198 ifs = ifsset( ) ? ifsval() : " \t\n";199 ifs = ifsset(psh) ? ifsval(psh) : " \t\n"; 199 200 200 201 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) 201 p = exptilde(p , flag);202 p = exptilde(psh, p, flag); 202 203 for (;;) { 203 204 switch (c = *p++) { … … 210 211 break; 211 212 if ((flag & EXP_FULL) != 0) 212 STPUTC(psh, c, expdest);213 STPUTC(psh, c, psh->expdest); 213 214 ifs_split = 0; 214 215 break; … … 218 219 case CTLESC: 219 220 if (quotes) 220 STPUTC(psh, c, expdest);221 STPUTC(psh, c, psh->expdest); 221 222 c = *p++; 222 STPUTC(psh, c, expdest);223 STPUTC(psh, c, psh->expdest); 223 224 break; 224 225 case CTLVAR: 225 p = evalvar(p , (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));226 p = evalvar(psh, p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split)); 226 227 break; 227 228 case CTLBACKQ: 228 229 case CTLBACKQ|CTLQUOTE: 229 expbackq( argbackq->n, c & CTLQUOTE, flag);230 argbackq =argbackq->next;230 expbackq(psh, psh->argbackq->n, c & CTLQUOTE, flag); 231 psh->argbackq = psh->argbackq->next; 231 232 break; 232 233 case CTLENDARI: … … 239 240 * assignments (after the first '=' and after ':'s). 240 241 */ 241 STPUTC(psh, c, expdest);242 STPUTC(psh, c, psh->expdest); 242 243 if (flag & EXP_VARTILDE && *p == '~') { 243 244 if (c == '=') { … … 247 248 break; 248 249 } 249 p = exptilde(p , flag);250 p = exptilde(psh, p, flag); 250 251 } 251 252 break; 252 253 default: 253 STPUTC(psh, c, expdest);254 STPUTC(psh, c, psh->expdest); 254 255 if (flag & EXP_IFS_SPLIT & ifs_split && strchr(ifs, c) != NULL) { 255 256 /* We need to get the output split here... */ 256 recordregion( expdest - stackblock(psh) - 1,257 expdest - stackblock(psh), 0);257 recordregion(psh, (int)(psh->expdest - stackblock(psh) - 1), 258 (int)(psh->expdest - stackblock(psh)), 0); 258 259 } 259 260 break; … … 263 264 264 265 STATIC char * 265 exptilde( char *p, int flag)266 exptilde(shinstance *psh, char *p, int flag) 266 267 { 267 268 char c, *startp = p; … … 300 301 while ((c = *home++) != '\0') { 301 302 if (quotes && SQSYNTAX[(int)c] == CCTL) 302 STPUTC(psh, CTLESC, expdest);303 STPUTC(psh, c, expdest);303 STPUTC(psh, CTLESC, psh->expdest); 304 STPUTC(psh, c, psh->expdest); 304 305 } 305 306 return (p); … … 311 312 312 313 STATIC void 313 removerecordregions( int endoff)314 { 315 if ( ifslastp == NULL)314 removerecordregions(shinstance *psh, int endoff) 315 { 316 if (psh->ifslastp == NULL) 316 317 return; 317 318 318 if ( ifsfirst.endoff > endoff) {319 while ( ifsfirst.next != NULL) {319 if (psh->ifsfirst.endoff > endoff) { 320 while (psh->ifsfirst.next != NULL) { 320 321 struct ifsregion *ifsp; 321 322 INTOFF; 322 ifsp = ifsfirst.next->next;323 ckfree( ifsfirst.next);324 ifsfirst.next = ifsp;323 ifsp = psh->ifsfirst.next->next; 324 ckfree(psh->ifsfirst.next); 325 psh->ifsfirst.next = ifsp; 325 326 INTON; 326 327 } 327 if ( ifsfirst.begoff > endoff)328 ifslastp = NULL;328 if (psh->ifsfirst.begoff > endoff) 329 psh->ifslastp = NULL; 329 330 else { 330 ifslastp = &ifsfirst;331 ifsfirst.endoff = endoff;331 psh->ifslastp = &psh->ifsfirst; 332 psh->ifsfirst.endoff = endoff; 332 333 } 333 334 return; 334 335 } 335 336 336 ifslastp = &ifsfirst;337 while ( ifslastp->next &&ifslastp->next->begoff < endoff)338 ifslastp=ifslastp->next;339 while ( ifslastp->next != NULL) {337 psh->ifslastp = &psh->ifsfirst; 338 while (psh->ifslastp->next && psh->ifslastp->next->begoff < endoff) 339 psh->ifslastp=psh->ifslastp->next; 340 while (psh->ifslastp->next != NULL) { 340 341 struct ifsregion *ifsp; 341 342 INTOFF; 342 ifsp = ifslastp->next->next;343 ckfree( ifslastp->next);344 ifslastp->next = ifsp;343 ifsp = psh->ifslastp->next->next; 344 ckfree(psh->ifslastp->next); 345 psh->ifslastp->next = ifsp; 345 346 INTON; 346 347 } 347 if ( ifslastp->endoff > endoff)348 ifslastp->endoff = endoff;348 if (psh->ifslastp->endoff > endoff) 349 psh->ifslastp->endoff = endoff; 349 350 } 350 351 … … 355 356 */ 356 357 void 357 expari( int flag)358 expari(shinstance *psh, int flag) 358 359 { 359 360 char *p, *start; … … 378 379 #error "integers with more than 10 digits are not supported" 379 380 #endif 380 CHECKSTRSPACE(psh, 12 - 2, expdest);381 USTPUTC(psh, '\0', expdest);381 CHECKSTRSPACE(psh, 12 - 2, psh->expdest); 382 USTPUTC(psh, '\0', psh->expdest); 382 383 start = stackblock(psh); 383 p = expdest - 1;384 p = psh->expdest - 1; 384 385 while (*p != CTLARI && p >= start) 385 386 --p; … … 395 396 else 396 397 quoted=0; 397 begoff = p - start;398 removerecordregions( begoff);398 begoff = (int)(p - start); 399 removerecordregions(psh, begoff); 399 400 if (quotes) 400 401 rmescapes(psh, p+2); … … 406 407 407 408 if (quoted == 0) 408 recordregion( begoff, p - 1 - start, 0);409 result = expdest - p + 1;410 STADJUST(psh, -result, expdest);409 recordregion(psh, begoff, (int)(p - 1 - start), 0); 410 result = (int)(psh->expdest - p + 1); 411 STADJUST(psh, -result, psh->expdest); 411 412 } 412 413 … … 417 418 418 419 STATIC void 419 expbackq( union node *cmd, int quoted, int flag)420 expbackq(shinstance *psh, union node *cmd, int quoted, int flag) 420 421 { 421 422 struct backcmd in; … … 423 424 char buf[128]; 424 425 char *p; 425 char *dest = expdest;426 char *dest = psh->expdest; 426 427 struct ifsregion saveifs, *savelastp; 427 428 struct nodelist *saveargbackq; 428 429 char lastc; 429 int startloc = dest - stackblock(psh);430 int startloc = (int)(dest - stackblock(psh)); 430 431 char const *syntax = quoted? DQSYNTAX : BASESYNTAX; 431 432 int saveherefd; … … 433 434 434 435 INTOFF; 435 saveifs = ifsfirst;436 savelastp = ifslastp;437 saveargbackq = argbackq;436 saveifs = psh->ifsfirst; 437 savelastp = psh->ifslastp; 438 saveargbackq = psh->argbackq; 438 439 saveherefd = psh->herefd; 439 440 psh->herefd = -1; … … 441 442 evalbackcmd(psh, cmd, &in); 442 443 ungrabstackstr(psh, p, dest); 443 ifsfirst = saveifs;444 ifslastp = savelastp;445 argbackq = saveargbackq;444 psh->ifsfirst = saveifs; 445 psh->ifslastp = savelastp; 446 psh->argbackq = saveargbackq; 446 447 psh->herefd = saveherefd; 447 448 … … 477 478 ckfree(in.buf); 478 479 if (in.jp) 479 back_exitstatus = waitforjob(psh, in.jp);480 psh->back_exitstatus = waitforjob(psh, in.jp); 480 481 if (quoted == 0) 481 recordregion( startloc, dest - stackblock(psh), 0);482 recordregion(psh, startloc, (int)(dest - stackblock(psh)), 0); 482 483 TRACE((psh, "evalbackq: size=%d: \"%.*s\"\n", 483 484 (dest - stackblock(psh)) - startloc, 484 485 (dest - stackblock(psh)) - startloc, 485 486 stackblock(psh) + startloc)); 486 expdest = dest;487 psh->expdest = dest; 487 488 INTON; 488 489 } … … 491 492 492 493 STATIC int 493 subevalvar( char *p, char *str, int strloc, int subtype, int startloc, int varflags)494 subevalvar(shinstance *psh, char *p, char *str, int strloc, int subtype, int startloc, int varflags) 494 495 { 495 496 char *startp; … … 498 499 int c = 0; 499 500 int saveherefd = psh->herefd; 500 struct nodelist *saveargbackq = argbackq;501 struct nodelist *saveargbackq = psh->argbackq; 501 502 int amount; 502 503 503 504 psh->herefd = -1; 504 argstr(p , 0);505 STACKSTRNUL(psh, expdest);505 argstr(psh, p, 0); 506 STACKSTRNUL(psh, psh->expdest); 506 507 psh->herefd = saveherefd; 507 argbackq = saveargbackq;508 psh->argbackq = saveargbackq; 508 509 startp = stackblock(psh) + startloc; 509 510 if (str == NULL) … … 513 514 case VSASSIGN: 514 515 setvar(psh, str, startp, 0); 515 amount = startp - expdest;516 STADJUST(psh, amount, expdest);516 amount = (int)(startp - psh->expdest); 517 STADJUST(psh, amount, psh->expdest); 517 518 varflags &= ~VSNUL; 518 519 if (c != 0) … … 592 593 recordleft: 593 594 *loc = c; 594 amount = ( (str - 1) - (loc - startp)) - expdest;595 STADJUST(psh, amount, expdest);595 amount = (int)(((str - 1) - (loc - startp)) - psh->expdest); 596 STADJUST(psh, amount, psh->expdest); 596 597 while (loc != str - 1) 597 598 *startp++ = *loc++; … … 599 600 600 601 recordright: 601 amount = loc - expdest;602 STADJUST(psh, amount, expdest);603 STPUTC(psh, '\0', expdest);604 STADJUST(psh, -1, expdest);602 amount = (int)(loc - psh->expdest); 603 STADJUST(psh, amount, psh->expdest); 604 STPUTC(psh, '\0', psh->expdest); 605 STADJUST(psh, -1, psh->expdest); 605 606 return 1; 606 607 } … … 613 614 614 615 STATIC char * 615 evalvar( char *p, int flag)616 evalvar(shinstance *psh, char *p, int flag) 616 617 { 617 618 int subtype; … … 636 637 again: /* jump here after setting a variable with ${var=text} */ 637 638 if (special) { 638 set = varisset( var, varflags & VSNUL);639 set = varisset(psh, var, varflags & VSNUL); 639 640 val = NULL; 640 641 } else { … … 648 649 649 650 varlen = 0; 650 startloc = expdest - stackblock(psh);651 startloc = (int)(psh->expdest - stackblock(psh)); 651 652 652 653 if (!set && uflag(psh)) { … … 666 667 /* insert the value of the variable */ 667 668 if (special) { 668 varvalue( var, varflags & VSQUOTE, subtype, flag);669 varvalue(psh, var, varflags & VSQUOTE, subtype, flag); 669 670 if (subtype == VSLENGTH) { 670 varlen = expdest - stackblock(psh) - startloc;671 STADJUST(psh, -varlen, expdest);671 varlen = (int)(psh->expdest - stackblock(psh) - startloc); 672 STADJUST(psh, -varlen, psh->expdest); 672 673 } 673 674 } else { … … 681 682 while (*val) { 682 683 if (quotes && syntax[(int)*val] == CCTL) 683 STPUTC(psh, CTLESC, expdest);684 STPUTC(psh, *val++, expdest);684 STPUTC(psh, CTLESC, psh->expdest); 685 STPUTC(psh, *val++, psh->expdest); 685 686 } 686 687 … … 691 692 692 693 apply_ifs = ((varflags & VSQUOTE) == 0 || 693 (*var == '@' && shellparam.nparam != 1));694 (*var == '@' && psh->shellparam.nparam != 1)); 694 695 695 696 switch (subtype) { 696 697 case VSLENGTH: 697 expdest = cvtnum(varlen,expdest);698 psh->expdest = cvtnum(psh, varlen, psh->expdest); 698 699 break; 699 700 … … 706 707 case VSMINUS: 707 708 if (!set) { 708 argstr(p , flag | (apply_ifs ? EXP_IFS_SPLIT : 0));709 argstr(psh, p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0)); 709 710 /* 710 711 * ${x-a b c} doesn't get split, but removing the … … 727 728 * right after it 728 729 */ 729 STPUTC(psh, '\0', expdest);730 patloc = expdest - stackblock(psh);731 if (subevalvar(p , NULL, patloc, subtype,730 STPUTC(psh, '\0', psh->expdest); 731 patloc = (int)(psh->expdest - stackblock(psh)); 732 if (subevalvar(psh, p, NULL, patloc, subtype, 732 733 startloc, varflags) == 0) { 733 int amount = ( expdest - stackblock(psh) - patloc) + 1;734 STADJUST(psh, -amount, expdest);734 int amount = (int)(psh->expdest - stackblock(psh) - patloc) + 1; 735 STADJUST(psh, -amount, psh->expdest); 735 736 } 736 737 /* Remove any recorded regions beyond start of variable */ 737 removerecordregions( startloc);738 removerecordregions(psh, startloc); 738 739 apply_ifs = 1; 739 740 break; … … 743 744 if (set) 744 745 break; 745 if (subevalvar(p , var, 0, subtype, startloc, varflags)) {746 if (subevalvar(psh, p, var, 0, subtype, startloc, varflags)) { 746 747 varflags &= ~VSNUL; 747 748 /* … … 749 750 * start of variable 750 751 */ 751 removerecordregions( startloc);752 removerecordregions(psh, startloc); 752 753 goto again; 753 754 } … … 760 761 761 762 if (apply_ifs) 762 recordregion( startloc, expdest - stackblock(psh),763 recordregion(psh, startloc, (int)(psh->expdest - stackblock(psh)), 763 764 varflags & VSQUOTE); 764 765 … … 770 771 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { 771 772 if (set) 772 argbackq =argbackq->next;773 psh->argbackq = psh->argbackq->next; 773 774 } else if (c == CTLVAR) { 774 775 if ((*p++ & VSTYPE) != VSNORMAL) … … 790 791 791 792 STATIC int 792 varisset( char *name, int nulok)793 varisset(shinstance *psh, char *name, int nulok) 793 794 { 794 795 if (*name == '!') 795 return backgndpid != -1;796 return psh->backgndpid != -1; 796 797 else if (*name == '@' || *name == '*') { 797 if (* shellparam.p == NULL)798 if (*psh->shellparam.p == NULL) 798 799 return 0; 799 800 … … 801 802 char **av; 802 803 803 for (av = shellparam.p; *av; av++)804 for (av = psh->shellparam.p; *av; av++) 804 805 if (**av != '\0') 805 806 return 1; … … 810 811 int num = atoi(name); 811 812 812 if (num > shellparam.nparam)813 if (num > psh->shellparam.nparam) 813 814 return 0; 814 815 815 816 if (num == 0) 816 ap = arg0;817 ap = psh->arg0; 817 818 else 818 ap = shellparam.p[num - 1];819 ap = psh->shellparam.p[num - 1]; 819 820 820 821 if (nulok && (ap == NULL || *ap == '\0')) … … 831 832 832 833 STATIC void 833 varvalue( char *name, int quoted, int subtype, int flag)834 varvalue(shinstance *psh, char *name, int quoted, int subtype, int flag) 834 835 { 835 836 int num; … … 846 847 while (*p) { \ 847 848 if (syntax[(int)*p] == CCTL) \ 848 STPUTC(psh, CTLESC, expdest); \849 STPUTC(psh, *p++, expdest); \849 STPUTC(psh, CTLESC, psh->expdest); \ 850 STPUTC(psh, *p++, psh->expdest); \ 850 851 } \ 851 852 } else \ 852 853 while (*p) \ 853 STPUTC(psh, *p++, expdest); \854 STPUTC(psh, *p++, psh->expdest); \ 854 855 } while (0) 855 856 … … 857 858 switch (*name) { 858 859 case '$': 859 num = rootpid;860 num = psh->rootpid; 860 861 goto numvar; 861 862 case '?': 862 num = exitstatus;863 num = psh->exitstatus; 863 864 goto numvar; 864 865 case '#': 865 num = shellparam.nparam;866 num = psh->shellparam.nparam; 866 867 goto numvar; 867 868 case '!': 868 num = backgndpid;869 num = psh->backgndpid; 869 870 numvar: 870 expdest = cvtnum(num,expdest);871 psh->expdest = cvtnum(psh, num, psh->expdest); 871 872 break; 872 873 case '-': 873 for (i = 0; optlist[i].name; i++) {874 if ( optlist[i].val)875 STPUTC(psh, optlist[i].letter,expdest);874 for (i = 0; psh->optlist[i].name; i++) { 875 if (psh->optlist[i].val) 876 STPUTC(psh, psh->optlist[i].letter, psh->expdest); 876 877 } 877 878 break; 878 879 case '@': 879 880 if (flag & EXP_FULL && quoted) { 880 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {881 for (ap = psh->shellparam.p ; (p = *ap++) != NULL ; ) { 881 882 STRTODEST(p); 882 883 if (*ap) 883 STPUTC(psh, '\0', expdest);884 STPUTC(psh, '\0', psh->expdest); 884 885 } 885 886 break; … … 887 888 /* fall through */ 888 889 case '*': 889 if (ifsset( ) != 0)890 sep = ifsval( )[0];890 if (ifsset(psh) != 0) 891 sep = ifsval(psh)[0]; 891 892 else 892 893 sep = ' '; 893 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {894 for (ap = psh->shellparam.p ; (p = *ap++) != NULL ; ) { 894 895 STRTODEST(p); 895 896 if (*ap && sep) 896 STPUTC(psh, sep, expdest);897 STPUTC(psh, sep, psh->expdest); 897 898 } 898 899 break; 899 900 case '0': 900 p = arg0;901 p = psh->arg0; 901 902 STRTODEST(p); 902 903 break; … … 904 905 if (is_digit(*name)) { 905 906 num = atoi(name); 906 if (num > 0 && num <= shellparam.nparam) {907 p = shellparam.p[num - 1];907 if (num > 0 && num <= psh->shellparam.nparam) { 908 p = psh->shellparam.p[num - 1]; 908 909 STRTODEST(p); 909 910 } … … 921 922 922 923 STATIC void 923 recordregion( int start, int end, int inquotes)924 recordregion(shinstance *psh, int start, int end, int inquotes) 924 925 { 925 926 struct ifsregion *ifsp; 926 927 927 if ( ifslastp == NULL) {928 ifsp = & ifsfirst;928 if (psh->ifslastp == NULL) { 929 ifsp = &psh->ifsfirst; 929 930 } else { 930 if ( ifslastp->endoff == start931 && ifslastp->inquotes == inquotes) {931 if (psh->ifslastp->endoff == start 932 && psh->ifslastp->inquotes == inquotes) { 932 933 /* extend previous area */ 933 ifslastp->endoff = end;934 psh->ifslastp->endoff = end; 934 935 return; 935 936 } 936 937 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); 937 ifslastp->next = ifsp;938 } 939 ifslastp = ifsp;940 ifslastp->next = NULL;941 ifslastp->begoff = start;942 ifslastp->endoff = end;943 ifslastp->inquotes = inquotes;938 psh->ifslastp->next = ifsp; 939 } 940 psh->ifslastp = ifsp; 941 psh->ifslastp->next = NULL; 942 psh->ifslastp->begoff = start; 943 psh->ifslastp->endoff = end; 944 psh->ifslastp->inquotes = inquotes; 944 945 } 945 946 … … 952 953 */ 953 954 STATIC void 954 ifsbreakup( char *string, struct arglist *arglist)955 ifsbreakup(shinstance *psh, char *string, struct arglist *arglist) 955 956 { 956 957 struct ifsregion *ifsp; … … 967 968 inquotes = 0; 968 969 969 if ( ifslastp == NULL) {970 if (psh->ifslastp == NULL) { 970 971 /* Return entire argument, IFS doesn't apply to any of it */ 971 972 sp = (struct strlist *)stalloc(psh, sizeof *sp); … … 976 977 } 977 978 978 ifs = ifsset( ) ? ifsval() : " \t\n";979 980 for (ifsp = & ifsfirst; ifsp != NULL; ifsp = ifsp->next) {979 ifs = ifsset(psh) ? ifsval(psh) : " \t\n"; 980 981 for (ifsp = &psh->ifsfirst; ifsp != NULL; ifsp = ifsp->next) { 981 982 p = string + ifsp->begoff; 982 983 inquotes = ifsp->inquotes; … … 1051 1052 1052 1053 STATIC void 1053 ifsfree( void)1054 { 1055 while ( ifsfirst.next != NULL) {1054 ifsfree(shinstance *psh) 1055 { 1056 while (psh->ifsfirst.next != NULL) { 1056 1057 struct ifsregion *ifsp; 1057 1058 INTOFF; 1058 ifsp = ifsfirst.next->next;1059 ckfree( ifsfirst.next);1060 ifsfirst.next = ifsp;1059 ifsp = psh->ifsfirst.next->next; 1060 ckfree(psh->ifsfirst.next); 1061 psh->ifsfirst.next = ifsp; 1061 1062 INTON; 1062 1063 } 1063 ifslastp = NULL;1064 ifsfirst.next = NULL;1064 psh->ifslastp = NULL; 1065 psh->ifsfirst.next = NULL; 1065 1066 } 1066 1067 … … 1069 1070 /* 1070 1071 * Expand shell metacharacters. At this point, the only control characters 1071 * should be escapes. The results are stored in the list exparg.1072 */ 1073 1074 char *expdir;1072 * should be escapes. The results are stored in the list psh->exparg. 1073 */ 1074 1075 //char *expdir; 1075 1076 1076 1077 1077 1078 STATIC void 1078 expandmeta(s truct strlist *str, int flag)1079 expandmeta(shinstance *psh, struct strlist *str, int flag) 1079 1080 { 1080 1081 char *p; … … 1094 1095 break; 1095 1096 } 1096 savelastp = exparg.lastp;1097 savelastp = psh->exparg.lastp; 1097 1098 INTOFF; 1098 if ( expdir == NULL) {1099 int i = strlen(str->text);1100 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */1101 } 1102 1103 expmeta( expdir, str->text);1104 ckfree( expdir);1105 expdir = NULL;1099 if (psh->expdir == NULL) { 1100 size_t i = strlen(str->text); 1101 psh->expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ 1102 } 1103 1104 expmeta(psh, psh->expdir, str->text); 1105 ckfree(psh->expdir); 1106 psh->expdir = NULL; 1106 1107 INTON; 1107 if ( exparg.lastp == savelastp) {1108 if (psh->exparg.lastp == savelastp) { 1108 1109 /* 1109 1110 * no matches 1110 1111 */ 1111 1112 nometa: 1112 * exparg.lastp = str;1113 *psh->exparg.lastp = str; 1113 1114 rmescapes(psh, str->text); 1114 exparg.lastp = &str->next;1115 psh->exparg.lastp = &str->next; 1115 1116 } else { 1116 * exparg.lastp = NULL;1117 *psh->exparg.lastp = NULL; 1117 1118 *savelastp = sp = expsort(*savelastp); 1118 1119 while (sp->next != NULL) 1119 1120 sp = sp->next; 1120 exparg.lastp = &sp->next;1121 psh->exparg.lastp = &sp->next; 1121 1122 } 1122 1123 str = str->next; … … 1130 1131 1131 1132 STATIC void 1132 expmeta( char *enddir, char *name)1133 expmeta(shinstance *psh, char *enddir, char *name) 1133 1134 { 1134 1135 char *p; … … 1180 1181 } 1181 1182 if (metaflag == 0) { /* we've reached the end of the file name */ 1182 if (enddir != expdir)1183 if (enddir != psh->expdir) 1183 1184 metaflag++; 1184 1185 for (p = name ; ; p++) { … … 1191 1192 break; 1192 1193 } 1193 if (metaflag == 0 || lstat( expdir, &statb) >= 0)1194 addfname( expdir);1194 if (metaflag == 0 || lstat(psh->expdir, &statb) >= 0) 1195 addfname(psh, psh->expdir); 1195 1196 return; 1196 1197 } … … 1206 1207 } 1207 1208 } 1208 if (enddir == expdir) {1209 if (enddir == psh->expdir) { 1209 1210 cp = "."; 1210 } else if (enddir == expdir + 1 && *expdir == '/') {1211 } else if (enddir == psh->expdir + 1 && *psh->expdir == '/') { 1211 1212 cp = "/"; 1212 1213 } else { 1213 cp = expdir;1214 cp = psh->expdir; 1214 1215 enddir[-1] = '\0'; 1215 1216 } 1216 1217 if ((dirp = opendir(cp)) == NULL) 1217 1218 return; 1218 if (enddir != expdir)1219 if (enddir != psh->expdir) 1219 1220 enddir[-1] = '/'; 1220 1221 if (*endname == 0) { … … 1238 1239 if (atend) { 1239 1240 scopy(dp->d_name, enddir); 1240 addfname( expdir);1241 addfname(psh, psh->expdir); 1241 1242 } else { 1242 1243 for (p = enddir, cp = dp->d_name; … … 1244 1245 continue; 1245 1246 p[-1] = '/'; 1246 expmeta(p , endname);1247 expmeta(psh, p, endname); 1247 1248 } 1248 1249 } … … 1259 1260 1260 1261 STATIC void 1261 addfname( char *name)1262 addfname(shinstance *psh, char *name) 1262 1263 { 1263 1264 char *p; … … 1268 1269 sp = (struct strlist *)stalloc(psh, sizeof *sp); 1269 1270 sp->text = p; 1270 * exparg.lastp = sp;1271 exparg.lastp = &sp->next;1271 *psh->exparg.lastp = sp; 1272 psh->exparg.lastp = &sp->next; 1272 1273 } 1273 1274 … … 1339 1340 1340 1341 int 1341 patmatch( char *pattern, char *string, int squoted)1342 patmatch(shinstance *psh, char *pattern, char *string, int squoted) 1342 1343 { 1343 1344 #ifdef notdef … … 1474 1475 1475 1476 void 1476 rmescapes( char *str)1477 rmescapes(shinstance *psh, char *str) 1477 1478 { 1478 1479 char *p, *q; … … 1503 1504 1504 1505 int 1505 casematch( union node *pattern, char *val)1506 casematch(shinstance *psh, union node *pattern, char *val) 1506 1507 { 1507 1508 struct stackmark smark; … … 1510 1511 1511 1512 setstackmark(psh, &smark); 1512 argbackq = pattern->narg.backquote;1513 STARTSTACKSTR(psh, expdest);1514 ifslastp = NULL;1515 argstr(p attern->narg.text, EXP_TILDE | EXP_CASE);1516 STPUTC(psh, '\0', expdest);1517 p = grabstackstr(psh, expdest);1513 psh->argbackq = pattern->narg.backquote; 1514 STARTSTACKSTR(psh, psh->expdest); 1515 psh->ifslastp = NULL; 1516 argstr(psh, pattern->narg.text, EXP_TILDE | EXP_CASE); 1517 STPUTC(psh, '\0', psh->expdest); 1518 p = grabstackstr(psh, psh->expdest); 1518 1519 result = patmatch(psh, p, val, 0); 1519 1520 popstackmark(psh, &smark); … … 1526 1527 1527 1528 STATIC char * 1528 cvtnum( int num, char *buf)1529 cvtnum(shinstance *psh, int num, char *buf) 1529 1530 { 1530 1531 char temp[32]; -
trunk/src/kash/expand.h
r884 r1203 34 34 * @(#)expand.h 8.2 (Berkeley) 5/4/95 35 35 */ 36 37 #ifndef ___expand_h 38 #define ___expand_h 36 39 37 40 struct strlist { … … 71 74 void arith_lex_reset(void); 72 75 int yylex(void); 76 77 #endif -
trunk/src/kash/generated/init.c
r1202 r1203 210 210 /* from eval.c: */ 211 211 { 212 evalskip = 0;213 loopnest = 0;214 funcnest = 0;212 psh->evalskip = 0; 213 psh->loopnest = 0; 214 psh->funcnest = 0; 215 215 } 216 216 217 217 /* from input.c: */ 218 218 { 219 if ( exception != EXSHELLPROC)220 p arselleft =parsenleft = 0; /* clear input buffer */219 if (psh->exception != EXSHELLPROC) 220 psh->parselleft = psh->parsenleft = 0; /* clear input buffer */ 221 221 popallfiles(psh); 222 222 } … … 234 234 /* from parser.c: */ 235 235 { 236 tokpushback = 0;237 checkkwd = 0;236 psh->tokpushback = 0; 237 psh->checkkwd = 0; 238 238 } 239 239 240 240 /* from redir.c: */ 241 241 { 242 while ( redirlist)242 while (psh->redirlist) 243 243 popredir(psh); 244 244 } … … 261 261 /* from eval.c: */ 262 262 { 263 exitstatus = 0;263 psh->exitstatus = 0; 264 264 } 265 265 … … 276 276 /* from jobs.c: */ 277 277 { 278 backgndpid = -1;278 psh->backgndpid = -1; 279 279 #if JOBS 280 280 jobctl = 0; -
trunk/src/kash/histedit.c
r1202 r1203 130 130 else 131 131 unsetenv("TERM"); 132 shname = arg0;132 shname = psh->arg0; 133 133 if (shname[0] == '-') 134 134 shname++; -
trunk/src/kash/jobs.c
r1202 r1203 94 94 static int njobs; /* size of array */ 95 95 static int jobs_invalid; /* set in child */ 96 MKINIT pid_t backgndpid = -1; /* pid of last background process */96 MKINIT pid_t psh->backgndpid = -1; /* pid of last background process */ 97 97 #if JOBS 98 98 int initialpgrp; /* pgrp of shell on invocation */ … … 215 215 setsignal(psh, SIGTTOU, 0); 216 216 setsignal(psh, SIGTTIN, 0); 217 if (getpgid(0) != rootpid && setpgid(0,rootpid) == -1)217 if (getpgid(0) != psh->rootpid && sh_setpgid(0, psh->rootpid) == -1) 218 218 error(psh, "Cannot set process group (%s) at %d", 219 219 strerror(errno), __LINE__); 220 if (tcsetpgrp(ttyfd, rootpid) == -1)220 if (tcsetpgrp(ttyfd, psh->rootpid) == -1) 221 221 error(psh, "Cannot set tty process group (%s) at %d", 222 222 strerror(errno), __LINE__); 223 223 } else { /* turning job control off */ 224 if (getpgid(0) != initialpgrp && s etpgid(0, initialpgrp) == -1)224 if (getpgid(0) != initialpgrp && sh_setpgid(0, initialpgrp) == -1) 225 225 error(psh, "Cannot set process group (%s) at %d", 226 226 strerror(errno), __LINE__); … … 242 242 243 243 SHELLPROC { 244 backgndpid = -1;245 #if JOBS 246 jobctl = 0;244 psh->backgndpid = -1; 245 #if JOBS 246 psh->jobctl = 0; 247 247 #endif 248 248 } … … 511 511 else 512 512 mode = SHOW_PGID; 513 if (* argptr)513 if (*psh->argptr) 514 514 do 515 showjob(out1, getjob(* argptr,0), mode);516 while (*++ argptr);515 showjob(out1, getjob(*psh->argptr,0), mode); 516 while (*++psh->argptr); 517 517 else 518 518 showjobs(psh, out1, mode); … … 608 608 nextopt(psh, ""); 609 609 610 if (!* argptr) {610 if (!*psh->argptr) { 611 611 /* wait for all jobs */ 612 612 jp = jobtab; … … 629 629 630 630 retval = 127; /* XXXGCC: -Wuninitialized */ 631 for (; * argptr;argptr++) {632 job = getjob(* argptr, 1);631 for (; *psh->argptr; psh->argptr++) { 632 job = getjob(*psh->argptr, 1); 633 633 if (!job) { 634 634 retval = 127; … … 666 666 667 667 nextopt(psh, ""); 668 jp = getjob(* argptr, 0);668 jp = getjob(*psh->argptr, 0); 669 669 for (i = 0 ; i < jp->nprocs ; ) { 670 670 out1fmt(psh, "%ld", (long)jp->ps[i].pid); … … 879 879 } 880 880 if (mode == FORK_BG) 881 backgndpid = pid; /* set $! */881 psh0->backgndpid = pid; /* set $! */ 882 882 if (jp) { 883 883 struct procstat *ps = &jp->ps[jp->nprocs++]; -
trunk/src/kash/miscbltin.c
r1202 r1203 117 117 } 118 118 119 if (*(ap = argptr) == NULL)119 if (*(ap = psh->argptr) == NULL) 120 120 error(psh, "arg count"); 121 121 … … 230 230 INTON; 231 231 232 if ((ap = * argptr) == NULL) {232 if ((ap = *psh->argptr) == NULL) { 233 233 if (symbolic_mode) { 234 234 char u[4], g[4], o[4]; … … 386 386 error(psh, "internal error (%c)", what); 387 387 388 set = * argptr ? 1 : 0;388 set = *psh->argptr ? 1 : 0; 389 389 if (set) { 390 char *p = * argptr;391 392 if (all || argptr[1])390 char *p = *psh->argptr; 391 392 if (all || psh->argptr[1]) 393 393 error(psh, "too many arguments"); 394 394 if (strcmp(p, "unlimited") == 0) -
trunk/src/kash/shinstance.h
r1201 r1203 36 36 #include "options.h" 37 37 38 #include "expand.h" 39 #include "exec.h" 38 40 #include "var.h" 39 41 … … 70 72 }; 71 73 74 /* exec.c */ 75 #define CMDTABLESIZE 31 /* should be prime */ 76 #define ARB 1 /* actual size determined at run time */ 77 78 struct tblentry { 79 struct tblentry *next; /* next entry in hash chain */ 80 union param param; /* definition of builtin function */ 81 short cmdtype; /* index identifying command */ 82 char rehash; /* if set, cd done since entry created */ 83 char cmdname[ARB]; /* name of command */ 84 }; 85 86 /* expand.c */ 87 /* 88 * Structure specifying which parts of the string should be searched 89 * for IFS characters. 90 */ 91 struct ifsregion { 92 struct ifsregion *next; /* next region in list */ 93 int begoff; /* offset of start of region */ 94 int endoff; /* offset of end of region */ 95 int inquotes; /* search for nul bytes only */ 96 }; 72 97 73 98 … … 91 116 struct jmploc *handler; 92 117 int exception; 93 int exerrno ;118 int exerrno/* = 0 */; /**< Last exec error */ 94 119 int volatile suppressint; 95 120 int volatile intpending; … … 188 213 const char *pathopt; /**< set by padvance */ 189 214 215 /* exec.c */ 216 struct tblentry *cmdtable[CMDTABLESIZE]; 217 int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */ 218 219 190 220 /* eval.h */ 191 221 char *commandname; /**< currently executing command */ … … 215 245 /* eval.c */ 216 246 int vforked; 247 248 /* expand.c */ 249 char *expdest; /**< output of current string */ 250 struct nodelist *argbackq; /**< list of back quote expressions */ 251 struct ifsregion ifsfirst; /**< first struct in list of ifs regions */ 252 struct ifsregion *ifslastp; /**< last struct in list */ 253 struct arglist exparg; /**< holds expanded arg list */ 254 char *expdir; /**< Used by expandmeta. */ 217 255 218 256 /* mail.c */ … … 259 297 int sh_sysconf_clk_tck(void); 260 298 261 /* wait */299 /* wait / process */ 262 300 #ifdef _MSC_VER 263 301 # include <process.h> … … 284 322 pid_t sh_waitpid(shinstance *, pid_t, int *, int); 285 323 void sh__exit(shinstance *, int); 286 287 288 #endif 324 int sh_execve(shinstance *, const char *, const char * const*, const char * const *); 325 326 #endif -
trunk/src/kash/trap.c
r1202 r1203 401 401 for (;;) { 402 402 for (i = 1 ; ; i++) { 403 if ( gotsig[i - 1])403 if (psh->gotsig[i - 1]) 404 404 break; 405 405 if (i >= NSIG) 406 406 goto done; 407 407 } 408 gotsig[i - 1] = 0;409 savestatus= exitstatus;408 psh->gotsig[i - 1] = 0; 409 savestatus=psh->exitstatus; 410 410 evalstring(psh, trap[i], 0); 411 exitstatus=savestatus;411 psh->exitstatus=savestatus; 412 412 } 413 413 done: 414 p endingsigs = 0;414 psh->pendingsigs = 0; 415 415 } 416 416 -
trunk/src/kash/var.c
r1202 r1203 681 681 } 682 682 683 while ((name = * argptr++) != NULL) {683 while ((name = *psh->argptr++) != NULL) { 684 684 if ((p = strchr(name, '=')) != NULL) { 685 685 p++; … … 708 708 if (! in_function(psh)) 709 709 error(psh, "Not in a function"); 710 while ((name = * argptr++) != NULL) {710 while ((name = *psh->argptr++) != NULL) { 711 711 mklocal(psh, name, 0); 712 712 } … … 830 830 flg_var = 1; 831 831 832 for (ap = argptr; *ap ; ap++) {832 for (ap = psh->argptr; *ap ; ap++) { 833 833 if (flg_func) 834 834 ret |= unsetfunc(psh, *ap);
Note:
See TracChangeset
for help on using the changeset viewer.