Script to copy functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to copy functions
# 1  
Old 09-24-2012
Script to copy functions

I need script code that does the following.

It would go into all the .c files in the current directory and in each file, it would copy the function headers for all the functions that are not static(don't begin with static) and stores them in one file with the word "extern" added at the beginning(if they are not already) and parameter names removed.

The output should look something like this:

Code:
/* file1.c */
extern Queue *newQueue(void);
extern void enqueue(Queue *, void *);
extern *void dequeue(Queue *);

/* file2.c */
extern StringBuffer *newStringBuffer(void);
extern void clear(StringBuffer *);
extern char *input(StringBuffer *, FILE *);

file1 and file2 would obviously be the names of the files. And each block would contain the functions in that file.

Extra: have #include'd in each source file, and remove redundant extern declarations from them.

Last edited by James Denton; 09-24-2012 at 04:18 PM..
# 2  
Old 09-24-2012
Well, sed and shell easily do the first bit.
Code:
find ... -type f -name '*.c' | while read f
do
  e=$(
    sed '
      s/^[ \t]*//
      s/^extern[ \t][\ \t]*//
      /^[a-zA-Z0-9_]\{1,99\}(/{
        :loop
        /)/!{
          $d
          N
          s/\n[ \t]*/ /
          b loop
          }
        s/[ \t]\{1,99\}/ /g
        s/ \([,)]\)/\1/g
        s/ *\* */ */g
        s/[a-zA-Z0-9_]\{1,99\}\([,)]\)/\1/g
        s/^/extern /
        b
        }
      d
     ' $f
    )
 
  if [ "$e" != "" ]
  then
    echo "/* $f */
$e"
  fi
done >output

Narrative: find files, for each file $f, capute in e the output of the sed script on $f. The sed first clears any leading white space, any extern with white space, and detects a name with a ( tight on the name (if you have spaces, we need to remove them just before this). For that, it loops adding lines until there is a ')', but if eof shows up, deletes everything, then normalizes all white space to a single space, ensures one space before any asterisk and none after, ensures names end in ',' or ')' tight, removes the names, adds the extern, and writes out the product.

Not sure what the second thing is. Do you want every subroutine in its own file and and extern prototype in its place? Subroutine/file names might conflict, but that is a good practice. #include'ing subroutines is a step backward; learn to make a library mystuff.a using ar or even mystuff.so (dynamic) from that using gcc. Creating a shared and static library with the gnu compiler [gcc]

Last edited by DGPickett; 09-24-2012 at 05:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find C programming functions using shell script?

I want to find c function definition with pattern with shell script by checking condition for each line: data_type functionname(param_list){ .... } I knew cscope or ctag is usable for this task, but if there any ways to do without using them. I am thinking of checking line condition... (3 Replies)
Discussion started by: cmdcmd
3 Replies

2. Shell Programming and Scripting

Functions in a shell script

so i have a very big script that has the following format: functionA () { .... ... .... } Results=$(functionA) the code inside of functionA is very huge. so by the time the script gets to the "Results=" part, several seconds have already passed. the script is about 15MB in size.... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

4. Shell Programming and Scripting

Perl script to find where functions is called in c

Hello, I need to write a perl script to find where functions is called in c files.. The script should scan the file and find out the function names and then search to see where they are called... Lets for now assume all functions are internal. I don't know where to start :( ... (4 Replies)
Discussion started by: bojomojo
4 Replies

5. Shell Programming and Scripting

importing functions from an external bash script

Hi all, I'm trying to import some functions I have saved in a file named functions.sh into a different script (protocol.sh). Can anybody show me how to do this? I tried source functions.sh as I would do at the terminal but source command cannot be found from within the script protocol.sh. ... (2 Replies)
Discussion started by: tevang
2 Replies

6. Shell Programming and Scripting

Passing array to functions in ksh script

Let me know if there is a way to pass array to a funtion in ksh script. function isPresent { typeset member member=$1 dbList=$2 echo '$1:' $1 echo '$2' $dbList The array will be at the second position....something like this isPresent 12 <array> if then echo... (3 Replies)
Discussion started by: prasperl
3 Replies

7. Shell Programming and Scripting

Changing script into 2 functions.

ed... always same values get printed out... (2 Replies)
Discussion started by: HardyV2
2 Replies

8. Shell Programming and Scripting

using library functions in shell script

hey guys, i made up a library file called common.lib so as to reuse the same code without typing it again. here is the code. its pretty basic . ## This is the second function compare() { file1 = $1 file2 = $2 cmp $file1 $file2 if then echo "comparison is possible"... (1 Reply)
Discussion started by: Irishboy24
1 Replies

9. Shell Programming and Scripting

Creating functions in shell script

hi friends, I am working with shell commands and all these works properly. Then i created a small function which includes these commands. Now the problem arises. When the commands are run by calling this fuction.it shows error. Why i am not able to run the unix command inside a function.... (1 Reply)
Discussion started by: gjithin
1 Replies

10. Shell Programming and Scripting

Shell script functions

Simple shell script : date test_fn() { echo "function within test shell script " } on the shell prompt I run > . test Then I invoke the function on the command line as below : test_fn() It echos the line function within test shell script and works as expected. ... (5 Replies)
Discussion started by: r_subrahmanian
5 Replies
Login or Register to Ask a Question