Re: [Toybox] [PATCH] logger.c was failing to properly log local0-local6 facilities, due to a string parsing error. This patch enables proper local facility handling.

2020-09-09 Thread Chris Sarra via Toybox
Just ran it through a short suite of tests on my end and all looks good
here.
Thanks for the cleanup!
+Chris


On Wed, Sep 9, 2020 at 12:57 AM Rob Landley  wrote:

>
>
> On 9/9/20 12:30 AM, Rob Landley wrote:
> > On 9/8/20 2:16 PM, Chris Sarra via Toybox wrote:
> >> ---
> >>  toys/posix/logger.c | 7 ---
> >>  1 file changed, 4 insertions(+), 3 deletions(-)
> >>
> >> diff --git a/toys/posix/logger.c b/toys/posix/logger.c
> >> index 3bcfb174..d1cc7291 100644
> >> --- a/toys/posix/logger.c
> >> +++ b/toys/posix/logger.c
> >> @@ -64,9 +64,10 @@ void logger_main(void)
> >>  else {
> >>*s1++ = len = 0;
> >>facility = arrayfind(TT.p, facilities, ARRAY_LEN(facilities));
> >> -  if (facility == -1 && strncasecmp(TT.p, "local", 5)) {
> >> -facility = s1[5]-'0';
> >> -if (facility>7 || s1[6]) facility = -1;
> >> +  if (facility == -1 && strncasecmp(TT.p, "local", 5) == 0) {
> >> +s2 = TT.p;
> >> +facility = s2[5]-'0';
> >> +if (facility>7 || s2[6]) facility = -1;
> >
> > Sigh, why did I promote this out of pending? arrayfind() initializes
> matchlen to
> > 0 and then never sets it to anything ELSE, so it ONLY returns exact
> matches not
> > longest unambiguous match (which is the point of the function I think?)
> >
> > Applied your patch, but I have some cleanup to do to this command...
>
> I did the cleanup but I have no tests/logger.test, so I dunno if I broke
> it. (It
> survived obvious smoketesting, but...?)
>
> Could you try the attached and see if it works for you?
>
> Thanks,
>
> Rob
>
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] logger.c was failing to properly log local0-local6 facilities, due to a string parsing error. This patch enables proper local facility handling.

2020-09-09 Thread Rob Landley


On 9/9/20 12:30 AM, Rob Landley wrote:
> On 9/8/20 2:16 PM, Chris Sarra via Toybox wrote:
>> ---
>>  toys/posix/logger.c | 7 ---
>>  1 file changed, 4 insertions(+), 3 deletions(-)
>>
>> diff --git a/toys/posix/logger.c b/toys/posix/logger.c
>> index 3bcfb174..d1cc7291 100644
>> --- a/toys/posix/logger.c
>> +++ b/toys/posix/logger.c
>> @@ -64,9 +64,10 @@ void logger_main(void)
>>  else {
>>*s1++ = len = 0;
>>facility = arrayfind(TT.p, facilities, ARRAY_LEN(facilities));
>> -  if (facility == -1 && strncasecmp(TT.p, "local", 5)) {
>> -facility = s1[5]-'0';
>> -if (facility>7 || s1[6]) facility = -1;
>> +  if (facility == -1 && strncasecmp(TT.p, "local", 5) == 0) {
>> +s2 = TT.p;
>> +facility = s2[5]-'0';
>> +if (facility>7 || s2[6]) facility = -1;
> 
> Sigh, why did I promote this out of pending? arrayfind() initializes matchlen 
> to
> 0 and then never sets it to anything ELSE, so it ONLY returns exact matches 
> not
> longest unambiguous match (which is the point of the function I think?)
> 
> Applied your patch, but I have some cleanup to do to this command...

I did the cleanup but I have no tests/logger.test, so I dunno if I broke it. (It
survived obvious smoketesting, but...?)

Could you try the attached and see if it works for you?

Thanks,

Rob
/* logger.c - Log messages.
 *
 * Copyright 2013 Ilya Kuzmich 
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/logger.html
 *
 * Deviations from posix: specified manner and format, defined implementation.

USE_LOGGER(NEWTOY(logger, "t:p:s", TOYFLAG_USR|TOYFLAG_BIN))

config LOGGER
  bool "logger"
  default y
  help
usage: logger [-s] [-t TAG] [-p [FACILITY.]PRIORITY] [MESSAGE...]

Log message (or stdin) to syslog.

-s	Also write message to stderr
-t	Use TAG instead of username to identify message source
-p	Specify PRIORITY with optional FACILITY. Default is "user.notice"
*/

#define FOR_logger
#include "toys.h"

GLOBALS(
  char *p, *t;
)

// find str in names[], accepting unambiguous short matches
// returns offset into array of match, or -1 if no match
int arrayfind(char *str, char *names[], int len)
{
  int j, i, ll = 0, maybe = -1;

  for (j = 0; jll) maybe = j;
  else if (i==ll) maybe = -1;
  break;
}
if (!names[j][i] || toupper(str[i])!=toupper(names[j][i])) break;
  }

  return maybe;
}

void logger_main(void)
{
  int facility = LOG_USER, priority = LOG_NOTICE, len = 0;
  char *s1, *s2, **arg,
*priorities[] = {"emerg", "alert", "crit", "error", "warning", "notice",
 "info", "debug"},
*facilities[] = {"kern", "user", "mail", "daemon", "auth", "syslog",
 "lpr", "news", "uucp", "cron", "authpriv", "ftp"};

  if (!TT.t) TT.t = xgetpwuid(geteuid())->pw_name;
  if (TT.p) {
if (!(s1 = strchr(TT.p, '.'))) s1 = TT.p;
else {
  *s1++ = 0;
  facility = arrayfind(TT.p, facilities, ARRAY_LEN(facilities));
  if (facility<0) {
if (sscanf(TT.p, "local%d", )>0 && !(facility&~7))
  facility += 16;
else error_exit("bad facility: %s", TT.p);
  }
  facility *= 8;
}

priority = arrayfind(s1, priorities, ARRAY_LEN(priorities));
if (priority<0) error_exit("bad priority: %s", s1);
  }

  if (toys.optc) {
for (arg = toys.optargs; *arg; arg++) len += strlen(*arg)+1;
s1 = s2 = xmalloc(len);
for (arg = toys.optargs; *arg; arg++) {
  if (arg != toys.optargs) *s2++ = ' ';
  s2 = stpcpy(s2, *arg);
}
  } else toybuf[readall(0, s1 = toybuf, sizeof(toybuf)-1)] = 0;

  openlog(TT.t, LOG_PERROR*FLAG(s), facility);
  syslog(priority, "%s", s1);
  closelog();
}
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] logger.c was failing to properly log local0-local6 facilities, due to a string parsing error. This patch enables proper local facility handling.

2020-09-08 Thread Rob Landley
On 9/8/20 2:16 PM, Chris Sarra via Toybox wrote:
> ---
>  toys/posix/logger.c | 7 ---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/toys/posix/logger.c b/toys/posix/logger.c
> index 3bcfb174..d1cc7291 100644
> --- a/toys/posix/logger.c
> +++ b/toys/posix/logger.c
> @@ -64,9 +64,10 @@ void logger_main(void)
>  else {
>*s1++ = len = 0;
>facility = arrayfind(TT.p, facilities, ARRAY_LEN(facilities));
> -  if (facility == -1 && strncasecmp(TT.p, "local", 5)) {
> -facility = s1[5]-'0';
> -if (facility>7 || s1[6]) facility = -1;
> +  if (facility == -1 && strncasecmp(TT.p, "local", 5) == 0) {
> +s2 = TT.p;
> +facility = s2[5]-'0';
> +if (facility>7 || s2[6]) facility = -1;

Sigh, why did I promote this out of pending? arrayfind() initializes matchlen to
0 and then never sets it to anything ELSE, so it ONLY returns exact matches not
longest unambiguous match (which is the point of the function I think?)

Applied your patch, but I have some cleanup to do to this command...

Rob
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


[Toybox] [PATCH] logger.c was failing to properly log local0-local6 facilities, due to a string parsing error. This patch enables proper local facility handling.

2020-09-08 Thread Chris Sarra via Toybox
---
 toys/posix/logger.c | 7 ---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/toys/posix/logger.c b/toys/posix/logger.c
index 3bcfb174..d1cc7291 100644
--- a/toys/posix/logger.c
+++ b/toys/posix/logger.c
@@ -64,9 +64,10 @@ void logger_main(void)
 else {
   *s1++ = len = 0;
   facility = arrayfind(TT.p, facilities, ARRAY_LEN(facilities));
-  if (facility == -1 && strncasecmp(TT.p, "local", 5)) {
-facility = s1[5]-'0';
-if (facility>7 || s1[6]) facility = -1;
+  if (facility == -1 && strncasecmp(TT.p, "local", 5) == 0) {
+s2 = TT.p;
+facility = s2[5]-'0';
+if (facility>7 || s2[6]) facility = -1;
 if (facility>=0) facility += 16;
   }
   if (facility<0) error_exit("bad facility: %s", TT.p);
-- 
2.28.0.526.ge36021eeef-goog

___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net