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