Exemplo n.º 1
0
/* this creates the sun4 timers: */
void
_tme_sun4_timer_new(struct tme_sun4 *sun4)
{
  struct tme_sun4_timer *timer;

  /* loop over the timers: */
  timer = &sun4->tme_sun4_timers[0];
  do {

    /* initialize and reset the timer: */
    timer->tme_sun4_timer_sun4 = sun4;
    tme_cond_init(&timer->tme_sun4_timer_cond);
    _tme_sun4_timer_reset(timer);

    /* start the thread for this timer: */
    tme_thread_create((tme_thread_t) _tme_sun4_timer_th, timer);

  } while (++timer != (&sun4->tme_sun4_timers[0] + TME_ARRAY_ELS(sun4->tme_sun4_timers)));
}
Exemplo n.º 2
0
/* the new serial function: */
TME_ELEMENT_SUB_NEW_DECL(tme_host_posix,serial) {
  struct tme_posix_serial *serial;
  const char *filename_in;
  const char *filename_out;
  int fd_in, fd_out;
  int usage;
  int arg_i;
  int saved_errno;
  int emulate_break;

  /* initialize: */
  filename_in = NULL;
  filename_out = NULL;
  emulate_break = FALSE;
  arg_i = 1;
  usage = FALSE;

  /* loop reading our arguments: */
  for (;;) {

    /* the device we're supposed to use for input: */
    if (TME_ARG_IS(args[arg_i + 0], "device-input")
	&& args[arg_i + 1] != NULL
	&& filename_in == NULL) {
      filename_in = args[arg_i + 1];
      arg_i += 2;
    }

    /* the device we're supposed to use for output: */
    else if (TME_ARG_IS(args[arg_i + 0], "device-output")
	     && args[arg_i + 1] != NULL
	     && filename_out == NULL) {
      filename_out = args[arg_i + 1];
      arg_i += 2;
    }

    /* the device we're supposed to use for input and output: */
    else if (TME_ARG_IS(args[arg_i + 0], "device")
	     && args[arg_i + 1] != NULL
	     && filename_in == NULL
	     && filename_out == NULL) {
      filename_in = filename_out = args[arg_i + 1];
      arg_i += 2;
    }

    /* if we're supposed to emulate break: */
    else if (TME_ARG_IS(args[arg_i + 0], "break-carats")) {
      emulate_break = TRUE;
      arg_i++;
    }

    /* if we've run out of arguments: */
    else if (args[arg_i + 0] == NULL) {

      /* we must have been given input and output devices: */
      if (filename_in == NULL
	  || filename_out == NULL) {
	usage = TRUE;
      }
      break;
    }

    /* this is a bad argument: */
    else {
      tme_output_append_error(_output,
			      "%s %s", 
			      args[arg_i],
			      _("unexpected"));
      usage = TRUE;
      break;
    }
  }

  if (usage) {
    tme_output_append_error(_output, 
			    "%s %s { device %s | { device-input %s device-output %s } } [break-carats]",
			    _("usage:"),
			    args[0],
			    _("DEVICE"),
			    _("DEVICE"),
			    _("DEVICE"));
    return (EINVAL);
  }

  /* open the devices: */
  fd_in = fd_out = -1;
  if (fd_in < 0
      && !strcmp(filename_in, "-")) {
    fd_in = STDIN_FILENO;
  }
  if (fd_out < 0
      && !strcmp(filename_out, "-")) {
    fd_out = STDOUT_FILENO;
  }
  if (fd_in < 0) {
    if (strcmp(filename_in, filename_out) == 0) {
      if (strcmp(filename_in, "pty") == 0) {
	fd_in = fd_out = posix_openpt(O_RDWR | O_NONBLOCK);
	if (fd_in != -1) {
	  int serrno;
	  if (grantpt(fd_in) == -1) {
bad:	     serrno = errno;
	    (void)close(fd_in);
	    (void)close(fd_out);
	    errno = serrno;
	  } else {
	    filename_in = filename_out = ptsname(fd_in);
	    if (filename_in) {
	      tme_output_append(_output, "Using %s as console\n", filename_in);
	    } else {
	      goto bad;
	    }
	  }
	}
      } else {
	fd_in = fd_out = open(filename_in, O_RDWR | O_NONBLOCK);
      }
    }
    else {
      fd_in = open(filename_in, O_RDONLY | O_NONBLOCK);
    }
    if (fd_in < 0) {
      tme_output_append_error(_output, "%s", filename_in);
      return (errno);
    }
  }
  if (fd_out < 0) {
    fd_out = open(filename_out, O_WRONLY | O_NONBLOCK);
    if (fd_out < 0) {
      saved_errno = errno;
      close(fd_in);
      tme_output_append_error(_output, "%s", filename_out);
      return (saved_errno);
    }
  }

  /* start the serial structure: */
  serial = tme_new0(struct tme_posix_serial, 1);
  serial->tme_posix_serial_element = element;
  serial->tme_posix_serial_fd_in = fd_in;
  serial->tme_posix_serial_fd_out = fd_out;
  serial->tme_posix_serial_emulate_break = emulate_break;
  serial->tme_posix_serial_ctrl_callout = 0;
  serial->tme_posix_serial_ctrl_callout_last = 0;
  tme_serial_buffer_init(&serial->tme_posix_serial_buffer_in, 
			 TME_POSIX_SERIAL_BUFFER_SIZE);
  tme_serial_buffer_init(&serial->tme_posix_serial_buffer_out, 
			 TME_POSIX_SERIAL_BUFFER_SIZE);

  /* start the threads: */
  tme_mutex_init(&serial->tme_posix_serial_mutex);
  tme_cond_init(&serial->tme_posix_serial_cond_writer);
  tme_thread_create((tme_thread_t) _tme_posix_serial_th_writer, serial);
  tme_thread_create((tme_thread_t) _tme_posix_serial_th_reader, serial);
  tme_thread_create((tme_thread_t) _tme_posix_serial_th_ctrl, serial);

  /* fill the element: */
  element->tme_element_private = serial;
  element->tme_element_connections_new = _tme_posix_serial_connections_new;

  return (TME_OK);
}