.TH GVPR 1 "14 November 2003" .SH NAME gvpr \- graph pattern scanning and processing language .br ( previously known as .B gpr ) .SH SYNOPSIS .B gvpr [\fB-icV?\fP] [ .BI -o .I outfile ] [ .BI -a .I args ] [ .I 'prog' | .BI -f .I progfile ] [ .I files ] .SH DESCRIPTION .B gvpr is a graph stream editor inspired by \fBawk\fP. It copies input graphs to its output, possibly transforming their structure and attributes, creating new graphs, or printing arbitrary information. The graph model is that provided by .IR libagraph (3). In particular, \fBgvpr\fP reads and writes graphs using the dot language. .PP Basically, .B gvpr traverses each input graph, denoted by \fB$G\fP, visiting each node and edge, matching it with the predicate-action rules supplied in the input program. The rules are evaluated in order. For each predicate evaluating to true, the corresponding action is performed. During the traversal, the current node or edge being visited is denoted by \fB$\fP. .PP For each input graph, there is a target subgraph, denoted by \fB$T\fP, initially empty and used to accumulate chosen entities, and an output graph, \fB$O\fP, used for final processing and then written to output. By default, the output graph is the target graph. The output graph can be set in the program or, in a limited sense, on the command line. .SH OPTIONS The following options are supported: .TP .BI \-a " args" The string \fIargs\fP is split into whitespace-separated tokens, with the individual tokens available as strings in the \fBgvpr\fP program as \fBARGV[\fI0\fP],...,ARGV[ARGC-1]\fR. .TP .B \-c Use the source graph as the output graph. .TP .B \-i Derive the node-induced subgraph extension of the output graph in the context of its root graph. .TP .BI \-o " outfile" Causes the output stream to be written to the specified file; by default, output is written to \fBstdout\fP. .TP .BI \-f " progfile" Use the contents of the specified file as the program to execute on the input. If .B \-f is not given, .B gvpr will use the first non-option argument as the program. .TP .B \-V Causes the program to print version information and exit. .TP .B \-? Causes the program to print usage information and exit. .SH OPERANDS The following operand is supported: .TP 8 .I files Names of files containing 1 or more graphs in the dot language. If no .B -f option is given, the first name is removed from the list and used as the input program. If the list of files is empty, \fBstdin\fP will be used. .SH PROGRAMS A .B gvpr program consists of a list of predicate-action clauses, having one of the forms: .IP .BI "BEGIN { " action " }" .IP .BI "BEG_G { " action " }" .IP .BI "N [ " predicate " ] { " action " } .IP .BI "E [ " predicate " ] { " action " } .IP .BI "END_G { " action " }" .IP .BI "END { " action " }" .PP A program can contain at most one of each of the \fBBEGIN\fP, \fBBEG_G\fP, \fBEND_G\fP and \fBEND\fP clauses. There can be any number of \fBN\fP and \fBE\fP statements, the first applied to nodes, the second to edges. The top-level semantics of a \fBgvpr\fP program are: .EX Evaluate the \fBBEGIN\fP clause, if any. For each input graph \fIG\fP { Set \fIG\fP as the current graph and current object. Evaluate the \fBBEG_G\fP clause, if any. For each node and edge in \fIG\fP { Set the node or edge as the current object. Evaluate the \fBN\fP or \fBE\fP clauses, as appropriate. } Set \fIG\fP as the current object. Evaluate the \fBEND_G\fP clause, if any. } Evaluate the \fBEND\fP clause, if any. .EE The actions of the \fBBEGIN\fP, \fBBEG_G\fP, \fBEND_G\fP and \fBEND\fP clauses are performed when the clauses are evaluated. For \fBN\fP or \fBE\fP clauses, either the predicate or action may be omitted. If there is no predicate with an action, the action is performed on every node or edge, as appropriate. If there is no action and the predicate evaluates to true, the associated node or edge is added to the target graph. .PP Predicates and actions are sequences of statements in the C dialect supported by the .IR libexpr (3) library. The only difference between predicates and actions is that the former must have a type that may interpreted as either true or false. Here the usual C convention is followed, in which a non-zero value is considered true. This would include non-empty strings and non-empty references to nodes, edges, etc. However, if a string can be converted to an integer, this value is used. .PP In addition to the usual C base types (\f5void\fP, \f5int\fP, \f5char\fP, \f5float\fP, \f5long\fP, \f5unsigned\fP and \f5double\fP), \fBgvpr\fP \fRprovides \f5string\fP as a synonym for \f5char*\fP, and the graph-based types \f5node_t\fP, \f5edge_t\fP, \f5graph_t\fP and \f5obj_t\fP. The \f5obj_t\fP type can be viewed as a supertype of the other 3 concrete types; the correct base type is maintained dynamically. Besides these base types, the only other supported type expressions are (associative) arrays. .PP Constants follow C syntax, but strings may be quoted with either \fB"..."\fP or \fB'...'\fP. In certain contexts, string values are interpreted as patterns for the purpose of regular expression matching. Patterns use .IR ksh (1) file match pattern syntax. \fBgvpr\fP uses C++ comments. .PP A statement can be a declaration of a function, a variable or an array, or an executable statement. For declarations, there is a single scope. Array declarations have the form: .PP .EX .ta \w'\f(CWdelete array[expression]'u .RS .nf \fI type array \fP[\fI var \fP] .fi .RE .EE .ST .PP where the \fI var \fP is optional. As in C, variables and arrays must be declared. In particular, an undeclared variable will be interpreted as the name of an attribute of a node, edge or graph, depending on the context. .PP Executable statements can be one of the following: .EX .ta \w'\f(CWdelete array[expression]'u .RS .nf {\fR [\fP\fI statement ... \fP\fR] \fP} \fIexpression\fP \fR// commonly\fP\fI var = expression\fP if(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP for(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP for(\fI array \fP[\fI var \fP])\fI statement\fP while(\fI expression \fP)\fI statement\fP switch(\fI expression \fP)\fI case statements\fP break [\fI expression \fP] continue [\fI expression \fP] return [\fI expression \fP] .fi .RE .EE .ST .PP In the second form of the \fBfor\fP statement, the variable \fIvar\fP is set to each value used as an index in the specified array and then the associated \fIstatement\fP is evaluated. Function definitions can only appear in the \fBBEGIN\fP clause. .PP Expressions include the usual C expressions. String comparisons using \fB==\fP and \fB!=\fP treat the right hand operand as a pattern. \fBgvpr\fP will attempt to use an expression as a string or numeric value as appropriate. .PP Expressions of graphical type (i.e., \f5graph_t, node_t, edge_t, obj_t\fP) may be followed by a field reference in the form of \fB.\fP\fIname\fP. The resulting value is the value of the attribute named \fIname\fP of the given object. In addition, in certain contexts an undeclared, unmodified identifier is taken to be an attribute name. Specifically, such identifiers denote attributes of the current node or edge, respectively, in \fBN\fP and \fBE\fP clauses, and the current graph in \fBBEG_G\fP and \fBEND_G\fP clauses. .PP As usual in the .IR libagraph (3) model, attributes are string-valued. In addition, .B gvpr supports certain pseudo-attributes of graph objects, not necessarily string-valued. These reflect intrinsic properties of the graph objects and cannot be set by the user. .TP \fBhead\fR : \fBnode_t\fR the head of an edge. .TP \fBtail\fR : \fBnode_t\fR the tail of an edge. .TP \fBname\fR : \fBstring\fR the name of an edge, node or graph. The name of an edge has the form "\fI<tail-name><edge-op><head-name>\fB[\fI<key>\fB]\fR", where \fI<edge-op>\fP is "\fB->\fP" or "\fB--\fP" depending on whether the graph is directed or not. The bracket part \fB[\fI<key>\fB]\fR only appears if the edge has a non-trivial key. .TP \fBindegree\fR : \fBint\fR the indegree of a node. .TP \fBoutdegree\fR : \fBint\fR the outdegree of a node. .TP \fBdegree\fR : \fBint\fR the degree of a node. .TP \fBroot\fR : \fBgraph_t\fR the root graph of an object. The root of a root graph is itself. .TP \fBparent\fR : \fBgraph_t\fR the parent graph of a subgraph. The parent of a root graph is \fBNULL\fP .TP \fBn_edges\fR : \fBint\fR the number of edges in the graph .TP \fBn_nodes\fR : \fBint\fR the number of nodes in the graph .TP \fBdirected\fR : \fBint\fR true (non-zero) if the graph is directed .TP \fBstrict\fR : \fBint\fR true (non-zero) if the graph is strict .SH "BUILT-IN FUNCTIONS" .PP The following functions are built into \fBgvpr\fP. Those functions returning references to graph objects return \fBNULL\fP in case of failure. .SS "Graphs and subgraph" .TP \fBgraph\fP(\fIs\fP : \fBstring\fP, \fIt\fP : \fBstring\fP) : \fBgraph_t\fP creates a graph whose name is \fIs\fP and whose type is specified by the string \fIt\fP. Ignoring case, the characters \f5U, D, S, N\fR have the interpretation undirected, directed, strict, and non-strict, respectively. If \fIt\fP is empty, a directed, non-strict graph is generated. .TP \fBsubg\fP(\fIg\fP : \fBgraph_t\fP, \fIs\fP : \fBstring\fP) : \fBgraph_t\fP creates a subgraph in graph \fIg\fP with name \fIs\fP. If the subgraph already exists, it is returned. .TP \fBisSubg\fP(\fIg\fP : \fBgraph_t\fP, \fIs\fP : \fBstring\fP) : \fBgraph_t\fP returns the subgraph in graph \fIg\fP with name \fIs\fP, if it exists, or \fBNULL\fP otherwise. .TP \fBfstsubg\fP(\fIg\fP : \fBgraph_t\fP) : \fBgraph_t\fP returns the first subgraph in graph \fIg\fP, or \fBNULL\fP if none exists. .TP \fBnxtsubg\fP(\fIsg\fP : \fBgraph_t\fP) : \fBgraph_t\fP returns the next subgraph after \fIsg\fP, or \fBNULL\fP. .TP \fBisDirect\fP(\fIg\fP : \fBgraph_t\fP) : \fBint\fP returns true if and only if \fIg\fP is directed. .TP \fBisStrict\fP(\fIg\fP : \fBgraph_t\fP) : \fBint\fP returns true if and only if \fIg\fP is strict. .TP \fBnNodes\fP(\fIg\fP : \fBgraph_t\fP) : \fBint\fP returns the number of nodes in \fIg\fP. .TP \fBnEdges\fP(\fIg\fP : \fBgraph_t\fP) : \fBint\fP returns the number of edges in \fIg\fP. .SS "Nodes" .TP \fBnode\fP(\fIsg\fP : \fBgraph_t\fP, \fIs\fP : \fBstring\fP) : \fBnode_t\fP creates a node in graph \fIg\fP of name \fIs\fP. If such a node already exists, it is returned. .TP \fBsubnode\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBnode_t\fP inserts the node \fIn\fP into the subgraph \fIg\fP. Returns the node. .TP \fBfstnode\fP(\fIg\fP : \fBgraph_t\fP) : \fBnode_t\fP returns the first node in graph \fIg\fP, or \fBNULL\fP if none exists. .TP \fBnxtnode\fP(\fIn\fP : \fBnode_t\fP) : \fBnode_t\fP returns the next node after \fIn\fP, or \fBNULL\fP. .TP \fBisNode\fP(\fIsg\fP : \fBgraph_t\fP, \fIs\fP : \fBstring\fP) : \fBnode_t\fP looks for a node in graph \fIg\fP of name \fIs\fP. If such a node exists, it is returned. Otherwise, \fBNULL\fP is returned. .SS "Edges" .TP \fBedge\fP(\fIt\fP : \fBnode_t\fP, \fIh\fP : \fBnode_t\fP, \fIs\fP : \fBstring\fP) : \fBedge_t\fP creates an edge with tail node \fIt\fP, head node \fIh\fP and name \fIs\fP. If the graph is undirected, the distinction between head and tail nodes is unimportant. If such an edge already exists, it is returned. .TP \fBsubedge\fP(\fIg\fP : \fBgraph_t\fP, \fIe\fP : \fBedge_t\fP) : \fBedge_t\fP inserts the edge \fIe\fP into the subgraph \fIg\fP. Returns the edge. .TP \fBisEdge\fP(\fIt\fP : \fBnode_t\fP, \fIh\fP : \fBnode_t\fP, \fIs\fP : \fBstring\fP) : \fBedge_t\fP looks for an edge with tail node \fIt\fP, head node \fIh\fP and name \fIs\fP. If the graph is undirected, the distinction between head and tail nodes is unimportant. If such an edge exists, it is returned. Otherwise, \fBNULL\fP is returned. .TP \fBfstout\fP(\fIn\fP : \fBnode_t\fP) : \fBedge_t\fP returns the first out edge of node \fIn\fP. .TP \fBnxtout\fP(\fIe\fP : \fBedge_t\fP) : \fBedge_t\fP returns the next out edge after \fIe\fP. .TP \fBfstin\fP(\fIn\fP : \fBnode_t\fP) : \fBedge_t\fP returns the first in edge of node \fIn\fP. .TP \fBnxtin\fP(\fIe\fP : \fBedge_t\fP) : \fBedge_t\fP returns the next in edge after \fIe\fP. .TP \fBfstedge\fP(\fIn\fP : \fBnode_t\fP) : \fBedge_t\fP returns the first edge of node \fIn\fP. .TP \fBnxtedge\fP(\fIe\fP : \fBedge_t\fP) : \fBedge_t\fP returns the next edge after \fIe\fP. .SS "Graph I/O" .TP \fBwrite\fP(\fIg\fP : \fBgraph_t\fP) : \fBvoid\fP prints \fIg\fP in dot format onto the output stream. .TP \fBwriteG\fP(\fIg\fP : \fBgraph_t\fP, \fIfname\fP : \fBstring\fP) : \fBvoid\fP prints \fIg\fP in dot format into the file \fIfname\fP. .TP \fBfwriteG\fP(\fIg\fP : \fBgraph_t\fP, \fIfd\fP : \fBint\fP) : \fBvoid\fP prints \fIg\fP in dot format onto the open stream denoted by the integer \fIfd\fP. .TP \fBreadG\fP(\fIfname\fP : \fBstring\fP) : \fBgraph_t\fP returns a graph read from the file \fIfname\fP. The graph should be in dot format. If no graph can be read, \fBNULL\fP is returned. .TP \fBfreadG\fP(\fIfd\fP : \fBint\fP) : \fBgraph_t\fP returns the next graph read from the open stream \fIfd\fP. Returns \fBNULL\fP at end of file. .SS "Graph miscellany" .TP \fBdelete\fP(\fIg\fP : \fBgraph_t\fP, \fIx\fP : \fBobj_t\fP) : \fBvoid\fP deletes object \fIx\fP from graph \fIg\fP. If \fIg\fP is \fBNULL\fP, the function uses the root graph of \fIx\fP. If \fIx\fP is a graph or subgraph, it is closed unless \fIx\fP is locked. .TP \fBisIn\fP(\fIg\fP : \fBgraph_t\fP, \fIx\fP : \fBobj_t\fP) : \fBint\fP returns true if \fIx\fP is in subgraph \fIg\fP. If \fIx\fP is a graph, this indicates that \fIg\fP is the immediate parent graph of \fIx\fP. .TP \fBclone\fP(\fIg\fP : \fBgraph_t\fP, \fIx\fP : \fBobj_t\fP) : \fBobj_t\fP creates a clone of object \fIx\fP in graph \fIg\fP. In particular, the new object has the same name/value attributes and structure as the original object. If an object with the same key as \fIx\fP already exists, its attributes are overlaid by those of \fIx\fP and the object is returned. If an edge is cloned, both endpoints are implicitly cloned. If a graph is cloned, all nodes, edges and subgraphs are implicitly cloned. If \fIx\fP is a graph, \fIg\fP may be \fBNULL\fP, in which case the cloned object will be a new root graph. .TP \fBcopy\fP(\fIg\fP : \fBgraph_t\fP, \fIx\fP : \fBobj_t\fP) : \fBobj_t\fP creates a copy of object \fIx\fP in graph \fIg\fP, where the new object has the same name/value attributes as the original object. If an object with the same key as \fIx\fP already exists, its attributes are overlaid by those of \fIx\fP and the object is returned. Note that this is a shallow copy. If \fIx\fP is a graph, none of its nodes, edges or subgraphs are copied into the new graph. If \fIx\fP is an edge, the endpoints are created if necessary, but they are not cloned. If \fIx\fP is a graph, \fIg\fP may be \fBNULL\fP, in which case the cloned object will be a new root graph. .TP \fBinduce\fP(\fIg\fP : \fBgraph_t\fP) : \fBvoid\fP extends \fIg\fP to its node-induced subgraph extension in its root graph. .TP \fBcompOf\fP(\fIg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBgraph_t\fP returns the connected component of the graph \fIg\fP containing node \fIn\fP, as a subgraph of \fIg\fP. The subgraph only contains the nodes. One can use \fIinduce\fP to add the edges. The function fails and returns \fBNULL\fP if \fIn\fP is not in \fIg\fP. Connectivity is based on the underlying undirected graph of \fIg\fP. .TP \fBlock\fP(\fIg\fP : \fBgraph_t\fP, \fIv\fP : \fBint\fP) : \fBint\fP implements graph locking on root graphs. If the integer \fIv\fP is positive, the graph is set so that future calls to \fBdelete\fP have no immediate effect. If \fIv\fP is zero, the graph is unlocked. If there has been a call to delete the graph while it was locked, the graph is closed. If \fIv\fP is negative, nothing is done. In all cases, the previous lock value is returned. .SS "Strings" .TP \fBsprintf\fP(\fIfmt\fP : \fBstring\fP, \fI...\fP) : \fBstring\fP returns the string resulting from formatting the values of the expressions occurring after \fIfmt\fP according to the .IR printf (3) format .I fmt .TP \fBgsub\fP(\fIstr\fP : \fBstring\fP, \fIpat\fP : \fBstring\fP) : \fBstring\fP .TP \fBgsub\fP(\fIstr\fP : \fBstring\fP, \fIpat\fP : \fBstring\fP, \fIrepl\fP : \fBstring\fP) : \fBstring\fP returns \fIstr\fP with all substrings matching \fIpat\fP deleted or replaced by \fIrepl\fP, respectively. .TP \fBsub\fP(\fIstr\fP : \fBstring\fP, \fIpat\fP : \fBstring\fP) : \fBstring\fP .TP \fBsub\fP(\fIstr\fP : \fBstring\fP, \fIpat\fP : \fBstring\fP, \fIrepl\fP : \fBstring\fP) : \fBstring\fP returns \fIstr\fP with the leftmost substring matching \fIpat\fP deleted or replaced by \fIrepl\fP, respectively. The characters '^' and '$' may be used at the beginning and end, respectively, of \fIpat\fP to anchor the pattern to the beginning or end of \fIstr\fP. .TP \fBsubstr\fP(\fIstr\fP : \fBstring\fP, \fIidx\fP : \fBint\fP) : \fBstring\fP .TP \fBsubstr\fP(\fIstr\fP : \fBstring\fP, \fIidx\fP : \fBint\fP, \fIlen\fP : \fBint\fP) : \fBstring\fP returns the substring of \fIstr\fP starting at position \fIidx\fP to the end of the string or of length \fIlen\fP, respectively. Indexing starts at 0. If \fIidx\fP is negative or \fIidx\fP is greater than the length of \fIstr\fP, a fatal error occurs. Similarly, in the second case, if \fIlen\fP is negative or \fIidx\fP + \fIlen\fP is greater than the length of \fIstr\fP, a fatal error occurs. .TP \fBlength\fP(\fIs\fP : \fBstring\fP) : \fBint\fP returns the length of the string \fIs\fP. .TP \fBindex\fP(\fIs\fP : \fBstring\fP, \fIt\fP : \fBstring\fP) : \fBint\fP returns the index of the character in string \fIs\fP where the leftmost copy of string \fIt\fP can be found, or -1 if \fIt\fP is not a substring of \fIs\fP. .TP \fBmatch\fP(\fIs\fP : \fBstring\fP, \fIp\fP : \fBstring\fP) : \fBint\fP returns the index of the character in string \fIs\fP where the leftmost match of pattern \fIp\fP can be found, or -1 if no substring of \fIs\fP matches \fIp\fP. .TP \fBcanon\fP(\fIs\fP : \fBstring\fP) : \fBstring\fP returns a version of \fIs\fP appropriate to be used as an identifier in a dot file. .TP \fBxOf\fP(\fIs\fP : \fBstring\fP) : \fBstring\fP returns the string "\fIx\fP" if \fIs\fP has the form "\fIx\fP,\fIy\fP", where both \fIx\fP and \fIy\fP are numeric. .TP \fByOf\fP(\fIs\fP : \fBstring\fP) : \fBstring\fP returns the string "\fIy\fP" if \fIs\fP has the form "\fIx\fP,\fIy\fP", where both \fIx\fP and \fIy\fP are numeric. .TP \fBllOf\fP(\fIs\fP : \fBstring\fP) : \fBstring\fP returns the string "\fIllx\fP,\fIlly\fP" if \fIs\fP has the form "\fIllx\fP,\fIlly\fP,\fIurx\fP,\fIury\fP", where all of \fIllx\fP, \fIlly\fP, \fIurx\fP, and \fIury\fP are numeric. .TP .BI urOf( s ) \fBurOf\fP(\fIs\fP : \fBstring\fP) : \fBstring\fP returns the string "\fIurx\fP,\fIury\fP" if \fIs\fP has the form "\fIllx\fP,\fIlly\fP,\fIurx\fP,\fIury\fP", where all of \fIllx\fP, \fIlly\fP, \fIurx\fP, and \fIury\fP are numeric. .SS "I/O" .TP \fBprint\fP(\fI...\fP) : \fBvoid\fP .BI print( " expr" , " ...\fB ) prints a string representation of each argument in turn onto \fBstdout\fP, followed by a newline. .TP \fBprintf\fP(\fIfmt\fP : \fBstring\fP, \fI...\fP) : \fBint\fP .TP \fBprintf\fP(\fIfd\fP : \fBint\fP, \fIfmt\fP : \fBstring\fP, \fI...\fP) : \fBint\fP prints the string resulting from formatting the values of the expressions following \fIfmt\fP according to the .IR printf (3) format .IR fmt . Returns 0 on success. By default, it prints on \fBstdout\fP. If the optional integer \fIfd\fP is given, output is written on the open stream associated with \fIfd\fP. .TP \fBopenF\fP(\fIs\fP : \fBstring\fP, \fIt\fP : \fBstring\fP) : \fBint\fP opens the file \fIs\fP as an I/O stream. The string argument \fIt\fP specifies how the file is opened. The arguments are the same as for the C function .IR fopen (3). It returns an integer denoting the stream, or -1 on error. .sp As usual, streams 0, 1 and 2 are already open as \fBstdin\fP, \fBstdout\fP, and \fBstderr\fP, respectively. Since \fBgvpr\fP may use \fBstdin\fP to read the input graphs, the user should avoid using this stream. .TP \fBcloseF\fP(\fIfd\fP : \fBint\fP) : \fBint\fP closes the open stream denoted by the integer \fIfd\fP. Streams 0, 1 and 2 cannot be closed. Returns 0 on success. .TP \fBreadL\fP(\fIfd\fP : \fBint\fP) : \fBstring\fP returns the next line read from the input stream \fIfd\fP. It returns the empty string "" on end of file. Note that the newline character is left in the returned string. .SS "Math" .TP \fBexp\fP(\fId\fP : \fBdouble\fP) : \fBdouble\fP returns e to the \fId\fPth power. .TP \fBlog\fP(\fId\fP : \fBdouble\fP) : \fBdouble\fP returns the natural log of \fId\fP. .TP \fBsqrt\fP(\fId\fP : \fBdouble\fP) : \fBdouble\fP returns the square root of the double \fId\fP. .TP \fBpow\fP(\fId\fP : \fBdouble\fP, \fIx\fP : \fBdouble\fP) : \fBdouble\fP returns \fId\fP raised to the \fIx\fPth power. .TP \fBcos\fP(\fId\fP : \fBdouble\fP) : \fBdouble\fP returns the cosine of \fId\fP. .TP \fBsin\fP(\fId\fP : \fBdouble\fP) : \fBdouble\fP returns the sine of \fId\fP. .TP \fBatan2\fP(\fIy\fP : \fBdouble\fP, \fIx\fP : \fBdouble\fP) : \fBdouble\fP returns the arctangent of \fIy/x\fP in the range -pi to pi. .SS "Miscellaneous" .TP \fBexit\fP() : \fBvoid\fP .TP \fBexit\fP(\fIv\fP : \fBint\fP) : \fBvoid\fP causes .B gvpr to exit with the exit code .IR v . .I v defaults to 0 if omitted. .TP \fBrand\fP() : \fBdouble\fP returns a pseudo-random double between 0 and 1. .TP \fBsrand\fP() : \fBint\fP .TP \fBsrand\fP(\fIv\fP : \fBint\fP) : \fBint\fP sets a seed for the random number generator. The optional argument gives the seed; if it is omitted, the current time is used. The previous seed value is returned. \fBsrand\fP should be called before any calls to \fBrand\fP. .SH "BUILT-IN VARIABLES" .PP .B gvpr provides certain special, built-in variables, whose values are set automatically by \fBgvpr\fP depending on the context. Except as noted, the user cannot modify their values. .TP \fB$\fP : \fBobj_t\fP denotes the current object (node, edge, graph) depending on the context. It is not available in \fBBEGIN\fP or \fBEND\fP clauses. .TP \fB$F\fP : \fBstring\fP is the name of the current input file. .TP \fB$G\fP : \fBgraph_t\fP denotes the current graph being processed. It is not available in \fBBEGIN\fP or \fBEND\fP clauses. .TP \fB$O\fP : \fBgraph_t\fP denotes the output graph. Before graph traversal, it is initialized to the target graph. After traversal and any \fBEND_G\fP actions, if it refers to a non-empty graph, that graph is printed onto the output stream. It is only valid in \fBN\fP, \fBE\fP and \fBEND_G\fP clauses. The output graph may be set by the user. .TP \fB$T\fP : \fBgraph_t\fP denotes the current target graph. It is a subgraph of \fB$G\fP and is available only in \fBN\fP, \fBE\fP and \fBEND_G\fP clauses. .TP \fB$tgtname\fP : \fBstring\fP denotes the name of the target graph. By default, it is set to \f5"gvpr_result"\fP. If used multiple times during the execution of .BR gvpr , the name will be appended with an integer. This variable may be set by the user. .TP \fB$tvroot\fP : \fBnode_t\fP indicates the starting node for a (directed or undirected) depth-first traversal of the graph (cf. \fB$tvtype\fP below). The default value is \fBNULL\fP for each input graph. .TP \fB$tvtype\fP : \fBtvtype_t\fP indicates how \fBgvpr\fP traverses a graph. At present, it can only take one of six values: \fBTV_flat\fP, \fBTV_dfs\fP, \fBTV_fwd\fP, \fBTV_ref\fP, \fBTV_ne\fP, and \fBTV_en\fP. \fBTV_flat\fP is the default. The meaning of these values is discussed below. .TP \fBARGC\fP : \fBint\fP denotes the number of arguments specified by the \fB-a\fP \fIargs\fP command-line argument. .TP \fBARGV\fP : \fBstring array\fP denotes the array of arguments specified by the \fB-a\fP \fIargs\fP command-line argument. The \fIi\fPth argument is given by \fBARGV[\fIi\fP]\fR. .SH "BUILT-IN CONSTANTS" .PP There are several symbolic constants defined by \fBgvpr\fP. .TP \fBNULL\fR : \fIobj_t\fR a null object reference, equivalent to 0. .TP \fBTV_flat\fR : \fItvtype_t\fR a simple, flat traversal, with graph objects visited in seemingly arbitrary order. .TP \fBTV_ne\fR : \fItvtype_t\fR a traversal which first visits all of the nodes, then all of the edges. .TP \fBTV_en\fR : \fItvtype_t\fR a traversal which first visits all of the edges, then all of the nodes. .TP \fBTV_dfs\fR : \fItvtype_t\fR a traversal of the graph using a depth-first search on the underlying undirected graph. To do the traversal, \fBgvpr\fP will check the value of \fB$tvroot\fP. If this has the same value that it had previously (at the start, the previous value is initialized to \fBNULL\fP.), \fBgvpr\fP will simply look for some unvisited node and traverse its connected component. On the other hand, if \fB$tvroot\fP has changed, its connected component will be toured, assuming it has not been previously visited or, if \fB$tvroot\fP is \fBNULL\fP, the traversal will stop. Note that using \fBTV_dfs\fP and \fB$tvroot\fP, it is possible to create an infinite loop. .TP \fBTV_fwd\fR : \fItvtype_t\fR a traversal of the graph using a depth-first search on the graph following only forward arcs. In .IR libagraph (3), edges in undirected graphs are given an arbitrary direction, which is used for this traversal. The choice of roots for the traversal is the same as described for \fBTV_dfs\fR above. .TP \fBTV_rev\fR : \fItvtype_t\fR a traversal of the graph using a depth-first search on the graph following only reverse arcs. In .IR libagraph (3), edges in undirected graphs are given an arbitrary direction, which is used for this traversal. The choice of roots for the traversal is the same as described for \fBTV_dfs\fR above. .SH EXAMPLES .EX gvpr -i 'N[color=="blue"]' file.dot .EE Generate the node-induced subgraph of all nodes with color blue. .EX gvpr -c 'N[color=="blue"]{color = "red"}' file.dot .EE Make all blue nodes red. .EX BEGIN { int n, e; int tot_n = 0; int tot_e = 0; } BEG_G { n = nNodes($G); e = nEdges($G); printf ("%d nodes %d edges %s\n", n, e, $G.name); tot_n += n; tot_e += e; } END { printf ("%d nodes %d edges total\n", tot_n, tot_e) } .EE Version of the program \fBgc\fP. .EX gvpr -c "" .EE Equivalent to \fBnop\fP. .EX BEG_G { graph_t g = graph ("merge", "S"); } E { node_t h = clone(g,$.head); node_t t = clone(g,$.tail); edge_t e = edge(t,h,""); e.weight = e.weight + 1; } END_G { $O = g; } .EE Produces a strict version of the input graph, where the weight attribute of an edge indicates how many edges from the input graph the edge represents. .EX BEGIN {node_t n; int deg[]} E{deg[head]++; deg[tail]++; } END_G { for (deg[n]) { printf ("deg[%s] = %d\n", n.name, deg[n]); } } .EE Computes the degrees of nodes with edges. .SH BUGS When the program is given as a command line argument, the usual shell interpretation takes place, which may affect some of the special names in \fBgvpr\fP. To avoid this, it is best to wrap the program in single quotes. .PP The constants \fBTV_flat\fP, \fBTV_dfs\fP, \fBTV_fwd\fP, and \fBTV_rev\fP .PP There is a single scope and the extent of all variables is the entire life of the program. It might be preferable for scope to reflect the natural nesting of the clauses, or for the program to at least reset locally declared variables. .PP The expr library does not support string values of (char*)0. This means we can't distinguish between "" and (char*)0 edge keys. For the purposes of looking up and creating edges, we translate "" to be (char*)0, since this latter value is necessary in order to look up any edge with a matching head and tail. .PP The language inherits the usual C problems such as dangling references and the confusion between '=' and '=='. .SH AUTHOR Emden R. Gansner <erg@research.att.com> .SH "SEE ALSO" .PP awk(1), gc(1), dot(1), nop(1), libexpr(3), libagraph(3)