meta: update various documentation, logs, authors, etc.
[mit.git] / modindex.c
blobda397d09d0c2a0c6a84e9b59371a1df163066810
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <getopt.h>
4 #include <errno.h>
5 #include <string.h>
7 #include "util.h"
8 #include "logging.h"
9 #include "index.h"
11 static void write_index(const char *filename)
13 struct index_node *index;
14 char *line, *pos;
15 FILE *cfile;
16 unsigned int linenum = 0;
18 cfile = fopen(filename, "w");
19 if (!cfile)
20 fatal("Could not open %s for writing: %s\n",
21 filename, strerror(errno));
23 index = index_create();
25 while((line = getline_wrapped(stdin, &linenum))) {
26 pos = strchr(line, ' ');
27 *pos++ = '\0';
28 index_insert(index, line, pos, linenum);
29 free(line);
32 index_write(index, cfile);
33 index_destroy(index);
34 fclose(cfile);
37 static struct index_file *open_index(const char *filename)
39 struct index_file *index;
41 index = index_file_open(filename);
42 if (!index) {
43 if (errno == EINVAL)
44 fatal("%s has wrong magic or version number", filename);
46 fatal("Could not open %s for reading: %s\n",
47 filename, strerror(errno));
50 return index;
53 static void dump_index(const char *filename)
55 struct index_file *index = open_index(filename);
57 index_dump(index, stdout, "");
59 index_file_close(index);
62 static void search_index(const char *filename, char *key)
64 struct index_file *index = open_index(filename);
65 char *value;
67 value = index_search(index, key);
68 if (value)
69 printf("Found value:\n%s\n", value);
70 else
71 printf("Not found.\n");
73 free(value);
74 index_file_close(index);
77 static void searchwild_index(const char *filename, char *key)
79 struct index_file *index = open_index(filename);
80 struct index_value *values, *v;
82 values = index_searchwild(index, key);
83 if (values)
84 printf("Found value(s):\n");
85 else
86 printf("Not found.\n");
88 for (v = values; v; v = v->next)
89 printf("%s\n", v->value);
91 index_values_free(values);
92 index_file_close(index);
95 static void print_usage(const char *progname)
97 fprintf(stderr,
98 "Usage: %s [MODE] [FILE] ...\n"
99 " -o, --output <outfile>\n"
100 " -d, --dump <infile>\n"
101 " -s, --search <key> <infile>\n"
102 " -w, --searchwild <key> <infile>\n"
103 ,progname);
104 exit(1);
107 static struct option options[] = {
108 { "output", 0, NULL, 'o' },
109 { "dump", 0, NULL, 'd' },
110 { "search", 1, NULL, 's' },
111 { "searchwild", 1, NULL, 'w' },
114 int main(int argc, char *argv[])
116 int opt;
117 char mode = 0;
118 char *filename = NULL;
119 char *key = NULL;
121 while ((opt = getopt_long(argc, argv, "ods:w:", options, NULL))
122 != -1) {
123 switch (opt) {
124 case 'o':
125 mode = 'o';
126 break;
127 case 'd':
128 mode = 'd';
129 break;
130 case 's':
131 mode = 's';
132 key = optarg;
133 break;
134 case 'w':
135 mode = 'w';
136 key = optarg;
137 break;
138 default:
139 print_usage(argv[0]);
142 if (!mode)
143 print_usage(argv[0]);
145 if (optind >= argc)
146 print_usage(argv[0]);
147 filename = argv[optind];
149 switch(mode) {
150 case 'o':
151 write_index(filename);
152 break;
153 case 'd':
154 dump_index(filename);
155 break;
156 case 's':
157 search_index(filename, key);
158 break;
159 case 'w':
160 searchwild_index(filename, key);
161 break;
164 return 0;