Graphviz 14.1.3~dev.20260209.0759
Loading...
Searching...
No Matches
gprstate.c
Go to the documentation of this file.
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
12/*
13 * gpr state
14 *
15 */
16
17#include "config.h"
18
19#include <cgraph/cgraph.h>
20#include <gvpr/gprstate.h>
21#include <ast/error.h>
22#include <stdbool.h>
23#include <stdlib.h>
24#include <string.h>
25#include <util/alloc.h>
26#include <util/list.h>
27
28static int name_used;
29
30bool validTVT(long long c) {
31 return TV_flat <= c && c <= TV_prepostrev;
32}
33
34void initGPRState(Gpr_t *state) {
35 state->tgtname = strdup("gvpr_result");
36}
37
39{
40 Gpr_t *state;
41
42 if (!(state = calloc(1, sizeof(Gpr_t)))) {
43 error(ERROR_ERROR, "Could not create gvpr state: out of memory");
44 return state;
45 }
46
47 state->tvt = TV_flat;
48 state->name_used = name_used;
49 state->tvroot = 0;
50 state->tvnext = 0;
51 state->tvedge = 0;
52 state->outFile = info->outFile;
53 state->args = info->args;
54 state->errf = info->errf;
55 state->flags = info->flags;
56
57 return state;
58}
59
60
61static int
62bindingcmpf (const void *key, const void *ip)
63{
64 const gvprbinding *const k = key;
65 const gvprbinding *const i = ip;
66 return strcmp(k->name, i->name);
67}
68
69/* findBinding:
70 */
72findBinding (Gpr_t* state, char* fname)
73{
74 if (!state->bindings) {
75 error(ERROR_ERROR,"call(\"%s\") failed: no bindings", fname);
76 return NULL;
77 }
78 if (!fname) {
79 error(ERROR_ERROR,"NULL function name for call()");
80 return NULL;
81 }
82
83 const gvprbinding key = {.name = fname};
84 gvprbinding *bp = bsearch(&key, state->bindings, state->n_bindings,
85 sizeof(gvprbinding), bindingcmpf);
86 if (!bp)
87 error(ERROR_ERROR, "No binding for \"%s\" in call()", fname);
88 return bp;
89}
90
91/* addBindings:
92 * Validate input, sort lexicographically, and attach
93 */
94void addBindings (Gpr_t* state, gvprbinding* bindings)
95{
96 size_t n = 0;
97 gvprbinding* bp = bindings;
98 gvprbinding* buf;
99 gvprbinding* bufp;
100
101 while (bp && bp->name) {
102 if (bp->fn) n++;
103 bp++;
104 }
105
106 if (n == 0) return;
107 bufp = buf = gv_calloc(n, sizeof(gvprbinding));
108 bp = bindings;
109 while (bp->name) {
110 if (bp->fn) {
111 *bufp = *bp;
112 bufp++;
113 }
114 bp++;
115 }
116 qsort (buf, n, sizeof(gvprbinding), bindingcmpf);
117
118 state->bindings = buf;
119 state->n_bindings = n;
120}
121
123{
124 if (!state) return;
125
126 // we manually delete the managed graphs rather than setting
127 // `state->open_graphs.dtor` in `openGPRState` to allow list items to be
128 // removed in e.g. `getval` without closing the graph
129 for (size_t i = 0; i < LIST_SIZE(&state->open_graphs); ++i) {
130 (void)agclose(LIST_GET(&state->open_graphs, i));
131 }
132 LIST_FREE(&state->open_graphs);
133
134 name_used = state->name_used;
135 free(state->tgtname);
136 free (state->dp);
137 free (state);
138}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
abstract graph C library, Cgraph API
static char * fname
#define ERROR_ERROR
Definition error.h:36
void free(void *)
static int bindingcmpf(const void *key, const void *ip)
Definition gprstate.c:62
void initGPRState(Gpr_t *state)
Definition gprstate.c:34
gvprbinding * findBinding(Gpr_t *state, char *fname)
Definition gprstate.c:72
bool validTVT(long long c)
Definition gprstate.c:30
void addBindings(Gpr_t *state, gvprbinding *bindings)
Definition gprstate.c:94
static int name_used
Definition gprstate.c:28
Gpr_t * openGPRState(gpr_info *info)
Definition gprstate.c:38
void closeGPRState(Gpr_t *state)
Definition gprstate.c:122
@ TV_flat
Definition gprstate.h:32
@ TV_prepostrev
Definition gprstate.h:36
node NULL
Definition grammar.y:181
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:97
table Syntax error
Definition htmlparse.y:288
type-generic dynamically expanding list
#define LIST_SIZE(list)
Definition list.h:80
#define LIST_FREE(list)
Definition list.h:370
#define LIST_GET(list, index)
Definition list.h:155
Agedge_t * tvedge
Definition gprstate.h:54
char * tgtname
Definition gprstate.h:48
int flags
Definition gprstate.h:57
FILE * outFile
Definition gprstate.h:50
size_t n_bindings
Definition gprstate.h:59
Exdisc_t * dp
Definition gprstate.h:45
Agnode_t * tvroot
Definition gprstate.h:52
Agnode_t * tvnext
Definition gprstate.h:53
gvprbinding * bindings
Definition gprstate.h:58
Exerror_f errf
Definition gprstate.h:46
int name_used
Definition gprstate.h:55
strviews_t args
Definition gprstate.h:56
trav_type tvt
Definition gprstate.h:51
char * name
Definition gvpr.h:59
gvpruserfn fn
Definition gvpr.h:60