Skip to content

Commit 2f5b866

Browse files
forejtvpeterschrammel
authored andcommitted
Autocomplete script for bash
See documentation in scripts/bash-autocomplete/Readme.md
1 parent fc756f3 commit 2f5b866

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

scripts/bash-autocomplete/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cbmc.sh

scripts/bash-autocomplete/Readme.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# CBMC Autocomplete Scripts for Bash
2+
This directory contains an autocomplete script for bash.
3+
## Installation
4+
1. Compile cbmc and
5+
6+
2. `cd scripts/bash-autocomplete`
7+
8+
3. `./extract-switches.sh`
9+
10+
4. Put the following at the end of you in your `~/.bashrc`, with the directories adapted to your directory structure:
11+
```bash
12+
cbmcautocomplete=~/diffblue/cbmc/scripts/bash-autocomplete/cbmc.sh
13+
if [ -f $cbmcautocomplete ]; then
14+
. $cbmcautocomplete
15+
fi
16+
```
17+
## Usage
18+
As with the usual autocomplete in bash, start typing a switch to complete it, for example:
19+
```
20+
cbmc --clas<TAB>
21+
```
22+
will complete to
23+
```
24+
cbmc --classpath
25+
```
26+
27+
## Features implemented
28+
29+
* Completing all switches
30+
* Completing values for `--cover`, `--mm` and `--arch`
31+
* When completing a name of a file to analyze, only files with supported extensions are shown.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
_cbmc_autocomplete()
3+
{
4+
#list of all switches cbmc has. IMPORTANT: in the template file, this variable must be defined on line 5.
5+
local switches=""
6+
#word on which the cursor is
7+
local cur=${COMP_WORDS[COMP_CWORD]}
8+
#previous word (in case it is a switch with a parameter)
9+
local prev=${COMP_WORDS[COMP_CWORD-1]}
10+
11+
#check if the command before cursor is a switch that takes parameters, if yes,
12+
#offer a choice of parameters
13+
case "$prev" in
14+
--cover) #for coverage we list the options explicitly
15+
COMPREPLY=( $( compgen -W "assertion path branch location decision condition mcdc cover" -- $cur ) )
16+
return 0
17+
;;
18+
--mm) #for memory models we list the options explicitly
19+
COMPREPLY=( $( compgen -W "sc tso pso" -- $cur ) )
20+
return 0
21+
;;
22+
--arch) #for architecture we list the options explicitly
23+
COMPREPLY=( $( compgen -W "i386 x86_64" -- $cur ) )
24+
return 0
25+
;;
26+
-I|--classpath|-cp|--outfile|--existing-coverage|--graphml-cex)
27+
#a switch that takes a file parameter of which we don't know an extension
28+
#TODO probably we can do more for -I, --classpath, -cp
29+
_filedir
30+
return 0
31+
;;
32+
esac
33+
34+
#complete a switch from a standard list, if the parameter under cursor starts with a hyphen
35+
if [[ "$cur" == -* ]]; then
36+
COMPREPLY=( $( compgen -W "$switches" -- $cur ) )
37+
return 0
38+
fi
39+
40+
#if none of the above applies, offer directories and files that we can analyze
41+
_filedir "@(class|jar|cpp|cc|c\+\+|ii|cxx|c|i|gb)"
42+
}
43+
complete -F _cbmc_autocomplete cbmc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
echo "Compiling the helper file to extract the raw list of parameters from cbmc"
3+
g++ -c -MMD -MP -std=c++11 -Wall -I ../../src/ -ftrack-macro-expansion=0 -fno-diagnostics-show-caret switch_extractor_helper.c -o tmp.o 2> pragma.txt
4+
5+
retval=$?
6+
7+
#clean up compiled files, we don't need them.
8+
rm tmp.o 2> /dev/null
9+
rm tmp.d 2> /dev/null
10+
11+
#check if compilation went fine
12+
if [ $retval -ne 0 ]; then
13+
echo "Problem compiling the helper file, parameter list not extracted."
14+
exit 1;
15+
fi
16+
17+
echo "Converting the raw parameter list to the format required by autocomplete scripts"
18+
rawstring=`sed "s/^.*pragma message: \(.*\)/\1/" pragma.txt`
19+
#delete pragma file, we won't need it
20+
rm pragma.txt 2> /dev/null
21+
22+
#now the main bit, convert from raw format to a proper list of switches
23+
cleanstring=`(
24+
#extract 2-hyphen switches, such as --foo
25+
#grep for '(foo)' expressions, and then use sed to remove parantheses and put '--' at the start
26+
(echo $rawstring | grep -o "([^)]*)" | sed "s/^.\(.*\).$/--\1/") ;
27+
#extract 1-hyphen switches, such as -F
28+
#use sed to remove all (foo) expressions, then you're left with switches and ':', so grep the colons out and then use sed to include the '-'
29+
(echo $rawstring | sed "s/([^)]*)//g" | grep -o "[a-zA-Z0-9]" | sed "s/\(.*\)/-\1/")
30+
) | tr '\n' ' '`
31+
32+
#sanity check that there is only one line of output
33+
if [ `echo $cleanstring | wc -l | awk '{print $1}'` -ne 1 ]; then
34+
echo "Problem converting the parameter list to the correct format, I was expecting one line but either got 0 or >2. This is likely to be an error in this conversion script."
35+
exit 1;
36+
fi
37+
38+
#sanity check that there are no dangerous characters
39+
echo $cleanstring | grep -q "[^a-zA-Z0-9 -]"
40+
if [ $? -eq 0 ]; then
41+
echo "Problem converting the parameter list to the correct format, illegal characters detected. This is likely to be an error in this conversion script."
42+
exit 1;
43+
fi
44+
45+
echo "Injecting the parameter list to the autocomplete file."
46+
sed "5 s/.*/ local switches=\"$cleanstring\"/" cbmc.sh.template > cbmc.sh
47+
48+
rm pragma.txt 2> /dev/null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "cbmc/cbmc_parse_options.h"
2+
3+
#pragma message CBMC_OPTIONS

0 commit comments

Comments
 (0)