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

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

1.6     ! deraadt     1: /*     $OpenBSD: expand.c,v 1.5 2003/06/03 02:56:07 millert Exp $      */
1.1       deraadt     2: /*     $NetBSD: expand.c,v 1.5 1995/09/02 06:19:46 jtc Exp $   */
                      3:
                      4: /*
                      5:  * Copyright (c) 1980, 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.
1.5       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: static char copyright[] =
                     35: "@(#) Copyright (c) 1980, 1993\n\
                     36:        The Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)expand.c   8.1 (Berkeley) 6/9/93";
                     42: #endif
1.6     ! deraadt    43: static char rcsid[] = "$OpenBSD: expand.c,v 1.5 2003/06/03 02:56:07 millert Exp $";
1.1       deraadt    44: #endif /* not lint */
                     45:
                     46: #include <stdio.h>
                     47: #include <stdlib.h>
                     48: #include <ctype.h>
                     49: #include <unistd.h>
                     50:
                     51: /*
                     52:  * expand - expand tabs to equivalent spaces
                     53:  */
                     54: int    nstops;
                     55: int    tabstops[100];
                     56:
                     57: static void getstops();
                     58: static void usage();
                     59:
                     60: int
1.6     ! deraadt    61: main(int argc, char *argv[])
1.1       deraadt    62: {
1.4       mpech      63:        int c, column;
                     64:        int n;
1.1       deraadt    65:
                     66:        /* handle obsolete syntax */
1.3       deraadt    67:        while (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) {
1.1       deraadt    68:                getstops(&argv[1][1]);
                     69:                argc--; argv++;
                     70:        }
                     71:
                     72:        while ((c = getopt (argc, argv, "t:")) != -1) {
                     73:                switch (c) {
                     74:                case 't':
                     75:                        getstops(optarg);
                     76:                        break;
                     77:                case '?':
                     78:                default:
                     79:                        usage();
                     80:                        /* NOTREACHED */
                     81:                }
                     82:        }
                     83:        argc -= optind;
                     84:        argv += optind;
                     85:
                     86:        do {
                     87:                if (argc > 0) {
                     88:                        if (freopen(argv[0], "r", stdin) == NULL) {
                     89:                                perror(argv[0]);
                     90:                                exit(1);
                     91:                        }
                     92:                        argc--, argv++;
                     93:                }
                     94:                column = 0;
                     95:                while ((c = getchar()) != EOF) {
                     96:                        switch (c) {
                     97:                        case '\t':
                     98:                                if (nstops == 0) {
                     99:                                        do {
                    100:                                                putchar(' ');
                    101:                                                column++;
                    102:                                        } while (column & 07);
                    103:                                        continue;
                    104:                                }
                    105:                                if (nstops == 1) {
                    106:                                        do {
                    107:                                                putchar(' ');
                    108:                                                column++;
1.6     ! deraadt   109:                                        } while (((column - 1) %
        !           110:                                            tabstops[0]) != (tabstops[0] - 1));
1.1       deraadt   111:                                        continue;
                    112:                                }
                    113:                                for (n = 0; n < nstops; n++)
                    114:                                        if (tabstops[n] > column)
                    115:                                                break;
                    116:                                if (n == nstops) {
                    117:                                        putchar(' ');
                    118:                                        column++;
                    119:                                        continue;
                    120:                                }
                    121:                                while (column < tabstops[n]) {
                    122:                                        putchar(' ');
                    123:                                        column++;
                    124:                                }
                    125:                                continue;
                    126:
                    127:                        case '\b':
                    128:                                if (column)
                    129:                                        column--;
                    130:                                putchar('\b');
                    131:                                continue;
                    132:
                    133:                        default:
                    134:                                putchar(c);
                    135:                                column++;
                    136:                                continue;
                    137:
                    138:                        case '\n':
                    139:                                putchar(c);
                    140:                                column = 0;
                    141:                                continue;
                    142:                        }
                    143:                }
                    144:        } while (argc > 0);
                    145:        exit(0);
                    146: }
                    147:
                    148: static void
1.6     ! deraadt   149: getstops(char *cp)
1.1       deraadt   150: {
1.4       mpech     151:        int i;
1.1       deraadt   152:
                    153:        nstops = 0;
                    154:        for (;;) {
                    155:                i = 0;
                    156:                while (*cp >= '0' && *cp <= '9')
                    157:                        i = i * 10 + *cp++ - '0';
                    158:                if (i <= 0 || i > 256) {
                    159: bad:
                    160:                        fprintf(stderr, "Bad tab stop spec\n");
                    161:                        exit(1);
                    162:                }
                    163:                if (nstops > 0 && i <= tabstops[nstops-1])
                    164:                        goto bad;
                    165:                tabstops[nstops++] = i;
                    166:                if (*cp == 0)
                    167:                        break;
                    168:                if (*cp != ',' && *cp != ' ')
                    169:                        goto bad;
                    170:                cp++;
                    171:        }
                    172: }
                    173:
                    174: static void
1.6     ! deraadt   175: usage(void)
1.1       deraadt   176: {
                    177:        (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n");
                    178:        exit(1);
                    179: }