move tools used during build into build_tools subdirectory
[asterisk/asterisk.git] / build_tools / vercomp.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * A simple program version comparison tool.
5  * 
6  * Copyright (C) 2005, Steven Michael Murphy (murf at e-tools dot com).
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 /* vercomp.c
13    args: <program> <comparison> <version>
14
15         where:
16
17    program = path to program (bison or flex)
18    comparison = ">", "<", "<=", ">=", "="  -- depending on shell, you may have to use backslash escapes
19    version = a version compare against, say 1.875, or 2.5.4, or whatever.
20
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 char *program_version[5];
27 char *arg_version[5];
28
29 void get_program_version_string(char *command, char *output)
30 {
31         char cbuf[8000];
32         char pbuf[8000];
33         char zbuf[8000];
34         char *res;
35         FILE *p1;
36
37         zbuf[0] = 0;
38         
39         sprintf( cbuf, "%s --version", command );
40         p1 = popen(cbuf, "r");
41         if( !p1 )
42         {
43                 fprintf(stderr,"vercomp: Could not execute the command: %s\n", command);
44                 exit(125);
45         }
46         /* the first line is the magic one */
47         res = fgets(zbuf, 8000, p1);
48         /* clear the trailing blank */
49         if( zbuf[strlen(zbuf)-1] == '\n' )
50                 zbuf[strlen(zbuf)-1] = 0;
51         /* the rest is cruft, just empty the input stream */
52         while( res )
53         {
54                 res = fgets(pbuf, 8000, p1);
55         }
56         /* close the stream. Hopefully, we have what we need */
57         pclose(p1);
58         /* all we want is the last "word"-- so find the last blank, and grab everything after that */
59
60         res = strrchr(zbuf,' ');
61         if( !res )
62         {
63                 fprintf(stderr,"Something is wrong with the version string: %s\n", zbuf);
64                 exit(124);
65         }
66         strcpy(output,res+1);
67 }
68
69
70 void extract_version(char *ver_string, char **where)
71 {
72         int i=0;
73         char *p=ver_string;
74         
75         while( p && *p )
76         {
77                 where[i++] = p;
78                 p = strchr(p,'.');
79                 if( p )
80                 {
81                         *p= 0;
82                         p++;
83                 }
84         }
85 }
86
87 void compare_versions(char *compare_func)
88 {
89         int i;
90         
91         for(i=0;i<5;i++)
92         {
93                 /* start out at the beginning, then go to the end */
94                 if( program_version[i] && arg_version[i] && *program_version[i] && *arg_version[i] )
95                 {
96                         
97                         if( strlen(program_version[i]) == strspn(program_version[i],"0123456789")
98                                 && strlen(arg_version[i]) == strspn(arg_version[i],"0123456789") )
99                         {
100                                 /* just pure numbers -- do a numeric compare */
101                                 int pv = atoi(program_version[i]);
102                                 int av = atoi(arg_version[i]);
103                                 
104                                 if( pv < av )
105                                 {
106                                         if( !strcmp(compare_func,"=") )
107                                         {
108                                                 printf("false\n");
109                                                 exit(0);
110                                         }
111                                         else if( !strcmp(compare_func, ">") )
112                                         {
113                                                 printf("false\n");
114                                                 exit(0);
115                                         }
116                                         else if( !strcmp(compare_func, "<") )
117                                         {
118                                                 printf("true\n");
119                                                 exit(0);
120                                         }
121                                         else if( !strcmp(compare_func, ">=") )
122                                         {
123                                                 printf("false\n");
124                                                 exit(0);
125                                         }
126                                         else if( !strcmp(compare_func, "<=") )
127                                         {
128                                                 printf("true\n");
129                                                 exit(0);
130                                         }
131                                 }
132                                 else if( pv > av )
133                                 {
134                                         if( !strcmp(compare_func,"=") )
135                                         {
136                                                 printf("false\n");
137                                                 exit(0);
138                                         }
139                                         else if( !strcmp(compare_func, ">") )
140                                         {
141                                                 printf("true\n");
142                                                 exit(0);
143                                         }
144                                         else if( !strcmp(compare_func, "<") )
145                                         {
146                                                 printf("false\n");
147                                                 exit(0);
148                                         }
149                                         else if( !strcmp(compare_func, ">=") )
150                                         {
151                                                 printf("true\n");
152                                                 exit(0);
153                                         }
154                                         else if( !strcmp(compare_func, "<=") )
155                                         {
156                                                 printf("false\n");
157                                                 exit(0);
158                                         }
159                                 }
160                         }
161                         else
162                         {
163                                 /* other junk thrown in -- do string compare */
164                                 int res = strcmp(program_version[i], arg_version[i]);
165                                 if( res < 0 ) /* prog is less than arg */
166                                 {
167                                         if( !strcmp(compare_func,"=") )
168                                         {
169                                                 printf("false\n");
170                                                 exit(0);
171                                         }
172                                         else if( !strcmp(compare_func, ">") )
173                                         {
174                                                 printf("false\n");
175                                                 exit(0);
176                                         }
177                                         else if( !strcmp(compare_func, "<") )
178                                         {
179                                                 printf("true\n");
180                                                 exit(0);
181                                         }
182                                         else if( !strcmp(compare_func, ">=") )
183                                         {
184                                                 printf("false\n");
185                                                 exit(0);
186                                         }
187                                         else if( !strcmp(compare_func, "<=") )
188                                         {
189                                                 printf("true\n");
190                                                 exit(0);
191                                         }
192                                 }
193                                 else if( res > 0 ) /* prog is greater than arg */
194                                 {
195                                         if( !strcmp(compare_func,"=") )
196                                         {
197                                                 printf("false\n");
198                                                 exit(0);
199                                         }
200                                         else if( !strcmp(compare_func, ">") )
201                                         {
202                                                 printf("true\n");
203                                                 exit(0);
204                                         }
205                                         else if( !strcmp(compare_func, "<") )
206                                         {
207                                                 printf("false\n");
208                                                 exit(0);
209                                         }
210                                         else if( !strcmp(compare_func, ">=") )
211                                         {
212                                                 printf("true\n");
213                                                 exit(0);
214                                         }
215                                         else if( !strcmp(compare_func, "<=") )
216                                         {
217                                                 printf("false\n");
218                                                 exit(0);
219                                         }
220                                 }
221                         }
222                 }
223                 else if( program_version[i] && *program_version[i] )
224                 {
225                         if( !strcmp(compare_func,"=") )
226                         {
227                                 printf("false\n");
228                                 exit(0);
229                         }
230                         else if( !strcmp(compare_func, ">") )
231                         {
232                                 printf("true\n");
233                                 exit(0);
234                         }
235                         else if( !strcmp(compare_func, "<") )
236                         {
237                                 printf("false\n");
238                                 exit(0);
239                         }
240                         else if( !strcmp(compare_func, ">=") )
241                         {
242                                 printf("true\n");
243                                 exit(0);
244                         }
245                         else if( !strcmp(compare_func, "<=") )
246                         {
247                                 printf("false\n");
248                                 exit(0);
249                         }
250                         
251                 }
252                 else if( arg_version[i] && *arg_version[i] )
253                 {
254                         if( !strcmp(compare_func,"=") )
255                         {
256                                 printf("false\n");
257                                 exit(0);
258                         }
259                         else if( !strcmp(compare_func, ">") )
260                         {
261                                 printf("false\n");
262                                 exit(0);
263                         }
264                         else if( !strcmp(compare_func, "<") )
265                         {
266                                 printf("true\n");
267                                 exit(0);
268                         }
269                         else if( !strcmp(compare_func, ">=") )
270                         {
271                                 printf("false\n");
272                                 exit(0);
273                         }
274                         else if( !strcmp(compare_func, "<=") )
275                         {
276                                 printf("true\n");
277                                 exit(0);
278                         }
279                 }
280                 else
281                         break;
282         }
283         if( !strcmp(compare_func,"=") )
284         {
285                 printf("true\n");
286                 exit(0);
287         }
288         else if( !strcmp(compare_func, ">") )
289         {
290                 printf("false\n");
291                 exit(0);
292         }
293         else if( !strcmp(compare_func, "<") )
294         {
295                 printf("false\n");
296                 exit(0);
297         }
298         else if( !strcmp(compare_func, ">=") )
299         {
300                 printf("true\n");
301                 exit(0);
302         }
303         else if( !strcmp(compare_func, "<=") )
304         {
305                 printf("true\n");
306                 exit(0);
307         }
308 }
309
310 void usage(void)
311 {
312         printf("Usage: <program-path> <comparison> <version>\n\
313 \n\
314         where:\n\
315 \n\
316    program-path = path to program (bison or flex)\n\
317    comparison = '>', '<', '<=', '>=', '='  -- depending on shell, you may have to use backslash escapes\n\
318    version = a version compare against, say 1.875, or 2.5.4, or whatever.\n\n");
319 }
320
321
322 int main(int argc, char **argv)
323 {
324         char program_version_string[8000];
325         
326         /* before starting, check args and make sure all is OK */
327         if( argc < 4 || argc > 4 )
328         {
329                 usage();
330                 exit(-256);
331         }
332         if ( strcmp(argv[2],"=") && strcmp(argv[2],">") && strcmp(argv[2],"<") && strcmp(argv[2],">=") && strcmp(argv[2],"<=") )
333         {
334                 fprintf(stderr,"vercomp: ILLEGAL input Comparison value: %s\n\n", argv[2]);
335                 usage();
336                 exit(-256);
337         }
338                  
339         /* first, extract a version from the command line arg */
340         extract_version(argv[3], arg_version);
341
342         /* next, extract a version from the command line */
343         get_program_version_string(argv[1], program_version_string);
344         extract_version(program_version_string, program_version);
345         
346         /* next compare and return result */
347         compare_versions(argv[2]);
348         /* the above func shouldn't return */
349 }