[libply] Drop terminal class

It was a really lame small wrapper around creating pseudoterminals.
The wrapper didn't buy anything and the name ply-terminal would be
better used for managing tty settings, etc.
This commit is contained in:
Ray Strode 2009-08-26 23:09:46 -04:00
parent a8f0572cb2
commit d338e6becb
7 changed files with 57 additions and 277 deletions

View file

@ -23,7 +23,6 @@ libply_HEADERS = \
ply-logger.h \
ply-key-file.h \
ply-progress.h \
ply-terminal.h \
ply-terminal-session.h \
ply-trigger.h \
ply-utils.h
@ -45,7 +44,6 @@ libply_la_SOURCES = ply-event-loop.c \
ply-logger.c \
ply-key-file.c \
ply-progress.c \
ply-terminal.c \
ply-terminal-session.c \
ply-trigger.c \
ply-utils.c

View file

@ -39,12 +39,11 @@
#include "ply-event-loop.h"
#include "ply-logger.h"
#include "ply-terminal.h"
#include "ply-utils.h"
struct _ply_terminal_session
{
ply_terminal_t *terminal;
int pseudoterminal_master_fd;
ply_logger_t *logger;
ply_event_loop_t *loop;
char **argv;
@ -71,7 +70,7 @@ ply_terminal_session_open_console (ply_terminal_session_t *session)
int fd;
const char *terminal_name;
terminal_name = ply_terminal_get_device_name (session->terminal);
terminal_name = ptsname (session->pseudoterminal_master_fd);
fd = open (terminal_name, O_RDONLY);
@ -128,8 +127,8 @@ ply_terminal_session_new (const char * const *argv)
assert (argv == NULL || argv[0] != NULL);
session = calloc (1, sizeof (ply_terminal_session_t));
session->pseudoterminal_master_fd = -1;
session->argv = argv == NULL ? NULL : ply_copy_string_array (argv);
session->terminal = ply_terminal_new ();
session->logger = ply_logger_new ();
session->is_running = false;
session->console_is_redirected = false;
@ -147,7 +146,8 @@ ply_terminal_session_free (ply_terminal_session_t *session)
ply_logger_free (session->logger);
ply_free_string_array (session->argv);
ply_terminal_free (session->terminal);
close (session->pseudoterminal_master_fd);
free (session);
}
@ -181,7 +181,7 @@ ply_terminal_session_redirect_console (ply_terminal_session_t *session)
assert (session != NULL);
terminal_name = ply_terminal_get_device_name (session->terminal);
terminal_name = ptsname (session->pseudoterminal_master_fd);
assert (terminal_name != NULL);
@ -218,6 +218,49 @@ ply_terminal_session_unredirect_console (ply_terminal_session_t *session)
session->console_is_redirected = false;
}
static void
close_pseudoterminal (ply_terminal_session_t *session)
{
close (session->pseudoterminal_master_fd);
session->pseudoterminal_master_fd = -1;
}
static bool
open_pseudoterminal (ply_terminal_session_t *session)
{
ply_trace ("opening device '/dev/ptmx'");
session->pseudoterminal_master_fd = posix_openpt (O_RDWR | O_NOCTTY);
if (session->pseudoterminal_master_fd < 0)
return false;
ply_trace (" opened device '/dev/ptmx'");
ply_trace ("creating pseudoterminal");
if (grantpt (session->pseudoterminal_master_fd) < 0)
{
ply_save_errno ();
ply_trace ("could not create psuedoterminal: %m");
close_pseudoterminal (session);
ply_restore_errno ();
return false;
}
ply_trace ("done creating pseudoterminal");
ply_trace ("unlocking pseudoterminal");
if (unlockpt (session->pseudoterminal_master_fd) < 0)
{
ply_save_errno ();
close_pseudoterminal (session);
ply_restore_errno ();
return false;
}
ply_trace ("unlocked pseudoterminal");
return true;
}
bool
ply_terminal_session_run (ply_terminal_session_t *session,
ply_terminal_session_flags_t flags,
@ -240,7 +283,7 @@ ply_terminal_session_run (ply_terminal_session_t *session,
(flags & PLY_TERMINAL_SESSION_FLAGS_REDIRECT_CONSOLE) != 0;
ply_trace ("creating terminal device");
if (!ply_terminal_create_device (session->terminal))
if (!open_pseudoterminal (session))
return false;
ply_trace ("done creating terminal device");
@ -250,7 +293,7 @@ ply_terminal_session_run (ply_terminal_session_t *session,
!ply_terminal_session_redirect_console (session))
{
ply_save_errno ();
ply_terminal_destroy_device (session->terminal);
close_pseudoterminal (session);
ply_restore_errno ();
return false;
}
@ -264,7 +307,7 @@ ply_terminal_session_run (ply_terminal_session_t *session,
{
ply_save_errno ();
ply_terminal_session_unredirect_console (session);
ply_terminal_destroy_device (session->terminal);
close_pseudoterminal (session);
ply_restore_errno ();
return false;
}
@ -316,12 +359,12 @@ ply_terminal_session_attach (ply_terminal_session_t *session,
if (ptmx >= 0)
{
ply_trace ("ptmx passed in, using it");
ply_terminal_set_fd(session->terminal, ptmx);
session->pseudoterminal_master_fd = ptmx;
}
else
{
ply_trace ("ptmx not passed in, creating one");
if (!ply_terminal_create_device (session->terminal))
if (!open_pseudoterminal (session))
{
ply_trace ("could not create pseudo-terminal: %m");
return false;
@ -336,7 +379,7 @@ ply_terminal_session_attach (ply_terminal_session_t *session,
!ply_terminal_session_redirect_console (session))
{
ply_save_errno ();
ply_terminal_destroy_device (session->terminal);
close_pseudoterminal (session);
ply_restore_errno ();
return false;
}
@ -370,7 +413,7 @@ ply_terminal_session_detach (ply_terminal_session_t *session)
if (session->created_terminal_device)
{
ply_trace ("ptmx wasn't originally passed in, destroying created one");
ply_terminal_destroy_device (session->terminal);
close_pseudoterminal (session);
session->created_terminal_device = false;
}
@ -386,7 +429,7 @@ ply_terminal_session_get_fd (ply_terminal_session_t *session)
{
assert (session != NULL);
return ply_terminal_get_fd (session->terminal);
return session->pseudoterminal_master_fd;
}
static void

View file

@ -1,201 +0,0 @@
/* 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 <krh@redhat.com>
* Ray Strode <rstrode@redhat.com>
*/
#include "config.h"
#include "ply-terminal.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "ply-logger.h"
#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_destroy_device (terminal);
free (terminal);
}
bool
ply_terminal_create_device (ply_terminal_t *terminal)
{
assert (terminal != NULL);
assert (!ply_terminal_has_device (terminal));
ply_trace ("opening device '/dev/ptmx'");
terminal->fd = posix_openpt (O_RDWR | O_NOCTTY);
if (terminal->fd < 0)
return false;
ply_trace (" opened device '/dev/ptmx'");
ply_trace ("creating pseudoterminal");
if (grantpt (terminal->fd) < 0)
{
ply_save_errno ();
ply_trace ("could not create psuedoterminal: %m");
ply_terminal_destroy_device (terminal);
ply_restore_errno ();
return false;
}
ply_trace ("done creating pseudoterminal");
ply_trace ("unlocking pseudoterminal");
if (unlockpt (terminal->fd) < 0)
{
ply_save_errno ();
ply_terminal_destroy_device (terminal);
ply_restore_errno ();
return false;
}
ply_trace ("unlocked pseudoterminal");
terminal->name = strdup (ptsname (terminal->fd));
ply_trace ("pseudoterminal '%s' ready for action", terminal->name);
return true;
}
bool
ply_terminal_has_device (ply_terminal_t *terminal)
{
assert (terminal != NULL);
return terminal->fd >= 0;
}
void
ply_terminal_destroy_device (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;
}
void
ply_terminal_set_fd (ply_terminal_t *terminal, int fd)
{
assert (terminal != NULL);
terminal->fd = fd;
if (terminal->name)
{
free(terminal->name);
terminal->name = NULL;
}
if (terminal->fd >= 0)
terminal->name = strdup (ptsname (terminal->fd));
}
const char *
ply_terminal_get_device_name (ply_terminal_t *terminal)
{
assert (terminal != NULL);
assert (ply_terminal_has_device (terminal));
assert (terminal->name != NULL);
return terminal->name;
}
#ifdef PLY_TERMINAL_ENABLE_TEST
#include <stdio.h>
int
main (int argc,
char **argv)
{
ply_terminal_t *terminal;
const char *name;
uint8_t byte;
int exit_code;
exit_code = 0;
terminal = ply_terminal_new ();
if (!ply_terminal_create_device (terminal))
{
exit_code = errno;
perror ("could not open new terminal");
return exit_code;
}
name = ply_terminal_get_device_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: */

View file

@ -1,43 +0,0 @@
/* 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 <rstrode@redhat.com>
*/
#ifndef PLY_TERMINAL_H
#define PLY_TERMINAL_H
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
typedef struct _ply_terminal ply_terminal_t;
#ifndef PLY_HIDE_FUNCTION_DECLARATIONS
ply_terminal_t *ply_terminal_new (void);
void ply_terminal_free (ply_terminal_t *terminal);
bool ply_terminal_create_device (ply_terminal_t *terminal);
bool ply_terminal_has_device (ply_terminal_t *terminal);
void ply_terminal_destroy_device (ply_terminal_t *terminal);
int ply_terminal_get_fd (ply_terminal_t *terminal);
void ply_terminal_set_fd (ply_terminal_t *terminal, int fd);
const char *ply_terminal_get_device_name (ply_terminal_t *terminal);
#endif
#endif /* PLY_TERMINAL_H */
/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */

View file

@ -6,7 +6,6 @@ TESTS =
if ENABLE_TESTS
include $(srcdir)/ply-frame-buffer-test.am
include $(srcdir)/ply-terminal-test.am
include $(srcdir)/ply-terminal-session-test.am
include $(srcdir)/ply-logger-test.am
include $(srcdir)/ply-array-test.am

View file

@ -14,7 +14,5 @@ ply_terminal_session_test_SOURCES =
$(srcdir)/../ply-list.c \
$(srcdir)/../ply-event-loop.h \
$(srcdir)/../ply-event-loop.c \
$(srcdir)/../ply-terminal.h \
$(srcdir)/../ply-terminal.c \
$(srcdir)/../ply-terminal-session.h \
$(srcdir)/../ply-terminal-session.c

View file

@ -1,14 +0,0 @@
TESTS += ply-terminal-test
ply_terminal_test_CFLAGS = $(PLYMOUTH_CFLAGS) -DPLY_TERMINAL_ENABLE_TEST
ply_terminal_test_LDADD = $(PLYMOUTH_LIBS)
ply_terminal_test_SOURCES = \
$(srcdir)/../ply-list.h \
$(srcdir)/../ply-list.c \
$(srcdir)/../ply-logger.h \
$(srcdir)/../ply-logger.c \
$(srcdir)/../ply-utils.h \
$(srcdir)/../ply-utils.c \
$(srcdir)/../ply-terminal.h \
$(srcdir)/../ply-terminal.c