From: Ray Strode Date: Wed, 16 May 2007 16:04:14 +0000 (-0400) Subject: add the beginning of an api for managing ptys X-Git-Tag: 0.1.0~297 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ce3f6f9f387e55368d11e1e476f53cf6189073cf;p=thirdparty%2Fplymouth.git add the beginning of an api for managing ptys --- diff --git a/src/ply-terminal.c b/src/ply-terminal.c new file mode 100644 index 00000000..11876bed --- /dev/null +++ b/src/ply-terminal.c @@ -0,0 +1,174 @@ +/* ply-terminal.c - psuedoterminal abstraction + * + * Copyright (C) 2006, 2007 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written by: Kristian Høgsberg + * Ray Strode + */ +#include "config.h" +#include "ply-terminal.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ply-utils.h" + +struct _ply_terminal +{ + char *name; + int fd; +}; + +ply_terminal_t * +ply_terminal_new (void) +{ + ply_terminal_t *terminal; + + terminal = calloc (1, sizeof (ply_terminal_t)); + terminal->fd = -1; + + return terminal; +} + +void +ply_terminal_free (ply_terminal_t *terminal) +{ + assert (terminal != NULL); + + ply_terminal_close (terminal); + free (terminal); +} + +bool +ply_terminal_open (ply_terminal_t *terminal) +{ + int saved_errno; + + assert (terminal != NULL); + assert (!ply_terminal_is_open (terminal)); + + terminal->fd = posix_openpt (O_RDWR | O_NOCTTY); + + if (terminal->fd < 0) + return false; + + if (grantpt (terminal->fd) < 0) + { + saved_errno = errno; + ply_terminal_close (terminal); + errno = saved_errno; + return false; + } + + if (unlockpt (terminal->fd) < 0) + { + saved_errno = errno; + ply_terminal_close (terminal); + errno = saved_errno; + return false; + } + + terminal->name = strdup (ptsname (terminal->fd)); + + return true; +} + +bool +ply_terminal_is_open (ply_terminal_t *terminal) +{ + assert (terminal != NULL); + + return terminal->fd >= 0; +} + +void +ply_terminal_close (ply_terminal_t *terminal) +{ + assert (terminal != NULL); + + free (terminal->name); + terminal->name = NULL; + + close (terminal->fd); + terminal->fd = -1; +} + +int +ply_terminal_get_fd (ply_terminal_t *terminal) +{ + assert (terminal != NULL); + + return terminal->fd; +} + +char * +ply_terminal_get_name (ply_terminal_t *terminal) +{ + assert (terminal != NULL); + assert (ply_terminal_is_open (terminal)); + + assert (terminal->name != NULL); + return strdup (terminal->name); +} + +#ifdef PLY_TERMINAL_ENABLE_TEST + +#include + +int +main (int argc, + char **argv) +{ + ply_terminal_t *terminal; + char *name; + uint8_t byte; + int exit_code; + + exit_code = 0; + + terminal = ply_terminal_new (); + + if (!ply_terminal_open (terminal)) + { + exit_code = errno; + perror ("could not open new terminal"); + return exit_code; + } + + name = ply_terminal_get_name (terminal); + printf ("terminal name is '%s'\n", name); + + while (read (ply_terminal_get_fd (terminal), + &byte, sizeof (byte)) == 1) + printf ("%c", byte); + + ply_terminal_free (terminal); + + return exit_code; +} + +#endif /* PLY_TERMINAL_ENABLE_TEST */ +/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */ diff --git a/src/ply-terminal.h b/src/ply-terminal.h new file mode 100644 index 00000000..cfdc3c57 --- /dev/null +++ b/src/ply-terminal.h @@ -0,0 +1,42 @@ +/* ply-terminal.h - psuedoterminal abstraction + * + * Copyright (C) 2007 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written By: Ray Strode + */ +#ifndef PLY_TERMINAL_H +#define PLY_TERMINAL_H + +#include +#include +#include + +typedef struct _ply_terminal ply_terminal_t; + +#ifndef PLY_HIDE_FUNCTION_DECLARATIONS +ply_terminal_t *ply_terminal_new (); +void ply_terminal_free (ply_terminal_t *terminal); +bool ply_terminal_open (ply_terminal_t *terminal); +bool ply_terminal_is_open (ply_terminal_t *terminal); +void ply_terminal_close (ply_terminal_t *terminal); +int ply_terminal_get_fd (ply_terminal_t *terminal); +char *ply_terminal_get_name (ply_terminal_t *terminal); +#endif + +#endif /* PLY_TERMINAL_H */ +/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */