Skip to content

Commit 1d2cfb5

Browse files
author
Daniel Kroening
authored
Merge pull request #1263 from tautschnig/shell-auto-complete
[tgs->master] Shell auto completion scripts
2 parents 48c5b10 + 6881b39 commit 1d2cfb5

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

scripts/bash-autocomplete/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cbmc.sh

scripts/bash-autocomplete/Readme.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# CBMC Autocomplete Scripts for Bash
2+
This directory contains an autocomplete script for bash.
3+
4+
## Installation for bash
5+
1. Compile cbmc and
6+
7+
2. `cd scripts/bash-autocomplete`
8+
9+
3. `./extract-switches.sh`
10+
11+
4. Put the following at the end of your `~/.bashrc`, with the directories adapted to your directory structure:
12+
```bash
13+
cbmcautocomplete=~/diffblue/cbmc/scripts/bash-autocomplete/cbmc.sh
14+
if [ -f $cbmcautocomplete ]; then
15+
. $cbmcautocomplete
16+
fi
17+
```
18+
19+
5. `source ~/.bashrc`
20+
21+
## Installation for zsh
22+
Follow 1. 2. and 3. as above.
23+
24+
4. Put the following at the end of your `~/.zshrc`, with the directories adapted to your directory structure:
25+
```bash
26+
autoload bashcompinit
27+
bashcompinit
28+
cbmcautocomplete=~/diffblue/cbmc/scripts/bash-autocomplete/cbmc.sh
29+
if [ -f $cbmcautocomplete ]; then
30+
. $cbmcautocomplete
31+
fi
32+
```
33+
5. `source ~/.zshrc`
34+
35+
## Usage
36+
As with the usual autocomplete in bash, start typing a switch to complete it, for example:
37+
```
38+
cbmc --clas<TAB>
39+
```
40+
will complete to
41+
```
42+
cbmc --classpath
43+
```
44+
45+
## Features implemented
46+
47+
* Completing all switches
48+
* Completing values for `--cover`, `--mm` and `--arch`
49+
* When completing a name of a file to analyze, only files with supported extensions are shown.
Lines changed: 43 additions & 0 deletions
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+
COMPREPLY=( $(compgen -f -- $cur) )
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+
COMPREPLY=( $(compgen -G '*.class|*.jar|*.cpp|*.cc|*.c\+\+|*.ii|*.cxx|*.c|*.i|*.gb' -- $cur) )
42+
}
43+
complete -F _cbmc_autocomplete cbmc
Lines changed: 48 additions & 0 deletions
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
Lines changed: 3 additions & 0 deletions
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)