[BACK]Return to asa.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / asa

Annotation of src/usr.bin/asa/asa.c, Revision 1.8

1.8     ! moritz      1: /*     $OpenBSD: asa.c,v 1.7 2007/05/17 10:55:16 moritz Exp $  */
1.1       deraadt     2: /*     $NetBSD: asa.c,v 1.10 1995/04/21 03:01:41 cgd Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1993,94 Winning Strategies, Inc.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *      This product includes software developed by Winning Strategies, Inc.
                     19:  * 4. The name of the author may not be used to endorse or promote products
                     20:  *    derived from this software without specific prior written permission
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     23:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     24:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     25:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     26:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     27:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     28:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     29:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     30:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     31:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     32:  */
                     33:
                     34: #ifndef lint
1.8     ! moritz     35: static char rcsid[] = "$OpenBSD: asa.c,v 1.7 2007/05/17 10:55:16 moritz Exp $";
1.1       deraadt    36: #endif
                     37:
1.8     ! moritz     38: #include <err.h>
1.1       deraadt    39: #include <stdio.h>
                     40: #include <stdlib.h>
1.7       moritz     41: #include <string.h>
1.6       robert     42: #include <unistd.h>
1.1       deraadt    43:
1.8     ! moritz     44: void asa(FILE *);
        !            45: __dead void usage(void);
1.1       deraadt    46:
                     47: int
1.4       deraadt    48: main(int argc, char *argv[])
1.1       deraadt    49: {
1.6       robert     50:        int ch;
1.1       deraadt    51:        FILE *fp;
                     52:
1.6       robert     53:        while ((ch = getopt(argc, argv, "")) != -1) {
                     54:                switch(ch) {
                     55:                default:
                     56:                        usage();
                     57:                        /* NOTREACHED */
                     58:                }
                     59:        }
                     60:        argc -= optind;
                     61:        argv += optind;
1.1       deraadt    62:
1.8     ! moritz     63:        if (!argc)
        !            64:                asa(stdin);
        !            65:        else
        !            66:                for (; *argv != NULL; argv++) {
        !            67:                        if ((fp = fopen(*argv, "r")) == NULL) {
        !            68:                                warn("%s", *argv);
1.1       deraadt    69:                                continue;
1.3       deraadt    70:                        }
1.8     ! moritz     71:                        asa(fp);
        !            72:                        fclose(fp);
1.3       deraadt    73:                }
1.1       deraadt    74:
1.8     ! moritz     75:        exit(0);
1.1       deraadt    76: }
                     77:
1.8     ! moritz     78: void
1.4       deraadt    79: asa(FILE *f)
1.1       deraadt    80: {
1.7       moritz     81:        char *buf, *lbuf = NULL;
1.1       deraadt    82:        size_t len;
1.8     ! moritz     83:        int firstline = 1;
1.1       deraadt    84:
1.8     ! moritz     85:        while ((buf = fgetln(f, &len)) != NULL) {
1.7       moritz     86:                if (buf[len - 1] == '\n')
                     87:                        buf[len - 1] = '\0';
                     88:                else {
                     89:                        if ((lbuf = malloc(len + 1)) == NULL)
                     90:                                err(1, NULL);
                     91:                        memcpy(lbuf, buf, len);
                     92:                        lbuf[len] = '\0';
                     93:                        buf = lbuf;
                     94:                }
1.1       deraadt    95:                /* special case the first line  */
1.8     ! moritz     96:                if (firstline) {
        !            97:                        firstline = 0;
        !            98:                        switch (buf[0]) {
        !            99:                        case '0':
        !           100:                                putchar ('\n');
        !           101:                                break;
        !           102:                        case '1':
        !           103:                                putchar ('\f');
        !           104:                                break;
1.7       moritz    105:                        }
1.8     ! moritz    106:                } else {
1.1       deraadt   107:                        switch (buf[0]) {
                    108:                        default:
                    109:                        case ' ':
                    110:                                putchar ('\n');
                    111:                                break;
                    112:                        case '0':
                    113:                                putchar ('\n');
                    114:                                putchar ('\n');
                    115:                                break;
                    116:                        case '1':
                    117:                                putchar ('\f');
                    118:                                break;
                    119:                        case '+':
                    120:                                putchar ('\r');
                    121:                                break;
                    122:                        }
                    123:                }
1.8     ! moritz    124:                if (buf[0] && buf[1]) {
        !           125:                        fputs (&buf[1], stdout);
        !           126:                }
        !           127:        }
        !           128:        free(lbuf);
1.1       deraadt   129:
1.8     ! moritz    130:        if (!firstline)
1.1       deraadt   131:                putchar ('\n');
1.6       robert    132: }
                    133:
1.8     ! moritz    134: __dead void
1.6       robert    135: usage(void)
                    136: {
                    137:        extern char *__progname;
                    138:        fprintf(stderr, "usage: %s [file ...]\n", __progname);
                    139:        exit(1);
1.1       deraadt   140: }