home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume4 / indent < prev    next >
Encoding:
Text File  |  1989-02-03  |  8.1 KB  |  264 lines

  1. Path: xanth!mcnc!rutgers!ukma!cwjcc!hal!ncoast!allbery
  2. From: chad@anasaz.UUCP
  3. Newsgroups: comp.sources.misc
  4. Subject: v04i089: comp.sources.misc submission
  5. Message-ID: <8809211216.AA21093@noao.edu>
  6. Date: 25 Sep 88 01:26:27 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: chad@anasaz.UUCP ()
  9. Lines: 252
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 4, Issue 89
  13. Submitted-by: "A. Nonymous" <chad@anasaz.UUCP>
  14. Archive-name: indent
  15.  
  16. This is a filter that allows you to expand/contract the leading white
  17. space in a file.  It is useful to change the indentation for nesting
  18. levels in C source, or handling source created with an editor who's
  19. tabstops are not where your editor thinks they are.  For example, the
  20. C Beautifier cb(1) adjusts its indent level by one full hardware tab
  21. for each "{" or "}".  I like my indent level to be 4 spaces, so I do:
  22.     "cb -s foo.c | indent | hold foo.c"
  23. If you wanted an indent of 2 you could do:
  24.     "cb -s foo.c | indent -r4 | hold foo.c"
  25. If you wanted an indent of 6 you could do:
  26.     "cb -s foo.c | indent -r4 -e3 | hold foo.c"
  27. There are obviously many other permutations.  You also have control
  28. over whether the output file will use spaces only or a combination of
  29. spaces and tabs in the leading white space:
  30.     "indent -r1 foo.c | hold foo.c"
  31. will convert leading white space to spaces & tabs with no other change
  32. to the file.
  33. ---------------
  34. "I read the news today, oh boy!"  --John Lennon
  35. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  36. | DCF, Inc.               | UUCP: ...ncar!noao!nud!anasaz!dcfinc!chad   |
  37. | 14623 North 49th Place  | Ma Bell: (602) 953-1392                     |
  38. | Scottsdale, AZ 85254    | Loran: N-33deg37min20sec W-111deg58min26sec |
  39. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  40. |         Disclaimer: These ARE the opinions of my employer!            |
  41. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  42.  
  43. #! /bin/sh
  44. # This is a shell archive.  Remove anything before this line, then unpack
  45. # it by saving it into a file and typing "sh file".  To overwrite existing
  46. # files, type "sh file -c".  You can also feed this as standard input via
  47. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  48. # will see the following message at the end:
  49. #        "End of shell archive."
  50. # Contents:  indent.c Makefile test_file
  51. # Wrapped by chad@dcfinc on Tue Sep 20 19:58:43 1988
  52. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  53. if test -f 'indent.c' -a "${1}" != "-c" ; then 
  54.   echo shar: Will not clobber existing file \"'indent.c'\"
  55. else
  56. echo shar: Extracting \"'indent.c'\" \(4160 characters\)
  57. sed "s/^X//" >'indent.c' <<'END_OF_FILE'
  58. X/* indent.c - Adjust indentation of file - 1.2 */
  59. X
  60. X/*
  61. X** This filter will adjust the indentation of an existing file.  It
  62. X** is most useful for C source created with an editor program that 
  63. X** doesn't agree with your notion of tab stop locations.  I use it to
  64. X** make the output of cb(1) have a nesting level of 4, rather than 8.
  65. X** In the spirit of Unix tools, I'm sure you will find other uses.
  66. X*/
  67. X
  68. X/*
  69. X** Author:
  70. X**   Chad R. Larson            This program is placed in the
  71. X**   DCF, Inc.                Public Domain.  You may do with
  72. X**   14623 North 49th Place        it as you please.
  73. X**   Scottsdale, AZ 85254
  74. X*/
  75. X
  76. X/*
  77. X** Usage is "indent [-tn] [-rn] [-en] [-s] [file]"
  78. X**   Where:
  79. X**    -tn    Set hardware tab stop (for both input and output)
  80. X**        to n.  If this option is omitted, n=8.
  81. X**    -rn    Set reduction factor to n.  That is, if n=3 each
  82. X**        output line will be indented 1/3 as much as its
  83. X**        corresponding input line.  Default value for n is 2.
  84. X**    -en    Set expansion factor to n.  That is, if n=3 each
  85. X**        output line will be indented 3 times as much as its
  86. X**        corresponding input line.  Default value for n is 1.
  87. X**        The -e and -r options may be used in conjunction,
  88. X**        for example to get a two thirds indentation reduction.
  89. X**    -s    Supress tabs in output.  Normally, multiple leading
  90. X**        spaces will be replaced with the necessary amount of
  91. X**        tab characters to reduce the size of the output file.
  92. X**        Note this option only applies to leading white space.
  93. X**        No other tabs will be affected.
  94. X**    If no output file is specified, the standard input will be read.
  95. X**    Output is to the standard output.  Error messages will be printed
  96. X**    on standard error (pretty standard, hmmm?).
  97. X*/
  98. X
  99. X#include <stdio.h>
  100. X
  101. X#define    FALSE 0
  102. X#define TRUE ~FALSE
  103. X#define MAX_LINE 512
  104. X
  105. X/* linked in later */
  106. Xvoid    exit();
  107. X
  108. X/* give the operator a clue */
  109. Xvoid usage(prog_name)
  110. Xchar    *prog_name;
  111. X{
  112. X    (void) fprintf(stderr, "%s version 1.2 - 9/20/88\n", prog_name);
  113. X    (void) fprintf(stderr,
  114. X      "Usage: %s [-tn] [-rn] [-en] [-s] [file]\n", prog_name);
  115. X    exit(1);
  116. X}
  117. X
  118. X/* here it is */
  119. Xvoid main(argc, argv)
  120. Xint    argc;
  121. Xchar    *argv[];
  122. X{
  123. X    int            expand = 1;        /* expansion factor */
  124. X    int            reduce = 2;        /* reduction factor */
  125. X    int            tabsize = 8;        /* hardware tab size */
  126. X    int            tabflag = TRUE;        /* insert tabs in output? */
  127. X    int            c;            /* generic character */
  128. X    int            i;            /* generic integer */
  129. X    extern int        optind;            /* argument index */
  130. X    extern char        *optarg;        /* argument to options */
  131. X    FILE        *infile;        /* input file descriptor */
  132. X    char        inbuf[MAX_LINE];    /* input buffer */
  133. X    char        outbuf[MAX_LINE];    /* output buffer */
  134. X    register char    *ip, *op;        /* buffer in & out pointers */
  135. X    register int    count;            /* whitespace counter */
  136. X
  137. X    /* sort out the command line options, if any */
  138. X    while ((c = getopt(argc, argv, "st:r:e:")) != EOF)
  139. X    switch (c) {
  140. X    case 's':
  141. X        tabflag = FALSE;
  142. X        break;
  143. X    case 't':
  144. X        tabsize = atoi(optarg);
  145. X        break;
  146. X    case 'r':
  147. X        reduce = atoi(optarg);
  148. X        break;
  149. X    case 'e':
  150. X        expand = atoi(optarg);
  151. X        break;
  152. X    case '?':
  153. X        usage(argv[0]);
  154. X    }
  155. X
  156. X    /* open input file or stdin as required */
  157. X    if (argv[optind] != NULL) {
  158. X    if ((infile = fopen(argv[optind], "r")) == NULL) {
  159. X        (void) fprintf(stderr,
  160. X          "%s: Cannot open %s\n", argv[0], argv[optind]);
  161. X        exit(2);
  162. X    }
  163. X    } else {
  164. X    if (isatty(fileno(stdin)) == 0)
  165. X        infile = stdin;
  166. X    else
  167. X        usage(argv[0]);
  168. X    }
  169. X
  170. X    /* options are parsed, input file is open, let's go! */
  171. X    while ((ip = fgets(inbuf, MAX_LINE, infile)) != NULL) {
  172. X
  173. X    /* count leading white space */
  174. X    count = 0;
  175. X    while ((c = *ip++) == ' ' || c == '\t')
  176. X        switch (c) {
  177. X        case ' ':
  178. X        count++;
  179. X        break;
  180. X        case '\t':
  181. X        count += tabsize - (count % tabsize);
  182. X        break;
  183. X        }
  184. X        ip--;
  185. X
  186. X    /* ratio the white space */
  187. X    count = (count * expand) / reduce;
  188. X
  189. X    /* put proper white space in output buffer */
  190. X    op = outbuf;
  191. X    if (tabflag) {
  192. X        i = count / tabsize;
  193. X        while (i--)
  194. X        *op++ = '\t';
  195. X        count = count % tabsize;
  196. X    }
  197. X    while (count--)
  198. X        *op++ = ' ';
  199. X
  200. X    /* copy rest of input to output */
  201. X    while (*op++ = *ip++);
  202. X
  203. X    /* dump the output buffer */
  204. X    (void) fputs(outbuf, stdout);
  205. X    }
  206. X    exit(0);
  207. X}
  208. END_OF_FILE
  209. if test 4160 -ne `wc -c <'indent.c'`; then
  210.     echo shar: \"'indent.c'\" unpacked with wrong size!
  211. fi
  212. # end of 'indent.c'
  213. fi
  214. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  215.   echo shar: Will not clobber existing file \"'Makefile'\"
  216. else
  217. echo shar: Extracting \"'Makefile'\" \(239 characters\)
  218. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  219. X# Makefile for indent - 1.1
  220. X# Last changed 9/20/88 19:57:16
  221. X
  222. XBINDIR = /usr/local/bin
  223. X
  224. Xindent:        indent.c
  225. X    $(CC) $(CFLAGS) indent.c -o indent
  226. X
  227. Xinstall:    indent
  228. X    strip indent
  229. X    -ln indent $(BINDIR)
  230. X    touch install
  231. X
  232. Xlint:
  233. X    lint -p indent.c >Lint
  234. END_OF_FILE
  235. if test 239 -ne `wc -c <'Makefile'`; then
  236.     echo shar: \"'Makefile'\" unpacked with wrong size!
  237. fi
  238. # end of 'Makefile'
  239. fi
  240. if test -f 'test_file' -a "${1}" != "-c" ; then 
  241.   echo shar: Will not clobber existing file \"'test_file'\"
  242. else
  243. echo shar: Extracting \"'test_file'\" \(201 characters\)
  244. sed "s/^X//" >'test_file' <<'END_OF_FILE'
  245. XTest file for indent:
  246. XNo indent.
  247. X    One tab.
  248. X        Two tabs.
  249. X            Three tabs.
  250. X One space.
  251. X       Seven spaces.
  252. X        Eight spaces.
  253. X         Nine spaces.
  254. X       Three spaces, one tab.
  255. X       One tab, three spaces.
  256. END_OF_FILE
  257. if test 201 -ne `wc -c <'test_file'`; then
  258.     echo shar: \"'test_file'\" unpacked with wrong size!
  259. fi
  260. # end of 'test_file'
  261. fi
  262. echo shar: End of shell archive.
  263. exit 0
  264.