home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 110_01 / cbc.c < prev    next >
Text File  |  1984-03-03  |  8KB  |  313 lines

  1. /*
  2. NAME:        CBC.C
  3. AUTHOR:        Ward Christensen
  4. WRITTEN:    November, 1980
  5. DESCRIPTION:    BDS-C    Curly    Brace    Checker.  Also counts
  6.             =    =    =
  7.         comments, since they may now be nested.
  8. PROCESSOR:    BDS-C.  Tested at the 1.41-1.43 levels
  9. DEPENDENCIES:    BDS-C 1.4 file I/O
  10. EXECUTION:    CBC filename
  11.         Output to the console.
  12. REVISIONS:    NONE
  13. COMMENTS:    When the BDS-C compiler tells you you have problems
  14.         with your curly braces, and you can't readily find
  15.         the problem, run CBC to help highlight them.  Every
  16.         time the count of curly-brace nesting goes to 0,
  17.         it prints a line of "----------------" to highlight
  18.         it.  This should typically be the end of a function,
  19.         or the end of a comment outside of a function.
  20. */
  21.  
  22. #include bdscio.h
  23.  
  24. /*        GLOBALS        (start with capital letters)
  25. */
  26.     char Ibuf[BUFSIZ],Linebuf[132];
  27.  
  28. main(argc,argv)
  29. int argc; char **argv;
  30. {
  31.     int    fd,    /* file descriptor        */
  32.         ncb,    /* depth of curly braces    */
  33.         pncb,    /* prev ncb to hilite fncs    */
  34.         lno,    /* line #            */
  35.         errflg,    /* negative nesting error    */
  36.         i;    /* character buffer index    */
  37.  
  38.     ncb=0;        /* init brace depth        */
  39.     pncb=0;        /* no prev nesting        */
  40.     errflg=0;    /* not close before open brace    */
  41.     lno=1;        /* init line #            */
  42.  
  43.     if (argc != 2)    /* validate argument list */
  44.     {    printf("CBC: Curly Brace Checker for C programs\n");
  45.         printf("by Ward Christensen\n");
  46.         printf("Usage: cbc filename\n");
  47.         exit();
  48.     }
  49.     if ((fd=fopen(argv[1],Ibuf))==-1)
  50.     {    printf("cannot open: %s\n",argv[1]);
  51.         exit();
  52.     }
  53.  
  54.     while (fgets(Linebuf,Ibuf))    /* while data    */
  55.     {    for(i=0;Linebuf[i];++i)
  56.         {    /* count close brace and comments */
  57.             if(Linebuf[i]=='}')--ncb;
  58.             if(Linebuf[i]=='*' &&
  59.                Linebuf[i+1]=='/')--ncb;
  60.             if(ncb<0)++errflg;
  61.  
  62.             /* count open brace and comments */
  63.             if(Linebuf[i]=='{')++ncb;
  64.             if(Linebuf[i]=='/' &&
  65.                Linebuf[i+1]=='*')++ncb;
  66.         }            
  67.         if(errflg>0)
  68.         {    printf("\n++ bad nesting: ++\n");
  69.             errflg=0;
  70.         }
  71.         if(errflg<0)
  72.             errflg=0;
  73.         printf("%3d %2d: %s",lno++,ncb,Linebuf);
  74.         if(pncb && (ncb==0))        /* just decrement to 0?    */
  75.             printf("----------------------\n");
  76.         pncb=ncb;
  77.     }
  78. }
  79. ~?ccccccc~x`?A|                                                                    π  ≤  ²▀      π   U                                                                                                                                                                                                                                                                /*
  80.  
  81.     The following is a most extroadinary program.
  82.  
  83.  
  84.     Why??
  85.  
  86.     It's a software analogue to David and Goliath,  where an
  87. inexpensive S-100 home computer programmed in a high level language
  88. slays a giant** IBM mainframe written that most giant of all
  89. languages, Assembler.
  90.  
  91.     My purpose in disseminating these results is to prod people
  92. into considering the advantages of programming in a modern, rich,
  93. portabale, maintainable high level language such as C.
  94.  
  95.     You can't dismiss a high level language out of hand just
  96. because of slow execution speed!
  97.  
  98. ---------------------------------------------------------------------
  99.  
  100. TIMES:
  101.  
  102. TRS-80 BASIC        Z80    23470 SEC
  103.  
  104. ASSEMBLER        Z80     1370 SEC
  105.  
  106. FIFTH            Z80      365 SEC
  107.  
  108. OPTIMIZING        370/148       79 SEC
  109. PL/I
  110.  
  111. BAL            370/148       56 SEC
  112.  
  113. VAX-11 BASIC        11/780       58.2 CPU SEC
  114.     (I/O TIME NOT INCLUDED)
  115.  
  116. BDS C 1.42 + CP/M    CROMENCO ZPU 4 MHZ 56 SEC
  117.  
  118. BDS C 1.42 + CP/M        Z-89      127 sec
  119.  
  120. BDS C 1.43a    Godbout dual    8085       45.84 sec
  121.     (8085 running at 5 Mhz)
  122.  
  123. */
  124.  
  125. /*
  126.  * C version of Prime Nuber Prog #3 of 3
  127.  * From Page 7-21 of 1980 DEC VAX Technical Summary
  128.  * Originally from March 80 Byte Magazine
  129.  * Incredible execution times are at end of this file.
  130.  *
  131.  * Sorry to say, my translation is no more readable than the
  132.  * Basic version.
  133.  *
  134.  */
  135.  
  136. /* Translated to C by Chuck Forsberg WA7KGX 7/3/81 */
  137. #include "a:bdscio.h"
  138. int c,m,k,p;
  139. struct _buf out;
  140.  
  141. main()
  142. {
  143.     fcreat("B:primes3.tmp", out);
  144.     printf("PRIME3\n\007");
  145.     fprintf(out, "    %d    %d    %d\n", 1,2,3);
  146.     c=0; m=3;
  147. p80:
  148.     m += 2;
  149.     p = m>>1;
  150.     for(k=3; k<=p; k += k-1) {
  151.         if(m%k == 0)
  152.             goto p190;
  153.     }
  154.     if(++c & 07)
  155.         fprintf(out, "    %d", m);
  156.     else
  157.         fprintf(out, "    %d\n", m);
  158. p190:
  159.     if(m<10000)
  160.         goto p80;
  161.     fprintf(out, "\nC= %d\n", c);
  162.     printf("C= %d\n", c);
  163.     /*
  164.      * I'm cheating just like VAX Basic by excluding file
  165.      * open and closing times.
  166.      */
  167.     printf("\007Time's Up\n");
  168.     putc(CPMEOF, out);
  169.     fflush(out);
  170.     fclose(out);
  171. }
  172. /*    The VAX Basic program.
  173. 15 DECLARE INTEGER M,K;
  174. 20 OPEN "PRIMES3.TMP" FOR OUTPUT AS FILE #1
  175. 30 PRINT "PRIME3"+TIME$(0)
  176. 40 PRINT
  177. 50 PRINT #1, 1;2;3;
  178. 55 C=0
  179. 70 M=3
  180. 80 M=M+2
  181. 90 FOR K=3 TO M/2 STEP K-1
  182. 100  IF(INT(M/K)*K-M = 0 THEN 190
  183. 110 NEXT K
  184. 121 PRINT #1%, M;
  185. 122 C=C+1
  186. 190 IF M < 10000 THEN 80
  187. 195 PRINT #1%, "C=";C
  188. 196 PRINT "C=";C
  189. 199 T2=TIME(1%)
  190.     P$="DONE"+NUM1$(0.1*(T2-T1))+"CPU SEC"
  191. 200 PRINT P$
  192.     PRINT #1, P$
  193. 201 END
  194.  
  195.     Version modified for Microsoft BASIC-80 Dialect
  196.  
  197. 15 DEFINT C,I,J,K,M
  198. 30 PRINT "PRIMES3"
  199. 40 PRINT
  200. 50 PRINT 1;2;3
  201. 55 C=0
  202. 70 M=3
  203. 80 M=M+2
  204. 90 FOR K=3 TO M/2 STEP K-1
  205. 100 IF ( M MOD K ) = 0 THEN 190
  206. 110 NEXT K
  207. 121 PRINT M;
  208. 122 C=C+1
  209. 190 IF M < 10000 THEN 80
  210. 196 PRINT "C=";C
  211. 200 PRINT "DONE"
  212.  
  213.     The above program was stopped at 7 minutes, it had only
  214. reached about 3000, running on a 4Mhz ZPU with no wait states.
  215.  
  216.     FIFTH version of the above.  365 Seconds on 64k Z89
  217. (2 mHz Z80).
  218.  
  219. ( prime numbers benchmark ) PRIMARY DECIMAL
  220. : PBENCH ." Prime numbers benchmark" CR CR ." 1 2 3 "
  221.         0 ( C)   3 ( m )
  222.         BEGIN 2+ ( m=m+2)
  223.             TRUE ( flag) OVER 2/ 3 DO
  224.               OVER ( m) I MOD  ( m mod k)
  225.               IF ELSE NOT LEAVE ENDIF I 1- +LOOP ( step k-1)
  226.               IF DUP . SWAP 1+ SWAP ENDIF ( print prime, c=c+1)
  227.         DUP 10000 >= UNTIL
  228.         CR ." C=" DROP . CR ." Done" 7 0 PUT ;
  229.  
  230. ;S
  231.  
  232. */
  233.  
  234. /*
  235.  * BAL means Basic Assembly Language, used on IBM mainframes
  236.  * BDS C was timed on a S-100 system with Cromenco ZPU 4 Mhz,
  237.  * 4FDC, Persci 277 (Single Dens), Microbyte static RAM, and
  238.  * Tek 4012 at 9600 baud.  Timed by Microma Digital Chronograph
  239.  * and bell-activated right hand index finger.
  240.  * The Zenith Z-89 had 64k and twin 5 inch drives.
  241.  * Timing for the BDS C systems includes most of the i/o, except
  242.  * for the final buffer flush and file closing.  The VAX Basic
  243.  * (and, presumably, PDP-10 basic as well) do NOT include i/o
  244.  * time!
  245.  *
  246.  *
  247.  * Other benchmarks suggest that C on 16 bit CPU's is many
  248.  * times faster than BDS C (on 8080's or Z80's).
  249.  *
  250.  * DEC IBM BDS 11 TRS et al trademarks
  251.  */
  252. cuchleft!=CURLEFT) {
  253.         poke(cuaddr,cuchleft);
  254.         poke(cuaddr+1,cuchrght);
  255.         }
  256.     switch (x)
  257.         {
  258.     case 'h': left();    /* move cursor left */
  259.         break;
  260.     case 'k': right();    /* and right */
  261.         break;
  262.     case 'j': down();    /* or down */
  263.         break;
  264.     case 'u': up();        /* or up */
  265.         break;
  266.     case 'b': bright();    /* place dot at cursor position */
  267.         break;
  268.     case 'd': dim();    /* remove dot f/*
  269.     Binary to HEX-ASCII utility
  270.  
  271.     Makes a binary file into a HEX file, 32 bytes/block with
  272.     proper sumcheck byte at the end. The starting address for the
  273.     HEX file is settable; the input is assumed to be contiguous
  274.     locations. Some garbage may be included at the end of the
  275.     HEX file since the program does not recognize 0x1A as an
  276.     end-of-file code in the binary input.
  277.  
  278.     Called with:
  279.         bin2hex [<binary file 1> [<bin. file 2>...]]
  280.  
  281.     Nothing special required, but if you have NSECTS
  282.     set larger than 1 you may want to include the
  283.     modified "getc" for better end-of-file indication.
  284.  
  285. written by:
  286.     Robert Pasky
  287.     36 Wiswall Rd
  288.     Newton Centre, MA 01259
  289. */
  290.  
  291. #include "bdscio.h"
  292.  
  293. #define CTRLQ 0x11    /* quit key: control-q */
  294.  
  295. int i, j, fd;
  296. int addr,sad;
  297. char fnbuf[30], *fname;        /* filename buffer & ptr */
  298. char onbuf[30], *oname;        /* output filename buf & ptr */
  299. char ibuf[BUFSIZ];    /* buffered input buffer */
  300. char obuf[BUFSIZ];    /* buffered output */
  301. char hextab[16];
  302. char kybdbuf[20];
  303. char sumcheck;
  304.  
  305. main(argc,argv)
  306. char **argv;
  307. {
  308.     strcpy(hextab,"0123456789ABCDEF");
  309.  
  310.     while (1)
  311.     {
  312.         oname = onbuf;
  313.         if (argc