Merged with tip.
[cacao.git] / contrib / vmlog / vmlogindex.c
1 /* vmlog - high-speed logging for free VMs                  */
2 /* Copyright (C) 2006 Edwin Steiner <edwin.steiner@gmx.net> */
3
4 /* This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "vmlog.h"
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <ctype.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32
33 char *opt_listfname;
34 char *opt_prefix;
35
36 char *g_idxfname;
37 char *g_strfname;
38
39 const char *usage =
40 "Usage: vmlogindex prefix listfile\n";
41
42 #define VMLOG_MAXLINELENGTH  1024*16
43
44 void parse_command_line(int argc,char **argv)
45 {
46         int r;
47
48         while (1) {
49                 r = getopt(argc,argv,"");
50                 if (r == -1)
51                         break;
52                 switch (r) {
53                         case '?': vmlog_die("invalid option");
54                 }
55         }
56
57         if (argc - optind < 2)
58                 vmlog_die_usage(usage,1);
59
60         opt_prefix = argv[optind++];
61         opt_listfname = argv[optind++];
62 }
63
64 int main(int argc,char **argv)
65 {
66         int i;
67         int len;
68         vmlog_log *vml;
69         char *buf;
70         FILE *listfile;
71         char *p;
72         int warnws;
73         
74         if (argc)
75                 vmlog_set_progname(argv[0]);
76         parse_command_line(argc,argv);
77
78         listfile = fopen(opt_listfname,"r");
79         if (!listfile)
80                 vmlog_die("could not open file: %s: %s",opt_listfname,strerror(errno));
81         
82         vml = vmlog_log_new(opt_prefix,1);
83
84         buf = VMLOG_NEW_ARRAY(char,VMLOG_MAXLINELENGTH+1);
85         while (!feof(listfile)) {
86                 p = fgets(buf,VMLOG_MAXLINELENGTH,listfile);
87                 if (!p) {
88                         if (ferror(listfile))
89                                 vmlog_die("reading from file: %s: %s",opt_listfname,strerror(errno));
90                         assert(feof(listfile));
91                         break;
92                 }
93
94                 len = strlen(p);
95                 if (p[len-1] != '\n') {
96                         vmlog_warn("line without newline or overlong line read");
97                 }
98                 else {
99                         /* chop of newline */
100                         p[--len] = 0;
101                 }
102                 warnws = 0;
103                 while (isspace(p[len-1])) {
104                         p[--len] = 0;
105                         warnws = 1;
106                 }
107                 if (warnws) {
108                         vmlog_warn("removed trailing whitespace from line");
109                 }
110                 i = vmlog_get_string_index(vml,buf,len);
111         }
112
113         vmlog_log_free(vml);
114         fclose(listfile);
115
116         return 0;
117 }
118
119 /* vim: noet ts=8 sw=8
120  */
121