pkg_version in C [was: Re: Perl scripts that need rewriting - Progress!]

2002-05-14 Thread Jeremy Lea

Hi,

On Thu, May 09, 2002 at 08:33:22PM +0100, Mark Murray wrote:
 /usr/sbin/pkg_version Jeremy Lea [EMAIL PROTECTED] - re

OK, the first revision is attached.  It appears to work for me...  It
needs some spit and polish, and probably a few more people to test.

I've not implemented the -d flag since it sort of became unneeded, and
it's not really the way things are done in the rest of pkg_*.  I've also
not implemented -c.  There were enough warnings that it wasn't really
useful, and portupgrade does a much better job... 

Regards,
  -Jeremy

-- 
FreeBSD - Because the best things in life are free...
   http://www.freebsd.org/


Index: lib/lib.h
===
RCS file: /home/ncvs/src/usr.sbin/pkg_install/lib/lib.h,v
retrieving revision 1.43
diff -u -r1.43 lib.h
--- lib/lib.h   5 May 2002 21:03:25 -   1.43
+++ lib/lib.h   14 May 2002 12:41:41 -
@@ -201,6 +201,8 @@
 
 /* Version */
 intverscmp(Package *, int, int);
+const char *version_of(const char *, int *, int *);
+intversion_cmp(const char *, const char *);
 
 /* Externs */
 extern Boolean Verbose;
Index: lib/version.c
===
RCS file: /home/ncvs/src/usr.sbin/pkg_install/lib/version.c,v
retrieving revision 1.2
diff -u -r1.2 version.c
--- lib/version.c   1 Apr 2002 09:39:07 -   1.2
+++ lib/version.c   14 May 2002 12:41:41 -
@@ -14,6 +14,15 @@
  * Maxim Sobolev
  * 31 July 2001
  *
+ */
+ 
+#include sys/cdefs.h
+__FBSDID($FreeBSD: src/usr.sbin/pkg_install/lib/version.c,v 1.2 2002/04/01 09:39:07 
+obrien Exp $);
+
+#include lib.h
+#include err.h
+
+/*
  * Routines to assist with PLIST_FMT_VER numbers in the packing
  * lists.
  *
@@ -23,13 +32,6 @@
  *  value insted of the hash of an object this links points to.
  *
  */
