/* * Copyright 1988 by the Massachusetts Institute of Technology. * For copying and distribution information, please see the file * . * * Purpose: * This module was developed to parse the "~/.klogin" files for * Kerberos-authenticated rlogin/rcp/rsh services. However, it is * general purpose and can be used to parse any such parameter file. * * The parameter file should consist of one or more entries, with each * entry on a separate line and consisting of zero or more * "keyword=value" combinations. The keyword is case insensitive, but * the value is not. Any string may be enclosed in quotes, and * c-style "\" literals are supported. A comma may be used to * separate the k/v combinations, and multiple commas are ignored. * Whitespace (blank or tab) may be used freely and is ignored. * * Full error processing is available. When PS_BAD_KEYWORD or * PS_SYNTAX is returned from fGetParameterSet(), the string ErrorMsg * contains a meaningful error message. * * Keywords and their default values are programmed by an external * table. * * Routines: * fGetParameterSet() parse one line of the parameter file * fGetKeywordValue() parse one "keyword=value" combo * fGetToken() parse one token * * * from: kparse.c,v 4.5 89/01/21 17:20:39 jtkohl Exp $ * $Id: kparse.c,v 1.3 1995/07/18 16:38:58 mark Exp $ */ #if 0 #ifndef lint static char rcsid[] = "$Id: kparse.c,v 1.3 1995/07/18 16:38:58 mark Exp $"; #endif lint #endif #include #include #include #include #ifndef FALSE #define FALSE 0 #define TRUE 1 #endif #define MAXKEY 80 #define MAXVALUE 80 int fGetKeywordValue(FILE *fp, char *keyword, int klen, char *value, int vlen); int fGetToken(FILE *fp, char *dest, int maxlen); int fGetChar(FILE *fp); int fUngetChar(int ch, FILE*fp); int fGetLiteral(FILE *fp);; int LineNbr=1; /* current line nbr in parameter file */ char ErrorMsg[80]; /* meaningful only when KV_SYNTAX, PS_SYNTAX, * or PS_BAD_KEYWORD is returned by * fGetKeywordValue or fGetParameterSet */ int fGetParameterSet(FILE *fp, parmtable parm[], int parmcount ) { int rc,i; char keyword[MAXKEY]; char value[MAXVALUE]; while (TRUE) { rc=fGetKeywordValue(fp,keyword,MAXKEY,value,MAXVALUE); switch (rc) { case KV_EOF: return(PS_EOF); case KV_EOL: return(PS_OKAY); case KV_SYNTAX: return(PS_SYNTAX); case KV_OKAY: /* * got a reasonable keyword/value pair. Search the * parameter table to see if we recognize the keyword; if * not, return an error. If we DO recognize it, make sure * it has not already been given. If not already given, * save the value. */ for (i=0; i= parmcount) { sprintf(ErrorMsg, "unrecognized keyword \"%s\" found", keyword); return(PS_BAD_KEYWORD); } break; default: sprintf(ErrorMsg, "panic: bad return (%d) from fGetToken()",rc); break; } } } /* * Routine: ParmCompare * * Purpose: * ParmCompare checks a specified value for a particular keyword. * fails if keyword not found or keyword found but the value was * different. Like strcmp, ParmCompare returns 0 for a match found, -1 * otherwise */ int ParmCompare(parmtable parm[], int parmcount, char *keyword, char *value ) { int i; for (i=0; i\n"); exit(1); } if (!(fp=fopen(*++argv,"r"))) { fprintf(stderr,"can\'t open input file \"%s\"\n",filename); exit(1); } filename = *argv; while ((rc=fGetKeywordValue(fp,key,MAXKEY,valu,MAXVALUE))!=KV_EOF){ switch (rc) { case KV_EOL: printf("%s, line %d: nada mas.\n",filename,LineNbr-1); break; case KV_SYNTAX: printf("%s, line %d: syntax error: %s\n", filename,LineNbr,ErrorMsg); while ( ((ch=fGetChar(fp))!=EOF) && (ch!='\n') ); break; case KV_OKAY: printf("%s, line %d: okay, %s=\"%s\"\n", filename,LineNbr,key,valu); break; default: printf("panic: bad return (%d) from fGetKeywordValue\n",rc); break; } } printf("EOF"); fclose(fp); exit(0); } #endif #ifdef PSTEST parmtable kparm[] = { /* keyword, default, found value */ { "user", "", (char *)NULL }, { "realm", "Athena", (char *)NULL }, { "instance", "", (char *)NULL } }; main(int argc, char **argv) { int rc,i,ch; FILE *fp; char *filename; if (argc != 2) { fprintf(stderr,"usage: test \n"); exit(1); } if (!(fp=fopen(*++argv,"r"))) { fprintf(stderr,"can\'t open input file \"%s\"\n",filename); exit(1); } filename = *argv; while ((rc=fGetParameterSet(fp,kparm,PARMCOUNT(kparm))) != PS_EOF) { switch (rc) { case PS_BAD_KEYWORD: printf("%s, line %d: %s\n",filename,LineNbr,ErrorMsg); while ( ((ch=fGetChar(fp))!=EOF) && (ch!='\n') ); break; case PS_SYNTAX: printf("%s, line %d: syntax error: %s\n", filename,LineNbr,ErrorMsg); while ( ((ch=fGetChar(fp))!=EOF) && (ch!='\n') ); break; case PS_OKAY: printf("%s, line %d: valid parameter set found:\n", filename,LineNbr-1); for (i=0; i