bump release to v3.6-pre5
[mit.git] / modindex.c
bloba167d3a7115d2509c153a74ccded8dfabee0d4f4
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <getopt.h>
4 #include <errno.h>
5 #include <string.h>
7 #include "logging.h"
8 #include "index.h"
10 static char *getline_wrapped(FILE *file, unsigned int *linenum)
12 int size = 256;
13 int i = 0;
14 char *buf = NOFAIL(malloc(size));
15 for(;;) {
16 int ch = getc_unlocked(file);
18 switch(ch) {
19 case EOF:
20 if (i == 0) {
21 free(buf);
22 return NULL;
24 /* else fall through */
26 case '\n':
27 if (linenum)
28 (*linenum)++;
29 if (i == size)
30 buf = NOFAIL(realloc(buf, size + 1));
31 buf[i] = '\0';
32 return buf;
34 case '\\':
35 ch = getc_unlocked(file);
37 if (ch == '\n') {
38 if (linenum)
39 (*linenum)++;
40 continue;
42 /* else fall through */
44 default:
45 buf[i++] = ch;
47 if (i == size) {
48 size *= 2;
49 buf = NOFAIL(realloc(buf, size));
55 static void write_index(const char *filename)
57 struct index_node *index;
58 char *line;
59 FILE *cfile;
61 cfile = fopen(filename, "w");
62 if (!cfile)
63 fatal("Could not open %s for writing: %s\n",
64 filename, strerror(errno));
66 index = index_create();
68 while((line = getline_wrapped(stdin, NULL))) {
69 index_insert(index, line);
70 free(line);
73 index_write(index, cfile);
74 index_destroy(index);
75 fclose(cfile);
78 static void dump_index(const char *filename)
80 FILE *cfile;
82 cfile = fopen(filename, "r");
83 if (!cfile)
84 fatal("Could not open %s for reading: %s\n",
85 filename, strerror(errno));
87 index_dump(cfile, stdout, "");
89 fclose(cfile);
92 static void search_index(const char *filename, char *key)
94 char *value;
95 FILE *cfile;
97 cfile = fopen(filename, "r");
98 if (!cfile)
99 fatal("Could not open %s for reading: %s\n",
100 filename, strerror(errno));
102 value = index_search(cfile, key);
103 if (value)
104 printf("Found value:\n%s\n", value);
105 else
106 printf("Not found.\n");
108 free(value);
109 fclose(cfile);
112 static void searchwild_index(const char *filename, char *key)
114 struct index_value *values;
115 FILE *cfile;
117 cfile = fopen(filename, "r");
118 if (!cfile)
119 fatal("Could not open %s for reading: %s\n",
120 filename, strerror(errno));
122 values = index_searchwild(cfile, key);
123 if (values)
124 printf("Found value(s):\n");
125 else
126 printf("Not found.\n");
128 while(values) {
129 struct index_value *next = values->next;
131 printf("%s\n", values->value);
132 free(values);
133 values = next;
136 fclose(cfile);
139 static void print_usage(const char *progname)
141 fprintf(stderr,
142 "Usage: %s [MODE] [FILE] ...\n"
143 " -o, --output <outfile>\n"
144 " -d, --dump <infile>\n"
145 " -s, --search <infile> <key>\n"
146 " -w, --searchwild <infile> <key>\n"
147 ,progname);
148 exit(1);
151 static struct option options[] = {
152 { "output", 0, NULL, 'o' },
153 { "dump", 0, NULL, 'd' },
154 { "search", 0, NULL, 's' },
155 { "searchwild", 0, NULL, 'w' },
158 int main(int argc, char *argv[])
160 char opt;
161 char mode = 0;
162 char *filename = NULL;
163 char *key = NULL;
165 while ((opt = getopt_long(argc, argv, "odsw", options, NULL))
166 != -1) {
167 switch (opt) {
168 case 'o':
169 mode = 'o';
170 break;
171 case 'd':
172 mode = 'd';
173 break;
174 case 's':
175 mode = 's';
176 break;
177 case 'w':
178 mode = 'w';
179 break;
180 default:
181 print_usage(argv[0]);
184 if (!mode)
185 print_usage(argv[0]);
187 if (optind >= argc)
188 print_usage(argv[0]);
189 filename = argv[optind];
191 if (mode == 's' || mode == 'w') {
192 if (optind+1 >= argc)
193 print_usage(argv[0]);
194 key = argv[optind+1];
197 switch(mode) {
198 case 'o':
199 write_index(filename);
200 break;
201 case 'd':
202 dump_index(filename);
203 break;
204 case 's':
205 search_index(filename, key);
206 break;
207 case 'w':
208 searchwild_index(filename, key);
209 break;
212 return 0;