-
Notifications
You must be signed in to change notification settings - Fork 1
/
reorg.c
301 lines (272 loc) · 7.55 KB
/
reorg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* $Id: reorg.c,v 1.6 2019/05/01 11:03:31 schwarze Exp $ */
/*
* Copyright (c) 2019 Ingo Schwarze <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "string.h"
#include "node.h"
#include "reorg.h"
/*
* The implementation of the tree reorganizer.
*/
static void
reorg_root(struct pnode *root, const char *sec)
{
struct pnode *date, *info, *name, *vol, *nc;
if (root == NULL)
return;
/* Collect prologue information. */
if ((date = pnode_takefirst(root, NODE_PUBDATE)) == NULL &&
(date = pnode_takefirst(root, NODE_DATE)) == NULL) {
date = pnode_alloc(NULL);
pnode_alloc_text(date, "$Mdocdate" "$");
}
date->node = NODE_DATE;
date->parent = root;
name = vol = NULL;
if ((nc = pnode_findfirst(root, NODE_REFMETA)) != NULL) {
name = pnode_takefirst(nc, NODE_REFENTRYTITLE);
vol = pnode_takefirst(nc, NODE_MANVOLNUM);
}
if (name == NULL) {
name = pnode_alloc(NULL);
name->node = NODE_REFENTRYTITLE;
name->parent = root;
pnode_alloc_text(name,
pnode_getattr_raw(root, ATTRKEY_ID, "UNKNOWN"));
}
if (vol == NULL || sec != NULL) {
pnode_unlink(vol);
vol = pnode_alloc(NULL);
vol->node = NODE_MANVOLNUM;
vol->parent = root;
pnode_alloc_text(vol, sec == NULL ? "1" : sec);
}
/* Insert prologue information at the beginning. */
if (pnode_findfirst(root, NODE_REFNAMEDIV) == NULL &&
((info = pnode_findfirst(root, NODE_BOOKINFO)) != NULL ||
(info = pnode_findfirst(root, NODE_REFENTRYINFO)) != NULL)) {
if ((nc = pnode_takefirst(info, NODE_ABSTRACT)) != NULL)
TAILQ_INSERT_HEAD(&root->childq, nc, child);
if ((nc = pnode_takefirst(info, NODE_TITLE)) != NULL)
TAILQ_INSERT_HEAD(&root->childq, nc, child);
}
TAILQ_INSERT_HEAD(&root->childq, vol, child);
TAILQ_INSERT_HEAD(&root->childq, name, child);
TAILQ_INSERT_HEAD(&root->childq, date, child);
}
static void
reorg_refentry(struct pnode *n)
{
struct pnode *info, *meta, *nc, *title;
struct pnode *match, *later;
/* Collect nodes that remained behind from the prologue. */
meta = NULL;
info = pnode_takefirst(n, NODE_BOOKINFO);
if (info != NULL && TAILQ_FIRST(&info->childq) == NULL) {
pnode_unlink(info);
info = NULL;
}
if (info == NULL) {
info = pnode_takefirst(n, NODE_REFENTRYINFO);
if (info != NULL && TAILQ_FIRST(&info->childq) == NULL) {
pnode_unlink(info);
info = NULL;
}
if (info == NULL)
info = pnode_takefirst(n, NODE_INFO);
meta = pnode_takefirst(n, NODE_REFMETA);
if (meta != NULL && TAILQ_FIRST(&meta->childq) == NULL) {
pnode_unlink(meta);
meta = NULL;
}
}
if (info == NULL && meta == NULL)
return;
/*
* Find the best place to put this information.
* Use the last existing AUTHORS node, if any.
* Otherwise, put it behind all standard sections that
* conventionally precede AUTHORS, and also behind any
* non-standard sections that follow the last of these,
* but before the next standard section.
*/
match = later = NULL;
TAILQ_FOREACH(nc, &n->childq, child) {
switch (nc->node) {
case NODE_REFENTRY:
case NODE_REFNAMEDIV:
case NODE_REFSYNOPSISDIV:
later = NULL;
continue;
case NODE_APPENDIX:
case NODE_INDEX:
if (later == NULL)
later = nc;
continue;
default:
break;
}
if ((title = pnode_findfirst(nc, NODE_TITLE)) == NULL ||
(title = TAILQ_FIRST(&title->childq)) == NULL ||
title->node != NODE_TEXT)
continue;
if (strcasecmp(title->b, "AUTHORS") == 0 ||
strcasecmp(title->b, "AUTHOR") == 0)
match = nc;
else if (strcasecmp(title->b, "NAME") == 0 ||
strcasecmp(title->b, "SYNOPSIS") == 0 ||
strcasecmp(title->b, "DESCRIPTION") == 0 ||
strcasecmp(title->b, "RETURN VALUES") == 0 ||
strcasecmp(title->b, "ENVIRONMENT") == 0 ||
strcasecmp(title->b, "FILES") == 0 ||
strcasecmp(title->b, "EXIT STATUS") == 0 ||
strcasecmp(title->b, "EXAMPLES") == 0 ||
strcasecmp(title->b, "DIAGNOSTICS") == 0 ||
strcasecmp(title->b, "ERRORS") == 0 ||
strcasecmp(title->b, "SEE ALSO") == 0 ||
strcasecmp(title->b, "STANDARDS") == 0 ||
strcasecmp(title->b, "HISTORY") == 0)
later = NULL;
else if ((strcasecmp(title->b, "CAVEATS") == 0 ||
strcasecmp(title->b, "BUGS") == 0) &&
later == NULL)
later = nc;
}
/*
* Disabled - it yields a mess in the AUTHORS section.
*/
#if 0
/*
* If no AUTHORS section was found, create one from scratch,
* and insert that at the place selected earlier.
*/
if (match == NULL) {
match = pnode_alloc(NULL);
match->node = NODE_SECTION;
match->flags |= NFLAG_SPC;
match->parent = n;
nc = pnode_alloc(match);
nc->node = NODE_TITLE;
nc->flags |= NFLAG_SPC;
nc = pnode_alloc_text(nc, "AUTHORS");
nc->flags |= NFLAG_SPC;
if (later == NULL)
TAILQ_INSERT_TAIL(&n->childq, match, child);
else
TAILQ_INSERT_BEFORE(later, match, child);
}
/*
* Dump the stuff excised at the beginning
* into this AUTHORS section.
*/
if (info != NULL)
TAILQ_INSERT_TAIL(&match->childq, info, child);
if (meta != NULL)
TAILQ_INSERT_TAIL(&match->childq, meta, child);
#endif
}
static void
default_title(struct pnode *n, const char *title)
{
struct pnode *nc;
if (n->parent == NULL)
return;
TAILQ_FOREACH(nc, &n->childq, child)
if (nc->node == NODE_TITLE)
return;
nc = pnode_alloc(NULL);
nc->node = NODE_TITLE;
nc->parent = n;
TAILQ_INSERT_HEAD(&n->childq, nc, child);
pnode_alloc_text(nc, title);
}
static void
reorg_function(struct pnode *n)
{
struct pnode *nc;
size_t sz;
if ((nc = TAILQ_FIRST(&n->childq)) != NULL &&
nc->node == NODE_TEXT &&
TAILQ_NEXT(nc, child) == NULL &&
(sz = strlen(nc->b)) > 2 &&
nc->b[sz - 2] == '(' && nc->b[sz - 1] == ')')
nc->b[sz - 2] = '\0';
}
static void
reorg_recurse(struct pnode *n)
{
struct pnode *nc;
if (n == NULL)
return;
switch (n->node) {
case NODE_ABSTRACT:
default_title(n, "Abstract");
n->node = NODE_SECTION;
break;
case NODE_APPENDIX:
if (n->parent == NULL)
reorg_refentry(n);
default_title(n, "Appendix");
break;
case NODE_CAUTION:
default_title(n, "Caution");
n->node = NODE_NOTE;
break;
case NODE_FUNCTION:
reorg_function(n);
break;
case NODE_LEGALNOTICE:
default_title(n, "Legal Notice");
n->node = NODE_SIMPLESECT;
break;
case NODE_NOTE:
default_title(n, "Note");
break;
case NODE_PREFACE:
if (n->parent == NULL)
reorg_refentry(n);
default_title(n, "Preface");
n->node = NODE_SECTION;
break;
case NODE_REFENTRY:
reorg_refentry(n);
break;
case NODE_SECTION:
if (n->parent == NULL)
reorg_refentry(n);
/* FALLTHROUGH */
case NODE_SIMPLESECT:
default_title(n, "Untitled");
break;
case NODE_TIP:
default_title(n, "Tip");
n->node = NODE_NOTE;
break;
case NODE_WARNING:
default_title(n, "Warning");
n->node = NODE_NOTE;
break;
default:
break;
}
TAILQ_FOREACH(nc, &n->childq, child)
reorg_recurse(nc);
}
void
ptree_reorg(struct ptree *tree, const char *sec)
{
reorg_root(tree->root, sec);
reorg_recurse(tree->root);
}