Changeset 1201 for trunk/src/kash/input.c
- Timestamp:
- Oct 7, 2007, 1:57:35 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/input.c
r1199 r1201 69 69 #include "parser.h" 70 70 #include "myhistedit.h" 71 #include "shinstance.h" 71 72 72 73 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 73 74 74 MKINIT75 struct strpush {76 struct strpush *prev; /* preceding string on stack */77 char *prevstring;78 int prevnleft;79 int prevlleft;80 struct alias *ap; /* if push was associated with an alias */81 };82 83 / *84 * The parsefile structure pointed to by the global variable parsefile85 * contains information about the current file being read.86 */87 88 MKINIT89 struct parsefile {90 struct parsefile *prev; /* preceding file on stack */91 int linno; /* current line */92 int fd; /* file descriptor (or -1 if string) */93 int nleft; /* number of chars left in this line */94 int lleft; /* number of chars left in this buffer */95 char *nextc; /* next char in buffer */96 char *buf; /* input buffer */97 struct strpush *strpush; /* for pushing strings at this level */98 struct strpush basestrpush; /* so pushing one is fast */99 };100 101 102 int plinno = 1; /* input line number */103 int parsenleft; /* copy of parsefile->nleft */104 MKINIT int parselleft; /* copy of parsefile->lleft */105 char *parsenextc; /* copy of parsefile->nextc */106 MKINIT struct parsefile basepf; /* top level input file */107 MKINIT char basebuf[BUFSIZ]; /* buffer for top level input file */108 struct parsefile *parsefile = &basepf; /* current input file */109 int init_editline = 0; /* editline library initialized? */110 int whichprompt; /* 1 == PS1, 2 == PS2 */111 112 #ifndef SMALL113 EditLine *el; /* cookie for editline package */114 #endif115 116 STATIC void pushfile( void);117 static int preadfd( void);75 //MKINIT 76 //struct strpush { 77 // struct strpush *prev; /* preceding string on stack */ 78 // char *prevstring; 79 // int prevnleft; 80 // int prevlleft; 81 // struct alias *ap; /* if push was associated with an alias */ 82 //}; 83 // 84 ///* 85 // * The parsefile structure pointed to by the global variable parsefile 86 // * contains information about the current file being read. 87 // */ 88 // 89 //MKINIT 90 //struct parsefile { 91 // struct parsefile *prev; /* preceding file on stack */ 92 // int linno; /* current line */ 93 // int fd; /* file descriptor (or -1 if string) */ 94 // int nleft; /* number of chars left in this line */ 95 // int lleft; /* number of chars left in this buffer */ 96 // char *nextc; /* next char in buffer */ 97 // char *buf; /* input buffer */ 98 // struct strpush *strpush; /* for pushing strings at this level */ 99 // struct strpush basestrpush; /* so pushing one is fast */ 100 //}; 101 // 102 // 103 //int plinno = 1; /* input line number */ 104 //int parsenleft; /* copy of parsefile->nleft */ 105 //MKINIT int parselleft; /* copy of parsefile->lleft */ 106 //char *parsenextc; /* copy of parsefile->nextc */ 107 //MKINIT struct parsefile basepf; /* top level input file */ 108 //MKINIT char basebuf[BUFSIZ]; /* buffer for top level input file */ 109 //struct parsefile *parsefile = &basepf; /* current input file */ 110 //int init_editline = 0; /* editline library initialized? */ 111 //int whichprompt; /* 1 == PS1, 2 == PS2 */ 112 // 113 //#ifndef SMALL 114 //EditLine *el; /* cookie for editline package */ 115 //#endif 116 117 STATIC void pushfile(shinstance *psh); 118 static int preadfd(shinstance *psh); 118 119 119 120 #ifdef mkinit … … 123 124 124 125 INIT { 125 basepf.nextc = basepf.buf =basebuf;126 psh->basepf.nextc = psh->basepf.buf = psh->basebuf; 126 127 } 127 128 128 129 RESET { 129 if ( exception != EXSHELLPROC)130 p arselleft =parsenleft = 0; /* clear input buffer */131 popallfiles( );130 if (psh->exception != EXSHELLPROC) 131 psh->parselleft = psh->parsenleft = 0; /* clear input buffer */ 132 popallfiles(psh); 132 133 } 133 134 134 135 SHELLPROC { 135 popallfiles( );136 popallfiles(psh); 136 137 } 137 138 #endif … … 143 144 144 145 char * 145 pfgets( char *line, int len)146 pfgets(shinstance *psh, char *line, int len) 146 147 { 147 148 char *p = line; … … 150 151 151 152 while (--nleft > 0) { 152 c = pgetc_macro( );153 c = pgetc_macro(psh); 153 154 if (c == PEOF) { 154 155 if (p == line) … … 172 173 173 174 int 174 pgetc( void)175 { 176 return pgetc_macro( );175 pgetc(shinstance *psh) 176 { 177 return pgetc_macro(psh); 177 178 } 178 179 179 180 180 181 static int 181 preadfd( void)182 preadfd(shinstance *psh) 182 183 { 183 184 int nr; 184 char *buf = 185 p arsenextc = buf;185 char *buf = psh->parsefile->buf; 186 psh->parsenextc = buf; 186 187 187 188 retry: 188 189 #ifndef SMALL 189 if (p arsefile->fd == 0 &&el) {190 if (psh->parsefile->fd == 0 && psh->el) { 190 191 static const char *rl_cp; 191 192 static int el_len; 192 193 193 194 if (rl_cp == NULL) 194 rl_cp = el_gets( el, &el_len);195 rl_cp = el_gets(psh->el, &el_len); 195 196 if (rl_cp == NULL) 196 197 nr = 0; … … 209 210 } else 210 211 #endif 211 nr = read(parsefile->fd, buf, BUFSIZ - 8);212 nr = shfile_read(&psh->fdtab, psh->parsefile->fd, buf, BUFSIZ - 8); 212 213 213 214 … … 216 217 if (errno == EINTR) 217 218 goto retry; 218 if (p arsefile->fd == 0 && errno == EWOULDBLOCK) {219 int flags = fcntl(0, F_GETFL, 0);219 if (psh->parsefile->fd == 0 && errno == EWOULDBLOCK) { 220 int flags = shfile_fcntl(&psh->fdtab, 0, F_GETFL, 0); 220 221 if (flags >= 0 && flags & O_NONBLOCK) { 221 222 flags &=~ O_NONBLOCK; 222 if ( fcntl(0, F_SETFL, flags) >= 0) {223 if (shfile_fcntl(&psh->fdtab, 0, F_SETFL, flags) >= 0) { 223 224 out2str(psh, "sh: turning off NDELAY mode\n"); 224 225 goto retry; … … 243 244 244 245 int 245 preadbuffer( void)246 preadbuffer(shinstance *psh) 246 247 { 247 248 char *p, *q; … … 250 251 char savec; 251 252 252 if (p arsefile->strpush) {253 popstring( );254 if (--p arsenleft >= 0)255 return (*p arsenextc++);256 } 257 if (p arsenleft == EOF_NLEFT ||parsefile->buf == NULL)253 if (psh->parsefile->strpush) { 254 popstring(psh); 255 if (--psh->parsenleft >= 0) 256 return (*psh->parsenextc++); 257 } 258 if (psh->parsenleft == EOF_NLEFT || psh->parsefile->buf == NULL) 258 259 return PEOF; 259 flushout(& output);260 flushout(& errout);260 flushout(&psh->output); 261 flushout(&psh->errout); 261 262 262 263 again: 263 if (p arselleft <= 0) {264 if ((p arselleft = preadfd()) == -1) {265 p arselleft =parsenleft = EOF_NLEFT;264 if (psh->parselleft <= 0) { 265 if ((psh->parselleft = preadfd(psh)) == -1) { 266 psh->parselleft = psh->parsenleft = EOF_NLEFT; 266 267 return PEOF; 267 268 } 268 269 } 269 270 270 q = p = p arsenextc;271 q = p = psh->parsenextc; 271 272 272 273 /* delete nul characters */ … … 283 284 284 285 case '\n': 285 p arsenleft = q - parsenextc;286 psh->parsenleft = (int)(q - psh->parsenextc); 286 287 more = 0; /* Stop processing here */ 287 288 break; … … 294 295 *q++ = *p++; 295 296 check: 296 if (--p arselleft <= 0) {297 p arsenleft = q - parsenextc - 1;298 if (p arsenleft < 0)297 if (--psh->parselleft <= 0) { 298 psh->parsenleft = (int)(q - psh->parsenextc - 1); 299 if (psh->parsenleft < 0) 299 300 goto again; 300 301 *q = '\0'; … … 307 308 308 309 #ifndef SMALL 309 if (p arsefile->fd == 0 && hist && something) {310 if (psh->parsefile->fd == 0 && hist && something) { 310 311 HistEvent he; 311 312 INTOFF; 312 history(hist, &he, whichprompt == 1? H_ENTER : H_APPEND,313 p arsenextc);313 history(hist, &he, psh->whichprompt == 1? H_ENTER : H_APPEND, 314 psh->parsenextc); 314 315 INTON; 315 316 } … … 317 318 318 319 if (vflag(psh)) { 319 out2str(psh, p arsenextc);320 flushout( out2);320 out2str(psh, psh->parsenextc); 321 flushout(psh->out2); 321 322 } 322 323 323 324 *q = savec; 324 325 325 return *p arsenextc++;326 return *psh->parsenextc++; 326 327 } 327 328 … … 332 333 333 334 void 334 pungetc( void)335 { 336 p arsenleft++;337 p arsenextc--;335 pungetc(shinstance *psh) 336 { 337 psh->parsenleft++; 338 psh->parsenextc--; 338 339 } 339 340 … … 343 344 */ 344 345 void 345 pushstring( char *s, int len, void *ap)346 pushstring(shinstance *psh, char *s, int len, void *ap) 346 347 { 347 348 struct strpush *sp; … … 349 350 INTOFF; 350 351 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ 351 if (p arsefile->strpush) {352 if (psh->parsefile->strpush) { 352 353 sp = ckmalloc(sizeof (struct strpush)); 353 sp->prev = p arsefile->strpush;354 p arsefile->strpush = sp;354 sp->prev = psh->parsefile->strpush; 355 psh->parsefile->strpush = sp; 355 356 } else 356 sp = p arsefile->strpush = &(parsefile->basestrpush);357 sp->prevstring = p arsenextc;358 sp->prevnleft = p arsenleft;359 sp->prevlleft = p arselleft;357 sp = psh->parsefile->strpush = &(psh->parsefile->basestrpush); 358 sp->prevstring = psh->parsenextc; 359 sp->prevnleft = psh->parsenleft; 360 sp->prevlleft = psh->parselleft; 360 361 sp->ap = (struct alias *)ap; 361 362 if (ap) 362 363 ((struct alias *)ap)->flag |= ALIASINUSE; 363 p arsenextc = s;364 p arsenleft = len;364 psh->parsenextc = s; 365 psh->parsenleft = len; 365 366 INTON; 366 367 } 367 368 368 369 void 369 popstring( void)370 { 371 struct strpush *sp = p arsefile->strpush;370 popstring(shinstance *psh) 371 { 372 struct strpush *sp = psh->parsefile->strpush; 372 373 373 374 INTOFF; 374 p arsenextc = sp->prevstring;375 p arsenleft = sp->prevnleft;376 p arselleft = sp->prevlleft;377 /*dprintf("*** calling popstring: restoring to '%s'\n", p arsenextc);*/375 psh->parsenextc = sp->prevstring; 376 psh->parsenleft = sp->prevnleft; 377 psh->parselleft = sp->prevlleft; 378 /*dprintf("*** calling popstring: restoring to '%s'\n", psh->parsenextc);*/ 378 379 if (sp->ap) 379 380 sp->ap->flag &= ~ALIASINUSE; 380 p arsefile->strpush = sp->prev;381 if (sp != &(p arsefile->basestrpush))381 psh->parsefile->strpush = sp->prev; 382 if (sp != &(psh->parsefile->basestrpush)) 382 383 ckfree(sp); 383 384 INTON; … … 390 391 391 392 void 392 setinputfile( const char *fname, int push)393 setinputfile(shinstance *psh, const char *fname, int push) 393 394 { 394 395 int fd; … … 396 397 397 398 INTOFF; 398 if ((fd = open(fname, O_RDONLY)) < 0) 399 /** @todo shfile fixme */ 400 if ((fd = shfile_open(&psh->fdtab, fname, O_RDONLY)) < 0) 399 401 error(psh, "Can't open %s", fname); 400 402 if (fd < 10) { 401 fd2 = copyfd( fd, 10);402 close(fd);403 fd2 = copyfd(psh, fd, 10); 404 shfile_close(&psh->fdtab, fd); 403 405 if (fd2 < 0) 404 406 error(psh, "Out of file descriptors"); 405 407 fd = fd2; 406 408 } 407 setinputfd( fd, push);409 setinputfd(psh, fd, push); 408 410 INTON; 409 411 } … … 416 418 417 419 void 418 setinputfd( int fd, int push)419 { 420 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);420 setinputfd(shinstance *psh, int fd, int push) 421 { 422 (void) shfile_fcntl(&psh->fdtab, fd, F_SETFD, FD_CLOEXEC); 421 423 if (push) { 422 pushfile( );423 p arsefile->buf = ckmalloc(BUFSIZ);424 } 425 if (p arsefile->fd > 0)426 close(parsefile->fd);427 p arsefile->fd = fd;428 if (p arsefile->buf == NULL)429 p arsefile->buf = ckmalloc(BUFSIZ);430 p arselleft =parsenleft = 0;431 p linno = 1;424 pushfile(psh); 425 psh->parsefile->buf = ckmalloc(BUFSIZ); 426 } 427 if (psh->parsefile->fd > 0) 428 shfile_close(&psh->fdtab, psh->parsefile->fd); 429 psh->parsefile->fd = fd; 430 if (psh->parsefile->buf == NULL) 431 psh->parsefile->buf = ckmalloc(BUFSIZ); 432 psh->parselleft = psh->parsenleft = 0; 433 psh->plinno = 1; 432 434 } 433 435 … … 438 440 439 441 void 440 setinputstring( char *string, int push)442 setinputstring(shinstance *psh, char *string, int push) 441 443 { 442 444 INTOFF; 443 445 if (push) 444 pushfile( );445 p arsenextc = string;446 p arselleft = parsenleft =strlen(string);447 p arsefile->buf = NULL;448 p linno = 1;446 pushfile(psh); 447 psh->parsenextc = string; 448 psh->parselleft = psh->parsenleft = (int)strlen(string); 449 psh->parsefile->buf = NULL; 450 psh->plinno = 1; 449 451 INTON; 450 452 } … … 458 460 459 461 STATIC void 460 pushfile( void)462 pushfile(shinstance *psh) 461 463 { 462 464 struct parsefile *pf; 463 465 464 p arsefile->nleft =parsenleft;465 p arsefile->lleft =parselleft;466 p arsefile->nextc =parsenextc;467 p arsefile->linno =plinno;466 psh->parsefile->nleft = psh->parsenleft; 467 psh->parsefile->lleft = psh->parselleft; 468 psh->parsefile->nextc = psh->parsenextc; 469 psh->parsefile->linno = psh->plinno; 468 470 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 469 pf->prev = p arsefile;471 pf->prev = psh->parsefile; 470 472 pf->fd = -1; 471 473 pf->strpush = NULL; 472 474 pf->basestrpush.prev = NULL; 473 p arsefile = pf;474 } 475 476 477 void 478 popfile( void)479 { 480 struct parsefile *pf = p arsefile;475 psh->parsefile = pf; 476 } 477 478 479 void 480 popfile(shinstance *psh) 481 { 482 struct parsefile *pf = psh->parsefile; 481 483 482 484 INTOFF; 483 485 if (pf->fd >= 0) 484 close(pf->fd);486 shfile_close(&psh->fdtab, pf->fd); 485 487 if (pf->buf) 486 488 ckfree(pf->buf); 487 489 while (pf->strpush) 488 popstring( );489 p arsefile = pf->prev;490 popstring(psh); 491 psh->parsefile = pf->prev; 490 492 ckfree(pf); 491 p arsenleft =parsefile->nleft;492 p arselleft =parsefile->lleft;493 p arsenextc =parsefile->nextc;494 p linno =parsefile->linno;493 psh->parsenleft = psh->parsefile->nleft; 494 psh->parselleft = psh->parsefile->lleft; 495 psh->parsenextc = psh->parsefile->nextc; 496 psh->plinno = psh->parsefile->linno; 495 497 INTON; 496 498 } … … 502 504 503 505 void 504 popallfiles( void)505 { 506 while (p arsefile != &basepf)507 popfile( );506 popallfiles(shinstance *psh) 507 { 508 while (psh->parsefile != &psh->basepf) 509 popfile(psh); 508 510 } 509 511 … … 525 527 526 528 void 527 closescript( int vforked)529 closescript(shinstance *psh, int vforked) 528 530 { 529 531 if (vforked) 530 532 return; 531 popallfiles( );532 if (p arsefile->fd > 0) {533 close(parsefile->fd);534 p arsefile->fd = 0;535 } 536 } 533 popallfiles(psh); 534 if (psh->parsefile->fd > 0) { 535 shfile_close(&psh->fdtab, psh->parsefile->fd); 536 psh->parsefile->fd = 0; 537 } 538 }
Note:
See TracChangeset
for help on using the changeset viewer.