Debian Codegraph @

lib/cgraph/attr.c

raw
1 /**
2 * @file
3 * @brief implementation of dynamic attributes
4 * @ingroup cgraph_attr
5 * @ingroup cgraph_core
6 */
7 /*************************************************************************
8 * Copyright (c) 2011 AT&T Intellectual Property
9 * All rights reserved. This program and the accompanying materials
10 * are made available under the terms of the Eclipse Public License v1.0
11 * which accompanies this distribution, and is available at
12 * https://www.eclipse.org/legal/epl-v10.html
13 *
14 * Contributors: Details at https://graphviz.org
15 *************************************************************************/
16
17 #include "config.h"
18
19 #include <cgraph/cghdr.h>
20 #include <stdbool.h>
21 #include <stdlib.h>
22 #include <util/alloc.h>
23 #include <util/streq.h>
24 #include <util/unreachable.h>
25
26 /*
27 * dynamic attributes
28 */
29
30 /* to create a graph's data dictionary */
31
32 static void freesym(void *obj);
33
34 Dtdisc_t AgDataDictDisc = {
35 (int) offsetof(Agsym_t, name), /* use symbol name as key */
36 -1,
37 (int) offsetof(Agsym_t, link),
38 NULL,
39 freesym,
40 NULL,
41 };
42
43 static char DataDictName[] = "_AG_datadict";
44 static void init_all_attrs(Agraph_t * g);
45 static Agdesc_t ProtoDesc = {.directed = true, .no_loop = true,
46 .no_write = true};
47 static Agraph_t *ProtoGraph;
48
49 Agdatadict_t *agdatadict(Agraph_t *g, bool cflag) {
50 Agdatadict_t *rv = (Agdatadict_t *) aggetrec(g, DataDictName, 0);
51 if (rv || !cflag)
52 return rv;
53 init_all_attrs(g);
54 rv = (Agdatadict_t *) aggetrec(g, DataDictName, 0);
55 return rv;
56 }
57
58 static Dict_t *agdictof(Agraph_t * g, int kind)
59 {
60 Agdatadict_t *const dd = agdatadict(g, false);
61 if (dd)
62 switch (kind) {
63 case AGRAPH:
64 return dd->dict.g;
65 case AGNODE:
66 return dd->dict.n;
67 case AGINEDGE:
68 case AGOUTEDGE:
69 return dd->dict.e;
70 default:
71 agerrorf("agdictof: unknown kind %d\n", kind);
72 break;
73 }
74 return NULL;
75 }
76
77 /// @param is_html Is `value` an HTML-like string?
78 static Agsym_t *agnewsym(Agraph_t * g, const char *name, const char *value,
79 bool is_html, int id, int kind) {
80 Agsym_t *sym = gv_alloc(sizeof(Agsym_t));
81 sym->kind = (unsigned char) kind;
82 sym->name = agstrdup(g, name);
83 sym->defval = is_html ? agstrdup_html(g, value) : agstrdup(g, value);
84 sym->id = id;
85 sym->owner = g;
86 return sym;
87 }
88
89 static void agcopydict(Dict_t * src, Dict_t * dest, Agraph_t * g, int kind)
90 {
91 assert(dtsize(dest) == 0);
92 for (Agsym_t *sym = dtfirst(src); sym; sym = dtnext(src, sym)) {
93 const bool is_html = aghtmlstr(sym->defval);
94 Agsym_t *const newsym = agnewsym(g, sym->name, sym->defval, is_html, sym->id,
95 kind);
96 newsym->print = sym->print;
97 newsym->fixed = sym->fixed;
98 dtinsert(dest, newsym);
99 }
100 }
101
102 static Agdatadict_t *agmakedatadict(Agraph_t * g)
103 {
104 Agraph_t *par;
105
106 Agdatadict_t *const dd = agbindrec(g, DataDictName, sizeof(Agdatadict_t),
107 false);
108 dd->dict.n = agdtopen(&AgDataDictDisc, Dttree);
109 dd->dict.e = agdtopen(&AgDataDictDisc, Dttree);
110 dd->dict.g = agdtopen(&AgDataDictDisc, Dttree);
111 if ((par = agparent(g))) {
112 Agdatadict_t *const parent_dd = agdatadict(par, false);
113 assert(dd != parent_dd);
114 dtview(dd->dict.n, parent_dd->dict.n);
115 dtview(dd->dict.e, parent_dd->dict.e);
116 dtview(dd->dict.g, parent_dd->dict.g);
117 } else {
118 if (ProtoGraph && g != ProtoGraph) {
119 /* it's not ok to dtview here for several reasons. the proto
120 graph could change, and the sym indices don't match */
121 Agdatadict_t *const parent_dd = agdatadict(ProtoGraph, false);
122 agcopydict(parent_dd->dict.n, dd->dict.n, g, AGNODE);
123 agcopydict(parent_dd->dict.e, dd->dict.e, g, AGEDGE);
124 agcopydict(parent_dd->dict.g, dd->dict.g, g, AGRAPH);
125 }
126 }
127 return dd;
128 }
129
130 /* look up an attribute with possible viewpathing */
131 static Agsym_t *agdictsym(Dict_t * dict, char *name)
132 {
133 Agsym_t key = {.name = name};
134 return dtsearch(dict, &key);
135 }
136
137 /* look up attribute in local dictionary with no view pathing */
138 static Agsym_t *aglocaldictsym(Dict_t * dict, char *name)
139 {
140 Dict_t *const view = dtview(dict, NULL);
141 Agsym_t *const rv = agdictsym(dict, name);
142 dtview(dict, view);
143 return rv;
144 }
145
146 Agsym_t *agattrsym(void *obj, char *name)
147 {
148 Agattr_t *const data = agattrrec(obj);
149 if (data)
150 return agdictsym(data->dict, name);
151 return NULL;
152 }
153
154 /* to create a graph's, node's edge's string attributes */
155
156 const char AgDataRecName[] = "_AG_strdata";
157
158 static int topdictsize(Agobj_t * obj)
159 {
160 Dict_t *const d = agdictof(agroot(agraphof(obj)), AGTYPE(obj));
161 return d ? dtsize(d) : 0;
162 }
163
164 /* g can be either the enclosing graph, or ProtoGraph */
165 static Agrec_t *agmakeattrs(Agraph_t * context, void *obj)
166 {
167 Agattr_t *const rec = agbindrec(obj, AgDataRecName, sizeof(Agattr_t),
168 false);
169 Dict_t *const datadict = agdictof(context, AGTYPE(obj));
170 assert(datadict);
171 if (rec->dict == NULL) {
172 rec->dict = agdictof(agroot(context), AGTYPE(obj));
173 const int sz = topdictsize(obj);
174 rec->str = gv_calloc((size_t)sz, sizeof(char *));
175 /* doesn't call agxset() so no obj-modified callbacks occur */
176 for (Agsym_t *sym = dtfirst(datadict); sym; sym = dtnext(datadict, sym)) {
177 if (aghtmlstr(sym->defval)) {
178 rec->str[sym->id] = agstrdup_html(agraphof(obj), sym->defval);
179 } else {
180 rec->str[sym->id] = agstrdup(agraphof(obj), sym->defval);
181 }
182 }
183 } else {
184 assert(rec->dict == datadict);
185 }
186 return &rec->h;
187 }
188
189 static void freeattr(Agobj_t * obj, Agattr_t * attr)
190 {
191 int i, sz;
192 Agraph_t *g;
193
194 g = agraphof(obj);
195 sz = topdictsize(obj);
196 for (i = 0; i < sz; i++)
197 agstrfree(g, attr->str[i], aghtmlstr(attr->str[i]));
198 free(attr->str);
199 }
200
201 static void freesym(void *obj) {
202 Agsym_t *const sym = obj;
203 agstrfree(sym->owner, sym->name, false);
204 agstrfree(sym->owner, sym->defval, aghtmlstr(sym->defval));
205 free(sym);
206 }
207
208 Agattr_t *agattrrec(void *obj)
209 {
210 return (Agattr_t *)aggetrec(obj, AgDataRecName, 0);
211 }
212
213 static void addattr(Agraph_t *g, Agobj_t *obj, void *symbol) {
214 Agsym_t *const sym = symbol;
215 Agattr_t *attr = agattrrec(obj);
216 assert(attr != NULL);
217 attr->str = gv_recalloc(attr->str, (size_t)sym->id, (size_t)sym->id + 1,
218 sizeof(char *));
219 if (aghtmlstr(sym->defval)) {
220 attr->str[sym->id] = agstrdup_html(g, sym->defval);
221 } else {
222 attr->str[sym->id] = agstrdup(g, sym->defval);
223 }
224 }
225
226 static Agsym_t *getattr(Agraph_t *g, int kind, char *name) {
227 Agsym_t *rv = 0;
228 Dict_t *dict = agdictof(g, kind);
229 if (dict) {
230 rv = agdictsym(dict, name); // viewpath up to root
231 }
232 return rv;
233 }
234
235 static void unviewsubgraphsattr(Agraph_t *parent, char *name) {
236 Agraph_t *subg;
237 Agsym_t *psym, *lsym;
238 Dict_t *ldict;
239
240 psym = getattr(parent, AGRAPH, name);
241 if (!psym) {
242 return; // supposedly can't happen, see setattr()
243 }
244 for (subg = agfstsubg(parent); subg; subg = agnxtsubg(subg)) {
245 ldict = agdatadict(subg, true)->dict.g;
246 lsym = aglocaldictsym(ldict, name);
247 if (lsym) {
248 continue;
249 }
250 char *const value = agxget(subg, psym);
251 const bool is_html = aghtmlstr(value);
252 lsym = agnewsym(agroot(subg), name, value, is_html, psym->id, AGRAPH);
253 dtinsert(ldict, lsym);
254 }
255 }
256
257 static int agxset_(void *obj, Agsym_t *sym, const char *value, bool is_html);
258
259 /// @param is_html Is `value` an HTML-like string?
260 static Agsym_t *setattr(Agraph_t * g, int kind, char *name, const char *value,
261 bool is_html) {
262 Agsym_t *rv;
263
264 assert(value);
265 Agraph_t *root = agroot(g);
266 agdatadict(g, true); /* force initialization of string attributes */
267 Dict_t *ldict = agdictof(g, kind);
268 Agsym_t *lsym = aglocaldictsym(ldict, name);
269 if (lsym) { /* update old local definition */
270 if (g != root && streq(name, "layout"))
271 agwarningf("layout attribute is invalid except on the root graph\n");
272 if (kind == AGRAPH) {
273 unviewsubgraphsattr(g,name);
274 }
275 agstrfree(g, lsym->defval, aghtmlstr(lsym->defval));
276 lsym->defval = is_html ? agstrdup_html(g, value) : agstrdup(g, value);
277 rv = lsym;
278 } else {
279 Agsym_t *psym = agdictsym(ldict, name); // search with viewpath up to root
280 if (psym) { /* new local definition */
281 lsym = agnewsym(g, name, value, is_html, psym->id, kind);
282 dtinsert(ldict, lsym);
283 rv = lsym;
284 } else { /* new global definition */
285 Dict_t *rdict = agdictof(root, kind);
286 Agsym_t *rsym = agnewsym(root, name, value, is_html, dtsize(rdict), kind);
287 dtinsert(rdict, rsym);
288 switch (kind) {
289 case AGRAPH:
290 agapply(root, &root->base, addattr, rsym, true);
291 break;
292 case AGNODE:
293 for (Agnode_t *n = agfstnode(root); n; n = agnxtnode(root, n))
294 addattr(g, &n->base, rsym);
295 break;
296 case AGINEDGE:
297 case AGOUTEDGE:
298 for (Agnode_t *n = agfstnode(root); n; n = agnxtnode(root, n))
299 for (Agedge_t *e = agfstout(root, n); e; e = agnxtout(root, e))
300 addattr(g, &e->base, rsym);
301 break;
302 default:
303 UNREACHABLE();
304 }
305 rv = rsym;
306 }
307 }
308 if (rv && kind == AGRAPH)
309 agxset_(g, rv, value, is_html);
310 agmethod_upd(g, g, rv);
311 return rv;
312 }
313
314 /*
315 * create or update an existing attribute and return its descriptor.
316 * if the new value is NULL, this is only a search, no update.
317 * when a new attribute is created, existing graphs/nodes/edges
318 * receive its default value.
319 */
320 static Agsym_t *agattr_(Agraph_t *g, int kind, char *name, const char *value,
321 bool is_html) {
322 Agsym_t *rv;
323
324 if (g == NULL) {
325 if (ProtoGraph == NULL)
326 ProtoGraph = agopen(NULL, ProtoDesc, NULL);
327 g = ProtoGraph;
328 }
329 if (value)
330 rv = setattr(g, kind, name, value, is_html);
331 else
332 rv = getattr(g, kind, name);
333 return rv;
334 }
335
336 Agsym_t *agattr_text(Agraph_t *g, int kind, char *name, const char *value) {
337 return agattr_(g, kind, name, value, false);
338 }
339
340 Agsym_t *agattr_html(Agraph_t *g, int kind, char *name, const char *value) {
341 return agattr_(g, kind, name, value, true);
342 }
343
344 Agsym_t *agattr(Agraph_t *g, int kind, char *name, const char *value) {
345 if (g == NULL) {
346 if (ProtoGraph == NULL) {
347 ProtoGraph = agopen(NULL, ProtoDesc, NULL);
348 }
349 g = ProtoGraph;
350 }
351
352 // Is the value we were passed a previously created HTML-like string? We
353 // essentially want to ask `aghtmlstr(value)` but cannot safely because
354 // `value` may not have originated from `agstrdup`/`agstrdup_html`.
355 if (value != NULL) {
356 const char *const alias = agstrbind_html(g, value);
357 if (alias == value && aghtmlstr(alias)) {
358 return agattr_html(g, kind, name, value);
359 }
360 }
361
362 return agattr_text(g, kind, name, value);
363 }
364
365 Agsym_t *agnxtattr(Agraph_t * g, int kind, Agsym_t * attr)
366 {
367 Dict_t *d;
368 Agsym_t *rv;
369
370 if ((d = agdictof(g, kind))) {
371 if (attr)
372 rv = dtnext(d, attr);
373 else
374 rv = dtfirst(d);
375 } else
376 rv = 0;
377 return rv;
378 }
379
380 /* Create or delete attributes associated with an object */
381
382 void agraphattr_init(Agraph_t * g)
383 {
384 Agraph_t *context;
385
386 g->desc.has_attrs = true;
387 agmakedatadict(g);
388 if (!(context = agparent(g)))
389 context = g;
390 agmakeattrs(context, g);
391 }
392
393 int agraphattr_delete(Agraph_t * g)
394 {
395 Agdatadict_t *dd;
396 Agattr_t *attr;
397
398 if ((attr = agattrrec(g))) {
399 freeattr(&g->base, attr);
400 agdelrec(g, attr->h.name);
401 }
402
403 if ((dd = agdatadict(g, false))) {
404 if (agdtclose(g, dd->dict.n)) return 1;
405 if (agdtclose(g, dd->dict.e)) return 1;
406 if (agdtclose(g, dd->dict.g)) return 1;
407 agdelrec(g, dd->h.name);
408 }
409 return 0;
410 }
411
412 void agnodeattr_init(Agraph_t * g, Agnode_t * n)
413 {
414 Agattr_t *data;
415
416 data = agattrrec(n);
417 if (!data || !data->dict)
418 (void) agmakeattrs(g, n);
419 }
420
421 void agnodeattr_delete(Agnode_t * n)
422 {
423 Agattr_t *rec;
424
425 if ((rec = agattrrec(n))) {
426 freeattr(&n->base, rec);
427 agdelrec(n, AgDataRecName);
428 }
429 }
430
431 void agedgeattr_init(Agraph_t * g, Agedge_t * e)
432 {
433 Agattr_t *data;
434
435 data = agattrrec(e);
436 if (!data || !data->dict)
437 (void) agmakeattrs(g, e);
438 }
439
440 void agedgeattr_delete(Agedge_t * e)
441 {
442 Agattr_t *rec;
443
444 if ((rec = agattrrec(e))) {
445 freeattr(&e->base, rec);
446 agdelrec(e, AgDataRecName);
447 }
448 }
449
450 char *agget(void *obj, char *name)
451 {
452 Agsym_t *const sym = agattrsym(obj, name);
453 if (sym == NULL) {
454 return NULL; // note was "", but this provides more info
455 }
456 Agattr_t *const data = agattrrec(obj);
457 return data->str[sym->id];
458 }
459
460 char *agxget(void *obj, Agsym_t * sym)
461 {
462 Agattr_t *const data = agattrrec(obj);
463 assert(sym->id >= 0 && sym->id < topdictsize(obj));
464 return data->str[sym->id];
465 }
466
467 static int agset_(void *obj, char *name, const char *value, bool is_html) {
468 Agsym_t *const sym = agattrsym(obj, name);
469 if (sym == NULL)
470 return FAILURE;
471 if (is_html) {
472 return agxset_html(obj, sym, value);
473 }
474 return agxset_text(obj, sym, value);
475 }
476
477 int agset(void *obj, char *name, const char *value) {
478
479 // Is the value we were passed a previously created HTML-like string? We
480 // essentially want to ask `aghtmlstr(value)` but cannot safely because
481 // `value` may not have originated from `agstrdup`/`agstrdup_html`.
482 if (value != NULL) {
483 const char *const alias = agstrbind_html(agraphof(obj), value);
484 if (alias == value && aghtmlstr(alias)) {
485 return agset_html(obj, name, value);
486 }
487 }
488
489 return agset_text(obj, name, value);
490 }
491
492 int agset_text(void *obj, char *name, const char *value) {
493 return agset_(obj, name, value, false);
494 }
495
496 int agset_html(void *obj, char *name, const char *value) {
497 return agset_(obj, name, value, true);
498 }
499
500 static int agxset_(void *obj, Agsym_t *sym, const char *value, bool is_html) {
501 Agsym_t *lsym;
502
503 Agraph_t *g = agraphof(obj);
504 Agobj_t *hdr = obj;
505 Agattr_t *data = agattrrec(hdr);
506 assert(sym->id >= 0 && sym->id < topdictsize(obj));
507 agstrfree(g, data->str[sym->id], aghtmlstr(data->str[sym->id]));
508 data->str[sym->id] = is_html ? agstrdup_html(g, value) : agstrdup(g, value);
509 if (hdr->tag.objtype == AGRAPH) {
510 /* also update dict default */
511 Dict_t *dict = agdatadict(g, false)->dict.g;
512 if ((lsym = aglocaldictsym(dict, sym->name))) {
513 agstrfree(g, lsym->defval, aghtmlstr(lsym->defval));
514 lsym->defval = is_html ? agstrdup_html(g, value) : agstrdup(g, value);
515 } else {
516 lsym = agnewsym(g, sym->name, value, is_html, sym->id, AGTYPE(hdr));
517 dtinsert(dict, lsym);
518 }
519 }
520 agmethod_upd(g, obj, sym);
521 return SUCCESS;
522 }
523
524 int agxset(void *obj, Agsym_t *sym, const char *value) {
525
526 // Is the value we were passed a previously created HTML-like string? We
527 // essentially want to ask `aghtmlstr(value)` but cannot safely because
528 // `value` may not have originated from `agstrdup`/`agstrdup_html`.
529 if (value != NULL) {
530 const char *const alias = agstrbind_html(agraphof(obj), value);
531 if (alias == value && aghtmlstr(alias)) {
532 return agxset_html(obj, sym, value);
533 }
534 }
535
536 return agxset_text(obj, sym, value);
537 }
538
539 int agxset_text(void *obj, Agsym_t *sym, const char *value) {
540 return agxset_(obj, sym, value, false);
541 }
542
543 int agxset_html(void *obj, Agsym_t *sym, const char *value) {
544 return agxset_(obj, sym, value, true);
545 }
546
547 int agsafeset_text(void *obj, char *name, const char *value, const char *def) {
548 Agsym_t *a = agattr_text(agraphof(obj), AGTYPE(obj), name, NULL);
549 if (!a)
550 a = agattr_text(agraphof(obj), AGTYPE(obj), name, def);
551 return agxset(obj, a, value);
552 }
553
554 int agsafeset_html(void *obj, char *name, const char *value, const char *def) {
555 Agsym_t *a = agattr_html(agraphof(obj), AGTYPE(obj), name, NULL);
556 if (a == NULL) {
557 a = agattr_html(agraphof(obj), AGTYPE(obj), name, def);
558 }
559 return agxset_html(obj, a, value);
560 }
561
562 int agsafeset(void *obj, char *name, const char *value, const char *def) {
563
564 // find the graph containing the target object
565 Agraph_t *const graph = agraphof(obj);
566
567 // get the existing attribute, if there is one
568 Agsym_t *a = agattr_text(graph, AGTYPE(obj), name, NULL);
569
570 if (a == NULL) {
571 // Is the default we were passed a previously created HTML-like string? We
572 // essentially want to ask `aghtmlstr(def)` but cannot safely because `def`
573 // may not have originated from `agstrdup`/`agstrdup_html`.
574 bool is_def_html = false;
575 if (def != NULL) {
576 const char *const alias = agstrbind_html(graph, def);
577 if (alias == def && aghtmlstr(alias)) {
578 is_def_html = true;
579 }
580 }
581
582 if (is_def_html) {
583 a = agattr_html(graph, AGTYPE(obj), name, def);
584 } else {
585 a = agattr_text(graph, AGTYPE(obj), name, def);
586 }
587 }
588
589 // Is the value we were passed a previously created HTML-like string? We
590 // essentially want to ask `aghtmlstr(value)` but cannot safely because
591 // `value` may not have originated from `agstrdup`/`agstrdup_html`.
592 if (value != NULL) {
593 const char *const alias = agstrbind_html(graph, value);
594 if (alias == value && aghtmlstr(alias)) {
595 return agxset_html(obj, a, value);
596 }
597 }
598
599 return agxset(obj, a, value);
600 }
601
602 static void agraphattr_init_wrapper(Agraph_t *g, Agobj_t *ignored1,
603 void *ignored2) {
604 (void)ignored1;
605 (void)ignored2;
606
607 agraphattr_init(g);
608 }
609
610 /*
611 * attach attributes to the already created graph objs.
612 * presumably they were already initialized, so we don't invoke
613 * any of the old methods.
614 */
615 static void init_all_attrs(Agraph_t * g)
616 {
617 Agraph_t *root;
618 Agnode_t *n;
619 Agedge_t *e;
620
621 root = agroot(g);
622 agapply(root, &root->base, agraphattr_init_wrapper, NULL, true);
623 for (n = agfstnode(root); n; n = agnxtnode(root, n)) {
624 agnodeattr_init(g, n);
625 for (e = agfstout(root, n); e; e = agnxtout(root, e)) {
626 agedgeattr_init(g, e);
627 }
628 }
629 }
630
631 /* Assumes attributes have already been declared.
632 * Do not copy key attribute for edges, as this must be distinct.
633 * Returns non-zero on failure or if objects have different type.
634 */
635 int agcopyattr(void *oldobj, void *newobj)
636 {
637 Agraph_t *g;
638 Agsym_t *sym;
639 Agsym_t *newsym;
640 char* val;
641 int r = 1;
642
643 g = agraphof(oldobj);
644 if (AGTYPE(oldobj) != AGTYPE(newobj))
645 return 1;
646 sym = 0;
647 while ((sym = agnxtattr(g, AGTYPE(oldobj), sym))) {
648 newsym = agattrsym(newobj, sym->name);
649 if (!newsym)
650 return 1;
651 val = agxget(oldobj, sym);
652 if (aghtmlstr(val)) {
653 r = agxset_html(newobj, newsym, val);
654 } else {
655 r = agxset(newobj, newsym, val);
656 }
657 }
658 return r;
659 }