home *** CD-ROM | disk | FTP | other *** search
- /* genfpt.c
- * Generieren von 'C'-Funktionen-Prototypen
- * Version 1.0
- * November 89
- * Josef Konrad
- * Comp. QC 2.0
- * Aufruf: genfpt datei.c [datei.c ...]
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdlib.h>
-
- #define MAXLEN 255 /* Laenge einer Zeile */
- #define FALSE 0
- #define TRUE 1
-
- #define READLINE if(!GetLine()) \
- { printf("***Unerwartetes EOF***"); return; }
-
- void main (int argc, char *argv[]);
- void SearchFunction (FILE *infile);
- int CheckFunction (void);
- void HandleParams (void);
- int GetPara (void);
- int GetTyp (void);
- int ReadLine (void);
- int GetLine (void);
- void SkipBraces (void);
- void SkipComment (void);
- void SkipQuot (void);
- void CompressStr (char *pString);
- int CheckIntTyp (char *pString);
-
- char cFunktDefBuf[MAXLEN],cLine[MAXLEN],cParaBuf[MAXLEN],
- cFunktBuf[MAXLEN];
- char cTmpBuf[MAXLEN];
- char *pTmpBuf, *pStrPos;
- char *pPara[100], *pActTyp;
- char *pLine;
- FILE *infile, *outfile;
-
- /*--------------------------------------------------------*/
-
- void main (int argc, char *argv[])
- {
- char *pPos;
-
- if (argc < 2)
- {
- puts ("Falsche Anzahl Argumente");
- exit (1);
- }
- while (--argc)
- {
- if ((infile = fopen (*++argv, "rt")) == NULL)
- {
- perror (*argv);
- exit (1);
- }
- pPos = strchr (*argv, '.');
- if (!pPos)
- {
- puts ("Falscher Dateiname");
- exit (1);
- }
- strncpy (cTmpBuf, *argv, pPos - *argv);
- cTmpBuf[pPos - *argv] = '\0';
- strcat (cTmpBuf, ".fpt");
- if ((outfile = fopen ( cTmpBuf, "w")) == NULL)
- {
- perror (cTmpBuf);
- exit (1);
- }
- printf("\nDatei: %s\n", *argv);
- SearchFunction (infile);
- fclose (infile);
- }
- }
-
- /*-----------------------------------------------------*/
- /* lesen aus Datei und auf Funktionsdefinition pruefen */
-
- void SearchFunction (FILE *infile)
- {
- while (!feof (infile))
- {
- if (!GetLine ())
- break;
- if (strchr (cLine, '{'))
- /* Ueberlesen z.B. Strukturen */
- if (!strchr (cLine, '}'))
- {
- SkipBraces ();
- continue;
- }
- else
- continue; /* Einzeilige Strukturen etc. */
- if (strchr (cLine,'(') && !strchr (cLine,';'))
- if (CheckFunction ())
- {
- cParaBuf[0] = '\0';
- if (!strchr (cFunktDefBuf, '{'))
- /* Funkt. Rumpf */
- { /* Suche Beginn Funktionsrumpf */
- READLINE;
- while (!strchr (cLine, '{'))
- {
- strcat (cParaBuf, cLine);
- /* Parameterdef. */
- READLINE;
- }
- }
- HandleParams ();
- if (!strchr (cLine, '}'))
- SkipBraces ();
- /* Funktionsrumpf ueberlesen */
- }
- } /* while */
- }
-
- /*--------------------------------------------------------*/
- /* Pruefen, ob Funktionsdefinition */
-
- CheckFunction (void)
- {
- int iParenCnt;
-
- if (cLine[0] == '#') /* Preprozessor-Anweisung */
- return (0);
- iParenCnt = 1;
- pLine = strchr (cLine, '(');
- pLine++;
- cFunktDefBuf[0] = '\0';
- while (iParenCnt) /* Solange Klammern offen sind */
- {
- while (*pLine != '\n')
- {
- if (*pLine == '(')
- iParenCnt++;
- else
- if (*pLine == ')')
- iParenCnt--;
- pLine++;
- }
- strcat (cFunktDefBuf, cLine);
- if (iParenCnt)
- READLINE;
- }
- if (strchr (cLine, ';')) /* Noch ';' gefunden ? */
- return 0;
- return 1; /* Funktionsdefinition */
- }
-
- /*--------------------------------------------------------*/
- /* pruefen, ob Parametertypen in () angegeben sind, falls
- nicht, werden sie erzeugt, ebenso wird 'void' bei ()
- erzeugt und 'int', wenn kein
- Typ fuer den Rueckgabewert angegeben ist */
-
- void HandleParams (void)
- {
- int iParaCnt, Cnt, fFound;
- char *pPos1, *pPos2, fVoid;
-
- fVoid = TRUE;
- pPos1 = pPos2 = strchr (cFunktDefBuf, '(');
- while (*++pPos1 != ')')
- if (!isspace (*pPos1++))
- {
- fVoid = FALSE; /* Parameter in Klammern */
- break;
- }
- if (fVoid) /* Keine Parameter, kein 'void' */
- {
- strncpy (cFunktBuf,cFunktDefBuf,pPos2
- - cFunktDefBuf + 1);
- pPos1 = strchr (cFunktBuf, '(');
- *++pPos1 = '\0';
- strcat (cFunktBuf, "void);");
- CompressStr (cFunktBuf);
- if (CheckIntTyp (cFunktBuf))
- {
- strcpy (cFunktDefBuf, "int ");
- strcat (cFunktDefBuf, cFunktBuf);
- fprintf (outfile, "%s\n", cFunktDefBuf);
- printf ("%s\n", cFunktDefBuf);
- }
- else
- {
- fprintf (outfile, "%s\n", cFunktBuf);
- printf ("%s\n", cFunktBuf);
- }
- return;
- }
- if (cParaBuf[0] == '\0')
- /* Typen sind in () angegeben */
- {
- pPos1 = strrchr (cFunktDefBuf, ')');
- if (pPos1)
- *++pPos1 = '\0';
- strcat (cFunktDefBuf, ";");
- CompressStr (cFunktDefBuf);
- if (CheckIntTyp (cFunktDefBuf))
- {
- strcpy (cFunktBuf, "int ");
- strcat (cFunktBuf, cFunktDefBuf);
- fprintf (outfile, "%s\n", cFunktBuf);
- printf ("%s\n", cFunktBuf);
- }
- else
- {
- fprintf (outfile, "%s\n", cFunktDefBuf);
- printf ("%s\n", cFunktDefBuf);
- }
- return;
- }
- strrev (cParaBuf);
- pStrPos = strchr (cParaBuf, ';');
- /* ersten ';' ueberspr.*/
- pStrPos++;
- cFunktBuf[0] = '\0';
- do /* Aufbauen Parameter und Typ Zeile */
- {
- iParaCnt = GetPara ();
- fFound = GetTyp ();
- for (Cnt = 0; Cnt < iParaCnt; Cnt++)
- {
- strcat (cFunktBuf, pPara[Cnt]);
- strcat (cFunktBuf, " ");
- strcat (cFunktBuf, pActTyp);
- strcat (cFunktBuf, ",");
- free (pPara[Cnt]);
- }
- }
- while (fFound);
- strrev (cFunktBuf);
- *++pPos2 = '\0';
- /* Funktionsdef. bis einschl. '(' */
- strcat (cFunktDefBuf, &cFunktBuf[1]);
- /* [0] steht ',' */
- strcat (cFunktDefBuf, ");");
- CompressStr (cFunktDefBuf);
- if (CheckIntTyp (cFunktDefBuf))
- {
- strcpy (cFunktBuf, "int ");
- strcat (cFunktBuf, cFunktDefBuf);
- fprintf (outfile, "%s\n", cFunktBuf);
- printf ("%s\n", cFunktBuf);
- }
- else
- {
- fprintf (outfile, "%s\n", cFunktDefBuf);
- printf ("%s\n", cFunktDefBuf);
- }
- }
-
- /*--------------------------------------------------------*/
- /* Holt Parametergruppe aus der Parameterzeile
- und setzt array von Zeigern auf die Parameter */
-
- int GetPara (void)
- {
- int iParaCnt;
-
- iParaCnt = 0;
- do
- {
- pTmpBuf = cTmpBuf;
- while (isspace (*pStrPos) || *pStrPos == ',')
- pStrPos++;
- while (*pStrPos != ',' && !isspace (*pStrPos))
- *pTmpBuf++ = *pStrPos++;
- *pTmpBuf = '\0';
- pPara[iParaCnt] = strdup (cTmpBuf);
- /* in Parameterliste */
- iParaCnt++;
- while (isspace (*pStrPos))
- pStrPos++;
- }
- while (*pStrPos == ',');
- return (iParaCnt); /* Anzahl der Parameter */
- }
-
- /*--------------------------------------------------------*/
- /* Holt Datentyp einer Parametergr.,setzt Zeiger auf Typ */
-
- int GetTyp (void)
- {
- pTmpBuf = cTmpBuf;
- while (*pStrPos != ';' && *pStrPos) /* Ende Typ,String*/
- if (*pStrPos != '\n')
- *pTmpBuf++ = *pStrPos++;
- else
- pStrPos++;
- *pTmpBuf = '\0';
- pActTyp = cTmpBuf; /* Zeiger setzen */
- if (*pStrPos) /* Noch nicht am String-Ende ? */
- {
- pStrPos++; /* ';' ueberspringen */
- return TRUE;
- }
- else
- return FALSE; /* Kennzeichnet String-Ende */
- }
-
- /*--------------------------------------------------------*/
-
- ReadLine (void)
- {
- if (fgets (cTmpBuf, MAXLEN, infile) == NULL)
- return FALSE;
- pTmpBuf = cTmpBuf;
- return TRUE;
- }
-
- /*--------------------------------------------------------*/
- /* Einlesen einer Zeile, wobei Kommentare und Text innerhalb
- von einfachen oder doppelten Anfuehrungszeichen
- uebersprungen werden */
-
- GetLine (void)
- {
- do
- {
- if (!ReadLine())
- return FALSE;
- pLine = cLine;
- do
- {
- if (*pTmpBuf == '\'' || *pTmpBuf == '"')
- SkipQuot ();
- else
- if (*pTmpBuf == '/' && *(pTmpBuf+1) == '*')
- SkipComment ();
- else
- *pLine++ = *pTmpBuf++;
- }
- while (*pTmpBuf != '\n'&& *pTmpBuf);
- *pLine++ = *pTmpBuf++;
- *pLine = '\0';
- pLine = cLine;
- }
- while (cLine[0] == '\n'); /* keine leeren Zeilen */
- return TRUE;
- }
-
- /*-------------------------------------------------------*/
- /* Ueberlesen Code zwischen '{' und '}' */
-
- void SkipBraces (void)
- {
- int iBraceCnt = 1;
-
- while (iBraceCnt)
- {
- if (!GetLine ()) return;
- while (*pLine != '\n')
- {
- if (*pLine == '{')
- iBraceCnt++;
- else
- if (*pLine == '}')
- iBraceCnt--;
- pLine++;
- }
- }
- }
-
- /*--------------------------------------------------------*/
-
- void SkipComment (void)
- {
- while (!(*pTmpBuf++ == '*' && *pTmpBuf == '/' ))
- if (*pTmpBuf == '\n')
- if (!ReadLine ()) return;
- pTmpBuf++;
- }
-
- /*--------------------------------------------------------*/
-
- void SkipQuot (void)
- {
- char quotchar;
-
- quotchar = *pTmpBuf;
- do
- {
- pTmpBuf++;
- if (*pTmpBuf == '\n')
- if (!ReadLine ()) return;
- if (*pTmpBuf == '\\' && *(pTmpBuf-1) != '\\')
- if (*++pTmpBuf == quotchar)
- pTmpBuf++;
- }
- while (*pTmpBuf != quotchar && *pTmpBuf);
- pTmpBuf++;
- }
-
- /*--------------------------------------------------------*/
- /* Entfernt aus String fuehrende und nachfolgende Leer-
- zeichen und reduziert den Abstand zwischen Worten auf
- jeweils 1 Leerzeichen.
- Kopiert dann den komprimierten String nach pString */
-
- void CompressStr (char *pString)
- {
- char *pTmp;
- pTmpBuf = cTmpBuf;
- pTmp = pString;
- while (*pString)
- {
- while (isspace (*pString) && *pString)
- pString++;
- while (!isspace (*pString) && *pString)
- *pTmpBuf++ = *pString++;
- *pTmpBuf++ = ' ';
- while (isspace (*pString) && *pString)
- pString++;
- }
- while (*pTmpBuf != ' ')
- pTmpBuf--; /* Letzte Blank entfernen */
- *pTmpBuf = '\0';
- strcpy (pTmp, cTmpBuf);
- }
-
- /*--------------------------------------------------------*/
- /* Pruefen, ob Ergebnistyp angegeben ist */
-
- CheckIntTyp (char *pString)
- {
- char *pTmp;
-
- pTmp = strchr (pString, '(');
- if (!pTmp)
- return FALSE; /* Fuer Fehlerfall */
- pTmp--; /* Letztes Zeichen Funktionsname */
- while (isspace (*pTmp))
- pTmp--;
- while (iscsym (*pTmp))
- if (pTmp-- == pString)
- return TRUE; /* Kein Typ angegeben -> int */
- while (!iscsym (*pTmp))
- if (pTmp-- == pString)
- return TRUE;
- /* Kein Typ angegeben --> int */
- return FALSE;
- }
-
-