BSD 4_4 release
[unix-history] / usr / src / usr.bin / ul / ul.c
index 826993d..75e6f95 100644 (file)
@@ -1,6 +1,45 @@
+/*
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+static char copyright[] =
+"@(#) Copyright (c) 1980, 1993\n\
+       The Regents of the University of California.  All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)ul.c       4.5 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)ul.c       8.1 (Berkeley) 6/6/93";
+#endif /* not lint */
 
 #include <stdio.h>
 
 
 #include <stdio.h>
 
@@ -29,8 +68,6 @@ struct        CHAR    {
        char    c_char;
 } ;
 
        char    c_char;
 } ;
 
-char   buf[BUFSIZ];
-
 struct CHAR    obuf[MAXBUF];
 int    col, maxcol;
 int    mode;
 struct CHAR    obuf[MAXBUF];
 int    col, maxcol;
 int    mode;
@@ -38,17 +75,20 @@ int halfpos;
 int    upln;
 int    iflag;
 
 int    upln;
 int    iflag;
 
+int    outchar();
+#define        PRINT(s)        if (s == NULL) /* void */; else tputs(s, 1, outchar)
+
 main(argc, argv)
        int argc;
        char **argv;
 {
 main(argc, argv)
        int argc;
        char **argv;
 {
+       extern int optind;
+       extern char *optarg;
        int c;
        int c;
-       char *cp, *termtype;
+       char *termtype;
        FILE *f;
        char termcap[1024];
        FILE *f;
        char termcap[1024];
-       char *getenv();
-       extern int optind;
-       extern char *optarg;
+       char *getenv(), *strcpy();
 
        termtype = getenv("TERM");
        if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
 
        termtype = getenv("TERM");
        if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
@@ -66,7 +106,7 @@ main(argc, argv)
 
                default:
                        fprintf(stderr,
 
                default:
                        fprintf(stderr,
-                               "Usage: %s [ -i ] [ -tTerm ] file...\n",
+                               "usage: %s [ -i ] [ -tTerm ] file...\n",
                                argv[0]);
                        exit(1);
                }
                                argv[0]);
                        exit(1);
                }
@@ -82,14 +122,13 @@ main(argc, argv)
 
        case 0:
                /* No such terminal type - assume dumb */
 
        case 0:
                /* No such terminal type - assume dumb */
