Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin add sortinfo



details:   https://anonhg.NetBSD.org/src/rev/97f2052daa70
branches:  trunk
changeset: 812505:97f2052daa70
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Dec 18 18:56:47 2015 +0000

description:
add sortinfo

diffstat:

 usr.bin/Makefile            |    4 +-
 usr.bin/sortinfo/Makefile   |    9 ++
 usr.bin/sortinfo/sortinfo.1 |   54 ++++++++++++++++
 usr.bin/sortinfo/sortinfo.c |  145 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 210 insertions(+), 2 deletions(-)

diffs (238 lines):

diff -r 55c6c8af53ac -r 97f2052daa70 usr.bin/Makefile
--- a/usr.bin/Makefile  Fri Dec 18 18:56:33 2015 +0000
+++ b/usr.bin/Makefile  Fri Dec 18 18:56:47 2015 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.221 2015/06/03 14:32:16 christos Exp $
+#      $NetBSD: Makefile,v 1.222 2015/12/18 18:56:47 christos Exp $
 #      from: @(#)Makefile      8.3 (Berkeley) 1/7/94
 
 .include <bsd.own.mk>
@@ -25,7 +25,7 @@
        renice rev revoke rfcomm_sppd rlogin rpcgen rpcinfo rs rsh \
        rup ruptime rusers rwall rwho \
        script sdiff sdpquery sed seq shar shlock \
-       showmount shuffle sockstat sort spell split stat su systat \
+       showmount shuffle sockstat sort sortinfo spell split stat su systat \
        tabs tail talk tcopy tee telnet tftp tic time timeout tip touch \
        tpfmt tput \
        tr true tset tsort tty ul uname unexpand unifdef \
diff -r 55c6c8af53ac -r 97f2052daa70 usr.bin/sortinfo/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/sortinfo/Makefile Fri Dec 18 18:56:47 2015 +0000
@@ -0,0 +1,9 @@
+#      $NetBSD: Makefile,v 1.1 2015/12/18 18:56:47 christos Exp $
+
+WARNS=6
+PROG=sortinfo
+
+LDADD+=-lutil
+DPADD+=${LIBUTIL}
+
+.include <bsd.prog.mk>
diff -r 55c6c8af53ac -r 97f2052daa70 usr.bin/sortinfo/sortinfo.1
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/sortinfo/sortinfo.1       Fri Dec 18 18:56:47 2015 +0000
@@ -0,0 +1,54 @@
+.\"    $NetBSD: sortinfo.1,v 1.1 2015/12/18 18:56:47 christos Exp $
+.\"
+.\" Copyright (c) 2015 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Christos Zoulas.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\"
+.Dd December 18, 2015
+.Dt SORTINFO 1
+.Os
+.Sh NAME
+.Nm sortinfo
+.Nd sort
+.Xr texinfo 1
+files
+.Sh SYNOPSIS
+.Nm
+.Sh DESCRIPTION
+The
+.Nm
+reads a directory file from the standard input built by
+.Xr install-info 1 ,
+sorts its sections and prints the sorted contents to the standard output.
+.Sh SEE ALSO
+.Xr install-info 1 ,
+.Xr texinfo 1
+.Sh HISTORY
+The
+.Nm
+command appeared
+.Nx 8 .
diff -r 55c6c8af53ac -r 97f2052daa70 usr.bin/sortinfo/sortinfo.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/sortinfo/sortinfo.c       Fri Dec 18 18:56:47 2015 +0000
@@ -0,0 +1,145 @@
+/*     $NetBSD: sortinfo.c,v 1.1 2015/12/18 18:56:47 christos Exp $    */
+
+/*-
+ * Copyright (c) 2015 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: sortinfo.c,v 1.1 2015/12/18 18:56:47 christos Exp $");
+
+/*
+ * Sort a texinfo(1) directory file.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <err.h>
+#include <util.h>
+
+struct line {
+       char *data;
+       struct line *next;
+};
+
+struct section {
+       const char *name;
+       struct line *line;
+};
+
+static struct section *slist;
+static size_t nsections, maxsections;
+
+static struct section *
+addsection(const char *line)
+{
+       if (nsections >= maxsections) {
+               maxsections += 20;
+               slist = erealloc(slist, maxsections * sizeof(*slist));
+       }
+       slist[nsections].name = estrdup(line);
+       slist[nsections].line = NULL;
+       return &slist[nsections++];
+}
+
+static struct line *
+addline(struct section *s, struct line *l, const char *line)
+{
+       if (l == NULL)
+               s->line = l = emalloc(sizeof(*l));
+       else {
+               l->next = emalloc(sizeof(*l));
+               l = l->next;
+       }
+       l->next = NULL;
+       l->data = estrdup(line);
+       return l;
+}
+
+static int
+compsection(const void *a, const void *b)
+{
+       const struct section *sa = a, *sb = b;
+       return strcmp(sa->name, sb->name);
+}
+
+static void
+printsection(const struct section *s)
+{
+       struct line *l;
+       fputc('\n', stdout);
+       printf("%s", s->name);
+       for (l = s->line; l; l = l->next)
+               printf("%s", l->data);
+}
+       
+int
+main(int argc, char *argv[])
+{
+       size_t i;
+       char *line;
+       int needsection;
+       struct section *s = NULL;
+       struct line *l = NULL;
+
+       while ((line = fgetln(stdin, &i)) != NULL) {
+               fputs(line, stdout);
+               if (strcmp(line, "* Menu:\n") == 0)
+                       break;
+       }
+
+       if (line == NULL)
+               errx(EXIT_FAILURE, "Did not find menu line");
+
+       needsection = 0;
+       while ((line = fgetln(stdin, &i)) != NULL)
+               switch (*line) {
+               case '\n':
+                       needsection = 1;
+                       continue;
+               case '*':
+                       if (s == NULL)
+                               errx(EXIT_FAILURE, "No current section");
+                       l = addline(s, l, line);
+                       continue;
+               default:
+                       if (needsection == 0)
+                               errx(EXIT_FAILURE, "Already in section");
+                       s = addsection(line);
+                       l = NULL;
+                       needsection = 0;
+                       continue;
+               }
+
+       qsort(slist, nsections, sizeof(*slist), compsection);
+       for (i = 0; i < nsections; i++)
+               printsection(&slist[i]);
+
+       return EXIT_SUCCESS;
+}



Home | Main Index | Thread Index | Old Index