/* file: signum.c G. Moody 9 April 2008
-------------------------------------------------------------------------------
signum: Print signal names for signal numbers given as arguments
Copyright (C) 2008 George B. Moody
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, see .
You may contact the author by e-mail (wfdb@physionet.org) or postal mail
(MIT Room E25-505A, Cambridge, MA 02139 USA). For updates to this software,
please visit PhysioNet (http://www.physionet.org/).
_______________________________________________________________________________
*/
#include
#include
char *pname;
main(int argc, char **argv)
{
char *p, *record = NULL, *prog_name();
int i, isiglist = 0, j, nsig, nosig = 0;
WFDB_Siginfo *si;
void help();
pname = prog_name(argv[0]);
for (i = 1; i < argc; i++) {
if (*argv[i] == '-') switch (*(argv[i]+1)) {
case 'h': /* help requested */
help();
exit(0);
break;
case 'r': /* record name */
if (++i >= argc) {
(void)fprintf(stderr, "%s: record name must follow -r\n",
pname);
exit(1);
}
record = argv[i];
break;
case 's': /* signal list follows */
isiglist = i+1; /* index of 1st argument containing a signal # */
while (i+1 < argc && *argv[i+1] != '-') {
i++;
nosig++; /* number of elements in signal list */
}
if (nosig == 0) {
(void)fprintf(stderr, "%s: signal list must follow -s\n",
pname);
exit(1);
}
break;
default:
(void)fprintf(stderr, "%s: unrecognized option %s\n", pname,
argv[i]);
exit(1);
}
else {
(void)fprintf(stderr, "%s: unrecognized argument %s\n", pname,
argv[i]);
exit(1);
}
}
if (record == NULL) {
help();
exit(1);
}
if ((nsig = isigopen(record, NULL, 0)) <= 0) exit(2);
if ((si = malloc(nsig * sizeof(WFDB_Siginfo))) == NULL) {
(void)fprintf(stderr, "%s: insufficient memory\n", pname);
exit(2);
}
if ((nsig = isigopen(record, si, -nsig)) <= 0) exit(2);
if (isiglist)
for (i = 0; i < nosig; i++) {
j = atoi(argv[isiglist+i]);
if (0 <= j && j <= nsig) p = si[j].desc;
else p = "[INVALID]";
printf("%s\n", p);
}
else
for (i = 0; i < nsig; i++)
printf("%s\n", si[i].desc);
wfdbquit();
exit(0);
}
char *prog_name(s)
char *s;
{
char *p = s + strlen(s);
#ifdef MSDOS
while (p >= s && *p != '\\' && *p != ':') {
if (*p == '.')
*p = '\0'; /* strip off extension */
if ('A' <= *p && *p <= 'Z')
*p += 'a' - 'A'; /* convert to lower case */
p--;
}
#else
while (p >= s && *p != '/')
p--;
#endif
return (p+1);
}
static char *help_strings[] = {
"usage: %s -r RECORD [OPTIONS ...]\n",
"where RECORD is the name of the input record, and OPTIONS may include:",
" -h print this usage summary",
" -s SIGNAL [SIGNAL ...] print signal name(s) for the specified signal(s)",
"If no -s option is given, the names of all signals in the record are output.",
NULL
};
void help()
{
int i;
(void)fprintf(stderr, help_strings[0], pname);
for (i = 1; help_strings[i] != NULL; i++)
(void)fprintf(stderr, "%s\n", help_strings[i]);
}