Add elf_core.c into the EXTRA_SOURCES for the static library
[mit.git] / modindex.c
blob2798838fc5e9c1e245dbfa461a250782f6fbc1e6
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 <infile> <key>\n"
102 " -w, --searchwild <infile> <key>\n"
103 ,progname);
104 exit(1);
107 static struct option options[] = {
108 { "output", 0, NULL, 'o' },
109 { "dump", 0, NULL, 'd' },
110 { "search", 0, NULL, 's' },
111 { "searchwild", 0, NULL, 'w' },
114 int main(int argc, char *argv[])
116 char opt;
117 char mode = 0;
118 char *filename = NULL;
119 char *key = NULL;
121 while ((opt = getopt_long(argc, argv, "odsw", 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 break;
133 case 'w':
134 mode = 'w';
135 break;
136 default:
137 print_usage(argv[0]);
140 if (!mode)
141 print_usage(argv[0]);
143 if (optind >= argc)
144 print_usage(argv[0]);
145 filename = argv[optind];
147 if (mode == 's' || mode == 'w') {
148 if (optind+1 >= argc)
149 print_usage(argv[0]);
150 key = argv[optind+1];
153 switch(mode) {
154 case 'o':
155 write_index(filename);
156 break;
157 case 'd':
158 dump_index(filename);
159 break;
160 case 's':
161 search_index(filename, key);
162 break;
163 case 'w':
164 searchwild_index(filename, key);
165 break;
168 return 0;