

 KR2ANSI (1)               Commands and Applications                KR2ANSI (1)




 NNAAMMEE 
      kr2ansi - generate function prototypes from K&R C files (Version 1.0) 

 SSYYNNOOPPSSIISS 
      kr2ansi [-p] [ [-r file1] | [usrdef1 ... usrdef20] ] file2 

 DDEESSCCRRIIPPTTIIOONN 
      kr2ansi  reads  a  C  source  file  (written  in  the  programming  style
      introduced  by Kernighan  and  Ritchie, in  "The C Programming Language",
      1st  Edition),  and  writes  the  functions'  prototypes  on the standard
      output.  Output  redirection, via sh (or any other shell), should be used
      for sending the output to a text file.  

 OOPPTTIIOONNSS 
      The following command line options are available: 

      -p        Include parameter-names in protypes

      -r file   Read user-defined data-types from file. This option should
                be the last one, before the name of the C source file.

      usrdef1 ... usrdef20
                Up to 20 user-defined data-types can be specified in the
                command line, providing that option -r is not present

 NNOOTTEESS 
      C  programmers,  who  write  K & R code, may use kr2ansi to create header
      files  with  ANSI prototypes, so their code can be compiled with any ANSI
      compiler.   (kr2ansi  automatically  writes the word "extern" in front of
      every  output  line,  thus making it easy to create header files which in
      turn  can  be  included  (via  #include)  in existant K&R code ). All ten
      standard  C-data-types  - namely: void, char, int, long, unsigned, short,
      float,  double, and FILE (as well the combinations of unsigned, short and
      long  with  int,  and the combination of register with any data-type) are
      recognised  by  the  program.  In  addition,  the  user can specify up to
      twenty  user-defined  types  in  the  command  line.  The program is very
      sensitive  to  the  format of the source file, especially to comments; it
      expects all functions to be declared in the following form: 

      function-type function-name(p1,p2,...,pn) /* COMMENTS */
      /* ALSO YOU CAN HAVE ... */
      /* ...MORE COMMENTS HERE */
          parameter-type  p1,p2;      /* COMMENTS II */
          /* ... OR HERE */
          parameter-type  p4;         /* MORE COMMENTS */
          parameter-type  p5,...,pn;  /* YET MORE COMMENTS */
      {                       /* ... OR EVEN HERE */
        function-body
      }



 Tue October 1 16:24 1991                                                Page 1


 KR2ANSI (1)               Commands and Applications                KR2ANSI (1)




 DDIIAAGGNNOOSSTTIICCSS 
      kr2ansi  warns that something is wrong  by  displaying two error messages
      in the output function-prototypes: 
      1.   "U-N-D-E-F-I-N-E-D" means that either the data types of the specific
           parameters  are undefined, or the words that describe the data types
           are  too  long.  In the first case you should re-run kr2ansi, making
           sure  that you type all user-defined types (used in the source file)
           in  the  command  line.The  second case requires that you change the
           names  of  all data-types that exceed  words' length limit (normally
           set to 50 characters).  
           *** IMPORTANT ***
           Function-declaration   lines   with   undefined  data-type  for  the
           functions,  are  completely  ignored.  (Unfortunately  this  case is
           frequently found in functions of type int).  
      2.   "<...>"   means  that  the  specified  functions  produce prototype-
           lines   which  are   exceeding  lines' length limit (normally set to
           255  characters).   Unfortunately there is no easy way to solve this
           problem;the  best  one  is  to  edit  the output file manually. (the
           other one is to re-compile kr2ansi.c  8*) 

 RREESSTTRRIICCTTIIOONNSS 
      Although  white  characters  (i.e.  tabs,  newlines,  and spaces)  do not
      affect  the  parsing,   extra attention should be given to comments. It's
      possible  to  have  perfectly valid C code which is not handled correctly
      by kr2ansi. The program gets very confused in the following cases: 

      o when a function-declaration does not include the data-type
        (usually found in declarations of integer functions).
      o when comments appear before or among words in a function's
        (or parameter's) declaration line.
      o when there are comments inside the parameter list.
      o when comments open in a line and close in a different one.
      o when a  semicolon in the parameter-declaration comes after
        any commented string(s).
      o when the function-declaration line contains a commented ';'
      o when the character '{' is not the first non-blank char in a
        separate line.

      Here's  an example of a valid C function declaration which drives kr2ansi
      crazy: 

      foo(x,y /* integers */,z/* non-integer*/) /* call: foo(x,y,z);*/
          int   x,       /* MY PROGRAMMING STYLE IS...
                            ...SO UGLY, THAT I DO NOT ...
      */        y        /* ...EXPECT kr2ansi, OR ANY OTHER... */ ;
        char /* ...SIMILAR PROGRAM TO... */   * z[];
        /* ...PARSE MY CODE CORRECTLY;...
           ...UNLESS ITS AUTHOR IS A MAZOCHIST */ {
        return(1);



 Tue October 1 16:24 1991                                                Page 2


 KR2ANSI (1)               Commands and Applications                KR2ANSI (1)



      }

 EEXXAAMMPPLLEESS 
          Suppose we use cat to create a file called 'math.c' as follows:
          % cat >math.c
          >/* ------------------------------------ */
          >#include <stdio.h>
          >
          >typedef int INTEGER;
          >
          >INTEGER main(argc, argv)
          >    int   argc;
          >    char  *argv[];
          >{
          >  void usage();
          >  INTEGER add(), sub();
          >  extern int strcmp();
          >
          >  if (argc < 4)
          >    usage("math add/sub n1 n2");
          >  else if ( !strcmp(argv[1],"add") )
          >    printf("%d\n", add(argv[2],argv[3]) );
          >  else if ( !strcmp(argv[1],"sub") )
          >    printf("%d\n", sub(argv[2],argv[3]) );
          >  else
          >    usage(:math add/sub n1 n2");
          >  return(0);
          >}
          >/* ------------------------------------ */
          >void usage(s)
          >   char *s;
          >{
          >  puts(s);
          >  exit(1);
          >}
          >/* ------------------------------------ */
          >INTEGER add(s1, s2)
          >   char *s1, *s2;
          >{
          >  extern int atoi();
          >
          >  return( atoi(s1) + atoi(s2) );
          >}
          >/* ------------------------------------ */
          >INTEGER sub(s1, s2)
          >   char  *s1, *s2;
          >{
          >  extern int atoi();
          >
          >  return( atoi(s1) - atoi(s2) );



 Tue October 1 16:24 1991                                                Page 3


 KR2ANSI (1)               Commands and Applications                KR2ANSI (1)



          >}
          >/* ------------------------------------ */
          >void dummy()
          >{
          >  /* empty */
          >}
          >/* ------------------------------------ */
          >^D

      a)  Now we can generate an ANSI prototype-file 'math1.ans' by typing:
          % kr2ansi INTEGER math.c > math1.ans

          The resulting file will look like this:
          % cat math1.ans
          extern INTEGER main(int, char **);
          extern void usage(char *);
          extern INTEGER add(char *, char *);
          extern INTEGER sub(char *, char *);
          extern void dummy(void);
          %

      b)  If we want parameters to show in the prototypes, we type:
          % kr2ansi -p INTEGER math.c > math2.ans

          to get:
          %cat math2.ans
          extern INTEGER main(int argc, char *argv[]);
          extern void usage(char *s);
          extern INTEGER add(char *s1, char *s2);
          extern INTEGER sub(char *s1, char *s2);
          extern void dummy(void);
          %

      c)  Alternatively, we can create a file "types.txt"
          % cat >types.txt
          >INTEGER
          >^D
          %

          and use it for generating "math1.ans" and "math2.ans"
          % kr2ansi -r types.txt math.c > math1.ans
          % kr2ansi -p -r types.txt math.c > math2.ans


 SSEEEE AALLSSOO 
      sh(1),  cat(1) 


 VVEERRSSIIOONN 
      kr2ansi ver 1.0  9/27/1991 



 Tue October 1 16:24 1991                                                Page 4


 KR2ANSI (1)               Commands and Applications                KR2ANSI (1)




AAUUTTHHOORR 
     Harry Karayiannis

     INTERnet:
         harryk@bucsf.bu.edu   (OR nicos@bucsf.bu.edu)
     BITnet:
         cscrzcc@buacca.bu.edu (OR cscrzcc@buacca.BITNET)
     Post:
         15 North Beacon, #316
         Allston, MA 02134
         U.S.A.









































 Tue October 1 16:24 1991                                                Page 5

