121553Sdist /*
2*63057Sbostic * Copyright (c) 1980, 1993
3*63057Sbostic * The Regents of the University of California. All rights reserved.
436173Sbostic *
542728Sbostic * %sccs.include.redist.c%
621553Sdist */
721553Sdist
821553Sdist #ifndef lint
9*63057Sbostic static char copyright[] =
10*63057Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*63057Sbostic The Regents of the University of California. All rights reserved.\n";
1236173Sbostic #endif /* not lint */
1321553Sdist
1421553Sdist #ifndef lint
15*63057Sbostic static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 06/09/93";
1636173Sbostic #endif /* not lint */
1721553Sdist
181008Sbill #include <stdio.h>
191008Sbill /*
201008Sbill * expand - expand tabs to equivalent spaces
211008Sbill */
221008Sbill int nstops;
231008Sbill int tabstops[100];
241008Sbill
main(argc,argv)251008Sbill main(argc, argv)
261008Sbill int argc;
271008Sbill char *argv[];
281008Sbill {
291008Sbill register int c, column;
301008Sbill register int n;
311008Sbill
321008Sbill argc--, argv++;
331008Sbill do {
341008Sbill while (argc > 0 && argv[0][0] == '-') {
351008Sbill getstops(argv[0]);
361008Sbill argc--, argv++;
371008Sbill }
381008Sbill if (argc > 0) {
391008Sbill if (freopen(argv[0], "r", stdin) == NULL) {
401008Sbill perror(argv[0]);
411008Sbill exit(1);
421008Sbill }
431008Sbill argc--, argv++;
441008Sbill }
451008Sbill column = 0;
461008Sbill for (;;) {
471008Sbill c = getc(stdin);
481008Sbill if (c == -1)
491008Sbill break;
501008Sbill switch (c) {
511008Sbill
521008Sbill case '\t':
531008Sbill if (nstops == 0) {
541008Sbill do {
551008Sbill putchar(' ');
561008Sbill column++;
571008Sbill } while (column & 07);
581008Sbill continue;
591008Sbill }
601008Sbill if (nstops == 1) {
611008Sbill do {
621008Sbill putchar(' ');
631008Sbill column++;
641008Sbill } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1));
651008Sbill continue;
661008Sbill }
671008Sbill for (n = 0; n < nstops; n++)
681008Sbill if (tabstops[n] > column)
691008Sbill break;
701008Sbill if (n == nstops) {
711008Sbill putchar(' ');
721008Sbill column++;
731008Sbill continue;
741008Sbill }
751008Sbill while (column < tabstops[n]) {
761008Sbill putchar(' ');
771008Sbill column++;
781008Sbill }
791008Sbill continue;
801008Sbill
811008Sbill case '\b':
821008Sbill if (column)
831008Sbill column--;
841008Sbill putchar('\b');
851008Sbill continue;
861008Sbill
871008Sbill default:
881008Sbill putchar(c);
891008Sbill column++;
901008Sbill continue;
911008Sbill
921008Sbill case '\n':
931008Sbill putchar(c);
941008Sbill column = 0;
951008Sbill continue;
961008Sbill }
971008Sbill }
981008Sbill } while (argc > 0);
991008Sbill exit(0);
1001008Sbill }
1011008Sbill
getstops(cp)1021008Sbill getstops(cp)
1031008Sbill register char *cp;
1041008Sbill {
1051008Sbill register int i;
1061008Sbill
1071008Sbill nstops = 0;
1081008Sbill cp++;
1091008Sbill for (;;) {
1101008Sbill i = 0;
1111008Sbill while (*cp >= '0' && *cp <= '9')
1121008Sbill i = i * 10 + *cp++ - '0';
1131008Sbill if (i <= 0 || i > 256) {
1141008Sbill bad:
1151008Sbill fprintf(stderr, "Bad tab stop spec\n");
1161008Sbill exit(1);
1171008Sbill }
1182368Skre if (nstops > 0 && i <= tabstops[nstops-1])
1191008Sbill goto bad;
1201008Sbill tabstops[nstops++] = i;
1211008Sbill if (*cp == 0)
1221008Sbill break;
1231008Sbill if (*cp++ != ',')
1241008Sbill goto bad;
1251008Sbill }
1261008Sbill }
127