Convert string using sed

I have a couple structure definitions in my input code. For example:

struct node {
   int val;
   struct node *next;
};

or

typedef struct {
   int numer;
   int denom;
} Rational;

I used the following line to convert them into one line and copy it twice.

sed '/struct[^(){]*{/{:l N;s/\n//;/}[^}]*;/!t l;s/  */ /g;p;p}'

The result is this:

struct node { int val; struct node *next;};
struct node { int val; struct node *next;};
struct node { int val; struct node *next;};

typedef struct { int numer; int denom;} Rational;
typedef struct { int numer; int denom;} Rational;
typedef struct { int numer; int denom;} Rational;

This is what I want:

I want sed commands that takes an input file that contains those lines(repeating struct definitions) and does the following.

  1. I would like the first line to be restored to the original structure block
  2. I would like the second line to turn into to a function heading that looks like this...

    text void init_structName( structName *var, int data1, int data2 )

-structName is basically the name of the structure.
-var is any name you like.
-data1, data2.... are values that are in the struct.

   3.   I would like the third line to turn into to the function body.  Where I initialize the the data parameters. It would look like this.
    {
        var->data1 = data1;
        var->data2 = data2;
    }

So my input initially consists of struct definitions. After running the sed command shown above, all of them are converted into one line and copied twice.

This is the input we need to work with. So when the code finds a structure defintion it can assume that there will be two more copies below.

For example, this is the output I want if the input file had the repeating lines shown above(node and rational).

struct node {
   int val;
   struct node *next;
};
void init_node(struct node *var, int val, struct node *next)
{
var->val = val;
var->next =  next;
}

typedef struct {
   int numer;
   int denom;
} Rational;
void init_Rational( Rational *var, int numer, int denom ) 
{
   var->numer = numer;
   var->denom = denom;
}

In case someone was curious. These functions will be called from the main to initialize the struct variables.

If you want clarification please ask, I will respond quickly!!

Can someone help? Thanks so much!!
-James D

Think someone has already asked for this:

Thanks!!

If you're going to ask for help with your homework, there's a homework forum with additional posting requirements. Read the FAQ.

Regards,
Alister