Graphviz 13.0.0~dev.20250210.0415
Loading...
Searching...
No Matches
exopen.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 * Glenn Fowler
13 * AT&T Research
14 *
15 * expression library
16 */
17
18#include <expr/exlib.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22
24static void free_exid(void *p) {
25 Exid_t *const exid = p;
26
27 // the `Exid_t` itself was allocated through the program’s `vm` allocator, so
28 // will be automatically deallocated, but we need to manually clean up the
29 // `local` set that is used for arrays
30 dtclose(exid->local);
31}
32
33/*
34 * allocate a new expression program environment
35 */
36
37Expr_t*
39{
40 Expr_t* program;
41 Exid_t* sym;
42
43 if (!(program = calloc(1, sizeof(Expr_t))))
44 return 0;
45 static Dtdisc_t symdisc = {.key = offsetof(Exid_t, name), .freef = free_exid};
46 if (!(program->symbols = dtopen(&symdisc, Dtset)) ||
47 !(program->vm = vmopen()) ||
48 !(program->ve = vmopen()))
49 {
50 exclose(program);
51 return 0;
52 }
53 program->id = "libexpr:expr";
54 program->disc = disc;
55 setcontext(program);
56 program->file[0] = stdin;
57 program->file[1] = stdout;
58 program->file[2] = stderr;
59 strcpy(program->main.name, "main");
60 program->main.lex = PROCEDURE;
61 program->main.index = PROCEDURE;
62 dtinsert(program->symbols, &program->main);
63 for (sym = exbuiltin; *sym->name; sym++)
64 dtinsert(program->symbols, sym);
65 if ((sym = disc->symbols))
66 for (; *sym->name; sym++)
67 dtinsert(program->symbols, sym);
68 return program;
69}
CDT_API Dtmethod_t * Dtset
set with unique elements
Definition dthash.c:277
#define dtinsert(d, o)
Definition cdt.h:185
CDT_API int dtclose(Dt_t *)
Definition dtclose.c:8
CDT_API Dt_t * dtopen(Dtdisc_t *, Dtmethod_t *)
Definition dtopen.c:9
Exid_t exbuiltin[]
Definition exdata.c:24
#define setcontext(p)
Definition exlib.h:127
static void free_exid(void *p)
destructor for Exid_t
Definition exopen.c:24
Expr_t * exopen(Exdisc_t *disc)
Definition exopen.c:38
#define PROCEDURE
Definition exparse.c:269
static Dtdisc_t disc
Definition exparse.y:207
void exclose(Expr_t *)
Definition expr.h:93
char name[EX_NAMELEN]
Definition expr.h:101
void * local
user defined local stuff
Definition expr.h:100
Definition expr.h:198
Dt_t * symbols
Definition expr.h:200
FILE * file[10]
Definition expr.h:201
Vmalloc_t * vm
Definition expr.h:202
const char * id
Definition expr.h:199
int key
Definition cdt.h:85
Vmalloc_t * vmopen(void)
Definition vmopen.c:18