summaryrefslogtreecommitdiff
path: root/libps/tty.c
blob: d668827f6cc222d6a1d31021b3dcf61bdb3c908b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* The type ps_tty_t, for per-tty info.

   Copyright (C) 1995 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   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., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <hurd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <hurd/term.h>

#include "ps.h"
#include "common.h"

/* ---------------------------------------------------------------- */

/* Create a ps_tty_t for the tty referred to by PORT, returning it in TTY.
   If a memory allocation error occurs, ENOMEM is returned, otherwise 0.  */
error_t
ps_tty_create(file_t port, ps_tty_t *tty)
{
  *tty = NEW(struct ps_tty);
  if (*tty == NULL)
    return ENOMEM;

  (*tty)->port = port;
  (*tty)->name_state = PS_TTY_NAME_PENDING;
  (*tty)->short_name = NULL;
  (*tty)->short_name_alloced = FALSE;

  return 0;
}

/* Frees TTY and any resources it consumes.  */
void
ps_tty_free(ps_tty_t tty)
{
  mach_port_deallocate(mach_task_self(), tty->port);
  if (tty->name_state == PS_TTY_NAME_OK && tty->name != NULL)
    free(tty->name);
  if (tty->short_name_alloced)
    free(tty->short_name);
  free(tty);
}

/* ---------------------------------------------------------------- */

/* Returns the name of the tty, or NULL if it can't be figured out.  */
char *ps_tty_name(ps_tty_t tty)
{
  if (tty->name_state == PS_TTY_NAME_PENDING)
    {
      string_t buf;

      if (term_get_nodename(tty->port, buf) != 0)
	/* There is a terminal there, but we can't figure out its name.  */
	tty->name_state = PS_TTY_NAME_ERROR;
      else
	{
	  tty->name = NEWVEC(char, strlen(buf) + 1);
	  if (tty->name == NULL)
	    tty->name_state = PS_TTY_NAME_ERROR;
	  else
	    {
	      strcpy(tty->name, buf);
	      tty->name_state = PS_TTY_NAME_OK;
	    }
	}
    }

  if (tty->name_state == PS_TTY_NAME_OK)
    return tty->name;
  else
    return NULL;
}

/* ---------------------------------------------------------------- */

struct ps_tty_abbrev
{
  char *pfx;
  char *subst;
};

struct ps_tty_abbrev ps_tty_abbrevs[] =
{
  { "/tmp/console", "oc" },	/* temp hack */
  { "/dev/console", "co"},
  { "/dev/tty",     ""},
  { "/dev/pty",     ""},
  { "/dev/",	    ""},
  { 0 }
};

/* Returns the standard abbreviated name of the tty, the whole name if there
   is no standard abbreviation, or NULL if it can't be figured out.  */
char *
ps_tty_short_name(ps_tty_t tty)
{
  if (tty->short_name != NULL)
    return tty->short_name;
  else
    {
      struct ps_tty_abbrev *abbrev;
      char *name = ps_tty_name(tty);

      if (name)
	for (abbrev = ps_tty_abbrevs; abbrev->pfx != NULL; abbrev++)
	  {
	    char *subst = abbrev->subst;
	    unsigned pfx_len = strlen(abbrev->pfx);

	    if (strncmp(name, abbrev->pfx, pfx_len) == 0)
	      {
		if (name[pfx_len] == '\0')
		  tty->short_name = abbrev->subst;
		else if ((subst[0] = '\0'))
		  tty->short_name = name + pfx_len;
		else
		  {
		    tty->short_name =
		      malloc(strlen(subst) + strlen(name + pfx_len));
		    if (tty->short_name)
		      {
			tty->short_name_alloced = TRUE;
			strcpy(tty->short_name, subst);
			strcat(tty->short_name, name + pfx_len);
		      }
		  }
		break;
	      }
	  }

      if (tty->short_name == NULL)
	tty->short_name = name;

      return tty->short_name;
    }
}