OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [expect/] [pty_sgttyb.c] - Blame information for rev 1778

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* pty_bsd.c - routines to allocate ptys - BSD version
2
 
3
Written by: Don Libes, NIST, 2/6/90
4
 
5
Design and implementation of this program was paid for by U.S. tax
6
dollars.  Therefore it is public domain.  However, the author and NIST
7
would appreciate credit if this program or parts of it are used.
8
 
9
*/
10
 
11
#include <stdio.h>              /* tmp for debugging */
12
#include <signal.h>
13
 
14
#if defined(SIGCLD) && !defined(SIGCHLD)
15
#define SIGCHLD SIGCLD
16
#endif
17
 
18
#include <sys/types.h>
19
#include <sys/stat.h>
20
/*** #include <sys/ioctl.h> ***/
21
#include <sys/file.h>
22
#include <signal.h>
23
#include <setjmp.h>
24
#include "expect_cf.h"
25
#include "exp_rename.h"
26
#include "exp_tty_in.h"
27
#include "exp_pty.h"
28
 
29
void debuglog();
30
 
31
#ifndef TRUE
32
#define TRUE 1
33
#define FALSE 0
34
#endif
35
 
36
static char     master_name[] = "/dev/ptyXX";   /* master */
37
static char      slave_name[] = "/dev/ttyXX";   /* slave */
38
static char     *tty_type;              /* ptr to char [pt] denoting
39
                                           whether it is a pty or tty */
40
static char     *tty_bank;              /* ptr to char [p-z] denoting
41
                                           which bank it is */
42
static char     *tty_num;               /* ptr to char [0-f] denoting
43
                                           which number it is */
44
char *exp_pty_slave_name;
45
char *exp_pty_error;
46
 
47
static void
48
pty_stty(s,name)
49
char *s;                /* args to stty */
50
char *name;             /* name of pty */
51
{
52
#define MAX_ARGLIST 10240
53
        char buf[MAX_ARGLIST];  /* overkill is easier */
54
        RETSIGTYPE (*old)();    /* save old sigalarm handler */
55
 
56
#ifdef STTY_READS_STDOUT
57
        sprintf(buf,"/bin/stty %s > %s",s,name);
58
#else
59
        sprintf(buf,"/bin/stty %s < %s",s,name);
60
#endif
61
        old = signal(SIGCHLD, SIG_DFL);
62
        system(buf);
63
        signal(SIGCHLD, old);   /* restore signal handler */
64
}
65
 
66
int exp_dev_tty;        /* file descriptor to /dev/tty or -1 if none */
67
static int knew_dev_tty;/* true if we had our hands on /dev/tty at any time */
68
 
69
#ifdef TIOCGWINSZ
70
static struct winsize winsize = {0, 0};
71
#endif
72
#if defined(TIOCGSIZE) && !defined(TIOCGWINSZ)
73
static struct ttysize winsize = {0, 0};
74
#endif
75
 
76
exp_tty exp_tty_original;
77
 
78
#define GET_TTYTYPE     0
79
#define SET_TTYTYPE     1
80
static void
81
ttytype(request,fd,ttycopy,ttyinit,s)
82
int request;
83
int fd;
84
                /* following are used only if request == SET_TTYTYPE */
85
int ttycopy;    /* if true, copy from /dev/tty */
86
int ttyinit;    /* if true, initialize to sane state */
87
char *s;        /* stty args */
88
{
89
        static struct   tchars tc;              /* special characters */
90
        static struct   ltchars lc;             /* local special characters */
91
        static struct   winsize win;            /* window size */
92
        static int      lb;                     /* local modes */
93
        static int      l;                      /* line discipline */
94
 
95
        if (request == GET_TTYTYPE) {
96
                if (-1 == ioctl(fd, TIOCGETP, (char *)&exp_tty_original)
97
                 || -1 == ioctl(fd, TIOCGETC, (char *)&tc)
98
                 || -1 == ioctl(fd, TIOCGETD, (char *)&l)
99
                 || -1 == ioctl(fd, TIOCGLTC, (char *)&lc)
100
                 || -1 == ioctl(fd, TIOCLGET, (char *)&lb)
101
                 || -1 == ioctl(fd, TIOCGWINSZ, (char *)&win)) {
102
                        knew_dev_tty = FALSE;
103
                        exp_dev_tty = -1;
104
                }
105
#ifdef TIOCGWINSZ
106
                ioctl(fd,TIOCGWINSZ,&winsize);
107
#endif
108
#if defined(TIOCGSIZE) && !defined(TIOCGWINSZ)
109
                ioctl(fd,TIOCGSIZE,&winsize);
110
#endif
111
        } else {        /* type == SET_TTYTYPE */
112
                if (ttycopy && knew_dev_tty) {
113
                        (void) ioctl(fd, TIOCSETP, (char *)&exp_tty_current);
114
                        (void) ioctl(fd, TIOCSETC, (char *)&tc);
115
                        (void) ioctl(fd, TIOCSLTC, (char *)&lc);
116
                        (void) ioctl(fd, TIOCLSET, (char *)&lb);
117
                        (void) ioctl(fd, TIOCSETD, (char *)&l);
118
                        (void) ioctl(fd, TIOCSWINSZ, (char *)&win);
119
#ifdef TIOCSWINSZ
120
                        ioctl(fd,TIOCSWINSZ,&winsize);
121
#endif
122
#if defined(TIOCSSIZE) && !defined(TIOCSWINSZ)
123
                        ioctl(fd,TIOCGSIZE,&winsize);
124
#endif
125
                }
126
 
127
#ifdef __CENTERLINE__
128
#undef DFLT_STTY
129
#define DFLT_STTY "sane"
130
#endif
131
 
132
/* Apollo Domain doesn't need this */
133
#ifdef DFLT_STTY
134
                if (ttyinit) {
135
                        /* overlay parms originally supplied by Makefile */
136
                        pty_stty(DFLT_STTY,slave_name);
137
                }
138
#endif
139
 
140
                /* lastly, give user chance to override any terminal parms */
141
                if (s) {
142
                        pty_stty(s,slave_name);
143
                }
144
        }
145
}
146
 
