forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandocdb.c
2338 lines (2089 loc) · 54.8 KB
/
mandocdb.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: mandocdb.c,v 1.253 2017/07/28 14:48:25 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <[email protected]>
* Copyright (c) 2011-2017 Ingo Schwarze <[email protected]>
* Copyright (c) 2016 Ed Maste <[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 "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <assert.h>
#include <ctype.h>
#if HAVE_ERR
#include <err.h>
#endif
#include <errno.h>
#include <fcntl.h>
#if HAVE_FTS
#include <fts.h>
#else
#include "compat_fts.h"
#endif
#include <limits.h>
#if HAVE_SANDBOX_INIT
#include <sandbox.h>
#endif
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mandoc_aux.h"
#include "mandoc_ohash.h"
#include "mandoc.h"
#include "roff.h"
#include "mdoc.h"
#include "man.h"
#include "manconf.h"
#include "mansearch.h"
#include "dba_array.h"
#include "dba.h"
extern const char *const mansearch_keynames[];
enum op {
OP_DEFAULT = 0, /* new dbs from dir list or default config */
OP_CONFFILE, /* new databases from custom config file */
OP_UPDATE, /* delete/add entries in existing database */
OP_DELETE, /* delete entries from existing database */
OP_TEST /* change no databases, report potential problems */
};
struct str {
const struct mpage *mpage; /* if set, the owning parse */
uint64_t mask; /* bitmask in sequence */
char key[]; /* rendered text */
};
struct inodev {
ino_t st_ino;
dev_t st_dev;
};
struct mpage {
struct inodev inodev; /* used for hashing routine */
struct dba_array *dba;
char *sec; /* section from file content */
char *arch; /* architecture from file content */
char *title; /* title from file content */
char *desc; /* description from file content */
struct mpage *next; /* singly linked list */
struct mlink *mlinks; /* singly linked list */
int name_head_done;
enum form form; /* format from file content */
};
struct mlink {
char file[PATH_MAX]; /* filename rel. to manpath */
char *dsec; /* section from directory */
char *arch; /* architecture from directory */
char *name; /* name from file name (not empty) */
char *fsec; /* section from file name suffix */
struct mlink *next; /* singly linked list */
struct mpage *mpage; /* parent */
int gzip; /* filename has a .gz suffix */
enum form dform; /* format from directory */
enum form fform; /* format from file name suffix */
};
typedef int (*mdoc_fp)(struct mpage *, const struct roff_meta *,
const struct roff_node *);
struct mdoc_handler {
mdoc_fp fp; /* optional handler */
uint64_t mask; /* set unless handler returns 0 */
int taboo; /* node flags that must not be set */
};
int mandocdb(int, char *[]);
static void dbadd(struct dba *, struct mpage *);
static void dbadd_mlink(const struct mlink *mlink);
static void dbprune(struct dba *);
static void dbwrite(struct dba *);
static void filescan(const char *);
#if HAVE_FTS_COMPARE_CONST
static int fts_compare(const FTSENT *const *, const FTSENT *const *);
#else
static int fts_compare(const FTSENT **, const FTSENT **);
#endif
static void mlink_add(struct mlink *, const struct stat *);
static void mlink_check(struct mpage *, struct mlink *);
static void mlink_free(struct mlink *);
static void mlinks_undupe(struct mpage *);
static void mpages_free(void);
static void mpages_merge(struct dba *, struct mparse *);
static void parse_cat(struct mpage *, int);
static void parse_man(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static void parse_mdoc(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static int parse_mdoc_head(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static int parse_mdoc_Fd(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static void parse_mdoc_fname(struct mpage *, const struct roff_node *);
static int parse_mdoc_Fn(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static int parse_mdoc_Fo(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static int parse_mdoc_Nd(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static int parse_mdoc_Nm(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static int parse_mdoc_Sh(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static int parse_mdoc_Va(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static int parse_mdoc_Xr(struct mpage *, const struct roff_meta *,
const struct roff_node *);
static void putkey(const struct mpage *, char *, uint64_t);
static void putkeys(const struct mpage *, char *, size_t, uint64_t);
static void putmdockey(const struct mpage *,
const struct roff_node *, uint64_t, int);
static int render_string(char **, size_t *);
static void say(const char *, const char *, ...)
__attribute__((__format__ (__printf__, 2, 3)));
static int set_basedir(const char *, int);
static int treescan(void);
static size_t utf8(unsigned int, char [7]);
static int nodb; /* no database changes */
static int mparse_options; /* abort the parse early */
static int use_all; /* use all found files */
static int debug; /* print what we're doing */
static int warnings; /* warn about crap */
static int write_utf8; /* write UTF-8 output; else ASCII */
static int exitcode; /* to be returned by main */
static enum op op; /* operational mode */
static char basedir[PATH_MAX]; /* current base directory */
static struct mpage *mpage_head; /* list of distinct manual pages */
static struct ohash mpages; /* table of distinct manual pages */
static struct ohash mlinks; /* table of directory entries */
static struct ohash names; /* table of all names */
static struct ohash strings; /* table of all strings */
static uint64_t name_mask;
static const struct mdoc_handler __mdocs[MDOC_MAX - MDOC_Dd] = {
{ NULL, 0, NODE_NOPRT }, /* Dd */
{ NULL, 0, NODE_NOPRT }, /* Dt */
{ NULL, 0, NODE_NOPRT }, /* Os */
{ parse_mdoc_Sh, TYPE_Sh, 0 }, /* Sh */
{ parse_mdoc_head, TYPE_Ss, 0 }, /* Ss */
{ NULL, 0, 0 }, /* Pp */
{ NULL, 0, 0 }, /* D1 */
{ NULL, 0, 0 }, /* Dl */
{ NULL, 0, 0 }, /* Bd */
{ NULL, 0, 0 }, /* Ed */
{ NULL, 0, 0 }, /* Bl */
{ NULL, 0, 0 }, /* El */
{ NULL, 0, 0 }, /* It */
{ NULL, 0, 0 }, /* Ad */
{ NULL, TYPE_An, 0 }, /* An */
{ NULL, 0, 0 }, /* Ap */
{ NULL, TYPE_Ar, 0 }, /* Ar */
{ NULL, TYPE_Cd, 0 }, /* Cd */
{ NULL, TYPE_Cm, 0 }, /* Cm */
{ NULL, TYPE_Dv, 0 }, /* Dv */
{ NULL, TYPE_Er, 0 }, /* Er */
{ NULL, TYPE_Ev, 0 }, /* Ev */
{ NULL, 0, 0 }, /* Ex */
{ NULL, TYPE_Fa, 0 }, /* Fa */
{ parse_mdoc_Fd, 0, 0 }, /* Fd */
{ NULL, TYPE_Fl, 0 }, /* Fl */
{ parse_mdoc_Fn, 0, 0 }, /* Fn */
{ NULL, TYPE_Ft, 0 }, /* Ft */
{ NULL, TYPE_Ic, 0 }, /* Ic */
{ NULL, TYPE_In, 0 }, /* In */
{ NULL, TYPE_Li, 0 }, /* Li */
{ parse_mdoc_Nd, 0, 0 }, /* Nd */
{ parse_mdoc_Nm, 0, 0 }, /* Nm */
{ NULL, 0, 0 }, /* Op */
{ NULL, 0, 0 }, /* Ot */
{ NULL, TYPE_Pa, NODE_NOSRC }, /* Pa */
{ NULL, 0, 0 }, /* Rv */
{ NULL, TYPE_St, 0 }, /* St */
{ parse_mdoc_Va, TYPE_Va, 0 }, /* Va */
{ parse_mdoc_Va, TYPE_Vt, 0 }, /* Vt */
{ parse_mdoc_Xr, 0, 0 }, /* Xr */
{ NULL, 0, 0 }, /* %A */
{ NULL, 0, 0 }, /* %B */
{ NULL, 0, 0 }, /* %D */
{ NULL, 0, 0 }, /* %I */
{ NULL, 0, 0 }, /* %J */
{ NULL, 0, 0 }, /* %N */
{ NULL, 0, 0 }, /* %O */
{ NULL, 0, 0 }, /* %P */
{ NULL, 0, 0 }, /* %R */
{ NULL, 0, 0 }, /* %T */
{ NULL, 0, 0 }, /* %V */
{ NULL, 0, 0 }, /* Ac */
{ NULL, 0, 0 }, /* Ao */
{ NULL, 0, 0 }, /* Aq */
{ NULL, TYPE_At, 0 }, /* At */
{ NULL, 0, 0 }, /* Bc */
{ NULL, 0, 0 }, /* Bf */
{ NULL, 0, 0 }, /* Bo */
{ NULL, 0, 0 }, /* Bq */
{ NULL, TYPE_Bsx, NODE_NOSRC }, /* Bsx */
{ NULL, TYPE_Bx, NODE_NOSRC }, /* Bx */
{ NULL, 0, 0 }, /* Db */
{ NULL, 0, 0 }, /* Dc */
{ NULL, 0, 0 }, /* Do */
{ NULL, 0, 0 }, /* Dq */
{ NULL, 0, 0 }, /* Ec */
{ NULL, 0, 0 }, /* Ef */
{ NULL, TYPE_Em, 0 }, /* Em */
{ NULL, 0, 0 }, /* Eo */
{ NULL, TYPE_Fx, NODE_NOSRC }, /* Fx */
{ NULL, TYPE_Ms, 0 }, /* Ms */
{ NULL, 0, 0 }, /* No */
{ NULL, 0, 0 }, /* Ns */
{ NULL, TYPE_Nx, NODE_NOSRC }, /* Nx */
{ NULL, TYPE_Ox, NODE_NOSRC }, /* Ox */
{ NULL, 0, 0 }, /* Pc */
{ NULL, 0, 0 }, /* Pf */
{ NULL, 0, 0 }, /* Po */
{ NULL, 0, 0 }, /* Pq */
{ NULL, 0, 0 }, /* Qc */
{ NULL, 0, 0 }, /* Ql */
{ NULL, 0, 0 }, /* Qo */
{ NULL, 0, 0 }, /* Qq */
{ NULL, 0, 0 }, /* Re */
{ NULL, 0, 0 }, /* Rs */
{ NULL, 0, 0 }, /* Sc */
{ NULL, 0, 0 }, /* So */
{ NULL, 0, 0 }, /* Sq */
{ NULL, 0, 0 }, /* Sm */
{ NULL, 0, 0 }, /* Sx */
{ NULL, TYPE_Sy, 0 }, /* Sy */
{ NULL, TYPE_Tn, 0 }, /* Tn */
{ NULL, 0, NODE_NOSRC }, /* Ux */
{ NULL, 0, 0 }, /* Xc */
{ NULL, 0, 0 }, /* Xo */
{ parse_mdoc_Fo, 0, 0 }, /* Fo */
{ NULL, 0, 0 }, /* Fc */
{ NULL, 0, 0 }, /* Oo */
{ NULL, 0, 0 }, /* Oc */
{ NULL, 0, 0 }, /* Bk */
{ NULL, 0, 0 }, /* Ek */
{ NULL, 0, 0 }, /* Bt */
{ NULL, 0, 0 }, /* Hf */
{ NULL, 0, 0 }, /* Fr */
{ NULL, 0, 0 }, /* Ud */
{ NULL, TYPE_Lb, NODE_NOSRC }, /* Lb */
{ NULL, 0, 0 }, /* Lp */
{ NULL, TYPE_Lk, 0 }, /* Lk */
{ NULL, TYPE_Mt, NODE_NOSRC }, /* Mt */
{ NULL, 0, 0 }, /* Brq */
{ NULL, 0, 0 }, /* Bro */
{ NULL, 0, 0 }, /* Brc */
{ NULL, 0, 0 }, /* %C */
{ NULL, 0, 0 }, /* Es */
{ NULL, 0, 0 }, /* En */
{ NULL, TYPE_Dx, NODE_NOSRC }, /* Dx */
{ NULL, 0, 0 }, /* %Q */
{ NULL, 0, 0 }, /* %U */
{ NULL, 0, 0 }, /* Ta */
};
static const struct mdoc_handler *const mdocs = __mdocs - MDOC_Dd;
int
mandocdb(int argc, char *argv[])
{
struct manconf conf;
struct mparse *mp;
struct dba *dba;
const char *path_arg, *progname;
size_t j, sz;
int ch, i;
#if HAVE_PLEDGE
if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) {
warn("pledge");
return (int)MANDOCLEVEL_SYSERR;
}
#endif
#if HAVE_SANDBOX_INIT
if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1) {
warnx("sandbox_init");
return (int)MANDOCLEVEL_SYSERR;
}
#endif
memset(&conf, 0, sizeof(conf));
/*
* We accept a few different invocations.
* The CHECKOP macro makes sure that invocation styles don't
* clobber each other.
*/
#define CHECKOP(_op, _ch) do \
if (OP_DEFAULT != (_op)) { \
warnx("-%c: Conflicting option", (_ch)); \
goto usage; \
} while (/*CONSTCOND*/0)
path_arg = NULL;
op = OP_DEFAULT;
while (-1 != (ch = getopt(argc, argv, "aC:Dd:npQT:tu:v")))
switch (ch) {
case 'a':
use_all = 1;
break;
case 'C':
CHECKOP(op, ch);
path_arg = optarg;
op = OP_CONFFILE;
break;
case 'D':
debug++;
break;
case 'd':
CHECKOP(op, ch);
path_arg = optarg;
op = OP_UPDATE;
break;
case 'n':
nodb = 1;
break;
case 'p':
warnings = 1;
break;
case 'Q':
mparse_options |= MPARSE_QUICK;
break;
case 'T':
if (strcmp(optarg, "utf8")) {
warnx("-T%s: Unsupported output format",
optarg);
goto usage;
}
write_utf8 = 1;
break;
case 't':
CHECKOP(op, ch);
dup2(STDOUT_FILENO, STDERR_FILENO);
op = OP_TEST;
nodb = warnings = 1;
break;
case 'u':
CHECKOP(op, ch);
path_arg = optarg;
op = OP_DELETE;
break;
case 'v':
/* Compatibility with espie@'s makewhatis. */
break;
default:
goto usage;
}
argc -= optind;
argv += optind;
#if HAVE_PLEDGE
if (nodb) {
if (pledge("stdio rpath", NULL) == -1) {
warn("pledge");
return (int)MANDOCLEVEL_SYSERR;
}
}
#endif
if (OP_CONFFILE == op && argc > 0) {
warnx("-C: Too many arguments");
goto usage;
}
exitcode = (int)MANDOCLEVEL_OK;
mchars_alloc();
mp = mparse_alloc(mparse_options, MANDOCERR_MAX, NULL,
MANDOC_OS_OTHER, NULL);
mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev));
mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file));
if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
/*
* Most of these deal with a specific directory.
* Jump into that directory first.
*/
if (OP_TEST != op && 0 == set_basedir(path_arg, 1))
goto out;
dba = nodb ? dba_new(128) : dba_read(MANDOC_DB);
if (dba != NULL) {
/*
* The existing database is usable. Process
* all files specified on the command-line.
*/
#if HAVE_PLEDGE
if (!nodb) {
if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1) {
warn("pledge");
exitcode = (int)MANDOCLEVEL_SYSERR;
goto out;
}
}
#endif
use_all = 1;
for (i = 0; i < argc; i++)
filescan(argv[i]);
if (nodb == 0)
dbprune(dba);
} else {
/* Database missing or corrupt. */
if (op != OP_UPDATE || errno != ENOENT)
say(MANDOC_DB, "%s: Automatically recreating"
" from scratch", strerror(errno));
exitcode = (int)MANDOCLEVEL_OK;
op = OP_DEFAULT;
if (0 == treescan())
goto out;
dba = dba_new(128);
}
if (OP_DELETE != op)
mpages_merge(dba, mp);
if (nodb == 0)
dbwrite(dba);
dba_free(dba);
} else {
/*
* If we have arguments, use them as our manpaths.
* If we don't, use man.conf(5).
*/
if (argc > 0) {
conf.manpath.paths = mandoc_reallocarray(NULL,
argc, sizeof(char *));
conf.manpath.sz = (size_t)argc;
for (i = 0; i < argc; i++)
conf.manpath.paths[i] = mandoc_strdup(argv[i]);
} else
manconf_parse(&conf, path_arg, NULL, NULL);
if (conf.manpath.sz == 0) {
exitcode = (int)MANDOCLEVEL_BADARG;
say("", "Empty manpath");
}
/*
* First scan the tree rooted at a base directory, then
* build a new database and finally move it into place.
* Ignore zero-length directories and strip trailing
* slashes.
*/
for (j = 0; j < conf.manpath.sz; j++) {
sz = strlen(conf.manpath.paths[j]);
if (sz && conf.manpath.paths[j][sz - 1] == '/')
conf.manpath.paths[j][--sz] = '\0';
if (0 == sz)
continue;
if (j) {
mandoc_ohash_init(&mpages, 6,
offsetof(struct mpage, inodev));
mandoc_ohash_init(&mlinks, 6,
offsetof(struct mlink, file));
}
if ( ! set_basedir(conf.manpath.paths[j], argc > 0))
continue;
if (0 == treescan())
continue;
dba = dba_new(128);
mpages_merge(dba, mp);
if (nodb == 0)
dbwrite(dba);
dba_free(dba);
if (j + 1 < conf.manpath.sz) {
mpages_free();
ohash_delete(&mpages);
ohash_delete(&mlinks);
}
}
}
out:
manconf_free(&conf);
mparse_free(mp);
mchars_free();
mpages_free();
ohash_delete(&mpages);
ohash_delete(&mlinks);
return exitcode;
usage:
progname = getprogname();
fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
" %s [-aDnpQ] [-Tutf8] dir ...\n"
" %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
" %s [-Dnp] -u dir [file ...]\n"
" %s [-Q] -t file ...\n",
progname, progname, progname, progname, progname);
return (int)MANDOCLEVEL_BADARG;
}
/*
* To get a singly linked list in alpha order while inserting entries
* at the beginning, process directory entries in reverse alpha order.
*/
static int
#if HAVE_FTS_COMPARE_CONST
fts_compare(const FTSENT *const *a, const FTSENT *const *b)
#else
fts_compare(const FTSENT **a, const FTSENT **b)
#endif
{
return -strcmp((*a)->fts_name, (*b)->fts_name);
}
/*
* Scan a directory tree rooted at "basedir" for manpages.
* We use fts(), scanning directory parts along the way for clues to our
* section and architecture.
*
* If use_all has been specified, grok all files.
* If not, sanitise paths to the following:
*
* [./]man*[/<arch>]/<name>.<section>
* or
* [./]cat<section>[/<arch>]/<name>.0
*
* TODO: accommodate for multi-language directories.
*/
static int
treescan(void)
{
char buf[PATH_MAX];
FTS *f;
FTSENT *ff;
struct mlink *mlink;
int gzip;
enum form dform;
char *dsec, *arch, *fsec, *cp;
const char *path;
const char *argv[2];
argv[0] = ".";
argv[1] = NULL;
f = fts_open((char * const *)argv, FTS_PHYSICAL | FTS_NOCHDIR,
fts_compare);
if (f == NULL) {
exitcode = (int)MANDOCLEVEL_SYSERR;
say("", "&fts_open");
return 0;
}
dsec = arch = NULL;
dform = FORM_NONE;
while ((ff = fts_read(f)) != NULL) {
path = ff->fts_path + 2;
switch (ff->fts_info) {
/*
* Symbolic links require various sanity checks,
* then get handled just like regular files.
*/
case FTS_SL:
if (realpath(path, buf) == NULL) {
if (warnings)
say(path, "&realpath");
continue;
}
if (strstr(buf, basedir) != buf
#ifdef HOMEBREWDIR
&& strstr(buf, HOMEBREWDIR) != buf
#endif
) {
if (warnings) say("",
"%s: outside base directory", buf);
continue;
}
/* Use logical inode to avoid mpages dupe. */
if (stat(path, ff->fts_statp) == -1) {
if (warnings)
say(path, "&stat");
continue;
}
/* FALLTHROUGH */
/*
* If we're a regular file, add an mlink by using the
* stored directory data and handling the filename.
*/
case FTS_F:
if ( ! strcmp(path, MANDOC_DB))
continue;
if ( ! use_all && ff->fts_level < 2) {
if (warnings)
say(path, "Extraneous file");
continue;
}
gzip = 0;
fsec = NULL;
while (fsec == NULL) {
fsec = strrchr(ff->fts_name, '.');
if (fsec == NULL || strcmp(fsec+1, "gz"))
break;
gzip = 1;
*fsec = '\0';
fsec = NULL;
}
if (fsec == NULL) {
if ( ! use_all) {
if (warnings)
say(path,
"No filename suffix");
continue;
}
} else if ( ! strcmp(++fsec, "html")) {
if (warnings)
say(path, "Skip html");
continue;
} else if ( ! strcmp(fsec, "ps")) {
if (warnings)
say(path, "Skip ps");
continue;
} else if ( ! strcmp(fsec, "pdf")) {
if (warnings)
say(path, "Skip pdf");
continue;
} else if ( ! use_all &&
((dform == FORM_SRC &&
strncmp(fsec, dsec, strlen(dsec))) ||
(dform == FORM_CAT && strcmp(fsec, "0")))) {
if (warnings)
say(path, "Wrong filename suffix");
continue;
} else
fsec[-1] = '\0';
mlink = mandoc_calloc(1, sizeof(struct mlink));
if (strlcpy(mlink->file, path,
sizeof(mlink->file)) >=
sizeof(mlink->file)) {
say(path, "Filename too long");
free(mlink);
continue;
}
mlink->dform = dform;
mlink->dsec = dsec;
mlink->arch = arch;
mlink->name = ff->fts_name;
mlink->fsec = fsec;
mlink->gzip = gzip;
mlink_add(mlink, ff->fts_statp);
continue;
case FTS_D:
case FTS_DP:
break;
default:
if (warnings)
say(path, "Not a regular file");
continue;
}
switch (ff->fts_level) {
case 0:
/* Ignore the root directory. */
break;
case 1:
/*
* This might contain manX/ or catX/.
* Try to infer this from the name.
* If we're not in use_all, enforce it.
*/
cp = ff->fts_name;
if (ff->fts_info == FTS_DP) {
dform = FORM_NONE;
dsec = NULL;
break;
}
if ( ! strncmp(cp, "man", 3)) {
dform = FORM_SRC;
dsec = cp + 3;
} else if ( ! strncmp(cp, "cat", 3)) {
dform = FORM_CAT;
dsec = cp + 3;
} else {
dform = FORM_NONE;
dsec = NULL;
}
if (dsec != NULL || use_all)
break;
if (warnings)
say(path, "Unknown directory part");
fts_set(f, ff, FTS_SKIP);
break;
case 2:
/*
* Possibly our architecture.
* If we're descending, keep tabs on it.
*/
if (ff->fts_info != FTS_DP && dsec != NULL)
arch = ff->fts_name;
else
arch = NULL;
break;
default:
if (ff->fts_info == FTS_DP || use_all)
break;
if (warnings)
say(path, "Extraneous directory part");
fts_set(f, ff, FTS_SKIP);
break;
}
}
fts_close(f);
return 1;
}
/*
* Add a file to the mlinks table.
* Do not verify that it's a "valid" looking manpage (we'll do that
* later).
*
* Try to infer the manual section, architecture, and page name from the
* path, assuming it looks like
*
* [./]man*[/<arch>]/<name>.<section>
* or
* [./]cat<section>[/<arch>]/<name>.0
*
* See treescan() for the fts(3) version of this.
*/
static void
filescan(const char *file)
{
char buf[PATH_MAX];
struct stat st;
struct mlink *mlink;
char *p, *start;
assert(use_all);
if (0 == strncmp(file, "./", 2))
file += 2;
/*
* We have to do lstat(2) before realpath(3) loses
* the information whether this is a symbolic link.
* We need to know that because for symbolic links,
* we want to use the orginal file name, while for
* regular files, we want to use the real path.
*/
if (-1 == lstat(file, &st)) {
exitcode = (int)MANDOCLEVEL_BADARG;
say(file, "&lstat");
return;
} else if (0 == ((S_IFREG | S_IFLNK) & st.st_mode)) {
exitcode = (int)MANDOCLEVEL_BADARG;
say(file, "Not a regular file");
return;
}
/*
* We have to resolve the file name to the real path
* in any case for the base directory check.
*/
if (NULL == realpath(file, buf)) {
exitcode = (int)MANDOCLEVEL_BADARG;
say(file, "&realpath");
return;
}
if (OP_TEST == op)
start = buf;
else if (strstr(buf, basedir) == buf)
start = buf + strlen(basedir);
#ifdef HOMEBREWDIR
else if (strstr(buf, HOMEBREWDIR) == buf)
start = buf;
#endif
else {
exitcode = (int)MANDOCLEVEL_BADARG;
say("", "%s: outside base directory", buf);
return;
}
/*
* Now we are sure the file is inside our tree.
* If it is a symbolic link, ignore the real path
* and use the original name.
* This implies passing stuff like "cat1/../man1/foo.1"
* on the command line won't work. So don't do that.
* Note the stat(2) can still fail if the link target
* doesn't exist.
*/
if (S_IFLNK & st.st_mode) {
if (-1 == stat(buf, &st)) {
exitcode = (int)MANDOCLEVEL_BADARG;
say(file, "&stat");
return;
}
if (strlcpy(buf, file, sizeof(buf)) >= sizeof(buf)) {
say(file, "Filename too long");
return;
}
start = buf;
if (OP_TEST != op && strstr(buf, basedir) == buf)
start += strlen(basedir);
}
mlink = mandoc_calloc(1, sizeof(struct mlink));
mlink->dform = FORM_NONE;
if (strlcpy(mlink->file, start, sizeof(mlink->file)) >=
sizeof(mlink->file)) {
say(start, "Filename too long");
free(mlink);
return;
}
/*
* In test mode or when the original name is absolute
* but outside our tree, guess the base directory.
*/
if (op == OP_TEST || (start == buf && *start == '/')) {
if (strncmp(buf, "man/", 4) == 0)
start = buf + 4;
else if ((start = strstr(buf, "/man/")) != NULL)
start += 5;
else
start = buf;
}
/*
* First try to guess our directory structure.
* If we find a separator, try to look for man* or cat*.
* If we find one of these and what's underneath is a directory,
* assume it's an architecture.
*/
if (NULL != (p = strchr(start, '/'))) {
*p++ = '\0';
if (0 == strncmp(start, "man", 3)) {
mlink->dform = FORM_SRC;
mlink->dsec = start + 3;
} else if (0 == strncmp(start, "cat", 3)) {
mlink->dform = FORM_CAT;
mlink->dsec = start + 3;
}
start = p;
if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) {
*p++ = '\0';
mlink->arch = start;
start = p;
}
}
/*
* Now check the file suffix.
* Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
*/
p = strrchr(start, '\0');
while (p-- > start && '/' != *p && '.' != *p)
/* Loop. */ ;
if ('.' == *p) {
*p++ = '\0';
mlink->fsec = p;
}
/*
* Now try to parse the name.
* Use the filename portion of the path.
*/
mlink->name = start;
if (NULL != (p = strrchr(start, '/'))) {
mlink->name = p + 1;
*p = '\0';
}
mlink_add(mlink, &st);
}
static void
mlink_add(struct mlink *mlink, const struct stat *st)
{
struct inodev inodev;
struct mpage *mpage;
unsigned int slot;
assert(NULL != mlink->file);
mlink->dsec = mandoc_strdup(mlink->dsec ? mlink->dsec : "");
mlink->arch = mandoc_strdup(mlink->arch ? mlink->arch : "");
mlink->name = mandoc_strdup(mlink->name ? mlink->name : "");
mlink->fsec = mandoc_strdup(mlink->fsec ? mlink->fsec : "");
if ('0' == *mlink->fsec) {
free(mlink->fsec);
mlink->fsec = mandoc_strdup(mlink->dsec);
mlink->fform = FORM_CAT;
} else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec)
mlink->fform = FORM_SRC;
else
mlink->fform = FORM_NONE;
slot = ohash_qlookup(&mlinks, mlink->file);
assert(NULL == ohash_find(&mlinks, slot));
ohash_insert(&mlinks, slot, mlink);
memset(&inodev, 0, sizeof(inodev)); /* Clear padding. */
inodev.st_ino = st->st_ino;
inodev.st_dev = st->st_dev;
slot = ohash_lookup_memory(&mpages, (char *)&inodev,
sizeof(struct inodev), inodev.st_ino);
mpage = ohash_find(&mpages, slot);
if (NULL == mpage) {
mpage = mandoc_calloc(1, sizeof(struct mpage));
mpage->inodev.st_ino = inodev.st_ino;
mpage->inodev.st_dev = inodev.st_dev;
mpage->form = FORM_NONE;
mpage->next = mpage_head;
mpage_head = mpage;
ohash_insert(&mpages, slot, mpage);
} else
mlink->next = mpage->mlinks;
mpage->mlinks = mlink;
mlink->mpage = mpage;
}
static void
mlink_free(struct mlink *mlink)
{
free(mlink->dsec);
free(mlink->arch);
free(mlink->name);
free(mlink->fsec);
free(mlink);
}
static void
mpages_free(void)
{
struct mpage *mpage;
struct mlink *mlink;
while ((mpage = mpage_head) != NULL) {