home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume04 / indent < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  8.3 KB

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