Advertisement
Guest User

ash.c 1.00 read

a guest
Jul 16th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. /*
  2.  * The read builtin.  The -e option causes backslashes to escape the
  3.  * following character.
  4.  *
  5.  * This uses unbuffered input, which may be avoidable in some cases.
  6.  */
  7.  
  8. static int
  9. readcmd(int argc, char **argv)
  10. {
  11.     char **ap;
  12.     int backslash;
  13.     char c;
  14.     int rflag;
  15.     char *prompt;
  16.     const char *ifs;
  17.     char *p;
  18.     int startword;
  19.     int status;
  20.     int i;
  21.  
  22.     rflag = 0;
  23.     prompt = NULL;
  24.     while ((i = nextopt("p:r")) != '\0') {
  25.         if (i == 'p')
  26.             prompt = optionarg;
  27.         else
  28.             rflag = 1;
  29.     }
  30.     if (prompt && isatty(0)) {
  31.         out2str(prompt);
  32.     }
  33.     if (*(ap = argptr) == NULL)
  34.         error("arg count");
  35.     if ((ifs = bltinlookup("IFS")) == NULL)
  36.         ifs = defifs;
  37.     status = 0;
  38.     startword = 1;
  39.     backslash = 0;
  40.     STARTSTACKSTR(p);
  41.     for (;;) {
  42.         if (read(0, &c, 1) != 1) {
  43.             status = 1;
  44.             break;
  45.         }
  46.         if (c == '\0')
  47.             continue;
  48.         if (backslash) {
  49.             backslash = 0;
  50.             if (c != '\n')
  51.                 goto put;
  52.             continue;
  53.         }
  54.         if (!rflag && c == '\\') {
  55.             backslash++;
  56.             continue;
  57.         }
  58.         if (c == '\n')
  59.             break;
  60.         if (startword && *ifs == ' ' && strchr(ifs, c)) {
  61.             continue;
  62.         }
  63.         startword = 0;
  64.         if (ap[1] != NULL && strchr(ifs, c) != NULL) {
  65.             STACKSTRNUL(p);
  66.             setvar(*ap, stackblock(), 0);
  67.             ap++;
  68.             startword = 1;
  69.             STARTSTACKSTR(p);
  70.         } else {
  71. put:
  72.             STPUTC(c, p);
  73.         }
  74.     }
  75.     STACKSTRNUL(p);
  76.     /* Remove trailing blanks */
  77.     while ((char *)stackblock() <= --p && strchr(ifs, *p) != NULL)
  78.         *p = '\0';
  79.     setvar(*ap, stackblock(), 0);
  80.     while (*++ap != NULL)
  81.         setvar(*ap, nullstr, 0);
  82.     return status;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement