2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: trap.c /main/4 1996/08/16 15:31:57 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF *
29 * AT&T BELL LABORATORIES *
30 * AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN *
31 * ACCORDANCE WITH APPLICABLE AGREEMENTS *
33 * Copyright (c) 1995 AT&T Corp. *
34 * Unpublished & Not for Publication *
35 * All Rights Reserved *
37 * The copyright notice above does not evidence any *
38 * actual or intended publication of such source code *
40 * This software was created by the *
41 * Advanced Software Technology Department *
42 * AT&T Bell Laboratories *
44 * For further information contact *
45 * {research,attmail}!dgk *
47 ***************************************************************/
49 /* : : generated by proto : : */
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__ "C"
59 #define __PROTO__(x) x
61 #define __PARAM__(n,o) n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
74 #define __PROTO__(x) ()
75 #define __OTORP__(x) x
76 #define __PARAM__(n,o) o
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__ ...
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a) va_start(p,a)
92 #define __VA_START__(p,a) va_start(p)
103 static const char trapfmt[] = "trap -- %s %s\n";
105 static int sig_number __PROTO__((const char*));
106 static void sig_list __PROTO__((int));
108 int b_trap __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
109 register char *arg = argv[1];
110 register int sig, pflag = 0;
113 while (sig = optget(argv, sh_opttrap)) switch (sig)
122 error(ERROR_usage(2), opt_arg);
126 if(error_info.errors)
127 error(ERROR_usage(2),optusage((char*)0));
134 /* first argument all digits or - means clear */
137 clear = (arg!=action && *arg==0);
141 if(*action=='-' && action[1]==0)
145 error(ERROR_exit(1),e_condition);
149 sig = sig_number(arg);
166 if(arg=sh.st.trap[sig])
167 sfputr(sfstdout,sh_fmtq(arg),'\n');
171 free(sh.st.trap[sig]);
173 if(!clear && *action)
174 sh.st.trap[sig] = strdup(action);
175 if(sig == SH_DEBUGTRAP)
178 sh.trapnote |= SH_SIGTRAP;
191 if(arg=sh.st.trapcom[sig])
192 sfputr(sfstdout,sh_fmtq(arg),'\n');
198 if(sig >= sh.st.trapmax)
199 sh.st.trapmax = sig+1;
200 if(arg=sh.st.trapcom[sig])
202 sh.st.trapcom[sig] = strdup(action);
207 else /* print out current traps */
212 int b_kill __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
213 register char *signame;
214 register int sig=SIGTERM, flag=0, n;
217 while((n = optget(argv,sh_optkill))) switch(n)
220 if((signame=argv[opt_index++]) && (sig=sig_number(signame+1))>=0)
222 if(argv[opt_index] && strcmp(argv[opt_index],"--")==0)
240 error(ERROR_usage(2), opt_arg);
245 if(error_info.errors || flag==(L_FLAG|S_FLAG) || (!(*argv) && !(flag&L_FLAG)))
246 error(ERROR_usage(2),optusage((char*)0));
247 /* just in case we send a kill -9 $$ */
253 else while(signame = *argv++)
255 if(isdigit(*signame))
256 sig_list((atoi(signame)&0177)+1);
259 if((sig=sig_number(signame))<0)
262 error(ERROR_exit(1),e_nosignal,signame);
264 sfprintf(sfstdout,"%d\n",sig);
271 if((sig=sig_number(signame)) < 0 || sig > sh.sigmax)
272 error(ERROR_exit(1),e_nosignal,signame);
274 if(job_walk(sfstdout,job_kill,sig,argv))
280 * Given the name or number of a signal return the signal number
283 static int sig_number __PARAM__((const char *string), (string)) __OTORP__(const char *string;){
288 n = strtol(string,&last,10);
305 init_shtab_signals();
306 n = sh_lookup(stakptr(n),shtab_signals);
307 n &= (1<<SH_SIGBITS)-1;
315 * if <flag> is positive, then print signal name corresponding to <flag>
316 * if <flag> is zero, then print all signal names
317 * if <flag> is negative, then print all traps
319 static void sig_list __PARAM__((register int flag), (flag)) __OTORP__(register int flag;){
320 register const struct shtable2 *tp;
321 register int sig = sh.sigmax+1;
322 const char *names[SH_TRAP];
323 const char *traps[SH_DEBUGTRAP+1];
325 init_shtab_signals();
330 /* not all signals may be defined, so initialize */
333 for(sig=SH_DEBUGTRAP; sig>=0; sig--)
339 sig &= ((1<<SH_SIGBITS)-1);
342 sfprintf(sfstdout,"%s\n",tp->sh_name);
346 traps[sig&~SH_TRAP] = (char*)tp->sh_name;
347 else if(sig < sizeof(names)/sizeof(char*))
348 names[sig] = (char*)tp->sh_name;
352 sfprintf(sfstdout,"%d\n",flag-1);
355 /* print the traps */
356 register char *trap,*sname,**trapcom;
358 /* use parent traps if otrapcom is set (for $(trap) */
359 trapcom = (sh.st.otrapcom?sh.st.otrapcom:sh.st.trapcom);
362 if(!(trap=trapcom[sig]))
364 if(!(sname=(char*)names[sig+1]))
367 sname[3] = (sig/10)+'0';
368 sname[4] = (sig%10)+'0';
370 sfprintf(sfstdout,trapfmt,sh_fmtq(trap),sname);
372 for(sig=SH_DEBUGTRAP; sig>=0; sig--)
374 if(!(trap=sh.st.trap[sig]))
376 sfprintf(sfstdout,trapfmt,sh_fmtq(trap),traps[sig]);
381 /* print all the signal names */
382 for(sig=2; sig <= sh.sigmax; sig++)
385 sfputr(sfstdout,names[sig],'\n');
387 sfprintf(sfstdout,"SIG%d\n",sig-1);