]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/dict/mkindex.c
cc: use 7 octal digits for 21 bit runes
[plan9front.git] / sys / src / cmd / dict / mkindex.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "dict.h"
5
6 /*
7  * Use this to start making an index for a new dictionary.
8  * Get the dictionary-specific nextoff and printentry(_,'h')
9  * commands working, add a record to the dicts[] array below,
10  * and run this program to get a list of offset,headword
11  * pairs
12  */
13 Biobuf  boutbuf;
14 Biobuf  *bdict;
15 Biobuf  *bout = &boutbuf;
16 int     linelen;
17 int     breaklen = 2000;
18 int     outinhibit;
19 int     debug;
20
21 Dict    *dict;  /* current dictionary */
22
23 Entry   getentry(long);
24
25 void
26 usage(void)
27 {
28         fprint(2, "usage: %s [-D] [-d dictname]\n", argv0);
29         exits("usage");
30 }
31
32 void
33 main(int argc, char **argv)
34 {
35         int i;
36         long a, ae;
37         char *p;
38         Entry e;
39
40         Binit(&boutbuf, 1, OWRITE);
41         dict = &dicts[0];
42         ARGBEGIN {
43                 case 'd':
44                         p = EARGF(usage());
45                         dict = 0;
46
47                         for(i=0; dicts[i].name; i++) {
48                                 if(strcmp(p, dicts[i].name)==0) {
49                                         dict = &dicts[i];
50                                         break;
51                                 }
52                         }
53                         if(dict == nil) {
54                                 err("unknown dictionary: %s", p);
55                                 exits("nodict");
56                         }
57                         break;
58                 case 'D':
59                         debug++;
60                         break;
61                 default:
62                         usage();
63         }ARGEND
64         USED(argc,argv);
65         bdict = Bopen(dict->path, OREAD);
66         if(!bdict) {
67                 err("can't open dictionary %s", dict->path);
68                 exits("nodict");
69         }
70         ae = Bseek(bdict, 0, 2);
71         for(a = 0; a < ae; a = (*dict->nextoff)(a+1)) {
72                 linelen = 0;
73                 e = getentry(a);
74                 Bprint(bout, "%ld\t", a);
75                 linelen = 4;    /* only has to be approx right */
76                 (*dict->printentry)(e, 'h');
77         }
78         exits(0);
79 }
80
81 Entry
82 getentry(long b)
83 {
84         long e, n, dtop;
85         static Entry ans;
86         static int anslen = 0;
87
88         e = (*dict->nextoff)(b+1);
89         ans.doff = b;
90         if(e < 0) {
91                 dtop = Bseek(bdict, 0L, 2);
92                 if(b < dtop) {
93                         e = dtop;
94                 } else {
95                         err("couldn't seek to entry");
96                         ans.start = 0;
97                         ans.end = 0;
98                 }
99         }
100         n = e-b;
101         if(n) {
102                 if(n > anslen) {
103                         if((ans.start = realloc(ans.start, n)) == nil)
104                                 sysfatal("realloc: %r");
105                         anslen = n;
106                 }
107                 Bseek(bdict, b, 0);
108                 n = Bread(bdict, ans.start, n);
109                 ans.end = ans.start + n;
110         }
111         return ans;
112 }