chad@anasaz.UUCP (09/25/88)
Posting-number: Volume 4, Issue 89 Submitted-by: "A. Nonymous" <chad@anasaz.UUCP> Archive-name: indent This is a filter that allows you to expand/contract the leading white space in a file. It is useful to change the indentation for nesting levels in C source, or handling source created with an editor who's tabstops are not where your editor thinks they are. For example, the C Beautifier cb(1) adjusts its indent level by one full hardware tab for each "{" or "}". I like my indent level to be 4 spaces, so I do: "cb -s foo.c | indent | hold foo.c" If you wanted an indent of 2 you could do: "cb -s foo.c | indent -r4 | hold foo.c" If you wanted an indent of 6 you could do: "cb -s foo.c | indent -r4 -e3 | hold foo.c" There are obviously many other permutations. You also have control over whether the output file will use spaces only or a combination of spaces and tabs in the leading white space: "indent -r1 foo.c | hold foo.c" will convert leading white space to spaces & tabs with no other change to the file. --------------- "I read the news today, oh boy!" --John Lennon =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | DCF, Inc. | UUCP: ...ncar!noao!nud!anasaz!dcfinc!chad | | 14623 North 49th Place | Ma Bell: (602) 953-1392 | | Scottsdale, AZ 85254 | Loran: N-33deg37min20sec W-111deg58min26sec | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | Disclaimer: These ARE the opinions of my employer! | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of shell archive." # Contents: indent.c Makefile test_file # Wrapped by chad@dcfinc on Tue Sep 20 19:58:43 1988 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f 'indent.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'indent.c'\" else echo shar: Extracting \"'indent.c'\" \(4160 characters\) sed "s/^X//" >'indent.c' <<'END_OF_FILE' X/* indent.c - Adjust indentation of file - 1.2 */ X X/* X** This filter will adjust the indentation of an existing file. It X** is most useful for C source created with an editor program that X** doesn't agree with your notion of tab stop locations. I use it to X** make the output of cb(1) have a nesting level of 4, rather than 8. X** In the spirit of Unix tools, I'm sure you will find other uses. X*/ X X/* X** Author: X** Chad R. Larson This program is placed in the X** DCF, Inc. Public Domain. You may do with X** 14623 North 49th Place it as you please. X** Scottsdale, AZ 85254 X*/ X X/* X** Usage is "indent [-tn] [-rn] [-en] [-s] [file]" X** Where: X** -tn Set hardware tab stop (for both input and output) X** to n. If this option is omitted, n=8. X** -rn Set reduction factor to n. That is, if n=3 each X** output line will be indented 1/3 as much as its X** corresponding input line. Default value for n is 2. X** -en Set expansion factor to n. That is, if n=3 each X** output line will be indented 3 times as much as its X** corresponding input line. Default value for n is 1. X** The -e and -r options may be used in conjunction, X** for example to get a two thirds indentation reduction. X** -s Supress tabs in output. Normally, multiple leading X** spaces will be replaced with the necessary amount of X** tab characters to reduce the size of the output file. X** Note this option only applies to leading white space. X** No other tabs will be affected. X** If no output file is specified, the standard input will be read. X** Output is to the standard output. Error messages will be printed X** on standard error (pretty standard, hmmm?). X*/ X X#include <stdio.h> X X#define FALSE 0 X#define TRUE ~FALSE X#define MAX_LINE 512 X X/* linked in later */ Xvoid exit(); X X/* give the operator a clue */ Xvoid usage(prog_name) Xchar *prog_name; X{ X (void) fprintf(stderr, "%s version 1.2 - 9/20/88\n", prog_name); X (void) fprintf(stderr, X "Usage: %s [-tn] [-rn] [-en] [-s] [file]\n", prog_name); X exit(1); X} X X/* here it is */ Xvoid main(argc, argv) Xint argc; Xchar *argv[]; X{ X int expand = 1; /* expansion factor */ X int reduce = 2; /* reduction factor */ X int tabsize = 8; /* hardware tab size */ X int tabflag = TRUE; /* insert tabs in output? */ X int c; /* generic character */ X int i; /* generic integer */ X extern int optind; /* argument index */ X extern char *optarg; /* argument to options */ X FILE *infile; /* input file descriptor */ X char inbuf[MAX_LINE]; /* input buffer */ X char outbuf[MAX_LINE]; /* output buffer */ X register char *ip, *op; /* buffer in & out pointers */ X register int count; /* whitespace counter */ X X /* sort out the command line options, if any */ X while ((c = getopt(argc, argv, "st:r:e:")) != EOF) X switch (c) { X case 's': X tabflag = FALSE; X break; X case 't': X tabsize = atoi(optarg); X break; X case 'r': X reduce = atoi(optarg); X break; X case 'e': X expand = atoi(optarg); X break; X case '?': X usage(argv[0]); X } X X /* open input file or stdin as required */ X if (argv[optind] != NULL) { X if ((infile = fopen(argv[optind], "r")) == NULL) { X (void) fprintf(stderr, X "%s: Cannot open %s\n", argv[0], argv[optind]); X exit(2); X } X } else { X if (isatty(fileno(stdin)) == 0) X infile = stdin; X else X usage(argv[0]); X } X X /* options are parsed, input file is open, let's go! */ X while ((ip = fgets(inbuf, MAX_LINE, infile)) != NULL) { X X /* count leading white space */ X count = 0; X while ((c = *ip++) == ' ' || c == '\t') X switch (c) { X case ' ': X count++; X break; X case '\t': X count += tabsize - (count % tabsize); X break; X } X ip--; X X /* ratio the white space */ X count = (count * expand) / reduce; X X /* put proper white space in output buffer */ X op = outbuf; X if (tabflag) { X i = count / tabsize; X while (i--) X *op++ = '\t'; X count = count % tabsize; X } X while (count--) X *op++ = ' '; X X /* copy rest of input to output */ X while (*op++ = *ip++); X X /* dump the output buffer */ X (void) fputs(outbuf, stdout); X } X exit(0); X} END_OF_FILE if test 4160 -ne `wc -c <'indent.c'`; then echo shar: \"'indent.c'\" unpacked with wrong size! fi # end of 'indent.c' fi if test -f 'Makefile' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Makefile'\" else echo shar: Extracting \"'Makefile'\" \(239 characters\) sed "s/^X//" >'Makefile' <<'END_OF_FILE' X# Makefile for indent - 1.1 X# Last changed 9/20/88 19:57:16 X XBINDIR = /usr/local/bin X Xindent: indent.c X $(CC) $(CFLAGS) indent.c -o indent X Xinstall: indent X strip indent X -ln indent $(BINDIR) X touch install X Xlint: X lint -p indent.c >Lint END_OF_FILE if test 239 -ne `wc -c <'Makefile'`; then echo shar: \"'Makefile'\" unpacked with wrong size! fi # end of 'Makefile' fi if test -f 'test_file' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'test_file'\" else echo shar: Extracting \"'test_file'\" \(201 characters\) sed "s/^X//" >'test_file' <<'END_OF_FILE' XTest file for indent: XNo indent. X One tab. X Two tabs. X Three tabs. X One space. X Seven spaces. X Eight spaces. X Nine spaces. X Three spaces, one tab. X One tab, three spaces. END_OF_FILE if test 201 -ne `wc -c <'test_file'`; then echo shar: \"'test_file'\" unpacked with wrong size! fi # end of 'test_file' fi echo shar: End of shell archive. exit 0