Find all functions in a C file - ctags

I need a script to get list of all functions from one particular file.
Here is similar question and response for cscope tool.
how can I display all function name from cscope database?
I've tried to do it this way, but I get all functions in a project and I am not able to assign them to right file.
Any idea how to get just all functions of a specified C file?

You can use the -b option to cscope and just build the cross ref db the first time, and then use the mechanism in the link you specified, but don't use the -R option. Make it use the db you've already built.
find . -name foo.c > cscope.files
cscope -b

Related

How to limit the depth of the directory visited by the Unix command find?

Similar question has already been asked but I am not satisfyed with the answers. Indeed, I want to use the Unix find command and the find command I use does not allow the option -maxdepth. However, there is an option -depth but I did not succed to use it in a way that I am satisfied. I use ksh shell.
You can limit the listing if you add the grep command like this example:
find $mydir -name "BILL_*.pdf" | grep "$mydir/BILL"
Good luck...
Refer find manual
-depth Always evaluates to the value True. Causes the descent of the
directory hierarchy to be done so that all entries in a directory are
affected before the directory itself is affected. It can be useful
when the find command is used with the cpio command to transfer files
that are contained in directories without write permission.
So it does not accept an argument.

PAR packer is not including user defined modules

I have a Perl script test.pl which includes another Perl module fact.pm which is under crypt/Module dir.
the source code of crypt/test.pl is:
#!/usr/bin/perl
use Term::ANSIColor qw(:constants);
use File::Path;
use POSIX qw(strftime);
use File::Basename qw(dirname);
use Cwd qw(abs_path);
use lib dirname(dirname abs_path $0);
use crypt::Module::fact qw(factorial);
&factorial();#function present in fact.pm
print("Thanks for that thought \n");
The PAR packer command given is
pp -M Module::fact -o test test.pl
on copying just the executable test on a different directory path and executing it I am getting the below error:
Can't locate crypt/Module/fact.pm in #INC (you may need to install the crypt::Module::fact module)
how is it possible to include the module in the executable?
First, I'd recommend using the -c and/or -x options for the pp utility, which are used "to determine additonal run-time dependencies". I've gotten into the habit of using both.
Although you are using the -M option to add a module, I think that you have a typo with that option. In your code, you are using a "crypt::Module::fact" module, but you are specifying a "Module::fact" module with the -M option. Perhaps if you used "-M crypt::Module::fact" instead of "-M Module::fact", your problem may be solved.
Also, you might need to use -I (or -lib) to specify the path to any additional module library directories.

Bash - Remote Library

We're two student working on a Bash Tools Box, and we come across a problem :
Our local script use functions, stocked in two local libraries.
Our script uses functions inside the first library, and this last uses functions inside the second one library.
Script <-- Library1 <-- Library2
We can't fusion the two libraries in one.
We actually use this command to run our script on a remote computer :
ssh login#remoteIP bash < ~/script.sh
The question is : How to set in the script the library location
You need to use the . command in order to include external libraries. Yes, the command is simply called . - a literal dot. In library1.sh add at the top:
. /path/to/library2.sh
In script.sh add at the top:
. /path/to/library1.sh
I would recommend to use absolute paths since relative paths. If you use relative paths, you need to make sure that they are relative to one of your $PATH entries otherwise they would be relative to the current folder where script.sh gets executed.
Btw, there is also the source command which is doing excatly the same as the dot command. Both of them are bash builtins.
Type help . or help source to get help.
Embed your Library# code in your script using bashpp.
Replace your . calls with #include and then run it through bashpp:
bashpp ~/script.sh | ssh login#remoteIP bash -s

searching in the source code

Often I do different projects and sometimes there is a lack of documentation.
So I decided to use open-source code for looking how people solved different problems.
The idea is if I run into function what I don't how to use I look for different developers used that function before.
Approach:
I downloaded a few pretty decent projects done by other people and put them into one folder.
Now, if I don't know how a function is used (e.g. main() ), I do :
find . -name \*.py | xargs cat | grep -n "main()"
Consequently I get examples of its use:
But there is a problem. I don't know from which file examples are. It'd be perfectly if it was possible to get name of the file as well as number of line.
It seems to be limitation of use "cat" command because it mixes all files together and as result I get information about number not in the file but rather in cat output. So I feel this approach is bad in the root.
i.e.
I want to be able to look for functions/symbols in plethora of source code
and get information about the line and file where a certain combination was met.
I prefer console-way.
Any advice?
Try this:
find . -name \*.py -exec grep -nH "main()" {} \;
Explanation:
The "-exec" option says to execute the following command, up until \; for each file it finds.
The "-H" option to grep causes it to print the name of the file in which the string was found.
The "-n" option causes grep to print the line numbers.
The {} is a placeholder that expands to the name of the file that "find" just found.
You need only grep command:
$ grep -nr 'main()' /path/to/projects/folder/* | grep '.py:'
Want to search source files ? Why not http://beyondgrep.com/ ?
I wont answer you from the point of the bash.
I dont know which editor/IDE are you using, but for code dissecting there is no better tool for me then:
Vim with Ctags combination
Ctrl-p,Ctrl-p funky and MRU plugin +
proper search and regex usage.
good vim debugger
There is no part of code that cant be examined. Please sorry if are using some other tools, I am just suggesting you what do I find is the best for code analysis for me.

What is the equivalent to locate command in KornShell?

I am using KornShell (ksh) and I need to know what is the command to search a file in the system?
I have used locate in bash looking for a similar one.
Kindly help.
You can use "find" command to search for a particular file in the system.
There are various option to search by name,size,time,etc
You can refer to man find for more help.
E.g.
find . -name abc
will search abc file in the current directory and subdirectories
locate is not a bash-internal command, it is an external program. Provided that /usr/bin/locate is installed and in your $PATH environment variable, it should work just the same in ksh.
Try
which cmdName
and/or
whence cmdName
where of course, you replace cmdName with the command you are searching for.
which1 will searchs the $PATH variable, whilewhence` (if available on your system) searchs $PATH, aliases and functions.
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to use the check mark to accept the answer, and/or give it a + (or -) as a useful answer.
old post but imho still important:
locate is not the same as find. locate keeps a database of filenames, in which it searches for the files. It is therefore faster but less up-to-date than find, which browses the actual directories on the fly.

Resources