Graphviz 13.0.0~dev.20250424.1043
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 <gvpr/gprstate.h>
18#include <ast/error.h>
19#include <stdbool.h>
20#include <stdlib.h>
21#include <string.h>
22#include <util/alloc.h>
23
24static int name_used;
25
26bool validTVT(long long c) {
27 return TV_flat <= c && c <= TV_prepostrev;
28}
29
30void initGPRState(Gpr_t *state) {
31 state->tgtname = strdup("gvpr_result");
32}
33
35{
36 Gpr_t *state;
37
38 if (!(state = calloc(1, sizeof(Gpr_t)))) {
39 error(ERROR_ERROR, "Could not create gvpr state: out of memory");
40 return state;
41 }
42
43 state->tvt = TV_flat;
44 state->name_used = name_used;
45 state->tvroot = 0;
46 state->tvnext = 0;
47 state->tvedge = 0;
48 state->outFile = info->outFile;
49 state->argc = info->argc;
50 state->argv = info->argv;
51 state->errf = info->errf;
52 state->flags = info->flags;
53
54 return state;
55}
56
57
58static int
59bindingcmpf (const void *key, const void *ip)
60{
61 return strcmp (((const gvprbinding*)key)->name, ((const gvprbinding*)ip)->name);
62}
63
64/* findBinding:
65 */
67findBinding (Gpr_t* state, char* fname)
68{
69 if (!state->bindings) {
70 error(ERROR_ERROR,"call(\"%s\") failed: no bindings", fname);
71 return NULL;
72 }
73 if (!fname) {
74 error(ERROR_ERROR,"NULL function name for call()");
75 return NULL;
76 }
77
78 const gvprbinding key = {.name = fname};
79 gvprbinding *bp = bsearch(&key, state->bindings, state->n_bindings,
80 sizeof(gvprbinding), bindingcmpf);
81 if (!bp)
82 error(ERROR_ERROR, "No binding for \"%s\" in call()", fname);
83 return bp;
84}
85
86/* addBindings:
87 * Validate input, sort lexicographically, and attach
88 */
89void addBindings (Gpr_t* state, gvprbinding* bindings)
90{
91 size_t n = 0;
92 gvprbinding* bp = bindings;
93 gvprbinding* buf;
94 gvprbinding* bufp;
95
96 while (bp && bp->name) {
97 if (bp->fn) n++;
98 bp++;
99 }
100
101 if (n == 0) return;
102 bufp = buf = gv_calloc(n, sizeof(gvprbinding));
103 bp = bindings;
104 while (bp->name) {
105 if (bp->fn) {
106 *bufp = *bp;
107 bufp++;
108 }
109 bp++;
110 }
111 qsort (buf, n, sizeof(gvprbinding), bindingcmpf);
112
113 state->bindings = buf;
114 state->n_bindings = n;
115}
116
118{
119 if (!state) return;
120 name_used = state->name_used;
121 free(state->tgtname);
122 free (state->dp);
123 free (state);
124}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
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:59
void initGPRState(Gpr_t *state)
Definition gprstate.c:30
gvprbinding * findBinding(Gpr_t *state, char *fname)
Definition gprstate.c:67
bool validTVT(long long c)
Definition gprstate.c:26
void addBindings(Gpr_t *state, gvprbinding *bindings)
Definition gprstate.c:89
static int name_used
Definition gprstate.c:24
Gpr_t * openGPRState(gpr_info *info)
Definition gprstate.c:34
void closeGPRState(Gpr_t *state)
Definition gprstate.c:117
@ TV_flat
Definition gprstate.h:27
@ TV_prepostrev
Definition gprstate.h:31
node NULL
Definition grammar.y:163
table Syntax error
Definition htmlparse.y:294
char ** argv
Definition gprstate.h:52
Agedge_t * tvedge
Definition gprstate.h:49
char * tgtname
Definition gprstate.h:43
int flags
Definition gprstate.h:53
FILE * outFile
Definition gprstate.h:45
size_t n_bindings
Definition gprstate.h:55
Exdisc_t * dp
Definition gprstate.h:40
Agnode_t * tvroot
Definition gprstate.h:47
Agnode_t * tvnext
Definition gprstate.h:48
gvprbinding * bindings
Definition gprstate.h:54
Exerror_f errf
Definition gprstate.h:41
int name_used
Definition gprstate.h:50
int argc
Definition gprstate.h:51
trav_type tvt
Definition gprstate.h:46
char * name
Definition gvpr.h:59
gvpruserfn fn
Definition gvpr.h:60