Debian Codegraph @

cmd/tools/unflatten.c

raw
1 /**
2 * @file
3 * @brief adjust directed graphs to improve layout aspect ratio
4 */
5
6 /*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16
17 /*
18 * Written by Stephen North
19 * Updated by Emden Gansner
20 */
21
22 #include "config.h"
23
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <cgraph/cgraph.h>
28 #include <cgraph/ingraphs.h>
29
30 #include <getopt.h>
31 #include "openFile.h"
32 #include <util/exit.h>
33 #include <util/unreachable.h>
34
35 typedef graphviz_unflatten_options_t opts_t;
36
37 static FILE *outFile;
38 static char *cmd;
39
40 static char *useString =
41 "Usage: %s [-f?] [-l <M>] [-c <N>] [-o <outfile>] <files>\n\
42 -o <outfile> - put output in <outfile>\n\
43 -f - adjust immediate fanout chains\n\
44 -l <M> - stagger length of leaf edges between [1,<M>]\n\
45 -c <N> - put disconnected nodes in chains of length <N>\n\
46 -? - print usage\n";
47
48 static void usage(int v)
49 {
50 fprintf(stderr, useString, cmd);
51 graphviz_exit(v);
52 }
53
54 static char **scanargs(opts_t *opts, int argc, char **argv) {
55 int c, ival;
56
57 cmd = argv[0];
58 opterr = 0;
59 while ((c = getopt(argc, argv, ":fl:c:o:")) != -1) {
60 switch (c) {
61 case 'f':
62 opts->Do_fans = true;
63 break;
64 case 'l':
65 ival = atoi(optarg);
66 if (ival > 0)
67 opts->MaxMinlen = ival;
68 break;
69 case 'c':
70 ival = atoi(optarg);
71 if (ival > 0)
72 opts->ChainLimit = ival;
73 break;
74 case 'o':
75 if (outFile != NULL)
76 fclose(outFile);
77 outFile = openFile(cmd, optarg, "w");
78 break;
79 case '?':
80 if (optopt == '?')
81 usage(0);
82 else {
83 fprintf(stderr, "%s: option -%c unrecognized\n", cmd,
84 optopt);
85 usage(-1);
86 }
87 break;
88 case ':':
89 fprintf(stderr, "%s: missing argument for option -%c\n",
90 cmd, optopt);
91 usage(-1);
92 break;
93 default:
94 UNREACHABLE();
95 }
96 }
97 if (opts->Do_fans && opts->MaxMinlen < 1)
98 fprintf(stderr, "%s: Warning: -f requires -l flag\n", cmd);
99 argv += optind;
100 argc -= optind;
101
102 if (!outFile)
103 outFile = stdout;
104 if (argc)
105 return argv;
106 else
107 return 0;
108 }
109
110 int main(int argc, char **argv)
111 {
112 Agraph_t *g;
113 ingraph_state ig;
114 char **files;
115 opts_t opts = {0};
116
117 files = scanargs(&opts, argc, argv);
118 newIngraph(&ig, files);
119 while ((g = nextGraph(&ig))) {
120 graphviz_unflatten(g, &opts);
121 agwrite(g, outFile);
122 }
123 graphviz_exit(0);
124 }