home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / bin / stty / print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-09  |  5.9 KB  |  259 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)print.c    5.4 (Berkeley) 6/10/91";
  36. #endif /* not lint */
  37.  
  38. #include <sys/types.h>
  39. #include <stddef.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include "stty.h"
  43. #include "extern.h"
  44.  
  45. static void  binit __P((char *));
  46. static void  bput __P((char *));
  47. static char *ccval __P((int));
  48.  
  49. void
  50. print(tp, wp, ldisc, fmt)
  51.     struct termios *tp;
  52.     struct winsize *wp;
  53.     int ldisc;
  54.     enum FMT fmt;
  55. {
  56.     register struct cchar *p;
  57.     register long tmp;
  58.     register int cnt;
  59.     register u_char *cc;
  60.     int ispeed, ospeed;
  61.     char buf1[100], buf2[100];
  62.  
  63.     cnt = 0;
  64.  
  65.     /* Line discipline. */
  66.     if (ldisc != TTYDISC) {
  67.         switch(ldisc) {
  68.         case TABLDISC:    
  69.             cnt += printf("tablet disc; ");
  70.             break;
  71.         case SLIPDISC:    
  72.             cnt += printf("slip disc; ");
  73.             break;
  74.         default:    
  75.             cnt += printf("#%d disc; ", ldisc);
  76.             break;
  77.         }
  78.     }
  79.  
  80.     /* Line speed. */
  81.     ispeed = cfgetispeed(tp);
  82.     ospeed = cfgetospeed(tp);
  83.     if (ispeed != ospeed)
  84.         cnt +=
  85.             printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
  86.     else
  87.         cnt += printf("speed %d baud;", ispeed);
  88.     if (fmt >= BSD)
  89.         cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
  90.     if (cnt)
  91.         (void)printf("\n");
  92.  
  93. #define    on(f)    ((tmp&f) != 0)
  94. #define put(n, f, d) \
  95.     if (fmt >= BSD || on(f) != d) \
  96.         bput(n + on(f));
  97.  
  98.     /* "local" flags */
  99.     tmp = tp->c_lflag;
  100.     binit("lflags");
  101.     put("-icanon", ICANON, 1);
  102.     put("-isig", ISIG, 1);
  103.     put("-iexten", IEXTEN, 1);
  104.     put("-echo", ECHO, 1);
  105.     put("-echoe", ECHOE, 0);
  106.     put("-echok", ECHOK, 0);
  107.     put("-echoke", ECHOKE, 0);
  108.     put("-echonl", ECHONL, 0);
  109.     put("-echoctl", ECHOCTL, 0);
  110.     put("-echoprt", ECHOPRT, 0);
  111.     put("-altwerase", ALTWERASE, 0);
  112.     put("-noflsh", NOFLSH, 0);
  113.     put("-tostop", TOSTOP, 0);
  114.     put("-mdmbuf", MDMBUF, 0);
  115.     put("-flusho", FLUSHO, 0);
  116.     put("-pendin", PENDIN, 0);
  117.     put("-nokerninfo", NOKERNINFO, 0);
  118.     put("-extproc", EXTPROC, 0);
  119.  
  120.     /* input flags */
  121.     tmp = tp->c_iflag;
  122.     binit("iflags");
  123.     put("-istrip", ISTRIP, 0);
  124.     put("-icrnl", ICRNL, 1);
  125.     put("-inlcr", INLCR, 0);
  126.     put("-igncr", IGNCR, 0);
  127.     put("-ixon", IXON, 1);
  128.     put("-ixoff", IXOFF, 0);
  129.     put("-ixany", IXANY, 1);
  130.     put("-imaxbel", IMAXBEL, 1);
  131.     put("-ignbrk", IGNBRK, 0);
  132.     put("-brkint", BRKINT, 1);
  133.     put("-inpck", INPCK, 0);
  134.     put("-ignpar", IGNPAR, 0);
  135.     put("-parmrk", PARMRK, 0);
  136.  
  137.     /* output flags */
  138.     tmp = tp->c_oflag;
  139.     binit("oflags");
  140.     put("-opost", OPOST, 1);
  141.     put("-onlcr", ONLCR, 1);
  142.     put("-oxtabs", OXTABS, 1);
  143.  
  144.     /* control flags (hardware state) */
  145.     tmp = tp->c_cflag;
  146.     binit("cflags");
  147.     put("-cread", CREAD, 1);
  148.     switch(tmp&CSIZE) {
  149.     case CS5:
  150.         bput("cs5");
  151.         break;
  152.     case CS6:
  153.         bput("cs6");
  154.         break;
  155.     case CS7:
  156.         bput("cs7");
  157.         break;
  158.     case CS8:
  159.         bput("cs8");
  160.         break;
  161.     }
  162.     bput("-parenb" + on(PARENB));
  163.     put("-parodd", PARODD, 0);
  164.     put("-hupcl", HUPCL, 1);
  165.     put("-clocal", CLOCAL, 0);
  166.     put("-cstopb", CSTOPB, 0);
  167.     put("-crtscts", CRTSCTS, 0);
  168.  
  169.     /* special control characters */
  170.     cc = tp->c_cc;
  171.     if (fmt == POSIX) {
  172.         binit("cchars");
  173.         for (p = cchars1; p->name; ++p) {
  174.             (void)snprintf(buf1, sizeof(buf1), "%s = %s;",
  175.                 p->name, ccval(cc[p->sub]));
  176.             bput(buf1);
  177.         }
  178.         binit(NULL);
  179.     } else {
  180.         binit(NULL);
  181.         for (p = cchars1, cnt = 0; p->name; ++p) {
  182.             if (fmt != BSD && cc[p->sub] == p->def)
  183.                 continue;
  184. #define    WD    "%-8s"
  185.             (void)sprintf(buf1 + cnt * 8, WD, p->name);
  186.             (void)sprintf(buf2 + cnt * 8, WD, ccval(cc[p->sub]));
  187.             if (++cnt == LINELENGTH / 8) {
  188.                 cnt = 0;
  189.                 (void)printf("%s\n", buf1);
  190.                 (void)printf("%s\n", buf2);
  191.             }
  192.         }
  193.         if (cnt) {
  194.             (void)printf("%s\n", buf1);
  195.             (void)printf("%s\n", buf2);
  196.         }
  197.     }
  198. }
  199.  
  200. static int col;
  201. static char *label;
  202.  
  203. static void
  204. binit(lb)
  205.     char *lb;
  206. {
  207.     if (col) {
  208.         (void)printf("\n");
  209.         col = 0;
  210.     }
  211.     label = lb;
  212. }
  213.  
  214. static void
  215. bput(s)
  216.     char *s;
  217. {
  218.     if (col == 0) {
  219.         col = printf("%s: %s", label, s);
  220.         return;
  221.     }
  222.     if ((col + strlen(s)) > LINELENGTH) {
  223.         (void)printf("\n\t");
  224.         col = printf("%s", s) + 8;
  225.         return;
  226.     }
  227.     col += printf(" %s", s);
  228. }
  229.  
  230. static char *
  231. ccval(c)
  232.     int c;
  233. {
  234.     static char buf[5];
  235.     char *bp;
  236.  
  237.     if (c == _POSIX_VDISABLE)
  238.         return("<undef>");
  239.  
  240.     bp = buf;
  241.     if (c & 0200) {
  242.         *bp++ = 'M';
  243.         *bp++ = '-';
  244.         c &= 0177;
  245.     }
  246.     if (c == 0177) {
  247.         *bp++ = '^';
  248.         *bp++ = '?';
  249.     }
  250.     else if (c < 040) {
  251.         *bp++ = '^';
  252.         *bp++ = c + '@';
  253.     }
  254.     else
  255.         *bp++ = c;
  256.     *bp = '\0';
  257.     return(buf);
  258. }
  259.