home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / LANGUAGS / C / C-BEAUT.LBR / CB.DOC < prev    next >
Text File  |  2000-06-30  |  7KB  |  274 lines

  1. ***********************************************************************
  2. The following are fragments from the source to cb.c to help you understand
  3. how to use it and what to expect.  It was passed through cb before being
  4. made into this '.doc' file.
  5.  
  6. This program is independent of the WECO cb and has a number of important
  7. differences:
  8.     1.  My cb doesn't toss cookies.
  9.     2.  This beautifier doesn't deface pretty comments.
  10.     3.  Unless told to do so, cb doesn't waste much paper with breaks
  11. that aren't absolutely necessary for clarity.  This helps CRT users also.
  12.     4.  Some syntax errors are reported, especially bad strings and
  13. mismatched () or {}.
  14.     5.  Optionally, the SOURCE line number may be listed for each output
  15. line.
  16.  
  17.     AVAILABILITY: Currently, the only legitimate form of my 'cb' is as
  18. a BDS C .CRL file.  Of course, this is useful only to those with BDS C
  19. licenses (I hope).
  20.     I will endeavour to make a linkable object of this program available
  21. to users of any unrestricted PUBLIC DOMAIN C compiler which has a sufficient
  22. subset of C to compile this program.
  23.     Vendors of C compilers may obtain source to 'cb' by agreeing to
  24. provide me a binary license to their C compiler and system (if their own)
  25. for each such combination as they wish to make 'cb' available for.
  26.                     Chuck Forsberg
  27.                     503/621-3406 voice, 621-3193 RCP/M
  28.  
  29. Program beautifiers are especially useful in suggesting where braces have
  30. been inadvertently omitted or included.
  31.  
  32. There are some instances of C code which are nearly unreadable (to me)
  33. without benefit of this program.
  34.  
  35. /*
  36.  * cb - A Prettyprinter for C programs
  37.  * Copyright 1981 Computer Development Inc Beaverton OR
  38.  * All Rights Reserved
  39.  * Chuck Forsberg author
  40.  *
  41.  * The baroqueness of this program is a reflection of my
  42.  * preferred "style" of indenting C code.
  43.  */
  44.  
  45. #define VERSION "cb 10-3-81\n"
  46.  
  47. unsigned Edit;    /* bits indicating editing options selected */
  48. /*
  49.  * Editing options.  "nothing after" really means "nothing
  50.  * except perhaps a comment."
  51.  */
  52.  
  53. #define EBLBR 1        /* nothing before left brace */
  54. #define EALBR 2        /* nothing after left brace */
  55. #define EBRBR 4        /* nothing before right brace */
  56. #define EARBR 8        /* t "tail" or right brace */
  57. #define ECTL 16        /* newline after if, while, for head */
  58. #define ESEMI 32    /* newline after semicolons */
  59. #define ECASE 64    /* labels and cases one per line */
  60. #define ESHARP 128    /* # indent ifdef's et al */
  61. #define EFOOL 1024    /* anyplace where not breaking would be confusing */
  62. #ifdef xxx1
  63. #    ifdef xxx2
  64. #        ifdef xxx3
  65. #            ifdef xxx4
  66. /* test for levels of indentation on #if's */
  67. #            endif
  68. #        endif
  69. #    endif
  70. #endif
  71. /* WARNING- BDS C doesn't understand nested conditionals!! */
  72.  
  73.     cp="~|:Copyright CDI 1981 All Rights Reserved";
  74.     Edit=EFOOL;
  75.     Print=Verbose=Number=npats=0;
  76.     what[0]=0;
  77.     tabwidth=shiftwidth=8;
  78.  
  79.     if(argc<2)
  80.         goto usage;
  81.         
  82.     while (--argc) {
  83.         cp = *++argv;
  84.         if(*cp == '-') {
  85.             while( *++cp) {
  86. #ifdef CPM
  87.                 switch(tolower(*cp)) {
  88. #else
  89.                 switch(*cp) {
  90. #endif
  91.                 case 'e':
  92.                     while(*++cp) {
  93.                         switch(tolower(*cp)) {
  94.                         case 'z':
  95.                             Edit= 0;
  96.                         case 'e':
  97.                             Edit= ~0;
  98.                         case '#':
  99.                             Edit|=ESHARP;break;
  100.                         case 'l':
  101.                             Edit |= EBLBR;break;
  102.                         case 'a':
  103.                             Edit |= EALBR;break;
  104.                         case 'r':
  105.                             Edit |= EARBR;break;
  106.                         case 'b':
  107.                             Edit |= EBRBR;break;
  108.                         case 'c':
  109.                             Edit |= ECASE;break;
  110.                         case 'i':
  111.                             Edit |= ECTL;break;
  112.                         case 's':
  113.                             Edit |= ESEMI;break;
  114.                         default:
  115.                             goto usage;
  116.                         }
  117.                     }
  118.                     cp--;
  119.                     break;
  120.                 case 'p':
  121.                     out=2; Print++; break;
  122.                 case 'n':
  123.                     Number++; break;
  124.                 case 's':
  125.                     shiftwidth= *++cp - '0'; break;
  126.                 case 't':
  127.                     tabwidth= *++cp - '0'; break;
  128.                 case 'v':
  129.                     Verbose++; break;
  130.                 default:    
  131.                     goto usage;
  132.                 }
  133.             }
  134.         }
  135.         else if( !npats && argc) {
  136.             if(argv[0][0]) {
  137.                 npats=argc;
  138.                 patts=argv;
  139.             }
  140.         }
  141.     }
  142.     if(npats < 1 || npats > 2) {
  143. usage:
  144.         printf(VERSION);
  145.         printf("    CB - a C program Beautifier\n");
  146.         printf("By Chuck Forsberg, Computer Development Inc\n");
  147.         printf("6700 S.W. 105th Beaverton OR 97005 503/646-1599\n");
  148.         printf("Send Comments/Bug-reports via RCP/M 503/621-3193\n");
  149.         printf("Copyright 1981 Computer Development INC. All rights Reserved.\n");
  150.         printf("The BDS C relocatable file 'CB.CRL' is hereby placed\n");
  151.         printf("in the public domain by its author.  This file must be\n");
  152.         printf("linked by the BDS C linker to produce an executable\n");
  153.         printf("program.  This program may not be distributed in any other\n");
  154.         printf("form without written permission from the above.\n\n");
  155.         printf("Usage: cb [-e{ze#labric}] [-pnsMtMv] infile [outfile]\n");
  156.         printf("where:\n    -e{option} affects editing:\n");
  157.         printf("\t\tl Break line before left brace\n");
  158.         printf("\t\ta Break line after left brace\n");
  159.         printf("\t\tr Break line after right brace\n");
  160.         printf("\t\tb Break line before right brace\n");
  161.         printf("\t\tc Break line at cases\n");
  162.         printf("\t\ti Break line after do,if, etc.\n");
  163.         printf("\t\t# Indenet nested #if...'s\n");
  164.         printf("\t\tz No line breaks or indentented #if..'s\n");
  165.         printf("\t\te All of the above (`ala Kernighan/Ritchie)\n");
  166.  
  167. #ifdef CPM
  168.         printf("    -p Output to LST: device\n");
  169. #endif
  170.         printf("    -n Number source lines\n");
  171.         printf("    -sM Indentation shift width is 1<=M<=9 (Default 8)\n");
  172.         printf("    -tM Assume Hardware tab width is 1<=M<=9 (Default 8)\n");
  173.         printf("    -v Verbose More v's are more verbose\n");
  174. #ifdef CPM
  175.         printf("    [outfile] write ouput to outfile\n");
  176.         exit(1);
  177.     }
  178. #ifdef CPM
  179.  
  180.     if(npats) {
  181.         pathname=patts[0];
  182.         if(fopen( patts[0], fin) == ERROR)
  183.         {
  184.             fprintf(stderr, "Can't open %s\n", patts[0]);
  185.             exit(128);
  186.         }
  187.         else
  188.             in=fin;
  189.     }
  190.     if(npats==2) {
  191.         if(fopen(patts[1], fout) != ERROR) {
  192.             fprintf(stderr, "Output file exists");
  193.             exit(9);
  194.         }
  195.         if(fcreat(patts[1], fout)== ERROR)
  196.         {
  197.             fprintf(stderr, "Can't create %s\n", patts[1]);
  198.             exit(128);
  199.         }
  200.         else
  201.             out=fout;
  202.     }
  203.             goto qagain;
  204. /*
  205.  * Comments are printed in a particular style.
  206.  */
  207.             /*
  208.              * If found at the margin, they stay there.
  209.              */
  210. cagain:
  211.         case '/':
  212.             if(*pos == '/' && pos[1] == NATHAN) {
  213.                 if(pos==line) {
  214.                     jamcom++;
  215.                     jamleft |= 1;
  216.                 }
  217.                 if(comments++)
  218.                     nestedc++;
  219.                 pos +=2;
  220.             }
  221.             else if(comments==0)
  222.                 break;
  223.             if(*pos == NATHAN && pos[1] == '/') {
  224.                 pos++;
  225.                 if(--comments == 0)
  226.                 {
  227.                     jamcom= 0;
  228.                     break;
  229.                 }
  230.                 pos++;
  231.             }
  232.             if(*pos == 0)
  233.             {
  234.                 if(jamcom) {
  235.                     jamleft |= 1;
  236.                     pos0= line;
  237.                 }
  238.                 doline();
  239.                 if( *pos0 == NATHAN && pos0[-1] == ' ' && pos0 > line)
  240.                     --pos0;
  241.                 if(jamcom)
  242.                     jamleft |= 1;
  243.             }
  244.             else
  245.                 pos++;
  246.             goto cagain;
  247.  
  248. #ifdef CPM
  249. #undef putc
  250. xputc(cha, channel)    /* a putch that tabs the printer */
  251. {
  252.  
  253.     if(channel==2) {
  254.         if(cha=='\t') {
  255.             do
  256.                 putc(040, 2);
  257.                 while( ++column & 07);
  258.         }
  259.         else {
  260.             putc(cha, 2);
  261.             if(cha > 037)
  262.                 column++;
  263.             else
  264.                 column=0;
  265.         }
  266.         return OK;
  267.     }
  268.     else
  269.         return putc(cha, channel);
  270. }
  271. #endif
  272.         /*
  273.              * If found at the margin, they stay there.
  274.