libs/lmo: fix logic errors in po2lmo
[project/luci.git] / libs / lmo / src / lmo_po2lmo.c
1 /*
2  * lmo - Lua Machine Objects - PO to LMO conversion tool
3  *
4  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #include "lmo.h"
20
21 static void die(const char *msg)
22 {
23         fprintf(stderr, "Error: %s\n", msg);
24         exit(1);
25 }
26
27 static void usage(const char *name)
28 {
29         fprintf(stderr, "Usage: %s input.po output.lmo\n", name);
30         exit(1);
31 }
32
33 static void print(const void *ptr, size_t size, size_t nmemb, FILE *stream)
34 {
35         if( fwrite(ptr, size, nmemb, stream) == 0 )
36                 die("Failed to write stdout");
37 }
38
39 static int extract_string(const char *src, char *dest, int len)
40 {
41         int pos = 0;
42         int esc = 0;
43         int off = -1;
44
45         for( pos = 0; (pos < strlen(src)) && (pos < len); pos++ )
46         {
47                 if( (off == -1) && (src[pos] == '"') )
48                 {
49                         off = pos + 1;
50                 }
51                 else if( off >= 0 )
52                 {
53                         if( esc == 1 )
54                         {
55                                 dest[pos-off] = src[pos];
56                                 esc = 0;
57                         }
58                         else if( src[pos] == '\\' )
59                         {
60                                 off++;
61                                 esc = 1;
62                                 
63                         }
64                         else if( src[pos] != '"' )
65                         {
66                                 dest[pos-off] = src[pos];
67                         }
68                         else
69                         {
70                                 dest[pos-off] = '\0';
71                                 break;
72                         }
73                 }
74         }
75
76         return (off > -1) ? strlen(dest) : -1;
77 }
78
79 int main(int argc, char *argv[])
80 {
81         char line[4096];
82         char key[4096];
83         char val[4096];
84         char tmp[4096];
85         int state  = 0;
86         int offset = 0;
87         int length = 0;
88
89         FILE *in;
90         FILE *out;
91
92         lmo_entry_t *head  = NULL;
93         lmo_entry_t *entry = NULL;
94
95         if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) )
96                 usage(argv[0]);
97
98         memset(line, 0, sizeof(key));
99         memset(key, 0, sizeof(val));
100         memset(val, 0, sizeof(val));
101
102         while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) )
103         {
104                 if( state == 0 && strstr(line, "msgid \"") == line )
105                 {
106                         switch(extract_string(line, key, sizeof(key)))
107                         {
108                                 case -1:
109                                         die("Syntax error in msgid");
110                                 case 0:
111                                         state = 1;
112                                         break;
113                                 default:
114                                         state = 2;
115                         }
116                 }
117                 else if( state == 1 || state == 2 )
118                 {
119                         if( strstr(line, "msgstr \"") == line || state == 2 )
120                         {
121                                 switch(extract_string(line, val, sizeof(val)))
122                                 {
123                                         case -1:
124                                                 state = 4;
125                                                 break;
126                                         default:
127                                                 state = 3;
128                                 }
129                         }
130                         else
131                         {
132                                 switch(extract_string(line, tmp, sizeof(tmp)))
133                                 {
134                                         case -1:
135                                                 state = 2;
136                                                 break;
137                                         default:
138                                                 strcat(key, tmp);
139                                 }
140                         }
141                 }
142                 else if( state == 3 )
143                 {
144                         switch(extract_string(line, tmp, sizeof(tmp)))
145                         {
146                                 case -1:
147                                         state = 4;
148                                         break;
149                                 default:
150                                         strcat(val, tmp);
151                         }
152                 }
153
154                 if( state == 4 )
155                 {
156                         if( strlen(key) > 0 && strlen(val) > 0 )
157                         {
158                                 if( (entry = (lmo_entry_t *) malloc(sizeof(lmo_entry_t))) != NULL )
159                                 {
160                                         memset(entry, 0, sizeof(entry));
161                                         length = strlen(val) + ((4 - (strlen(val) % 4)) % 4);
162
163                                         entry->key_id = htonl(sfh_hash(key, strlen(key)));
164                                         entry->val_id = htonl(sfh_hash(val, strlen(val)));
165                                         entry->offset = htonl(offset);
166                                         entry->length = htonl(strlen(val));
167
168                                         print(val, length, 1, out);
169                                         offset += length;
170
171                                         entry->next = head;
172                                         head = entry;
173                                 }
174                                 else
175                                 {
176                                         die("Out of memory");
177                                 }
178                         }
179
180                         state = 0;
181                         memset(key, 0, sizeof(key));
182                         memset(val, 0, sizeof(val));
183                 }
184
185                 memset(line, 0, sizeof(line));
186         }
187
188         entry = head;
189         while( entry != NULL )
190         {
191                 print(&entry->key_id, sizeof(uint32_t), 1, out);
192                 print(&entry->val_id, sizeof(uint32_t), 1, out);
193                 print(&entry->offset, sizeof(uint32_t), 1, out);
194                 print(&entry->length, sizeof(uint32_t), 1, out);
195                 entry = entry->next;
196         }
197
198         if( offset > 0 )
199         {
200                 offset = htonl(offset);
201                 print(&offset, sizeof(uint32_t), 1, out);
202                 fsync(fileno(out));
203                 fclose(out);
204         }
205         else
206         {
207                 fclose(out);
208                 unlink(argv[2]);
209         }
210
211         fclose(in);
212         return(0);
213 }