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

Annotation of src/usr.bin/lam/lam.c, Revision 1.6

1.6     ! krw         1: /*     $OpenBSD: lam.c,v 1.5 2003/04/05 16:24:23 deraadt Exp $ */
1.1       deraadt     2: /*     $NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $      */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1993
                      6:  *     The Regents of the University of California.  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 the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)lam.c      8.1 (Berkeley) 6/6/93";
                     46: #endif
1.6     ! krw        47: static char rcsid[] = "$OpenBSD: lam.c,v 1.5 2003/04/05 16:24:23 deraadt Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: /*
                     51:  *     lam - laminate files
                     52:  *     Author:  John Kunze, UCB
                     53:  */
                     54:
                     55: #include <stdio.h>
                     56: #include <stdlib.h>
                     57: #include <string.h>
                     58:
                     59: #define        MAXOFILES       20
                     60: #define        BIGBUFSIZ       5 * BUFSIZ
                     61:
                     62: struct openfile {              /* open file structure */
                     63:        FILE    *fp;            /* file pointer */
                     64:        short   eof;            /* eof flag */
                     65:        short   pad;            /* pad flag for missing columns */
                     66:        char    eol;            /* end of line character */
                     67:        char    *sepstring;     /* string to print before each line */
                     68:        char    *format;        /* printf(3) style string spec. */
                     69: }      input[MAXOFILES];
                     70:
                     71: int    morefiles;              /* set by getargs(), changed by gatherline() */
                     72: int    nofinalnl;              /* normally append \n to each output line */
                     73: char   line[BIGBUFSIZ];
                     74: char   *linep;
                     75:
1.4       millert    76: void    error(char *, char *);
                     77: char   *gatherline(struct openfile *);
                     78: void    getargs(char *[]);
                     79: char   *pad(struct openfile *);
