Debian Codegraph @

cmd/tools/tred.c

raw
1 /**
2 * @file
3 * @brief
4 * [transitive reduction](https://en.wikipedia.org/wiki/Transitive_reduction)
5 * filter for directed graphs
6 */
7
8 /*************************************************************************
9 * Copyright (c) 2011 AT&T Intellectual Property
10 * All rights reserved. This program and the accompanying materials
11 * are made available under the terms of the Eclipse Public License v1.0
12 * which accompanies this distribution, and is available at
13 * https://www.eclipse.org/legal/epl-v10.html
14 *************************************************************************/
15
16
17 /*
18 * Written by Stephen North
19 * Updated by Emden Gansner
20 */
21
22 /*
23 * reads a sequence of graphs on stdin, and writes their
24 * transitive reduction
25 */
26
27 #include "config.h"
28
29 #include <cgraph/cgraph.h>
30 #include <cgraph/ingraphs.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <getopt.h>
35 #include <util/exit.h>
36 #include <util/unreachable.h>
37
38 static char **Files;
39 static char *CmdName;
40
41 typedef graphviz_tred_options_t opts_t;
42
43 static char *useString = "Usage: %s [-vr?] <files>\n\
44 -o FILE - redirect output (default to stdout)\n\
45 -v - verbose (to stderr)\n\
46 -r - print removed edges to stderr\n\
47 -? - print usage\n\
48 If no files are specified, stdin is used\n";
49
50 static void usage(int v)
51 {
52 printf(useString, CmdName);
53 graphviz_exit(v);
54 }
55
56 static void init(opts_t *opts, int argc, char *argv[]) {
57 int c;
58
59 CmdName = argv[0];
60 opterr = 0;
61 while ((c = getopt(argc, argv, "o:vr?")) != -1) {
62 switch (c) {
63 case 'o':
64 (void)fclose(opts->out);
65 opts->out = fopen(optarg, "w");
66 if (opts->out == NULL) {
67 fprintf(stderr, "cannot open %s for writing\n",
68 optarg);
69 usage(1);
70 }
71 break;
72 case 'v':
73 opts->Verbose = true;
74 break;
75 case 'r':
76 opts->PrintRemovedEdges = true;
77 break;
78 case '?':
79 if (optopt == '\0' || optopt == '?')
80 usage(0);
81 else {
82 fprintf(stderr, "%s: option -%c unrecognized\n",
83 CmdName, optopt);
84 usage(1);
85 }
86 break;
87 default:
88 UNREACHABLE();
89 }
90 }
91 argv += optind;
92 argc -= optind;
93
94 if (argc)
95 Files = argv;
96 }
97
98 int main(int argc, char **argv)
99 {
100 Agraph_t *g;
101 ingraph_state ig;
102 opts_t opts = {.out = stdout, .err = stderr};
103
104 init(&opts, argc, argv);
105 newIngraph(&ig, Files);
106
107 while ((g = nextGraph(&ig)) != 0) {
108 if (agisdirected(g))
109 graphviz_tred(g, &opts);
110 agclose(g);
111 }
112
113 graphviz_exit(0);
114 }
115