Graphviz 13.0.0~dev.20250511.0440
Loading...
Searching...
No Matches
tcldot.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#include "tcldot.h"
12#include "../tcl-compat.h"
13#include <cgraph/rdr.h>
14#include <stdlib.h>
15#include <string.h>
16#include <tcl.h>
17#include <util/alloc.h>
18#include <util/streq.h>
19
20static int dotnew_internal(ClientData clientData, Tcl_Interp *interp, int argc,
21 char *argv[]) {
22 ictx_t *ictx = (ictx_t *)clientData;
23 Agraph_t *g;
24 int i;
25 Agdesc_t kind;
26
27 if (argc < 2) {
28 Tcl_AppendResult(
29 interp, "wrong # args: should be \"", argv[0],
30 " graphtype ?graphname? ?attributename attributevalue? ?...?\"", NULL);
31 return TCL_ERROR;
32 }
33 if (streq("digraph", argv[1])) {
34 kind = Agdirected;
35 } else if (streq("digraphstrict", argv[1])) {
36 kind = Agstrictdirected;
37 } else if (streq("graph", argv[1])) {
38 kind = Agundirected;
39 } else if (streq("graphstrict", argv[1])) {
40 kind = Agstrictundirected;
41 } else {
42 Tcl_AppendResult(interp, "bad graphtype \"", argv[1], "\": must be one of:",
43 "\n\tdigraph, digraphstrict, graph, graphstrict.", NULL);
44 return TCL_ERROR;
45 }
46 if (argc % 2) {
47 /* if odd number of args then argv[2] is name */
48 g = agopen(argv[2], kind, &ictx->mydisc);
49 i = 3;
50 } else {
51 /* else use handle as name */
52#if TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION >= 4
53 char *name = gv_strdup(Tcl_GetStringResult(interp));
54 g = agopen(name, kind, &ictx->mydisc);
55 free(name);
56#else
57 g = agopen(Tcl_GetStringResult(interp), kind, &ictx->mydisc);
58#endif
59 i = 2;
60 }
61 if (!g) {
62 Tcl_AppendResult(interp, "\nFailure to open graph.", NULL);
63 return TCL_ERROR;
64 }
65 setgraphattributes(g, &argv[i], (Tcl_Size)(argc - i));
66 Tcl_AppendResult(interp, obj2cmd(g), NULL);
67
68 return TCL_OK;
69}
70
71static int dotnew(ClientData clientData, Tcl_Interp *interp, int argc,
72 const char *argv[]) {
73 char **argv_copy = tcldot_argv_dup((Tcl_Size)argc, argv);
74 int rc = dotnew_internal(clientData, interp, argc, argv_copy);
75 tcldot_argv_free((Tcl_Size)argc, argv_copy);
76 return rc;
77}
78
79static int dotread(ClientData clientData, Tcl_Interp *interp, int argc,
80 const char *argv[]) {
81 Agraph_t *g;
82 Tcl_Channel channel;
83 int mode;
84 ictx_t *ictx = (ictx_t *)clientData;
85
86 ictx->myioDisc.afread =
87 myiodisc_afread; /* replace afread to use Tcl Channels */
88
89 if (argc < 2) {
90 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
91 " fileHandle\"", NULL);
92 return TCL_ERROR;
93 }
94 channel = Tcl_GetChannel(interp, argv[1], &mode);
95 if (channel == NULL || !(mode & TCL_READABLE)) {
96 Tcl_AppendResult(interp, "\nChannel \"", argv[1], "\"", "is unreadable.",
97 NULL);
98 return TCL_ERROR;
99 }
100 /*
101 * read a graph from the channel, the channel is left open
102 * ready to read the first line after the last line of
103 * a properly parsed graph. If the graph doesn't parse
104 * during reading then the channel will be left at EOF
105 */
106 g = agread((FILE *)channel, (Agdisc_t *)clientData);
107 if (!g) {
108 Tcl_AppendResult(interp, "\nFailure to read graph \"", argv[1], "\"", NULL);
109 if (agerrors()) {
110 Tcl_AppendResult(interp, " because of syntax errors.", NULL);
111 }
112 return TCL_ERROR;
113 }
114 if (agerrors()) {
115 Tcl_AppendResult(interp, "\nSyntax errors in file \"", argv[1], " \"",
116 NULL);
117 return TCL_ERROR;
118 }
119 Tcl_AppendResult(interp, obj2cmd(g), NULL);
120 return TCL_OK;
121}
122
123static int dotstring(ClientData clientData, Tcl_Interp *interp, int argc,
124 const char *argv[]) {
125 Agraph_t *g;
126 ictx_t *ictx = (ictx_t *)clientData;
127 rdr_t rdr;
128
129 if (argc < 2) {
130 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " string\"",
131 NULL);
132 return TCL_ERROR;
133 }
134
135 ictx->myioDisc.afread =
136 myiodisc_memiofread; /* replace afread to use memory range */
137 rdr.data = argv[1];
138 rdr.len = strlen(rdr.data);
139 rdr.cur = 0;
140
141 /* agmemread() is broken for our use because it replaces the id disc */
142 g = agread(&rdr, (Agdisc_t *)clientData);
143 if (!g) {
144 Tcl_AppendResult(interp, "\nFailure to read string \"", argv[1], "\"",
145 NULL);
146 if (agerrors()) {
147 Tcl_AppendResult(interp, " because of syntax errors.", NULL);
148 }
149 return TCL_ERROR;
150 }
151 if (agerrors()) {
152 Tcl_AppendResult(interp, "\nSyntax errors in string \"", argv[1], " \"",
153 NULL);
154 return TCL_ERROR;
155 }
156 Tcl_AppendResult(interp, obj2cmd(g), NULL);
157 return TCL_OK;
158}
159
160int Tcldot_Init(Tcl_Interp *interp);
161int Tcldot_Init(Tcl_Interp *interp) {
162 ictx_t *ictx = calloc(1, sizeof(ictx_t));
163 if (!ictx)
164 return TCL_ERROR;
165
166 ictx->interp = interp;
167 /* build disciplines dynamically so we can selectively replace functions */
168
169 ictx->myioDisc.afread =
170 NULL; /* set in dotread() or dotstring() according to need */
171 ictx->myioDisc.putstr = AgIoDisc.putstr; /* no change */
172 ictx->myioDisc.flush = AgIoDisc.flush; /* no change */
173
174 ictx->mydisc.id = &myiddisc; /* complete replacement */
175 ictx->mydisc.io = &ictx->myioDisc; /* change parts */
176
177 ictx->ctr = 1; /* init to first odd number, increment by 2 */
178
179#ifdef USE_TCL_STUBS
180 if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
181 free(ictx);
182 return TCL_ERROR;
183 }
184#else
185 if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 0) == NULL) {
186 free(ictx);
187 return TCL_ERROR;
188 }
189#endif
190 // inter-release Graphviz versions have a number including '~dev.' that does
191 // not comply with TCL version number rules, so replace this with 'b'
192 char adjusted_version[sizeof(PACKAGE_VERSION)] = PACKAGE_VERSION;
193 char *tilde_dev = strstr(adjusted_version, "~dev.");
194 if (tilde_dev != NULL) {
195 *tilde_dev = 'b';
196 memmove(tilde_dev + 1, tilde_dev + strlen("~dev."),
197 strlen(tilde_dev + strlen("~dev.")) + 1);
198 }
199 if (Tcl_PkgProvide(interp, "Tcldot", adjusted_version) != TCL_OK) {
200 free(ictx);
201 return TCL_ERROR;
202 }
203
204#ifdef HAVE_LIBGD
205 Gdtclft_Init(interp);
206#endif
207
208 /* create a GraphViz Context and pass a pointer to it in clientdata */
209 ictx->gvc = gvContextPlugins(lt_preloaded_symbols, DEMAND_LOADING);
210
211 Tcl_CreateCommand(interp, "dotnew", dotnew, ictx, free);
212 Tcl_CreateCommand(interp, "dotread", dotread, ictx, NULL);
213 Tcl_CreateCommand(interp, "dotstring", dotstring, ictx, NULL);
214
215 return TCL_OK;
216}
217
218int Tcldot_SafeInit(Tcl_Interp *interp);
219int Tcldot_SafeInit(Tcl_Interp *interp) { return Tcldot_Init(interp); }
220
221int Tcldot_builtin_Init(Tcl_Interp *interp);
222int Tcldot_builtin_Init(Tcl_Interp *interp) { return Tcldot_Init(interp); }
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
Definition alloc.h:101
mode
Definition cvtgxl.c:33
lt_symlist_t lt_preloaded_symbols[]
Tcl_AppInitProc Gdtclft_Init
Definition gdtclft.c:42
void free(void *)
node NULL
Definition grammar.y:163
Agiodisc_t AgIoDisc
Definition io.c:39
int agerrors(void)
Definition agerror.c:181
Agdesc_t Agundirected
undirected
Definition graph.c:276
Agdesc_t Agstrictundirected
strict undirected
Definition graph.c:277
Agdesc_t Agstrictdirected
strict directed. A strict graph cannot have multi-edges or self-arcs.
Definition graph.c:275
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Definition graph.c:44
Agraph_t * agread(void *chan, Agdisc_t *disc)
constructs a new graph
Definition grammar.c:2300
Agdesc_t Agdirected
directed
Definition graph.c:274
GVC_t * gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
Definition gvc.c:35
static bool streq(const char *a, const char *b)
are a and b equal?
Definition streq.h:11
graph descriptor
Definition cgraph.h:284
user's discipline
Definition cgraph.h:336
Agiddisc_t * id
Definition cgraph.h:337
Agiodisc_t * io
Definition cgraph.h:338
int(* afread)(void *chan, char *buf, int bufsize)
Definition cgraph.h:327
int(* flush)(void *chan)
Definition cgraph.h:329
int(* putstr)(void *chan, const char *str)
Definition cgraph.h:328
graph or subgraph
Definition cgraph.h:424
Tcl_Interp * interp
Definition tcldot.h:29
uint64_t ctr
Definition tcldot.h:28
Agdisc_t mydisc
Definition tcldot.h:26
Agiodisc_t myioDisc
Definition tcldot.h:27
GVC_t * gvc
Definition tcldot.h:30
Definition rdr.h:10
size_t cur
Definition rdr.h:13
const char * data
Definition rdr.h:11
size_t len
Definition rdr.h:12
#define Tcl_Size
Definition tcl-compat.h:33
Agiddisc_t myiddisc
Definition tcldot-id.c:91
int myiodisc_memiofread(void *chan, char *buf, int bufsize)
Definition tcldot-io.c:89
int myiodisc_afread(void *channel, char *ubuf, int n)
Definition tcldot-io.c:32
void tcldot_argv_free(Tcl_Size argc, char *argv[])
free the strings pointed to by argv
void setgraphattributes(Agraph_t *g, char *argv[], Tcl_Size argc)
char * obj2cmd(void *obj)
Definition tcldot-util.c:65
char ** tcldot_argv_dup(Tcl_Size argc, const char *argv[])
duplicate the strings pointed to by argv as non-const strings
int Tcldot_SafeInit(Tcl_Interp *interp)
Definition tcldot.c:219
int Tcldot_Init(Tcl_Interp *interp)
Definition tcldot.c:161
int Tcldot_builtin_Init(Tcl_Interp *interp)
Definition tcldot.c:222
static int dotstring(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
Definition tcldot.c:123
static int dotnew_internal(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
Definition tcldot.c:20
static int dotread(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
Definition tcldot.c:79
static int dotnew(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
Definition tcldot.c:71
#define Tcl_GetStringResult(interp)
Definition tclpathplan.c:44