-
-#include sys/cdefs.h
-__FBSDID($FreeBSD: src/usr.sbin/pkg_install/lib/version.c,v 1.2 2002/04/01 09:39:07 
obrien Exp $);
-
-#include lib.h
-#include err.h
-
 int
 verscmp(Package *pkg, int major, int minor)
 {
@@ -43,4 +45,123 @@
rval = 1;
 
 return rval;
+}
+
+/*
+ * version_of(pkgname, epoch, revision) returns a pointer to the version
+ * portion of a package name and the two special components.
+ *
+ * Jeremy D. Lea.
+ */
+const char *
+version_of(const char *pkgname, int *epoch, int *revision)
+{
+char *ch;
+
+if (pkgname == NULL)
+   errx(2, %s: Passed NULL pkgname., __func__);
+if (epoch != NULL) {
+   if ((ch = strrchr(pkgname, ',')) == NULL)
+   *epoch = 0;
+   else
+   *epoch = atoi(ch[1]);
+}
+if (revision != NULL) {
+   if ((ch = strrchr(pkgname, '_')) == NULL)
+   *revision = 0;
+   else
+   *revision = atoi(ch[1]);
+}
+/* Cheat if we are just passed a version, not a valid package name */
+if ((ch = strrchr(pkgname, '-')) == NULL)
+   return pkgname;
+else
+   return ch[1];
+}
+
+/*
+ * version_cmp(pkg1, pkg2) returns -1, 0 or 1 depending on if the version
+ * components of pkg1 is less than, equal to or greater than pkg2. No
+ * comparision of the basenames is done.
+ *
+ * The port verison is defined by:
+ * ${PORTVERSION}[_${PORTREVISION}][,${PORTEPOCH}]
+ * ${PORTEPOCH} supercedes ${PORTVERSION} supercedes ${PORTREVISION}.
+ * See the commit log for revision 1.349 of ports/Mk/bsd.port.mk
+ * for more information.
+ *
+ * The epoch and revision are defined to be a single number, while the rest
+ * of the version should conform to the porting guidelines. It can contain
+ * multiple components, seperated by a period, including letters.
+ *
+ * The tests below allow for significantly more latitude in the version
+ * numbers than is allowed in the guidelines. No point in wasting user's
+ * time enforcing them here. That's what flamewars are for.
+ *
+ * Jeremy D. Lea.
+ */
+int
+version_cmp(const char *pkg1, const char *pkg2)
+{
+const char *c1, *c2, *v1, *v2;
+char *t1, *t2;
+int e1, e2, r1, r2, n1, n2;
+
+v1 = version_of(pkg1, e1, r1);
+v2 = version_of(pkg2, e2, r2);
+/* Minor optimisation. */
+if (strcmp(v1, v2) == 0)
+   return 0;
+/* First compare epoch. */
+if (e1 != e2)
+   return (e1  e2 ? -1 : 1);
+else {
+   /* We walk down the versions, trying to convert to numbers.
+* We terminate when we reach an underscore, a comma or the
+* string terminator, thanks to a nasty trick with strchr().
+*
+* strtol() conveniently gobbles up the chars it converts.
+*/
+   c1 = strchr(_,, v1[0]);
+   c2 = strchr(_,, v2[0]);
+   while (c1 == NULL  c2 == NULL) {
+   n1 = strtol(v1, t1, 10);
+   n2 = strtol(v2, t2, 10);
+   if (n1 != n2)
+   return (n1  n2 ? -1 : 1);
+   /* The numbers are equal, check for letters. They're letters
+  purely because 

Re: pkg_version in C [was: Re: Perl scripts that need rewriting - Progress!]

2002-05-14 Thread Mark Murray

 OK, the first revision is attached.  It appears to work for me...  It
 needs some spit and polish, and probably a few more people to test.
 
 I've not implemented the -d flag since it sort of became unneeded, and
 it's not really the way things are done in the rest of pkg_*.  I've also
 not implemented -c.  There were enough warnings that it wasn't really
 useful, and portupgrade does a much better job... 
 
 Regards,
   -Jeremy

No need to wait for me on this one. If folks are happy, this is good
for me!

M
-- 
o   Mark Murray
\_
O.\_Warning: this .sig is umop ap!sdn
#text/plain; name=cv.doc [Mark Murray CV Plain Text] cv.doc
#application/octet-stream; name=cv.pdf [Mark Murray CV PDF] cv.pdf

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: pkg_version in C [was: Re: Perl scripts that need rewriting - Progress!]

2002-05-14 Thread Maxim Sobolev

Jeremy Lea wrote:
 
 Hi,
 
 On Thu, May 09, 2002 at 08:33:22PM +0100, Mark Murray wrote:
  /usr/sbin/pkg_version Jeremy Lea [EMAIL PROTECTED] - re
 
 OK, the first revision is attached.  It appears to work for me...  It
 needs some spit and polish, and probably a few more people to test.
 
 I've not implemented the -d flag since it sort of became unneeded, and
 it's not really the way things are done in the rest of pkg_*.  I've also
 not implemented -c.  There were enough warnings that it wasn't really
 useful, and portupgrade does a much better job...

Cool! Few notes:

+++ version/perform.c   14 May 2002 12:41:41 -
[...]
+   strlcpy(tmp, PORTS_DIR, PATH_MAX);
+   strlcat(tmp, /INDEX, PATH_MAX);

I'd suggest snprintf(3)

[...]
+ftsp = fts_open((char * const *)(uintptr_t)paths, FTS_LOGICAL |
FTS_NOCHDIR | FTS_NOSTAT, fname_cmp);
+if (ftsp != NULL) {
+   while ((f = fts_read(ftsp)) != NULL) {
+   if (f-fts_info == FTS_D  f-fts_level == 1) {
+   fts_set(ftsp, f, FTS_SKIP);
+   if (MatchName == NULL || strstr(f-fts_name,
MatchName))
+   err_cnt += pkg_do(f-fts_name);
+   }
+   }
+   fts_close(ftsp);
+}

Why noy use matchinstalled() which do something similar?

[...]
+strlcpy(tmp, LOG_DIR, PATH_MAX);
+strlcat(tmp, /, PATH_MAX);
+strlcat(tmp, pkg, PATH_MAX);
+strlcat(tmp, /, PATH_MAX);
+strlcat(tmp, CONTENTS_FNAME, PATH_MAX);

I'd suggest snprintf(3)

[...]
+   strlcpy(tmp, PORTS_DIR, PATH_MAX);
+   strlcat(tmp, /, PATH_MAX);
+   strlcat(tmp, plist.origin, PATH_MAX);

snprintf(3)

[...]
+ funny:
+warnx(This is a very funny looking INDEX!);
+return 1;

I don't think this is a good choice for an error message.

[...]
+static int
+fname_cmp(const FTSENT **a, const FTSENT **b)
+{
+return strcmp((*a)-fts_name, (*b)-fts_name);
+}

Could be routed to /dev/null if matchinstalled() is used.

-Maxim

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: pkg_version in C [was: Re: Perl scripts that need rewriting - Progress!]

2002-05-14 Thread Jeremy Lea

Hi,

On Tue, May 14, 2002 at 04:19:29PM +0300, Maxim Sobolev wrote:
 
 +++ version/perform.c   14 May 2002 12:41:41 -
 [...]
 +   strlcpy(tmp, PORTS_DIR, PATH_MAX);
 +   strlcat(tmp, /INDEX, PATH_MAX);
 
 I'd suggest snprintf(3)

Yeah.  Like I said, it needs a bit of polishing.  I tend to do things
this way while I'm developing because I find it easier to keep track of
what's going where in the string.  There's also a bunch of places where
the 'l' versions aren't needed...

 Why noy use matchinstalled() which do something similar?

I looked at it, but it was taking more code to make it work than to
duplicate it because the -s flag is not a regex or a globing pattern. 
It's a poorly designed interface...  Now that it's working I'll look at
it again and see if I can get it to do the right thing.  Simplifying
working code is a lot easier than building it.

 + funny:
 +warnx(This is a very funny looking INDEX!);
 +return 1;
 
 I don't think this is a good choice for an error message.

;-)

Regards,
  -Jeremy

-- 
FreeBSD - Because the best things in life are free...
   http://www.freebsd.org/

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: pkg_version in C [was: Re: Perl scripts that need rewriting - Progress!]

2002-05-14 Thread Maxim Sobolev

Jeremy Lea wrote:
 
 Hi,
 
 On Tue, May 14, 2002 at 04:19:29PM +0300, Maxim Sobolev wrote:
 
  +++ version/perform.c   14 May 2002 12:41:41 -
  [...]
  +   strlcpy(tmp, PORTS_DIR, PATH_MAX);
  +   strlcat(tmp, /INDEX, PATH_MAX);
 
  I'd suggest snprintf(3)
 
 Yeah.  Like I said, it needs a bit of polishing.  I tend to do things
 this way while I'm developing because I find it easier to keep track of
 what's going where in the string.  There's also a bunch of places where
 the 'l' versions aren't needed...

Tastes differ, granted, but your version is inconsistent with the
existing coding style of pkg_install, where snprintf's are used in
such cases.

 
  Why noy use matchinstalled() which do something similar?
 
 I looked at it, but it was taking more code to make it work than to
 duplicate it because the -s flag is not a regex or a globing pattern.
 It's a poorly designed interface...  Now that it's working I'll look at
 it again and see if I can get it to do the right thing.  Simplifying
 working code is a lot easier than building it.

In this case you just need to call matchinstalled(MATCH_ALL, NULL,
NULL) and iterate over results, just like you do with your fts_read()
loop. This should make code a bit cleaner and more compact.

  + funny:
  +warnx(This is a very funny looking INDEX!);
  +return 1;
 
  I don't think this is a good choice for an error message.
 
 ;-)

-Maxim

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: pkg_version in C [was: Re: Perl scripts that need rewriting - Progress!]

2002-05-14 Thread Kris Kennaway

On Tue, May 14, 2002 at 02:50:39PM +0200, Jeremy Lea wrote:
 Hi,
 
 On Thu, May 09, 2002 at 08:33:22PM +0100, Mark Murray wrote:
  /usr/sbin/pkg_version   Jeremy Lea [EMAIL PROTECTED] - re
 
 OK, the first revision is attached.  It appears to work for me...  It
 needs some spit and polish, and probably a few more people to test.

Thanks for doing this.

Kris



msg38345/pgp0.pgp
Description: PGP signature


Re: pkg_version in C [was: Re: Perl scripts that need rewriting - Progress!]

2002-05-14 Thread Bruce A. Mah

If memory serves me right, Jeremy Lea wrote:

 On Thu, May 09, 2002 at 08:33:22PM +0100, Mark Murray wrote:
  /usr/sbin/pkg_version   Jeremy Lea [EMAIL PROTECTED] - re
 
 OK, the first revision is attached.  It appears to work for me...  It
 needs some spit and polish, and probably a few more people to test.
 
 I've not implemented the -d flag since it sort of became unneeded, and
 it's not really the way things are done in the rest of pkg_*.  I've also
 not implemented -c.  There were enough warnings that it wasn't really
 useful, and portupgrade does a much better job... 

Hi Jeremy--

This looks very nice.  I'll let Maxim and other ports gurus comment on 
the coding, and content myself with some high-level comments:

1.  The version comparisons passed all of the regression tests that knu
and I made for the original pkg_version (test-pkg_version.sh).  This
gives me a nice warm fuzzy feeling about that part of the code.

2.  -c is still in the usage message, even though it's not in the 
code anymore (yay!).  Might want to take this out.

3.  The AUTHORS section in the manpage isn't marked up quite right.  I'd
recommend something like this:

-
.Sh AUTHORS
The
.Nm
utility was written by
.An Jeremy D. Lea Aq [EMAIL PROTECTED] ,
partially based on a Perl script written by
.An Bruce A. Mah Aq [EMAIL PROTECTED] .
-

4.  You commented that you didn't like the way we did -s before.  If
you think it'd be better as (say) a regex or a globbing expression, I
don't believe there'd be much problem with going that route.  knu 
used globbing when he wrote portversion, if you want some precedent.

Good job!

Bruce.



To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: Perl scripts that need rewriting - Progress!

2002-05-12 Thread Mark Murray

 Hi,
 
  On Thu, 09 May 2002 20:33:22 +0100
  Mark Murray [EMAIL PROTECTED] said:
 
 mark /usr/sbin/scriptdump
 
 This script is from KAME.  It seems that NetBSD doesn't install it.
 Is someone actually using it?  If okay, I'll change to don't install
 it.

That sounds good to me! Go right ahead! :)

M
-- 
o   Mark Murray
\_
O.\_Warning: this .sig is umop ap!sdn
#text/plain; name=cv.doc [Mark Murray CV Plain Text] cv.doc
#application/octet-stream; name=cv.pdf [Mark Murray CV PDF] cv.pdf

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: Perl scripts that need rewriting - Progress!

2002-05-10 Thread Hajimu UMEMOTO

Hi,

 On Thu, 09 May 2002 20:33:22 +0100
 Mark Murray [EMAIL PROTECTED] said:

mark /usr/sbin/scriptdump

This script is from KAME.  It seems that NetBSD doesn't install it.
Is someone actually using it?  If okay, I'll change to don't install
it.

Sincerely,

--
Hajimu UMEMOTO @ Internet Mutual Aid Society Yokohama, Japan
[EMAIL PROTECTED]  [EMAIL PROTECTED]  ume@{,jp.}FreeBSD.org
http://www.imasy.org/~ume/

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Perl scripts that need rewriting - Progress!

2002-05-09 Thread Mark Murray

Hi

The response to the perl-script rewriting project has been
very hearteningly _fantastic_!

Here is the list as it stands. The gaps are fairly obvious (and
probably mostly not critical in the short term):

/usr/bin/afmtodit
/usr/bin/catman John Rochester [EMAIL PROTECTED] - re
/usr/bin/makewhatis John Rochester [EMAIL PROTECTED] - re
/usr/bin/mmroff
/usr/bin/sockstat   des - re
/usr/bin/whereissheldonh - re
/usr/sbin/adduser   Mike Makonnen [EMAIL PROTECTED] - re
/usr/sbin/kbdmapJonathan Belson [EMAIL PROTECTED] - re
/usr/sbin/pkg_updatepaul - del - done
/usr/sbin/pkg_version   Jeremy Lea [EMAIL PROTECTED] - re
/usr/sbin/rmuserMike Makonnen [EMAIL PROTECTED] - re
/usr/sbin/scriptdump
/usr/sbin/spkrtest  Riccardo Torrini [EMAIL PROTECTED] - re - ready
/usr/sbin/vidfont   Jonathan Belson [EMAIL PROTECTED] - re

Key - re == to be rewritten
  del == to be deleted
  ready == work done, just waiting for commit.
  done == completed

M
-- 
o   Mark Murray
\_
O.\_Warning: this .sig is umop ap!sdn
#text/plain; name=cv.doc [Mark Murray CV Plain Text] cv.doc
#application/octet-stream; name=cv.pdf [Mark Murray CV PDF] cv.pdf

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message