Re: [Toybox] [TOY] lspci - no *alloc

2013-07-24 Thread Isaac
On Wed, Jul 24, 2013 at 09:20:51PM +0200, Felix Janda wrote:
> Attached Isaac's toy edited to use toybuf instead of
> dynamic memory allocation. It could look prettier, though...
> 
> Felix
> 
Broken:
/toybox lspci -e
00:00.0 Class 0600: 8086
:27ac
 
00:02.0 Class 0300: 8086
:27ae
 
..
/toybox lspci
00:00.0 Class 0600: 8086:27ac 
00:02.0 Class 0300: 8086:27ae
..

The -e flag should be expanding the width of "class" (it isn't),
and it should not be making lspci print 3 lines per entry.
And I'm not sure how to fix it.

By the way, there are requests for more features (-v/-vv and text output) 
that would mean slightly different approaches to getting data; (p)readat_name 
would work in many of these cases, but text output means reading to toybuf
(which would clobber this), and I would also need to add more fields of varying 
sizes.

> 

> /*
>  * lspci - written by Isaac Dunham
> 
> USE_LSPCI(NEWTOY(lspci, "emkns:", TOYFLAG_USR|TOYFLAG_BIN))
> 
> config LSPCI
>   bool "lspci"
>   default n
>   help
> usage: lspci [-ekmn]
> 
> List PCI devices.
> -e  Print all 6 digits in class (like elspci)
> -k  Print kernel driver
> -m  Machine parseable format
> -n  Numeric output (default)
> */
> #define FOR_lspci
> #include "toys.h"
> 
> int do_lspci(struct dirtree *new)
> {
>   int alen = 8, dirfd;
>   char *dname = dirtree_path(new, &alen);
>   struct {
> char class[16], vendor[16], device[16], module[256];
>   } *bufs = (void*)(toybuf + 2);
> 
>   if (!strcmp("/sys/bus/pci/devices", dname)) return DIRTREE_RECURSE;
>   errno = 0;
>   dirfd = open(dname, O_RDONLY);
>   if (dirfd > 0) {
> char *p, **fields = (char*[]){"class", "vendor", "device", ""};
> 
> for (p = toybuf; **fields; p+=16, fields++) {
>   int fd, size;
> 
>   if ((fd = openat(dirfd, *fields, O_RDONLY)) < 0) continue;
>   size = 6 + 2*((toys.optflags & FLAG_e) && (p != toybuf));
>   p[read(fd, p, size)] = '\0';
>   close(fd);
> }
> 
> close(dirfd);
> if (!errno) {
>   char *driver = "";
>   char *fmt = toys.optflags & FLAG_m ? "%s, \"%s\" \"%s\" \"%s\" \"%s\"\n"
>: "%s Class %s: %s:%s 
> %s\n";
> 
>   if (toys.optflags & FLAG_k) {
> strcat(dname, "/driver");
> if (readlink(dname, bufs->module, sizeof(bufs->module)) != -1)
>   driver = basename(bufs->module);
>   }
>   printf(fmt, new->name + 5, bufs->class, bufs->vendor, bufs->device, 
>driver);
> }
>   }
>   return 0;
> }
> 
> void lspci_main(void)
> {
>   dirtree_read("/sys/bus/pci/devices", do_lspci);
> }


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


[Toybox] [CLEANUP] logger

2013-07-24 Thread Felix Janda
Various fixes to logger in below patch.

Mainly inline parse_priority(). Many other small changes e.g. s/const//,
move around some variables, ...

-Felix

# HG changeset patch
# User Felix Janda 
# Date 1374706109 -7200
# Node ID 8cc26924c2ef2e862c975a31167a685e18f555ec
# Parent  8ad85a95f7c3b75214fbd3a46fe834de45c39417
logger: Some cleanup

diff -r 8ad85a95f7c3 -r 8cc26924c2ef toys/pending/logger.c
--- a/toys/pending/logger.c Tue Jul 23 20:19:31 2013 -0500
+++ b/toys/pending/logger.c Thu Jul 25 00:48:29 2013 +0200
@@ -18,23 +18,18 @@
 #define FOR_logger
 #include "toys.h"
 #include 
-#include 
-#include 
 
 GLOBALS(
   char *priority_arg;
   char *ident;
-
-  int facility;
-  int priority;
 )
 
 struct mapping {
-  const char *key;
+  char *key;
   int value;
 };
 
-static const struct mapping facilities[] = {
+static struct mapping facilities[] = {
   {"user", LOG_USER}, {"main", LOG_MAIL}, {"news", LOG_NEWS},
   {"uucp", LOG_UUCP}, {"daemon", LOG_DAEMON}, {"auth", LOG_AUTH},
   {"cron", LOG_CRON}, {"lpr", LOG_LPR}, {"local0", LOG_LOCAL0},
@@ -44,64 +39,51 @@
   {NULL, 0}
 };
 
-static const struct mapping priorities[] = {
+static struct mapping priorities[] = {
   {"emerg", LOG_EMERG}, {"alert", LOG_ALERT}, {"crit", LOG_CRIT},
   {"err", LOG_ERR}, {"warning", LOG_WARNING}, {"notice", LOG_NOTICE},
   {"info", LOG_INFO}, {"debug", LOG_DEBUG},
   {NULL, 0}
 };
 
-static int lookup(const struct mapping *where, const char *key)
+static int lookup(struct mapping *where, char *key)
 {
-  int i;
-  for (i = 0; where[i].key; i++)
-if (!strcasecmp(key, where[i].key))
-  return where[i].value;
+  for (; where->key; where++)
+if (!strcasecmp(key, where->key)) return where->value;
 
   return -1;
 }
 
-static void parse_priority()
+void logger_main(void)
 {
-  char *sep = strchr(TT.priority_arg, '.');
+  int facility = LOG_USER, priority = LOG_NOTICE;
+  char *message = NULL;
 
-  if (sep)
-  {
-*sep = '\0';
-if ((TT.facility = lookup(facilities, TT.priority_arg)) == -1)
-  error_exit("bad facility: %s", TT.priority_arg);
-TT.priority_arg = sep+1;
+  if (toys.optflags & FLAG_p) {
+char *sep = strchr(TT.priority_arg, '.');
+
+if (sep) {
+  *sep = '\0';
+  if ((facility = lookup(facilities, TT.priority_arg)) == -1)
+error_exit("bad facility: %s", TT.priority_arg);
+  TT.priority_arg = sep+1;
+}
+
+if ((priority = lookup(priorities, TT.priority_arg)) == -1)
+  error_exit("bad priority: %s", TT.priority_arg);
   }
 
-  if ((TT.priority = lookup(priorities, TT.priority_arg)) == -1)
-error_exit("bad priority: %s", TT.priority_arg);
-}
+  if (!(toys.optflags & FLAG_t)) {
+struct passwd *pw = getpwuid(geteuid());
 
-void logger_main(void)
-{
-  if (toys.optflags & FLAG_p)
-parse_priority();
-  else
-  {
-TT.facility = LOG_USER;
-TT.priority = LOG_NOTICE;
-  }
-
-  if (!(toys.optflags & FLAG_t))
-  {
-struct passwd *pw = getpwuid(geteuid());
-if (!pw)
-  perror_exit("getpwuid");
+if (!pw) perror_exit("getpwuid");
 TT.ident = xstrdup(pw->pw_name);
   }
 
-  char *message = NULL;
   if (toys.optc) {
-int length = 0;
-int pos = 0;
+int length = 0, pos = 0;
 
-for (;*toys.optargs; (void) *(toys.optargs)++) // shut up gcc
-{
+for (;*toys.optargs; toys.optargs++) {
   length += strlen(*(toys.optargs)) + 1; // plus one for the args spacing
   message = xrealloc(message, length + 1); // another one for the null byte
 
@@ -113,7 +95,7 @@
 message = toybuf;
   }
 
-  openlog(TT.ident, (toys.optflags & FLAG_s ? LOG_PERROR : 0) , TT.facility);
-  syslog(TT.priority, "%s", message);
+  openlog(TT.ident, (toys.optflags & FLAG_s ? LOG_PERROR : 0) , facility);
+  syslog(priority, "%s", message);
   closelog();
 }
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


toybox@lists.landley.net

2013-07-24 Thread Strake
# HG changeset patch
# User Strake
# Date 1374665513 18000
# Node ID 16ed6e15cd35c0d9798150e769de3155dc6a2e9b
# Parent  7fd031a672f6f6835373b75d0987a61da2c8d3bc
grep: s/astrcat/x&/g

diff -r 7fd031a672f6 -r 16ed6e15cd35 toys/pending/grep.c
--- a/toys/pending/grep.c   Sun Jun 08 10:15:17 2003 -0500
+++ b/toys/pending/grep.c   Wed Jul 24 06:31:53 2013 -0500
@@ -107,8 +107,8 @@

 void addRE (char *x) {
   if (toys.optflags & FLAG_F) x = regfix (x);
-  if (TT.re_xs) TT.re_xs = astrcat (TT.re_xs, "|");
-  TT.re_xs = astrcat (TT.re_xs, x);
+  if (TT.re_xs) TT.re_xs = xastrcat (TT.re_xs, "|");
+  TT.re_xs = xastrcat (TT.re_xs, x);
   if (toys.optflags & FLAG_F) free (x);
 }
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


[Toybox] [patch] grep: add -b flag

2013-07-24 Thread Strake
# HG changeset patch
# User Strake
# Date 1055085317 18000
# Node ID 7fd031a672f6f6835373b75d0987a61da2c8d3bc
# Parent  50b52474ea9f8fb3b016e1bc3e41780f8d943c0e
grep: add -b flag

diff -r 50b52474ea9f -r 7fd031a672f6 toys/pending/grep.c
--- a/toys/pending/grep.c   Sun Jun 08 10:09:05 2003 -0500
+++ b/toys/pending/grep.c   Sun Jun 08 10:15:17 2003 -0500
@@ -5,13 +5,13 @@
  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
  * See 
http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html

-USE_GREP(NEWTOY(grep, "EFHahinosvwclqe*f*m#", TOYFLAG_BIN))
+USE_GREP(NEWTOY(grep, "EFHabhinosvwclqe*f*m#", TOYFLAG_BIN))

 config GREP
   bool "grep"
   default n
   help
-usage: grep [-clq] [-EFHhinosvw] (-e RE | -f REfile | RE) [file...]
+usage: grep [-clq] [-EFHbhinosvw] (-e RE | -f REfile | RE) [file...]

 modes:
   default: print lines from each file what match regular expression RE.
@@ -23,6 +23,7 @@
   -E:   extended RE syntax
   -F:   fixed RE syntax, i.e. all characters literal
   -H:   print file name
+  -b:   print byte offset of match
   -h:   not print file name
   -i:   case insensitive
   -n:   print line numbers
@@ -73,6 +74,8 @@
   default:
 if (!(toys.optflags & FLAG_h)) printf ("%s:", name);
 if ( (toys.optflags & FLAG_n)) printf ("%d:", n);
+if ( (toys.optflags & FLAG_b)) printf ("%ld:", lseek (0, 0,
SEEK_CUR) - strlen (y) +
+   (toys.optflags
& FLAG_o ? matches[2].rm_so : 0));
 if (!(toys.optflags & FLAG_o)) fputs (x, stdout);
 else {
   y += matches[2].rm_so;
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


[Toybox] [patch] grep: add -w flag

2013-07-24 Thread Strake
# HG changeset patch
# User Strake
# Date 1055084945 18000
# Node ID 50b52474ea9f8fb3b016e1bc3e41780f8d943c0e
# Parent  8ad85a95f7c3b75214fbd3a46fe834de45c39417
grep: add -w flag

diff -r 8ad85a95f7c3 -r 50b52474ea9f toys/pending/grep.c
--- a/toys/pending/grep.c   Tue Jul 23 20:19:31 2013 -0500
+++ b/toys/pending/grep.c   Sun Jun 08 10:09:05 2003 -0500
@@ -5,13 +5,13 @@
  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
  * See 
http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html

-USE_GREP(NEWTOY(grep, "EFHahinosvclqe*f*m#", TOYFLAG_BIN))
+USE_GREP(NEWTOY(grep, "EFHahinosvwclqe*f*m#", TOYFLAG_BIN))

 config GREP
   bool "grep"
   default n
   help
-usage: grep [-clq] [-EFHhinosv] (-e RE | -f REfile | RE) [file...]
+usage: grep [-clq] [-EFHhinosvw] (-e RE | -f REfile | RE) [file...]

 modes:
   default: print lines from each file what match regular expression RE.
@@ -29,6 +29,7 @@
   -o:   print only matching part
   -s:   keep silent on error
   -v:   invert match
+  -w:   match full word only
 */

 #define FOR_grep
@@ -48,7 +49,7 @@

   for (;;) {
 char *x, *y;
-regmatch_t match;
+regmatch_t matches[3];
 int atBOL = 1;

 x = get_rawline (fd, 0, '\n');
@@ -56,7 +57,7 @@
 y = x;
 n++; /* start at 1 */

-while (regexec (&re, y, 1, &match, atBOL ? 0 : REG_NOTBOL) == 0) {
+while (regexec (&re, y, 3, matches, atBOL ? 0 : REG_NOTBOL) == 0) {
   if (atBOL) nMatch++;
   toys.exitval = 0;
   atBOL = 0;
@@ -74,8 +75,8 @@
 if ( (toys.optflags & FLAG_n)) printf ("%d:", n);
 if (!(toys.optflags & FLAG_o)) fputs (x, stdout);
 else {
-  y += match.rm_so;
-  printf ("%.*s\n", match.rm_eo - match.rm_so, y++);
+  y += matches[2].rm_so;
+  printf ("%.*s\n", matches[2].rm_eo - matches[2].rm_so, y++);
 }
   }
   if (!(toys.optflags & FLAG_o)) break;
@@ -141,6 +142,8 @@
 toys.optc--; toys.optargs++;
   }

+  TT.re_xs = xmsprintf (toys.optflags & FLAG_w ?
"(^|[^_[:alnum:]])(%s)($|[^_[:alnum:]])" : "()(%s)()", TT.re_xs);
+
   if (regcomp (&re, TT.re_xs,
(toys.optflags & (FLAG_E | FLAG_F) ? REG_EXTENDED : 0) |
(toys.optflags &  FLAG_i   ? REG_ICASE: 0)) != 0) {
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [TOY] lspci - no *alloc

2013-07-24 Thread Felix Janda
Attached Isaac's toy edited to use toybuf instead of
dynamic memory allocation. It could look prettier, though...

Felix
/*
 * lspci - written by Isaac Dunham

USE_LSPCI(NEWTOY(lspci, "emkns:", TOYFLAG_USR|TOYFLAG_BIN))

config LSPCI
  bool "lspci"
  default n
  help
usage: lspci [-ekmn]

List PCI devices.
-e  Print all 6 digits in class (like elspci)
-k  Print kernel driver
-m  Machine parseable format
-n  Numeric output (default)
*/
#define FOR_lspci
#include "toys.h"

int do_lspci(struct dirtree *new)
{
  int alen = 8, dirfd;
  char *dname = dirtree_path(new, &alen);
  struct {
char class[16], vendor[16], device[16], module[256];
  } *bufs = (void*)(toybuf + 2);

  if (!strcmp("/sys/bus/pci/devices", dname)) return DIRTREE_RECURSE;
  errno = 0;
  dirfd = open(dname, O_RDONLY);
  if (dirfd > 0) {
char *p, **fields = (char*[]){"class", "vendor", "device", ""};

for (p = toybuf; **fields; p+=16, fields++) {
  int fd, size;

  if ((fd = openat(dirfd, *fields, O_RDONLY)) < 0) continue;
  size = 6 + 2*((toys.optflags & FLAG_e) && (p != toybuf));
  p[read(fd, p, size)] = '\0';
  close(fd);
}

close(dirfd);
if (!errno) {
  char *driver = "";
  char *fmt = toys.optflags & FLAG_m ? "%s, \"%s\" \"%s\" \"%s\" \"%s\"\n"
   : "%s Class %s: %s:%s %s\n";

  if (toys.optflags & FLAG_k) {
strcat(dname, "/driver");
if (readlink(dname, bufs->module, sizeof(bufs->module)) != -1)
  driver = basename(bufs->module);
  }
  printf(fmt, new->name + 5, bufs->class, bufs->vendor, bufs->device, 
   driver);
}
  }
  return 0;
}

void lspci_main(void)
{
  dirtree_read("/sys/bus/pci/devices", do_lspci);
}
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] Git mirror at https://github.com/gfto/toybox

2013-07-24 Thread Georgi Chorbadzhiyski
Around 07/24/2013 09:51 PM, Georgi Chorbadzhiyski scribbled:
> Around 07/24/2013 09:03 PM, Rob Landley scribbled:
>> Georgi: since I haven't gotten my own git mirror of toybox up yet, do  
>> you mind if I link to yours from the toybox website?
>>
>> How often is it updated? (I applied Strake's grep commit last night and  
>> the git mirror hasn't got it yet. But it's only been ~8 hours. Updated  
>> daily, perhaps?)
> 
> I update it manually every day or at least once every few days (along
> with the oscam repo that I'm also mirroring and using for development).
> 
> You can link to the repo and I'll try to keep it even more up to date.

I've set up automatic mirroring of the hg repo to my github repo.

It is executed every six hours (my timezone is EET, currently EEST).

-- 
Georgi Chorbadzhiyski
http://github.com/gfto/
http://georgi.unixsol.org/
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [TOY] lspci

2013-07-24 Thread Felix Janda
Sandeep Sharma wrote:
[...]
>   hi,
>  I have some questions actually :-
> 
>  > > -  fd = openat(dirfd, fname, O_RDONLY);
> > > -  if (fd < 0) {
> > > -return NULL;
> > > -  }
> > > -  lseek(fd, offset, SEEK_SET);
> > > +  char *buf = calloc(1, nbyte+1);
> > > +
> why calloc and why not __xzalloc__ ?  Are not we following
> toybox coding style.
>
>   And what if _calloc_ returns NULL. we will be  using a NULL buffer, it
> may lead to a potentional crash.

Yes, xzalloc would be preferable. Freeing the memory at some point would also
be nice. I'd even more prefer to have no dynamic allocation in this toy (at
least in the current form) at all. I'm going to post a new version like this
soon but the code is not as clear as the original one.

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


Re: [Toybox] Git mirror at https://github.com/gfto/toybox

2013-07-24 Thread Georgi Chorbadzhiyski
Around 07/24/2013 09:03 PM, Rob Landley scribbled:
> Georgi: since I haven't gotten my own git mirror of toybox up yet, do  
> you mind if I link to yours from the toybox website?
> 
> How often is it updated? (I applied Strake's grep commit last night and  
> the git mirror hasn't got it yet. But it's only been ~8 hours. Updated  
> daily, perhaps?)

I update it manually every day or at least once every few days (along
with the oscam repo that I'm also mirroring and using for development).

You can link to the repo and I'll try to keep it even more up to date.

-- 
Georgi Chorbadzhiyski
http://github.com/gfto/
http://georgi.unixsol.org/
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [TOY] lspci

2013-07-24 Thread Felix Janda
Isaac wrote:
[...]
> On Mon, Jul 22, 2013 at 09:05:26PM +0200, Felix Janda wrote:
> > Are there any more options you have in mind that should be implemented?
> 
> -s DEVICE is highly desireable, but I didn't get it working right.
> (-s shows only the device that has a matching end to its name:
> -s 03:00.0 shows :03:00.0)

Ok. pciutils lspci -s also accepts something like -s 03:*.0 to match
e.g. something like :03:01.0. Also if only one number is given it
matches the device (-s 3 and -s 3. are equivalent).

Is piping the output to grep not sufficient for many purposes or is
this option used by a lot of scripts?

> > Just out of curiosity: Could you post a link to the forum post?
> > 
> http://www.murga-linux.com/puppy/viewtopic.php?t=63971&start=30
> "It seems that bb-lspci is not able to replace elspci because it shows 
> [ like lspci-FULL ] only the 4 middle chars of /sys/bus/pci/devices/*/class,
>  while elspci shows the last 6 chars."

Thanks.

> In which case it should be readat_name.  The "p" is because pread()
> accepts an offset.
> (I named it for the similarities/diferences from the standard functions:
> it's similar to pread(), but it takes a parent directory like openat() and
> uses a filename.)

Of course feel free to fix that and post a new version.

> The down side is the longer printf...but that's not worth mentioning, 
> I think.

One could reintroduce the last argument and instead of seeking just add the
offset to the return value. But then the 2 just appears at another place.

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


[Toybox] Git mirror at https://github.com/gfto/toybox

2013-07-24 Thread Rob Landley
Georgi: since I haven't gotten my own git mirror of toybox up yet, do  
you mind if I link to yours from the toybox website?


How often is it updated? (I applied Strake's grep commit last night and  
the git mirror hasn't got it yet. But it's only been ~8 hours. Updated  
daily, perhaps?)


Thanks,

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


Re: [Toybox] [PATCH] grep: cleanup, add -Hs

2013-07-24 Thread Rob Landley

On 07/23/2013 08:22:08 PM, Strake wrote:

# HG changeset patch
# User Strake
# Date 1374628771 18000
# Node ID 8ad85a95f7c3b75214fbd3a46fe834de45c39417
# Parent  019f54d50c8be39306675e0f182a124f6446307d
grep



Would have preferred a litle more description than that, but it's  
applied to my tree now anyway. :)


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