1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
|
.TH GVPR 1 "24 April 2008"
.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\(hyaction 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\(hyseparated tokens,
with the individual tokens
available as strings in the \fBgvpr\fP program
as \fBARGV[\fI0\fP],...,ARGV[ARGC\-1]\fR.
Whitespace characters within single or double quoted substrings, or
preceded by a backslash, are ignored as separators.
In general, a backslash character turns off any special meaning of the
following character.
Note that the tokens derived from multiple \fB\-a\fP flags are concatenated.
.TP
.B \-c
Use the source graph as the output graph.
.TP
.B \-i
Derive the node\(hyinduced 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 \fIprogfile\fP contains a slash character, the name is taken
as the pathname of the file. Otherwise, \fBgvpr\fP will use the
directories specified in the environment variable \fBGPRPATH\fP to look
for the file. If
.B \-f
is not given,
.B gvpr
will use the first non\(hyoption 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\(hyaction 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\(hylevel semantics of a \fBgvpr\fP program are:
.PP
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
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.
.fi
.RE
.DT
.PP
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\(hyzero value is
considered true. This would include non\(hyempty strings and non\(hyempty
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
(\fBvoid\fP, \fBint\fP, \fBchar\fP, \fBfloat\fP, \fBlong\fP,
\fBunsigned\fP and \fBdouble\fP),
\fBgvpr\fP \fRprovides \fBstring\fP as a synonym for \fBchar*\fP, and
the graph\(hybased types \fBnode_t\fP,
\fBedge_t\fP, \fBgraph_t\fP and \fBobj_t\fP.
The \fBobj_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 accepts C++ comments as well as cpp\(hytype comments.
For the latter, if a line begins with a '#' character, the rest of
the line is ignored.
.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
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
\fI type array \fB[\fP type0 \fB]\fR
.fi
.RE
.DT
.PP
where \fI type0 \fP is optional. If it is supplied, the parser will
enforce that all array subscripts have the specified type. If it is
not supplied, objects of all types can be used as subscripts.
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:
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
\fB{\fR [\fI statement ... \fR] \fB}\fR
\fIexpression\fP \fR// commonly\fP\fI var \fB=\fP expression\fR
\fBif(\fI expression \fP)\fI statement \fR[ \fBelse\fI statement \fR]
\fBfor(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP
\fBfor(\fI array \fP[\fI var \fP])\fI statement\fP
\fBwhile(\fI expression \fP)\fI statement\fP
\fBswitch(\fI expression \fP)\fI case statements\fP
\fBbreak [\fI expression \fP]
\fBcontinue [\fI expression \fP]
\fBreturn [\fI expression \fP]\fR
.fi
.RE
.ST
Items in brackets are optional.
.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., \fBgraph_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\(hyvalued.
In addition,
.B gvpr
supports certain pseudo\(hyattributes of graph objects, not necessarily
string\(hyvalued. 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\(hyname><edge\(hyop><head\(hyname>\fB[\fI<key>\fB]\fR",
where \fI<edge\(hyop>\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\(hytrivial 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\(hyzero) if the graph is directed
.TP
\fBstrict\fR : \fBint\fR
true (non\(hyzero) if the graph is strict
.SH "BUILT\(hyIN 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
\fBU, D, S, N\fR have the interpretation undirected, directed,
strict, and non\(hystrict, respectively. If \fIt\fP is empty,
a directed, non\(hystrict 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 in the root graph, or \fBNULL\fP.
.TP
\fBnxtnode_sg\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBnode_t\fP
returns the next node after \fIn\fP in \fIsg\fP, or \fBNULL\fP.
.TP
\fBisNode\fP(\fIsg\fP : \fBgraph_t\fP, \fIs\fP : \fBstring\fP) : \fBnode_t\fP
looks for a node in (sub)graph \fIsg\fP of name \fIs\fP. If such a node
exists, it is returned. Otherwise, \fBNULL\fP is returned.
.TP
\fBisSubnode\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBint\fP
returns non-zero if node \fIn\fP is in (sub)graph \fIsg\fP, or zero
otherwise.
.TP
\fBindegreeOf\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBint\fP
returns the indegree of node \fIn\fP in (sub)graph \fIsg\fP.
.TP
\fBoutdegreeOf\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBint\fP
returns the outdegree of node \fIn\fP in (sub)graph \fIsg\fP.
.TP
\fBdegreeOf\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBint\fP
returns the degree of node \fIn\fP in (sub)graph \fIsg\fP.
.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 in the root graph. If the graph is undirected, the
distinction between head and tail nodes is unimportant.
If such an edge already exists, it is returned.
.TP
\fBedge_sg\fP(\fIsg\fP : \fBgraph_t\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
in (sub)graph \fIsg\fP (and all parent graphs). 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
\fBisEdge_sg\fP(\fIsg\fP : \fBgraph_t\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 in (sub)graph \fIsg\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
\fBisSubedge\fP(\fIg\fP : \fBgraph_t\fP, \fIe\fP : \fBedge_t\fP) : \fBint\fP
returns non-zero if edge \fIe\fP is in (sub)graph \fIsg\fP, or zero
otherwise.
.TP
\fBfstout\fP(\fIn\fP : \fBnode_t\fP) : \fBedge_t\fP
returns the first outedge of node \fIn\fP in the root graph.
.TP
\fBfstout_sg\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBedge_t\fP
returns the first outedge of node \fIn\fP in (sub)graph \fIsg\fP.
.TP
\fBnxtout\fP(\fIe\fP : \fBedge_t\fP) : \fBedge_t\fP
returns the next outedge after \fIe\fP in the root graph.
.TP
\fBnxtout_sg\fP(\fIsg\fP : \fBgraph_t\fP, \fIe\fP : \fBedge_t\fP) : \fBedge_t\fP
returns the next outedge after \fIe\fP in graph \fIsg\fP.
.TP
\fBfstin\fP(\fIn\fP : \fBnode_t\fP) : \fBedge_t\fP
returns the first inedge of node \fIn\fP in the root graph.
.TP
\fBfstin_sg\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBedge_t\fP
returns the first inedge of node \fIn\fP in graph \fIsg\fP.
.TP
\fBnxtin\fP(\fIe\fP : \fBedge_t\fP) : \fBedge_t\fP
returns the next inedge after \fIe\fP in the root graph.
.TP
\fBnxtin_sg\fP(\fIsg\fP : \fBgraph_t\fP, \fIe\fP : \fBedge_t\fP) : \fBedge_t\fP
returns the next inedge after \fIe\fP in graph \fIsg\fP.
.TP
\fBfstedge\fP(\fIn\fP : \fBnode_t\fP) : \fBedge_t\fP
returns the first edge of node \fIn\fP in the root graph.
.TP
\fBfstedge_sg\fP(\fIsg\fP : \fBgraph_t\fP, \fIn\fP : \fBnode_t\fP) : \fBedge_t\fP
returns the first edge of node \fIn\fP in graph \fIsg\fP.
.TP
\fBnxtedge\fP(\fIe\fP : \fBedge_t\fP, \fBnode_t\fP) : \fBedge_t\fP
returns the next edge after \fIe\fP in the root graph.
.TP
\fBnxtedge_sg\fP(\fIsg\fP : \fBgraph_t\fP, \fIe\fP : \fBedge_t\fP, \fBnode_t\fP) : \fBedge_t\fP
returns the next edge after \fIe\fP in the graph \fIsg\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
\fBcopyA\fP(\fIsrc\fP : \fBobj_t\fP, \fItgt\fP : \fBobj_t\fP) : \fBint\fP
copies the attributes of object \fIsrc\fP to object \fItgt\fP, overwriting
any attribute values \fItgt\fP may initially have.
.TP
\fBinduce\fP(\fIg\fP : \fBgraph_t\fP) : \fBvoid\fP
extends \fIg\fP to its node\(hyinduced subgraph extension in its root graph.
.TP
\fBaget\fP(\fIsrc\fP : \fBobj_t\fP, \fIname\fP : \fBstring\fP) : \fBstring\fP
returns the value of attribute \fIname\fP in object \fIsrc\fP. This is
useful for those cases when \fIname\fP conflicts with one of the keywords
such as "head" or "root".
Returns \fBNULL\fP on failure or if the attribute is not defined.
.TP
\fBaset\fP(\fIsrc\fP : \fBobj_t\fP, \fIname\fP : \fBstring\fP, \fIvalue\fP : \fBstring\fP) : \fBint\fP
sets the value of attribute \fIname\fP in object \fIsrc\fP to \fIvalue\fP.
Returns 0 on success, non\(hyzero on failure. See \fBaget\fP above.
.TP
\fBgetDflt\fP(\fIg\fP : \fBgraph_t\fP, \fIkind\fP : \fBstring\fP, \fIname\fP : \fBstring\fP) : \fBstring\fP
returns the default value of attribute \fIname\fP in objects in \fIg\fP of
the given \fIkind\fP. For nodes, edges, and graphs, \fIkind\fP
should be "N", "E", and "G", respectively.
Returns \fBNULL\fP on failure or if the attribute is not defined.
.TP
\fBsetDflt\fP(\fIg\fP : \fBgraph_t\fP, \fIkind\fP : \fBstring\fP, \fIname\fP : \fBstring\fP, \fIvalue\fP : \fBstring\fP) : \fBint\fP
sets the default value of attribute \fIname\fP to \fIvalue\fP in
objects in \fIg\fP of
the given \fIkind\fP. For nodes, edges, and graphs, \fIkind\fP
should be "N", "E", and "G", respectively.
Returns 0 on success, non\(hyzero on failure. See \fBsetDflt\fP above.
.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
\fBkindOf\fP(\fIobj\fP : \fBobj_t\fP) : \fBstring\fP
returns an indication of what kind of graph object is the argument.
For nodes, edges, and graphs, it returns
should be "N", "E", and "G", respectively.
.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.
.TP
\fBsscanf\fP(\fIs\fP : \fBstring\fP, \fIfmt\fP : \fBstring\fP, \fI...\fP) : \fBint\fP
scans the string \fIs\fP, extracting values
according to the
.IR sscanf (3)
format
.IR fmt .
The values are stored in the addresses following \fIfmt\fP,
addresses having the form \fB&\fP\fIv\fP, where \fIv\fP is some declared
variable of the correct type.
Returns the number of items successfully scanned.
.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
\fBscanf\fP(\fIfmt\fP : \fBstring\fP, \fI...\fP) : \fBint\fP
.TP
\fBscanf\fP(\fIfd\fP : \fBint\fP, \fIfmt\fP : \fBstring\fP, \fI...\fP) : \fBint\fP
scans in values from an input stream according to the
.IR scanf (3)
format
.IR fmt .
The values are stored in the addresses following \fIfmt\fP,
addresses having the form \fB&\fP\fIv\fP, where \fIv\fP is some declared
variable of the correct type.
By default, it reads from \fBstdin\fP.
If the optional integer \fIfd\fP is given, input is read from the open
stream associated with \fIfd\fP.
Returns the number of items successfully scanned.
.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\(hyrandom 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\(hyIN VARIABLES"
.PP
.B gvpr
provides certain special, built\(hyin 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\(hyempty 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 \fB"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\(hyfirst 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_bfs\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\(hyline argument.
.TP
\fBARGV\fP : \fBstring array\fP
denotes the array of arguments specified by the
\fB\-a\fP \fIargs\fP
command\(hyline argument. The \fIi\fPth argument is given
by \fBARGV[\fIi\fP]\fR.
.SH "BUILT\(hyIN 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\(hyfirst 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\(hyfirst search on the
graph following only forward arcs. In
.TP
\fBTV_bfs\fR : \fItvtype_t\fR
a traversal of the graph using a bread\(hyfirst search on the
graph ignoring edge directions. See the item on \fBTV_dfs\fR above
for the role of \fB$tvroot\fP.
.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\(hyfirst 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
.PP
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
\fBgvpr \-i 'N[color=="blue"]' file.dot\fP
.fi
.RE
.DT
.PP
Generate the node\(hyinduced subgraph of all nodes with color blue.
.PP
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
\fBgvpr \-c 'N[color=="blue"]{color = "red"}' file.dot\fP
.fi
.RE
.DT
.PP
Make all blue nodes red.
.PP
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
\fBBEGIN { 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) }\fP
.fi
.RE
.DT
.PP
Version of the program \fBgc\fP.
.PP
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
\fBgvpr \-c ""\fP
.fi
.RE
.DT
.PP
Equivalent to \fBnop\fP.
.PP
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
\fBBEG_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; }\fP
.fi
.RE
.DT
.PP
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.
.PP
.ta \w'\f(CWdelete array[expression]'u
.RS
.nf
\fBBEGIN {node_t n; int deg[]}
E{deg[head]++; deg[tail]++; }
END_G {
for (deg[n]) {
printf ("deg[%s] = %d\n", n.name, deg[n]);
}
}\fP
.fi
.RE
.DT
.PP
Computes the degrees of nodes with edges.
.SH ENVIRONMENT
.TP
.B GPRPATH
Colon\(hyseparated list of directories to be searched to find
the file specified by the \-f option.
.SH BUGS AND WARNINGS
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
As of 24 April 2008, \fBgvpr\fP switched to using a new, underlying
graph library, which uses the simpler model that there is only one
copy of a node, not one copy for each subgraph logically containing
it. This means that iterators such as \Inxtnode\P cannot traverse
a subgraph using just a node argument. For this reason, subgraph
traversal requires new functions ending in "_sg", which also take
a subgraph argument. The versions without that suffix will always
traverse the root graph.
.PP
There is a single global scope, except for formal function parameters,
and even these can interfere with the type system. Also, 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.
For now, it is advisable to use distinct names for all variables.
.PP
If a function ends with a complex statement, such as an
IF statement, with each branch doing a return, type checking may fail.
Functions should use a return at the end.
.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
Related to this, strings converted to integers act like char pointers,
getting the value 0 or 1 depending on whether the string consists
solely of zeroes or not. Thus, the ((int)"2") evaluates to 1.
.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)
|