2019-06-28 11:55:43 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/*
|
|
|
|
The digraph command performs queries over unlabelled directed graphs
|
|
|
|
represented in text form. It is intended to integrate nicely with
|
|
|
|
typical UNIX command pipelines.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
your-application | digraph [command]
|
|
|
|
|
|
|
|
The support commands are:
|
|
|
|
|
|
|
|
nodes
|
|
|
|
the set of all nodes
|
|
|
|
degree
|
|
|
|
the in-degree and out-degree of each node
|
2019-07-18 14:20:30 -06:00
|
|
|
transpose
|
|
|
|
the reverse of the input edges
|
2019-06-28 11:55:43 -06:00
|
|
|
preds <node> ...
|
|
|
|
the set of immediate predecessors of the specified nodes
|
|
|
|
succs <node> ...
|
|
|
|
the set of immediate successors of the specified nodes
|
|
|
|
forward <node> ...
|
|
|
|
the set of nodes transitively reachable from the specified nodes
|
|
|
|
reverse <node> ...
|
|
|
|
the set of nodes that transitively reach the specified nodes
|
|
|
|
somepath <node> <node>
|
|
|
|
the list of nodes on some arbitrary path from the first node to the second
|
|
|
|
allpaths <node> <node>
|
|
|
|
the set of nodes on all paths from the first node to the second
|
|
|
|
sccs
|
|
|
|
all strongly connected components (one per line)
|
|
|
|
scc <node>
|
|
|
|
the set of nodes nodes strongly connected to the specified one
|
2019-10-13 22:20:27 -06:00
|
|
|
focus <node>
|
|
|
|
the subgraph containing all directed paths that pass through the specified node
|
2019-06-28 11:55:43 -06:00
|
|
|
|
|
|
|
Input format:
|
|
|
|
|
|
|
|
Each line contains zero or more words. Words are separated by unquoted
|
|
|
|
whitespace; words may contain Go-style double-quoted portions, allowing spaces
|
|
|
|
and other characters to be expressed.
|
|
|
|
|
|
|
|
Each word declares a node, and if there are more than one, an edge from the
|
|
|
|
first to each subsequent one. The graph is provided on the standard input.
|
|
|
|
|
|
|
|
For instance, the following (acyclic) graph specifies a partial order among the
|
|
|
|
subtasks of getting dressed:
|
|
|
|
|
|
|
|
$ cat clothes.txt
|
|
|
|
socks shoes
|
|
|
|
"boxer shorts" pants
|
|
|
|
pants belt shoes
|
|
|
|
shirt tie sweater
|
|
|
|
sweater jacket
|
|
|
|
hat
|
|
|
|
|
|
|
|
The line "shirt tie sweater" indicates the two edges shirt -> tie and
|
|
|
|
shirt -> sweater, not shirt -> tie -> sweater.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
Using digraph with existing Go tools:
|
|
|
|
|
|
|
|
$ go mod graph | digraph nodes # Operate on the Go module graph.
|
|
|
|
$ go list -m all | digraph nodes # Operate on the Go package graph.
|
|
|
|
|
|
|
|
Show the transitive closure of imports of the digraph tool itself:
|
|
|
|
$ go list -f '{{.ImportPath}} {{join .Imports " "}}' ... | digraph forward golang.org/x/tools/cmd/digraph
|
|
|
|
|
|
|
|
Show which clothes (see above) must be donned before a jacket:
|
|
|
|
$ digraph reverse jacket
|
|
|
|
|
|
|
|
*/
|
|
|
|
package main // import "golang.org/x/tools/cmd/digraph"
|
|
|
|
|
2014-10-23 07:13:39 -06:00
|
|
|
// TODO(adonovan):
|
|
|
|
// - support input files other than stdin
|
2018-08-10 12:13:41 -06:00
|
|
|
// - support alternative formats (AT&T GraphViz, CSV, etc),
|
2014-10-23 07:13:39 -06:00
|
|
|
// a comment syntax, etc.
|
|
|
|
// - allow queries to nest, like Blaze query language.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-11-12 11:37:06 -07:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2014-10-23 07:13:39 -06:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sort"
|
2014-11-12 11:37:06 -07:00
|
|
|
"strconv"
|
2019-10-13 22:20:27 -06:00
|
|
|
"strings"
|
2014-11-12 11:37:06 -07:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2014-10-23 07:13:39 -06:00
|
|
|
)
|
|
|
|
|
2019-06-28 11:55:43 -06:00
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(os.Stderr, `Usage: your-application | digraph [command]
|
|
|
|
|
|
|
|
The support commands are:
|
|
|
|
nodes
|
|
|
|
the set of all nodes
|
|
|
|
degree
|
|
|
|
the in-degree and out-degree of each node
|
2019-07-18 14:20:30 -06:00
|
|
|
transpose
|
|
|
|
the reverse of the input edges
|
2019-06-28 11:55:43 -06:00
|
|
|
preds <node> ...
|
|
|
|
the set of immediate predecessors of the specified nodes
|
|
|
|
succs <node> ...
|
|
|
|
the set of immediate successors of the specified nodes
|
|
|
|
forward <node> ...
|
|
|
|
the set of nodes transitively reachable from the specified nodes
|
|
|
|
reverse <node> ...
|
|
|
|
the set of nodes that transitively reach the specified nodes
|
|
|
|
somepath <node> <node>
|
|
|
|
the list of nodes on some arbitrary path from the first node to the second
|
|
|
|
allpaths <node> <node>
|
|
|
|
the set of nodes on all paths from the first node to the second
|
|
|
|
sccs
|
|
|
|
all strongly connected components (one per line)
|
|
|
|
scc <node>
|
|
|
|
the set of nodes nodes strongly connected to the specified one
|
2019-10-13 22:20:27 -06:00
|
|
|
focus <node>
|
|
|
|
the subgraph containing all directed paths that pass through the specified node
|
2019-06-28 11:55:43 -06:00
|
|
|
`)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-10-23 07:13:39 -06:00
|
|
|
|
|
|
|
func main() {
|
2019-06-28 11:55:43 -06:00
|
|
|
flag.Usage = usage
|
2014-10-23 07:13:39 -06:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) == 0 {
|
2019-06-28 11:55:43 -06:00
|
|
|
usage()
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := digraph(args[0], args[1:]); err != nil {
|
2014-11-17 10:58:28 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "digraph: %s\n", err)
|
2014-10-23 07:13:39 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodelist []string
|
|
|
|
|
|
|
|
func (l nodelist) println(sep string) {
|
2019-10-13 22:01:58 -06:00
|
|
|
for i, node := range l {
|
2014-10-23 07:13:39 -06:00
|
|
|
if i > 0 {
|
|
|
|
fmt.Fprint(stdout, sep)
|
|
|
|
}
|
2019-10-13 22:01:58 -06:00
|
|
|
fmt.Fprint(stdout, node)
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
fmt.Fprintln(stdout)
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:20:27 -06:00
|
|
|
type nodeset map[string]bool // TODO(deklerk): change bool to struct to reduce memory footprint
|
2014-10-23 07:13:39 -06:00
|
|
|
|
|
|
|
func (s nodeset) sort() nodelist {
|
2019-10-13 22:01:58 -06:00
|
|
|
nodes := make(nodelist, len(s))
|
2014-10-23 07:13:39 -06:00
|
|
|
var i int
|
2019-10-13 22:01:58 -06:00
|
|
|
for node := range s {
|
|
|
|
nodes[i] = node
|
2014-10-23 07:13:39 -06:00
|
|
|
i++
|
|
|
|
}
|
2019-10-13 22:01:58 -06:00
|
|
|
sort.Strings(nodes)
|
|
|
|
return nodes
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s nodeset) addAll(x nodeset) {
|
2019-10-13 22:01:58 -06:00
|
|
|
for node := range x {
|
|
|
|
s[node] = true
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A graph maps nodes to the non-nil set of their immediate successors.
|
|
|
|
type graph map[string]nodeset
|
|
|
|
|
2019-10-13 22:01:58 -06:00
|
|
|
func (g graph) addNode(node string) nodeset {
|
|
|
|
edges := g[node]
|
2014-10-23 07:13:39 -06:00
|
|
|
if edges == nil {
|
|
|
|
edges = make(nodeset)
|
2019-10-13 22:01:58 -06:00
|
|
|
g[node] = edges
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
return edges
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g graph) addEdges(from string, to ...string) {
|
|
|
|
edges := g.addNode(from)
|
|
|
|
for _, to := range to {
|
|
|
|
g.addNode(to)
|
|
|
|
edges[to] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g graph) reachableFrom(roots nodeset) nodeset {
|
|
|
|
seen := make(nodeset)
|
2019-10-13 22:01:58 -06:00
|
|
|
var visit func(node string)
|
|
|
|
visit = func(node string) {
|
|
|
|
if !seen[node] {
|
|
|
|
seen[node] = true
|
|
|
|
for e := range g[node] {
|
2014-10-23 07:13:39 -06:00
|
|
|
visit(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for root := range roots {
|
|
|
|
visit(root)
|
|
|
|
}
|
|
|
|
return seen
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g graph) transpose() graph {
|
|
|
|
rev := make(graph)
|
2019-10-13 22:01:58 -06:00
|
|
|
for node, edges := range g {
|
|
|
|
rev.addNode(node)
|
2014-10-23 07:13:39 -06:00
|
|
|
for succ := range edges {
|
2019-10-13 22:01:58 -06:00
|
|
|
rev.addEdges(succ, node)
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rev
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g graph) sccs() []nodeset {
|
|
|
|
// Kosaraju's algorithm---Tarjan is overkill here.
|
|
|
|
|
|
|
|
// Forward pass.
|
|
|
|
S := make(nodelist, 0, len(g)) // postorder stack
|
|
|
|
seen := make(nodeset)
|
2019-10-13 22:01:58 -06:00
|
|
|
var visit func(node string)
|
|
|
|
visit = func(node string) {
|
|
|
|
if !seen[node] {
|
|
|
|
seen[node] = true
|
|
|
|
for e := range g[node] {
|
2014-10-23 07:13:39 -06:00
|
|
|
visit(e)
|
|
|
|
}
|
2019-10-13 22:01:58 -06:00
|
|
|
S = append(S, node)
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
}
|
2019-10-13 22:01:58 -06:00
|
|
|
for node := range g {
|
|
|
|
visit(node)
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse pass.
|
|
|
|
rev := g.transpose()
|
|
|
|
var scc nodeset
|
|
|
|
seen = make(nodeset)
|
2019-10-13 22:01:58 -06:00
|
|
|
var rvisit func(node string)
|
|
|
|
rvisit = func(node string) {
|
|
|
|
if !seen[node] {
|
|
|
|
seen[node] = true
|
|
|
|
scc[node] = true
|
|
|
|
for e := range rev[node] {
|
2014-10-23 07:13:39 -06:00
|
|
|
rvisit(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var sccs []nodeset
|
|
|
|
for len(S) > 0 {
|
|
|
|
top := S[len(S)-1]
|
|
|
|
S = S[:len(S)-1] // pop
|
|
|
|
if !seen[top] {
|
|
|
|
scc = make(nodeset)
|
|
|
|
rvisit(top)
|
|
|
|
sccs = append(sccs, scc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sccs
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:22:56 -06:00
|
|
|
func (g graph) allpaths(from, to string) error {
|
|
|
|
// Mark all nodes to "to".
|
|
|
|
seen := make(nodeset) // value of seen[x] indicates whether x is on some path to "to"
|
|
|
|
var visit func(node string) bool
|
|
|
|
visit = func(node string) bool {
|
|
|
|
reachesTo, ok := seen[node]
|
|
|
|
if !ok {
|
|
|
|
reachesTo = node == to
|
|
|
|
seen[node] = reachesTo
|
|
|
|
for e := range g[node] {
|
|
|
|
if visit(e) {
|
|
|
|
reachesTo = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if reachesTo && node != to {
|
|
|
|
seen[node] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reachesTo
|
|
|
|
}
|
|
|
|
visit(from)
|
|
|
|
|
|
|
|
// For each marked node, collect its marked successors.
|
|
|
|
var edges []string
|
|
|
|
for n := range seen {
|
|
|
|
for succ := range g[n] {
|
|
|
|
if seen[succ] {
|
|
|
|
edges = append(edges, n+" "+succ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort (so that this method is deterministic) and print edges.
|
|
|
|
sort.Strings(edges)
|
|
|
|
for _, e := range edges {
|
|
|
|
fmt.Fprintln(stdout, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:12:11 -06:00
|
|
|
func (g graph) somepath(from, to string) error {
|
|
|
|
type edge struct{ from, to string }
|
|
|
|
seen := make(nodeset)
|
|
|
|
var dfs func(path []edge, from string) bool
|
|
|
|
dfs = func(path []edge, from string) bool {
|
|
|
|
if !seen[from] {
|
|
|
|
seen[from] = true
|
|
|
|
if from == to {
|
|
|
|
// fmt.Println(path, len(path), cap(path))
|
|
|
|
// Print and unwind.
|
|
|
|
for _, e := range path {
|
|
|
|
fmt.Fprintln(stdout, e.from+" "+e.to)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for e := range g[from] {
|
|
|
|
if dfs(append(path, edge{from: from, to: e}), e) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
maxEdgesInGraph := len(g) * (len(g) - 1)
|
|
|
|
if !dfs(make([]edge, 0, maxEdgesInGraph), from) {
|
|
|
|
return fmt.Errorf("no path from %q to %q", from, to)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:13:39 -06:00
|
|
|
func parse(rd io.Reader) (graph, error) {
|
|
|
|
g := make(graph)
|
|
|
|
|
2014-11-12 11:37:06 -07:00
|
|
|
var linenum int
|
2014-10-23 07:13:39 -06:00
|
|
|
in := bufio.NewScanner(rd)
|
|
|
|
for in.Scan() {
|
2014-11-12 11:37:06 -07:00
|
|
|
linenum++
|
|
|
|
// Split into words, honoring double-quotes per Go spec.
|
|
|
|
words, err := split(in.Text())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("at line %d: %v", linenum, err)
|
|
|
|
}
|
2014-10-23 07:13:39 -06:00
|
|
|
if len(words) > 0 {
|
|
|
|
g.addEdges(words[0], words[1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := in.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:22:56 -06:00
|
|
|
// Overridable for testing purposes.
|
2014-10-23 07:13:39 -06:00
|
|
|
var stdin io.Reader = os.Stdin
|
|
|
|
var stdout io.Writer = os.Stdout
|
|
|
|
|
|
|
|
func digraph(cmd string, args []string) error {
|
|
|
|
// Parse the input graph.
|
|
|
|
g, err := parse(stdin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the command line.
|
|
|
|
switch cmd {
|
|
|
|
case "nodes":
|
|
|
|
if len(args) != 0 {
|
2014-11-17 10:58:28 -07:00
|
|
|
return fmt.Errorf("usage: digraph nodes")
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
nodes := make(nodeset)
|
2019-10-13 22:01:58 -06:00
|
|
|
for node := range g {
|
|
|
|
nodes[node] = true
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
nodes.sort().println("\n")
|
|
|
|
|
|
|
|
case "degree":
|
|
|
|
if len(args) != 0 {
|
2014-11-17 10:58:28 -07:00
|
|
|
return fmt.Errorf("usage: digraph degree")
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
nodes := make(nodeset)
|
2019-10-13 22:01:58 -06:00
|
|
|
for node := range g {
|
|
|
|
nodes[node] = true
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
rev := g.transpose()
|
2019-10-13 22:01:58 -06:00
|
|
|
for _, node := range nodes.sort() {
|
|
|
|
fmt.Fprintf(stdout, "%d\t%d\t%s\n", len(rev[node]), len(g[node]), node)
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
|
2019-07-18 14:20:30 -06:00
|
|
|
case "transpose":
|
|
|
|
if len(args) != 0 {
|
2019-09-11 00:14:36 -06:00
|
|
|
return fmt.Errorf("usage: digraph transpose")
|
2019-07-18 14:20:30 -06:00
|
|
|
}
|
|
|
|
var revEdges []string
|
|
|
|
for node, succs := range g.transpose() {
|
|
|
|
for succ := range succs {
|
|
|
|
revEdges = append(revEdges, fmt.Sprintf("%s %s", node, succ))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(revEdges) // make output deterministic
|
|
|
|
for _, e := range revEdges {
|
|
|
|
fmt.Fprintln(stdout, e)
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:13:39 -06:00
|
|
|
case "succs", "preds":
|
|
|
|
if len(args) == 0 {
|
2019-11-20 20:43:00 -07:00
|
|
|
return fmt.Errorf("usage: digraph %s <node> ... ", cmd)
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
g := g
|
|
|
|
if cmd == "preds" {
|
|
|
|
g = g.transpose()
|
|
|
|
}
|
|
|
|
result := make(nodeset)
|
|
|
|
for _, root := range args {
|
|
|
|
edges := g[root]
|
|
|
|
if edges == nil {
|
|
|
|
return fmt.Errorf("no such node %q", root)
|
|
|
|
}
|
|
|
|
result.addAll(edges)
|
|
|
|
}
|
|
|
|
result.sort().println("\n")
|
|
|
|
|
|
|
|
case "forward", "reverse":
|
|
|
|
if len(args) == 0 {
|
2019-11-20 20:43:00 -07:00
|
|
|
return fmt.Errorf("usage: digraph %s <node> ... ", cmd)
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
roots := make(nodeset)
|
|
|
|
for _, root := range args {
|
|
|
|
if g[root] == nil {
|
|
|
|
return fmt.Errorf("no such node %q", root)
|
|
|
|
}
|
|
|
|
roots[root] = true
|
|
|
|
}
|
|
|
|
g := g
|
|
|
|
if cmd == "reverse" {
|
|
|
|
g = g.transpose()
|
|
|
|
}
|
|
|
|
g.reachableFrom(roots).sort().println("\n")
|
|
|
|
|
|
|
|
case "somepath":
|
|
|
|
if len(args) != 2 {
|
2014-11-17 10:58:28 -07:00
|
|
|
return fmt.Errorf("usage: digraph somepath <from> <to>")
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
from, to := args[0], args[1]
|
|
|
|
if g[from] == nil {
|
|
|
|
return fmt.Errorf("no such 'from' node %q", from)
|
|
|
|
}
|
|
|
|
if g[to] == nil {
|
|
|
|
return fmt.Errorf("no such 'to' node %q", to)
|
|
|
|
}
|
2019-07-16 16:12:11 -06:00
|
|
|
if err := g.somepath(from, to); err != nil {
|
|
|
|
return err
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case "allpaths":
|
|
|
|
if len(args) != 2 {
|
2014-11-17 10:58:28 -07:00
|
|
|
return fmt.Errorf("usage: digraph allpaths <from> <to>")
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
from, to := args[0], args[1]
|
|
|
|
if g[from] == nil {
|
|
|
|
return fmt.Errorf("no such 'from' node %q", from)
|
|
|
|
}
|
|
|
|
if g[to] == nil {
|
|
|
|
return fmt.Errorf("no such 'to' node %q", to)
|
|
|
|
}
|
2019-07-16 16:12:11 -06:00
|
|
|
if err := g.allpaths(from, to); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-23 07:13:39 -06:00
|
|
|
|
|
|
|
case "sccs":
|
|
|
|
if len(args) != 0 {
|
2014-11-17 10:58:28 -07:00
|
|
|
return fmt.Errorf("usage: digraph sccs")
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
for _, scc := range g.sccs() {
|
|
|
|
scc.sort().println(" ")
|
|
|
|
}
|
|
|
|
|
|
|
|
case "scc":
|
|
|
|
if len(args) != 1 {
|
2019-10-13 22:01:58 -06:00
|
|
|
return fmt.Errorf("usage: digraph scc <node>")
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
2019-10-13 22:01:58 -06:00
|
|
|
node := args[0]
|
|
|
|
if g[node] == nil {
|
|
|
|
return fmt.Errorf("no such node %q", node)
|
2014-10-23 07:13:39 -06:00
|
|
|
}
|
|
|
|
for _, scc := range g.sccs() {
|
2019-10-13 22:01:58 -06:00
|
|
|
if scc[node] {
|
2014-10-23 07:13:39 -06:00
|
|
|
scc.sort().println("\n")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:20:27 -06:00
|
|
|
case "focus":
|
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("usage: digraph focus <node>")
|
|
|
|
}
|
|
|
|
node := args[0]
|
|
|
|
if g[node] == nil {
|
|
|
|
return fmt.Errorf("no such node %q", node)
|
|
|
|
}
|
|
|
|
|
|
|
|
edges := make(map[string]struct{})
|
|
|
|
for from := range g.reachableFrom(nodeset{node: true}) {
|
|
|
|
for to := range g[from] {
|
|
|
|
edges[fmt.Sprintf("%s %s", from, to)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gtrans := g.transpose()
|
|
|
|
for from := range gtrans.reachableFrom(nodeset{node: true}) {
|
|
|
|
for to := range gtrans[from] {
|
|
|
|
edges[fmt.Sprintf("%s %s", to, from)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 08:44:25 -07:00
|
|
|
edgesSorted := make([]string, 0, len(edges))
|
2019-10-13 22:20:27 -06:00
|
|
|
for e := range edges {
|
|
|
|
edgesSorted = append(edgesSorted, e)
|
|
|
|
}
|
|
|
|
sort.Strings(edgesSorted)
|
|
|
|
fmt.Fprintln(stdout, strings.Join(edgesSorted, "\n"))
|
|
|
|
|
2014-10-23 07:13:39 -06:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("no such command %q", cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-12 11:37:06 -07:00
|
|
|
|
|
|
|
// -- Utilities --------------------------------------------------------
|
|
|
|
|
|
|
|
// split splits a line into words, which are generally separated by
|
|
|
|
// spaces, but Go-style double-quoted string literals are also supported.
|
|
|
|
// (This approximates the behaviour of the Bourne shell.)
|
|
|
|
//
|
|
|
|
// `one "two three"` -> ["one" "two three"]
|
|
|
|
// `a"\n"b` -> ["a\nb"]
|
|
|
|
//
|
|
|
|
func split(line string) ([]string, error) {
|
|
|
|
var (
|
|
|
|
words []string
|
|
|
|
inWord bool
|
|
|
|
current bytes.Buffer
|
|
|
|
)
|
|
|
|
|
|
|
|
for len(line) > 0 {
|
|
|
|
r, size := utf8.DecodeRuneInString(line)
|
|
|
|
if unicode.IsSpace(r) {
|
|
|
|
if inWord {
|
|
|
|
words = append(words, current.String())
|
|
|
|
current.Reset()
|
|
|
|
inWord = false
|
|
|
|
}
|
|
|
|
} else if r == '"' {
|
|
|
|
var ok bool
|
|
|
|
size, ok = quotedLength(line)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("invalid quotation")
|
|
|
|
}
|
|
|
|
s, err := strconv.Unquote(line[:size])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
current.WriteString(s)
|
|
|
|
inWord = true
|
|
|
|
} else {
|
|
|
|
current.WriteRune(r)
|
|
|
|
inWord = true
|
|
|
|
}
|
|
|
|
line = line[size:]
|
|
|
|
}
|
|
|
|
if inWord {
|
|
|
|
words = append(words, current.String())
|
|
|
|
}
|
|
|
|
return words, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// quotedLength returns the length in bytes of the prefix of input that
|
|
|
|
// contain a possibly-valid double-quoted Go string literal.
|
|
|
|
//
|
|
|
|
// On success, n is at least two (""); input[:n] may be passed to
|
|
|
|
// strconv.Unquote to interpret its value, and input[n:] contains the
|
|
|
|
// rest of the input.
|
|
|
|
//
|
|
|
|
// On failure, quotedLength returns false, and the entire input can be
|
|
|
|
// passed to strconv.Unquote if an informative error message is desired.
|
|
|
|
//
|
|
|
|
// quotedLength does not and need not detect all errors, such as
|
|
|
|
// invalid hex or octal escape sequences, since it assumes
|
|
|
|
// strconv.Unquote will be applied to the prefix. It guarantees only
|
|
|
|
// that if there is a prefix of input containing a valid string literal,
|
|
|
|
// its length is returned.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): move this into a strconv-like utility package.
|
|
|
|
//
|
|
|
|
func quotedLength(input string) (n int, ok bool) {
|
|
|
|
var offset int
|
|
|
|
|
|
|
|
// next returns the rune at offset, or -1 on EOF.
|
|
|
|
// offset advances to just after that rune.
|
|
|
|
next := func() rune {
|
|
|
|
if offset < len(input) {
|
|
|
|
r, size := utf8.DecodeRuneInString(input[offset:])
|
|
|
|
offset += size
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if next() != '"' {
|
|
|
|
return // error: not a quotation
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
r := next()
|
|
|
|
if r == '\n' || r < 0 {
|
|
|
|
return // error: string literal not terminated
|
|
|
|
}
|
|
|
|
if r == '"' {
|
|
|
|
return offset, true // success
|
|
|
|
}
|
|
|
|
if r == '\\' {
|
|
|
|
var skip int
|
|
|
|
switch next() {
|
|
|
|
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', '"':
|
|
|
|
skip = 0
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7':
|
|
|
|
skip = 2
|
|
|
|
case 'x':
|
|
|
|
skip = 2
|
|
|
|
case 'u':
|
|
|
|
skip = 4
|
|
|
|
case 'U':
|
|
|
|
skip = 8
|
|
|
|
default:
|
|
|
|
return // error: invalid escape
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < skip; i++ {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|