| 1 |
/** |
| 2 |
* @file |
| 3 |
* @ingroup cgraph_core |
| 4 |
*/ |
| 5 |
/************************************************************************* |
| 6 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 7 |
* All rights reserved. This program and the accompanying materials |
| 8 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 9 |
* which accompanies this distribution, and is available at |
| 10 |
* https://www.eclipse.org/legal/epl-v10.html |
| 11 |
* |
| 12 |
* Contributors: Details at https://graphviz.org |
| 13 |
*************************************************************************/ |
| 14 |
|
| 15 |
%require "3.0" |
| 16 |
|
| 17 |
/* By default, Bison emits a parser using symbols prefixed with "yy". Graphviz |
| 18 |
* contains multiple Bison-generated parsers, so we alter this prefix to avoid |
| 19 |
* symbol clashes. |
| 20 |
*/ |
| 21 |
%define api.prefix {aag} |
| 22 |
|
| 23 |
/* Generate a reentrant parser with no global state. */ |
| 24 |
%define api.pure full |
| 25 |
/* aagparse() gets an argument defined by flex. */ |
| 26 |
%param { aagscan_t scanner } |
| 27 |
|
| 28 |
|
| 29 |
%code requires { |
| 30 |
#include <cghdr.h> |
| 31 |
#include <util/agxbuf.h> |
| 32 |
|
| 33 |
struct gstack_s; |
| 34 |
|
| 35 |
struct aagextra_s { |
| 36 |
/* Common */ |
| 37 |
Agdisc_t *Disc; /* discipline passed to agread or agconcat */ |
| 38 |
void *Ifile; |
| 39 |
Agraph_t *G; /* top level graph */ |
| 40 |
/* Parser */ |
| 41 |
int SubgraphDepth; |
| 42 |
struct gstack_s *S; |
| 43 |
/* Lexer */ |
| 44 |
int line_num; // = 1; |
| 45 |
int html_nest; /* nesting level for html strings */ |
| 46 |
const char *InputFile; |
| 47 |
agxbuf InputFileBuffer; |
| 48 |
int graphType; |
| 49 |
/* buffer for arbitrary length strings */ |
| 50 |
agxbuf Sbuf; |
| 51 |
}; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
%code provides { |
| 56 |
/* defined in/generated by scan.l */ |
| 57 |
void aagerror(aagscan_t, const char*); |
| 58 |
int aaglex(AAGSTYPE *, aagscan_t); |
| 59 |
} |
| 60 |
|
| 61 |
%{ |
| 62 |
|
| 63 |
#include <stdbool.h> |
| 64 |
#include <stdio.h> |
| 65 |
#include <cghdr.h> |
| 66 |
#include <stdlib.h> |
| 67 |
#include <util/alloc.h> |
| 68 |
#include <util/gv_math.h> |
| 69 |
#include <util/streq.h> |
| 70 |
#include <util/unreachable.h> |
| 71 |
|
| 72 |
static const char Key[] = "key"; |
| 73 |
|
| 74 |
typedef union s { /* possible items in generic list */ |
| 75 |
Agnode_t *n; |
| 76 |
Agraph_t *subg; |
| 77 |
Agedge_t *e; |
| 78 |
Agsym_t *asym; /* bound attribute */ |
| 79 |
char *name; /* unbound attribute */ |
| 80 |
struct item_s *list; /* list-of-lists (for edgestmt) */ |
| 81 |
} val_t; |
| 82 |
|
| 83 |
typedef struct item_s { /* generic list */ |
| 84 |
int tag; /* T_node, T_subgraph, T_edge, T_attr */ |
| 85 |
val_t u; /* primary element */ |
| 86 |
char *str; /* secondary value - port or attr value */ |
| 87 |
struct item_s *next; |
| 88 |
} item; |
| 89 |
|
| 90 |
typedef struct list_s { /* maintain head and tail ptrs for fast append */ |
| 91 |
item *first; |
| 92 |
item *last; |
| 93 |
} list_t; |
| 94 |
|
| 95 |
typedef struct gstack_s { |
| 96 |
Agraph_t *g; |
| 97 |
Agraph_t *subg; |
| 98 |
list_t nodelist,edgelist,attrlist; |
| 99 |
struct gstack_s *down; |
| 100 |
} gstack_t; |
| 101 |
|
| 102 |
/* functions */ |
| 103 |
static void appendnode(aagscan_t scanner, char *name, char *port, char *sport); |
| 104 |
static void attrstmt(aagscan_t scanner, int tkind, char *macroname); |
| 105 |
static void startgraph(aagscan_t scanner, char *name, bool directed, bool strict); |
| 106 |
static void getedgeitems(aagscan_t scanner); |
| 107 |
static void newedge(aagscan_t scanner, Agnode_t *t, char *tport, Agnode_t *h, char *hport, char *key); |
| 108 |
static void edgerhs(aagscan_t scanner, Agnode_t *n, char *tport, item *hlist, char *key); |
| 109 |
static void appendattr(aagscan_t scanner, char *name, char *value); |
| 110 |
static void bindattrs(aagextra_t *ctx, int kind); |
| 111 |
static void applyattrs(aagextra_t *ctx, void *obj); |
| 112 |
static void endgraph(aagscan_t scanner); |
| 113 |
static void endnode(aagscan_t scanner); |
| 114 |
static void endedge(aagscan_t scanner); |
| 115 |
static void freestack(aagscan_t scanner); |
| 116 |
static char* concat(aagscan_t scanner, char*, char*); |
| 117 |
static char* concatPort(Agraph_t *G, char*, char*); |
| 118 |
|
| 119 |
static void opensubg(aagscan_t scanner, char *name); |
| 120 |
static void closesubg(aagscan_t scanner); |
| 121 |
static void graph_error(aagscan_t scanner); |
| 122 |
|
| 123 |
%} |
| 124 |
|
| 125 |
%union { |
| 126 |
int i; |
| 127 |
char *str; |
| 128 |
struct Agnode_s *n; |
| 129 |
} |
| 130 |
|
| 131 |
%token <i> T_graph T_node T_edge T_digraph T_subgraph T_strict T_edgeop |
| 132 |
/* T_list, T_attr are internal tags, not really tokens */ |
| 133 |
%token T_list T_attr |
| 134 |
%token <str> T_atom T_qatom |
| 135 |
|
| 136 |
%type <i> optstrict graphtype rcompound attrtype |
| 137 |
%type <str> optsubghdr optgraphname optmacroname atom qatom |
| 138 |
|
| 139 |
|
| 140 |
%% |
| 141 |
|
| 142 |
graph : hdr body {freestack(scanner); endgraph(scanner);} |
| 143 |
| error {graph_error(scanner);} |
| 144 |
| /* empty */ |
| 145 |
; |
| 146 |
|
| 147 |
body : '{' optstmtlist '}' ; |
| 148 |
|
| 149 |
hdr : optstrict graphtype optgraphname {startgraph(scanner,$3,$2 != 0,$1 != 0);} |
| 150 |
; |
| 151 |
|
| 152 |
optgraphname: atom {$$=$1;} | /* empty */ {$$=0;} ; |
| 153 |
|
| 154 |
optstrict : T_strict {$$=1;} | /* empty */ {$$=0;} ; |
| 155 |
|
| 156 |
graphtype : T_graph {$$ = 0;} | T_digraph {$$ = 1;} ; |
| 157 |
|
| 158 |
optstmtlist : stmtlist | /* empty */ ; |
| 159 |
|
| 160 |
stmtlist : stmtlist stmt | stmt ; |
| 161 |
|
| 162 |
optsemi : ';' | ; |
| 163 |
|
| 164 |
stmt : attrstmt optsemi |
| 165 |
| compound optsemi |
| 166 |
; |
| 167 |
|
| 168 |
compound : simple rcompound optattr |
| 169 |
{if ($2) endedge(scanner); else endnode(scanner);} |
| 170 |
; |
| 171 |
|
| 172 |
simple : nodelist | subgraph ; |
| 173 |
|
| 174 |
rcompound : T_edgeop {getedgeitems(scanner);} simple {getedgeitems(scanner);} rcompound {$$ = 1;} |
| 175 |
| /* empty */ {$$ = 0;} |
| 176 |
; |
| 177 |
|
| 178 |
|
| 179 |
nodelist : node | nodelist ',' node ; |
| 180 |
|
| 181 |
node : atom {appendnode(scanner,$1,NULL,NULL);} |
| 182 |
| atom ':' atom {appendnode(scanner,$1,$3,NULL);} |
| 183 |
| atom ':' atom ':' atom {appendnode(scanner,$1,$3,$5);} |
| 184 |
; |
| 185 |
|
| 186 |
attrstmt : attrtype optmacroname attrlist {attrstmt(scanner,$1,$2);} |
| 187 |
| graphattrdefs {attrstmt(scanner,T_graph,NULL);} |
| 188 |
; |
| 189 |
|
| 190 |
attrtype : T_graph {$$ = T_graph;} |
| 191 |
| T_node {$$ = T_node;} |
| 192 |
| T_edge {$$ = T_edge;} |
| 193 |
; |
| 194 |
|
| 195 |
optmacroname : atom '=' {$$ = $1;} |
| 196 |
| /* empty */ {$$ = NULL; } |
| 197 |
; |
| 198 |
|
| 199 |
optattr : attrlist | /* empty */ ; |
| 200 |
|
| 201 |
attrlist : optattr '[' optattrdefs ']' ; |
| 202 |
|
| 203 |
optattrdefs : optattrdefs attrdefs |
| 204 |
| /* empty */ ; |
| 205 |
|
| 206 |
attrdefs : attrassignment optseparator |
| 207 |
; |
| 208 |
|
| 209 |
attrassignment : atom '=' atom {appendattr(scanner,$1,$3);} |
| 210 |
; |
| 211 |
|
| 212 |
graphattrdefs : attrassignment |
| 213 |
; |
| 214 |
|
| 215 |
subgraph : optsubghdr {opensubg(scanner,$1);} body {closesubg(scanner);} |
| 216 |
; |
| 217 |
|
| 218 |
optsubghdr : T_subgraph atom {$$=$2;} |
| 219 |
| T_subgraph {$$=NULL;} |
| 220 |
| /* empty */ {$$=NULL;} |
| 221 |
; |
| 222 |
|
| 223 |
optseparator : ';' | ',' | /*empty*/ ; |
| 224 |
|
| 225 |
atom : T_atom {$$ = $1;} |
| 226 |
| qatom {$$ = $1;} |
| 227 |
; |
| 228 |
|
| 229 |
qatom : T_qatom {$$ = $1;} |
| 230 |
| qatom '+' T_qatom {$$ = concat(scanner, $1,$3);} |
| 231 |
; |
| 232 |
%% |
| 233 |
|
| 234 |
static item *newitem(int tag, void *p0, char *p1) |
| 235 |
{ |
| 236 |
item *rv = gv_alloc(sizeof(item)); |
| 237 |
rv->tag = tag; |
| 238 |
rv->u.name = p0; |
| 239 |
rv->str = p1; |
| 240 |
return rv; |
| 241 |
} |
| 242 |
|
| 243 |
static item *cons_node(Agnode_t *n, char *port) |
| 244 |
{ return newitem(T_node,n,port); } |
| 245 |
|
| 246 |
static item *cons_attr(char *name, char *value) |
| 247 |
{ return newitem(T_atom,name,value); } |
| 248 |
|
| 249 |
static item *cons_list(item *list) |
| 250 |
{ return newitem(T_list,list,NULL); } |
| 251 |
|
| 252 |
static item *cons_subg(Agraph_t *subg) |
| 253 |
{ return newitem(T_subgraph,subg,NULL); } |
| 254 |
|
| 255 |
static gstack_t *push(gstack_t *s, Agraph_t *subg) { |
| 256 |
gstack_t *rv = gv_alloc(sizeof(gstack_t)); |
| 257 |
rv->down = s; |
| 258 |
rv->g = subg; |
| 259 |
return rv; |
| 260 |
} |
| 261 |
|
| 262 |
static gstack_t *pop(gstack_t *s) |
| 263 |
{ |
| 264 |
gstack_t *rv; |
| 265 |
rv = s->down; |
| 266 |
free(s); |
| 267 |
return rv; |
| 268 |
} |
| 269 |
|
| 270 |
static void delete_items(Agraph_t *G, item *ilist) |
| 271 |
{ |
| 272 |
item *p,*pn; |
| 273 |
|
| 274 |
for (p = ilist; p; p = pn) { |
| 275 |
pn = p->next; |
| 276 |
if (p->tag == T_list) delete_items(G, p->u.list); |
| 277 |
if (p->tag == T_atom) agstrfree(G, p->str, aghtmlstr(p->str)); |
| 278 |
free(p); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
static void deletelist(Agraph_t *G, list_t *list) |
| 283 |
{ |
| 284 |
delete_items(G,list->first); |
| 285 |
list->first = list->last = NULL; |
| 286 |
} |
| 287 |
|
| 288 |
static void listapp(list_t *list, item *v) |
| 289 |
{ |
| 290 |
if (list->last) list->last->next = v; |
| 291 |
list->last = v; |
| 292 |
if (list->first == NULL) list->first = v; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
/* attrs */ |
| 297 |
static void appendattr(aagscan_t scanner, char *name, char *value) |
| 298 |
{ |
| 299 |
item *v; |
| 300 |
aagextra_t *ctx = aagget_extra(scanner); |
| 301 |
|
| 302 |
assert(value != NULL); |
| 303 |
v = cons_attr(name,value); |
| 304 |
listapp(&ctx->S->attrlist, v); |
| 305 |
} |
| 306 |
|
| 307 |
static void bindattrs(aagextra_t *ctx, int kind) |
| 308 |
{ |
| 309 |
item *aptr; |
| 310 |
char *name; |
| 311 |
|
| 312 |
for (aptr = ctx->S->attrlist.first; aptr; aptr = aptr->next) { |
| 313 |
assert(aptr->tag == T_atom); /* signifies unbound attr */ |
| 314 |
name = aptr->u.name; |
| 315 |
if (kind == AGEDGE && streq(name,Key)) continue; |
| 316 |
if ((aptr->u.asym = agattr_text(ctx->S->g,kind,name,NULL)) == NULL) |
| 317 |
aptr->u.asym = agattr_text(ctx->S->g,kind,name,""); |
| 318 |
aptr->tag = T_attr; /* signifies bound attr */ |
| 319 |
agstrfree(ctx->G, name, false); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/* attach node/edge specific attributes */ |
| 324 |
static void applyattrs(aagextra_t *ctx, void *obj) |
| 325 |
{ |
| 326 |
item *aptr; |
| 327 |
|
| 328 |
for (aptr = ctx->S->attrlist.first; aptr; aptr = aptr->next) { |
| 329 |
if (aptr->tag == T_attr) { |
| 330 |
if (aptr->u.asym) { |
| 331 |
if (aghtmlstr(aptr->str)) { |
| 332 |
agxset_html(obj, aptr->u.asym, aptr->str); |
| 333 |
} else { |
| 334 |
agxset(obj, aptr->u.asym, aptr->str); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
else { |
| 339 |
assert(AGTYPE(obj) == AGINEDGE || AGTYPE(obj) == AGOUTEDGE); |
| 340 |
assert(aptr->tag == T_atom); |
| 341 |
assert(streq(aptr->u.name,Key)); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
static void nomacros(void) |
| 347 |
{ |
| 348 |
agwarningf("attribute macros not implemented"); |
| 349 |
} |
| 350 |
|
| 351 |
/* attrstmt: |
| 352 |
* First argument is always attrtype, so switch covers all cases. |
| 353 |
* This function is used to handle default attribute value assignment. |
| 354 |
*/ |
| 355 |
static void attrstmt(aagscan_t scanner, int tkind, char *macroname) |
| 356 |
{ |
| 357 |
item *aptr; |
| 358 |
int kind = 0; |
| 359 |
aagextra_t *ctx = aagget_extra(scanner); |
| 360 |
Agsym_t* sym; |
| 361 |
Agraph_t *G = ctx->G; |
| 362 |
gstack_t *S = ctx->S; |
| 363 |
|
| 364 |
/* creating a macro def */ |
| 365 |
if (macroname) nomacros(); |
| 366 |
/* invoking a macro def */ |
| 367 |
for (aptr = S->attrlist.first; aptr; aptr = aptr->next) |
| 368 |
if (aptr->str == NULL) nomacros(); |
| 369 |
|
| 370 |
switch(tkind) { |
| 371 |
case T_graph: kind = AGRAPH; break; |
| 372 |
case T_node: kind = AGNODE; break; |
| 373 |
case T_edge: kind = AGEDGE; break; |
| 374 |
default: UNREACHABLE(); |
| 375 |
} |
| 376 |
bindattrs(ctx, kind); /* set up defaults for new attributes */ |
| 377 |
for (aptr = S->attrlist.first; aptr; aptr = aptr->next) { |
| 378 |
/* If the tag is still T_atom, aptr->u.asym has not been set */ |
| 379 |
if (aptr->tag == T_atom) continue; |
| 380 |
if (!aptr->u.asym->fixed || S->g != G) { |
| 381 |
if (aghtmlstr(aptr->str)) { |
| 382 |
sym = agattr_html(S->g, kind, aptr->u.asym->name, aptr->str); |
| 383 |
} else { |
| 384 |
sym = agattr_text(S->g, kind, aptr->u.asym->name, aptr->str); |
| 385 |
} |
| 386 |
} else |
| 387 |
sym = aptr->u.asym; |
| 388 |
if (S->g == G) |
| 389 |
sym->print = true; |
| 390 |
} |
| 391 |
deletelist(G, &S->attrlist); |
| 392 |
} |
| 393 |
|
| 394 |
/* nodes */ |
| 395 |
|
| 396 |
static void appendnode(aagscan_t scanner, char *name, char *port, char *sport) |
| 397 |
{ |
| 398 |
item *elt; |
| 399 |
aagextra_t *ctx = aagget_extra(scanner); |
| 400 |
Agraph_t *G = ctx->G; |
| 401 |
gstack_t *S = ctx->S; |
| 402 |
|
| 403 |
if (sport) { |
| 404 |
port = concatPort (G, port, sport); |
| 405 |
} |
| 406 |
elt = cons_node(agnode(S->g, name, 1), port); |
| 407 |
listapp(&S->nodelist, elt); |
| 408 |
agstrfree(G, name, false); |
| 409 |
} |
| 410 |
|
| 411 |
/* apply current optional attrs to nodelist and clean up lists */ |
| 412 |
/* what's bad is that this could also be endsubg. also, you can't |
| 413 |
clean up S->subg in closesubg() because S->subg might be needed |
| 414 |
to construct edges. these are the sort of notes you write to yourself |
| 415 |
in the future. */ |
| 416 |
static void endnode(aagscan_t scanner) |
| 417 |
{ |
| 418 |
item *ptr; |
| 419 |
aagextra_t *ctx = aagget_extra(scanner); |
| 420 |
Agraph_t *G = ctx->G; |
| 421 |
gstack_t *S = ctx->S; |
| 422 |
|
| 423 |
bindattrs(ctx, AGNODE); |
| 424 |
for (ptr = S->nodelist.first; ptr; ptr = ptr->next) |
| 425 |
applyattrs(ctx, ptr->u.n); |
| 426 |
deletelist(G, &S->nodelist); |
| 427 |
deletelist(G, &S->attrlist); |
| 428 |
deletelist(G, &S->edgelist); |
| 429 |
S->subg = 0; /* notice a pattern here? :-( */ |
| 430 |
} |
| 431 |
|
| 432 |
/* edges - store up node/subg lists until optional edge key can be seen */ |
| 433 |
|
| 434 |
static void getedgeitems(aagscan_t scanner) |
| 435 |
{ |
| 436 |
aagextra_t *ctx = aagget_extra(scanner);; |
| 437 |
gstack_t *S = ctx->S; |
| 438 |
item *v = 0; |
| 439 |
|
| 440 |
if (S->nodelist.first) { |
| 441 |
v = cons_list(S->nodelist.first); |
| 442 |
S->nodelist.first = S->nodelist.last = NULL; |
| 443 |
} |
| 444 |
else {if (S->subg) v = cons_subg(S->subg); S->subg = 0;} |
| 445 |
/* else nil append */ |
| 446 |
if (v) listapp(&S->edgelist, v); |
| 447 |
} |
| 448 |
|
| 449 |
static void endedge(aagscan_t scanner) |
| 450 |
{ |
| 451 |
char *key; |
| 452 |
item *aptr,*tptr,*p; |
| 453 |
|
| 454 |
Agnode_t *t; |
| 455 |
Agraph_t *subg; |
| 456 |
aagextra_t *ctx = aagget_extra(scanner); |
| 457 |
Agraph_t *G = ctx->G; |
| 458 |
|
| 459 |
bindattrs(ctx, AGEDGE); |
| 460 |
|
| 461 |
/* look for "key" pseudo-attribute */ |
| 462 |
key = NULL; |
| 463 |
for (aptr = ctx->S->attrlist.first; aptr; aptr = aptr->next) { |
| 464 |
if (aptr->tag == T_atom && streq(aptr->u.name,Key)) |
| 465 |
key = aptr->str; |
| 466 |
} |
| 467 |
|
| 468 |
/* can make edges with node lists or subgraphs */ |
| 469 |
for (p = ctx->S->edgelist.first; p->next; p = p->next) { |
| 470 |
if (p->tag == T_subgraph) { |
| 471 |
subg = p->u.subg; |
| 472 |
for (t = agfstnode(subg); t; t = agnxtnode(subg,t)) |
| 473 |
edgerhs(scanner,agsubnode(ctx->S->g, t, 0), NULL, p->next, key); |
| 474 |
} |
| 475 |
else { |
| 476 |
for (tptr = p->u.list; tptr; tptr = tptr->next) |
| 477 |
edgerhs(scanner,tptr->u.n,tptr->str,p->next,key); |
| 478 |
} |
| 479 |
} |
| 480 |
deletelist(G, &ctx->S->nodelist); |
| 481 |
deletelist(G, &ctx->S->edgelist); |
| 482 |
deletelist(G, &ctx->S->attrlist); |
| 483 |
ctx->S->subg = 0; |
| 484 |
} |
| 485 |
|
| 486 |
/* concat: |
| 487 |
*/ |
| 488 |
static char* |
| 489 |
concat (aagscan_t scanner, char* s1, char* s2) |
| 490 |
{ |
| 491 |
agxbuf buf = {0}; |
| 492 |
Agraph_t *G = aagget_extra(scanner)->G; |
| 493 |
|
| 494 |
agxbprint(&buf, "%s%s", s1, s2); |
| 495 |
char *const s = agstrdup(G, agxbuse(&buf)); |
| 496 |
agstrfree(G, s1, false); |
| 497 |
agstrfree(G, s2, false); |
| 498 |
agxbfree(&buf); |
| 499 |
return s; |
| 500 |
} |
| 501 |
|
| 502 |
static char* |
| 503 |
concatPort (Agraph_t *G, char* s1, char* s2) |
| 504 |
{ |
| 505 |
agxbuf buf = {0}; |
| 506 |
|
| 507 |
agxbprint(&buf, "%s:%s", s1, s2); |
| 508 |
char *s = agstrdup(G, agxbuse(&buf)); |
| 509 |
agstrfree(G, s1, false); |
| 510 |
agstrfree(G, s2, false); |
| 511 |
agxbfree(&buf); |
| 512 |
return s; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
static void edgerhs(aagscan_t scanner, Agnode_t *tail, char *tport, item *hlist, char *key) |
| 517 |
{ |
| 518 |
Agnode_t *head; |
| 519 |
Agraph_t *subg; |
| 520 |
item *hptr; |
| 521 |
aagextra_t *ctx = aagget_extra(scanner); |
| 522 |
|
| 523 |
if (hlist->tag == T_subgraph) { |
| 524 |
subg = hlist->u.subg; |
| 525 |
for (head = agfstnode(subg); head; head = agnxtnode(subg,head)) |
| 526 |
newedge(scanner, tail, tport, agsubnode(ctx->S->g, head, 0), NULL, key); |
| 527 |
} |
| 528 |
else { |
| 529 |
for (hptr = hlist->u.list; hptr; hptr = hptr->next) |
| 530 |
newedge(scanner, tail, tport, agsubnode(ctx->S->g, hptr->u.n, 0), hptr->str, key); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
static void mkport(aagscan_t scanner, Agedge_t *e, char *name, char *val) |
| 535 |
{ |
| 536 |
Agsym_t *attr; |
| 537 |
aagextra_t *ctx = aagget_extra(scanner); |
| 538 |
|
| 539 |
if (val) { |
| 540 |
if ((attr = agattr_text(ctx->S->g,AGEDGE,name,NULL)) == NULL) |
| 541 |
attr = agattr_text(ctx->S->g,AGEDGE,name,""); |
| 542 |
agxset(e,attr,val); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
static void newedge(aagscan_t scanner, Agnode_t *t, char *tport, Agnode_t *h, char *hport, char *key) |
| 547 |
{ |
| 548 |
Agedge_t *e; |
| 549 |
aagextra_t *ctx = aagget_extra(scanner); |
| 550 |
|
| 551 |
e = agedge(ctx->S->g, t, h, key, 1); |
| 552 |
if (e) { /* can fail if graph is strict and t==h */ |
| 553 |
char *tp = tport; |
| 554 |
char *hp = hport; |
| 555 |
if (agtail(e) != aghead(e) && aghead(e) == t) { |
| 556 |
/* could happen with an undirected edge */ |
| 557 |
SWAP(&tp, &hp); |
| 558 |
} |
| 559 |
mkport(scanner, e,TAILPORT_ID,tp); |
| 560 |
mkport(scanner, e,HEADPORT_ID,hp); |
| 561 |
applyattrs(ctx, e); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/* graphs and subgraphs */ |
| 566 |
|
| 567 |
|
| 568 |
static void startgraph(aagscan_t scanner, char *name, bool directed, bool strict) |
| 569 |
{ |
| 570 |
aagextra_t *ctx = aagget_extra(scanner); |
| 571 |
if (ctx->G == NULL) { |
| 572 |
ctx->SubgraphDepth = 0; |
| 573 |
Agdesc_t req = {.directed = directed, .strict = strict, .maingraph = true}; |
| 574 |
ctx->G = agopen(name,req,ctx->Disc); |
| 575 |
} |
| 576 |
ctx->S = push(ctx->S,ctx->G); |
| 577 |
agstrfree(NULL, name, false); |
| 578 |
} |
| 579 |
|
| 580 |
static void endgraph(aagscan_t scanner) |
| 581 |
{ |
| 582 |
aglexeof(scanner); |
| 583 |
aginternalmapclearlocalnames(aagget_extra(scanner)->G); |
| 584 |
} |
| 585 |
|
| 586 |
static void opensubg(aagscan_t scanner, char *name) |
| 587 |
{ |
| 588 |
aagextra_t *ctx = aagget_extra(scanner); |
| 589 |
|
| 590 |
if (++ctx->SubgraphDepth >= YYMAXDEPTH/2) { |
| 591 |
agerrorf("subgraphs nested more than %d deep", YYMAXDEPTH); |
| 592 |
} |
| 593 |
ctx->S = push(ctx->S, agsubg(ctx->S->g, name, 1)); |
| 594 |
agstrfree(ctx->G, name, false); |
| 595 |
} |
| 596 |
|
| 597 |
static void closesubg(aagscan_t scanner) |
| 598 |
{ |
| 599 |
aagextra_t *ctx = aagget_extra(scanner); |
| 600 |
Agraph_t *subg = ctx->S->g; |
| 601 |
|
| 602 |
--ctx->SubgraphDepth; |
| 603 |
ctx->S = pop(ctx->S); |
| 604 |
ctx->S->subg = subg; |
| 605 |
assert(subg); |
| 606 |
} |
| 607 |
|
| 608 |
static void freestack(aagscan_t scanner) |
| 609 |
{ |
| 610 |
aagextra_t *ctx = aagget_extra(scanner); |
| 611 |
while (ctx->S) { |
| 612 |
deletelist(ctx->G, &ctx->S->nodelist); |
| 613 |
deletelist(ctx->G, &ctx->S->attrlist); |
| 614 |
deletelist(ctx->G, &ctx->S->edgelist); |
| 615 |
ctx->S = pop(ctx->S); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
static void graph_error(aagscan_t scanner) |
| 620 |
{ |
| 621 |
aagextra_t *ctx = aagget_extra(scanner); |
| 622 |
if (ctx->G) { |
| 623 |
freestack(scanner); |
| 624 |
endgraph(scanner); |
| 625 |
agclose(ctx->G); |
| 626 |
ctx->G = NULL; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
Agraph_t *agconcat(Agraph_t *g, const char *filename, void *chan, |
| 631 |
Agdisc_t *disc) { |
| 632 |
aagscan_t scanner = NULL; |
| 633 |
aagextra_t extra = { |
| 634 |
.Disc = disc ? disc : &AgDefaultDisc, |
| 635 |
.Ifile = chan, |
| 636 |
.G = g, |
| 637 |
.line_num = 1, |
| 638 |
.InputFile = filename, |
| 639 |
}; |
| 640 |
if (aaglex_init_extra(&extra, &scanner)) { |
| 641 |
return NULL; |
| 642 |
} |
| 643 |
aagset_in(chan, scanner); |
| 644 |
aagparse(scanner); |
| 645 |
if (extra.G == NULL) aglexbad(scanner); |
| 646 |
aaglex_destroy(scanner); |
| 647 |
agxbfree(&extra.InputFileBuffer); |
| 648 |
agxbfree(&extra.Sbuf); |
| 649 |
return extra.G; |
| 650 |
} |
| 651 |
|
| 652 |
Agraph_t *agread(void *fp, Agdisc_t *disc) { |
| 653 |
return agconcat(NULL, NULL, fp, disc); |
| 654 |
} |
| 655 |
|