xref: /netbsd-src/usr.bin/sortinfo/sortinfo.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: sortinfo.c,v 1.6 2017/04/30 13:45:06 abhinav 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.6 2017/04/30 13:45:06 abhinav 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(char *line)
62 {
63 	if (nsections >= maxsections) {
64 		maxsections += 20;
65 		slist = erealloc(slist, maxsections * sizeof(*slist));
66 	}
67 	slist[nsections].name = 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, 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++] = 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 	ssize_t ll;
116 	char *line;
117 	int needsection;
118 	struct section *s;
119 
120 	s = NULL;
121 	line = NULL;
122 	i = 0;
123 
124 	while ((ll = getline(&line, &i, stdin)) != -1) {
125 		fputs(line, stdout);
126 		if (strcmp(line, "* Menu:\n") == 0)
127 			break;
128 	}
129 
130 	if (ll == -1)
131 		errx(EXIT_FAILURE, "Did not find menu line");
132 
133 	needsection = 0;
134 	while ((ll = getline(&line, &i, stdin)) != -1)
135 		switch (*line) {
136 		case '\n':
137 			needsection = 1;
138 			continue;
139 		case '*':
140 			if (s == NULL)
141 				errx(EXIT_FAILURE, "No current section");
142 			addline(s, line);
143 			line = NULL;
144 			i = 0;
145 			continue;
146 		default:
147 			if (needsection == 0)
148 				errx(EXIT_FAILURE, "Already in section");
149 			s = addsection(line);
150 			line = NULL;
151 			i = 0;
152 			needsection = 0;
153 			continue;
154 		}
155 
156 	free(line);
157 	qsort(slist, nsections, sizeof(*slist), compsection);
158 	for (i = 0; i < nsections; i++) {
159 		s = &slist[i];
160 		qsort(s->lines, s->nlines, sizeof(*s->lines), strptrcmp);
161 		printsection(&slist[i]);
162 	}
163 
164 	return EXIT_SUCCESS;
165 }
166