Graphviz 12.0.1~dev.20240716.0800
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 <cgraph/alloc.h>
20#include <stdbool.h>
21#include <stdlib.h>
22#include <string.h>
23
24static int name_used;
25
26bool validTVT(long long c) {
27 return TV_flat <= c && c <= TV_prepostrev;
28}
29
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 */
68{
70 gvprbinding* bp;
71
72 if (!state->bindings) {
73 error(ERROR_ERROR,"call(\"%s\") failed: no bindings", fname);
74 return NULL;
75 }
76 if (!fname) {
77 error(ERROR_ERROR,"NULL function name for call()");
78 return NULL;
79 }
80
81 key.name = fname;
82 bp = bsearch(&key, state->bindings, state->n_bindings, sizeof(gvprbinding), bindingcmpf);
83 if (!bp)
84 error(ERROR_ERROR, "No binding for \"%s\" in call()", fname);
85 return bp;
86}
87
88/* addBindings:
89 * Validate input, sort lexicographically, and attach
90 */
92{
93 size_t n = 0;
94 gvprbinding* bp = bindings;
95 gvprbinding* buf;
96 gvprbinding* bufp;
97
98 while (bp && bp->name) {
99 if (bp->fn) n++;
100 bp++;
101 }
102
103 if (n == 0) return;
104 bufp = buf = gv_calloc(n, sizeof(gvprbinding));
105 bp = bindings;
106 while (bp->name) {
107 if (bp->fn) {
108 *bufp = *bp;
109 bufp++;
110 }
111 bp++;
112 }
113 qsort (buf, n, sizeof(gvprbinding), bindingcmpf);
114
115 state->bindings = buf;
116 state->n_bindings = n;
117}
118
120{
121 if (!state) return;
122 name_used = state->name_used;
123 free(state->tgtname);
124 free (state->dp);
125 free (state);
126}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static char * fname
void error(int level, const char *s,...)
Definition error.c:83
#define ERROR_ERROR
Definition error.h:36
disc key
Definition exparse.y:214
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:91
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:119
@ TV_flat
Definition gprstate.h:26
@ TV_prepostrev
Definition gprstate.h:30
node NULL
Definition grammar.y:149
static lexstate_t state
Definition htmllex.c:61
char * name
Definition gvpr.h:59
gvpruserfn fn
Definition gvpr.h:60