| 1 |
/// @file |
| 2 |
/// @ingroup common_render |
| 3 |
/************************************************************************* |
| 4 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* https://www.eclipse.org/legal/epl-v10.html |
| 9 |
* |
| 10 |
* Contributors: Details at https://graphviz.org |
| 11 |
*************************************************************************/ |
| 12 |
|
| 13 |
#include "config.h" |
| 14 |
|
| 15 |
#include <common/render.h> |
| 16 |
#include <common/htmltable.h> |
| 17 |
#include <errno.h> |
| 18 |
#include <gvc/gvc.h> |
| 19 |
#include <xdot/xdot.h> |
| 20 |
#include <limits.h> |
| 21 |
#include <stdbool.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <util/agxbuf.h> |
| 25 |
#include <util/alloc.h> |
| 26 |
#include <util/exit.h> |
| 27 |
#include <util/gv_ctype.h> |
| 28 |
#include <util/gv_fopen.h> |
| 29 |
#include <util/gv_math.h> |
| 30 |
#include <util/startswith.h> |
| 31 |
#include <util/strcasecmp.h> |
| 32 |
#include <util/streq.h> |
| 33 |
|
| 34 |
static char *usageFmt = |
| 35 |
"Usage: %s [-Vv?] [-(GNEA)name=val] [-(KTlso)<val>] <dot files>\n"; |
| 36 |
|
| 37 |
static char *genericItems = "\n\ |
| 38 |
-V - Print version and exit\n\ |
| 39 |
-v - Enable verbose mode \n\ |
| 40 |
-Gname=val - Set graph attribute 'name' to 'val'\n\ |
| 41 |
-Nname=val - Set node attribute 'name' to 'val'\n\ |
| 42 |
-Ename=val - Set edge attribute 'name' to 'val'\n\ |
| 43 |
-Aname=val - Set attribute 'name' to 'val' for graph, node, and edge\n\ |
| 44 |
-Tv - Set output format to 'v'\n\ |
| 45 |
-Kv - Set layout engine to 'v' (overrides default based on command name)\n\ |
| 46 |
-lv - Use external library 'v'\n\ |
| 47 |
-ofile - Write output to 'file'\n\ |
| 48 |
-O - Automatically generate an output filename based on the input filename with a .'format' appended. (Causes all -ofile options to be ignored.) \n\ |
| 49 |
-P - Internally generate a graph of the current plugins. \n\ |
| 50 |
-q[l] - Set level of message suppression (=1)\n\ |
| 51 |
-s[v] - Scale input by 'v' (=72)\n\ |
| 52 |
-y - Invert y coordinate in output\n"; |
| 53 |
|
| 54 |
static char *neatoFlags = |
| 55 |
"(additional options for neato) [-x] [-n<v>]\n"; |
| 56 |
static char *neatoItems = "\n\ |
| 57 |
-n[v] - No layout mode 'v' (=1)\n\ |
| 58 |
-x - Reduce graph\n"; |
| 59 |
|
| 60 |
static char *fdpFlags = |
| 61 |
"(additional options for fdp) [-L(gO)] [-L(nUCT)<val>]\n"; |
| 62 |
static char *fdpItems = "\n\ |
| 63 |
-Lg - Don't use grid\n\ |
| 64 |
-LO - Use old attractive force\n\ |
| 65 |
-Ln<i> - Set number of iterations to i\n\ |
| 66 |
-LU<i> - Set unscaled factor to i\n\ |
| 67 |
-LC<v> - Set overlap expansion factor to v\n\ |
| 68 |
-LT[*]<v> - Set temperature (temperature factor) to v\n"; |
| 69 |
|
| 70 |
static char *configFlags = "(additional options for config) [-cv]\n"; |
| 71 |
static char *configItems = "\n\ |
| 72 |
-c - Configure plugins (Writes $prefix/lib/graphviz/config \n\ |
| 73 |
with available plugin information. Needs write privilege.)\n\ |
| 74 |
-? - Print usage and exit\n"; |
| 75 |
|
| 76 |
/* Print usage information. If GvExitOnUsage is set, exit with |
| 77 |
* given exval, else return exval+1. |
| 78 |
* |
| 79 |
* @param argv0 Command name to use in usage message |
| 80 |
*/ |
| 81 |
int dotneato_usage(const char *argv0, int exval) { |
| 82 |
FILE *outs; |
| 83 |
|
| 84 |
if (exval > 0) |
| 85 |
outs = stderr; |
| 86 |
else |
| 87 |
outs = stdout; |
| 88 |
|
| 89 |
fprintf(outs, usageFmt, argv0); |
| 90 |
fputs(neatoFlags, outs); |
| 91 |
fputs(fdpFlags, outs); |
| 92 |
fputs(configFlags, outs); |
| 93 |
fputs(genericItems, outs); |
| 94 |
fputs(neatoItems, outs); |
| 95 |
fputs(fdpItems, outs); |
| 96 |
fputs(configItems, outs); |
| 97 |
|
| 98 |
if (GvExitOnUsage && exval >= 0) |
| 99 |
graphviz_exit(exval); |
| 100 |
return exval + 1; |
| 101 |
} |
| 102 |
|
| 103 |
/* Look for flag parameter. idx is index of current argument. |
| 104 |
* We assume argv[*idx] has the form "-x..." If there are characters |
| 105 |
* after the x, return |
| 106 |
* these, else if there are more arguments, return the next one, |
| 107 |
* else return NULL. |
| 108 |
*/ |
| 109 |
static char *getFlagOpt(int argc, char **argv, int *idx) |
| 110 |
{ |
| 111 |
int i = *idx; |
| 112 |
char *arg = argv[i]; |
| 113 |
|
| 114 |
if (arg[2]) |
| 115 |
return arg + 2; |
| 116 |
if (i < argc - 1) { |
| 117 |
i++; |
| 118 |
arg = argv[i]; |
| 119 |
if (*arg && *arg != '-') { |
| 120 |
*idx = i; |
| 121 |
return arg; |
| 122 |
} |
| 123 |
} |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
/* Partial implementation of real basename. |
| 128 |
* Skip over any trailing slashes or backslashes; then |
| 129 |
* find next (back)slash moving left; return string to the right. |
| 130 |
* If no next slash is found, return the whole string. |
| 131 |
*/ |
| 132 |
static char *dotneato_basename(char *pathname) { |
| 133 |
char* ret; |
| 134 |
char* s = pathname; |
| 135 |
if (*s == '\0') return pathname; /* empty string */ |
| 136 |
#ifdef _WIN32 |
| 137 |
/* On Windows, executables, by convention, end in ".exe". Thus, |
| 138 |
* this may be part of the path name and must be removed for |
| 139 |
* matching to work. |
| 140 |
*/ |
| 141 |
{ |
| 142 |
char* dotp = strrchr (s, '.'); |
| 143 |
if (dotp && !strcasecmp(dotp+1,"exe")) *dotp = '\0'; |
| 144 |
} |
| 145 |
#endif |
| 146 |
while (*s) s++; |
| 147 |
s--; |
| 148 |
/* skip over trailing slashes, nulling out as we go */ |
| 149 |
while (s > pathname && (*s == '/' || *s == '\\')) |
| 150 |
*s-- = '\0'; |
| 151 |
if (s == pathname) ret = pathname; |
| 152 |
else { |
| 153 |
while (s > pathname && *s != '/' && *s != '\\') s--; |
| 154 |
if (*s == '/' || *s == '\\') ret = s+1; |
| 155 |
else ret = pathname; |
| 156 |
} |
| 157 |
#ifdef _WIN32 |
| 158 |
/* On Windows, names are case-insensitive, so make name lower-case |
| 159 |
*/ |
| 160 |
gv_tolower_str(ret); |
| 161 |
#endif |
| 162 |
return ret; |
| 163 |
} |
| 164 |
|
| 165 |
static void use_library(GVC_t *gvc, const char *name) |
| 166 |
{ |
| 167 |
static size_t cnt = 0; |
| 168 |
if (name) { |
| 169 |
const size_t old_nmemb = cnt == 0 ? cnt : cnt + 1; |
| 170 |
Lib = gv_recalloc(Lib, old_nmemb, cnt + 2, sizeof(const char *)); |
| 171 |
Lib[cnt++] = name; |
| 172 |
Lib[cnt] = NULL; |
| 173 |
} |
| 174 |
gvc->common.lib = Lib; |
| 175 |
} |
| 176 |
|
| 177 |
static void global_def(char *dcl, int kind) { |
| 178 |
char *p; |
| 179 |
char *rhs = "true"; |
| 180 |
agxbuf xb = {0}; |
| 181 |
|
| 182 |
attrsym_t *sym; |
| 183 |
if ((p = strchr(dcl, '='))) { |
| 184 |
agxbput_n(&xb, dcl, (size_t)(p - dcl)); |
| 185 |
rhs = p+1; |
| 186 |
} |
| 187 |
else |
| 188 |
agxbput(&xb, dcl); |
| 189 |
sym = agattr_text(NULL, kind, agxbuse(&xb), rhs); |
| 190 |
sym->fixed = 1; |
| 191 |
agxbfree(&xb); |
| 192 |
} |
| 193 |
|
| 194 |
static int gvg_init(GVC_t *gvc, graph_t *g, char *fn, int gidx) |
| 195 |
{ |
| 196 |
GVG_t *gvg = gv_alloc(sizeof(GVG_t)); |
| 197 |
if (!gvc->gvgs) |
| 198 |
gvc->gvgs = gvg; |
| 199 |
else |
| 200 |
gvc->gvg->next = gvg; |
| 201 |
gvc->gvg = gvg; |
| 202 |
gvg->gvc = gvc; |
| 203 |
gvg->g = g; |
| 204 |
gvg->input_filename = fn; |
| 205 |
gvg->graph_index = gidx; |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
static graph_t *P_graph; |
| 210 |
|
| 211 |
graph_t *gvPluginsGraph(GVC_t *gvc) |
| 212 |
{ |
| 213 |
gvg_init(gvc, P_graph, "<internal>", 0); |
| 214 |
return P_graph; |
| 215 |
} |
| 216 |
|
| 217 |
/* Scan argv[] for allowed flags. |
| 218 |
* Return 0 on success; v+1 if calling function should call exit(v). |
| 219 |
* If -c is set, config file is created and we exit. |
| 220 |
*/ |
| 221 |
int dotneato_args_initialize(GVC_t * gvc, int argc, char **argv) |
| 222 |
{ |
| 223 |
char c; |
| 224 |
bool Kflag = false; |
| 225 |
|
| 226 |
/* establish if we are running in a CGI environment */ |
| 227 |
HTTPServerEnVar = getenv("SERVER_NAME"); |
| 228 |
|
| 229 |
// test `$GV_FILE_PATH`, a legacy knob, is not set |
| 230 |
if (getenv("GV_FILE_PATH") != NULL) { |
| 231 |
fprintf(stderr, "$GV_FILE_PATH environment variable set; exiting\n" |
| 232 |
"\n" |
| 233 |
"This sandboxing mechanism is no longer supported\n"); |
| 234 |
graphviz_exit(EXIT_FAILURE); |
| 235 |
} |
| 236 |
|
| 237 |
gvc->common.cmdname = dotneato_basename(argv[0]); |
| 238 |
if (gvc->common.verbose) { |
| 239 |
fprintf(stderr, "%s - %s version %s (%s)\n", |
| 240 |
gvc->common.cmdname, gvc->common.info[0], |
| 241 |
gvc->common.info[1], gvc->common.info[2]); |
| 242 |
} |
| 243 |
|
| 244 |
/* configure for available plugins */ |
| 245 |
/* needs to know if "dot -c" is set (gvc->common.config) */ |
| 246 |
/* must happen before trying to select any plugins */ |
| 247 |
if (gvc->common.config) { |
| 248 |
gvconfig(gvc, gvc->common.config); |
| 249 |
graphviz_exit(0); |
| 250 |
} |
| 251 |
|
| 252 |
/* feed the globals */ |
| 253 |
Verbose = gvc->common.verbose > UCHAR_MAX |
| 254 |
? UCHAR_MAX |
| 255 |
: (unsigned char)gvc->common.verbose; |
| 256 |
|
| 257 |
size_t nfiles = 0; |
| 258 |
for (int i = 1; i < argc; i++) |
| 259 |
if (argv[i] && argv[i][0] != '-') |
| 260 |
nfiles++; |
| 261 |
gvc->input_filenames = gv_calloc(nfiles + 1, sizeof(char *)); |
| 262 |
nfiles = 0; |
| 263 |
for (int i = 1; i < argc; i++) { |
| 264 |
if (argv[i] && |
| 265 |
(startswith(argv[i], "-V") || strcmp(argv[i], "--version") == 0)) { |
| 266 |
fprintf(stderr, "%s - %s version %s (%s)\n", |
| 267 |
gvc->common.cmdname, gvc->common.info[0], |
| 268 |
gvc->common.info[1], gvc->common.info[2]); |
| 269 |
if (GvExitOnUsage) graphviz_exit(0); |
| 270 |
return 1; |
| 271 |
} else if (argv[i] && |
| 272 |
(startswith(argv[i], "-?") || strcmp(argv[i], "--help") == 0)) { |
| 273 |
return dotneato_usage(argv[0], 0); |
| 274 |
} else if (argv[i] && startswith(argv[i], "--filepath=")) { |
| 275 |
free(Gvfilepath); |
| 276 |
Gvfilepath = gv_strdup(argv[i] + strlen("--filepath=")); |
| 277 |
} else if (argv[i] && argv[i][0] == '-') { |
| 278 |
char *const rest = &argv[i][2]; |
| 279 |
switch (c = argv[i][1]) { |
| 280 |
case 'G': |
| 281 |
if (*rest) |
| 282 |
global_def(rest, AGRAPH); |
| 283 |
else { |
| 284 |
fprintf(stderr, "Missing argument for -G flag\n"); |
| 285 |
return dotneato_usage(argv[0], 1); |
| 286 |
} |
| 287 |
break; |
| 288 |
case 'N': |
| 289 |
if (*rest) |
| 290 |
global_def(rest, AGNODE); |
| 291 |
else { |
| 292 |
fprintf(stderr, "Missing argument for -N flag\n"); |
| 293 |
return dotneato_usage(argv[0], 1); |
| 294 |
} |
| 295 |
break; |
| 296 |
case 'E': |
| 297 |
if (*rest) |
| 298 |
global_def(rest, AGEDGE); |
| 299 |
else { |
| 300 |
fprintf(stderr, "Missing argument for -E flag\n"); |
| 301 |
return dotneato_usage(argv[0], 1); |
| 302 |
} |
| 303 |
break; |
| 304 |
case 'A': |
| 305 |
if (*rest) { |
| 306 |
global_def(rest, AGRAPH); |
| 307 |
global_def(rest, AGNODE); |
| 308 |
global_def(rest, AGEDGE); |
| 309 |
} else { |
| 310 |
fprintf(stderr, "Missing argument for -A flag\n"); |
| 311 |
return dotneato_usage(argv[0], 1); |
| 312 |
} |
| 313 |
break; |
| 314 |
case 'T': { |
| 315 |
const char *const val = getFlagOpt(argc, argv, &i); |
| 316 |
if (!val) { |
| 317 |
fprintf(stderr, "Missing argument for -T flag\n"); |
| 318 |
return dotneato_usage(argv[0], 1); |
| 319 |
} |
| 320 |
if (!gvjobs_output_langname(gvc, val)) { |
| 321 |
const char *const fmts = gvplugin_list(gvc, API_device, val); |
| 322 |
fprintf(stderr, "Format: \"%s\" not recognized.", val); |
| 323 |
if (strlen(fmts) > 1) { |
| 324 |
fprintf(stderr, " Use one of:%s\n", fmts); |
| 325 |
} else { |
| 326 |
/* Q: Should 'dot -c' be suggested generally or only when val = "dot"? */ |
| 327 |
fprintf(stderr, " No formats found.\nPerhaps \"dot -c\" needs to be run (with installer's privileges) to register the plugins?\n"); |
| 328 |
} |
| 329 |
if (GvExitOnUsage) graphviz_exit(1); |
| 330 |
return 2; |
| 331 |
} |
| 332 |
break; |
| 333 |
} |
| 334 |
case 'K': { |
| 335 |
const char *const val = getFlagOpt(argc, argv, &i); |
| 336 |
if (!val) { |
| 337 |
fprintf(stderr, "Missing argument for -K flag\n"); |
| 338 |
return dotneato_usage(argv[0], 1); |
| 339 |
} |
| 340 |
const int v = gvlayout_select(gvc, val); |
| 341 |
if (v == NO_SUPPORT) { |
| 342 |
fprintf(stderr, "There is no layout engine support for \"%s\"\n", val); |
| 343 |
if (streq(val, "dot")) { |
| 344 |
fprintf(stderr, "Perhaps \"dot -c\" needs to be run (with installer's privileges) to register the plugins?\n"); |
| 345 |
} |
| 346 |
else { |
| 347 |
const char *const lyts = gvplugin_list(gvc, API_layout, val); |
| 348 |
if (strlen(lyts) > 1) { |
| 349 |
fprintf(stderr, " Use one of:%s\n", lyts); |
| 350 |
} else { |
| 351 |
/* Q: Should 'dot -c' be suggested generally or only when val = "dot"? */ |
| 352 |
fprintf(stderr, " No layouts found.\nPerhaps \"dot -c\" needs to be run (with installer's privileges) to register the plugins?\n"); |
| 353 |
} |
| 354 |
} |
| 355 |
if (GvExitOnUsage) graphviz_exit(1); |
| 356 |
return 2; |
| 357 |
} |
| 358 |
Kflag = true; |
| 359 |
break; |
| 360 |
} |
| 361 |
case 'P': |
| 362 |
P_graph = gvplugin_graph(gvc); |
| 363 |
break; |
| 364 |
case 'l': { |
| 365 |
const char *const val = getFlagOpt(argc, argv, &i); |
| 366 |
if (!val) { |
| 367 |
fprintf(stderr, "Missing argument for -l flag\n"); |
| 368 |
return dotneato_usage(argv[0], 1); |
| 369 |
} |
| 370 |
use_library(gvc, val); |
| 371 |
break; |
| 372 |
} |
| 373 |
case 'o': { |
| 374 |
const char *const val = getFlagOpt(argc, argv, &i); |
| 375 |
if (!val) { |
| 376 |
fprintf(stderr, "Missing argument for -o flag\n"); |
| 377 |
return dotneato_usage(argv[0], 1); |
| 378 |
} |
| 379 |
if (! gvc->common.auto_outfile_names) |
| 380 |
gvjobs_output_filename(gvc, val); |
| 381 |
break; |
| 382 |
} |
| 383 |
case 'q': |
| 384 |
if (*rest) { |
| 385 |
const int v = atoi(rest); |
| 386 |
if (v <= 0) { |
| 387 |
fprintf(stderr, |
| 388 |
"Invalid parameter \"%s\" for -q flag - ignored\n", |
| 389 |
rest); |
| 390 |
} else if (v == 1) |
| 391 |
agseterr(AGERR); |
| 392 |
else |
| 393 |
agseterr(AGMAX); |
| 394 |
} else |
| 395 |
agseterr(AGERR); |
| 396 |
break; |
| 397 |
case 's': |
| 398 |
if (*rest) { |
| 399 |
PSinputscale = atof(rest); |
| 400 |
if (PSinputscale < 0) { |
| 401 |
fprintf(stderr, |
| 402 |
"Invalid parameter \"%s\" for -s flag\n", |
| 403 |
rest); |
| 404 |
return dotneato_usage(argv[0], 1); |
| 405 |
} |
| 406 |
else if (is_exactly_zero(PSinputscale)) |
| 407 |
PSinputscale = POINTS_PER_INCH; |
| 408 |
} else |
| 409 |
PSinputscale = POINTS_PER_INCH; |
| 410 |
break; |
| 411 |
case 'x': |
| 412 |
Reduce = true; |
| 413 |
break; |
| 414 |
case 'y': |
| 415 |
Y_invert = true; |
| 416 |
break; |
| 417 |
default: |
| 418 |
agerrorf("%s: option -%c unrecognized\n\n", gvc->common.cmdname, |
| 419 |
c); |
| 420 |
return dotneato_usage(argv[0], 1); |
| 421 |
} |
| 422 |
} else if (argv[i]) |
| 423 |
gvc->input_filenames[nfiles++] = argv[i]; |
| 424 |
} |
| 425 |
|
| 426 |
/* if no -K, use cmd name to set layout type */ |
| 427 |
if (!Kflag) { |
| 428 |
const char *layout = gvc->common.cmdname; |
| 429 |
if (streq(layout, "dot_static") |
| 430 |
|| streq(layout, "dot_builtins") |
| 431 |
|| streq(layout, "lt-dot") |
| 432 |
|| streq(layout, "lt-dot_builtins") |
| 433 |
|| streq(layout, "") /* when run as a process from Gvedit on Windows */ |
| 434 |
) |
| 435 |
layout = "dot"; |
| 436 |
const int i = gvlayout_select(gvc, layout); |
| 437 |
if (i == NO_SUPPORT) { |
| 438 |
fprintf(stderr, "There is no layout engine support for \"%s\"\n", layout); |
| 439 |
if (streq(layout, "dot")) { |
| 440 |
fprintf(stderr, "Perhaps \"dot -c\" needs to be run (with installer's privileges) to register the plugins?\n"); |
| 441 |
} else { |
| 442 |
const char *const lyts = gvplugin_list(gvc, API_layout, ""); |
| 443 |
if (strlen(lyts) > 1) { |
| 444 |
fprintf(stderr, " Use one of:%s\n", lyts); |
| 445 |
} else { |
| 446 |
/* Q: Should 'dot -c' be suggested generally or only when val = "dot"? */ |
| 447 |
fprintf(stderr, " No layouts found.\nPerhaps \"dot -c\" needs to be run (with installer's privileges) to register the plugins?\n"); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
if (GvExitOnUsage) graphviz_exit(1); |
| 452 |
return 2; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/* if no -Txxx, then set default format */ |
| 457 |
if (!gvc->jobs || !gvc->jobs->output_langname) { |
| 458 |
if (!gvjobs_output_langname(gvc, "dot")) { |
| 459 |
fprintf(stderr, |
| 460 |
"Unable to find even the default \"-Tdot\" renderer. Has the config\nfile been generated by running \"dot -c\" with installer's privileges?\n"); |
| 461 |
return 2; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
/* set persistent attributes here (if not already set from command line options) */ |
| 466 |
if (!agattr_text(NULL, AGNODE, "label", 0)) |
| 467 |
agattr_text(NULL, AGNODE, "label", NODENAME_ESC); |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
/* converts a graph attribute in inches to a pointf in points. |
| 472 |
* If only one number is given, it is used for both x and y. |
| 473 |
* Returns true if the attribute ends in '!'. |
| 474 |
*/ |
| 475 |
static bool getdoubles2ptf(graph_t *g, char *name, pointf *result) { |
| 476 |
char *p; |
| 477 |
int i; |
| 478 |
double xf, yf; |
| 479 |
char c = '\0'; |
| 480 |
bool rv = false; |
| 481 |
|
| 482 |
if ((p = agget(g, name))) { |
| 483 |
i = sscanf(p, "%lf,%lf%c", &xf, &yf, &c); |
| 484 |
if (i > 1 && xf > 0 && yf > 0) { |
| 485 |
result->x = POINTS(xf); |
| 486 |
result->y = POINTS(yf); |
| 487 |
if (c == '!') |
| 488 |
rv = true; |
| 489 |
} |
| 490 |
else { |
| 491 |
c = '\0'; |
| 492 |
i = sscanf(p, "%lf%c", &xf, &c); |
| 493 |
if (i > 0 && xf > 0) { |
| 494 |
result->y = result->x = POINTS(xf); |
| 495 |
if (c == '!') rv = true; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
return rv; |
| 500 |
} |
| 501 |
|
| 502 |
void getdouble(graph_t * g, char *name, double *result) |
| 503 |
{ |
| 504 |
char *p; |
| 505 |
double f; |
| 506 |
|
| 507 |
if ((p = agget(g, name))) { |
| 508 |
if (sscanf(p, "%lf", &f) >= 1) |
| 509 |
*result = f; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
graph_t *gvNextInputGraph(GVC_t *gvc) |
| 514 |
{ |
| 515 |
graph_t *g = NULL; |
| 516 |
static char *fn; |
| 517 |
static FILE *fp; |
| 518 |
static int gidx; |
| 519 |
|
| 520 |
while (!g) { |
| 521 |
if (!fp) { |
| 522 |
if (!(fn = gvc->input_filenames[0])) { |
| 523 |
if (gvc->fidx++ == 0) |
| 524 |
fp = stdin; |
| 525 |
} |
| 526 |
else { |
| 527 |
while ((fn = gvc->input_filenames[gvc->fidx++]) && !(fp = gv_fopen(fn, "r"))) { |
| 528 |
agerrorf("%s: can't open %s: %s\n", gvc->common.cmdname, fn, strerror(errno)); |
| 529 |
graphviz_errors++; |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
if (fp == NULL) |
| 534 |
break; |
| 535 |
g = agconcat(NULL, fn ? fn : "<stdin>", fp, NULL); |
| 536 |
if (g) { |
| 537 |
gvg_init(gvc, g, fn, gidx++); |
| 538 |
break; |
| 539 |
} |
| 540 |
if (fp != stdin) |
| 541 |
fclose (fp); |
| 542 |
fp = NULL; |
| 543 |
gidx = 0; |
| 544 |
} |
| 545 |
return g; |
| 546 |
} |
| 547 |
|
| 548 |
/* Check if the charset attribute is defined for the graph and, if |
| 549 |
* so, return the corresponding internal value. If undefined, return |
| 550 |
* CHAR_UTF8 |
| 551 |
*/ |
| 552 |
static unsigned char findCharset(graph_t *g) { |
| 553 |
char* p; |
| 554 |
|
| 555 |
p = late_nnstring(g,agfindgraphattr(g,"charset"),"utf-8"); |
| 556 |
if (!strcasecmp(p,"latin-1") |
| 557 |
|| !strcasecmp(p,"latin1") |
| 558 |
|| !strcasecmp(p,"l1") |
| 559 |
|| !strcasecmp(p,"ISO-8859-1") |
| 560 |
|| !strcasecmp(p,"ISO_8859-1") |
| 561 |
|| !strcasecmp(p,"ISO8859-1") |
| 562 |
|| !strcasecmp(p,"ISO-IR-100")) |
| 563 |
return CHAR_LATIN1; |
| 564 |
if (!strcasecmp(p,"big-5") |
| 565 |
|| !strcasecmp(p,"big5")) |
| 566 |
return CHAR_BIG5; |
| 567 |
if (!strcasecmp(p,"utf-8") |
| 568 |
|| !strcasecmp(p,"utf8")) |
| 569 |
return CHAR_UTF8; |
| 570 |
agwarningf("Unsupported charset \"%s\" - assuming utf-8\n", p); |
| 571 |
return CHAR_UTF8; |
| 572 |
} |
| 573 |
|
| 574 |
/// Checks "ratio" attribute, if any, and sets enum type. |
| 575 |
static void setRatio(graph_t * g) |
| 576 |
{ |
| 577 |
char *p; |
| 578 |
double ratio; |
| 579 |
|
| 580 |
if ((p = agget(g, "ratio"))) { |
| 581 |
if (streq(p, "auto")) { |
| 582 |
GD_drawing(g)->ratio_kind = R_AUTO; |
| 583 |
} else if (streq(p, "compress")) { |
| 584 |
GD_drawing(g)->ratio_kind = R_COMPRESS; |
| 585 |
} else if (streq(p, "expand")) { |
| 586 |
GD_drawing(g)->ratio_kind = R_EXPAND; |
| 587 |
} else if (streq(p, "fill")) { |
| 588 |
GD_drawing(g)->ratio_kind = R_FILL; |
| 589 |
} else { |
| 590 |
ratio = atof(p); |
| 591 |
if (ratio > 0.0) { |
| 592 |
GD_drawing(g)->ratio_kind = R_VALUE; |
| 593 |
GD_drawing(g)->ratio = ratio; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
void graph_init(graph_t * g, bool use_rankdir) |
| 600 |
{ |
| 601 |
char *p; |
| 602 |
double xf; |
| 603 |
static char *rankname[] = { "local", "global", "none", NULL }; |
| 604 |
static int rankcode[] = { LOCAL, GLOBAL, NOCLUST, LOCAL }; |
| 605 |
static char *fontnamenames[] = {"gd","ps","svg", NULL}; |
| 606 |
static int fontnamecodes[] = {NATIVEFONTS,PSFONTS,SVGFONTS,-1}; |
| 607 |
int rankdir; |
| 608 |
GD_drawing(g) = gv_alloc(sizeof(layout_t)); |
| 609 |
|
| 610 |
/* reparseable input */ |
| 611 |
if ((p = agget(g, "postaction"))) { /* requires a graph wrapper for yyparse */ |
| 612 |
agxbuf buf = {0}; |
| 613 |
agxbprint(&buf, "%s { %s }", agisdirected(g) ? "digraph" : "graph", p); |
| 614 |
agmemconcat(g, agxbuse(&buf)); |
| 615 |
agxbfree(&buf); |
| 616 |
} |
| 617 |
|
| 618 |
/* set this up fairly early in case any string sizes are needed */ |
| 619 |
if ((p = agget(g, "fontpath")) || (p = getenv("DOTFONTPATH"))) { |
| 620 |
/* overide GDFONTPATH in local environment if dot |
| 621 |
* wants its own */ |
| 622 |
#ifdef HAVE_SETENV |
| 623 |
setenv("GDFONTPATH", p, 1); |
| 624 |
#else |
| 625 |
static agxbuf buf; |
| 626 |
agxbprint(&buf, "GDFONTPATH=%s", p); |
| 627 |
putenv(agxbuse(&buf)); |
| 628 |
#endif |
| 629 |
} |
| 630 |
|
| 631 |
GD_charset(g) = findCharset (g); |
| 632 |
|
| 633 |
if (!HTTPServerEnVar) { |
| 634 |
Gvimagepath = agget (g, "imagepath"); |
| 635 |
if (!Gvimagepath) { |
| 636 |
Gvimagepath = Gvfilepath; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
GD_drawing(g)->quantum = |
| 641 |
late_double(g, agfindgraphattr(g, "quantum"), 0.0, 0.0); |
| 642 |
|
| 643 |
/* setting rankdir=LR is only defined in dot, |
| 644 |
* but having it set causes shape code and others to use it. |
| 645 |
* The result is confused output, so we turn it off unless requested. |
| 646 |
* This effective rankdir is stored in the bottom 2 bits of g->u.rankdir. |
| 647 |
* Sometimes, the code really needs the graph's rankdir, e.g., neato -n |
| 648 |
* with record shapes, so we store the real rankdir in the next 2 bits. |
| 649 |
*/ |
| 650 |
rankdir = RANKDIR_TB; |
| 651 |
if ((p = agget(g, "rankdir"))) { |
| 652 |
if (streq(p, "LR")) |
| 653 |
rankdir = RANKDIR_LR; |
| 654 |
else if (streq(p, "BT")) |
| 655 |
rankdir = RANKDIR_BT; |
| 656 |
else if (streq(p, "RL")) |
| 657 |
rankdir = RANKDIR_RL; |
| 658 |
} |
| 659 |
if (use_rankdir) |
| 660 |
SET_RANKDIR (g, (rankdir << 2) | rankdir); |
| 661 |
else |
| 662 |
SET_RANKDIR(g, rankdir << 2); |
| 663 |
|
| 664 |
xf = late_double(g, agfindgraphattr(g, "nodesep"), |
| 665 |
DEFAULT_NODESEP, MIN_NODESEP); |
| 666 |
GD_nodesep(g) = POINTS(xf); |
| 667 |
|
| 668 |
p = late_string(g, agfindgraphattr(g, "ranksep"), NULL); |
| 669 |
if (p) { |
| 670 |
if (sscanf(p, "%lf", &xf) == 0) |
| 671 |
xf = DEFAULT_RANKSEP; |
| 672 |
else { |
| 673 |
if (xf < MIN_RANKSEP) |
| 674 |
xf = MIN_RANKSEP; |
| 675 |
} |
| 676 |
if (strstr(p, "equally")) |
| 677 |
GD_exact_ranksep(g) = true; |
| 678 |
} else |
| 679 |
xf = DEFAULT_RANKSEP; |
| 680 |
GD_ranksep(g) = POINTS(xf); |
| 681 |
|
| 682 |
{ |
| 683 |
int showboxes = late_int(g, agfindgraphattr(g, "showboxes"), 0, 0); |
| 684 |
if (showboxes > UCHAR_MAX) { |
| 685 |
showboxes = UCHAR_MAX; |
| 686 |
} |
| 687 |
GD_showboxes(g) = (unsigned char)showboxes; |
| 688 |
} |
| 689 |
p = late_string(g, agfindgraphattr(g, "fontnames"), NULL); |
| 690 |
GD_fontnames(g) = maptoken(p, fontnamenames, fontnamecodes); |
| 691 |
|
| 692 |
setRatio(g); |
| 693 |
GD_drawing(g)->filled = getdoubles2ptf(g, "size", &GD_drawing(g)->size); |
| 694 |
getdoubles2ptf(g, "page", &GD_drawing(g)->page); |
| 695 |
|
| 696 |
GD_drawing(g)->centered = mapbool(agget(g, "center")); |
| 697 |
|
| 698 |
if ((p = agget(g, "rotate"))) |
| 699 |
GD_drawing(g)->landscape = atoi(p) == 90; |
| 700 |
else if ((p = agget(g, "orientation"))) |
| 701 |
GD_drawing(g)->landscape = p[0] == 'l' || p[0] == 'L'; |
| 702 |
else if ((p = agget(g, "landscape"))) |
| 703 |
GD_drawing(g)->landscape = mapbool(p); |
| 704 |
|
| 705 |
p = agget(g, "clusterrank"); |
| 706 |
CL_type = maptoken(p, rankname, rankcode); |
| 707 |
p = agget(g, "concentrate"); |
| 708 |
Concentrate = mapbool(p); |
| 709 |
State = GVBEGIN; |
| 710 |
EdgeLabelsDone = 0; |
| 711 |
|
| 712 |
GD_drawing(g)->dpi = late_double(g, agfindgraphattr(g, "dpi"), |
| 713 |
late_double(g, |
| 714 |
agfindgraphattr(g, |
| 715 |
"resolution"), |
| 716 |
0, 0), 0); |
| 717 |
|
| 718 |
do_graph_label(g); |
| 719 |
|
| 720 |
Initial_dist = MYHUGE; |
| 721 |
|
| 722 |
G_ordering = agfindgraphattr(g, "ordering"); |
| 723 |
G_gradientangle = agfindgraphattr(g,"gradientangle"); |
| 724 |
G_margin = agfindgraphattr(g, "margin"); |
| 725 |
|
| 726 |
/* initialize nodes */ |
| 727 |
N_height = agfindnodeattr(g, "height"); |
| 728 |
N_width = agfindnodeattr(g, "width"); |
| 729 |
N_shape = agfindnodeattr(g, "shape"); |
| 730 |
N_color = agfindnodeattr(g, "color"); |
| 731 |
N_fillcolor = agfindnodeattr(g, "fillcolor"); |
| 732 |
N_style = agfindnodeattr(g, "style"); |
| 733 |
N_fontsize = agfindnodeattr(g, "fontsize"); |
| 734 |
N_fontname = agfindnodeattr(g, "fontname"); |
| 735 |
N_fontcolor = agfindnodeattr(g, "fontcolor"); |
| 736 |
N_label = agfindnodeattr(g, "label"); |
| 737 |
if (!N_label) |
| 738 |
N_label = agattr_text(g, AGNODE, "label", NODENAME_ESC); |
| 739 |
N_xlabel = agfindnodeattr(g, "xlabel"); |
| 740 |
N_showboxes = agfindnodeattr(g, "showboxes"); |
| 741 |
N_penwidth = agfindnodeattr(g, "penwidth"); |
| 742 |
N_ordering = agfindnodeattr(g, "ordering"); |
| 743 |
/* attribs for polygon shapes */ |
| 744 |
N_sides = agfindnodeattr(g, "sides"); |
| 745 |
N_peripheries = agfindnodeattr(g, "peripheries"); |
| 746 |
N_skew = agfindnodeattr(g, "skew"); |
| 747 |
N_orientation = agfindnodeattr(g, "orientation"); |
| 748 |
N_distortion = agfindnodeattr(g, "distortion"); |
| 749 |
N_fixed = agfindnodeattr(g, "fixedsize"); |
| 750 |
N_imagescale = agfindnodeattr(g, "imagescale"); |
| 751 |
N_imagepos = agfindnodeattr(g, "imagepos"); |
| 752 |
N_nojustify = agfindnodeattr(g, "nojustify"); |
| 753 |
N_layer = agfindnodeattr(g, "layer"); |
| 754 |
N_group = agfindnodeattr(g, "group"); |
| 755 |
N_comment = agfindnodeattr(g, "comment"); |
| 756 |
N_vertices = agfindnodeattr(g, "vertices"); |
| 757 |
N_z = agfindnodeattr(g, "z"); |
| 758 |
N_gradientangle = agfindnodeattr(g,"gradientangle"); |
| 759 |
|
| 760 |
/* initialize edges */ |
| 761 |
E_weight = agfindedgeattr(g, "weight"); |
| 762 |
E_color = agfindedgeattr(g, "color"); |
| 763 |
E_fillcolor = agfindedgeattr(g, "fillcolor"); |
| 764 |
E_fontsize = agfindedgeattr(g, "fontsize"); |
| 765 |
E_fontname = agfindedgeattr(g, "fontname"); |
| 766 |
E_fontcolor = agfindedgeattr(g, "fontcolor"); |
| 767 |
E_label = agfindedgeattr(g, "label"); |
| 768 |
E_xlabel = agfindedgeattr(g, "xlabel"); |
| 769 |
E_label_float = agfindedgeattr(g, "labelfloat"); |
| 770 |
E_dir = agfindedgeattr(g, "dir"); |
| 771 |
E_headlabel = agfindedgeattr(g, "headlabel"); |
| 772 |
E_taillabel = agfindedgeattr(g, "taillabel"); |
| 773 |
E_labelfontsize = agfindedgeattr(g, "labelfontsize"); |
| 774 |
E_labelfontname = agfindedgeattr(g, "labelfontname"); |
| 775 |
E_labelfontcolor = agfindedgeattr(g, "labelfontcolor"); |
| 776 |
E_labeldistance = agfindedgeattr(g, "labeldistance"); |
| 777 |
E_labelangle = agfindedgeattr(g, "labelangle"); |
| 778 |
E_minlen = agfindedgeattr(g, "minlen"); |
| 779 |
E_showboxes = agfindedgeattr(g, "showboxes"); |
| 780 |
E_style = agfindedgeattr(g, "style"); |
| 781 |
E_decorate = agfindedgeattr(g, "decorate"); |
| 782 |
E_arrowsz = agfindedgeattr(g, "arrowsize"); |
| 783 |
E_constr = agfindedgeattr(g, "constraint"); |
| 784 |
E_layer = agfindedgeattr(g, "layer"); |
| 785 |
E_comment = agfindedgeattr(g, "comment"); |
| 786 |
E_tailclip = agfindedgeattr(g, "tailclip"); |
| 787 |
E_headclip = agfindedgeattr(g, "headclip"); |
| 788 |
E_penwidth = agfindedgeattr(g, "penwidth"); |
| 789 |
|
| 790 |
/* background */ |
| 791 |
GD_drawing(g)->xdots = init_xdot (g); |
| 792 |
|
| 793 |
/* initialize id, if any */ |
| 794 |
if ((p = agget(g, "id")) && *p) |
| 795 |
GD_drawing(g)->id = strdup_and_subst_obj(p, g); |
| 796 |
} |
| 797 |
|
| 798 |
void graph_cleanup(graph_t *g) |
| 799 |
{ |
| 800 |
if (GD_drawing(g) && GD_drawing(g)->xdots) |
| 801 |
freeXDot(GD_drawing(g)->xdots); |
| 802 |
if (GD_drawing(g)) |
| 803 |
free (GD_drawing(g)->id); |
| 804 |
free(GD_drawing(g)); |
| 805 |
GD_drawing(g) = NULL; |
| 806 |
free_label(GD_label(g)); |
| 807 |
//FIX HERE , STILL SHALLOW |
| 808 |
//memset(&(g->u), 0, sizeof(Agraphinfo_t)); |
| 809 |
agclean(g, AGRAPH,"Agraphinfo_t"); |
| 810 |
} |
| 811 |
|
| 812 |
/// Given an internal charset value, return a canonical string representation. |
| 813 |
char* |
| 814 |
charsetToStr (int c) |
| 815 |
{ |
| 816 |
char* s; |
| 817 |
|
| 818 |
switch (c) { |
| 819 |
case CHAR_UTF8 : |
| 820 |
s = "UTF-8"; |
| 821 |
break; |
| 822 |
case CHAR_LATIN1 : |
| 823 |
s = "ISO-8859-1"; |
| 824 |
break; |
| 825 |
case CHAR_BIG5 : |
| 826 |
s = "BIG-5"; |
| 827 |
break; |
| 828 |
default : |
| 829 |
agerrorf("Unsupported charset value %d\n", c); |
| 830 |
s = "UTF-8"; |
| 831 |
break; |
| 832 |
} |
| 833 |
return s; |
| 834 |
} |
| 835 |
|
| 836 |
/// Set characteristics of graph label if it exists. |
| 837 |
void do_graph_label(graph_t * sg) |
| 838 |
{ |
| 839 |
char *str, *pos, *just; |
| 840 |
int pos_ix; |
| 841 |
|
| 842 |
/* it would be nice to allow multiple graph labels in the future */ |
| 843 |
if ((str = agget(sg, "label")) && *str != '\0') { |
| 844 |
char pos_flag; |
| 845 |
pointf dimen; |
| 846 |
|
| 847 |
GD_has_labels(sg->root) |= GRAPH_LABEL; |
| 848 |
|
| 849 |
GD_label(sg) = make_label(sg, str, aghtmlstr(str), false, |
| 850 |
late_double(sg, agfindgraphattr(sg, "fontsize"), |
| 851 |
DEFAULT_FONTSIZE, MIN_FONTSIZE), |
| 852 |
late_nnstring(sg, agfindgraphattr(sg, "fontname"), |
| 853 |
DEFAULT_FONTNAME), |
| 854 |
late_nnstring(sg, agfindgraphattr(sg, "fontcolor"), |
| 855 |
DEFAULT_COLOR)); |
| 856 |
|
| 857 |
/* set label position */ |
| 858 |
pos = agget(sg, "labelloc"); |
| 859 |
if (sg != agroot(sg)) { |
| 860 |
if (pos && pos[0] == 'b') |
| 861 |
pos_flag = LABEL_AT_BOTTOM; |
| 862 |
else |
| 863 |
pos_flag = LABEL_AT_TOP; |
| 864 |
} else { |
| 865 |
if (pos && pos[0] == 't') |
| 866 |
pos_flag = LABEL_AT_TOP; |
| 867 |
else |
| 868 |
pos_flag = LABEL_AT_BOTTOM; |
| 869 |
} |
| 870 |
just = agget(sg, "labeljust"); |
| 871 |
if (just) { |
| 872 |
if (just[0] == 'l') |
| 873 |
pos_flag |= LABEL_AT_LEFT; |
| 874 |
else if (just[0] == 'r') |
| 875 |
pos_flag |= LABEL_AT_RIGHT; |
| 876 |
} |
| 877 |
GD_label_pos(sg) = pos_flag; |
| 878 |
|
| 879 |
if (sg == agroot(sg)) |
| 880 |
return; |
| 881 |
|
| 882 |
/* Set border information for cluster labels to allow space |
| 883 |
*/ |
| 884 |
dimen = GD_label(sg)->dimen; |
| 885 |
PAD(dimen); |
| 886 |
if (!GD_flip(agroot(sg))) { |
| 887 |
if (GD_label_pos(sg) & LABEL_AT_TOP) |
| 888 |
pos_ix = TOP_IX; |
| 889 |
else |
| 890 |
pos_ix = BOTTOM_IX; |
| 891 |
GD_border(sg)[pos_ix] = dimen; |
| 892 |
} else { |
| 893 |
/* when rotated, the labels will be restored to TOP or BOTTOM */ |
| 894 |
if (GD_label_pos(sg) & LABEL_AT_TOP) |
| 895 |
pos_ix = RIGHT_IX; |
| 896 |
else |
| 897 |
pos_ix = LEFT_IX; |
| 898 |
GD_border(sg)[pos_ix].x = dimen.y; |
| 899 |
GD_border(sg)[pos_ix].y = dimen.x; |
| 900 |
} |
| 901 |
} |
| 902 |
} |