Beispiel #1
0
/* handle escape characters, writing to output */
void cook_buf(int fd, char *buf, int num) { 
  int current = 0;
  
  if (in_escape) {
    /* cook_buf last called with an incomplete escape sequence */
    switch (help_state) {
    case 0:
      help_send_escape(fd, buf[0]);
      break;
    case 1:
      help_set_terminal(fd, buf[0]);
      break;
    default:
      help_set_speed(fd, buf[0]);
    }
    num--; /* advance to the next character in the buffer */
    buf++; 
  }

  while (current < num) { /* big while loop, to process all the charactes in buffer */

    /* look for the next escape character '~' */
    while ((current < num) && (buf[current] != 0x7e)) current++;
    /* and write the sequence before esc char to the comm port */
    if (current) {
      DEBUG_DUMP_MEMORY(buf,current);
      const ssize_t written = write (fd, buf, current);
      if (written == -1) {
	const int error = errno;
	ERROR_MSG("error writting to device %d (%m)",error);
      } else if (written != current) {
	ERROR_MSG("error writting to device only %d bytes written",written);
      }
    }

    if (current < num) { /* process an escape sequence */
      /* found an escape character */
      if (help_state == 0) {
	help_escape();
      }
      current++;
      if (current >= num) {
	/* interpret first character of next sequence */
	in_escape = 1;
	return;
      }

      switch (help_state) {
      case 0:
	help_send_escape(fd, buf[current]);
	break;
      case 1:
	help_set_terminal(fd, buf[current]);
	break;
      default:
 	help_set_speed(fd, buf[current]);
      } /* end switch */
      current++;
      if (current >= num)
	return;
    } /* if - end of processing escape sequence */
    num -= current;
    buf += current;
    current = 0;
  } /* while - end of processing all the charactes in the buffer */
  return;
}
Beispiel #2
0
/* handle escape characters, writing to output */
void cook_buf(struct ios_ops *ios, unsigned char *buf, int num)
{
    int current = 0;

    if (in_escape) {
        /* cook_buf last called with an incomplete escape sequence */
        switch (help_state) {
        case 0:
            help_send_escape(ios, buf[0]);
            break;
        case 1:
            help_set_terminal(ios, buf[0]);
            break;
        default:
            help_set_speed(ios, buf[0]);
        }
        num--;		/* advance to the next character in the buffer */
        buf++;
    }

    while (current < num) {	/* big while loop, to process all the charactes in buffer */

        /* look for the next escape character '~' */
        while ((current < num) && (buf[current] != 28))
            current++;
        /* and write the sequence befor esc char to the comm port */
        if (current)
        {
            if (in_echo)
            {
                write(STDOUT_FILENO, buf, current);	/* echo */
            }

            if (!in_line)
                write(ios->fd, buf, current);
            else
                writeline(ios->fd, buf, current);
        }

        if (current < num) {	/* process an escape sequence */
            /* flush buffers */
            fflush(stdout);
            if (in_line)
                flushline(ios->fd);
            /* found an escape character */
            if (help_state == 0)
                help_escape();
            current++;
            if (current >= num) {
                /* interpret first character of next sequence */
                in_escape = 1;
                return;
            }

            switch (help_state) {
            case 0:
                help_send_escape(ios, buf[current]);
                break;
            case 1:
                help_set_terminal(ios, buf[current]);
                break;
            default:
                help_set_speed(ios, buf[current]);
            }	/* end switch */
            current++;
            if (current >= num)
                return;
        }		/* if - end of processing escape sequence */
        num -= current;
        buf += current;
        current = 0;
    }			/* while - end of processing all the charactes in the buffer */
    return;
}