1 /*===========================================================================
2  Copyright (c) 1998-2000, The Santa Cruz Operation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  *Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
11  *Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  *Neither name of The Santa Cruz Operation nor the names of its contributors
16  may be used to endorse or promote products derived from this software
17  without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  INTERRUPTION)
27  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  DAMAGE.
31  =========================================================================*/
32 
33 /*
34  *	file editing functions
35  *
36  *	cscope - interactive C symbol cross-reference
37  */
38 
39 #include "global-cscope.h"
40 #if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
41 #include <ncurses.h>
42 #else
43 #include <curses.h>
44 #endif
45 #include "path.h"
46 #include "encodepath.h"
47 
48 /** edit this displayed reference */
49 
50 void
editref(int i)51 editref(int i)
52 {
53 	char	file[PATHLEN + 1];	/* file name */
54 	char	linenum[NUMLEN + 1];	/* line number */
55 
56 	/* verify that there is a references found file */
57 	if (refsfound == NULL) {
58 		return;
59 	}
60 	/* get the selected line */
61 	seekline(i + topline);
62 
63 	/* get the file name and line number */
64 	if (fscanf(refsfound, "%" PATHLEN_STR "s%*s%" NUMLEN_STR "s", file, linenum) == 2) {
65 		edit(decode_path(file), linenum);	/* edit it */
66 	}
67 	seekline(topline);	/* restore the line pointer */
68 }
69 
70 /** edit all references */
71 
72 void
editall(void)73 editall(void)
74 {
75 	char	file[PATHLEN + 1];	/* file name */
76 	char	linenum[NUMLEN + 1];	/* line number */
77 	int	c;
78 
79 	/* verify that there is a references found file */
80 	if (refsfound == NULL) {
81 		return;
82 	}
83 	/* get the first line */
84 	seekline(1);
85 
86 	/* get each file name and line number */
87 	while (fscanf(refsfound, "%" PATHLEN_STR "s%*s%" NUMLEN_STR "s%*[^\n]", file, linenum) == 2) {
88 		edit(decode_path(file), linenum);	/* edit it */
89 		if (editallprompt == YES) {
90 			addstr("Type ^D to stop editing all lines, or any other character to continue: ");
91 			if ((c = mygetch()) == EOF || c == ctrl('D') || c == ctrl('Z')) {
92 				break;
93 			}
94 		}
95 	}
96 	seekline(topline);
97 }
98 
99 /** call the editor */
100 
101 void
edit(char * file,char * linenum)102 edit(char *file, char *linenum)
103 {
104 	char	msg[MSGLEN + 1];	/* message */
105 	char	plusnum[NUMLEN + 20];	/* line number option: allow space for wordy line# flag */
106 	char	*s;
107 
108 	file = filepath(file);
109 	(void) snprintf(msg, sizeof(msg), "%s +%s %s", mybasename(editor), linenum, file);
110 	postmsg(msg);
111 	(void) snprintf(plusnum, sizeof(plusnum), lineflag, linenum);
112 	/* if this is the more or page commands */
113 	if (strcmp(s = mybasename(editor), "more") == 0 || strcmp(s, "page") == 0) {
114 
115 		/* get it to pause after displaying a file smaller than the screen
116 		   length */
117 		(void) execute(editor, editor, plusnum, file, NULL_DEVICE, NULL);
118 	}
119 	else if (lineflagafterfile) {
120 		(void) execute(editor, editor, file, plusnum, NULL);
121 	}
122 	else {
123 		(void) execute(editor, editor, plusnum, file, NULL);
124 	}
125 	clear();	/* redisplay screen */
126 }
127 
128 /** if requested, prepend a path to a relative file name */
129 
130 char *
filepath(char * file)131 filepath(char *file)
132 {
133 	static	char	path[PATHLEN + 1];
134 
135 	if (prependpath != NULL && *file != '/') {
136 		(void) snprintf(path, sizeof(path), "%s/%s", prependpath, file);
137 		file = path;
138 	}
139 	return(file);
140 }
141