2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * File: spc-termio.c $TOG: spc-termio.c /main/5 1998/04/03 17:08:32 mgreess $
27 * (c) Copyright 1990, Hewlett-Packard Company, all rights reserved.
29 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
30 * (c) Copyright 1993, 1994 International Business Machines Corp. *
31 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
32 * (c) Copyright 1993, 1994 Novell, Inc. *
35 #include <bms/sbport.h>
36 #include <bms/stringbuf.h>
40 #include <sys/termio.h>
42 #include <codelibs/stringx.h>
47 #define STRTOKX(b,f) strtokx((b), (f))
49 #define STRTOKX(b,f) strtokx((char **)(&b), (f))
52 /***************************************************************************************
54 The routines in this file decode and encode a termios struct back and forth between the
55 actual binary representation of the structure and an ASCII string representation which
56 may be portably sent across the network.
58 <version>!<i-flags>!<o-flags>!<c-flags>!<l-flags>!<i-speed>!<o-speed>!<cc-array>
62 <version> == the termios struct protocal version (currently XPG3)
64 <i-flags> == comma separated list of input mode flags as defined in
65 the termios.h header file.
67 <o-flags> == comma separated list of output mode flags as defined in
68 the termios.h header file.
70 <c-flags> == comma separated list of control mode flags as defined in
71 the termios.h header file. The terminal speed info is
72 not part of this list.
74 <i-speed> == The input speed as defined in the termios.h header file.
76 <o-speed> == The output speed as defined in the termios.h header file.
78 <cc-array> == comma separated list of control control character names and
79 values formatted as (<name>=<value),<name>=<value>...)
81 ******************************************************************************************/
83 #define SPC_TERMIO_VER_FIELD 1 /* Define field indicies for above string */
84 #define SPC_TERMIO_IMODE_FIELD 2
85 #define SPC_TERMIO_OMODE_FIELD 3
86 #define SPC_TERMIO_CMODE_FIELD 4
87 #define SPC_TERMIO_LMODE_FIELD 5
88 #define SPC_TERMIO_ISPEED_FIELD 6
89 #define SPC_TERMIO_OSPEED_FIELD 7
90 #define SPC_TERMIO_CC_FIELD 8
91 #define SPC_TERMIO_LAST_FIELD SPC_TERMIO_CC_FIELD
93 #define SPC_TERMIO_SEP "!" /* These three MUST be only 1 char */
94 #define SPC_TCFLAG_SEP ","
95 #define SPC_CC_SEP "="
97 #define SPC_CC_FORMAT "%u" /* CC values are printed with this format */
117 /* The following lists are the valid componants of a "struct termios" */
118 /* as defined in XPG3. These MUST be kept up to date with the X/Open */
119 /* standard. No platform specific items can exist here as we pass this */
120 /* data to other systems on the network and they be from any vendor. */
121 /* -------------------------------------------------------------------- */
123 #define SPC_TERMIO_VERSION "XPG3"
125 #define END_OF_LIST "End_Of_List"
127 static struct modes_s Input_modes[] = {
142 static struct modes_s Output_modes[] = {
176 static struct baud_s Baud_rates[] = {
197 static struct modes_s Control_modes[] = {
213 static struct modes_s Local_modes[] = {
227 static struct cc_s CC_Array[] = {
243 /*----------------------------------------------------------------------+*/
245 SPC_Decode_TCflag(tcflag_t flag,
246 struct modes_s modes[],
248 /*----------------------------------------------------------------------+*/
250 Boolean first_time = TRUE;
252 /* Given a binary representation of flag (tcflag_t), convert */
253 /* it into a comma separated list of ASCII flag names. */
255 while (strcmp(modes->name, END_OF_LIST) != 0)
257 if ( (modes->value & flag) == modes->value)
262 XeAppendToStringBuffer(buff, SPC_TCFLAG_SEP);
264 XeAppendToStringBuffer(buff, modes->name);
269 /* We need at least one blank for strtokx to work when */
270 /* we take this string apart on the other side. */
271 /* --------------------------------------------------- */
273 XeAppendToStringBuffer(buff, (XeString)" ");
276 /*----------------------------------------------------------------------+*/
278 SPC_Encode_TCflag(XeString buff,
279 struct modes_s modes[])
280 /*----------------------------------------------------------------------+*/
282 /* Given a comma separated list of flag names, convert the list */
283 /* into a "tcflag_t" (binary) representation of that list. */
286 tcflag_t flag = (tcflag_t)0;
287 struct modes_s *orig_modes = modes;
292 while (item = (XeString)STRTOKX(buff, SPC_TCFLAG_SEP))
294 Boolean found = FALSE;
297 while (strcmp(modes->name, END_OF_LIST) != 0)
299 if (strcmp(modes->name, item) == 0)
301 flag |= modes->value;
309 SPC_Error(SPC_Bad_Termios_Mode, item);
315 /*----------------------------------------------------------------------+*/
317 SPC_Decode_Speed(speed_t speed,
318 struct baud_s bauds[],
320 /*----------------------------------------------------------------------+*/
322 /* Given a binary "speed_t" speed specification, convert it into */
323 /* a single ASCII string. */
325 while (strcmp(bauds->name, END_OF_LIST) != 0)
327 if (bauds->speed == speed)
329 XeAppendToStringBuffer(buff, bauds->name);
335 /* We need at least one blank for strtokx to work when */
336 /* we take this string apart on the other side. */
337 /* --------------------------------------------------- */
338 XeAppendToStringBuffer(buff, (XeString)" ");
341 /*----------------------------------------------------------------------+*/
343 SPC_Encode_Speed(XeString buff,
344 struct baud_s bauds[])
345 /*----------------------------------------------------------------------+*/
347 #define DEFAULT_SPEED B9600
349 /* Given a single ASCII name of a termio speed item, convert it into */
350 /* a binary (speed_t) representation. */
353 return DEFAULT_SPEED;
355 while (strcmp(bauds->name, END_OF_LIST) != 0)
357 if (strcmp(bauds->name, buff) == 0)
362 SPC_Error(SPC_Bad_Termios_Speed, buff);
363 return DEFAULT_SPEED;
366 /*----------------------------------------------------------------------+*/
368 SPC_Decode_CC(cc_t ccs[],
369 struct cc_s cc_array[],
371 /*----------------------------------------------------------------------+*/
373 Boolean first_time = TRUE;
375 /* Given an array of cc_s from a termios struct (binary */
376 /* representation), convert it into a comma sepearted list of */
377 /* CC names and values of the form <name>=<value> */
379 while (strcmp(cc_array->name, END_OF_LIST) != 0)
381 int index = ccs[cc_array->value];
389 XeAppendToStringBuffer(buff, SPC_TCFLAG_SEP);
391 XeAppendToStringBuffer(buff, cc_array->name);
392 XeAppendToStringBuffer(buff, SPC_CC_SEP);
393 sprintf(num, SPC_CC_FORMAT, index);
394 XeAppendToStringBuffer(buff, num);
399 /* We need at least one blank for strtokx to work when */
400 /* we take this string apart on the other side. */
401 /* --------------------------------------------------- */
403 XeAppendToStringBuffer(buff, (XeString)" ");
406 /*----------------------------------------------------------------------+*/
408 SPC_Encode_CC(XeString buff,
410 struct cc_s cc_array[])
411 /*----------------------------------------------------------------------+*/
413 /* we should have gotten a string that looks like this: */
415 /* <V-Name>=<value>,<V-Name>=<value>... */
417 /* For example: VEOF=4,VKILL=8 */
419 /* Parse this array and store the values in the "ccs" array passed in */
420 /* to us. It is assumed that the <V-name> strings are all defined in */
421 /* the "cc_array" list passed to us. */
422 /* ------------------------------------------------------------------ */
424 XeString cc_name, cc_value;
427 struct cc_s *orig_cc_array = cc_array;
429 for(i=0; i<NCCS; i++)
434 while (cc_name = (XeString)STRTOKX(buff, SPC_TCFLAG_SEP))
436 Boolean found = FALSE;
438 cc_value = strchr(cc_name, SPC_CC_SEP[0]); /* Go find the "=" */
441 SPC_Error(SPC_Bad_Termios_CC, cc_name);
444 *cc_value++ = XeChar_NULL; /* Replace "=" with null */
447 /* Look for "V-Name" in table ... */
448 /* ------------------------------ */
449 cc_array = orig_cc_array;
450 while (strcmp(cc_array->name, END_OF_LIST) != 0)
452 if (strcmp(cc_array->name, cc_name) == 0)
454 if (sscanf(cc_value, SPC_CC_FORMAT, &a_cc) != 1)
456 *(cc_value-1) = SPC_CC_SEP[0];
457 SPC_Error(SPC_Bad_Termios_CC, cc_name);
460 ccs[cc_array->value] = a_cc;
470 *(cc_value-1) = SPC_CC_SEP[0];
471 SPC_Error(SPC_Bad_Termios_CC, cc_name);
476 /*----------------------------------------------------------------------+*/
477 XeString SPC_Decode_Termios(struct termios *tio)
478 /*----------------------------------------------------------------------+*/
480 /* Given a termios struct, return an ascii string representation of */
481 /* it as defined at the front of this file. */
483 #define RESULT_BUFF_SIZE 32
487 XeStringBuffer result = XeMakeStringBuffer(RESULT_BUFF_SIZE);
489 XeAppendToStringBuffer(result, SPC_TERMIO_VERSION);
490 XeAppendToStringBuffer(result, SPC_TERMIO_SEP);
492 SPC_Decode_TCflag(tio->c_iflag, Input_modes, result);
493 XeAppendToStringBuffer(result, SPC_TERMIO_SEP);
495 SPC_Decode_TCflag(tio->c_oflag, Output_modes, result);
496 XeAppendToStringBuffer(result, SPC_TERMIO_SEP);
498 SPC_Decode_TCflag(tio->c_cflag, Control_modes, result);
499 XeAppendToStringBuffer(result, SPC_TERMIO_SEP);
501 SPC_Decode_TCflag(tio->c_lflag, Local_modes, result);
502 XeAppendToStringBuffer(result, SPC_TERMIO_SEP);
504 speed = cfgetispeed(tio);
505 SPC_Decode_Speed(speed, Baud_rates, result);
506 XeAppendToStringBuffer(result, SPC_TERMIO_SEP);
508 speed = cfgetospeed(tio);
509 SPC_Decode_Speed(speed, Baud_rates, result);
510 XeAppendToStringBuffer(result, SPC_TERMIO_SEP);
512 SPC_Decode_CC(tio->c_cc, CC_Array, result);
514 all = strdup(result->buffer);
516 XeFree(result->buffer);
522 /*----------------------------------------------------------------------+*/
523 void SPC_Encode_Termios(XeString buff, struct termios *tio)
524 /*----------------------------------------------------------------------+*/
526 /* Decodes the ascii representation of the termios struct. The format */
527 /* of the string is defined at the front of this file. */
530 XeString protocol_version;
534 while (item = (XeString)STRTOKX(buff, SPC_TERMIO_SEP))
536 /* We can possibly have an all blank field. Walk past them */
537 /* because the routines we will be calling don't expect any */
538 /* blanks in the string (but they do check for empty stings).*/
539 while (*item == (XeChar)' ') item++;
543 case SPC_TERMIO_VER_FIELD :
544 protocol_version = item;
545 /* Check this some day ??? */
548 case SPC_TERMIO_IMODE_FIELD :
549 tio->c_iflag = SPC_Encode_TCflag(item, Input_modes);
552 case SPC_TERMIO_OMODE_FIELD :
553 tio->c_oflag = SPC_Encode_TCflag(item, Output_modes);
556 case SPC_TERMIO_CMODE_FIELD :
557 tio->c_cflag = SPC_Encode_TCflag(item, Control_modes);
560 case SPC_TERMIO_LMODE_FIELD :
561 tio->c_lflag = SPC_Encode_TCflag(item, Local_modes);
564 case SPC_TERMIO_ISPEED_FIELD :
565 speed = SPC_Encode_Speed(item, Baud_rates);
566 cfsetispeed(tio, speed);
569 case SPC_TERMIO_OSPEED_FIELD :
570 speed = SPC_Encode_Speed(item, Baud_rates);
571 cfsetospeed(tio, speed);
574 case SPC_TERMIO_CC_FIELD :
575 SPC_Encode_CC(item, tio->c_cc, CC_Array);
585 if (item_cnt != SPC_TERMIO_LAST_FIELD)
586 SPC_Error(SPC_Bad_Termios_Proto,
587 (XeString) ((item_cnt < SPC_TERMIO_LAST_FIELD) ? "Too Few" : "Too Many"));
592 /*----------------------------------------------------------------------+*/
594 /*----------------------------------------------------------------------+*/
598 struct termios tio, tio2;
601 tio.c_iflag = BRKINT | IGNPAR | ICRNL | IXON;
602 tio.c_oflag = OPOST | ONLCR;
603 tio.c_cflag = CS7 | CREAD | CLOCAL;
604 tio.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK;
607 for(i=0; i<NCCS; i++)
609 tio.c_cc[VERASE] = 101;
610 tio.c_cc[VEOF] = 102;
611 tio.c_cc[VSTOP] = 103;
620 for(i=0; i<NCCS; i++)
623 cfsetispeed(&tio, B9600);
624 cfsetospeed(&tio, B1200);
626 s1 = strdup(s = SPC_Decode_Termios(&tio));
627 printf("Decoded string=\n<%s>\n\n", s);
630 SPC_Encode_Termios(s, &tio2);
631 s2 = strdup(s = SPC_Decode_Termios(&tio2));
632 printf("String after Encoding/decoding=\n<%s>\n\n", s2);
634 if (strcmp(s1, s2) == 0)
635 printf("...Identical ...\n");
637 printf("...Mismatch ...\n");