| 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 |
|
| 26 |
static int name_used; |
| 27 |
|
| 28 |
bool validTVT(long long c) { |
| 29 |
return TV_flat <= c && c <= TV_prepostrev; |
| 30 |
} |
| 31 |
|
| 32 |
void initGPRState(Gpr_t *state) { |
| 33 |
state->tgtname = strdup("gvpr_result"); |
| 34 |
} |
| 35 |
|
| 36 |
Gpr_t *openGPRState(gpr_info* info) |
| 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 |
|
| 59 |
static int |
| 60 |
bindingcmpf (const void *key, const void *ip) |
| 61 |
{ |
| 62 |
return strcmp (((const gvprbinding*)key)->name, ((const gvprbinding*)ip)->name); |
| 63 |
} |
| 64 |
|
| 65 |
/* findBinding: |
| 66 |
*/ |
| 67 |
gvprbinding* |
| 68 |
findBinding (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 |
*/ |
| 90 |
void 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 |
|
| 118 |
void closeGPRState(Gpr_t* state) |
| 119 |
{ |
| 120 |
if (!state) return; |
| 121 |
name_used = state->name_used; |
| 122 |
free(state->tgtname); |
| 123 |
free (state->dp); |
| 124 |
free (state); |
| 125 |
} |