Merge from vendor branch GDB:
[dragonfly.git] / sys / emulation / svr4 / svr4_ttold.c
1 /*
2  * Copyright (c) 1998 Mark Newton
3  * Copyright (c) 1994 Christos Zoulas
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  * 
28  * $FreeBSD: src/sys/svr4/svr4_ttold.c,v 1.7 1999/12/08 12:00:49 newton Exp $
29  * $DragonFly: src/sys/emulation/svr4/Attic/svr4_ttold.c,v 1.6 2005/12/10 16:06:20 swildner Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/systm.h>
35 #include <sys/file.h>
36 #include <sys/filedesc.h>
37 #include <sys/ioctl_compat.h>
38 #include <sys/termios.h>
39 #include <sys/file2.h>
40
41 #include "svr4.h"
42 #include "svr4_util.h"
43 #include "svr4_ttold.h"
44 #include "svr4_ioctl.h"
45
46
47 static void svr4_tchars_to_bsd_tchars (const struct svr4_tchars *st,
48                                            struct tchars *bt);
49 static void bsd_tchars_to_svr4_tchars (const struct tchars *bt,
50                                            struct svr4_tchars *st);
51 static void svr4_sgttyb_to_bsd_sgttyb (const struct svr4_sgttyb *ss,
52                                            struct sgttyb *bs);
53 static void bsd_sgttyb_to_svr4_sgttyb (const struct sgttyb *bs,
54                                            struct svr4_sgttyb *ss);
55 static void svr4_ltchars_to_bsd_ltchars (const struct svr4_ltchars *sl,
56                                              struct ltchars *bl);
57 static void bsd_ltchars_to_svr4_ltchars (const struct ltchars *bl,
58                                              struct svr4_ltchars *sl);
59
60 #ifdef DEBUG_SVR4
61 static void print_svr4_sgttyb (const char *, struct svr4_sgttyb *);
62 static void print_svr4_tchars (const char *, struct svr4_tchars *);
63 static void print_svr4_ltchars (const char *, struct svr4_ltchars *);
64
65 static void
66 print_svr4_sgttyb(str, ss)
67         const char *str;
68         struct svr4_sgttyb *ss;
69 {
70
71         uprintf("%s\nispeed=%o ospeed=%o ", str, ss->sg_ispeed, ss->sg_ospeed);
72         uprintf("erase=%o kill=%o flags=%o\n", ss->sg_erase, ss->sg_kill,
73             ss->sg_flags);
74 }
75
76 static void
77 print_svr4_tchars(str, st)
78         const char *str;
79         struct svr4_tchars *st;
80 {
81         uprintf("%s\nintrc=%o quitc=%o ", str, st->t_intrc, st->t_quitc);
82         uprintf("startc=%o stopc=%o eofc=%o brkc=%o\n", st->t_startc,
83             st->t_stopc, st->t_eofc, st->t_brkc);
84 }
85
86 static void
87 print_svr4_ltchars(str, sl)
88         const char *str;
89         struct svr4_ltchars *sl;
90 {
91         uprintf("%s\nsuspc=%o dsuspc=%o ", str, sl->t_suspc, sl->t_dsuspc);
92         uprintf("rprntc=%o flushc=%o werasc=%o lnextc=%o\n", sl->t_rprntc,
93             sl->t_flushc, sl->t_werasc, sl->t_lnextc);
94 }
95 #endif /* DEBUG_SVR4 */
96
97 static void
98 svr4_tchars_to_bsd_tchars(const struct svr4_tchars *st, struct tchars *bt)
99 {
100         bt->t_intrc  = st->t_intrc;
101         bt->t_quitc  = st->t_quitc;
102         bt->t_startc = st->t_startc;
103         bt->t_stopc  = st->t_stopc;
104         bt->t_eofc   = st->t_eofc;
105         bt->t_brkc   = st->t_brkc;
106 }
107
108
109 static void
110 bsd_tchars_to_svr4_tchars(const struct tchars *bt, struct svr4_tchars *st)
111 {
112         st->t_intrc  = bt->t_intrc;
113         st->t_quitc  = bt->t_quitc;
114         st->t_startc = bt->t_startc;
115         st->t_stopc  = bt->t_stopc;
116         st->t_eofc   = bt->t_eofc;
117         st->t_brkc   = bt->t_brkc;
118 }
119
120
121 static void
122 svr4_sgttyb_to_bsd_sgttyb(const struct svr4_sgttyb *ss, struct sgttyb *bs)
123 {
124         bs->sg_ispeed = ss->sg_ispeed;
125         bs->sg_ospeed = ss->sg_ospeed;
126         bs->sg_erase  = ss->sg_erase;   
127         bs->sg_kill   = ss->sg_kill;
128         bs->sg_flags  = ss->sg_flags;
129 };
130
131
132 static void
133 bsd_sgttyb_to_svr4_sgttyb(const struct sgttyb *bs, struct svr4_sgttyb *ss)
134 {
135         ss->sg_ispeed = bs->sg_ispeed;
136         ss->sg_ospeed = bs->sg_ospeed;
137         ss->sg_erase  = bs->sg_erase;   
138         ss->sg_kill   = bs->sg_kill;
139         ss->sg_flags  = bs->sg_flags;
140 }
141
142
143 static void
144 svr4_ltchars_to_bsd_ltchars(const struct svr4_ltchars *sl, struct ltchars *bl)
145 {
146         bl->t_suspc  = sl->t_suspc;
147         bl->t_dsuspc = sl->t_dsuspc;
148         bl->t_rprntc = sl->t_rprntc;
149         bl->t_flushc = sl->t_flushc;
150         bl->t_werasc = sl->t_werasc;
151         bl->t_lnextc = sl->t_lnextc;
152 }
153
154
155 static void
156 bsd_ltchars_to_svr4_ltchars(const struct ltchars *bl, struct svr4_ltchars *sl)
157 {
158         sl->t_suspc  = bl->t_suspc;
159         sl->t_dsuspc = bl->t_dsuspc;
160         sl->t_rprntc = bl->t_rprntc;
161         sl->t_flushc = bl->t_flushc;
162         sl->t_werasc = bl->t_werasc;
163         sl->t_lnextc = bl->t_lnextc;
164 }
165
166
167 int
168 svr4_ttold_ioctl(struct file *fp, struct thread *td, register_t *retval,
169                  int fd, u_long cmd, caddr_t data)
170 {
171         int                     error;
172
173         *retval = 0;
174
175         switch (cmd) {
176         case SVR4_TIOCGPGRP:
177                 {
178                         pid_t pid;
179
180                         if ((error = fo_ioctl(fp, TIOCGPGRP, (caddr_t) &pid, td)) != 0)
181                             return error;
182
183                         DPRINTF(("TIOCGPGRP %d\n", pid));
184
185                         if ((error = copyout(&pid, data, sizeof(pid))) != 0)
186                                 return error;
187
188                 }
189
190         case SVR4_TIOCSPGRP:
191                 {
192                         pid_t pid;
193
194                         if ((error = copyin(data, &pid, sizeof(pid))) != 0)
195                                 return error;
196
197                         DPRINTF(("TIOCSPGRP %d\n", pid));
198
199                         return fo_ioctl(fp, TIOCSPGRP, (caddr_t) &pid, td);
200                 }
201
202         case SVR4_TIOCGSID:
203                 {
204 #if defined(TIOCGSID)
205                         pid_t pid;
206                         if ((error = fo_ioctl(fp, TIOCGSID, (caddr_t) &pid, td)) != 0)
207                                 return error;
208
209                         DPRINTF(("TIOCGSID %d\n", pid));
210
211                         return copyout(&pid, data, sizeof(pid));
212 #else
213                         uprintf("ioctl(TIOCGSID) for td %p unsupported\n", td);
214                         return EINVAL;
215 #endif
216                 }
217
218         case SVR4_TIOCGETP:
219                 {
220                         struct sgttyb bs;
221                         struct svr4_sgttyb ss;
222
223                         error = fo_ioctl(fp, TIOCGETP, (caddr_t) &bs, td);
224                         if (error)
225                                 return error;
226
227                         bsd_sgttyb_to_svr4_sgttyb(&bs, &ss);
228 #ifdef DEBUG_SVR4
229                         print_svr4_sgttyb("SVR4_TIOCGETP", &ss);
230 #endif /* DEBUG_SVR4 */
231                         return copyout(&ss, data, sizeof(ss));
232                 }
233
234         case SVR4_TIOCSETP:
235         case SVR4_TIOCSETN:
236                 {
237                         struct sgttyb bs;
238                         struct svr4_sgttyb ss;
239
240                         if ((error = copyin(data, &ss, sizeof(ss))) != 0)
241                                 return error;
242
243                         svr4_sgttyb_to_bsd_sgttyb(&ss, &bs);
244 #ifdef DEBUG_SVR4
245                         print_svr4_sgttyb("SVR4_TIOCSET{P,N}", &ss);
246 #endif /* DEBUG_SVR4 */
247                         cmd = (cmd == SVR4_TIOCSETP) ? TIOCSETP : TIOCSETN;
248                         return fo_ioctl(fp, cmd, (caddr_t) &bs, td);
249                 }
250
251         case SVR4_TIOCGETC:
252                 {
253                         struct tchars bt;
254                         struct svr4_tchars st;
255
256                         error = fo_ioctl(fp, TIOCGETC, (caddr_t) &bt, td);
257                         if (error)
258                                 return error;
259
260                         bsd_tchars_to_svr4_tchars(&bt, &st);
261 #ifdef DEBUG_SVR4
262                         print_svr4_tchars("SVR4_TIOCGETC", &st);
263 #endif /* DEBUG_SVR4 */
264                         return copyout(&st, data, sizeof(st));
265                 }
266
267         case SVR4_TIOCSETC:
268                 {
269                         struct tchars bt;
270                         struct svr4_tchars st;
271
272                         if ((error = copyin(data, &st, sizeof(st))) != 0)
273                                 return error;
274
275                         svr4_tchars_to_bsd_tchars(&st, &bt);
276 #ifdef DEBUG_SVR4
277                         print_svr4_tchars("SVR4_TIOCSETC", &st);
278 #endif /* DEBUG_SVR4 */
279                         return fo_ioctl(fp, TIOCSETC, (caddr_t) &bt, td);
280                 }
281
282         case SVR4_TIOCGLTC:
283                 {
284                         struct ltchars bl;
285                         struct svr4_ltchars sl;
286
287                         error = fo_ioctl(fp, TIOCGLTC, (caddr_t) &bl, td);
288                         if (error)
289                                 return error;
290
291                         bsd_ltchars_to_svr4_ltchars(&bl, &sl);
292 #ifdef DEBUG_SVR4
293                         print_svr4_ltchars("SVR4_TIOCGLTC", &sl);
294 #endif /* DEBUG_SVR4 */
295                         return copyout(&sl, data, sizeof(sl));
296                 }
297
298         case SVR4_TIOCSLTC:
299                 {
300                         struct ltchars bl;
301                         struct svr4_ltchars sl;
302
303                         if ((error = copyin(data, &sl, sizeof(sl))) != 0)
304                                 return error;
305
306                         svr4_ltchars_to_bsd_ltchars(&sl, &bl);
307 #ifdef DEBUG_SVR4
308                         print_svr4_ltchars("SVR4_TIOCSLTC", &sl);
309 #endif /* DEBUG_SVR4 */
310                         return fo_ioctl(fp, TIOCSLTC, (caddr_t) &bl, td);
311                 }
312
313         case SVR4_TIOCLGET:
314                 {
315                         int flags;
316                         if ((error = fo_ioctl(fp, TIOCLGET, (caddr_t) &flags, td)) != 0)
317                                 return error;
318                         DPRINTF(("SVR4_TIOCLGET %o\n", flags));
319                         return copyout(&flags, data, sizeof(flags));
320                 }
321
322         case SVR4_TIOCLSET:
323         case SVR4_TIOCLBIS:
324         case SVR4_TIOCLBIC:
325                 {
326                         int flags;
327
328                         if ((error = copyin(data, &flags, sizeof(flags))) != 0)
329                                 return error;
330
331                         switch (cmd) {
332                         case SVR4_TIOCLSET:
333                                 cmd = TIOCLSET;
334                                 break;
335                         case SVR4_TIOCLBIS:
336                                 cmd = TIOCLBIS;
337                                 break;
338                         case SVR4_TIOCLBIC:
339                                 cmd = TIOCLBIC;
340                                 break;
341                         }
342
343                         DPRINTF(("SVR4_TIOCL{SET,BIS,BIC} %o\n", flags));
344                         return fo_ioctl(fp, cmd, (caddr_t) &flags, td);
345                 }
346
347         default:
348                 DPRINTF(("Unknown svr4 ttold %lx\n", cmd));
349                 return 0;       /* ENOSYS really */
350         }
351 }