1.1       deraadt    80:
                     81: int
                     82: main(argc, argv)
                     83:        int argc;
                     84:        char *argv[];
                     85: {
1.3       mpech      86:        struct  openfile *ip;
1.1       deraadt    87:
                     88:        getargs(argv);
                     89:        if (!morefiles)
                     90:                error("lam - laminate files", "");
                     91:        for (;;) {
                     92:                linep = line;
                     93:                for (ip = input; ip->fp != NULL; ip++)
                     94:                        linep = gatherline(ip);
                     95:                if (!morefiles)
                     96:                        exit(0);
                     97:                fputs(line, stdout);
                     98:                fputs(ip->sepstring, stdout);
                     99:                if (!nofinalnl)
                    100:                        putchar('\n');
                    101:        }
                    102: }
                    103:
                    104: void
                    105: getargs(av)
                    106:        char *av[];
                    107: {
1.3       mpech     108:        struct  openfile *ip = input;
                    109:        char *p;
                    110:        char *c;
1.1       deraadt   111:        static char fmtbuf[BUFSIZ];
                    112:        char *fmtp = fmtbuf;
                    113:        int P, S, F, T;
                    114:
                    115:        P = S = F = T = 0;              /* capitalized options */
                    116:        while ((p = *++av) != NULL) {
                    117:                if (*p != '-' || !p[1]) {
                    118:                        morefiles++;
                    119:                        if (*p == '-')
                    120:                                ip->fp = stdin;
                    121:                        else if ((ip->fp = fopen(p, "r")) == NULL) {
                    122:                                perror(p);
                    123:                                exit(1);
                    124:                        }
                    125:                        ip->pad = P;
                    126:                        if (!ip->sepstring)
                    127:                                ip->sepstring = (S ? (ip-1)->sepstring : "");
                    128:                        if (!ip->format)
                    129:                                ip->format = ((P || F) ? (ip-1)->format : "%s");
                    130:                        if (!ip->eol)
                    131:                                ip->eol = (T ? (ip-1)->eol : '\n');
                    132:                        ip++;
                    133:                        continue;
                    134:                }
                    135:                switch (*(c = ++p) | 040) {
                    136:                case 's':
                    137:                        if (*++p || (p = *++av))
                    138:                                ip->sepstring = p;
                    139:                        else
                    140:                                error("Need string after -%s", c);
                    141:                        S = (*c == 'S' ? 1 : 0);
                    142:                        break;
                    143:                case 't':
                    144:                        if (*++p || (p = *++av))
                    145:                                ip->eol = *p;
                    146:                        else
                    147:                                error("Need character after -%s", c);
                    148:                        T = (*c == 'T' ? 1 : 0);
                    149:                        nofinalnl = 1;
                    150:                        break;
                    151:                case 'p':
                    152:                        ip->pad = 1;
                    153:                        P = (*c == 'P' ? 1 : 0);
                    154:                case 'f':
                    155:                        F = (*c == 'F' ? 1 : 0);
                    156:                        if (*++p || (p = *++av)) {
                    157:                                fmtp += strlen(fmtp) + 1;
1.6     ! krw       158:                                if (fmtp >= fmtbuf + BUFSIZ)
1.1       deraadt   159:                                        error("No more format space", "");
1.5       deraadt   160:                                snprintf(fmtp, fmtbuf + BUFSIZ - fmtp,
                    161:                                    "%%%ss", p);
1.1       deraadt   162:                                ip->format = fmtp;
                    163:                        }
                    164:                        else
                    165:                                error("Need string after -%s", c);
                    166:                        break;
                    167:                default:
                    168:                        error("What do you mean by -%s?", c);
                    169:                        break;
                    170:                }
                    171:        }
                    172:        ip->fp = NULL;
                    173:        if (!ip->sepstring)
                    174:                ip->sepstring = "";
                    175: }
                    176:
                    177: char *
                    178: pad(ip)
                    179:        struct openfile *ip;
                    180: {
1.3       mpech     181:        char *p = ip->sepstring;
                    182:        char *lp = linep;
1.1       deraadt   183:
                    184:        while (*p)
                    185:                *lp++ = *p++;
                    186:        if (ip->pad) {
                    187:                sprintf(lp, ip->format, "");
                    188:                lp += strlen(lp);
                    189:        }
                    190:        return (lp);
                    191: }
                    192:
                    193: char *
                    194: gatherline(ip)
                    195:        struct openfile *ip;
                    196: {
                    197:        char s[BUFSIZ];
1.3       mpech     198:        int c;
                    199:        char *p;
                    200:        char *lp = linep;
1.1       deraadt   201:        char *end = s + BUFSIZ;
                    202:
                    203:        if (ip->eof)
                    204:                return (pad(ip));
                    205:        for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
                    206:                if ((*p = c) == ip->eol)
                    207:                        break;
                    208:        *p = '\0';
                    209:        if (c == EOF) {
                    210:                ip->eof = 1;
                    211:                if (ip->fp == stdin)
                    212:                        fclose(stdin);
                    213:                morefiles--;
                    214:                return (pad(ip));
                    215:        }
                    216:        p = ip->sepstring;
                    217:        while (*p)
                    218:                *lp++ = *p++;
                    219:        sprintf(lp, ip->format, s);
                    220:        lp += strlen(lp);
                    221:        return (lp);
                    222: }
                    223:
                    224: void
                    225: error(msg, s)
                    226:        char *msg, *s;
                    227: {
                    228:        fprintf(stderr, "lam: ");
                    229:        fprintf(stderr, msg, s);
                    230:        fprintf(stderr,
                    231: "\nUsage:  lam [ -[fp] min.max ] [ -s sepstring ] [ -t c ] file ...\n");
                    232:        if (strncmp("lam - ", msg, 6) == 0)
                    233:                fprintf(stderr, "Options:\n\t%s\t%s\t%s\t%s\t%s",
                    234:                    "-f min.max field widths for file fragments\n",
                    235:                    "-p min.max like -f, but pad missing fragments\n",
                    236:                    "-s sepstring       fragment separator\n",
                    237: "-t c          input line terminator is c, no \\n after output lines\n",
                    238:                    "Capitalized options affect more than one file.\n");
                    239:        exit(1);
                    240: }