1/* operand2sig.c -- common function for parsing signal specifications
2   Copyright (C) 2008-2010 Free Software Foundation, Inc.
3
4   This program is free software: you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation, either version 3 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17/* Extracted from kill.c/timeout.c by Pádraig Brady.
18   FIXME: Move this to gnulib/str2sig.c */
19
20
21/* Convert OPERAND to a signal number with printable representation SIGNAME.
22   Return the signal number, or -1 if unsuccessful.  */
23
24#include <config.h>
25#include <stdio.h>
26#include <sys/types.h>
27
28#if HAVE_SYS_WAIT_H
29# include <sys/wait.h>
30#endif
31#ifndef WIFSIGNALED
32# define WIFSIGNALED(s) (((s) & 0xFFFF) - 1 < (unsigned int) 0xFF)
33#endif
34#ifndef WTERMSIG
35# define WTERMSIG(s) ((s) & 0x7F)
36#endif
37
38#include "system.h"
39#include "error.h"
40#include "sig2str.h"
41#include "operand2sig.h"
42
43extern int
44operand2sig (char const *operand, char *signame)
45{
46  int signum;
47
48  if (ISDIGIT (*operand))
49    {
50      char *endp;
51      long int l = (errno = 0, strtol (operand, &endp, 10));
52      int i = l;
53      signum = (operand == endp || *endp || errno || i != l ? -1
54                : WIFSIGNALED (i) ? WTERMSIG (i) : i);
55    }
56  else
57    {
58      /* Convert signal to upper case in the C locale, not in the
59         current locale.  Don't assume ASCII; it might be EBCDIC.  */
60      char *upcased = xstrdup (operand);
61      char *p;
62      for (p = upcased; *p; p++)
63        if (strchr ("abcdefghijklmnopqrstuvwxyz", *p))
64          *p += 'A' - 'a';
65
66      /* Look for the signal name, possibly prefixed by "SIG",
67         and possibly lowercased.  */
68      if (!(str2sig (upcased, &signum) == 0
69            || (upcased[0] == 'S' && upcased[1] == 'I' && upcased[2] == 'G'
70                && str2sig (upcased + 3, &signum) == 0)))
71        signum = -1;
72
73      free (upcased);
74    }
75
76  if (signum < 0 || sig2str (signum, signame) != 0)
77    {
78      error (0, 0, _("%s: invalid signal"), operand);
79      return -1;
80    }
81
82  return signum;
83}
84