| 1 |
/** |
| 2 |
* @file |
| 3 |
* @brief <a href=https://en.wikipedia.org/wiki/GXL>GXL</a> converting program |
| 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 Emden R. Gansner and Krishnam Pericherla |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <getopt.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
|
| 27 |
#include "convert.h" |
| 28 |
#include "openFile.h" |
| 29 |
#include <cgraph/ingraphs.h> |
| 30 |
#include <util/exit.h> |
| 31 |
#include <util/gv_ctype.h> |
| 32 |
|
| 33 |
typedef enum { Unset, ToGV, ToGXL } mode; |
| 34 |
|
| 35 |
static FILE *outFile; |
| 36 |
static char *CmdName; |
| 37 |
static char **Files; |
| 38 |
static mode act = Unset; |
| 39 |
|
| 40 |
#ifdef HAVE_EXPAT |
| 41 |
static FILE *getFile(void) |
| 42 |
{ |
| 43 |
FILE *rv = NULL; |
| 44 |
static FILE *savef = NULL; |
| 45 |
static int cnt = 0; |
| 46 |
|
| 47 |
if (Files == NULL) { |
| 48 |
if (cnt++ == 0) { |
| 49 |
rv = stdin; |
| 50 |
} |
| 51 |
} else { |
| 52 |
if (savef) |
| 53 |
fclose(savef); |
| 54 |
while (Files[cnt]) { |
| 55 |
if ((rv = fopen(Files[cnt++], "r")) != 0) |
| 56 |
break; |
| 57 |
else |
| 58 |
fprintf(stderr, "Can't open %s\n", Files[cnt - 1]); |
| 59 |
} |
| 60 |
} |
| 61 |
savef = rv; |
| 62 |
return rv; |
| 63 |
} |
| 64 |
#endif |
| 65 |
|
| 66 |
static const char *use = "Usage: %s [-gd?] [-o<file>] [<graphs>]\n\ |
| 67 |
-g : convert to GXL\n\ |
| 68 |
-d : convert to GV\n\ |
| 69 |
-o<file> : output to <file> (stdout)\n\ |
| 70 |
-? : usage\n"; |
| 71 |
|
| 72 |
static void usage(int v) |
| 73 |
{ |
| 74 |
fprintf(stderr, use, CmdName); |
| 75 |
graphviz_exit(v); |
| 76 |
} |
| 77 |
|
| 78 |
static char *cmdName(char *path) |
| 79 |
{ |
| 80 |
char *sp; |
| 81 |
|
| 82 |
sp = strrchr(path, '/'); |
| 83 |
if (sp) |
| 84 |
sp++; |
| 85 |
else |
| 86 |
sp = path; |
| 87 |
#ifdef _WIN32 |
| 88 |
char *sp2 = strrchr(sp, '\\'); |
| 89 |
if (sp2 != NULL) |
| 90 |
sp = sp2 + 1; |
| 91 |
#endif |
| 92 |
return sp; |
| 93 |
} |
| 94 |
|
| 95 |
static void checkInput(void) |
| 96 |
{ |
| 97 |
char *ep; |
| 98 |
|
| 99 |
ep = strrchr(*Files, '.'); |
| 100 |
if (!ep) |
| 101 |
return; |
| 102 |
ep++; |
| 103 |
if (strcmp(ep, "gv") == 0) |
| 104 |
act = ToGXL; |
| 105 |
else if (strcmp(ep, "dot") == 0) |
| 106 |
act = ToGXL; |
| 107 |
else if (strcmp(ep, "gxl") == 0) |
| 108 |
act = ToGV; |
| 109 |
} |
| 110 |
|
| 111 |
static void setAction(void) |
| 112 |
{ |
| 113 |
if (gv_tolower(CmdName[0]) == 'd') |
| 114 |
act = ToGXL; |
| 115 |
else if (gv_tolower(CmdName[0]) == 'g') { |
| 116 |
if (gv_tolower(CmdName[1]) == 'v') |
| 117 |
act = ToGXL; |
| 118 |
else |
| 119 |
act = ToGV; |
| 120 |
} |
| 121 |
else if (Files) |
| 122 |
checkInput(); |
| 123 |
|
| 124 |
if (act == Unset) { |
| 125 |
fprintf(stderr, "Cannot determine conversion type\n"); |
| 126 |
usage(1); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
static void initargs(int argc, char **argv) |
| 131 |
{ |
| 132 |
int c; |
| 133 |
|
| 134 |
CmdName = cmdName(argv[0]); |
| 135 |
opterr = 0; |
| 136 |
while ((c = getopt(argc, argv, ":gdo:")) != -1) { |
| 137 |
switch (c) { |
| 138 |
case 'd': |
| 139 |
act = ToGV; |
| 140 |
break; |
| 141 |
case 'g': |
| 142 |
act = ToGXL; |
| 143 |
break; |
| 144 |
case 'o': |
| 145 |
if (outFile != NULL) |
| 146 |
fclose(outFile); |
| 147 |
outFile = openFile(CmdName, optarg, "w"); |
| 148 |
break; |
| 149 |
case ':': |
| 150 |
fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt); |
| 151 |
break; |
| 152 |
case '?': |
| 153 |
if (optopt == '?') |
| 154 |
usage(0); |
| 155 |
else { |
| 156 |
fprintf(stderr, "%s: option -%c unrecognized\n", CmdName, |
| 157 |
optopt); |
| 158 |
graphviz_exit(1); |
| 159 |
} |
| 160 |
break; |
| 161 |
default: |
| 162 |
fprintf(stderr, "cvtgxl: unexpected error\n"); |
| 163 |
graphviz_exit(EXIT_FAILURE); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
argv += optind; |
| 168 |
argc -= optind; |
| 169 |
|
| 170 |
if (argc > 0) |
| 171 |
Files = argv; |
| 172 |
if (!outFile) |
| 173 |
outFile = stdout; |
| 174 |
if (act == Unset) |
| 175 |
setAction(); |
| 176 |
} |
| 177 |
|
| 178 |
int main(int argc, char **argv) |
| 179 |
{ |
| 180 |
Agraph_t *G; |
| 181 |
Agraph_t *prev = 0; |
| 182 |
|
| 183 |
initargs(argc, argv); |
| 184 |
if (act == ToGXL) { |
| 185 |
ingraph_state ig; |
| 186 |
newIngraph(&ig, Files); |
| 187 |
|
| 188 |
while ((G = nextGraph(&ig))) { |
| 189 |
if (prev) |
| 190 |
agclose(prev); |
| 191 |
prev = G; |
| 192 |
gv_to_gxl(G, outFile); |
| 193 |
fflush(outFile); |
| 194 |
} |
| 195 |
} else { |
| 196 |
#ifdef HAVE_EXPAT |
| 197 |
FILE *inFile; |
| 198 |
while ((inFile = getFile())) { |
| 199 |
while ((G = gxl_to_gv(inFile))) { |
| 200 |
if (prev) |
| 201 |
agclose(prev); |
| 202 |
prev = G; |
| 203 |
agwrite(G, outFile); |
| 204 |
fflush(outFile); |
| 205 |
} |
| 206 |
} |
| 207 |
#else |
| 208 |
fputs("cvtgxl: not configured for conversion from GXL to GV\n", |
| 209 |
stderr); |
| 210 |
graphviz_exit(1); |
| 211 |
|
| 212 |
#endif |
| 213 |
} |
| 214 |
graphviz_exit(0); |
| 215 |
} |