| 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 "config.h" |
| 12 |
|
| 13 |
#include "gml2gv.h" |
| 14 |
#include <stdbool.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include "openFile.h" |
| 19 |
#include <getopt.h> |
| 20 |
#include <util/agxbuf.h> |
| 21 |
#include <util/alloc.h> |
| 22 |
#include <util/debug.h> |
| 23 |
#include <util/exit.h> |
| 24 |
#include <util/unreachable.h> |
| 25 |
|
| 26 |
static bool Verbose; |
| 27 |
static char *gname = ""; |
| 28 |
static FILE *outFile; |
| 29 |
static char *CmdName; |
| 30 |
static char **Files; |
| 31 |
|
| 32 |
static FILE *getFile(void) { |
| 33 |
FILE *rv = NULL; |
| 34 |
static FILE *savef = NULL; |
| 35 |
static int cnt = 0; |
| 36 |
|
| 37 |
if (Files == NULL) { |
| 38 |
if (cnt++ == 0) { |
| 39 |
rv = stdin; |
| 40 |
} |
| 41 |
} else { |
| 42 |
if (savef) |
| 43 |
fclose(savef); |
| 44 |
while (Files[cnt]) { |
| 45 |
if ((rv = fopen(Files[cnt++], "r")) != 0) |
| 46 |
break; |
| 47 |
else |
| 48 |
fprintf(stderr, "Can't open %s\n", Files[cnt - 1]); |
| 49 |
} |
| 50 |
} |
| 51 |
savef = rv; |
| 52 |
return rv; |
| 53 |
} |
| 54 |
|
| 55 |
static char *useString = "Usage: %s [-v?] [-g<name>] [-o<file>] <files>\n\ |
| 56 |
-g<name> : use <name> as template for graph names\n\ |
| 57 |
-v : verbose mode\n\ |
| 58 |
-o<file> : output to <file> (stdout)\n\ |
| 59 |
-? : print usage\n\ |
| 60 |
If no files are specified, stdin is used\n"; |
| 61 |
|
| 62 |
static void usage(int v) { |
| 63 |
printf(useString, CmdName); |
| 64 |
graphviz_exit(v); |
| 65 |
} |
| 66 |
|
| 67 |
static char *cmdName(char *path) { |
| 68 |
char *sp; |
| 69 |
|
| 70 |
sp = strrchr(path, '/'); |
| 71 |
if (sp) |
| 72 |
sp++; |
| 73 |
else |
| 74 |
sp = path; |
| 75 |
return sp; |
| 76 |
} |
| 77 |
|
| 78 |
static void initargs(int argc, char **argv) { |
| 79 |
int c; |
| 80 |
|
| 81 |
CmdName = cmdName(argv[0]); |
| 82 |
opterr = 0; |
| 83 |
while ((c = getopt(argc, argv, ":g:vo:")) != -1) { |
| 84 |
switch (c) { |
| 85 |
case 'g': |
| 86 |
gname = optarg; |
| 87 |
break; |
| 88 |
case 'v': |
| 89 |
Verbose = true; |
| 90 |
break; |
| 91 |
case 'o': |
| 92 |
if (outFile != NULL) |
| 93 |
fclose(outFile); |
| 94 |
outFile = openFile(CmdName, optarg, "w"); |
| 95 |
break; |
| 96 |
case ':': |
| 97 |
fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt); |
| 98 |
usage(1); |
| 99 |
break; |
| 100 |
case '?': |
| 101 |
if (optopt == '?') |
| 102 |
usage(0); |
| 103 |
else { |
| 104 |
fprintf(stderr, "%s: option -%c unrecognized\n", CmdName, optopt); |
| 105 |
usage(1); |
| 106 |
} |
| 107 |
break; |
| 108 |
default: |
| 109 |
UNREACHABLE(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
argv += optind; |
| 114 |
argc -= optind; |
| 115 |
|
| 116 |
if (argc) |
| 117 |
Files = argv; |
| 118 |
if (!outFile) |
| 119 |
outFile = stdout; |
| 120 |
} |
| 121 |
|
| 122 |
static char *nameOf(agxbuf *buf, char *name, int cnt) { |
| 123 |
if (*name == '\0') |
| 124 |
return name; |
| 125 |
if (cnt) { |
| 126 |
agxbprint(buf, "%s%d", name, cnt); |
| 127 |
return agxbuse(buf); |
| 128 |
} else |
| 129 |
return name; |
| 130 |
} |
| 131 |
|
| 132 |
int main(int argc, char **argv) { |
| 133 |
Agraph_t *G; |
| 134 |
FILE *inFile; |
| 135 |
int gcnt, cnt, rv; |
| 136 |
agxbuf buf = {0}; |
| 137 |
|
| 138 |
rv = 0; |
| 139 |
gcnt = 0; |
| 140 |
initargs(argc, argv); |
| 141 |
while ((inFile = getFile())) { |
| 142 |
cnt = 0; |
| 143 |
while ((G = gml_to_gv(nameOf(&buf, gname, gcnt), inFile, cnt, &rv))) { |
| 144 |
cnt++; |
| 145 |
gcnt++; |
| 146 |
GV_INFO("%s: %d nodes %d edges", agnameof(G), agnnodes(G), agnedges(G)); |
| 147 |
agwrite(G, outFile); |
| 148 |
agclose(G); |
| 149 |
fflush(outFile); |
| 150 |
} |
| 151 |
} |
| 152 |
agxbfree(&buf); |
| 153 |
graphviz_exit(rv); |
| 154 |
} |