本节中的示例说明如何使用 cscope 执行以下三项任务:将常量更改为预处理程序符号、向函数增加参数以及更改变量的值。第一个示例说明了更改文本字符串的过程,这与 cscope 菜单上的其他任务稍有不同。也就是说,在您输入要更改的文本字符串之后,cscope 会提示您输入新文本,显示包含旧文本的行,并等待您指定要更改的行。
假设您要将常量 100 更改为预处理程序符号 MAXSIZE。选择第六个菜单项 Change this text string,然后输入 \100。必须使用反斜杠将 1 转义,原因是它对 cscope 具有特殊意义(菜单上的第 1 项)。现在按回车键。cscope 将提示您输入新文本字符串。键入 MAXSIZE。
cscope 函数:更改文本字符串:
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: \100 Find this egrep pattern: Find this file: Find files #including this file: To: MAXSIZE |
cscope 显示包含指定文本字符串的行,并等待您选择要更改其中文本的行。
cscope 函数:提示要更改的行:
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: \100 Find this egrep pattern: Find this file: Find files #including this file: To: MAXSIZE |
您知道列表中第 1、2 和 3 行(列出的源文件的第 4、26 和 8 行)中的常量 100 应更改为 MAXSIZE。您还知道 read.c 中的 0100 和 err.c 中的 100.0(列表中的第 4 行和第 5 行)不应更改。使用以下单键命令选择要更改的行:
表 8–3 用于选择要更改的行的命令
1-9 |
标记要更改的行或去标记。 |
* |
标记要更改的所有显示行或去标记。 |
空格 |
显示下一组行。 |
+ |
显示下一组行。 |
… |
显示上一组行。 |
a |
标记要更改的所有行。 |
^d |
更改标记的行并退出。 |
Esc |
退出而不更改标记的行。 |
在本例中,输入 1、2 和 3。您键入的数字不会打印在屏幕上。相反,cscope 通过在列表中您要更改的每个列表项的行号后面打印 >(大于)符号来标记这些项。
cscope 函数:标记要更改的行:
Change "100" to "MAXSIZE" File Line 1>init.c 4 char s[100]; 2>init.c 26 for (i = 0; i < 100; i++) 3>find.c 8 if (c < 100) { 4 read.c 12 f = (bb & 0100); 5 err.c 19 p = total/100.0; /* get percentage */ 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): |
现在键入 ^d 更改所选行。cscope 将显示已更改的行并提示您继续执行。
cscope 函数:显示已更改的文本行:
Changed lines: char s[MAXSIZE]; for (i = 0; i < MAXSIZE; i++) if (c < MAXSIZE) { Press the RETURN key to continue: |
当您响应此提示而按回车键时,cscope 将刷新屏幕,从而使其恢复到您选择要更改的行之前的状态。
下一步是为新符号 MAXSIZE 添加 #define。由于将要显示 #define 的头文件不在其行得到显示的文件之列,因此您必须通过键入 ! 退回到 shell。shell 提示符出现在屏幕底部。然后进入编辑器,并添加 #define。
cscope 函数:退出到 Shell:
Text string: 100 File Line 1 init.c 4 char s[100]; 2 init.c 26 for (i = 0; i < 100; i++) 3 find.c 8 if (c < 100) { 4 read.c 12 f = (bb & 0100); 5 err.c 19 p = total/100.0; /* get percentage */ 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 |
要恢复 cscope 会话,请退出编辑器并键入 ^d 以退出 shell。
向函数增加参数包括两个步骤:编辑函数本身,以及向代码中调用函数的各个位置增加新参数。
首先,使用第二个菜单项 Find this global definition 编辑函数。接着,找出调用函数的位置。使用第四个菜单项 Find functions calling this function 获取调用此函数的所有函数的列表。使用此列表,您可以通过分别输入每行在列表中的编号为该行调用编辑器,或通过键入 ^e 自动为所有行调用编辑器。使用 cscope 进行此类更改可确保您需要编辑的任何函数均不会被忽略。
有时,您可能要了解建议的更改如何影响代码。
假设您要更改变量或预处理程序符号的值。在这样做之前,请使用第一个菜单项 Find this C symbol 获取受到影响的引用的列表。然后使用编辑器检查每个引用。此步骤有助于预测您建议的更改的总体影响。之后,可以按相同的方式使用 cscope,验证是否已进行更改。