Пример #1
0
int luv_new_tty (lua_State* L) {
  int before = lua_gettop(L);
  uv_file fd = luaL_checkint(L, 1);
  int readable = lua_toboolean(L, 2);
  luv_ref_t* ref;

  uv_tty_t* handle = (uv_tty_t*)lua_newuserdata(L, sizeof(uv_tty_t));
  uv_tty_init(uv_default_loop(), handle, fd, readable);

  // Set metatable for type
  luaL_getmetatable(L, "luv_tty");
  lua_setmetatable(L, -2);

  // Create a local environment for storing stuff
  lua_newtable(L);
  lua_setfenv (L, -2);

  // Store a reference to the userdata in the handle
  ref = (luv_ref_t*)malloc(sizeof(luv_ref_t));
  ref->L = L;
  lua_pushvalue(L, -1); // duplicate so we can _ref it
  ref->r = luaL_ref(L, LUA_REGISTRYINDEX);
  handle->data = ref;

  assert(lua_gettop(L) == before + 1);
  // return the userdata
  return 1;
}
Пример #2
0
void *init_stdio_handle(uv_file fd,int readable)
{
    void *handle;
    uv_handle_type type = uv_guess_handle(fd);
    switch(type)
    {
        case UV_TTY:
            handle = malloc(sizeof(uv_tty_t));
            uv_tty_init(jl_io_loop,(uv_tty_t*)handle,fd,readable);
            ((uv_tty_t*)handle)->data=0;
            uv_tty_set_mode((void*)handle,0); //cooked stdio
            break;
        case UV_NAMED_PIPE:
        case UV_FILE:
            handle = malloc(sizeof(uv_pipe_t));
            uv_pipe_init(jl_io_loop, (uv_pipe_t*)handle,(readable?UV_PIPE_READABLE:UV_PIPE_WRITEABLE));
            uv_pipe_open((uv_pipe_t*)handle,fd);
            ((uv_pipe_t*)handle)->data=0;
            break;
        case UV_TCP:
        case UV_UDP:
        default:
            handle=NULL;
            jl_errorf("This type of handle for stdio is not yet supported (%d)!\n",type);
            break;
    }
    return handle;
}
Пример #3
0
std::shared_ptr<Tty> Tty::Create(Loop& loop, uv_file fd, bool readable) {
  auto h = std::make_shared<Tty>(private_init{});
  int err = uv_tty_init(loop.GetRaw(), h->GetRaw(), fd, readable ? 1 : 0);
  if (err < 0) {
    loop.ReportError(err);
    return nullptr;
  }
  h->Keep();
  return h;
}
Пример #4
0
Файл: util.c Проект: tarruda/luv
static int new_tty(lua_State* L) {
  uv_tty_t* handle = luv_create_tty(L);
  uv_file fd = luaL_checkint(L, 1);
  int readable;
  luaL_checktype(L, 2, LUA_TBOOLEAN);
  readable = lua_toboolean(L, 2);
  if (uv_tty_init(uv_default_loop(), handle, fd, readable)) {
    uv_err_t err = uv_last_error(uv_default_loop());
    return luaL_error(L, "new_tty: %s", uv_strerror(err));
  }
  return 1;
}
Пример #5
0
Файл: tty.c Проект: luvit/luv
static int luv_new_tty(lua_State* L) {
    int readable, ret;
    uv_tty_t* handle;
    uv_file fd = luaL_checkinteger(L, 1);
    luaL_checktype(L, 2, LUA_TBOOLEAN);
    readable = lua_toboolean(L, 2);
    handle = (uv_tty_t*)luv_newuserdata(L, sizeof(*handle));
    ret = uv_tty_init(luv_loop(L), handle, fd, readable);
    if (ret < 0) {
        lua_pop(L, 1);
        return luv_error(L, ret);
    }
    handle->data = luv_setup_handle(L);
    return 1;
}
Пример #6
0
int main() {
    loop = uv_default_loop();

    uv_tty_init(loop, &tty, 1, 0);
    uv_tty_set_mode(&tty, 0);
    
    if (uv_tty_get_winsize(&tty, &width, &height)) {
        fprintf(stderr, "Could not get TTY information\n");
        uv_tty_reset_mode();
        return 1;
    }

    fprintf(stderr, "Width %d, height %d\n", width, height);
    uv_timer_init(loop, &tick);
    uv_timer_start(&tick, update, 200, 200);
    return uv_run(loop, UV_RUN_DEFAULT);
}
Пример #7
0
static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
{
  if (cnt <= 0) {
    uv_read_stop(stream);
    return;
  }

  int *interrupted = stream->data;

  for (int i = 0; i < cnt; i++) {
    if (buf->base[i] == CTRL_C) {
      (*interrupted)++;
    }
  }

  uv_loop_t write_loop;
  uv_loop_init(&write_loop);
  uv_tty_t out;
  uv_tty_init(&write_loop, &out, fileno(stdout), 0);

  uv_write_t req;
  uv_buf_t b = {
    .base = buf->base,
#ifdef WIN32
    .len = (ULONG)cnt
#else
    .len = (size_t)cnt
#endif
  };
  uv_write(&req, STRUCT_CAST(uv_stream_t, &out), &b, 1, NULL);
  uv_run(&write_loop, UV_RUN_DEFAULT);

  uv_close(STRUCT_CAST(uv_handle_t, &out), NULL);
  uv_run(&write_loop, UV_RUN_DEFAULT);
  if (uv_loop_close(&write_loop)) {
    abort();
  }
  free(buf->base);

  if (*interrupted >= 2) {
    uv_walk(uv_default_loop(), walk_cb, NULL);
  } else if (*interrupted == 1) {
    fprintf(stderr, "interrupt received, press again to exit\n");
  }
}
Пример #8
0
void *init_stdio_handle(uv_file fd,int readable)
{
    void *handle;
    uv_handle_type type = uv_guess_handle(fd);
    //printf("%d: %d -- %d\n", fd, type);
    switch(type)
    {
        case UV_TTY:
            handle = malloc(sizeof(uv_tty_t));
            if (uv_tty_init(jl_io_loop,(uv_tty_t*)handle,fd,readable)) {
                jl_errorf("Error initializing stdio in uv_tty_init (%d, %d)\n", fd, type);
                abort();
            }
            ((uv_tty_t*)handle)->data=0;
            uv_tty_set_mode((void*)handle,0); //cooked stdio
            break;
        case UV_FILE:
#ifdef _WIN32
            jl_errorf("This type of handle for stdio is not yet supported on Windows (%d, %d)!\n", fd, type);
            handle = NULL;
            break;
#endif
        case UV_NAMED_PIPE:
            handle = malloc(sizeof(uv_pipe_t));
            if (uv_pipe_init(jl_io_loop, (uv_pipe_t*)handle, (readable?UV_PIPE_READABLE:UV_PIPE_WRITEABLE))) {
                jl_errorf("Error initializing stdio in uv_pipe_init (%d, %d)\n", fd, type);
                abort();
            }
            if (uv_pipe_open((uv_pipe_t*)handle,fd)) {
                jl_errorf("Error initializing stdio in uv_pipe_open (%d, %d)\n", fd, type);
                abort();
            }
            ((uv_pipe_t*)handle)->data=0;
            break;
        case UV_TCP:
        case UV_UDP:
        default:
            jl_errorf("This type of handle for stdio is not yet supported (%d, %d)!\n", fd, type);
            handle = NULL;
            break;
    }
    return handle;
}
Пример #9
0
Файл: tty.c Проект: benoitc/pyuv
static int
TTY_tp_init(TTY *self, PyObject *args, PyObject *kwargs)
{
    int fd, r;
    uv_tty_t *uv_tty;
    Loop *loop;
    PyObject *readable;
    PyObject *tmp = NULL;

    UNUSED_ARG(kwargs);

    if (UV_HANDLE(self)) {
        PyErr_SetString(PyExc_StreamError, "Object already initialized");
        return -1;
    }

    if (!PyArg_ParseTuple(args, "O!iO!:__init__", &LoopType, &loop, &fd, &PyBool_Type, &readable)) {
        return -1;
    }

    tmp = (PyObject *)((Handle *)self)->loop;
    Py_INCREF(loop);
    ((Handle *)self)->loop = loop;
    Py_XDECREF(tmp);

    uv_tty = PyMem_Malloc(sizeof(uv_tty_t));
    if (!uv_tty) {
        PyErr_NoMemory();
        Py_DECREF(loop);
        return -1;
    }

    r = uv_tty_init(UV_HANDLE_LOOP(self), uv_tty, fd, (readable == Py_True) ? 1 : 0);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TTYError);
        Py_DECREF(loop);
        return -1;
    }
    uv_tty->data = (void *)self;
    UV_HANDLE(self) = (uv_handle_t *)uv_tty;

    return 0;
}
Пример #10
0
int main(void)
{
	uv_loop_t loop;

	struct sockaddr_in addr;
	uv_connect_t connect_req;

	int fd, r;

	/* loop init */
	uv_loop_init(&loop);

#if USE_PIPE
	uv_pipe_init(&loop, &client, 1);
	uv_pipe_connect(&connect_req, &client, "/var/tmp/pipe.server1",
			connect_cb);

#else
	/*tcp socket initial */
	uv_ip4_addr("127.0.0.1", 4789, &addr);

	uv_tcp_init(&loop, &client);

	uv_tcp_connect(&connect_req, &client,
		       (const struct sockaddr *)&addr, connect_cb);
#endif

	r = uv_tty_init(&loop, &tty, 0, 1);
	printf("r=%d\n", r);
	uv_read_start((uv_stream_t *) & tty, alloc_cb, recv_cb);

	uv_run(&loop, UV_RUN_DEFAULT);

	printf("end\n");
	return 0;
}
Пример #11
0
void stdinReadStart(WrenVM* vm)
{
  if (stdinStream == NULL)
  {
    if (uv_guess_handle(stdinDescriptor) == UV_TTY)
    {
      // stdin is connected to a terminal.
      uv_tty_t* handle = (uv_tty_t*)malloc(sizeof(uv_tty_t));
      uv_tty_init(getLoop(), handle, stdinDescriptor, true);
      stdinStream = (uv_stream_t*)handle;
    }
    else
    {
      // stdin is a pipe or a file.
      uv_pipe_t* handle = (uv_pipe_t*)malloc(sizeof(uv_pipe_t));
      uv_pipe_init(getLoop(), handle, false);
      uv_pipe_open(handle, stdinDescriptor);
      stdinStream = (uv_stream_t*)handle;
    }
  }

  uv_read_start(stdinStream, allocCallback, stdinReadCallback);
  // TODO: Check return.
}
Пример #12
0
int main(int argc, char **argv)
{
  if (!owns_tty()) {
    fprintf(stderr, "process does not own the terminal\n");
    exit(2);
  }

  if (!is_terminal(stdin)) {
    fprintf(stderr, "stdin is not a terminal\n");
    exit(2);
  }

  if (!is_terminal(stdout)) {
    fprintf(stderr, "stdout is not a terminal\n");
    exit(2);
  }

  if (!is_terminal(stderr)) {
    fprintf(stderr, "stderr is not a terminal\n");
    exit(2);
  }

  if (argc > 1) {
    errno = 0;
    int count = (int)strtol(argv[1], NULL, 10);
    if (errno != 0) {
      abort();
    }
    count = (count < 0 || count > 99999) ? 0 : count;
    for (int i = 0; i < count; i++) {
      printf("line%d\n", i);
    }
    fflush(stdout);
    return 0;
  }

  int interrupted = 0;
  uv_prepare_t prepare;
  uv_prepare_init(uv_default_loop(), &prepare);
  uv_prepare_start(&prepare, prepare_cb);
#ifndef WIN32
  uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1);
