/* option2.h ========= */ /* Sessai Special version of option.h(c) */ /* $Log: option.c,v $ Revision 1.2 1997/04/25 10:40:45 root Added check for null pointer on opterr. Revision 1.1 1997/04/25 10:22:32 root Initial revision */ /* $Revision: 1.2 $ */ #if 0 // #include <stdio.h> // #include <string.h> // #include <stdlib.h> // #include "option2.h" #endif char *scan_entry2(FILE *file) { int c=1; int chr; char *parse=NULL; if ((parse=malloc(256)) ==NULL) return NULL; do { /* skip whitespace */ while ( ((chr=fgetc(file)) != EOF) && ( (chr==' ') || (chr == '\n') || (chr== '\t') ) ); if (chr=='#') { /* skip this line its a comment */ while ( ((chr=fgetc(file)) !=EOF) && (chr !='\n')); chr='#'; } } while (chr=='#'); parse[0]=(char) chr; while ( (c<256) && ((chr=fgetc(file)) != EOF) && (chr !=' ') && (chr != '\n') && (chr != '\t') ) { parse[c]=(char) chr; c++; } if ((chr==EOF) && (c==1)) { free(parse); return NULL; } parse[c]=0; parse=realloc(parse,c+1); return parse; } int process_option2 (int argc,char *argv[], struct option *opt, void (*opterr)(char *)) { int i=1; while (i<argc) { int j=0; if (argv[i][0] !='-') { if (i==argc-1) return i; fprintf(stderr, "illeagal argument \'%s\' found, skip it!\n",&argv[i][0]); if (opterr !=NULL) (*opterr)(&argv[i][0]); i++; continue; } while ( (opt[j].optname !=NULL) && (strcmp(&argv[i][1],opt[j].optname) !=0) ) j++; if (opt[j].optname==NULL) { fprintf(stderr, "illeagal option \'%s\' found, skip it!\n",&argv[i][0]); if (opterr !=NULL) (*opterr)(&argv[i][1]); i++; continue; } if (( (opt[j].type=='s')||(opt[j].type=='l')||(opt[j].type=='f') ||(opt[j].type=='d')||(opt[j].type=='t')) && ((i+1) >=argc)) { fprintf(stderr, "option \'%s\' has no argument!, skip it!\n",&argv[i][0]); if (opterr !=NULL) (*opterr)(&argv[i][0]); i++; continue; } i++; // opt[j].set=1; opt[j].set++; switch (opt[j].type) { case 's' : /* short integer */ if (opt[j].ptr !=NULL) { if ((argv[i][0]=='-')&&((short int) atoi(argv[i])==0)) { fprintf(stderr, "option \'%s\' looks no argument!, skip it!\n" ,&argv[i-1][0]); opt[j].set--; continue; } *( (short int *) opt[j].ptr)=(short int) atoi(argv[i]); } i++; break; case 'l' : /* long integer */ if (opt[j].ptr !=NULL) { if ((argv[i][0]=='-')&&((long int) atol(argv[i])==0L)) { fprintf(stderr, "option \'%s\' looks no argument!, skip it!\n" ,&argv[i-1][0]); opt[j].set--; continue; } *( (long int *) opt[j].ptr)=(long int) atol(argv[i]); } i++; break; case 'f' : /* float */ if (opt[j].ptr !=NULL) { if ((argv[i][0]=='-')&&((float) atof(argv[i])==0.0)) { fprintf(stderr, "option \'%s\' looks no argument!, skip it!\n" ,&argv[i-1][0]); opt[j].set--; continue; } *( (float *) opt[j].ptr)=(float) atof(argv[i]); } i++; break; case 'd' : /* double */ if (opt[j].ptr !=NULL) { if ((argv[i][0]=='-')&&((double) atof(argv[i])==0.0)) { fprintf(stderr, "option \'%s\' looks no argument!, skip it!\n" ,&argv[i-1][0]); opt[j].set--; continue; } *( (double *) opt[j].ptr)=(double) atof(argv[i]); } i++; break; case 't' : /* string */ // if (opt[j].ptr !=NULL) strcpy(opt[j].ptr,argv[i]); if (opt[j].ptr !=NULL) strncpy(opt[j].ptr,argv[i],255); // opt[j].set=i; /* will always be > 1 - so no problem */ i++; break; default : if (opt[j].ptr !=NULL) *( (short int *) opt[j].ptr)=*( (short int *) opt[j].ptr)+1; break; } } return i; } void process_file2(FILE *fp,struct option *opt,void (*opterr)(char *)) { int fargc=1; char *fargv[256]; while ((fargc<256) && ((fargv[fargc]=scan_entry2(fp)) !=NULL)) fargc++; process_option2(fargc,fargv,opt,opterr); } /* a new function by Sessai */ int optset (char *optname, struct option *opt) { int j=0; while ( (opt[j].optname !=NULL) && (strcmp(optname,opt[j].optname) !=0) ) j++; if (opt[j].optname==NULL) return 0; return opt[j].set; }