home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / tools / uahelper.c < prev   
C/C++ Source or Header  |  1998-09-16  |  7KB  |  250 lines

  1. /*
  2.  * Program:    unANSIify
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        4545 15th Ave NE
  9.  *        Seattle, WA  98105-4527
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    1 June 1995
  13.  * Last Edited:    8 June 1996
  14.  *
  15.  * Copyright 1997 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. /* This program is designed to make traditional C sources out of my source
  38.  * files which use ANSI syntax.  This program depends heavily upon knowledge
  39.  * of the way I write code, and is not a general purpose tool.  I hope that
  40.  * someday someone will provide a general purpose tool...
  41.  */
  42.  
  43. #include <stdio.h>
  44.  
  45. #define LINELENGTH 1000
  46.  
  47.  
  48. /* Find first non-whitespace
  49.  * Accepts: string
  50.  * Returns: 0 if no non-whitespace found, or pointer to non-whitespace
  51.  */
  52.  
  53. char *fndnws (s)
  54.      char *s;
  55. {
  56.   while ((*s == ' ') || (*s == '\t'))
  57.     if ((*s++ == '\n') || !*s) return (char *) 0;
  58.   return s;
  59. }
  60.  
  61.  
  62. /* Find first whitespace
  63.  * Accepts: string
  64.  * Returns: 0 if no whitespace found, or pointer to whitespace
  65.  */
  66.  
  67. char *fndws (s)
  68.      char *s;
  69. {
  70.   while ((*s != ' ') && (*s != '\t'))
  71.     if ((*s++ == '\n') || !*s) return (char *) 0;
  72.   return s;
  73. }
  74.  
  75.  
  76. /* Find end of commend
  77.  * Accepts: string
  78.  * Returns: -1 if end of comment found, else 0
  79.  */
  80.  
  81. int fndcmt (s)
  82.      char *s;
  83. {
  84.   while (*s && (*s != '\n'))
  85.     if ((*s++ == '*') && (*s == '/')) return -1;
  86.   return 0;
  87. }
  88.  
  89. /* Output a line
  90.  * Accepts: string
  91.  */
  92.  
  93. void poot (s)
  94.      char *s;
  95. {
  96.   if (s) fputs (s,stdout);
  97. }
  98.  
  99.  
  100. /* Skip prefix
  101.  * Accepts: string
  102.  * Returns: updated string
  103.  */
  104.  
  105. char *skipfx (s)
  106.      char *s;
  107. {
  108.   char c,*t = s,*tt;
  109.                 /* skip leading whitespace too */
  110.   while ((*s == ' ') || (*s == '\t')) *s++;
  111.   if (s != t) {
  112.     c = *s;            /* save character */
  113.     *s = '\0';            /* tie off prefix */
  114.     poot (t);            /* output prefix */
  115.     *s = c;            /* restore character */
  116.   }
  117.                 /* a typedef? */
  118.   if (((s[0] == 't') && (s[1] == 'y') && (s[2] == 'p') && (s[3] == 'e') &&
  119.        (s[4] == 'd') && (s[5] == 'e') && (s[6] == 'f')) &&
  120.       (t = fndws (s)) && (t = fndnws (t))) {
  121.     if ((t[0] == 'u') && (t[1] == 'n') && (t[2] == 's') && (t[3] == 'i') &&
  122.     (t[4] == 'g') && (t[5] == 'n') && (t[6] == 'e') && (t[7] == 'd') &&
  123.     (tt = fndws (t+7)) && (tt = fndnws (tt))) t = tt;
  124.     c = *t;            /* save character */
  125.     *t = '\0';            /* tie off prefix */
  126.     poot (s);            /* output prefix */
  127.     *t = c;            /* restore character */
  128.     s = t;            /* new string pointer */
  129.   }
  130.                 /* one of the known prefixes? */
  131.   else if ((((s[0] == 'u') && (s[1] == 'n') && (s[2] == 's') &&
  132.          (s[3] == 'i') && (s[4] == 'g') && (s[5] == 'n') &&
  133.          (s[6] == 'e') && (s[7] == 'd')) ||
  134.         ((s[0] == 's') && (s[1] == 't') && (s[2] == 'r') &&
  135.          (s[3] == 'u') && (s[4] == 'c') && (s[5] == 't')) ||
  136.         ((s[0] == 's') && (s[1] == 't') && (s[2] == 'a') &&
  137.          (s[3] == 't') && (s[4] == 'i') && (s[5] == 'c')) ||
  138.         ((s[0] == 'd') && (s[1] == 'o')) ||
  139.         ((s[0] == 'e') && (s[1] == 'l') && (s[2] == 's') &&
  140.          (s[3] == 'e'))) &&
  141.        (t = fndws (s)) && (t = fndnws (t))) {
  142.     c = *t;            /* save character */
  143.     *t = '\0';            /* tie off prefix */
  144.     poot (s);            /* output prefix */
  145.     *t = c;            /* restore character */
  146.     s = t;            /* new string pointer */
  147.   }
  148.                 /* may look like a proto, but isn't */
  149.   else if ((s[0] == 'r') && (s[1] == 'e') && (s[2] == 't') && (s[3] == 'u') &&
  150.        (s[4] == 'r') && (s[5] == 'n')) {
  151.     poot (s);
  152.     return 0;
  153.   }
  154.   return s;
  155. }
  156.  
  157. /* UnANSI a line
  158.  * Accepts: string
  159.  */
  160.  
  161. void unansi (s)
  162.      char *s;
  163. {
  164.   char c,*t = s,*u,*v;
  165.   while (t[1] && (t[1] != '\n')) t++;
  166.   switch (*t) {
  167.   case ',':            /* continued on next line? */
  168.                 /* slurp remainder of line */
  169.     fgets (t + 1,LINELENGTH - (t + 1 - s),stdin);
  170.     unansi (s);            /* try again */
  171.     break;
  172.   case ';':            /* function prototype? */
  173.                 /* yes, tie it off */
  174.     *(fndws (fndnws (fndws (fndnws (s))))) = '\0';
  175.     printf ("%s ();\n",s);    /* and output non-ANSI form */
  176.     break;
  177.   case ')':
  178.     *t = '\0';            /* tie off args */
  179.     if (*(t = fndnws (fndws (fndnws (fndws (fndnws (s)))))) == '(') {
  180.       *t++ = '\0';        /* tie off */
  181.       while ((*t == ' ') || (*t == '\t')) t++;
  182.       if ((t[0] == 'v') && (t[1] == 'o') && (t[2] == 'i') && (t[3] == 'd') &&
  183.       !t[4]) *t = '\0';    /* make void be same as null */
  184.       printf ("%s(",s);        /* output start of function */
  185.       s = t;
  186.       while (*s) {        /* for each argument */
  187.     while ((*s == ' ') || (*s == '\t')) s++;
  188.     for (u = v = s; (*u && (*u != ',') && (*u != '[')); u++)
  189.       if ((*u == ' ') || (*u == '\t')) v = u;
  190.     c = *u;            /* remember delimiter */
  191.     *u = '\0';        /* tie off argument name */
  192.     while (*++v == '*');    /* remove leading pointer indication */
  193.     fputs (v,stdout);    /* write variable name */
  194.     *(s = u) = c;        /* restore delimiter */
  195.     while (*s && (*s != ',')) *s++;
  196.     if (*s) fputc (*s++,stdout);
  197.       }
  198.       puts (")");        /* end of function */
  199.       while (*t) {        /* for each argument */
  200.     fputs ("     ",stdout);
  201.     while ((*t == ' ') || (*t == '\t')) t++;
  202.     while (*t && (*t != ',')) fputc (*t++,stdout);
  203.     puts (";");
  204.     if (*t == ',') t++;
  205.       }
  206.     }
  207.     else printf ("%s)",s);    /* say what?? */
  208.     break;
  209.   default:            /* doesn't look like a function */
  210.     poot (s);
  211.   }
  212. }
  213.  
  214. main ()
  215. {
  216.   char *s,*t,line[LINELENGTH];
  217.   int c;
  218.   while (s = fgets (line,LINELENGTH,stdin)) switch (line[0]) {
  219.   case '/':            /* comment */
  220.     if ((s[1] != '*') || fndcmt (s+2)) poot (line);
  221.     else do poot (line);
  222.     while (!fndcmt (line) && (s = fgets (line,LINELENGTH,stdin)));
  223.     break;
  224.   case '{':            /* open function */
  225.   case '}':            /* close function */
  226.   case '\f':            /* formfeed */
  227.   case '\n':            /* newline */
  228.   case '#':            /* preprocessor command */
  229.     poot (line);
  230.     break;
  231.   case '\t':            /* whitespace */
  232.   case ' ':
  233.                 /* look like function arg def in structure? */
  234.     if ((t = skipfx (line)) && (s = fndws (t)) && (s = fndnws (s)) &&
  235.     (((*s == '(') && (s[1] == '*')) ||
  236.      ((*s == '*') && (s[1] == '(') && (s[2] == '*'))) &&
  237.     (s = fndws (s)) && (s[-1] == ')') && (s = fndnws (s)) && (*s == '('))
  238.       unansi (t);
  239.     else poot (t);
  240.     break;
  241.   default:            /* begins with anything else */
  242.                 /* look like function proto or def? */
  243.     if ((t = skipfx (line)) && (s = fndws (t)) && (s = fndnws (s)) &&
  244.     (s = fndws (s)) && (s = fndnws (s)) && (*s == '('))
  245.       unansi (t);
  246.     else poot (t);
  247.     break;
  248.   }
  249. }
  250.