xref: /illumos-gate/usr/src/tools/cscope-fast/edit.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1999 by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 /*
32  *	cscope - interactive C symbol cross-reference
33  *
34  *	file editing functions
35  */
36 
37 #include <curses.h>	/* KEY_BREAK and refresh */
38 #include <libgen.h>
39 #include <stdio.h>
40 #include "global.h"
41 
42 
43 /* edit this displayed reference */
44 
45 void
46 editref(int i)
47 {
48 	char	file[PATHLEN + 1];	/* file name */
49 	char	linenum[NUMLEN + 1];	/* line number */
50 
51 	/* verify that there is a references found file */
52 	if (refsfound == NULL) {
53 		return;
54 	}
55 	/* get the selected line */
56 	seekline(i + topline);
57 
58 	/* get the file name and line number */
59 	if (fscanf(refsfound, "%s%*s%s", file, linenum) == 2) {
60 		edit(file, linenum);	/* edit it */
61 	}
62 	seekline(topline);	/* restore the line pointer */
63 }
64 
65 /* edit all references */
66 
67 void
68 editall(void)
69 {
70 	char	file[PATHLEN + 1];	/* file name */
71 	char	linenum[NUMLEN + 1];	/* line number */
72 	int	c;
73 
74 	/* verify that there is a references found file */
75 	if (refsfound == NULL) {
76 		return;
77 	}
78 	/* get the first line */
79 	seekline(1);
80 
81 	/* get each file name and line number */
82 	while (fscanf(refsfound, "%s%*s%s%*[^\n]", file, linenum) == 2) {
83 		edit(file, linenum);	/* edit it */
84 		if (editallprompt == YES) {
85 			putmsg("Type ^D to stop editing all lines, "
86 			    "or any other character to continue: ");
87 			if ((c = mygetch()) == EOF || c == ctrl('D') ||
88 			    c == ctrl('Z') || c == KEY_BREAK) {
89 				/* needed for interrupt on first time */
90 				(void) refresh();
91 				break;
92 			}
93 		}
94 	}
95 	seekline(topline);
96 }
97 
98 /* call the editor */
99 
100 void
101 edit(char *file, char *linenum)
102 {
103 	char	msg[MSGLEN + 1];	/* message */
104 	char	plusnum[NUMLEN + 2];	/* line number option */
105 	char	*s;
106 
107 	(void) sprintf(msg, "%s +%s %s", editor, linenum, file);
108 	putmsg(msg);
109 	(void) sprintf(plusnum, "+%s", linenum);
110 
111 	/* if this is the more or page commands */
112 	if (strcmp(s = basename(editor), "more") == 0 ||
113 	    strcmp(s, "page") == 0) {
114 		/*
115 		 * get it to pause after displaying a file smaller
116 		 * than the screen length
117 		 */
118 		(void) execute(editor, editor, plusnum, file, "/dev/null",
119 		    (char *)NULL);
120 	} else {
121 		(void) execute(editor, editor, plusnum, file, (char *)NULL);
122 	}
123 }
124