-               strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
+               (void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
                break;
        }
        initcap();
        if (    (tgetflag("os") && ENTER_BOLD==NULL ) ||
                (tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
                        must_overstrike = 1;
                break;
        }
        initcap();
        if (    (tgetflag("os") && ENTER_BOLD==NULL ) ||
                (tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
                        must_overstrike = 1;
-       setbuf(stdout, buf);
        initbuf();
        if (optind == argc)
                filter(stdin);
        initbuf();
        if (optind == argc)
                filter(stdin);
@@ -105,11 +144,11 @@ main(argc, argv)
 }
 
 filter(f)
 }
 
 filter(f)
-FILE *f;
+       FILE *f;
 {
        register c;
 
 {
        register c;
 
-       while((c = getc(f)) != EOF) switch(c) {
+       while ((c = getc(f)) != EOF) switch(c) {
 
        case '\b':
                if (col > 0)
 
        case '\b':
                if (col > 0)
@@ -190,6 +229,11 @@ FILE *f;
                flushln();
                continue;
 
                flushln();
                continue;
 
+       case '\f':
+               flushln();
+               putchar('\f');
+               continue;
+
        default:
                if (c < ' ')    /* non printing */
                        continue;
        default:
                if (c < ' ')    /* non printing */
                        continue;
@@ -201,10 +245,8 @@ FILE *f;
                        obuf[col].c_mode |= UNDERL|mode;
                } else if (obuf[col].c_char == c)
                        obuf[col].c_mode |= BOLD|mode;
                        obuf[col].c_mode |= UNDERL|mode;
                } else if (obuf[col].c_char == c)
                        obuf[col].c_mode |= BOLD|mode;
-               else {
-                       obuf[col].c_mode = c;
+               else
                        obuf[col].c_mode = mode;
                        obuf[col].c_mode = mode;
-               }
                col++;
                if (col > maxcol)
                        maxcol = col;
                col++;
                if (col > maxcol)
                        maxcol = col;
@@ -228,9 +270,9 @@ flushln()
                        lastmode = obuf[i].c_mode;
                }
                if (obuf[i].c_char == '\0') {
                        lastmode = obuf[i].c_mode;
                }
                if (obuf[i].c_char == '\0') {
-                       if (upln) {
-                               puts(CURS_RIGHT);
-                       else
+                       if (upln)
+                               PRINT(CURS_RIGHT);
+                       else
                                outc(' ');
                } else
                        outc(obuf[i].c_char);
                                outc(' ');
                } else
                        outc(obuf[i].c_char);
@@ -243,7 +285,7 @@ flushln()
        putchar('\n');
        if (iflag && hadmodes)
                iattr();
        putchar('\n');
        if (iflag && hadmodes)
                iattr();
-       fflush(stdout);
+       (void)fflush(stdout);
        if (upln)
                upln--;
        initbuf();
        if (upln)
                upln--;
        initbuf();
@@ -315,12 +357,8 @@ iattr()
 
 initbuf()
 {
 
 initbuf()
 {
-       register i;
 
 
-       for (i=0; i<MAXBUF; i++) {
-               obuf[i].c_char = '\0';
-               obuf[i].c_mode = NORMAL;
-       }
+       bzero((char *)obuf, sizeof (obuf));     /* depends on NORMAL == 0 */
        col = 0;
        maxcol = 0;
        mode &= ALTSET;
        col = 0;
        maxcol = 0;
        mode &= ALTSET;
@@ -341,15 +379,14 @@ reverse()
 {
        upln++;
        fwd();
 {
        upln++;
        fwd();
-       puts(CURS_UP);
-       puts(CURS_UP);
+       PRINT(CURS_UP);
+       PRINT(CURS_UP);
        upln++;
 }
 
 initcap()
 {
        static char tcapbuf[512];
        upln++;
 }
 
 initcap()
 {
        static char tcapbuf[512];
-       char *termtype;
        char *bp = tcapbuf;
        char *getenv(), *tgetstr();
 
        char *bp = tcapbuf;
        char *getenv(), *tgetstr();
 
@@ -396,44 +433,32 @@ initcap()
         * letters the 37 has.
         */
 
         * letters the 37 has.
         */
 
-#ifdef notdef
-printf("so %s se %s us %s ue %s me %s\n",
-       ENTER_STANDOUT, EXIT_STANDOUT, ENTER_UNDERLINE,
-       EXIT_UNDERLINE, EXIT_ATTRIBUTES);
-#endif
        UNDER_CHAR =            tgetstr("uc", &bp);
        must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
 }
 
 outchar(c)
        UNDER_CHAR =            tgetstr("uc", &bp);
        must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
 }
 
 outchar(c)
-char c;
+       int c;
 {
 {
-       putchar(c&0177);
+       putchar(c & 0177);
 }
 
 }
 
-puts(str)
-char *str;
-{
-       if (str)
-               tputs(str, 1, outchar);
-}
+static int curmode = 0;
 
 
-static curmode = 0;
 outc(c)
 outc(c)
-char c;
+       int c;
 {
        putchar(c);
 {
        putchar(c);
-       if (must_use_uc &&  (curmode&UNDERL)) {
-               puts(CURS_LEFT);
-               puts(UNDER_CHAR);
+       if (must_use_uc && (curmode&UNDERL)) {
+               PRINT(CURS_LEFT);
+               PRINT(UNDER_CHAR);
        }
 }
 
 setmode(newmode)
        }
 }
 
 setmode(newmode)
-int newmode;
+       int newmode;
 {
 {
-       if (!iflag)
-       {
+       if (!iflag) {
                if (curmode != NORMAL && newmode != NORMAL)
                        setmode(NORMAL);
                switch (newmode) {
                if (curmode != NORMAL && newmode != NORMAL)
                        setmode(NORMAL);
                switch (newmode) {
@@ -442,100 +467,42 @@ int newmode;
                        case NORMAL:
                                break;
                        case UNDERL:
                        case NORMAL:
                                break;
                        case UNDERL:
-                               puts(EXIT_UNDERLINE);
+                               PRINT(EXIT_UNDERLINE);
                                break;
                        default:
                                /* This includes standout */
                                break;
                        default:
                                /* This includes standout */
-                               puts(EXIT_ATTRIBUTES);
+                               PRINT(EXIT_ATTRIBUTES);
                                break;
                        }
                        break;
                case ALTSET:
                                break;
                        }
                        break;
                case ALTSET:
-                       puts(ENTER_REVERSE);
+                       PRINT(ENTER_REVERSE);
                        break;
                case SUPERSC:
                        /*
                         * This only works on a few terminals.
                         * It should be fixed.
                         */
                        break;
                case SUPERSC:
                        /*
                         * This only works on a few terminals.
                         * It should be fixed.
                         */
-                       puts(ENTER_UNDERLINE);
-                       puts(ENTER_DIM);
+                       PRINT(ENTER_UNDERLINE);
+                       PRINT(ENTER_DIM);
                        break;
                case SUBSC:
                        break;
                case SUBSC:
-                       puts(ENTER_DIM);
+                       PRINT(ENTER_DIM);
                        break;
                case UNDERL:
                        break;
                case UNDERL:
-                       puts(ENTER_UNDERLINE);
+                       PRINT(ENTER_UNDERLINE);
                        break;
                case BOLD:
                        break;
                case BOLD:
-                       puts(ENTER_BOLD);
+                       PRINT(ENTER_BOLD);
                        break;
                default:
                        /*
                         * We should have some provision here for multiple modes
                         * on at once.  This will have to come later.
                         */
                        break;
                default:
                        /*
                         * We should have some provision here for multiple modes
                         * on at once.  This will have to come later.
                         */
-                       puts(ENTER_STANDOUT);
+                       PRINT(ENTER_STANDOUT);
                        break;
                }
        }
        curmode = newmode;
 }
                        break;
                }
        }
        curmode = newmode;
 }
-/*     @(#)getopt.c    3.2     */
-#define        ERR(s, c)       if(opterr){\
-       fputs(argv[0], stderr);\
-       fputs(s, stderr);\
-       fputc(c, stderr);\
-       fputc('\n', stderr);}
-
-int    opterr = 1;
-int    optind = 1;
-char   *optarg;
-char   *index();
-
-int
-getopt (argc, argv, opts)
-       char **argv, *opts;
-{
-       static int sp = 1;
-       char c;
-       char *cp;
-
-       if (sp == 1)
-               if (optind >= argc ||
-                  argv[optind][0] != '-' || argv[optind][1] == '\0')
-                       return EOF;
-               else if (strcmp(argv[optind], "--") == NULL) {
-                       optind++;
-                       return EOF;
-               }
-               else if (strcmp(argv[optind], "-?") == NULL) {
-                       optind++;
-                       return '?';
-               }
-       c = argv[optind][sp];
-       if (c == ':' || (cp=index(opts, c)) == NULL) {
-               ERR (": illegal option -- ", c);
-               if (argv[optind][++sp] == '\0') {
-                       optind++;
-                       sp = 1;
-               }
-               return '?';
-       }
-       if (*++cp == ':') {
-               if (argv[optind][2] != '\0')
-                       optarg = &argv[optind++][sp+1];
-               else if (++optind >= argc) {
-                       ERR (": option requires an argument -- ", c);
-                       sp = 1;
-                       return '?';
-               } else
-                       optarg = argv[optind++];
-               sp = 1;
-       }
-       else if (argv[optind][++sp] == '\0') {
-               sp = 1;
-               optind++;
-       }
-       return c;
-}