Script to insert a function into a C source file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to insert a function into a C source file
# 1  
Old 09-17-2012
Script to insert a function into a C source file

Hello,

I think I need sed, but perhaps awk could help.

I am trying to add a function to a C source file based off a struct declaration.

for example:

Code:
struct Rational
{
   int numerator;
   int denominator;
};

becomes

Code:
struct Rational
{
   int numerator;
   int denominator;
};

void init_Rational(struct Rational *ret, int numerator, int denominator)
{
   ret->numerator = numerator;
   ret->denominator = denominator;
}


How would I go about doing this?
# 2  
Old 09-17-2012
Try using awk:

Code:
awk '/^struct/ {s=$2;o=$0;next}
!o
o {o=o"\n"$0}
o && /int/{gsub(/;/,"");L[$2]++}
o && /}/ {
   printf("%s\n\nvoid init_%s(struct %s *ret", o, s, s)
   for(k in L) printf(", int %s", k)
   print ")\n{\n"
       for(k in L) printf("    ret->%s = %s;\n", k, k)
   print "}"
   split("",L);
   s=""
   o=""}' infile.c

# 3  
Old 09-18-2012
Same awk solution but supports formats:

Code:
struct Rational
{
   int numerator;
   int denominator;
};

Code:
struct Rational { int numerator; int denominator; };

Code:
struct Rational
{
   int numerator; int denominator;
};

Code:
struct Rational
{ int numerator; int denominator; };

awk code:

Code:
/^struct/ {r=1;}
r==1 {o=o $0; print $0;}
/}/ {
   ic=0;
   c=split(o,a);
   for (i=1; i<=c; i++) {
     if (a[i] ~ /^struct$/) {s=a[i+1]; sub("[^a-zA-Z_-]*$","",s);};
     if (a[i] ~ /^int$/) {t=a[i+1]; sub("[^a-zA-Z_-]*$","",t); L[t]=t;};
   }
   printf("\nvoid init_%s(struct %s *ret", s, s);
   for(k in L) printf(", int %s", k);
   print ")\n{"
   for(k in L) printf("    ret->%s = %s;\n", k, k)
   print "}\n\n"
   r==0;
   f="";
}


Last edited by rdrtx1; 09-18-2012 at 01:03 AM..
# 4  
Old 09-23-2012
If you're going to ask for help with your homework, there's a homework forum with additional posting requirements. Read the FAQ.
https://www.unix.com/homework-coursew...ons-forum.html

Regards,
Alister
# 5  
Old 09-24-2012
For typedef struct

Hello
Could you do the same for structures defined using typedef?

For example:

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

should turn into:

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

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

Remember that the name could be anything(not just Rational) and there could be any number of values inside the struct definition(not just numer and denom).

Also ret is just a random name. It is just used to initialize.

Thanks a lot

Last edited by James Denton; 09-24-2012 at 11:58 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to source file in my script

Hi, ls -ltrq res.file -rwxrwxr-x 1 ora install 4278 Nov 30 07:19 res.file $ more test.sh source res.file $ ./test.sh ./test.sh: .: res.file: cannot open $ id uid=600000014(ora) gid=63855(install) uname -a SunOS mymac 5.11 11.2 sun4v sparc sun4v The same thing works fine on a... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

3. Programming

Oracle Database: INSERT INTO with TO_DATE function

Hello, I am writting a procedure in shell script as, set serveroutput on declare date_gen DATE := $DATEGEN; begin INSERT INTO TBL1 ( EMPNAME,EMPID,EMPBDATE) VALUES($EMPNAME,$EMPID,TO_DATE(date_gen,'YYYY-MM-DD')); end; / Where DATEGEN is unix string variable which I need to use into... (10 Replies)
Discussion started by: Poonamol
10 Replies

4. Shell Programming and Scripting

Insert a function on an script's output

Hi, I have this code: #Convert epoch2date getdate() { perl -e ' use POSIX qw(strftime); $mydate = strftime "%c", localtime($ARGV); print $mydate; ' $1 } lsuser -a time_last_login gecos $username |awk '{print $1,$2,$3,$4}'|sed -e 's/gecos=/ Nombre: /' -e 's/time... (2 Replies)
Discussion started by: iga3725
2 Replies

5. Shell Programming and Scripting

Shell Script to display function names (called & defined) in a C++ Source Code

Hello to all, I am looking for a way to display only the names of function (calls & definition) of a C++ source code.There is already a post related to this, but the script is to find the functions using a specific variable, and the replies are not that convincing since they cannot be used for... (2 Replies)
Discussion started by: frozensmilz
2 Replies

6. Shell Programming and Scripting

Different behaviour when running script as source file

Hi all, OS: Solaris 10 I've written a script to scan the oratab file and give me a list of Oracle SIDs to choose from, and obviously in order to set the ORACLE_SID environment variable from within a script for the current shell, the script must be run as a source file. When I run it as a... (1 Reply)
Discussion started by: longjon
1 Replies

7. Shell Programming and Scripting

Insert a function in a jsp file using Shell scripting

Greetings to all.I am new to the forum as well as to UNIX as well.I have a jsp file which has the following selectedStartMonth = request.getParameter( "startMonth" ); selectedStartDay = request.getParameter( "startDay" ); selectedStartYear = request.getParameter( "startYear" );... (3 Replies)
Discussion started by: 20033716
3 Replies

8. Shell Programming and Scripting

create Insert script from a file

Hi , I have a text file text.txt which contains values as ULTRA,OTHERS,Mumbai,16912 ULTIMATE,OTHERS,Mumbai,16913 ULTIMATIUM,OTHERS,Mumbai,16914 I want to read the file line by line and create insert scripts like INSERT INTO TAB ( DESC,PLACE,NUMBER ) VALUES... (3 Replies)
Discussion started by: ultimatix
3 Replies

9. Shell Programming and Scripting

Source Env file in the Shell Script

Hi I am having a script which sets the application environment. In this script i am sourcing the applications env file, when i am debugging the script i see its executing all the environment values and all the variable values are set properply. Once this main shell script executes, then... (4 Replies)
Discussion started by: umakanthly
4 Replies

10. Shell Programming and Scripting

Tidy shell script source file

Hi all, Is there a tool to tidy up shell script source file and reformat it. Preferably I am looking for a perfect tool to do this. Also I have heard it is possible to do with emacs editor but unfortunately it is looking very different and new from vi. Emacs is new to me and I could not find... (1 Reply)
Discussion started by: meharo
1 Replies
Login or Register to Ask a Question