- Timestamp:
- Oct 7, 2007 7:13:10 PM (17 years ago)
- Location:
- trunk/src/kash
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/bltin/kill.c
r1207 r1213 47 47 48 48 #include <ctype.h> 49 #ifndef __sun__50 #include <err.h>51 #endif52 49 #include <errno.h> 53 #include <signal.h>54 50 #include <stdio.h> 55 51 #include <stdlib.h> 56 52 #include <string.h> 57 #include <termios.h> 58 #include <unistd.h> 59 #include <locale.h> 60 #include <sys/ioctl.h> 53 #include "jobs.h" 54 #include "error.h" 55 #include "shinstance.h" 61 56 62 57 #ifndef HAVE_SYS_SIGNAME … … 65 60 #endif 66 61 67 #ifdef SHELL /* sh (aka ash) builtin */ 68 #define main killcmd 69 #include "bltin/bltin.h" 70 #include "jobs.h" 71 #endif /* SHELL */ 72 73 static void nosig(char *); 74 static void printsignals(FILE *); 62 static int nosig(shinstance *, char *); 63 static void printsignals(shinstance *, struct output *); 75 64 static int signame_to_signum(char *); 76 static void usage(void); 77 int main(int, char *[]); 65 static int usage(shinstance *psh); 78 66 79 67 int 80 main(int argc, char *argv[])68 killcmd(shinstance *psh, int argc, char *argv[]) 81 69 { 82 70 int errors, numsig, pid; 83 71 char *ep; 84 72 85 setprogname(argv[0]);86 setlocale(LC_ALL, "");87 73 if (argc < 2) 88 usage();74 return usage(psh); 89 75 90 76 numsig = SIGTERM; 77 #ifndef HAVE_SYS_SIGNAME 78 init_sys_signame(); 79 #endif 91 80 92 81 argc--, argv++; … … 94 83 argc--, argv++; 95 84 if (argc > 1) 96 usage();85 return usage(psh); 97 86 if (argc == 1) { 98 87 if (isdigit((unsigned char)**argv) == 0) 99 usage();88 return usage(psh); 100 89 numsig = strtol(*argv, &ep, 10); 101 90 if (*ep != '\0') { 102 errx(EXIT_FAILURE, "illegal signal number: %s",91 sh_errx(psh, EXIT_FAILURE, "illegal signal number: %s", 103 92 *argv); 104 93 /* NOTREACHED */ … … 107 96 numsig -= 128; 108 97 if (numsig <= 0 || numsig >= NSIG) 109 nosig(*argv); 110 #ifndef HAVE_SYS_SIGNAME 111 init_sys_signame(); 112 #endif 113 printf("%s\n", sys_signame[numsig]); 114 exit(0); 115 } 116 printsignals(stdout); 117 exit(0); 98 return nosig(psh, *argv); 99 outfmt(psh->out1, "%s\n", sys_signame[numsig]); 100 //sh_exit(psh, 0); 101 return 0; 102 } 103 printsignals(psh, psh->out1); 104 //sh_exit(psh, 0); 105 return 0; 118 106 } 119 107 … … 121 109 argc--, argv++; 122 110 if (argc < 1) { 123 warnx("option requires an argument -- s");124 usage();111 sh_warnx(psh, "option requires an argument -- s"); 112 return usage(psh); 125 113 } 126 114 if (strcmp(*argv, "0")) { 127 115 if ((numsig = signame_to_signum(*argv)) < 0) 128 nosig(*argv);116 return nosig(psh, *argv); 129 117 } else 130 118 numsig = 0; … … 134 122 if (isalpha((unsigned char)**argv)) { 135 123 if ((numsig = signame_to_signum(*argv)) < 0) 136 nosig(*argv);124 return nosig(psh, *argv); 137 125 } else if (isdigit((unsigned char)**argv)) { 138 126 numsig = strtol(*argv, &ep, 10); 139 127 if (!*argv || *ep) { 140 errx(EXIT_FAILURE, "illegal signal number: %s",128 sh_errx(psh, EXIT_FAILURE, "illegal signal number: %s", 141 129 *argv); 142 130 /* NOTREACHED */ 143 131 } 144 132 if (numsig < 0 || numsig >= NSIG) 145 nosig(*argv);133 return nosig(psh, *argv); 146 134 } else 147 nosig(*argv);135 return nosig(psh, *argv); 148 136 argc--, argv++; 149 137 } 150 138 151 139 if (argc == 0) 152 usage();140 return usage(psh); 153 141 154 142 for (errors = 0; argc; argc--, argv++) { 155 #ifdef SHELL156 143 if (*argv[0] == '%') { 157 144 pid = getjobpgrp(psh, *argv); 158 145 if (pid == 0) { 159 warnx("illegal job id: %s", *argv);146 sh_warnx(psh, "illegal job id: %s", *argv); 160 147 errors = 1; 161 148 continue; 162 149 } 163 } else 164 #endif 165 { 150 } else { 166 151 pid = strtol(*argv, &ep, 10); 167 152 if (!**argv || *ep) { 168 warnx("illegal process id: %s", *argv);153 sh_warnx(psh, "illegal process id: %s", *argv); 169 154 errors = 1; 170 155 continue; 171 156 } 172 157 } 173 if ( kill(pid, numsig) == -1) {174 warn("%s", *argv);158 if (sh_kill(psh, pid, numsig) == -1) { 159 sh_warn(psh, "%s", *argv); 175 160 errors = 1; 176 161 } 177 #ifdef SHELL178 162 /* Wakeup the process if it was suspended, so it can 179 163 exit without an explicit 'fg'. */ 180 164 if (numsig == SIGTERM || numsig == SIGHUP) 181 kill(pid, SIGCONT);182 #endif 183 } 184 185 exit(errors);186 /* NOTREACHED */165 sh_kill(psh, pid, SIGCONT); 166 } 167 168 //sh_exit(psh, errors); 169 ///* NOTREACHED */ 170 return errors; 187 171 } 188 172 … … 191 175 { 192 176 int n; 193 #ifndef HAVE_SYS_SIGNAME194 init_sys_signame();195 #endif196 177 if (strncasecmp(sig, "sig", 3) == 0) 197 178 sig += 3; … … 203 184 } 204 185 186 static int 187 nosig(shinstance *psh, char *name) 188 { 189 sh_warnx(psh, "unknown signal %s; valid signals:", name); 190 printsignals(psh, psh->out2); 191 //sh_exit(psh, 1); 192 ///* NOTREACHED */ 193 return 1; 194 } 195 205 196 static void 206 nosig(char *name) 207 { 208 209 warnx("unknown signal %s; valid signals:", name); 210 printsignals(stderr); 211 exit(1); 212 /* NOTREACHED */ 213 } 214 215 static void 216 printsignals(FILE *fp) 197 printsignals(shinstance *psh, struct output *out) 217 198 { 218 199 int sig; 219 int len, nl;200 size_t len, nl; 220 201 const char *name; 221 202 int termwidth = 80; 222 203 223 #ifdef TIOCGWINSZ 224 if (isatty(fileno(fp))) { 225 struct winsize win; 226 if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0) 204 if (shfile_isatty(&psh->fdtab, out->fd)) { 205 sh_winsize win; 206 if (shfile_ioctl(&psh->fdtab, out->fd, TIOCGWINSZ, &win) == 0 && win.ws_col > 0) 227 207 termwidth = win.ws_col; 228 208 } 229 #else230 #ifndef _MSC_VER231 #warning TIOCGWINSZ is not present.232 #endif233 #endif234 #ifndef HAVE_SYS_SIGNAME235 init_sys_signame();236 #endif237 209 238 210 for (len = 0, sig = 1; sig < NSIG; sig++) { … … 241 213 242 214 if (len + nl >= termwidth) { 243 fprintf(fp, "\n");215 outfmt(out, "\n"); 244 216 len = 0; 245 } else 246 if (len != 0) 247 fprintf(fp, " "); 217 } else if (len != 0) 218 outfmt(out, " "); 248 219 len += nl; 249 fprintf(fp, "%s", name);220 outfmt(out, "%s", name); 250 221 } 251 222 if (len != 0) 252 fprintf(fp, "\n");253 } 254 255 static void256 usage( void)257 { 258 259 fprintf(stderr,"usage: %s [-s signal_name] pid ...\n"223 outfmt(out, "\n"); 224 } 225 226 static int 227 usage(shinstance *psh) 228 { 229 outfmt(psh->out2, 230 "usage: %s [-s signal_name] pid ...\n" 260 231 " %s -l [exit_status]\n" 261 232 " %s -signal_name pid ...\n" 262 233 " %s -signal_number pid ...\n", 263 getprogname(), getprogname(), getprogname(), getprogname()); 264 exit(1); 265 /* NOTREACHED */ 266 } 234 psh->commandname, psh->commandname, psh->commandname, psh->commandname); 235 //sh_exit(psh, 1); 236 ///* NOTREACHED */ 237 return 1; 238 } -
trunk/src/kash/error.h
r881 r1213 34 34 * @(#)error.h 8.2 (Berkeley) 5/4/95 35 35 */ 36 37 #ifndef ___error_h 38 #define ___error_h 36 39 37 40 #include <stdarg.h> … … 117 120 #define longjmp(jmploc, val) _longjmp(jmploc, val) 118 121 #endif 122 123 #endif -
trunk/src/kash/shfile.h
r1207 r1213 114 114 char *shfile_getcwd(shfdtab *, char *, int); 115 115 int shfile_isatty(shfdtab *, int); 116 int shfile_ioctl(shfdtab *, int, unsigned long, char*);116 int shfile_ioctl(shfdtab *, int, unsigned long, void *); 117 117 int shfile_access(shfdtab *, const char *, int); 118 #ifdef _MSC_VER 119 # define TIOCGWINSZ 0x4201 120 typedef struct sh_winsize 121 { 122 unsigned ws_row; /**< Rows, in characters. */ 123 unsigned ws_col; /**< Columns, in characters. */ 124 unsigned ws_xpixel; /**< Horizontal size, pixels. */ 125 unsigned ws_ypixel; /**< Vertical size, pixels. */ 126 } sh_winsize; 127 #else 128 typedef struct winsize sh_winsize; 129 #endif 118 130 119 131 #endif -
trunk/src/kash/shinstance.h
r1211 r1213 25 25 */ 26 26 27 #ifndef ___shinstance_h ___28 #define ___shinstance_h ___27 #ifndef ___shinstance_h 28 #define ___shinstance_h 29 29 30 30 #include <stdio.h> /* BUFSIZ */ 31 31 #include <signal.h> /* NSIG */ 32 #ifndef _MSC_VER 33 # include <termios.h> 34 # include <sys/ioctl.h> 35 #endif 32 36 33 37 #include "shtypes.h" … … 40 44 #include "exec.h" 41 45 #include "var.h" 46 47 #ifdef _MSC_VER 48 # define strcasecmp stricmp 49 # define strncasecmp strnicmp 50 #endif 51 42 52 43 53 /* memalloc.c */ … … 330 340 #define SH_SIG_DFL ((sh_sig_t)SIG_DFL) 331 341 #define SH_SIG_IGN ((sh_sig_t)SIG_IGN) 342 #ifdef _MSC_VER 343 # define SIG_BLOCK 1 344 # define SIG_UNBLOCK 2 345 # define SIG_SETMASK 3 346 # define SIGHUP 5 347 # define SIGQUIT 9 348 # define SIGPIPE 12 349 # define SIGTTOU 17 350 # define SIGTSTP 18 351 # define SIGTTIN 19 352 # define SIGCONT 20 353 #endif /* _MSC_VER */ 332 354 333 355 int sh_sigaction(int, const struct sh_sigaction *, struct sh_sigaction *); … … 389 411 pid_t sh_getpgid(shinstance *, pid_t); 390 412 int sh_setpgid(shinstance *, pid_t, pid_t); 413 int sh_kill(shinstance *, pid_t, int); 391 414 int sh_killpg(shinstance *, pid_t, int); 392 415 -
trunk/src/kash/sys_signame.c
r813 r1213 1 1 /* 2 * Fake sys_signame. 2 * Fake sys_signame. 3 3 */ 4 4 5 #include <signal.h>5 #include "shinstance.h" /* for MSC */ 6 6 #include <string.h> 7 7 #include <stdio.h> … … 9 9 static char sys_signame_initialized = 0; 10 10 char sys_signame[NSIG][16]; 11 11 12 12 void init_sys_signame(void) 13 13 { 14 14 unsigned i; 15 15 if (sys_signame_initialized) 16 17 18 16 return; 17 for (i = 0; i < NSIG; ++i) 18 sprintf(sys_signame[i], "%d", i); 19 19 #define SET_SIG_STR(sig) strcpy(sys_signame[SIG##sig], #sig); 20 20 #ifdef SIGHUP 21 22 #endif 21 SET_SIG_STR(HUP); 22 #endif 23 23 #ifdef SIGINT 24 25 #endif 24 SET_SIG_STR(INT); 25 #endif 26 26 #ifdef SIGQUIT 27 28 #endif 27 SET_SIG_STR(QUIT); 28 #endif 29 29 #ifdef SIGILL 30 31 #endif 30 SET_SIG_STR(ILL); 31 #endif 32 32 #ifdef SIGTRAP 33 34 #endif 33 SET_SIG_STR(TRAP); 34 #endif 35 35 #ifdef SIGABRT 36 37 #endif 36 SET_SIG_STR(ABRT); 37 #endif 38 38 #ifdef SIGIOT 39 40 #endif 39 SET_SIG_STR(IOT); 40 #endif 41 41 #ifdef SIGBUS 42 43 #endif 42 SET_SIG_STR(BUS); 43 #endif 44 44 #ifdef SIGFPE 45 46 #endif 45 SET_SIG_STR(FPE); 46 #endif 47 47 #ifdef SIGKILL 48 49 #endif 48 SET_SIG_STR(KILL); 49 #endif 50 50 #ifdef SIGUSR1 51 52 #endif 51 SET_SIG_STR(USR1); 52 #endif 53 53 #ifdef SIGSEGV 54 55 #endif 54 SET_SIG_STR(SEGV); 55 #endif 56 56 #ifdef SIGUSR2 57 58 #endif 57 SET_SIG_STR(USR2); 58 #endif 59 59 #ifdef SIGPIPE 60 61 #endif 60 SET_SIG_STR(PIPE); 61 #endif 62 62 #ifdef SIGALRM 63 64 #endif 63 SET_SIG_STR(ALRM); 64 #endif 65 65 #ifdef SIGTERM 66 67 #endif 66 SET_SIG_STR(TERM); 67 #endif 68 68 #ifdef SIGSTKFLT 69 70 #endif 69 SET_SIG_STR(STKFLT); 70 #endif 71 71 #ifdef SIGCHLD 72 73 #endif 72 SET_SIG_STR(CHLD); 73 #endif 74 74 #ifdef SIGCONT 75 76 #endif 75 SET_SIG_STR(CONT); 76 #endif 77 77 #ifdef SIGSTOP 78 79 #endif 78 SET_SIG_STR(STOP); 79 #endif 80 80 #ifdef SIGTSTP 81 82 #endif 81 SET_SIG_STR(TSTP); 82 #endif 83 83 #ifdef SIGTTIN 84 85 #endif 84 SET_SIG_STR(TTIN); 85 #endif 86 86 #ifdef SIGTTOU 87 88 #endif 87 SET_SIG_STR(TTOU); 88 #endif 89 89 #ifdef SIGURG 90 91 #endif 90 SET_SIG_STR(URG); 91 #endif 92 92 #ifdef SIGXCPU 93 94 #endif 93 SET_SIG_STR(XCPU); 94 #endif 95 95 #ifdef SIGXFSZ 96 97 #endif 96 SET_SIG_STR(XFSZ); 97 #endif 98 98 #ifdef SIGVTALRM 99 100 #endif 99 SET_SIG_STR(VTALRM); 100 #endif 101 101 #ifdef SIGPROF 102 103 #endif 102 SET_SIG_STR(PROF); 103 #endif 104 104 #ifdef SIGWINCH 105 106 #endif 105 SET_SIG_STR(WINCH); 106 #endif 107 107 #ifdef SIGIO 108 109 #endif 108 SET_SIG_STR(IO); 109 #endif 110 110 #ifdef SIGPWR 111 112 #endif 111 SET_SIG_STR(PWR); 112 #endif 113 113 #ifdef SIGSYS 114 115 #endif 114 SET_SIG_STR(SYS); 115 #endif 116 116 #ifdef SIGBREAK 117 118 #endif 117 SET_SIG_STR(BREAK); 118 #endif 119 119 #undef SET_SIG_STR 120 120 sys_signame_initialized = 1; 121 121 }
Note:
See TracChangeset
for help on using the changeset viewer.