home *** CD-ROM | disk | FTP | other *** search
- ***********************************************************************
- The following are fragments from the source to cb.c to help you understand
- how to use it and what to expect. It was passed through cb before being
- made into this '.doc' file.
-
- This program is independent of the WECO cb and has a number of important
- differences:
- 1. My cb doesn't toss cookies.
- 2. This beautifier doesn't deface pretty comments.
- 3. Unless told to do so, cb doesn't waste much paper with breaks
- that aren't absolutely necessary for clarity. This helps CRT users also.
- 4. Some syntax errors are reported, especially bad strings and
- mismatched () or {}.
- 5. Optionally, the SOURCE line number may be listed for each output
- line.
-
- AVAILABILITY: Currently, the only legitimate form of my 'cb' is as
- a BDS C .CRL file. Of course, this is useful only to those with BDS C
- licenses (I hope).
- I will endeavour to make a linkable object of this program available
- to users of any unrestricted PUBLIC DOMAIN C compiler which has a sufficient
- subset of C to compile this program.
- Vendors of C compilers may obtain source to 'cb' by agreeing to
- provide me a binary license to their C compiler and system (if their own)
- for each such combination as they wish to make 'cb' available for.
- Chuck Forsberg
- 503/621-3406 voice, 621-3193 RCP/M
-
- Program beautifiers are especially useful in suggesting where braces have
- been inadvertently omitted or included.
-
- There are some instances of C code which are nearly unreadable (to me)
- without benefit of this program.
-
- /*
- * cb - A Prettyprinter for C programs
- * Copyright 1981 Computer Development Inc Beaverton OR
- * All Rights Reserved
- * Chuck Forsberg author
- *
- * The baroqueness of this program is a reflection of my
- * preferred "style" of indenting C code.
- */
-
- #define VERSION "cb 10-3-81\n"
-
- unsigned Edit; /* bits indicating editing options selected */
- /*
- * Editing options. "nothing after" really means "nothing
- * except perhaps a comment."
- */
-
- #define EBLBR 1 /* nothing before left brace */
- #define EALBR 2 /* nothing after left brace */
- #define EBRBR 4 /* nothing before right brace */
- #define EARBR 8 /* t "tail" or right brace */
- #define ECTL 16 /* newline after if, while, for head */
- #define ESEMI 32 /* newline after semicolons */
- #define ECASE 64 /* labels and cases one per line */
- #define ESHARP 128 /* # indent ifdef's et al */
- #define EFOOL 1024 /* anyplace where not breaking would be confusing */
- #ifdef xxx1
- # ifdef xxx2
- # ifdef xxx3
- # ifdef xxx4
- /* test for levels of indentation on #if's */
- # endif
- # endif
- # endif
- #endif
- /* WARNING- BDS C doesn't understand nested conditionals!! */
-
- cp="~|:Copyright CDI 1981 All Rights Reserved";
- Edit=EFOOL;
- Print=Verbose=Number=npats=0;
- what[0]=0;
- tabwidth=shiftwidth=8;
-
- if(argc<2)
- goto usage;
-
- while (--argc) {
- cp = *++argv;
- if(*cp == '-') {
- while( *++cp) {
- #ifdef CPM
- switch(tolower(*cp)) {
- #else
- switch(*cp) {
- #endif
- case 'e':
- while(*++cp) {
- switch(tolower(*cp)) {
- case 'z':
- Edit= 0;
- case 'e':
- Edit= ~0;
- case '#':
- Edit|=ESHARP;break;
- case 'l':
- Edit |= EBLBR;break;
- case 'a':
- Edit |= EALBR;break;
- case 'r':
- Edit |= EARBR;break;
- case 'b':
- Edit |= EBRBR;break;
- case 'c':
- Edit |= ECASE;break;
- case 'i':
- Edit |= ECTL;break;
- case 's':
- Edit |= ESEMI;break;
- default:
- goto usage;
- }
- }
- cp--;
- break;
- case 'p':
- out=2; Print++; break;
- case 'n':
- Number++; break;
- case 's':
- shiftwidth= *++cp - '0'; break;
- case 't':
- tabwidth= *++cp - '0'; break;
- case 'v':
- Verbose++; break;
- default:
- goto usage;
- }
- }
- }
- else if( !npats && argc) {
- if(argv[0][0]) {
- npats=argc;
- patts=argv;
- }
- }
- }
- if(npats < 1 || npats > 2) {
- usage:
- printf(VERSION);
- printf(" CB - a C program Beautifier\n");
- printf("By Chuck Forsberg, Computer Development Inc\n");
- printf("6700 S.W. 105th Beaverton OR 97005 503/646-1599\n");
- printf("Send Comments/Bug-reports via RCP/M 503/621-3193\n");
- printf("Copyright 1981 Computer Development INC. All rights Reserved.\n");
- printf("The BDS C relocatable file 'CB.CRL' is hereby placed\n");
- printf("in the public domain by its author. This file must be\n");
- printf("linked by the BDS C linker to produce an executable\n");
- printf("program. This program may not be distributed in any other\n");
- printf("form without written permission from the above.\n\n");
- printf("Usage: cb [-e{ze#labric}] [-pnsMtMv] infile [outfile]\n");
- printf("where:\n -e{option} affects editing:\n");
- printf("\t\tl Break line before left brace\n");
- printf("\t\ta Break line after left brace\n");
- printf("\t\tr Break line after right brace\n");
- printf("\t\tb Break line before right brace\n");
- printf("\t\tc Break line at cases\n");
- printf("\t\ti Break line after do,if, etc.\n");
- printf("\t\t# Indenet nested #if...'s\n");
- printf("\t\tz No line breaks or indentented #if..'s\n");
- printf("\t\te All of the above (`ala Kernighan/Ritchie)\n");
-
- #ifdef CPM
- printf(" -p Output to LST: device\n");
- #endif
- printf(" -n Number source lines\n");
- printf(" -sM Indentation shift width is 1<=M<=9 (Default 8)\n");
- printf(" -tM Assume Hardware tab width is 1<=M<=9 (Default 8)\n");
- printf(" -v Verbose More v's are more verbose\n");
- #ifdef CPM
- printf(" [outfile] write ouput to outfile\n");
- exit(1);
- }
- #ifdef CPM
-
- if(npats) {
- pathname=patts[0];
- if(fopen( patts[0], fin) == ERROR)
- {
- fprintf(stderr, "Can't open %s\n", patts[0]);
- exit(128);
- }
- else
- in=fin;
- }
- if(npats==2) {
- if(fopen(patts[1], fout) != ERROR) {
- fprintf(stderr, "Output file exists");
- exit(9);
- }
- if(fcreat(patts[1], fout)== ERROR)
- {
- fprintf(stderr, "Can't create %s\n", patts[1]);
- exit(128);
- }
- else
- out=fout;
- }
- goto qagain;
- /*
- * Comments are printed in a particular style.
- */
- /*
- * If found at the margin, they stay there.
- */
- cagain:
- case '/':
- if(*pos == '/' && pos[1] == NATHAN) {
- if(pos==line) {
- jamcom++;
- jamleft |= 1;
- }
- if(comments++)
- nestedc++;
- pos +=2;
- }
- else if(comments==0)
- break;
- if(*pos == NATHAN && pos[1] == '/') {
- pos++;
- if(--comments == 0)
- {
- jamcom= 0;
- break;
- }
- pos++;
- }
- if(*pos == 0)
- {
- if(jamcom) {
- jamleft |= 1;
- pos0= line;
- }
- doline();
- if( *pos0 == NATHAN && pos0[-1] == ' ' && pos0 > line)
- --pos0;
- if(jamcom)
- jamleft |= 1;
- }
- else
- pos++;
- goto cagain;
-
- #ifdef CPM
- #undef putc
- xputc(cha, channel) /* a putch that tabs the printer */
- {
-
- if(channel==2) {
- if(cha=='\t') {
- do
- putc(040, 2);
- while( ++column & 07);
- }
- else {
- putc(cha, 2);
- if(cha > 037)
- column++;
- else
- column=0;
- }
- return OK;
- }
- else
- return putc(cha, channel);
- }
- #endif
- /*
- * If found at the margin, they stay there.
-