mirror of http://gerrit.asterisk.org/asterisk
use new C-coded version comparison program for bison and flex (bug #2058, with different Makefile changes)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5731 65c4cc65-6c06-0410-ace0-fbb531ad65f31.2-netsec
parent
d83eec795f
commit
813f0b9653
@ -0,0 +1,349 @@
|
||||
/*
|
||||
* Asterisk -- A telephony toolkit for Linux.
|
||||
*
|
||||
* A simple program version comparison tool.
|
||||
*
|
||||
* Copyright (C) 2005, 'murf'.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License
|
||||
*/
|
||||
|
||||
/* vercomp.c
|
||||
args: <program> <comparison> <version>
|
||||
|
||||
where:
|
||||
|
||||
program = path to program (bison or flex)
|
||||
comparison = ">", "<", "<=", ">=", "=" -- depending on shell, you may have to use backslash escapes
|
||||
version = a version compare against, say 1.875, or 2.5.4, or whatever.
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char *program_version[5];
|
||||
char *arg_version[5];
|
||||
|
||||
void get_program_version_string(char *command, char *output)
|
||||
{
|
||||
char cbuf[8000];
|
||||
char pbuf[8000];
|
||||
char zbuf[8000];
|
||||
char *res;
|
||||
FILE *p1;
|
||||
|
||||
zbuf[0] = 0;
|
||||
|
||||
sprintf( cbuf, "%s --version", command );
|
||||
p1 = popen(cbuf, "r");
|
||||
if( !p1 )
|
||||
{
|
||||
fprintf(stderr,"vercomp: Could not execute the command: %s\n", command);
|
||||
exit(125);
|
||||
}
|
||||
/* the first line is the magic one */
|
||||
res = fgets(zbuf, 8000, p1);
|
||||
/* clear the trailing blank */
|
||||
if( zbuf[strlen(zbuf)-1] == '\n' )
|
||||
zbuf[strlen(zbuf)-1] = 0;
|
||||
/* the rest is cruft, just empty the input stream */
|
||||
while( res )
|
||||
{
|
||||
res = fgets(pbuf, 8000, p1);
|
||||
}
|
||||
/* close the stream. Hopefully, we have what we need */
|
||||
pclose(p1);
|
||||
/* all we want is the last "word"-- so find the last blank, and grab everything after that */
|
||||
|
||||
res = strrchr(zbuf,' ');
|
||||
if( !res )
|
||||
{
|
||||
fprintf(stderr,"Something is wrong with the version string: %s\n", zbuf);
|
||||
exit(124);
|
||||
}
|
||||
strcpy(output,res+1);
|
||||
}
|
||||
|
||||
|
||||
void extract_version(char *ver_string, char **where)
|
||||
{
|
||||
int i=0;
|
||||
char *p=ver_string;
|
||||
|
||||
while( p && *p )
|
||||
{
|
||||
where[i++] = p;
|
||||
p = strchr(p,'.');
|
||||
if( p )
|
||||
{
|
||||
*p= 0;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void compare_versions(char *compare_func)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0;i<5;i++)
|
||||
{
|
||||
/* start out at the beginning, then go to the end */
|
||||
if( program_version[i] && arg_version[i] && *program_version[i] && *arg_version[i] )
|
||||
{
|
||||
|
||||
if( strlen(program_version[i]) == strspn(program_version[i],"0123456789")
|
||||
&& strlen(arg_version[i]) == strspn(arg_version[i],"0123456789") )
|
||||
{
|
||||
/* just pure numbers -- do a numeric compare */
|
||||
int pv = atoi(program_version[i]);
|
||||
int av = atoi(arg_version[i]);
|
||||
|
||||
if( pv < av )
|
||||
{
|
||||
if( !strcmp(compare_func,"=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
else if( pv > av )
|
||||
{
|
||||
if( !strcmp(compare_func,"=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* other junk thrown in -- do string compare */
|
||||
int res = strcmp(program_version[i], arg_version[i]);
|
||||
if( res < 0 ) /* prog is less than arg */
|
||||
{
|
||||
if( !strcmp(compare_func,"=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
else if( res > 0 ) /* prog is greater than arg */
|
||||
{
|
||||
if( !strcmp(compare_func,"=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( program_version[i] && *program_version[i] )
|
||||
{
|
||||
if( !strcmp(compare_func,"=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
else if( arg_version[i] && *arg_version[i] )
|
||||
{
|
||||
if( !strcmp(compare_func,"=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">=") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
if( !strcmp(compare_func,"=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<") )
|
||||
{
|
||||
printf("false\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, ">=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp(compare_func, "<=") )
|
||||
{
|
||||
printf("true\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void usage(void)
|
||||
{
|
||||
printf("Usage: <program-path> <comparison> <version>\n\
|
||||
\n\
|
||||
where:\n\
|
||||
\n\
|
||||
program-path = path to program (bison or flex)\n\
|
||||
comparison = '>', '<', '<=', '>=', '=' -- depending on shell, you may have to use backslash escapes\n\
|
||||
version = a version compare against, say 1.875, or 2.5.4, or whatever.\n\n");
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char program_version_string[8000];
|
||||
|
||||
/* before starting, check args and make sure all is OK */
|
||||
if( argc < 4 || argc > 4 )
|
||||
{
|
||||
usage();
|
||||
exit(-256);
|
||||
}
|
||||
if ( strcmp(argv[2],"=") && strcmp(argv[2],">") && strcmp(argv[2],"<") && strcmp(argv[2],">=") && strcmp(argv[2],"<=") )
|
||||
{
|
||||
fprintf(stderr,"vercomp: ILLEGAL input Comparison value: %s\n\n", argv[2]);
|
||||
usage();
|
||||
exit(-256);
|
||||
}
|
||||
|
||||
/* first, extract a version from the command line arg */
|
||||
extract_version(argv[3], arg_version);
|
||||
|
||||
/* next, extract a version from the command line */
|
||||
get_program_version_string(argv[1], program_version_string);
|
||||
extract_version(program_version_string, program_version);
|
||||
|
||||
/* next compare and return result */
|
||||
compare_versions(argv[2]);
|
||||
/* the above func shouldn't return */
|
||||
}
|
@ -1,163 +0,0 @@
|
||||
#! /bin/bash
|
||||
|
||||
### flex just outputs a single line:
|
||||
|
||||
## flex version 2.5.4
|
||||
|
||||
|
||||
### but bison is a bit more wordy
|
||||
|
||||
## bison (GNU Bison) 1.875c
|
||||
## Written by Robert Corbett and Richard Stallman.
|
||||
##
|
||||
## Copyright (C) 2003 Free Software Foundation, Inc.
|
||||
## This is free software; see the source for copying conditions. There is NO
|
||||
## warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
|
||||
### based on this, the version number of the program:
|
||||
### a. in the first line of output
|
||||
### b. is the last "word" of that line
|
||||
|
||||
program=$1
|
||||
comparefunc=$2
|
||||
argver=$3
|
||||
|
||||
progver1=`$program --version | head -1`
|
||||
|
||||
[[ $progver1 =~ '([^ ]+$)' ]]
|
||||
|
||||
progver=$BASH_REMATCH
|
||||
|
||||
progver2=$progver
|
||||
numprogverlist=0
|
||||
|
||||
while [[ $progver2 =~ '^([^.]+)\.(.*)' ]]; do
|
||||
progver2=${BASH_REMATCH[2]}
|
||||
progverlist[$numprogverlist]=${BASH_REMATCH[1]}
|
||||
progverlist[$(( ${numprogverlist}+1 ))]=${BASH_REMATCH[2]}
|
||||
|
||||
## echo ${BASH_REMATCH[0]}
|
||||
## echo ${BASH_REMATCH[1]}
|
||||
## echo ${BASH_REMATCH[2]}
|
||||
(( numprogverlist=$(( $numprogverlist+1 )) ))
|
||||
|
||||
done
|
||||
(( numprogverlist=$(( $numprogverlist+1 )) ))
|
||||
|
||||
## echo number of elements = $numprogverlist
|
||||
## echo element 0 = ${progverlist[0]}
|
||||
## echo element 1 = ${progverlist[1]}
|
||||
## echo element 2 = ${progverlist[2]}
|
||||
|
||||
argver2=$argver
|
||||
numargverlist=0
|
||||
|
||||
while [[ $argver2 =~ '^([^.]+)\.(.*)' ]]; do
|
||||
argver2=${BASH_REMATCH[2]}
|
||||
argverlist[$numargverlist]=${BASH_REMATCH[1]}
|
||||
argverlist[$(( ${numargverlist}+1 ))]=${BASH_REMATCH[2]}
|
||||
|
||||
## echo ${BASH_REMATCH[0]}
|
||||
## echo ${BASH_REMATCH[1]}
|
||||
## echo ${BASH_REMATCH[2]}
|
||||
(( numargverlist=$(( $numargverlist+1 )) ))
|
||||
|
||||
done
|
||||
(( numargverlist=$(( $numargverlist+1 )) ))
|
||||
|
||||
## echo number of argver elements = $numargverlist
|
||||
## echo element 0 = ${argverlist[0]}
|
||||
## echo element 1 = ${argverlist[1]}
|
||||
## echo element 2 = ${argverlist[2]}
|
||||
|
||||
if (( $numprogverlist < $numargverlist )); then
|
||||
for (( i=$numprogverlist ; $i < $numargverlist ; i=$i + 1 )) ; do
|
||||
## echo setting progverlist "[" $i "]" to 0
|
||||
(( progverlist[$i]='0' ))
|
||||
(( numprogverlist=${numprogverlist}+1 ))
|
||||
done
|
||||
elif (( $numargverlist < $numprogverlist )); then
|
||||
for (( i=$numargverlist ; $i < $numprogverlist ; i=$i + 1 )) ; do
|
||||
## echo setting argverlist "[" $i "]" to 0
|
||||
(( argverlist[$i]='0' ))
|
||||
(( numargverlist=${numargverlist}+1 ))
|
||||
done
|
||||
fi
|
||||
|
||||
## echo numarg=$numargverlist numprog=$numprogverlist
|
||||
## echo arg0: ${argverlist[0]}
|
||||
## echo arg1: ${argverlist[1]}
|
||||
## echo arg2: ${argverlist[2]}
|
||||
## echo prog0: ${progverlist[0]}
|
||||
## echo prog1: ${progverlist[1]}
|
||||
## echo prog2: ${progverlist[2]}
|
||||
|
||||
## the main comparison loop
|
||||
|
||||
for (( i=0 ; $i < $numargverlist ; i=$i + 1 )) ; do
|
||||
## echo i= $i
|
||||
|
||||
if [[ ${progverlist[$i]} =~ '^[0-9]+$' && ${argverlist[$i]} =~ '^[0-9]+$' ]] ; then ## nothing but numbers
|
||||
if (( ${progverlist[$i]} != ${argverlist[$i]} )); then
|
||||
if [[ ${progverlist[$i]} -lt ${argverlist[$i]} ]]; then
|
||||
if [[ $comparefunc == "=" ]]; then
|
||||
echo "false"
|
||||
exit 0;
|
||||
elif [[ $comparefunc == "<" || $comparefunc == "<=" ]]; then
|
||||
echo "true"
|
||||
exit 0;
|
||||
elif [[ $comparefunc == ">" || $comparefunc == ">=" ]]; then
|
||||
echo "false"
|
||||
exit 0;
|
||||
fi
|
||||
elif [[ ${progverlist[$i]} -gt ${argverlist[$i]} ]]; then
|
||||
if [[ $comparefunc == "=" ]]; then
|
||||
echo "false"
|
||||
exit 0;
|
||||
elif [[ $comparefunc == "<" || $comparefunc == "<=" ]]; then
|
||||
echo "false"
|
||||
exit 0;
|
||||
elif [[ $comparefunc == ">" || $comparefunc == ">=" ]]; then
|
||||
echo "true"
|
||||
exit 0;
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
else ## something besides just numbers
|
||||
if [[ ${progverlist[$i]} != ${argverlist[$i]} ]]; then
|
||||
if [[ ${progverlist[$i]} < ${argverlist[$i]} ]]; then
|
||||
if [[ $comparefunc == "=" ]]; then
|
||||
echo "false"
|
||||
exit 0;
|
||||
elif [[ $comparefunc == "<" || $comparefunc == "<=" ]]; then
|
||||
echo "true"
|
||||
exit 0;
|
||||
elif [[ $comparefunc == ">" || $comparefunc == ">=" ]]; then
|
||||
echo "false"
|
||||
exit 0;
|
||||
fi
|
||||
elif [[ ${progverlist[$i]} > ${argverlist[$i]} ]]; then
|
||||
if [[ $comparefunc == "=" ]]; then
|
||||
echo "false"
|
||||
exit 0;
|
||||
elif [[ $comparefunc == "<" || $comparefunc == "<=" ]]; then
|
||||
echo "false"
|
||||
exit 0;
|
||||
elif [[ $comparefunc == ">" || $comparefunc == ">=" ]]; then
|
||||
echo "true"
|
||||
exit 0;
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ $comparefunc == "=" ]]; then
|
||||
echo "true"
|
||||
elif [[ $comparefunc == "<=" || $comparefunc == ">=" ]]; then
|
||||
echo "true"
|
||||
else
|
||||
echo "false"
|
||||
fi
|
||||
|
||||
exit 0;
|
Loading…
Reference in new issue