Saturday, May 15, 2021

Essential CSCOPE shortcuts to browse the code - CSCOPE Cheat Sheet

    CSCOPE is an opensource tool which is used to browse the "C" source code. In very large projects, the source code is spanned across several directories and the code walk to understand a particular code flow becomes very difficult. In such situation, the CSCOPE tool becomes handy.

    When CSCOPE is run inside a directory where the source code of the project is present, it will builds up an internal database using the functions, variables, etc of the source code. Then the CSCOPE uses that database to help the users to browse the code.

    To start the CSCOPE for a new project, go the project directory where the source C code is present. Execute the command "cscope -R" which will start and build the CSCOPE database. The -R flag will tell the CSCOPE to recursively go through all the sub-directories of the project directory to build the database.

   Please find the below screenshot which shows the building of CSCOPE database when we run the "cscope -R" command for the first time.

 


   The CSCOPE window has two section. One is the list of menus at the lower half of the window which can be used to browse the code. The upper half of the window is used to show the results when we browse the code using one of the menus.

    As shown in the below screenshot, the search result of the C Symbol "sig_usr" is listed in the upper part of the screen.


Keyboard shortcuts to traverse the code browse options:

Tab
Move to the next input field.
Return
Move to the next input field.
ctrl+n
Move to the next input field.
ctrl+p
Move to the previous input field.
ctrl+y
Search with the last text typed.
ctrl+b
Move to the previous input field and search pattern.
ctrl+f
Move to the next input field and search pattern.
ctrl+c
Toggle ignore/use letter case when searching. For example, a search for FILE matches file and File when ignoring the letter case.
ctrl+r
Rebuild cross-reference.
!
Start an interactive shell. Type ^d to return to cscope.
ctrl+l
Redraw the screen.
?
Display the list of commands.
ctrl+d
Exit cscope.

Keyboard shortcuts to traverse the search results:

1 - 9
Edit the file referenced by this line. The number you type corresponds to an item in the list of lines printed by cscope.
Space
Display the next set of matching lines.
+
Display the next set of matching lines.
ctrl+v
Display the next set of matching lines.
--
Display the previous set of matching lines.
ctrl+e
Edit the displayed files in order.
>
Append the list of lines being displayed to a file.
|
Pipe all lines to a shell command.