2014-12-04 11:34:39 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-02-20 16:00:39 +00:00
|
|
|
#include <strings.h>
|
2014-12-04 11:34:39 +00:00
|
|
|
#define SYSLOG_NAMES
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-12-04 11:45:44 +00:00
|
|
|
static int
|
2014-12-09 15:44:31 +00:00
|
|
|
decodetable(CODE *table, char *name)
|
2014-12-04 11:34:39 +00:00
|
|
|
{
|
|
|
|
CODE *c;
|
|
|
|
|
2014-12-09 15:44:31 +00:00
|
|
|
for (c = table; c->c_name; c++)
|
|
|
|
if (!strcasecmp(name, c->c_name))
|
|
|
|
return c->c_val;
|
|
|
|
eprintf("invalid priority name: %s\n", name);
|
2015-03-06 23:08:43 +00:00
|
|
|
|
|
|
|
return -1; /* not reached */
|
2014-12-04 11:34:39 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 11:45:44 +00:00
|
|
|
static int
|
2014-12-04 11:34:39 +00:00
|
|
|
decodepri(char *pri)
|
|
|
|
{
|
2014-12-09 15:44:31 +00:00
|
|
|
char *lev, *fac = pri;
|
2014-12-04 11:34:39 +00:00
|
|
|
|
2014-12-09 15:44:31 +00:00
|
|
|
if (!(lev = strchr(pri, '.')))
|
2014-12-04 11:34:39 +00:00
|
|
|
eprintf("invalid priority name: %s\n", pri);
|
2014-12-09 15:44:31 +00:00
|
|
|
*lev++ = '\0';
|
|
|
|
if (!*lev)
|
2014-12-04 11:34:39 +00:00
|
|
|
eprintf("invalid priority name: %s\n", pri);
|
2015-03-06 23:08:43 +00:00
|
|
|
|
2014-12-09 15:44:31 +00:00
|
|
|
return (decodetable(facilitynames, fac) & LOG_FACMASK) |
|
|
|
|
(decodetable(prioritynames, lev) & LOG_PRIMASK);
|
2014-12-04 11:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-is] [-p priority] [-t tag] [message ...]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-03-06 23:08:43 +00:00
|
|
|
size_t sz;
|
|
|
|
int logflags = 0, priority = LOG_NOTICE, i;
|
2014-12-04 11:34:39 +00:00
|
|
|
char *buf = NULL, *tag = NULL;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'i':
|
|
|
|
logflags |= LOG_PID;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
priority = decodepri(EARGF(usage()));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
logflags |= LOG_PERROR;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
tag = EARGF(usage());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
openlog(tag ? tag : getlogin(), logflags, 0);
|
|
|
|
|
2015-03-06 23:08:43 +00:00
|
|
|
if (!argc) {
|
|
|
|
while (getline(&buf, &sz, stdin) >= 0)
|
2014-12-04 11:34:39 +00:00
|
|
|
syslog(priority, "%s", buf);
|
|
|
|
if (ferror(stdin))
|
2015-03-06 23:08:43 +00:00
|
|
|
eprintf("getline %s:", "<stdin>");
|
2014-12-04 11:34:39 +00:00
|
|
|
} else {
|
2015-03-06 23:08:43 +00:00
|
|
|
for (i = 0, sz = 0; i < argc; i++)
|
2014-12-04 11:34:39 +00:00
|
|
|
sz += strlen(argv[i]);
|
|
|
|
sz += argc;
|
|
|
|
buf = ecalloc(1, sz);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
strlcat(buf, argv[i], sz);
|
|
|
|
if (i + 1 < argc)
|
|
|
|
strlcat(buf, " ", sz);
|
|
|
|
}
|
|
|
|
syslog(priority, "%s", buf);
|
|
|
|
}
|
2015-03-06 23:08:43 +00:00
|
|
|
|
2014-12-04 11:34:39 +00:00
|
|
|
closelog();
|
2015-03-06 23:08:43 +00:00
|
|
|
|
2014-12-04 11:34:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|