📄 kparse.c
字号:
fUngetChar(ch,fp); } while (!done); *p = '\0'; if (ISLINEFEED(ch)) return(GTOK_BAD_QSTRING); return(GTOK_QSTRING); } /* * Not a quoted string. If its a token character (rules are * defined via the ISTOKENCHAR macro, in kparse.h) take it and all * token chars following it until we run out of space. */ digits=TRUE; if (ISTOKENCHAR(ch)) { while ( (ISTOKENCHAR(ch)) && len<maxlen-1 ) { if (!isdigit(ch)) digits=FALSE; *p++ = ch; len++; ch = fGetChar(fp); }; *p = '\0'; if (ch!=EOF) { fUngetChar(ch,fp); } if (digits) { return(GTOK_NUMBER); } else { return(GTOK_STRING); } } /* * Neither a quoted string nor a token character. Return a string * with just that one character in it. */ if (ch==EOF) { return(GTOK_EOF); } if (!ISWHITESPACE(ch)) { *p++ = ch; *p='\0'; } else { *p++ = ' '; /* white space is always the * blank character */ *p='\0'; /* * The character is a white space. Flush all additional white * space. */ while (ISWHITESPACE(ch) && ((ch=fGetChar(fp)) != EOF)) ; if (ch!=EOF) { fUngetChar(ch,fp); } return(GTOK_WHITE); } return(GTOK_PUNK);}/* * fGetLiteral is called after we find a '\' in the input stream. A * string of numbers following the backslash are converted to the * appropriate value; hex (0xn), octal (0n), and decimal (otherwise) * are all supported. If the char after the \ is not a number, we * special case certain values (\n, \f, \r, \b) or return a literal * otherwise (useful for \", for example). */fGetLiteral(fp) FILE *fp;{ int ch; int n=0; int base; ch = fGetChar(fp); if (!isdigit(ch)) { switch (ch) { case 'n': return('\n'); case 'f': return('\f'); case 'r': return('\r'); case 'b': return('\b'); default: return(ch); } } /* * got a number. might be decimal (no prefix), octal (prefix 0), * or hexadecimal (prefix 0x). Set the base appropriately. */ if (ch!='0') { base=10; /* its a decimal number */ } else { /* * found a zero, its either hex or octal */ ch = fGetChar(fp); if ((ch!='x') && (ch!='X')) { base=010; } else { ch = fGetChar(fp); base=0x10; } } switch (base) { case 010: /* octal */ while (ISOCTAL(ch)) { n = (n*base) + ch - '0'; ch = fGetChar(fp); } break; case 10: /* decimal */ while (isdigit(ch)) { n = (n*base) + ch - '0'; ch = fGetChar(fp); } break; case 0x10: /* hexadecimal */ while (isxdigit(ch)) { if (isdigit(ch)) { n = (n*base) + ch - '0'; } else { n = (n*base) + toupper(ch) - 'A' + 0xA ; } ch = fGetChar(fp); } break; default: fprintf(stderr,"fGetLiteral() died real bad. Fix gettoken.c."); exit(1); break; } fUngetChar(ch,fp); return(n);}/* * exactly the same as ungetc(3) except that the line number of the * input file is maintained. */fUngetChar(ch,fp) int ch; FILE *fp;{ if (ch=='\n') LineNbr--; return(ungetc(ch,fp));}/* * exactly the same as fgetc(3) except that the line number of the * input file is maintained. */fGetChar(fp) FILE *fp;{ int ch = fgetc(fp); if (ch=='\n') LineNbr++; return(ch);}/* * Routine Name: strsave * * Function: return a pointer to a saved copy of the * input string. the copy will be allocated * as large as necessary. * * Explicit Parameters: pointer to string to save * * Implicit Parameters: None * * External Procedures: malloc,strcpy,strlen * * Side Effects: None * * Return Value: pointer to copied string * */char * strsave(p) char *p;{ return(strcpy(malloc(strlen(p)+1),p));}/* * strutol changes all characters in a string to lower case, in place. * the pointer to the beginning of the string is returned. */char * strutol( start ) char *start;{ char *q; for (q=start; *q; q++) if (isupper(*q)) *q=tolower(*q); return(start);}#ifdef GTOK_TEST /* mainline test routine for fGetToken() */#define MAXTOKEN 100char *pgm = "gettoken";main(argc,argv) int argc; char **argv;{ char *p; int type; FILE *fp; if (--argc) { fp = fopen(*++argv,"ra"); if (fp == (FILE *)NULL) { fprintf(stderr,"can\'t open \"%s\"\n",*argv); } } else fp = stdin; p = malloc(MAXTOKEN); while (type = fGetToken(fp,p,MAXTOKEN)) { switch(type) { case GTOK_BAD_QSTRING: printf("BAD QSTRING!\t"); break; case GTOK_EOF: printf("EOF!\t"); break; case GTOK_QSTRING: printf("QSTRING\t"); break; case GTOK_STRING: printf("STRING\t"); break; case GTOK_NUMBER: printf("NUMBER\t"); break; case GTOK_PUNK: printf("PUNK\t"); break; case GTOK_WHITE: printf("WHITE\t"); break; default: printf("HUH?\t"); break; } if (*p=='\n') printf("\\n\n"); else printf("%s\n",p); } exit(0);}#endif#ifdef KVTESTmain(argc,argv) int argc; char **argv;{ int rc,ch; FILE *fp; char key[MAXKEY],valu[MAXVALUE]; char *filename; if (argc != 2) { fprintf(stderr,"usage: test <filename>\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 PSTESTparmtable kparm[] = { /* keyword, default, found value */ { "user", "", (char *)NULL }, { "realm", "Athena", (char *)NULL }, { "instance", "", (char *)NULL }};main(argc,argv) int argc; char **argv;{ int rc,i,ch; FILE *fp; char *filename; if (argc != 2) { fprintf(stderr,"usage: test <filename>\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<PARMCOUNT(kparm); i++) { printf("\t%s = \"%s\"\n",kparm[i].keyword, (kparm[i].value ? kparm[i].value : kparm[i].defvalue)); } break; default: printf("panic: bad return (%d) from fGetParameterSet\n",rc); break; } FreeParameterSet(kparm,PARMCOUNT(kparm)); } printf("EOF"); fclose(fp); exit(0);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -