1 /* $NetBSD: slc.c,v 1.15 2006/11/24 19:46:58 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)slc.c 8.1 (Berkeley) 6/4/93";
37 __RCSID("$NetBSD: slc.c,v 1.15 2006/11/24 19:46:58 christos Exp $");
47 static unsigned char *def_slcbuf
= (unsigned char *)0;
48 static int def_slclen
= 0;
49 static int slcchange
; /* change to slc is requested */
50 static unsigned char *slcptr
; /* pointer into slc buffer */
51 static unsigned char slcbuf
[NSLC
*6]; /* buffer for slc negotiation */
53 void default_slc(void);
54 void process_slc(u_int
, u_int
, cc_t
);
59 * Write out the current special characters to the client.
67 * Send out list of triplets of special characters
68 * to client. We only send info on the characters
69 * that are currently supported.
71 for (i
= 1; i
<= NSLC
; i
++) {
72 if ((slctab
[i
].defset
.flag
& SLC_LEVELBITS
) == SLC_NOSUPPORT
)
74 add_slc((unsigned char)i
, slctab
[i
].current
.flag
,
75 slctab
[i
].current
.val
);
78 } /* end of send_slc */
83 * Set pty special characters to all the defaults.
90 for (i
= 1; i
<= NSLC
; i
++) {
91 slctab
[i
].current
.val
= slctab
[i
].defset
.val
;
92 if (slctab
[i
].current
.val
== (cc_t
)(_POSIX_VDISABLE
))
93 slctab
[i
].current
.flag
= SLC_NOSUPPORT
;
95 slctab
[i
].current
.flag
= slctab
[i
].defset
.flag
;
97 *(slctab
[i
].sptr
) = slctab
[i
].defset
.val
;
102 } /* end of default_slc */
103 #endif /* LINEMODE */
108 * Initialize the slc mapping table.
111 get_slc_defaults(void)
117 for (i
= 1; i
<= NSLC
; i
++) {
118 slctab
[i
].defset
.flag
=
119 spcset(i
, &slctab
[i
].defset
.val
, &slctab
[i
].sptr
);
120 slctab
[i
].current
.flag
= SLC_NOSUPPORT
;
121 slctab
[i
].current
.val
= 0;
124 } /* end of get_slc_defaults */
130 * Add an slc triplet to the slc buffer.
133 add_slc(char func
, char flag
, cc_t val
)
136 if ((*slcptr
++ = (unsigned char)func
) == 0xff)
139 if ((*slcptr
++ = (unsigned char)flag
) == 0xff)
142 if ((*slcptr
++ = (unsigned char)val
) == 0xff)
145 } /* end of add_slc */
150 * Get ready to process incoming slc's and respond to them.
152 * The parameter getit is non-zero if it is necessary to grab a copy
153 * of the terminal control structures.
162 (void)snprintf((char *)slcbuf
, sizeof slcbuf
, "%c%c%c%c",
163 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
);
166 } /* end of start_slc */
171 * Finish up the slc negotiation. If something to send, then send it.
174 end_slc(unsigned char **bufp
)
179 * If a change has occurred, store the new terminal control
180 * structures back to the terminal driver.
187 * If the pty state has not yet been fully processed and there is a
188 * deferred slc request from the client, then do not send any
189 * sort of slc negotiation now. We will respond to the client's
192 if (def_slcbuf
&& (terminit() == 0)) {
196 if (slcptr
> (slcbuf
+ 4)) {
199 return(slcptr
- slcbuf
- 4);
201 (void) snprintf((char *)slcptr
,
202 sizeof(slcbuf
) - (slcptr
- slcbuf
), "%c%c",
205 len
= slcptr
- slcbuf
;
206 writenet(slcbuf
, len
);
207 netflush(); /* force it out immediately */
208 DIAG(TD_OPTIONS
, printsub('>', slcbuf
+2, len
-2););
213 } /* end of end_slc */
218 * Figure out what to do about the client's slc
221 process_slc(u_int func
, u_int flag
, cc_t val
)
223 int hislevel
, mylevel
, ack
;
226 * Ensure that we know something about this function
229 add_slc(func
, SLC_NOSUPPORT
, 0);
234 * Process the special case requests of 0 SLC_DEFAULT 0
235 * and 0 SLC_VARIABLE 0. Be a little forgiving here, don't
236 * worry about whether the value is actually 0 or not.
239 if ((flag
= flag
& SLC_LEVELBITS
) == SLC_DEFAULT
) {
242 } else if (flag
== SLC_VARIABLE
) {
249 * Appears to be a function that we know something about. So
250 * get on with it and see what we know.
253 hislevel
= flag
& SLC_LEVELBITS
;
254 mylevel
= slctab
[func
].current
.flag
& SLC_LEVELBITS
;
255 ack
= flag
& SLC_ACK
;
257 * ignore the command if:
258 * the function value and level are the same as what we already have;
259 * or the level is the same and the ack bit is set
261 if (hislevel
== mylevel
&& (val
== slctab
[func
].current
.val
|| ack
)) {
265 * If we get here, we got an ack, but the levels don't match.
266 * This shouldn't happen. If it does, it is probably because
267 * we have sent two requests to set a variable without getting
268 * a response between them, and this is the first response.
269 * So, ignore it, and wait for the next response.
273 change_slc(func
, flag
, val
);
276 } /* end of process_slc */
281 * Process a request to change one of our special characters.
282 * Compare client's request with what we are capable of supporting.
285 change_slc(int func
, int flag
, cc_t val
)
287 int hislevel
, mylevel
;
289 hislevel
= flag
& SLC_LEVELBITS
;
290 mylevel
= slctab
[func
].defset
.flag
& SLC_LEVELBITS
;
292 * If client is setting a function to NOSUPPORT
293 * or DEFAULT, then we can easily and directly
294 * accommodate the request.
296 if (hislevel
== SLC_NOSUPPORT
) {
297 slctab
[func
].current
.flag
= flag
;
298 slctab
[func
].current
.val
= (cc_t
)_POSIX_VDISABLE
;
300 add_slc(func
, flag
, val
);
303 if (hislevel
== SLC_DEFAULT
) {
305 * Special case here. If client tells us to use
306 * the default on a function we don't support, then
307 * return NOSUPPORT instead of what we may have as a
308 * default level of DEFAULT.
310 if (mylevel
== SLC_DEFAULT
) {
311 slctab
[func
].current
.flag
= SLC_NOSUPPORT
;
313 slctab
[func
].current
.flag
= slctab
[func
].defset
.flag
;
315 slctab
[func
].current
.val
= slctab
[func
].defset
.val
;
316 add_slc(func
, slctab
[func
].current
.flag
,
317 slctab
[func
].current
.val
);
322 * Client wants us to change to a new value or he
323 * is telling us that he can't change to our value.
324 * Some of the slc's we support and can change,
325 * some we do support but can't change,
326 * and others we don't support at all.
327 * If we can change it then we have a pointer to
328 * the place to put the new value, so change it,
329 * otherwise, continue the negotiation.
331 if (slctab
[func
].sptr
) {
333 * We can change this one.
335 slctab
[func
].current
.val
= val
;
336 *(slctab
[func
].sptr
) = val
;
337 slctab
[func
].current
.flag
= flag
;
340 add_slc(func
, flag
, val
);
343 * It is not possible for us to support this
344 * request as he asks.
346 * If our level is DEFAULT, then just ack whatever was
349 * If he can't change and we can't change,
350 * then degenerate to NOSUPPORT.
352 * Otherwise we send our level back to him, (CANTCHANGE
353 * or NOSUPPORT) and if CANTCHANGE, send
356 if (mylevel
== SLC_DEFAULT
) {
357 slctab
[func
].current
.flag
= flag
;
358 slctab
[func
].current
.val
= val
;
360 } else if (hislevel
== SLC_CANTCHANGE
&&
361 mylevel
== SLC_CANTCHANGE
) {
362 flag
&= ~SLC_LEVELBITS
;
363 flag
|= SLC_NOSUPPORT
;
364 slctab
[func
].current
.flag
= flag
;
366 flag
&= ~SLC_LEVELBITS
;
368 slctab
[func
].current
.flag
= flag
;
369 if (mylevel
== SLC_CANTCHANGE
) {
370 slctab
[func
].current
.val
=
371 slctab
[func
].defset
.val
;
372 val
= slctab
[func
].current
.val
;
375 add_slc(func
, flag
, val
);
378 } /* end of change_slc */
381 cc_t oldeofc
= '\004';
387 * Check the special characters in use and notify the client if any have
388 * changed. Only those characters that are capable of being changed are
389 * likely to have changed. If a local change occurs, kick the support level
390 * and flags up to the defaults.
397 for (i
= 1; i
<= NSLC
; i
++) {
400 * In a perfect world this would be a neat little
401 * function. But in this world, we should not notify
402 * client of changes to the VEOF char when
403 * ICANON is off, because it is not representing
404 * a special character.
407 if (!tty_isediting())
409 else if (slctab
[i
].sptr
)
410 oldeofc
= *(slctab
[i
].sptr
);
412 #endif /* VEOF == VMIN */
413 if (slctab
[i
].sptr
&&
414 (*(slctab
[i
].sptr
) != slctab
[i
].current
.val
)) {
415 slctab
[i
].current
.val
= *(slctab
[i
].sptr
);
416 if (*(slctab
[i
].sptr
) == (cc_t
)_POSIX_VDISABLE
)
417 slctab
[i
].current
.flag
= SLC_NOSUPPORT
;
419 slctab
[i
].current
.flag
= slctab
[i
].defset
.flag
;
420 add_slc((unsigned char)i
, slctab
[i
].current
.flag
,
421 slctab
[i
].current
.val
);
429 * Process an slc option buffer. Defer processing of incoming slc's
430 * until after the terminal state has been processed. Save the first slc
431 * request that comes along, but discard all others.
433 * ptr points to the beginning of the buffer, len is the length.
436 do_opt_slc(unsigned char *ptr
, int len
)
438 unsigned char func
, flag
;
440 unsigned char *end
= ptr
+ len
;
442 if (terminit()) { /* go ahead */
445 if (ptr
>= end
) break;
447 if (ptr
>= end
) break;
450 process_slc((u_int
)func
, (u_int
)flag
, val
);
455 * save this slc buffer if it is the first, otherwise dump
458 if (def_slcbuf
== (unsigned char *)0) {
460 def_slcbuf
= (unsigned char *)malloc((unsigned)len
);
461 if (def_slcbuf
== (unsigned char *)0)
462 return; /* too bad */
463 memcpy(def_slcbuf
, ptr
, len
);
467 } /* end of do_opt_slc */
472 * Do slc stuff that was deferred.
479 do_opt_slc(def_slcbuf
, def_slclen
);
482 def_slcbuf
= (unsigned char *)0;
486 } /* end of deferslc */
488 #endif /* LINEMODE */