Imported Upstream version 15.8a
[platform/upstream/cscope.git] / src / exec.c
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 /*      cscope - interactive C symbol cross-reference
34  *
35  *      process execution functions
36  */
37
38 #include <unistd.h>
39 #include "global.h"
40 #include <stdarg.h>
41 #include <sys/wait.h>
42 #include <sys/types.h>      /* pid_t */
43 #ifdef __DJGPP__
44 #include <process.h>
45 #endif
46 #if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
47 #include <ncurses.h>
48 #else
49 #include <curses.h>
50 #endif
51
52 static char const rcsid[] = "$Id: exec.c,v 1.12 2009/04/10 13:39:23 broeker Exp $";
53
54 static  sighandler_t oldsigquit; /* old value of quit signal */
55 static  sighandler_t oldsighup; /* old value of hangup signal */
56 static  sighandler_t oldsigtstp; /* old value of SIGTSTP */
57
58 #ifndef __MSDOS__ /* none of these is needed, there */
59 static  int     join(pid_t p);
60 static  int     myexecvp(char *a, char **args);
61 static  pid_t   myfork(void);
62 #endif
63
64 /* execute forks and executes a program or shell script, waits for it to
65  * finish, and returns its exit code.
66  */
67
68 /*VARARGS1*/
69 int
70 execute(char *a, ...)   /* note: "exec" is already defined on u370 */
71 {
72         va_list ap;
73         int     exitcode = -1;  /* initialize, to avoid warning */
74         char    *argv[BUFSIZ];
75         pid_t   p;
76
77         /* fork and exec the program or shell script */
78         endwin();       /* restore the terminal modes */
79         mousecleanup();
80         fflush(stdout);
81         va_start(ap, a);
82         for (p = 0; (argv[p] = va_arg(ap, char *)) != 0; p++)
83                 ;
84 #ifdef __MSDOS__
85         /* HBB 20010313: in MSDOG, everything is completely different.
86          * No fork()/exec()/wait(), but rather a single libc call: */
87         exitcode = spawnvp(P_WAIT, a, argv);
88 #else
89         if ((p = myfork()) == 0) {
90                 myexecvp(a, argv);      /* child */
91         }
92         else {
93                 exitcode = join(p);     /* parent */
94         }
95 #endif /* MSDOS */
96         
97         /* the menu and scrollbar may be changed by the command executed */
98 #if UNIXPC || !TERMINFO
99 # ifndef __DJGPP__ /* leave CRLF handling as is */      
100         nonl();
101 # endif
102         raw();  /* endwin() turns off cbreak mode so restore it */
103         noecho();
104 #endif
105         mousemenu();
106         drawscrollbar(topline, nextline);
107         va_end(ap);
108         return(exitcode);
109 }
110
111 #ifndef __MSDOS__ /* None of the following functions is used there */
112
113 /* myexecvp is an interface to the execvp system call to
114  * modify argv[0] to reference the last component of its path-name.
115  */
116 static int
117 myexecvp(char *a, char **args)
118 {
119     char    msg[MSGLEN + 1];
120         
121     /* modify argv[0] to reference the last component of its path name */
122     args[0] = mybasename(args[0]);
123
124     /* execute the program or shell script */
125     execvp(a, args);    /* returns only on failure */
126     snprintf(msg, sizeof(msg), "\nCannot exec %s", a);
127     perror(msg);                /* display the reason */
128     askforreturn();             /* wait until the user sees the message */
129     myexit(1);          /* exit the child */
130     /* NOTREACHED */
131 }
132
133 /* myfork acts like fork but also handles signals */
134
135 static pid_t
136 myfork(void)
137 {
138         pid_t   p;              /* process number */
139
140         p = fork();
141         
142         /* the parent ignores the interrupt, quit, and hangup signals */
143         if (p > 0) {
144                 oldsigquit = signal(SIGQUIT, SIG_IGN);
145                 oldsighup = signal(SIGHUP, SIG_IGN);
146 #ifdef SIGTSTP          
147                 oldsigtstp = signal(SIGTSTP, SIG_DFL);
148 #endif          
149         }
150         /* so they can be used to stop the child */
151         else if (p == 0) {
152                 signal(SIGINT, SIG_DFL);
153                 signal(SIGQUIT, SIG_DFL);
154                 signal(SIGHUP, SIG_DFL);
155 #ifdef SIGTSTP
156                 signal(SIGTSTP, SIG_DFL);
157 #endif                  
158         }
159         /* check for fork failure */
160         if (p == -1) {
161                 myperror("Cannot fork");
162         }
163         return p;
164 }
165
166 /* join is the compliment of fork */
167
168 static int
169 join(pid_t p) 
170 {
171         int     status;  
172         pid_t   w;
173
174         /* wait for the correct child to exit */
175         do {
176                 w = wait(&status);
177         } while (p != -1 && w != p);
178
179         /* restore signal handling */
180         signal(SIGQUIT, oldsigquit);
181         signal(SIGHUP, oldsighup);
182 #ifdef SIGTSTP
183         signal(SIGTSTP, oldsigtstp);
184 #endif  
185
186         /* return the child's exit code */
187         return(status >> 8);
188 }
189
190 #endif /* !MSDOS */