VirtualBox

Changeset 1206 in kBuild


Ignore:
Timestamp:
Oct 7, 2007 3:40:04 PM (17 years ago)
Author:
bird
Message:

some more.

Location:
trunk/src/kash
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/Makefile.kmk

    r1205 r1206  
    4949        miscbltin.c \
    5050        bltin/echo.c \
     51        bltin/test.c \
    5152        \
    5253        jobs.c \
     
    5657        var.c \
    5758        bltin/kill.c \
    58         bltin/test.c \
    5959        $(PATH_TARGET)/arith.c \
    6060        $(PATH_TARGET)/arith_lex.c \
  • trunk/src/kash/bltin/echo.c

    r1205 r1206  
    5252 */
    5353
    54 #include "bltin.h"
     54#include "shinstance.h"
     55#include "builtins.h"
    5556
    5657int
  • trunk/src/kash/bltin/test.c

    r843 r1206  
    1818#endif
    1919
    20 #include <sys/stat.h>
    2120#include <sys/types.h>
    2221
    2322#include <ctype.h>
    24 #ifndef __sun__
    25 #include <err.h>
    26 #endif
    2723#include <errno.h>
    2824#include <stdio.h>
    2925#include <stdlib.h>
    3026#include <string.h>
    31 #include <unistd.h>
    3227#include <stdarg.h>
     28
     29#include "error.h"
     30#include "shinstance.h"
     31
    3332
    3433/* test(1) accepts the following grammar:
     
    146145};
    147146
    148 static char **t_wp;
    149 static struct t_op const *t_wp_op;
    150 
    151 static void syntax(const char *, const char *);
    152 static int oexpr(enum token);
    153 static int aexpr(enum token);
    154 static int nexpr(enum token);
    155 static int primary(enum token);
    156 static int binop(void);
    157 static int filstat(char *, enum token);
    158 static enum token t_lex(char *);
    159 static int isoperand(void);
    160 static int getn(const char *);
    161 static int newerf(const char *, const char *);
    162 static int olderf(const char *, const char *);
    163 static int equalf(const char *, const char *);
    164 
    165 #if defined(SHELL)
    166 extern void error(const char *, ...) __attribute__((__noreturn__));
    167 #else
    168 static void error(const char *, ...) __attribute__((__noreturn__));
    169 
    170 static void
    171 error(const char *msg, ...)
    172 {
    173         va_list ap;
    174 
    175         va_start(ap, msg);
    176         verrx(2, msg, ap);
    177         /*NOTREACHED*/
    178         va_end(ap);
    179 }
    180 #endif
    181 
    182 #ifdef SHELL
    183 int testcmd(int, char **);
     147//static char **t_wp;
     148//static struct t_op const *t_wp_op;
     149
     150static void syntax(shinstance *, const char *, const char *);
     151static int oexpr(shinstance *, enum token);
     152static int aexpr(shinstance *, enum token);
     153static int nexpr(shinstance *, enum token);
     154static int primary(shinstance *, enum token);
     155static int binop(shinstance *);
     156static int filstat(shinstance *, char *, enum token);
     157static enum token t_lex(shinstance *, char *);
     158static int isoperand(shinstance *);
     159static int getn(shinstance *, const char *);
     160static int newerf(shinstance *, const char *, const char *);
     161static int olderf(shinstance *, const char *, const char *);
     162static int equalf(shinstance *, const char *, const char *);
     163
    184164
    185165int
    186 testcmd(int argc, char **argv)
    187 #else
    188 int main(int, char *[]);
    189 
    190 int
    191 main(int argc, char *argv[])
    192 #endif
     166testcmd(shinstance *psh, int argc, char **argv)
    193167{
    194168        int res;
    195169
    196 #ifdef HAVE_SETPROGNAME
    197         setprogname(argv[0]);
    198 #endif
    199170        if (strcmp(argv[0], "[") == 0) {
    200171                if (strcmp(argv[--argc], "]"))
    201                         error("missing ]");
     172                        error(psh, "missing ]");
    202173                argv[argc] = NULL;
    203174        }
     
    206177                return 1;
    207178
    208         t_wp = &argv[1];
    209         res = !oexpr(t_lex(*t_wp));
    210 
    211         if (*t_wp != NULL && *++t_wp != NULL)
    212                 syntax(*t_wp, "unexpected operator");
     179        psh->t_wp_op = NULL;
     180        psh->t_wp = &argv[1];
     181        res = !oexpr(psh, t_lex(psh, *psh->t_wp));
     182
     183        if (*psh->t_wp != NULL && *++psh->t_wp != NULL)
     184                syntax(psh, *psh->t_wp, "unexpected operator");
    213185
    214186        return res;
     
    216188
    217189static void
    218 syntax(const char *op, const char *msg)
     190syntax(shinstance *psh, const char *op, const char *msg)
    219191{
    220192
    221193        if (op && *op)
    222                 error("%s: %s", op, msg);
     194                error(psh, "%s: %s", op, msg);
    223195        else
    224                 error("%s", msg);
    225 }
    226 
    227 static int
    228 oexpr(enum token n)
     196                error(psh, "%s", msg);
     197}
     198
     199static int
     200oexpr(shinstance *psh, enum token n)
    229201{
    230202        int res;
    231203
    232         res = aexpr(n);
    233         if (t_lex(*++t_wp) == BOR)
    234                 return oexpr(t_lex(*++t_wp)) || res;
    235         t_wp--;
     204        res = aexpr(psh, n);
     205        if (t_lex(psh, *++psh->t_wp) == BOR)
     206                return oexpr(psh, t_lex(psh, *++psh->t_wp)) || res;
     207        psh->t_wp--;
    236208        return res;
    237209}
    238210
    239211static int
    240 aexpr(enum token n)
     212aexpr(shinstance *psh, enum token n)
    241213{
    242214        int res;
    243215
    244         res = nexpr(n);
    245         if (t_lex(*++t_wp) == BAND)
    246                 return aexpr(t_lex(*++t_wp)) && res;
    247         t_wp--;
     216        res = nexpr(psh, n);
     217        if (t_lex(psh, *++psh->t_wp) == BAND)
     218                return aexpr(psh, t_lex(psh, *++psh->t_wp)) && res;
     219        psh->t_wp--;
    248220        return res;
    249221}
    250222
    251223static int
    252 nexpr(enum token n)
     224nexpr(shinstance *psh, enum token n)
    253225{
    254226
    255227        if (n == UNOT)
    256                 return !nexpr(t_lex(*++t_wp));
    257         return primary(n);
    258 }
    259 
    260 static int
    261 primary(enum token n)
     228                return !nexpr(psh, t_lex(psh, *++psh->t_wp));
     229        return primary(psh, n);
     230}
     231
     232static int
     233primary(shinstance *psh, enum token n)
    262234{
    263235        enum token nn;
     
    267239                return 0;               /* missing expression */
    268240        if (n == LPAREN) {
    269                 if ((nn = t_lex(*++t_wp)) == RPAREN)
     241                if ((nn = t_lex(psh, *++psh->t_wp)) == RPAREN)
    270242                        return 0;       /* missing expression */
    271                 res = oexpr(nn);
    272                 if (t_lex(*++t_wp) != RPAREN)
    273                         syntax(NULL, "closing paren expected");
     243                res = oexpr(psh, nn);
     244                if (t_lex(psh, *++psh->t_wp) != RPAREN)
     245                        syntax(psh, NULL, "closing paren expected");
    274246                return res;
    275247        }
    276         if (t_wp_op && t_wp_op->op_type == UNOP) {
     248        if (psh->t_wp_op && psh->t_wp_op->op_type == UNOP) {
    277249                /* unary expression */
    278                 if (*++t_wp == NULL)
    279                         syntax(t_wp_op->op_text, "argument expected");
     250                if (*++psh->t_wp == NULL)
     251                        syntax(psh, psh->t_wp_op->op_text, "argument expected");
    280252                switch (n) {
    281253                case STREZ:
    282                         return strlen(*t_wp) == 0;
     254                        return strlen(*psh->t_wp) == 0;
    283255                case STRNZ:
    284                         return strlen(*t_wp) != 0;
     256                        return strlen(*psh->t_wp) != 0;
    285257                case FILTT:
    286                         return isatty(getn(*t_wp));
     258                        return shfile_isatty(&psh->fdtab, getn(psh, *psh->t_wp));
    287259                default:
    288                         return filstat(*t_wp, n);
     260                        return filstat(psh, *psh->t_wp, n);
    289261                }
    290262        }
    291263
    292         if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
    293                 return binop();
    294         }
    295 
    296         return strlen(*t_wp) > 0;
    297 }
    298 
    299 static int
    300 binop(void)
     264        if (t_lex(psh, psh->t_wp[1]), psh->t_wp_op && psh->t_wp_op->op_type == BINOP) {
     265                return binop(psh);
     266        }
     267
     268        return strlen(*psh->t_wp) > 0;
     269}
     270
     271static int
     272binop(shinstance *psh)
    301273{
    302274        const char *opnd1, *opnd2;
    303275        struct t_op const *op;
    304276
    305         opnd1 = *t_wp;
    306         (void) t_lex(*++t_wp);
    307         op = t_wp_op;
    308 
    309         if ((opnd2 = *++t_wp) == NULL)
    310                 syntax(op->op_text, "argument expected");
     277        opnd1 = *psh->t_wp;
     278        (void) t_lex(psh, *++psh->t_wp);
     279        op = psh->t_wp_op;
     280
     281        if ((opnd2 = *++psh->t_wp) == NULL)
     282                syntax(psh, op->op_text, "argument expected");
    311283
    312284        switch (op->op_num) {
     
    320292                return strcmp(opnd1, opnd2) > 0;
    321293        case INTEQ:
    322                 return getn(opnd1) == getn(opnd2);
     294                return getn(psh, opnd1) == getn(psh, opnd2);
    323295        case INTNE:
    324                 return getn(opnd1) != getn(opnd2);
     296                return getn(psh, opnd1) != getn(psh, opnd2);
    325297        case INTGE:
    326                 return getn(opnd1) >= getn(opnd2);
     298                return getn(psh, opnd1) >= getn(psh, opnd2);
    327299        case INTGT:
    328                 return getn(opnd1) > getn(opnd2);
     300                return getn(psh, opnd1) > getn(psh, opnd2);
    329301        case INTLE:
    330                 return getn(opnd1) <= getn(opnd2);
     302                return getn(psh, opnd1) <= getn(psh, opnd2);
    331303        case INTLT:
    332                 return getn(opnd1) < getn(opnd2);
     304                return getn(psh, opnd1) < getn(psh, opnd2);
    333305        case FILNT:
    334                 return newerf(opnd1, opnd2);
     306                return newerf(psh, opnd1, opnd2);
    335307        case FILOT:
    336                 return olderf(opnd1, opnd2);
     308                return olderf(psh, opnd1, opnd2);
    337309        case FILEQ:
    338                 return equalf(opnd1, opnd2);
     310                return equalf(psh, opnd1, opnd2);
    339311        default:
    340312                abort();
    341313                /* NOTREACHED */
    342         }
    343 }
    344 
    345 static int
    346 filstat(char *nm, enum token mode)
     314                return -1;
     315        }
     316}
     317
     318static int
     319filstat(shinstance *psh, char *nm, enum token mode)
    347320{
    348321        struct stat s;
    349322
    350         if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
     323        if (mode == FILSYM
     324                ? shfile_lstat(&psh->fdtab, nm, &s)
     325                : shfile_stat(&psh->fdtab, nm, &s))
    351326                return 0;
    352327
    353328        switch (mode) {
    354329        case FILRD:
    355                 return access(nm, R_OK) == 0;
     330                return shfile_access(&psh->fdtab, nm, R_OK) == 0;
    356331        case FILWR:
    357                 return access(nm, W_OK) == 0;
     332                return shfile_access(&psh->fdtab, nm, W_OK) == 0;
    358333        case FILEX:
    359                 return access(nm, X_OK) == 0;
     334                return shfile_access(&psh->fdtab, nm, X_OK) == 0;
    360335        case FILEXIST:
    361                 return access(nm, F_OK) == 0;
     336                return shfile_access(&psh->fdtab, nm, F_OK) == 0;
    362337        case FILREG:
    363338                return S_ISREG(s.st_mode);
     
    403378                return s.st_size > (off_t)0;
    404379        case FILUID:
    405                 return s.st_uid == geteuid();
     380                return s.st_uid == sh_geteuid(psh);
    406381        case FILGID:
    407                 return s.st_gid == getegid();
     382                return s.st_gid == sh_getegid(psh);
    408383        default:
    409384                return 1;
     
    412387
    413388static enum token
    414 t_lex(char *s)
     389t_lex(shinstance *psh, char *s)
    415390{
    416391        struct t_op const *op;
     
    419394
    420395        if (s == 0) {
    421                 t_wp_op = NULL;
     396                psh->t_wp_op = NULL;
    422397                return EOI;
    423398        }
    424399        while (op->op_text) {
    425400                if (strcmp(s, op->op_text) == 0) {
    426                         if ((op->op_type == UNOP && isoperand()) ||
    427                             (op->op_num == LPAREN && *(t_wp+1) == 0))
     401                        if ((op->op_type == UNOP && isoperand(psh)) ||
     402                            (op->op_num == LPAREN && *(psh->t_wp+1) == 0))
    428403                                break;
    429                         t_wp_op = op;
     404                        psh->t_wp_op = op;
    430405                        return op->op_num;
    431406                }
    432407                op++;
    433408        }
    434         t_wp_op = NULL;
     409        psh->t_wp_op = NULL;
    435410        return OPERAND;
    436411}
    437412
    438413static int
    439 isoperand(void)
     414isoperand(shinstance *psh)
    440415{
    441416        struct t_op const *op;
     
    443418
    444419        op = ops;
    445         if ((s  = *(t_wp+1)) == 0)
     420        if ((s  = *(psh->t_wp+1)) == 0)
    446421                return 1;
    447         if ((t = *(t_wp+2)) == 0)
     422        if ((t = *(psh->t_wp+2)) == 0)
    448423                return 0;
    449424        while (op->op_text) {
     
    458433/* atoi with error detection */
    459434static int
    460 getn(const char *s)
     435getn(shinstance *psh, const char *s)
    461436{
    462437        char *p;
     
    467442
    468443        if (errno != 0)
    469               error("%s: out of range", s);
     444              error(psh, "%s: out of range", s);
    470445
    471446        while (isspace((unsigned char)*p))
     
    473448
    474449        if (*p)
    475               error("%s: bad number", s);
     450              error(psh, "%s: bad number", s);
    476451
    477452        return (int) r;
     
    479454
    480455static int
    481 newerf(const char *f1, const char *f2)
     456newerf(shinstance *psh, const char *f1, const char *f2)
    482457{
    483458        struct stat b1, b2;
    484459
    485         return (stat(f1, &b1) == 0 &&
    486                 stat(f2, &b2) == 0 &&
     460        return (shfile_stat(&psh->fdtab, f1, &b1) == 0 &&
     461                shfile_stat(&psh->fdtab, f2, &b2) == 0 &&
    487462                b1.st_mtime > b2.st_mtime);
    488463}
    489464
    490465static int
    491 olderf(const char *f1, const char *f2)
     466olderf(shinstance *psh, const char *f1, const char *f2)
    492467{
    493468        struct stat b1, b2;
    494469
    495         return (stat(f1, &b1) == 0 &&
    496                 stat(f2, &b2) == 0 &&
     470        return (shfile_stat(&psh->fdtab, f1, &b1) == 0 &&
     471                shfile_stat(&psh->fdtab, f2, &b2) == 0 &&
    497472                b1.st_mtime < b2.st_mtime);
    498473}
    499474
    500475static int
    501 equalf(const char *f1, const char *f2)
     476equalf(shinstance *psh, const char *f1, const char *f2)
    502477{
    503478        struct stat b1, b2;
    504479
    505         return (stat(f1, &b1) == 0 &&
    506                 stat(f2, &b2) == 0 &&
     480        return (shfile_stat(&psh->fdtab, f1, &b1) == 0 &&
     481                shfile_stat(&psh->fdtab, f2, &b2) == 0 &&
    507482                b1.st_dev == b2.st_dev &&
    508483                b1.st_ino == b2.st_ino);
  • trunk/src/kash/shfile.h

    r1199 r1206  
    2929
    3030#include "shtypes.h"
     31#include <fcntl.h>
     32#include <sys/stat.h>
     33#ifndef _MSC_VER
     34# include <sys/fcntl.h>
     35# include <unistd.h>
     36# ifndef O_BINARY
     37#  define O_BINARY  0
     38# endif
     39# ifndef O_TEXT
     40#  define O_TEXT    0
     41# endif
     42
     43#else
     44# include <io.h>
     45# include <direct.h>
     46
     47# define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
     48# define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
     49# define S_ISLNK(m) 0
     50# define S_IRWXU    (_S_IREAD | _S_IWRITE | _S_IEXEC)
     51# define S_IXUSR    _S_IEXEC
     52# define S_IWUSR    _S_IWRITE
     53# define S_IRUSR    _S_IREAD
     54# define S_IRWXG    0000070
     55# define S_IRGRP    0000040
     56# define S_IWGRP    0000020
     57# define S_IXGRP    0000010
     58# define S_IRWXO    0000007
     59# define S_IROTH    0000004
     60# define S_IWOTH    0000002
     61# define S_IXOTH    0000001
     62# define S_ISUID    0004000
     63# define S_ISGID    0002000
     64# define ALLPERMS   0000777
     65
     66# define F_DUPFD    0
     67# define F_GETFD    1
     68# define F_SETFD    2
     69# define F_GETFL    3
     70# define F_SETFL    4
     71# define FD_CLOEXEC 1
     72
     73# define F_OK       0
     74# define X_OK       1
     75# define W_OK       2
     76# define R_OK       4
     77
     78#endif
     79
    3180
    3281/**
     
    51100} shfdtab;
    52101
    53 
    54102int shfile_open(shfdtab *, const char *, unsigned);
    55103int shfile_pipe(shfdtab *, int [2]);
     
    59107long shfile_lseek(shfdtab *, int, long, int);
    60108int shfile_fcntl(shfdtab *, int fd, int cmd, int arg);
    61 #ifdef _MSC_VER
    62 # define F_DUPFD    0
    63 # define F_GETFD    1
    64 # define F_SETFD    2
    65 # define F_GETFL    3
    66 # define F_SETFL    4
    67 # define FD_CLOEXEC 1
    68 #else
    69 # include <sys/fcntl.h>
    70 #endif
    71109
    72110int shfile_stat(shfdtab *, const char *, struct stat *);
     
    75113char *shfile_getcwd(shfdtab *, char *, int);
    76114int shfile_isatty(shfdtab *, int);
    77 /*int shfile_ioctl(shfdtab *, int, unsigned long, char *);*/
     115int shfile_ioctl(shfdtab *, int, unsigned long, char *);
     116int shfile_access(shfdtab *, const char *, int);
    78117
    79118#endif
  • trunk/src/kash/shinstance.h

    r1203 r1206  
    258258    int                 nmboxes;        /**< number of mailboxes */
    259259    time_t              mailtime[MAXMBOXES]; /**< times of mailboxes */
     260
     261    /* bltin/test.c */
     262    char              **t_wp;
     263    struct t_op const  *t_wp_op;
    260264
    261265} shinstance;
     
    323327void sh__exit(shinstance *, int);
    324328int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
    325 
    326 #endif
     329uid_t sh_getuid(shinstance *);
     330uid_t sh_geteuid(shinstance *);
     331gid_t sh_getgid(shinstance *);
     332gid_t sh_getegid(shinstance *);
     333
     334#endif
  • trunk/src/kash/show.c

    r1199 r1206  
    382382{
    383383        char s[100];
    384 #ifdef O_APPEND
     384#if defined(O_APPEND) && !defined(_MSC_VER)
    385385        int flags;
    386386#endif
     
    420420                }
    421421        }
    422 #ifdef O_APPEND
     422#if defined(O_APPEND) && !defined(_MSC_VER)
    423423        if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
    424424                fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
  • trunk/src/kash/shtypes.h

    r881 r1206  
    6969#define UINT64_MAX      0xffffffffffffffffULL
    7070
     71typedef int             pid_t;
     72typedef unsigned short  uid_t;
     73typedef unsigned short  gid_t;
     74
    7175#else
    7276# include <stdint.h>
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette