Back to index

wims  3.65+svn20090927
oncechar.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* This special program selects words composed by
00019         * selected characters, each selected character being used
00020         * at most once in the word */
00021 
00022        /* Selected characters are entered by the env var 'oncechar'.
00023         * Words entered by stdin. Output to stdout. */
00024 
00025 #include "../wims.h"
00026 #define MAX_WORDLEN 1023
00027 
00028 char wbuf[MAX_WORDLEN+1];
00029 char selbuf[256];
00030 
00031 int getword(void)
00032 {
00033     int t,c;
00034     
00035     do c=getchar(); while(isspace(c));
00036     for(t=0; t<MAX_WORDLEN && isalnum(c); t++, c=getchar()) wbuf[t]=c;
00037     wbuf[t]=0; return c;    
00038 }
00039 
00040 void checkword(void)
00041 {
00042     char sbuf[256];
00043     char *p, *pt;
00044     memmove(sbuf,selbuf,sizeof(sbuf));
00045     for(p=wbuf; *p; p++) {
00046        pt=strchr(sbuf,*p); if(pt==NULL) return;
00047        else *pt=' ';
00048     }
00049     printf("%s\n",wbuf);
00050 }
00051 
00052 int main(int argc, char *argv[])
00053 {
00054     int c;
00055     char *p;
00056     
00057     p=getenv("oncechar"); if(p==NULL || *p==0) return 0;
00058     snprintf(selbuf,sizeof(selbuf),"%s",p);
00059     for(p=selbuf; *p; p++) 
00060       if(isspace(*p) || strchr("^?*.${}[]()\\",*p)!=NULL) strcpy(p,p+1);
00061     do{
00062        c=getword(); checkword();
00063     }
00064     while(c!=EOF);
00065     return 0;
00066 }
00067