147
void
148
exp_init_pty()
149
{
150
        tty_type = & slave_name[strlen("/dev/")];
151
        tty_bank = &master_name[strlen("/dev/pty")];
152
        tty_num  = &master_name[strlen("/dev/ptyp")];
153
 
154
        exp_dev_tty = open("/dev/tty",O_RDWR);
155
 
156
#if experimental
157
        /* code to allocate force expect to get a controlling tty */
158
        /* even if it doesn't start with one (i.e., under cron). */
159
        /* This code is not necessary, but helpful for testing odd things. */
160
        if (exp_dev_tty == -1) {
161
                /* give ourselves a controlling tty */
162
                int master = getptymaster();
163
                fcntl(master,F_SETFD,1);        /* close-on-exec */
164
                setpgrp(0,0);
165
                close(0);
166
                close(1);
167
                getptyslave(exp_get_var(exp_interp,"stty_init"));
168
                close(2);
169
                fcntl(0,F_DUPFD,2);              /* dup 0 onto 2 */
170
        }
171
#endif
172
 
173
        knew_dev_tty = (exp_dev_tty != -1);
174
        if (knew_dev_tty) ttytype(GET_TTYTYPE,exp_dev_tty,0,0,(char *)0);
175
}
176
 
177
/* returns fd of master end of pseudotty */
178
int
179
getptymaster()
180
{
181
        int master = -1;
182
        char *hex, *bank;
183
        struct stat statbuf;
184
 
185
        exp_pty_error = 0;
186
 
187
        if (exp_pty_test_start() == -1) return -1;
188
 
189
        for (bank = "pqrstuvwxyzPQRSTUVWXYZ";*bank;bank++) {
190
                *tty_bank = *bank;
191
                *tty_num = '0';
192
                if (stat(master_name, &statbuf) < 0) break;
193
                for (hex = "0123456789abcdef";*hex;hex++) {
194
                        *tty_num = *hex;
195
 
196
                        /* generate slave name from master */
197
                        strcpy(slave_name,master_name);
198
                        *tty_type = 't';
199
 
200
                        master = exp_pty_test(master_name,slave_name,
201
                                                *tty_bank,tty_num);
202
                        if (master >= 0) goto done;
203
                }
204
        }
205
 done:
206
        exp_pty_test_end();
207
        exp_pty_slave_name = slave_name;
208
        return(master);
209
}
210
 
211
/* see comment in pty_termios.c */
212
/*ARGSUSED*/
213
void
214
exp_slave_control(master,control)
215
int master;
216
int control;
217
{
218
}
219
 
220
int
221
getptyslave(ttycopy,ttyinit,stty_args)
222
int ttycopy;
223
int ttyinit;
224
char *stty_args;
225
{
226
        int slave;
227
 
228
        if (0 > (slave = open(slave_name, O_RDWR))) return(-1);
229
 
230
        if (0 == slave) {
231
                /* if opened in a new process, slave will be 0 (and */
232
                /* ultimately, 1 and 2 as well) */
233
 
234
                /* duplicate 0 onto 1 and 2 to prepare for stty */
235
                fcntl(0,F_DUPFD,1);
236
                fcntl(0,F_DUPFD,2);
237
        }
238
 
239
        ttytype(SET_TTYTYPE,slave,ttycopy,ttyinit,stty_args);
240
        (void) exp_pty_unlock();
241
        return(slave);
242
}
243
 
244
void
245
exp_pty_exit()
246
{
247
        /* a stub so we can do weird things on the cray */
248
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.