#else
  uv_tty_init(uv_default_loop(), &tty, fileno(stdin), 1);
  uv_tty_init(uv_default_loop(), &tty_out, fileno(stdout), 0);
  int width, height;
  uv_tty_get_winsize(&tty_out, &width, &height);
#endif
  uv_tty_set_mode(&tty, UV_TTY_MODE_RAW);
  tty.data = &interrupted;
  uv_read_start(STRUCT_CAST(uv_stream_t, &tty), alloc_cb, read_cb);
#ifndef WIN32
  struct sigaction sa;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = sig_handler;
  sigaction(SIGHUP, &sa, NULL);
  sigaction(SIGWINCH, &sa, NULL);
#else
  uv_signal_t sigwinch_watcher;
  uv_signal_init(uv_default_loop(), &sigwinch_watcher);
  uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH);
#endif
  uv_run(uv_default_loop(), UV_RUN_DEFAULT);

#ifndef WIN32
  // XXX: Without this the SIGHUP handler is skipped on some systems.
  sleep(100);
#endif

  return 0;
}
Пример #13
0
Файл: mo_tty.cpp Проект: wehu/mo
TTY::TTY(uv_file fd, int readable){
  int r = uv_tty_init(uv_default_loop(), (uv_tty_t*)&handle, fd, readable);
  if(r) Logger::Err("uv_tty_init failed! : %d", r);
}
Пример #14
0
Файл: init.c Проект: aviks/julia
void *init_stdio_handle(uv_file fd,int readable)
{
    void *handle;
    uv_handle_type type = uv_guess_handle(fd);
    jl_uv_file_t *file;
#ifndef _OS_WINDOWS_
    // Duplicate the file descritor so we can later dup it over if we want to redirect
    // STDIO without having to worry about closing the associated libuv object.
    // On windows however, libuv objects remember streams by their HANDLE, so this is
    // unnessecary.
    fd = dup(fd);
#endif
    //printf("%d: %d -- %d\n", fd, type);
    switch(type)
    {
    case UV_TTY:
        handle = malloc(sizeof(uv_tty_t));
        if (uv_tty_init(jl_io_loop,(uv_tty_t*)handle,fd,readable)) {
            jl_errorf("Error initializing stdio in uv_tty_init (%d, %d)\n", fd, type);
            abort();
        }
        ((uv_tty_t*)handle)->data=0;
        uv_tty_set_mode((void*)handle,0); //cooked stdio
        break;
    case UV_FILE:
        file = malloc(sizeof(jl_uv_file_t));
        file->loop = jl_io_loop;
        file->type = UV_FILE;
        file->file = fd;
        file->data = 0;
        handle = file;
        break;
    case UV_NAMED_PIPE:
        handle = malloc(sizeof(uv_pipe_t));
        if (uv_pipe_init(jl_io_loop, (uv_pipe_t*)handle, (readable?UV_PIPE_READABLE:UV_PIPE_WRITABLE))) {
            jl_errorf("Error initializing stdio in uv_pipe_init (%d, %d)\n", fd, type);
            abort();
        }
        if (uv_pipe_open((uv_pipe_t*)handle,fd)) {
            jl_errorf("Error initializing stdio in uv_pipe_open (%d, %d)\n", fd, type);
            abort();
        }
        ((uv_pipe_t*)handle)->data=0;
        break;
    case UV_TCP:
        handle = malloc(sizeof(uv_tcp_t));
        if (uv_tcp_init(jl_io_loop, (uv_tcp_t*)handle)) {
            jl_errorf("Error initializing stdio in uv_tcp_init (%d, %d)\n", fd, type);
            abort();
        }
        if (uv_tcp_open((uv_tcp_t*)handle,fd)) {
            jl_errorf("Error initializing stdio in uv_tcp_open (%d, %d)\n", fd, type);
            abort();
        }
        ((uv_tcp_t*)handle)->data=0;
        break;
    case UV_UDP:
    default:
        jl_errorf("This type of handle for stdio is not yet supported (%d, %d)!\n", fd, type);
        handle = NULL;
        break;
    }
    return handle;
}
Пример #15
0
extern "C" int
rust_uv_tty_init(uv_loop_t *loop, uv_tty_t *tty, int fd, int readable) {
    return uv_tty_init(loop, tty, fd, readable);
}