diff --git a/juniper-config-to-set.php b/juniper-config-to-set.php new file mode 100644 index 0000000..4c4efee --- /dev/null +++ b/juniper-config-to-set.php @@ -0,0 +1,86 @@ +#!/usr/bin/php + $strlen) return false; + return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0; +} + +function addtotree($tree,$word) { + array_push($tree,$word); + return $tree; +} + +function removewordfromtree($tree) { + array_pop($tree); + return $tree; +} + +function printtree($tree) { + return implode(" ",$tree); +} + +$tree=array(); + +while($line = fgets(STDIN)){ + + // Skip any commented lines + if(strpos($line,'#')===0) { + continue; + } + + // Trim white space from the line + $line=trim($line); + + // What remains is a series of tests to see what the line ends with. + + // This test matches a ';' character which means its a leaf and we can output the + // whole line to STDOUT + if(endswith($line,';')) { + + // Trim the semi-colon from the end of the line, we don't need that. + $line = rtrim($line,';'); + + // Test to see if the tree is empty, if it is then we'll just print the leaf and + // not the tree to avoid excess white spaces + if(count($tree)==0) { + echo "set $line\n"; + } else { + echo "set " . printtree($tree) . " $line\n"; + } + } + + // This test matches a '{' character on the end of the line and means we need to add a + // branch to the tree + if(endswith($line,'{')) { + $line = rtrim($line,' {'); + $tree=addtotree($tree,$line); + } + + // This test matches a '}' character on the end of the line and means we need to + // remove the last branch that we added to the tree + if(endswith($line,'}')) { + $tree=removewordfromtree($tree); + } +} + +?>