|
|
These examples
show how cscope can be used
to perform three tasks: changing a constant
to a preprocessor symbol,
adding an argument to a function,
and changing the value of a variable.
The first example demonstrates the procedure for changing
a text string, which differs slightly from the other tasks
on the cscope menu.
Once you have entered the text string to be changed, cscope prompts you for the new text,
displays the lines containing the old text, and waits for you to
specify which of these lines you want it to change.
Suppose you want to change a constant, 100, to a preprocessor symbol,
MAXSIZE.
Select the sixth menu item, Change this text string
, and enter \100.
The 1 must be escaped with a backslash because it has a special meaning
(item 1 on the menu) to cscope.
Press
<RETURN>.
cscope will prompt you for the new text string.
Type MAXSIZE:
cscope Press the ? key for help
Find this C symbol: Find this global definition: Find functions called by this function: Find functions calling this function: Find this text string: Change this text string: Find this egrep pattern: Find this file: Find files #including this file: To: MAXSIZE
Changing a Text String
cscope displays the lines containing the specified text string, and waits for you to select those in which you want the text to be changed:
Change "100" to "MAXSIZE"File Line 1 err.c 19 p = total/100.0; /* get percentage */ 2 find.c 8 if (c < 100) { 3 init.c 4 char s[100]; 4 init.c 26 for (i = 0; i < 100; i++) 5 read.c 12 f = (bb & 0100);
Find this C symbol: Find this global definition: Find functions called by this function: Find functions calling this function: Find this text string: Change this text string: Find this egrep pattern: Find this file: Find files #including this file: Select lines to change (press the ? key for help):
cscope Prompts for Lines to Be Changed
You know that the constant ``100'' in lines 2, 3, and 4 of the list
(lines 4, 26, and 8 of the listed source files) should be changed to
``MAXSIZE''.
You also know that ``100'' in
err.c and 0100.0 in read.c
(lines 1 and 5 of the list) should not be changed.
You select the lines you want changed with the following single-key commands:
1-9 | mark or unmark the line to be changed |
* | mark or unmark all displayed lines to be changed |
space bar | display next set of lines |
+ | display next set of lines |
- | display previous set of lines |
a | mark or unmark all lines to be changed |
Ctrl-d | change the marked lines and exit |
ESC | exit without changing the marked lines |
Commands for Selecting Lines to Be Changed
In this case, enter 2, 3, and 4. Note that the numbers you type are not printed on the screen. Instead, cscope marks each list item you want to be changed by printing a <>> (greater than) symbol after its line number in the list:
Change "100" to "MAXSIZE"File Line 1 err.c 19 p = total/100.0; /* get percentage */ 2>find.c 8 if (c < 100) { 3>init.c 4 char s[100]; 4>init.c 26 for (i = 0; i < 100; i++) 5 read.c 12 f = (bb & 0100);
Find this C symbol: Find this global definition: Find functions called by this function: Find functions calling this function: Find this text string: Change this text string: Find this egrep pattern: Find this file: Find files #including this file: Select lines to change (press the ? key for help):
Marking Lines to Be Changed
Now press <Ctrl-d> to change the selected lines. cscope displays the lines that have been changed and prompts you to continue:
Changed lines: char s[MAXSIZE]; for (i = 0; i < MAXSIZE; i++) if (c < MAXSIZE) {Press the RETURN key to continue:
cscope Displays Changed Lines of Text
When you press <RETURN> in response to this prompt,
cscope redraws the screen, restoring it to its state before you selected
the lines to be changed, as shown in the screen below.
The next step is to add the #define for the new symbol MAXSIZE. Because the header file in which the #define is to appear is not among the files whose lines are displayed, you must escape to the shell by typing !. The shell prompt will appear at the bottom of the screen. Then enter the editor and add the #define:
Text string: 100File Line 1 err.c 19 p = total/100.0; /* get percentage */ 2 find.c 8 if (c < 100) { 3 init.c 4 char s[100]; 4 init.c 26 for (i = 0; i < 100; i++) 5 read.c 12 f = (bb & 0100);
Find this C symbol: Find this global definition: Find functions called by this function: Find functions calling this function: Find this text string: Change this text string: Find this egrep pattern: Find this file: Find files #including this file: $ vi defs.h
Escaping from cscope to the Shell
To resume the cscope session, quit the editor and press
<Ctrl-d>
to exit the shell.
Adding an argument to a function involves two steps: editing the function itself and adding the new argument to every place in the code where the function is called. cscope makes that easy.
First, edit the function by using the second menu item,
Find this global definition
.
Next, find out where the function is called.
Use the fourth menu item, Find functions calling this function
,
to get a list of all the functions that call it.
With this list, you can either invoke the editor for each line found
by entering the list number of the line individually, or invoke the
editor for all the lines automatically by pressing
<Ctrl-e>.
Using cscope to make this type of change assures that
none of the functions you need to edit will be overlooked.
The value of cscope as a browser becomes apparent
when you want to see how a proposed change will affect your code.
If you want to change the value of a variable or preprocessor symbol,
use the first menu item, Find this C symbol
, to obtain a list of references
that will be affected.
Then use the editor to examine each one.
This will help you predict the overall
effects of your proposed change.
You can also use this menu to verify that your changes have been made.