sortinfo.c revision 1.4 1 /* $NetBSD: sortinfo.c,v 1.4 2015/12/20 00:48:36 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: sortinfo.c,v 1.4 2015/12/20 00:48:36 christos Exp $");
38
39 /*
40 * Sort a texinfo(1) directory file.
41 */
42
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <err.h>
48 #include <util.h>
49
50 struct section {
51 const char *name;
52 char **lines;
53 size_t nlines;
54 size_t maxlines;
55 };
56
57 static struct section *slist;
58 static size_t nsections, maxsections;
59
60 static struct section *
61 addsection(const char *line)
62 {
63 if (nsections >= maxsections) {
64 maxsections += 20;
65 slist = erealloc(slist, maxsections * sizeof(*slist));
66 }
67 slist[nsections].name = estrdup(line);
68 slist[nsections].nlines = 0;
69 slist[nsections].maxlines = 20;
70 slist[nsections].lines = ecalloc(slist[nsections].maxlines,
71 sizeof(*slist[nsections].lines));
72 return &slist[nsections++];
73 }
74
75 static void
76 addline(struct section *s, const char *line)
77 {
78 if (s->nlines == s->maxlines) {
79 s->maxlines += 20;
80 s->lines = erealloc(s->lines, s->maxlines * sizeof(*s->lines));
81 }
82 s->lines[s->nlines++] = estrdup(line);
83 }
84
85 static int
86 compsection(const void *a, const void *b)
87 {
88 const struct section *sa = a, *sb = b;
89 return strcmp(sa->name, sb->name);
90 }
91
92 static int
93 strptrcmp(const void *a, const void *b)
94 {
95 const char *sa = *(const char * const *)a;
96 const char *sb = *(const char * const *)b;
97 return strcmp(sa, sb);
98 }
99
100 static void
101 printsection(const struct section *s)
102 {
103 size_t i;
104
105 fputc('\n', stdout);
106 printf("%s", s->name);
107 for (i = 0; i < s->nlines; i++)
108 printf("%s", s->lines[i]);
109 }
110
111 int
112 main(int argc, char *argv[])
113 {
114 size_t i;
115 char *line;
116 int needsection;
117 struct section *s = NULL;
118
119 while ((line = fgetln(stdin, &i)) != NULL) {
120 fputs(line, stdout);
121 if (strcmp(line, "* Menu:\n") == 0)
122 break;
123 }
124
125 if (line == NULL)
126 errx(EXIT_FAILURE, "Did not find menu line");
127
128 needsection = 0;
129 while ((line = fgetln(stdin, &i)) != NULL)
130 switch (*line) {
131 case '\n':
132 needsection = 1;
133 continue;
134 case '*':
135 if (s == NULL)
136 errx(EXIT_FAILURE, "No current section");
137 addline(s, line);
138 continue;
139 default:
140 if (needsection == 0)
141 errx(EXIT_FAILURE, "Already in section");
142 s = addsection(line);
143 needsection = 0;
144 continue;
145 }
146
147 qsort(slist, nsections, sizeof(*slist), compsection);
148 for (i = 0; i < nsections; i++) {
149 s = &slist[i];
150 qsort(s->lines, s->nlines, sizeof(*s->lines), strptrcmp);
151 printsection(&slist[i]);
152 }
153
154 return EXIT_SUCCESS;
155 }
156