Changeset 1206 in kBuild
- Timestamp:
- Oct 7, 2007 3:40:04 PM (17 years ago)
- Location:
- trunk/src/kash
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/Makefile.kmk
r1205 r1206 49 49 miscbltin.c \ 50 50 bltin/echo.c \ 51 bltin/test.c \ 51 52 \ 52 53 jobs.c \ … … 56 57 var.c \ 57 58 bltin/kill.c \ 58 bltin/test.c \59 59 $(PATH_TARGET)/arith.c \ 60 60 $(PATH_TARGET)/arith_lex.c \ -
trunk/src/kash/bltin/echo.c
r1205 r1206 52 52 */ 53 53 54 #include "bltin.h" 54 #include "shinstance.h" 55 #include "builtins.h" 55 56 56 57 int -
trunk/src/kash/bltin/test.c
r843 r1206 18 18 #endif 19 19 20 #include <sys/stat.h>21 20 #include <sys/types.h> 22 21 23 22 #include <ctype.h> 24 #ifndef __sun__25 #include <err.h>26 #endif27 23 #include <errno.h> 28 24 #include <stdio.h> 29 25 #include <stdlib.h> 30 26 #include <string.h> 31 #include <unistd.h>32 27 #include <stdarg.h> 28 29 #include "error.h" 30 #include "shinstance.h" 31 33 32 34 33 /* test(1) accepts the following grammar: … … 146 145 }; 147 146 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 150 static void syntax(shinstance *, const char *, const char *); 151 static int oexpr(shinstance *, enum token); 152 static int aexpr(shinstance *, enum token); 153 static int nexpr(shinstance *, enum token); 154 static int primary(shinstance *, enum token); 155 static int binop(shinstance *); 156 static int filstat(shinstance *, char *, enum token); 157 static enum token t_lex(shinstance *, char *); 158 static int isoperand(shinstance *); 159 static int getn(shinstance *, const char *); 160 static int newerf(shinstance *, const char *, const char *); 161 static int olderf(shinstance *, const char *, const char *); 162 static int equalf(shinstance *, const char *, const char *); 163 184 164 185 165 int 186 testcmd(int argc, char **argv) 187 #else 188 int main(int, char *[]); 189 190 int 191 main(int argc, char *argv[]) 192 #endif 166 testcmd(shinstance *psh, int argc, char **argv) 193 167 { 194 168 int res; 195 169 196 #ifdef HAVE_SETPROGNAME197 setprogname(argv[0]);198 #endif199 170 if (strcmp(argv[0], "[") == 0) { 200 171 if (strcmp(argv[--argc], "]")) 201 error( "missing ]");172 error(psh, "missing ]"); 202 173 argv[argc] = NULL; 203 174 } … … 206 177 return 1; 207 178 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"); 213 185 214 186 return res; … … 216 188 217 189 static void 218 syntax( const char *op, const char *msg)190 syntax(shinstance *psh, const char *op, const char *msg) 219 191 { 220 192 221 193 if (op && *op) 222 error( "%s: %s", op, msg);194 error(psh, "%s: %s", op, msg); 223 195 else 224 error( "%s", msg);225 } 226 227 static int 228 oexpr( enum token n)196 error(psh, "%s", msg); 197 } 198 199 static int 200 oexpr(shinstance *psh, enum token n) 229 201 { 230 202 int res; 231 203 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--; 236 208 return res; 237 209 } 238 210 239 211 static int 240 aexpr( enum token n)212 aexpr(shinstance *psh, enum token n) 241 213 { 242 214 int res; 243 215 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--; 248 220 return res; 249 221 } 250 222 251 223 static int 252 nexpr( enum token n)224 nexpr(shinstance *psh, enum token n) 253 225 { 254 226 255 227 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 232 static int 233 primary(shinstance *psh, enum token n) 262 234 { 263 235 enum token nn; … … 267 239 return 0; /* missing expression */ 268 240 if (n == LPAREN) { 269 if ((nn = t_lex( *++t_wp)) == RPAREN)241 if ((nn = t_lex(psh, *++psh->t_wp)) == RPAREN) 270 242 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"); 274 246 return res; 275 247 } 276 if ( t_wp_op &&t_wp_op->op_type == UNOP) {248 if (psh->t_wp_op && psh->t_wp_op->op_type == UNOP) { 277 249 /* 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"); 280 252 switch (n) { 281 253 case STREZ: 282 return strlen(* t_wp) == 0;254 return strlen(*psh->t_wp) == 0; 283 255 case STRNZ: 284 return strlen(* t_wp) != 0;256 return strlen(*psh->t_wp) != 0; 285 257 case FILTT: 286 return isatty(getn(*t_wp));258 return shfile_isatty(&psh->fdtab, getn(psh, *psh->t_wp)); 287 259 default: 288 return filstat( *t_wp, n);260 return filstat(psh, *psh->t_wp, n); 289 261 } 290 262 } 291 263 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 271 static int 272 binop(shinstance *psh) 301 273 { 302 274 const char *opnd1, *opnd2; 303 275 struct t_op const *op; 304 276 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"); 311 283 312 284 switch (op->op_num) { … … 320 292 return strcmp(opnd1, opnd2) > 0; 321 293 case INTEQ: 322 return getn( opnd1) == getn(opnd2);294 return getn(psh, opnd1) == getn(psh, opnd2); 323 295 case INTNE: 324 return getn( opnd1) != getn(opnd2);296 return getn(psh, opnd1) != getn(psh, opnd2); 325 297 case INTGE: 326 return getn( opnd1) >= getn(opnd2);298 return getn(psh, opnd1) >= getn(psh, opnd2); 327 299 case INTGT: 328 return getn( opnd1) > getn(opnd2);300 return getn(psh, opnd1) > getn(psh, opnd2); 329 301 case INTLE: 330 return getn( opnd1) <= getn(opnd2);302 return getn(psh, opnd1) <= getn(psh, opnd2); 331 303 case INTLT: 332 return getn( opnd1) < getn(opnd2);304 return getn(psh, opnd1) < getn(psh, opnd2); 333 305 case FILNT: 334 return newerf( opnd1, opnd2);306 return newerf(psh, opnd1, opnd2); 335 307 case FILOT: 336 return olderf( opnd1, opnd2);308 return olderf(psh, opnd1, opnd2); 337 309 case FILEQ: 338 return equalf( opnd1, opnd2);310 return equalf(psh, opnd1, opnd2); 339 311 default: 340 312 abort(); 341 313 /* NOTREACHED */ 342 } 343 } 344 345 static int 346 filstat(char *nm, enum token mode) 314 return -1; 315 } 316 } 317 318 static int 319 filstat(shinstance *psh, char *nm, enum token mode) 347 320 { 348 321 struct stat s; 349 322 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)) 351 326 return 0; 352 327 353 328 switch (mode) { 354 329 case FILRD: 355 return access(nm, R_OK) == 0;330 return shfile_access(&psh->fdtab, nm, R_OK) == 0; 356 331 case FILWR: 357 return access(nm, W_OK) == 0;332 return shfile_access(&psh->fdtab, nm, W_OK) == 0; 358 333 case FILEX: 359 return access(nm, X_OK) == 0;334 return shfile_access(&psh->fdtab, nm, X_OK) == 0; 360 335 case FILEXIST: 361 return access(nm, F_OK) == 0;336 return shfile_access(&psh->fdtab, nm, F_OK) == 0; 362 337 case FILREG: 363 338 return S_ISREG(s.st_mode); … … 403 378 return s.st_size > (off_t)0; 404 379 case FILUID: 405 return s.st_uid == geteuid();380 return s.st_uid == sh_geteuid(psh); 406 381 case FILGID: 407 return s.st_gid == getegid();382 return s.st_gid == sh_getegid(psh); 408 383 default: 409 384 return 1; … … 412 387 413 388 static enum token 414 t_lex( char *s)389 t_lex(shinstance *psh, char *s) 415 390 { 416 391 struct t_op const *op; … … 419 394 420 395 if (s == 0) { 421 t_wp_op = NULL;396 psh->t_wp_op = NULL; 422 397 return EOI; 423 398 } 424 399 while (op->op_text) { 425 400 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)) 428 403 break; 429 t_wp_op = op;404 psh->t_wp_op = op; 430 405 return op->op_num; 431 406 } 432 407 op++; 433 408 } 434 t_wp_op = NULL;409 psh->t_wp_op = NULL; 435 410 return OPERAND; 436 411 } 437 412 438 413 static int 439 isoperand( void)414 isoperand(shinstance *psh) 440 415 { 441 416 struct t_op const *op; … … 443 418 444 419 op = ops; 445 if ((s = *( t_wp+1)) == 0)420 if ((s = *(psh->t_wp+1)) == 0) 446 421 return 1; 447 if ((t = *( t_wp+2)) == 0)422 if ((t = *(psh->t_wp+2)) == 0) 448 423 return 0; 449 424 while (op->op_text) { … … 458 433 /* atoi with error detection */ 459 434 static int 460 getn( const char *s)435 getn(shinstance *psh, const char *s) 461 436 { 462 437 char *p; … … 467 442 468 443 if (errno != 0) 469 error( "%s: out of range", s);444 error(psh, "%s: out of range", s); 470 445 471 446 while (isspace((unsigned char)*p)) … … 473 448 474 449 if (*p) 475 error( "%s: bad number", s);450 error(psh, "%s: bad number", s); 476 451 477 452 return (int) r; … … 479 454 480 455 static int 481 newerf( const char *f1, const char *f2)456 newerf(shinstance *psh, const char *f1, const char *f2) 482 457 { 483 458 struct stat b1, b2; 484 459 485 return (s tat(f1, &b1) == 0 &&486 s tat(f2, &b2) == 0 &&460 return (shfile_stat(&psh->fdtab, f1, &b1) == 0 && 461 shfile_stat(&psh->fdtab, f2, &b2) == 0 && 487 462 b1.st_mtime > b2.st_mtime); 488 463 } 489 464 490 465 static int 491 olderf( const char *f1, const char *f2)466 olderf(shinstance *psh, const char *f1, const char *f2) 492 467 { 493 468 struct stat b1, b2; 494 469 495 return (s tat(f1, &b1) == 0 &&496 s tat(f2, &b2) == 0 &&470 return (shfile_stat(&psh->fdtab, f1, &b1) == 0 && 471 shfile_stat(&psh->fdtab, f2, &b2) == 0 && 497 472 b1.st_mtime < b2.st_mtime); 498 473 } 499 474 500 475 static int 501 equalf( const char *f1, const char *f2)476 equalf(shinstance *psh, const char *f1, const char *f2) 502 477 { 503 478 struct stat b1, b2; 504 479 505 return (s tat(f1, &b1) == 0 &&506 s tat(f2, &b2) == 0 &&480 return (shfile_stat(&psh->fdtab, f1, &b1) == 0 && 481 shfile_stat(&psh->fdtab, f2, &b2) == 0 && 507 482 b1.st_dev == b2.st_dev && 508 483 b1.st_ino == b2.st_ino); -
trunk/src/kash/shfile.h
r1199 r1206 29 29 30 30 #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 31 80 32 81 /** … … 51 100 } shfdtab; 52 101 53 54 102 int shfile_open(shfdtab *, const char *, unsigned); 55 103 int shfile_pipe(shfdtab *, int [2]); … … 59 107 long shfile_lseek(shfdtab *, int, long, int); 60 108 int shfile_fcntl(shfdtab *, int fd, int cmd, int arg); 61 #ifdef _MSC_VER62 # define F_DUPFD 063 # define F_GETFD 164 # define F_SETFD 265 # define F_GETFL 366 # define F_SETFL 467 # define FD_CLOEXEC 168 #else69 # include <sys/fcntl.h>70 #endif71 109 72 110 int shfile_stat(shfdtab *, const char *, struct stat *); … … 75 113 char *shfile_getcwd(shfdtab *, char *, int); 76 114 int shfile_isatty(shfdtab *, int); 77 /*int shfile_ioctl(shfdtab *, int, unsigned long, char *);*/ 115 int shfile_ioctl(shfdtab *, int, unsigned long, char *); 116 int shfile_access(shfdtab *, const char *, int); 78 117 79 118 #endif -
trunk/src/kash/shinstance.h
r1203 r1206 258 258 int nmboxes; /**< number of mailboxes */ 259 259 time_t mailtime[MAXMBOXES]; /**< times of mailboxes */ 260 261 /* bltin/test.c */ 262 char **t_wp; 263 struct t_op const *t_wp_op; 260 264 261 265 } shinstance; … … 323 327 void sh__exit(shinstance *, int); 324 328 int sh_execve(shinstance *, const char *, const char * const*, const char * const *); 325 326 #endif 329 uid_t sh_getuid(shinstance *); 330 uid_t sh_geteuid(shinstance *); 331 gid_t sh_getgid(shinstance *); 332 gid_t sh_getegid(shinstance *); 333 334 #endif -
trunk/src/kash/show.c
r1199 r1206 382 382 { 383 383 char s[100]; 384 #if def O_APPEND384 #if defined(O_APPEND) && !defined(_MSC_VER) 385 385 int flags; 386 386 #endif … … 420 420 } 421 421 } 422 #if def O_APPEND422 #if defined(O_APPEND) && !defined(_MSC_VER) 423 423 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0) 424 424 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); -
trunk/src/kash/shtypes.h
r881 r1206 69 69 #define UINT64_MAX 0xffffffffffffffffULL 70 70 71 typedef int pid_t; 72 typedef unsigned short uid_t; 73 typedef unsigned short gid_t; 74 71 75 #else 72 76 # include <stdint.h>
Note:
See TracChangeset
for help on using the changeset viewer.