#include <ctype.h>
#include <stdio.h>
#include "asn-incl.h"
#include "asn1module.h"
#include "mem.h"
#include "snacc-util.h"
#include "define.h"
#include "str-util.h"
#include "rules.h"
#include "type-info.h"
extern Module *usefulTypeModG;
static DefinedObj *definedNamesG;
void FillCTypeDefInfo PROTO ((CRules *r, Module *m, TypeDef *td));
static void FillCFieldNames PROTO ((CRules *r, NamedTypeList *firstSibling));
static void FillCTypeRefInfo PROTO ((CRules *r, Module *m, TypeDef *head, Type *t, CTypeId parentTypeId));
static void FillCStructElmts PROTO ((CRules *r, Module *m, TypeDef *head, NamedTypeList *t));
static void FillCChoiceElmts PROTO ((CRules *r, Module *m, TypeDef *head, NamedTypeList *first));
static int IsCPtr PROTO ((CRules *r, TypeDef *td, Type *t, CTypeId parentTypeId));
void ParseTypeDefAttribs PROTO ((CTDI *ctdi, AttributeList *attrList));
void ParseTypeRefAttribs PROTO ((CTRI *ctri, AttributeList *attrList));
void ParseAttr PROTO ((char *str, int *startLoc, char **attrName, char **attrValue));
int ParseBool PROTO ((char *str, int *result));
int ParseInt PROTO ((char *str, int *result));
int ParseCTypeId PROTO ((char *str, int *result));
void FillCTDIDefaults PROTO ((CRules *r, CTDI *ctdi, TypeDef *td));
void
FillCTypeInfo PARAMS ((r, modList),
CRules *r _AND_
ModuleList *modList)
{
TypeDef *td;
Module *m;
definedNamesG = NewObjList();
if (usefulTypeModG != NULL)
{
FOR_EACH_LIST_ELMT (td, usefulTypeModG->typeDefs)
FillCTypeDefInfo (r, usefulTypeModG, td);
}
FOR_EACH_LIST_ELMT (m, modList)
{
FOR_EACH_LIST_ELMT (td, m->typeDefs)
FillCTypeDefInfo (r, m, td);
}
if (usefulTypeModG != NULL)
{
FOR_EACH_LIST_ELMT (td, usefulTypeModG->typeDefs)
FillCTypeRefInfo (r, usefulTypeModG, td, td->type, C_TYPEDEF);
}
FOR_EACH_LIST_ELMT (m, modList)
{
FOR_EACH_LIST_ELMT (td, m->typeDefs)
FillCTypeRefInfo (r, m, td, td->type, C_TYPEDEF);
}
FreeDefinedObjs (&definedNamesG);
}
void
FillCTypeDefInfo PARAMS ((r, m, td),
CRules *r _AND_
Module *m _AND_
TypeDef *td)
{
int digit;
int len;
char *tmpName;
CTDI *ctdi;
if (td->cTypeDefInfo != NULL)
return;
ctdi = td->cTypeDefInfo = MT (CTDI);
ctdi->cTypeId = C_TYPEDEF;
FillCTDIDefaults (r, ctdi, td);
if ((td->type->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
(td->type->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
{
FillCTypeDefInfo (r, td->type->basicType->a.localTypeRef->module, td->type->basicType->a.localTypeRef->link);
memcpy (ctdi, td->type->basicType->a.localTypeRef->link->cTypeDefInfo, sizeof (CTDI));
}
ctdi->cTypeName = NULL;
ctdi->printRoutineName = NULL;
ctdi->encodeRoutineName = NULL;
ctdi->decodeRoutineName = NULL;
ctdi->freeRoutineName = NULL;
ParseTypeDefAttribs (ctdi, td->attrList);
if (ctdi->cTypeName == NULL)
{
tmpName = Asn1TypeName2CTypeName (td->definedName);
len = strlen (tmpName);
ctdi->cTypeName = Malloc (len + r->maxDigitsToAppend + 1);
strcpy (ctdi->cTypeName, tmpName);
Free (tmpName);
MakeCStrUnique (definedNamesG, ctdi->cTypeName,r->maxDigitsToAppend, 1);
DefineObj (&definedNamesG, ctdi->cTypeName);
}
if (ctdi->encodeRoutineName == NULL)
{
ctdi->encodeRoutineName =
Malloc (strlen (ctdi->cTypeName) + strlen (r->encodeRoutineBaseName)
+ 1);
strcpy (ctdi->encodeRoutineName, r->encodeRoutineBaseName);
strcat (ctdi->encodeRoutineName, ctdi->cTypeName);
}
if (ctdi->decodeRoutineName == NULL)
{
ctdi->decodeRoutineName =
Malloc (strlen (ctdi->cTypeName) + strlen (r->decodeRoutineBaseName) + 1);
strcpy (ctdi->decodeRoutineName, r->decodeRoutineBaseName);
strcat (ctdi->decodeRoutineName, ctdi->cTypeName);
}
if (ctdi->printRoutineName == NULL)
{
ctdi->printRoutineName =
Malloc (strlen (ctdi->cTypeName) + strlen (r->printRoutineBaseName) + 1);
strcpy (ctdi->printRoutineName, r->printRoutineBaseName);
strcat (ctdi->printRoutineName, ctdi->cTypeName);
}
if (ctdi->freeRoutineName == NULL)
{
ctdi->freeRoutineName =
Malloc (strlen (ctdi->cTypeName) + strlen (r->freeRoutineBaseName) + 1);
strcpy (ctdi->freeRoutineName, r->freeRoutineBaseName);
strcat (ctdi->freeRoutineName, ctdi->cTypeName);
}
}
static void
FillCTypeRefInfo PARAMS ((r, m, head, t, parentTypeId),
CRules *r _AND_
Module *m _AND_
TypeDef *head _AND_
Type *t _AND_
CTypeId parentTypeId)
{
char *typeStr;
CTRI *ctri;
CTDI *tmpCtdi;
ValueDef *namedElmt;
CNamedElmt *cne;
CNamedElmt **cneHndl;
char *elmtName;
char *listName;
char *choiceName;
char *unionName;
Type *tmpT;
int len, digit;
enum BasicTypeChoiceId basicTypeId;
if (t->cTypeRefInfo == NULL)
{
ctri = MT (CTRI);
t->cTypeRefInfo = ctri;
}
else
ctri = t->cTypeRefInfo;
basicTypeId = t->basicType->choiceId;
tmpCtdi = &r->typeConvTbl[basicTypeId];
ctri->cTypeId = tmpCtdi->cTypeId;
ctri->cTypeName = tmpCtdi->cTypeName;
ctri->optTestRoutineName = tmpCtdi->optTestRoutineName;
ctri->printRoutineName = tmpCtdi->printRoutineName;
ctri->encodeRoutineName = tmpCtdi->encodeRoutineName;
ctri->decodeRoutineName = tmpCtdi->decodeRoutineName;
ctri->freeRoutineName = tmpCtdi->freeRoutineName;
ctri->isEncDec = tmpCtdi->isEncDec;
if (ctri->cTypeId == C_ANY)
{
fprintf (stderr,"Warning - generated code for the \"ANY\" type in type \"%s\" will need modification by YOU.", head->definedName);
fprintf (stderr," The source files will have a \"/* ANY - Fix Me! */\" comment before related code.\n\n");
}
if ((basicTypeId == BASICTYPE_INTEGER || basicTypeId == BASICTYPE_ENUMERATED || basicTypeId == BASICTYPE_BITSTRING) && !(LIST_EMPTY (t->basicType->a.integer)))
{
ctri->cNamedElmts = AsnListNew (sizeof (void *));
FOR_EACH_LIST_ELMT (namedElmt, t->basicType->a.integer)
{
cneHndl = (CNamedElmt **)AsnListAppend (ctri->cNamedElmts);
cne = *cneHndl = MT (CNamedElmt);
elmtName = Asn1ValueName2CValueName (namedElmt->definedName);
len = strlen (elmtName);
cne->name = Malloc (len + 1 + r->maxDigitsToAppend);
strcpy (cne->name, elmtName);
Free (elmtName);
if (namedElmt->value->basicValue->choiceId == BASICVALUE_INTEGER)
cne->value = namedElmt->value->basicValue->a.integer;
else
{
fprintf (stderr,"Warning: unlinked defined value using -9999999\n");
cne->value = -9999999;
}
if (r->capitalizeNamedElmts)
Str2UCase (cne->name, len);
MakeCStrUnique (definedNamesG, cne->name, r->maxDigitsToAppend, 1);
DefineObj (&definedNamesG, cne->name);
}
}
if (r->typeConvTbl[basicTypeId].cTypeId == C_TYPEREF)
{
if (t->basicType->a.localTypeRef->link != NULL)
{
tmpCtdi= t->basicType->a.localTypeRef->link->cTypeDefInfo;
ctri->cTypeName = tmpCtdi->cTypeName;
ctri->printRoutineName = tmpCtdi->printRoutineName;
ctri->encodeRoutineName = tmpCtdi->encodeRoutineName;
ctri->decodeRoutineName = tmpCtdi->decodeRoutineName;
ctri->freeRoutineName = tmpCtdi->freeRoutineName;
ctri->isEncDec = tmpCtdi->isEncDec;
ctri->optTestRoutineName = tmpCtdi->optTestRoutineName;
}
else
{
fprintf (stderr,"Assuming C Type and Routine names for unresolved type ref \"%s\"\n",t->basicType->a.localTypeRef->typeName);
ctri->cTypeName = Asn1TypeName2CTypeName (t->basicType->a.localTypeRef->typeName);
ctri->printRoutineName = Malloc (strlen (r->printRoutineBaseName) + strlen (ctri->cTypeName) + 1);
strcpy (ctri->printRoutineName, r->printRoutineBaseName);
strcat (ctri->printRoutineName, ctri->cTypeName);
ctri->encodeRoutineName = Malloc (strlen (r->encodeRoutineBaseName)+ strlen (ctri->cTypeName) + 1);
strcpy (ctri->encodeRoutineName, r->encodeRoutineBaseName);
strcat (ctri->encodeRoutineName, ctri->cTypeName);
ctri->decodeRoutineName = Malloc (strlen (r->decodeRoutineBaseName)+ strlen (ctri->cTypeName) + 1);
strcpy (ctri->decodeRoutineName, r->decodeRoutineBaseName);
strcat (ctri->decodeRoutineName, ctri->cTypeName);
ctri->freeRoutineName = Malloc (strlen (ctri->cTypeName) + strlen (r->freeRoutineBaseName) + 1);
strcpy (ctri->freeRoutineName, r->freeRoutineBaseName);
strcat (ctri->freeRoutineName, ctri->cTypeName);
}
}
else if (r->typeConvTbl[basicTypeId].cTypeId == C_LIST)
{
FillCTypeRefInfo (r, m, head, t->basicType->a.setOf, C_LIST);
}
else if (r->typeConvTbl[basicTypeId].cTypeId == C_CHOICE)
{
len = strlen (head->cTypeDefInfo->cTypeName);
unionName = (char*) Malloc (len + strlen (r->choiceUnionDefSuffix) + r->maxDigitsToAppend + 1);
strcpy (unionName, head->cTypeDefInfo->cTypeName);
strcat (unionName, r->choiceUnionDefSuffix);
MakeCStrUnique (definedNamesG, unionName, r->maxDigitsToAppend, 1);
DefineObj (&definedNamesG, unionName);
ctri->cTypeName = unionName;
ctri->choiceIdEnumName = Malloc (len + strlen (r->choiceIdEnumSuffix) + r->maxDigitsToAppend + 1);
strcpy (ctri->choiceIdEnumName, head->cTypeDefInfo->cTypeName);
strcat (ctri->choiceIdEnumName, r->choiceIdEnumSuffix);
MakeCStrUnique (definedNamesG, ctri->choiceIdEnumName, r->maxDigitsToAppend, 1);
DefineObj (&definedNamesG, ctri->choiceIdEnumName);
ctri->choiceIdEnumFieldName = r->choiceIdFieldName;
ctri->cFieldName = r->choiceUnionFieldName;
FillCFieldNames (r, t->basicType->a.choice);
FillCChoiceElmts (r, m, head, t->basicType->a.choice);
}
else if (r->typeConvTbl[basicTypeId].cTypeId == C_STRUCT)
{
unionName = Malloc (strlen (head->cTypeDefInfo->cTypeName) +1);
strcpy (unionName, head->cTypeDefInfo->cTypeName);
ctri->cTypeName = unionName;
FillCStructElmts (r, m, head, t->basicType->a.set);
FillCFieldNames (r, t->basicType->a.set);
}
ctri->isPtr = IsCPtr (r, head, t, parentTypeId);
ParseTypeRefAttribs (ctri, t->attrList);
}
static void
FillCStructElmts PARAMS ((r, m, head, elmts),
CRules *r _AND_
Module *m _AND_
TypeDef *head _AND_
NamedTypeList *elmts)
{
NamedType *et;
FOR_EACH_LIST_ELMT (et, elmts)
{
FillCTypeRefInfo (r, m, head, et->type, C_STRUCT);
}
}
static void
FillCChoiceElmts PARAMS ((r, m, head, elmts),
CRules *r _AND_
Module *m _AND_
TypeDef *head _AND_
NamedTypeList *elmts)
{
NamedType *et;
int idCount = 0;
CTRI *ctri;
char *firstName;
char *secondName;
int len;
FOR_EACH_LIST_ELMT (et, elmts)
FillCTypeRefInfo (r, m, head, et->type, C_CHOICE);
FOR_EACH_LIST_ELMT (et, elmts)
{
ctri = et->type->cTypeRefInfo;
if (ctri == NULL)
continue;
ctri->choiceIdValue = idCount++;
firstName = Asn1TypeName2CTypeName (head->cTypeDefInfo->cTypeName);
secondName = ctri->cFieldName;
ctri->choiceIdSymbol = Malloc (strlen (firstName) + strlen (secondName) + 2 + r->maxDigitsToAppend);
strcpy (ctri->choiceIdSymbol, firstName);
strcat (ctri->choiceIdSymbol, "_");
strcat (ctri->choiceIdSymbol, secondName);
Free (firstName);
len = strlen (ctri->choiceIdSymbol);
if (r->capitalizeNamedElmts)
Str2UCase (ctri->choiceIdSymbol, len);
MakeCStrUnique (definedNamesG, ctri->choiceIdSymbol, r->maxDigitsToAppend, 0);
DefineObj (&definedNamesG, ctri->choiceIdSymbol);
}
}
static void
FillCFieldNames PARAMS ((r, elmts),
CRules *r _AND_
NamedTypeList *elmts)
{
NamedType *et;
CTRI *ctri;
DefinedObj *fieldNames;
int len, num, digit, i, tmpLen;
char *tmpName;
char *asn1FieldName;
char *cFieldName;
fieldNames = NewObjList();
FOR_EACH_LIST_ELMT (et, elmts)
{
ctri = et->type->cTypeRefInfo;
if (ctri == NULL)
{
ctri = MT (CTRI);
et->type->cTypeRefInfo = ctri;
}
if (et->fieldName != NULL)
{
asn1FieldName = et->fieldName;
ctri->cFieldName = Asn1FieldName2CFieldName (asn1FieldName);
DefineObj (&fieldNames, ctri->cFieldName);
}
}
FOR_EACH_LIST_ELMT (et, elmts)
{
ctri = et->type->cTypeRefInfo;
if (ctri->cFieldName == NULL)
{
if ((et->type->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
(et->type->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
{
tmpName = et->type->basicType->a.localTypeRef->link->cTypeDefInfo->cTypeName;
tmpName = Asn1TypeName2CTypeName (tmpName);
cFieldName = Malloc (strlen (tmpName) + r->maxDigitsToAppend +1);
strcpy (cFieldName, tmpName);
Free (tmpName);
if (isupper (cFieldName[0]))
cFieldName[0] = tolower (cFieldName[0]);
}
else
{
tmpName = r->typeConvTbl[et->type->basicType->choiceId].defaultFieldName;
cFieldName = Malloc (strlen (tmpName) + r->maxDigitsToAppend +1);
strcpy (cFieldName, tmpName);
if (isupper (cFieldName[0]))
cFieldName[0] = tolower (cFieldName[0]);
}
MakeCStrUnique (fieldNames, cFieldName, r->maxDigitsToAppend, 1);
DefineObj (&fieldNames, cFieldName);
ctri->cFieldName = cFieldName;
}
}
FreeDefinedObjs (&fieldNames);
}
static int
IsCPtr PARAMS ((r, td, t, parentCTypeId),
CRules *r _AND_
TypeDef *td _AND_
Type *t _AND_
CTypeId parentCTypeId)
{
CTDI *ctdi;
int retVal = FALSE;
if ((t->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
(t->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
{
ctdi = t->basicType->a.localTypeRef->link->cTypeDefInfo;
}
else
ctdi = &r->typeConvTbl[GetBuiltinType (t)];
if ((parentCTypeId == C_TYPEDEF) && (ctdi->isPtrForTypeDef))
retVal = TRUE;
else if ((parentCTypeId == C_STRUCT) && (ctdi->isPtrForTypeRef))
retVal = TRUE;
else if ((parentCTypeId == C_CHOICE) && (ctdi->isPtrInChoice))
retVal = TRUE;
else if (((t->optional) || (t->defaultVal != NULL)) && (ctdi->isPtrForOpt))
retVal = TRUE;
return retVal;
}
#define BAD_VALUE(attrValue, attrType)\
fprintf (stderr,"Warning: ignoring attribute with improper value (%s/%s)\n",attrType, attrValue)
void ParseTypeDefAttribs PARAMS ((ctdi, attrList),
CTDI *ctdi _AND_
AttributeList *attrList)
{
char *attrName;
char *attrValue;
int loc;
MyString attr;
int result;
if (attrList == NULL)
return;
FOR_EACH_LIST_ELMT (attr, attrList)
{
loc = 0;
while (1)
{
ParseAttr (attr, &loc, &attrName, &attrValue);
if (attrName == NULL)
break;
if (strcmp (attrName, "asn1TypeId") == 0)
{
if (ParseTypeId (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->asn1TypeId = result;
Free (attrValue);
}
else if (strcmp (attrName, "cTypeId") == 0)
{
if (ParseCTypeId (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->cTypeId = result;
Free (attrValue);
}
else if (strcmp (attrName, "cTypeName") == 0)
ctdi->cTypeName = attrValue;
else if (strcmp (attrName, "isPdu") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->isPdu = result;
Free (attrValue);
}
else if (strcmp (attrName, "isPtrForTypeDef") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->isPtrForTypeDef = result;
Free (attrValue);
}
else if (strcmp (attrName, "isPtrForTypeRef") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->isPtrForTypeRef = result;
Free (attrValue);
}
else if (strcmp (attrName, "isPtrInChoice") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->isPtrInChoice = result;
Free (attrValue);
}
else if (strcmp (attrName, "isPtrForOpt") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->isPtrForOpt = result;
Free (attrValue);
}
else if (strcmp (attrName, "optTestRoutineName") == 0)
ctdi->optTestRoutineName = attrValue;
else if (strcmp (attrName, "defaultFieldName") == 0)
ctdi->defaultFieldName = attrValue;
else if (strcmp (attrName, "printRoutineName") == 0)
ctdi->printRoutineName = attrValue;
else if (strcmp (attrName, "encodeRoutineName") == 0)
ctdi->encodeRoutineName = attrValue;
else if (strcmp (attrName, "decodeRoutineName") == 0)
ctdi->decodeRoutineName = attrValue;
else if (strcmp (attrName, "freeRoutineName") == 0)
ctdi->freeRoutineName = attrValue;
else if (strcmp (attrName, "isEncDec") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
{
ctdi->isEncDec = result;
}
Free (attrValue);
}
else if (strcmp (attrName, "genTypeDef") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
{
ctdi->genTypeDef = result;
}
Free (attrValue);
}
else if (strcmp (attrName, "genPrintRoutine") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->genPrintRoutine = result;
Free (attrValue);
}
else if (strcmp (attrName, "genEncodeRoutine") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->genEncodeRoutine = result;
Free (attrValue);
}
else if (strcmp (attrName, "genDecodeRoutine") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->genDecodeRoutine = result;
Free (attrValue);
}
else if (strcmp (attrName, "genFreeRoutine") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctdi->genFreeRoutine = result;
Free (attrValue);
}
else
fprintf (stderr,"Warning: ignoring unrecognized type def attribute value pair (%s/%s)\n", attrName, attrValue);
}
}
}
void ParseTypeRefAttribs PARAMS ((ctri, attrList),
CTRI *ctri _AND_
AttributeList *attrList)
{
char *attrName;
char *attrValue;
int loc;
int result;
MyString attr;
if (attrList == NULL)
return;
FOR_EACH_LIST_ELMT (attr, attrList)
{
loc = 0;
while (1)
{
ParseAttr (attr, &loc, &attrName, &attrValue);
if (attrName == NULL)
break;
if (strcmp (attrName, "cTypeId") == 0)
{
if (ParseCTypeId (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctri->cTypeId = result;
Free (attrValue);
}
else if (strcmp (attrName, "cTypeName") == 0)
ctri->cTypeName = attrValue;
else if (strcmp (attrName, "cFieldName") == 0)
ctri->cFieldName = attrValue;
else if (strcmp (attrName, "isPtr") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctri->isPtr = result;
Free (attrValue);
}
else if (strcmp (attrName, "choiceIdValue") == 0)
{
if (ParseInt (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctri->choiceIdValue = result;
Free (attrValue);
}
else if (strcmp (attrName, "choiceIdSymbol") == 0)
ctri->choiceIdSymbol = attrValue;
else if (strcmp (attrName, "choiceIdEnumName") == 0)
ctri->choiceIdEnumName = attrValue;
else if (strcmp (attrName, "choiceIdEnumFieldName") == 0)
ctri->choiceIdEnumFieldName = attrValue;
else if (strcmp (attrName, "optTestRoutineName") == 0)
ctri->optTestRoutineName = attrValue;
else if (strcmp (attrName, "printRoutineName") == 0)
ctri->printRoutineName = attrValue;
else if (strcmp (attrName, "encodeRoutineName") == 0)
ctri->encodeRoutineName = attrValue;
else if (strcmp (attrName, "decodeRoutineName") == 0)
ctri->decodeRoutineName = attrValue;
else if (strcmp (attrName, "isEncDec") == 0)
{
if (ParseBool (attrValue, &result) < 0)
BAD_VALUE (attrValue, attrName);
else
ctri->isEncDec = result;
Free (attrValue);
}
else
fprintf (stderr,"Warning: ignoring unrecognized type ref attribute value pair (%s/%s)\n", attrName, attrValue);
}
}
}
int
ParseBool PARAMS ((str, result),
char *str _AND_
int *result)
{
if (strcmp (str,"TRUE")==0)
{
*result = TRUE;
return 0;
}
if (strcmp (str,"FALSE")==0)
{
*result = FALSE;
return 0;
}
return -1;
}
int
ParseInt PARAMS ((str, result),
char *str _AND_
int *result)
{
*result = atoi (str);
return 0;
}
int
ParseCTypeId PARAMS ((str, result),
char *str _AND_
int *result)
{
if (strcmp (str,"C_CHOICE"))
{
*result = C_CHOICE;
return 0;
}
if (strcmp (str,"C_LIST"))
{
*result = C_LIST;
return 0;
}
if (strcmp (str,"C_ANY"))
{
*result = C_ANY;
return 0;
}
if (strcmp (str,"C_ANYDEFINEDBY"))
{
*result = C_ANYDEFINEDBY;
return 0;
}
if (strcmp (str,"C_LIB"))
{
*result = C_LIB;
return 0;
}
if (strcmp (str,"C_STRUCT"))
{
*result = C_STRUCT;
return 0;
}
if (strcmp (str,"C_TYPEREF"))
{
*result = C_TYPEREF;
return 0;
}
if (strcmp (str,"C_TYPEDEF"))
{
*result = C_TYPEDEF;
return 0;
}
if (strcmp (str,"C_NO_TYPE"))
{
*result = C_NO_TYPE;
return 0;
}
return -1;
}
int
ParseTypeId PARAMS ((str, result),
char *str _AND_
int *result)
{
if (strcmp (str,"UNKNOWN"))
{
*result = BASICTYPE_UNKNOWN;
return 0;
}
if (strcmp (str,"BOOLEAN"))
{
*result = BASICTYPE_BOOLEAN;
return 0;
}
if (strcmp (str,"INTEGER"))
{
*result = BASICTYPE_INTEGER;
return 0;
}
if (strcmp (str,"BITSTRING"))
{
*result = BASICTYPE_BITSTRING;
return 0;
}
if (strcmp (str,"OCTETSTRING"))
{
*result = BASICTYPE_OCTETSTRING;
return 0;
}
if (strcmp (str,"NULL"))
{
*result = BASICTYPE_NULL;
return 0;
}
if (strcmp (str,"OID"))
{
*result = BASICTYPE_OID;
return 0;
}
if (strcmp (str,"REAL"))
{
*result = BASICTYPE_REAL;
return 0;
}
if (strcmp (str,"ENUMERATED"))
{
*result = BASICTYPE_ENUMERATED;
return 0;
}
if (strcmp (str,"SEQUENCE"))
{
*result = BASICTYPE_SEQUENCE;
return 0;
}
if (strcmp (str,"SEQUENCEOF"))
{
*result = BASICTYPE_SEQUENCEOF;
return 0;
}
if (strcmp (str,"SET"))
{
*result = BASICTYPE_SET;
return 0;
}
if (strcmp (str,"SETOF"))
{
*result = BASICTYPE_SETOF;
return 0;
}
if (strcmp (str,"CHOICE"))
{
*result = BASICTYPE_CHOICE;
return 0;
}
if (strcmp (str,"ANY"))
{
*result = BASICTYPE_ANY;
return 0;
}
if (strcmp (str,"ANYDEFINEDBY"))
{
*result = BASICTYPE_ANYDEFINEDBY;
return 0;
}
if (strcmp (str,"LOCALTYPEREF"))
{
*result = BASICTYPE_LOCALTYPEREF;
return 0;
}
if (strcmp (str,"IMPORTYPEREF"))
{
*result = BASICTYPE_IMPORTTYPEREF;
return 0;
}
return -1;
}
void
ParseAttr PARAMS ((str, startLoc, attrName, attrValue),
char *str _AND_
int *startLoc _AND_
char **attrName _AND_
char **attrValue)
{
int len;
int loc;
int attrNameStart;
int attrNameEnd;
int attrValueStart;
int attrValueEnd;
static char retAttrName[200];
char *retAttrValue;
loc = *startLoc;
len = strlen (str)-1;
for (; (loc <= len) && str[loc] == ' '; loc++)
;
if (loc >= len)
{
*attrName = NULL;
*attrValue = NULL;
return;
}
attrNameStart = loc;
for (; (loc <= len) && str[loc] != ':'; loc++)
;
if (loc > len)
{
*attrName = NULL;
*attrValue = NULL;
return;
}
attrNameEnd = loc-1;
loc++;
if (str[loc++] != '"')
{
*attrName = NULL;
*attrValue = NULL;
fprintf (stderr,"ERROR in snacc comment attribute string \"%s\". Missing quote at beggining of field value\n",str);
return;
}
attrValueStart = loc;
for (; (loc <= len) && str[loc] != '"'; loc++)
;
attrValueEnd = loc-1;
if ((loc > len) || (str[attrValueStart-1] != '"'))
{
*attrName = NULL;
*attrValue = NULL;
fprintf (stderr,"Parsing Error after position %d in snacc attribute string \"%s\".\n",*startLoc, str);
return;
}
*startLoc = loc + 1;
retAttrValue = (char*) Malloc (attrValueEnd - attrValueStart + 2);
strncpy (retAttrName, &str[attrNameStart], attrNameEnd-attrNameStart+1);
strncpy (retAttrValue, &str[attrValueStart], attrValueEnd-attrValueStart+1);
retAttrValue[attrValueEnd-attrValueStart+1] = '\0';
retAttrName[attrNameEnd-attrNameStart+1] = '\0';
*attrName = retAttrName;
*attrValue = retAttrValue;
}
void
FillCTDIDefaults PARAMS ((r, ctdi, td),
CRules *r _AND_
CTDI *ctdi _AND_
TypeDef *td)
{
CTDI *tblCtdi;
int typeIndex;
typeIndex = GetBuiltinType (td->type);
if (typeIndex < 0)
return;
tblCtdi = &r->typeConvTbl[typeIndex];
memcpy (ctdi, tblCtdi, sizeof (CTDI));
}