8 #define iofail() do { perror(pname); exit(EXIT_FAILURE); } while (0)
9 #define iofailn(e) do { errno=(e); perror(pname); exit(EXIT_FAILURE); } while (0)
17 #define WHITE(c) ((c)==' '||(c)=='\t')
19 static char *getsym(char *s)
23 fprintf(stderr,"getsym(\"%s\")\n",s);
26 if (!strcmp(sm->sym,s)) {
28 fputs("getsym() - OK",stderr);
35 fputs("getsym() - NOT FOUND",stderr);
40 static void addsym(char *sym,char *val)
43 struct sym *sm=malloc(sizeof(*sm)+(syml=strlen(sym)+1)+strlen(val)+1);
45 fprintf(stderr,"addsym(\"%s\",\"%s\")\n",sym,val);
50 strcpy(symt->val=strcpy(symt->sym=(char *)sm+sizeof(*sm),sym)+syml,val);
52 fputs("addsym() end\n",stderr);
56 static char *strip(char *s)
60 fprintf(stderr,"strip(\"%s\")\n",s);
62 while (WHITE(*s)) s++;
63 for (s2=s;*s2;s2++); s2--;
64 while (s2>=s&&WHITE(*s2)) s2--;
67 fprintf(stderr,"strip()==\"%s\"\n",s);
72 static char *quote(char **sp)
75 while (WHITE(*s)) s++;
76 if (*s++!='"') return(NULL);
78 while (*s&&*s!='"') s++;
79 if (!*s) return(NULL);
84 int eval(char *s,char *buf,int bs)
87 fprintf(stderr,"eval(\"%s\")\n",s);
90 while (*s&&*s!='$'&&bs) bs--,*buf++=*s++;
95 fputs("eval() end\n",stderr);
101 while (*s&&*s!=')') s++;
104 if ((sym=getsym(smn))) {
105 while (*sym&&bs) bs--,*buf++=*sym++;
113 int main(int argc,char **argv)
115 char line[512],outt[sizeof(line)],*s;
116 char *c1,*c2,cn1[512],cn2[sizeof(cn1)];
117 int lins,run=1,cdpos,proc,i;
118 if ((pname=rindex(*argv,'/'))) pname++;
121 ##############################################################\n\
122 ### This file was created automatically by gmk2mk utility. ###\n\
123 ### PLEASE DON'T EDIT THIS FILE, edit GNUmakefile instead. ###\n\
124 ##############################################################\n");
125 while (fgets(line,sizeof(line),stdin)) {
126 if (line[(lins=strlen(line))-1]=='\n') line[--lins]='\0';
130 if (line[i]=='"') cdpos=!cdpos;
131 else if (cdpos&&line[i]=='#') {
136 if (!strncmp(line,"ifeq ",5)) {
139 proc=0; s=&line[cdpos];
140 if ((c1=quote(&s))) if ((c2=quote(&s))) {
141 while (WHITE(*s)) s++;
142 if (!*s&&eval(c1,cn1,sizeof(cn1))&&eval(c2,cn2,sizeof(cn2))) {
144 run=(!i&&cdpos==5)||(!!i&&cdpos==6);
148 else if (!strncmp(line,"ifneq ",6)) {
152 else if (!strncmp(line,"ifdef ",6)) {
153 run=!!getsym(strip(&line[6]));
156 else if (!strncmp(line,"ifndef ",7)) {
157 run=!getsym(strip(&line[7]));
160 else if (!strncmp(line,"else",4)) {
164 else if (!strncmp(line,"endif",5)) {
168 if (!run||!proc) putchar('#');
173 if (eval(strip(&line[i]),cn1,sizeof(cn1))) {
174 addsym(strip(line),cn1);
181 return(EXIT_SUCCESS);