Beispiel #1
0
/*
 * Get next input command from terminal.
 *
 *   Returns: 1 if got input
 *           -1 if EOF or error
 */
int get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
{
   int len;

   if (!stop) {
      if (output == stdout || teeout) {
         sendit(prompt);
      }
   }

again:
   len = sizeof_pool_memory(sock->msg) - 1;
   if (stop) {
      sleep(1);
      goto again;
   }

#ifdef HAVE_CONIO
   if (bisatty(fileno(input))) {
      input_line(sock->msg, len);
      goto ok_out;
   }
#endif

   if (input == stdin) {
      if (win32_cgets(sock->msg, len) == NULL) {
         return -1;
      }
   } else {
      if (fgets(sock->msg, len, input) == NULL) {
         return -1;
      }
   }

   if (usrbrk()) {
      clrbrk();
   }

   strip_trailing_junk(sock->msg);
   sock->msglen = strlen(sock->msg);

   return 1;
}
Beispiel #2
0
/*
 * Get next input command from terminal.
 *
 *   Returns: 1 if got input
 *            0 if timeout
 *           -1 if EOF or error
 */
int get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
{
   int len;

   if (!stop) {
      if (output == stdout || teeout) {
         sendit(prompt);
      }
   }

again:
   switch (wait_for_readable_fd(fileno(input), sec, true)) {
   case 0:
      return 0;                    /* timeout */
   case -1:
      return -1;                   /* error */
   default:
      len = sizeof_pool_memory(sock->msg) - 1;
      if (stop) {
         sleep(1);
         goto again;
      }
#ifdef HAVE_CONIO
      if (bisatty(fileno(input))) {
         input_line(sock->msg, len);
         break;
      }
#endif
      if (fgets(sock->msg, len, input) == NULL) {
         return -1;
      }
      break;
   }

   if (usrbrk()) {
      clrbrk();
   }

   strip_trailing_junk(sock->msg);
   sock->msglen = strlen(sock->msg);

   return 1;
}
Beispiel #3
0
int
input_line(char *string, int length)
{
   char curline[2000];                /* edit buffer */
   int noline;
   unsigned c;
   int more;
   int i;

    if (first) {
       poolinit();                   /* build line pool */
       first = 0;
    }
    noline = 1;                       /* no line fetched yet */
    for (cl=cp=0; cl<length && cl<(int)sizeof(curline); ) {
       if (usrbrk()) {
          clrbrk();
          break;
       }
       switch (c=input_char()) {
       case F_RETURN:                /* CR */
           t_sendl("\r\n", 2);       /* yes, print it and */
           goto done;                /* get out */
       case F_CLRSCRN:               /* clear screen */
          asclrs();
          t_sendl(curline, cl);
          ascurs(0, cp);
          break;
       case F_CSRUP:
           if (noline) {             /* no line fetched yet */
               getnext();            /* getnext so getprev gets current */
               noline = 0;           /* we now have line */
           }
           bstrncpy(curline, getprev(), sizeof(curline));
           prtcur(curline);
           break;
       case F_CSRDWN:
           noline = 0;               /* mark line fetched */
           bstrncpy(curline, getnext(), sizeof(curline));
           prtcur(curline);
           break;
       case F_INSCHR:
           insert_space(curline, sizeof(curline));
           break;
       case F_DELCHR:
           delchr(1, curline, sizeof(curline));       /* delete one character */
           break;
       case F_CSRLFT:                /* Backspace */
           backup(curline);
           break;
       case F_CSRRGT:
           forward(curline, sizeof(curline));
           break;
       case F_ERSCHR:                /* Rubout */
           backup(curline);
           delchr(1, curline, sizeof(curline));
           if (cp == 0) {
              t_char(' ');
              t_char(0x8);
           }
           break;
       case F_DELEOL:
           t_clrline(0, t_width);
           if (cl > cp)
               cl = cp;
           break;
       case F_NXTWRD:
           i = next_word(curline);
           while (i--) {
              forward(curline, sizeof(curline));
           }
           break;
       case F_PRVWRD:
           i = prev_word(curline);
           while (i--) {
              backup(curline);
           }
           break;
       case F_DELWRD:
           delchr(next_word(curline), curline, sizeof(curline)); /* delete word */
           break;
       case F_NXTMCH:                /* Ctl-X */
           if (cl==0) {
               *string = EOS;        /* terminate string */
               return(c);            /* give it to him */
           }
           /* Note fall through */
       case F_DELLIN:
       case F_ERSLIN:
           while (cp > 0) {
              backup(curline);      /* backup to beginning of line */
           }
           t_clrline(0, t_width);     /* erase line */
           cp = 0;
           cl = 0;                   /* reset cursor counter */
           t_char(' ');
           t_char(0x8);
           break;
       case F_SOL:
           while (cp > 0) {
              backup(curline);
           }
           break;
       case F_EOL:
           while (cp < cl) {
               forward(curline, sizeof(curline));
           }
           while (cp > cl) {
               backup(curline);
           }
           break;
       case F_TINS:                  /* toggle insert mode */
           mode_insert = !mode_insert;  /* flip bit */
           break;
       default:
           if (c > 255) {            /* function key hit */
               if (cl==0) {          /* if first character then */
                  *string = EOS;     /* terminate string */
                  return c;          /* return it */
               }
               t_honk_horn();        /* complain */
           } else {
               if ((c & 0xC0) == 0xC0) {
                  if ((c & 0xFC) == 0xFC) {
                     more = 5;
                  } else if ((c & 0xF8) == 0xF8) {
                     more = 4;
                  } else if ((c & 0xF0) == 0xF0) {
                     more = 3;
                  } else if ((c & 0xE0) == 0xE0) {
                     more = 2;
                  } else {
                     more = 1;
                  }
               } else {
                  more = 0;
               }
               if (mode_insert) {
                  insert_space(curline, sizeof(curline));
               }
               curline[cp++] = c;    /* store character in line being built */
               t_char(c);      /* echo character to terminal */
               while (more--) {
                  c= input_char();
                  insert_hole(curline, sizeof(curline));
                  curline[cp++] = c;    /* store character in line being built */
                  t_char(c);      /* echo character to terminal */
               }
               if (cp > cl) {
                  cl = cp;           /* keep current length */
                  curline[cp] = 0;
               }
           }
           break;
       }                             /* end switch */
    }
/* If we fall through here rather than goto done, the line is too long
   simply return what we have now. */
done:
   curline[cl++] = EOS;              /* terminate */
   bstrncpy(string,curline,length);           /* return line to caller */
   /* Save non-blank lines. Note, put line zaps curline */
   if (curline[0] != EOS) {
      putline(curline,cl);            /* save line for posterity */
   }
   return 0;                         /* give it to him/her */
}
Beispiel #4
0
static void read_and_process_input(FILE *input, BSOCK *UA_sock)
{
   const char *prompt = "*";
   bool at_prompt = false;
   int tty_input = isatty(fileno(input));
   int status;
   btimer_t *tid = NULL;

   while (1) {
      if (at_prompt) {                /* don't prompt multiple times */
         prompt = "";
      } else {
         prompt = "*";
         at_prompt = true;
      }
      if (tty_input) {
         status = get_cmd(input, prompt, UA_sock, 30);
         if (usrbrk() == 1) {
            clrbrk();
         }
         if (usrbrk()) {
            break;
         }
      } else {
         /*
          * Reading input from a file
          */
         int len = sizeof_pool_memory(UA_sock->msg) - 1;
         if (usrbrk()) {
            break;
         }
         if (fgets(UA_sock->msg, len, input) == NULL) {
            status = -1;
         } else {
            sendit(UA_sock->msg);     /* echo to terminal */
            strip_trailing_junk(UA_sock->msg);
            UA_sock->msglen = strlen(UA_sock->msg);
            status = 1;
         }
      }
      if (status < 0) {
         break;                       /* error or interrupt */
      } else if (status == 0) {       /* timeout */
         if (bstrcmp(prompt, "*")) {
            tid = start_bsock_timer(UA_sock, timeout);
            UA_sock->fsend(".messages");
            stop_bsock_timer(tid);
         } else {
            continue;
         }
      } else {
         at_prompt = false;
         /*
          * @ => internal command for us
          */
         if (UA_sock->msg[0] == '@') {
            parse_args(UA_sock->msg, &args, &argc, argk, argv, MAX_CMD_ARGS);
            if (!do_a_command(input, UA_sock)) {
               break;
            }
            continue;
         }
         tid = start_bsock_timer(UA_sock, timeout);
         if (!UA_sock->send()) {      /* send command */
            stop_bsock_timer(tid);
            break;                    /* error */
         }
         stop_bsock_timer(tid);
      }

      if (bstrcmp(UA_sock->msg, ".quit") || bstrcmp(UA_sock->msg, ".exit")) {
         break;
      }

      tid = start_bsock_timer(UA_sock, timeout);
      while ((status = UA_sock->recv()) >= 0 ||
             ((status == BNET_SIGNAL) && (
              (UA_sock->msglen != BNET_EOD) &&
              (UA_sock->msglen != BNET_MAIN_PROMPT) &&
              (UA_sock->msglen != BNET_SUB_PROMPT)))) {
         if (status == BNET_SIGNAL) {
            if (UA_sock->msglen == BNET_START_RTREE) {
               file_selection = true;
            } else if (UA_sock->msglen == BNET_END_RTREE) {
               file_selection = false;
            }
            continue;
         }

         if (at_prompt) {
            if (!stop) {
               sendit("\n");
            }
            at_prompt = false;
         }

         /*
          * Suppress output if running in background or user hit ctl-c
          */
         if (!stop && !usrbrk()) {
            if (UA_sock->msg) {
               sendit(UA_sock->msg);
            }
         }
      }
      stop_bsock_timer(tid);

      if (usrbrk() > 1) {
         break;
      } else {
         clrbrk();
      }
      if (!stop) {
         fflush(stdout);
      }

      if (is_bnet_stop(UA_sock)) {
         break;                       /* error or term */
      } else if (status == BNET_SIGNAL) {
         if (UA_sock->msglen == BNET_SUB_PROMPT) {
            at_prompt = true;
         }
         Dmsg1(100, "Got poll %s\n", bnet_sig_to_ascii(UA_sock));
      }
   }
}