AWK and preprocessing

Hello ,

I'm trying to to write simple ANSI C preprocessor but I have problem with
recursive call. When I call my function : preproc(filename) , I get message
" Segmentation fault Core Dumped : " .
Please Help

Pawe epko

Hire Is My Code :

#!/usr/bin/nawk -f
#Autor : Pawe epko 
# Simple Preprocesor

BEGIN {      
   print "//preprocessing ... ";
   print "  ";
   for(i=1;i<ARGC;i++)
   {
     filename = ARGV
     preproc(filename);
   }
}
END{
}

## MY FUNCTION 
function preproc(filename)
{ 
  # file preprocessing :
  while ( getline < filename )
  {
   name=""
   include_true="F"
   if ( NF >= 3 && $1=="#define" )
   {
     for(i=3;i<=NF;i++)
       name = name "" $i; # we need this for macro
     for ( def in define )
      gsub(def,define[def], name ) ; # in name we search for names defined before by define drictive 
     define[$2] = name ;
     name=""
   }
   else if ( NF==2 && $1=="#include" )
   {
     for ( def in define )
     {  
       if ( $2==def) # # check if $2 was defined by #define dericticve 
       { 
         plik = define[def];
         preproc(plik); # <--Recursive call - we have to preproc and include files defined by #include 
         inc_true="T";
       }
    }
    if ( inc_true != "T" )
    {   
        preproc($2); # the same as abowe
        print " "
    }
     else 
       inc_true="F"
  }
  else
  {
     # preprocessing ... not finished yet 
  }
  }
  return ;
}



while ( getline < filename )

getline returns 0 for eof but -1 for an error. And if there is an error, $0 is left unchanged. You are encountering a line like:
#include <stdio.h>
and trying to open the file called "<stdio.h>" in the current directory. You need to strip the puncuation off of the filename and then search for it as c would. And you need to be prepared to detect errors... especially "file not found".

I don't open files like <stdio.h>
I was tested my program only with two files :

MY first file samp.c look like this :

#define pi 3.14159
#define piwo Alfa
#define check pi +tyututuyut+ala+ola+piwo!%@domek+pi!wewewewe + ! pi * piwo
pi = rachunek
#define fname "ali.c"
#include fname

File ali.c look like this:

#define alfa pawel
#define sizeo 5899

I run my program only with one parametr : samp.c

Well, it's the same deal. You try to open "ali.c". You don't check for an error from getline. getline can't open "ali.c" because the file is named ali.c. You are trying to open "ali.c" WITH THE QUOTES. Like I said...strip the punctuation and check for errors.

thanks , it's working