Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gml2gv.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "gml2gv.h"
12#include <stdbool.h>
13#include <stdlib.h>
14#include <string.h>
15
16#include "openFile.h"
17#include <getopt.h>
18#include <util/agxbuf.h>
19#include <util/alloc.h>
20#include <util/exit.h>
21#include <util/unreachable.h>
22
23static bool Verbose;
24static char *gname = "";
25static FILE *outFile;
26static char *CmdName;
27static char **Files;
28
29static FILE *getFile(void) {
30 FILE *rv = NULL;
31 static FILE *savef = NULL;
32 static int cnt = 0;
33
34 if (Files == NULL) {
35 if (cnt++ == 0) {
36 rv = stdin;
37 }
38 } else {
39 if (savef)
40 fclose(savef);
41 while (Files[cnt]) {
42 if ((rv = fopen(Files[cnt++], "r")) != 0)
43 break;
44 else
45 fprintf(stderr, "Can't open %s\n", Files[cnt - 1]);
46 }
47 }
48 savef = rv;
49 return rv;
50}
51
52static char *useString = "Usage: %s [-v?] [-g<name>] [-o<file>] <files>\n\
53 -g<name> : use <name> as template for graph names\n\
54 -v : verbose mode\n\
55 -o<file> : output to <file> (stdout)\n\
56 -? : print usage\n\
57If no files are specified, stdin is used\n";
58
59static void usage(int v) {
60 printf(useString, CmdName);
62}
63
64static char *cmdName(char *path) {
65 char *sp;
66
67 sp = strrchr(path, '/');
68 if (sp)
69 sp++;
70 else
71 sp = path;
72 return sp;
73}
74
75static void initargs(int argc, char **argv) {
76 int c;
77
78 CmdName = cmdName(argv[0]);
79 opterr = 0;
80 while ((c = getopt(argc, argv, ":g:vo:")) != -1) {
81 switch (c) {
82 case 'g':
83 gname = optarg;
84 break;
85 case 'v':
86 Verbose = true;
87 break;
88 case 'o':
89 if (outFile != NULL)
90 fclose(outFile);
91 outFile = openFile(CmdName, optarg, "w");
92 break;
93 case ':':
94 fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt);
95 usage(1);
96 break;
97 case '?':
98 if (optopt == '?')
99 usage(0);
100 else {
101 fprintf(stderr, "%s: option -%c unrecognized\n", CmdName, optopt);
102 usage(1);
103 }
104 break;
105 default:
106 UNREACHABLE();
107 }
108 }
109
110 argv += optind;
111 argc -= optind;
112
113 if (argc)
114 Files = argv;
115 if (!outFile)
116 outFile = stdout;
117}
118
119static char *nameOf(agxbuf *buf, char *name, int cnt) {
120 if (*name == '\0')
121 return name;
122 if (cnt) {
123 agxbprint(buf, "%s%d", name, cnt);
124 return agxbuse(buf);
125 } else
126 return name;
127}
128
129int main(int argc, char **argv) {
130 Agraph_t *G;
131 Agraph_t *prev = 0;
132 FILE *inFile;
133 int gcnt, cnt, rv;
134 agxbuf buf = {0};
135
136 rv = 0;
137 gcnt = 0;
138 initargs(argc, argv);
139 while ((inFile = getFile())) {
140 cnt = 0;
141 while ((G = gml_to_gv(nameOf(&buf, gname, gcnt), inFile, cnt, &rv))) {
142 cnt++;
143 gcnt++;
144 if (prev)
145 agclose(prev);
146 prev = G;
147 if (Verbose)
148 fprintf(stderr, "%s: %d nodes %d edges\n", agnameof(G), agnnodes(G),
149 agnedges(G));
150 agwrite(G, outFile);
151 fflush(outFile);
152 }
153 }
154 agxbfree(&buf);
155 graphviz_exit(rv);
156}
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:78
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:234
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:307
Memory allocation wrappers that exit on failure.
static FILE * inFile
Definition acyclic.c:36
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
#define G
Definition gdefs.h:7
static FILE * getFile(void)
Definition gml2gv.c:29
static char * gname
Definition gml2gv.c:24
static bool Verbose
Definition gml2gv.c:23
static FILE * outFile
Definition gml2gv.c:25
static void initargs(int argc, char **argv)
Definition gml2gv.c:75
static char * cmdName(char *path)
Definition gml2gv.c:64
static char * nameOf(agxbuf *buf, char *name, int cnt)
Definition gml2gv.c:119
static char * useString
Definition gml2gv.c:52
static char ** Files
Definition gml2gv.c:27
static char * CmdName
Definition gml2gv.c:26
GML-DOT converter
Agraph_t * gml_to_gv(char *, FILE *, int, int *)
Definition gmlparse.c:2552
node NULL
Definition grammar.y:163
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:210
int agnedges(Agraph_t *g)
Definition graph.c:175
int agnnodes(Agraph_t *g)
Definition graph.c:169
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:102
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:730
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
static const char * usage
Definition gvpr.c:51
$2 u p prev
Definition htmlparse.y:297
static FILE * openFile(const char *argv0, const char *name, const char *mode)
Definition openFile.h:8
graph or subgraph
Definition cgraph.h:425
Definition types.h:81
int main()
#define UNREACHABLE()
Definition unreachable.h:30