Graphviz  2.41.20170921.2350
tcldot-nodecmd.c
Go to the documentation of this file.
1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 #include "tcldot.h"
15 
16 int nodecmd(ClientData clientData, Tcl_Interp * interp,
17 #ifndef TCLOBJ
18  int argc, char *argv[]
19 #else /* TCLOBJ */
20  int argc, Tcl_Obj * CONST objv[]
21 #endif /* TCLOBJ */
22  )
23 {
24  char c, **argv2;
25  int i, j, length, argc2;
26  Agraph_t *g;
27  Agnode_t *n, *head;
28  Agedge_t *e;
29  Agsym_t *a;
30  gctx_t *gctx = (gctx_t *)clientData;
31 
32  if (argc < 2) {
33  Tcl_AppendResult(interp, "Wrong # args: should be \"", argv[0], " option ?arg arg ...?\"", NULL);
34  return TCL_ERROR;
35  }
36  n = cmd2n(argv[0]);
37  if (!n) {
38  Tcl_AppendResult(interp, "Node \"", argv[0], "\" not found", NULL);
39  return TCL_ERROR;
40  }
41  g = agraphof(n);
42 
43  c = argv[1][0];
44  length = strlen(argv[1]);
45 
46 
47  if ((c == 'a') && (strncmp(argv[1], "addedge", length) == 0)) {
48  if ((argc < 3) || (!(argc % 2))) {
49  Tcl_AppendResult(interp, "Wrong # args: should be \"", argv[0], " addedge head ?attributename attributevalue? ?...?\"", NULL);
50  return TCL_ERROR;
51  }
52  head = cmd2n(argv[2]);
53  if (!head) {
54  if (!(head = agfindnode(g, argv[2]))) {
55  Tcl_AppendResult(interp, "Head node \"", argv[2], "\" not found.", NULL);
56  return TCL_ERROR;
57  }
58  }
59  if (agroot(g) != agroot(agraphof(head))) {
60  Tcl_AppendResult(interp, "Nodes ", argv[0], " and ", argv[2], " are not in the same graph.", NULL);
61  return TCL_ERROR;
62  }
63  e = agedge(g, n, head, NULL, 1);
64  Tcl_AppendResult(interp, obj2cmd(e), NULL);
65  setedgeattributes(agroot(g), e, &argv[3], argc - 3);
66  return TCL_OK;
67 
68  } else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
69  deleteNode(gctx, g, n);
70  return TCL_OK;
71 
72  } else if ((c == 'f') && (strncmp(argv[1], "findedge", length) == 0)) {
73  if (argc < 3) {
74  Tcl_AppendResult(interp, "Wrong # args: should be \"", argv[0], " findedge headnodename\"", NULL);
75  return TCL_ERROR;
76  }
77  if (!(head = agfindnode(g, argv[2]))) {
78  Tcl_AppendResult(interp, "Head node \"", argv[2], "\" not found.", NULL);
79  return TCL_ERROR;
80  }
81  if (!(e = agfindedge(g, n, head))) {
82  Tcl_AppendResult(interp, "Edge \"", argv[0], " - ", obj2cmd(head), "\" not found.", NULL);
83  return TCL_ERROR;
84  }
85  Tcl_AppendElement(interp, obj2cmd(head));
86  return TCL_OK;
87 
88  } else if ((c == 'l')
89  && (strncmp(argv[1], "listattributes", length) == 0)) {
90  listNodeAttrs (interp, g);
91  return TCL_OK;
92 
93  } else if ((c == 'l') && (strncmp(argv[1], "listedges", length) == 0)) {
94  for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
95  Tcl_AppendElement(interp, obj2cmd(e));
96  }
97  return TCL_OK;
98 
99  } else if ((c == 'l')
100  && (strncmp(argv[1], "listinedges", length) == 0)) {
101  for (e = agfstin(g, n); e; e = agnxtin(g, e)) {
102  Tcl_AppendElement(interp, obj2cmd(e));
103  }
104  return TCL_OK;
105 
106  } else if ((c == 'l')
107  && (strncmp(argv[1], "listoutedges", length) == 0)) {
108  for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
109  Tcl_AppendElement(interp, obj2cmd(e));
110  }
111  return TCL_OK;
112 
113  } else if ((c == 'q')
114  && (strncmp(argv[1], "queryattributes", length) == 0)) {
115  for (i = 2; i < argc; i++) {
116  if (Tcl_SplitList
117  (interp, argv[i], &argc2,
118  (CONST84 char ***) &argv2) != TCL_OK)
119  return TCL_ERROR;
120  for (j = 0; j < argc2; j++) {
121  if ((a = agfindnodeattr(g, argv2[j]))) {
122  Tcl_AppendElement(interp, agxget(n, a));
123  } else {
124  Tcl_AppendResult(interp, "No attribute named \"", argv2[j], "\"", NULL);
125  return TCL_ERROR;
126  }
127  }
128  Tcl_Free((char *) argv2);
129  }
130  return TCL_OK;
131 
132  } else if ((c == 'q')
133  && (strncmp(argv[1], "queryattributevalues", length) ==
134  0)) {
135  for (i = 2; i < argc; i++) {
136  if (Tcl_SplitList
137  (interp, argv[i], &argc2,
138  (CONST84 char ***) &argv2) != TCL_OK)
139  return TCL_ERROR;
140  for (j = 0; j < argc2; j++) {
141  if ((a = agfindnodeattr(g, argv2[j]))) {
142  Tcl_AppendElement(interp, argv2[j]);
143  Tcl_AppendElement(interp, agxget(n, a));
144  } else {
145  Tcl_AppendResult(interp, "No attribute named \"", argv2[j], "\"", NULL);
146  return TCL_ERROR;
147  }
148  }
149  Tcl_Free((char *) argv2);
150  }
151  return TCL_OK;
152 
153  } else if ((c == 's')
154  && (strncmp(argv[1], "setattributes", length) == 0)) {
155  g = agroot(g);
156  if (argc == 3) {
157  if (Tcl_SplitList
158  (interp, argv[2], &argc2,
159  (CONST84 char ***) &argv2) != TCL_OK)
160  return TCL_ERROR;
161  if ((argc2 == 0) || (argc2 % 2)) {
162  Tcl_AppendResult(interp, "Wrong # args: should be \"", argv[0],
163  "\" setattributes attributename attributevalue ?attributename attributevalue? ?...?",
164  NULL);
165  Tcl_Free((char *) argv2);
166  return TCL_ERROR;
167  }
168  setnodeattributes(g, n, argv2, argc2);
169  Tcl_Free((char *) argv2);
170  } else {
171  if ((argc < 4) || (argc % 2)) {
172  Tcl_AppendResult(interp, "Wrong # args: should be \"", argv[0],
173  "\" setattributes attributename attributevalue ?attributename attributevalue? ?...?",
174  NULL);
175  return TCL_ERROR;
176  }
177  setnodeattributes(g, n, &argv[2], argc - 2);
178  }
179  return TCL_OK;
180 
181  } else if ((c == 's') && (strncmp(argv[1], "showname", length) == 0)) {
182  Tcl_SetResult(interp, agnameof(n), TCL_STATIC);
183  return TCL_OK;
184 
185  } else {
186  Tcl_AppendResult(interp, "Bad option \"", argv[1],
187  "\": must be one of:",
188  "\n\taddedge, listattributes, listedges, listinedges,",
189  "\n\tlistoutedges, queryattributes, queryattributevalues,",
190  "\n\tsetattributes, showname.", NULL);
191  return TCL_ERROR;
192  }
193 }
void listNodeAttrs(Tcl_Interp *interp, Agraph_t *g)
Definition: tcldot-util.c:203
Agnode_t * cmd2n(char *cmd)
Definition: tcldot-util.c:37
#define head
Definition: dthdr.h:19
CGRAPH_API Agedge_t * agfstin(Agraph_t *g, Agnode_t *n)
Definition: edge.c:56
char * obj2cmd(void *obj)
Definition: tcldot-util.c:55
CGRAPH_API Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition: edge.c:86
CGRAPH_API Agraph_t * agroot(void *obj)
Definition: obj.c:169
CGRAPH_API Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition: edge.c:25
CGRAPH_API Agraph_t * agraphof(void *obj)
Definition: obj.c:185
CGRAPH_API char * agnameof(void *)
Definition: id.c:143
#define agfindnode(g, n)
Definition: types.h:611
int nodecmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
#define NULL
Definition: logic.h:39
Definition: tcldot.h:57
CGRAPH_API Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition: edge.c:95
void setedgeattributes(Agraph_t *g, Agedge_t *e, char *argv[], int argc)
Definition: tcldot-util.c:155
void deleteNode(gctx_t *gctx, Agraph_t *g, Agnode_t *n)
Definition: tcldot-util.c:87
CGRAPH_API Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Definition: edge.c:281
CGRAPH_API Agedge_t * agnxtin(Agraph_t *g, Agedge_t *e)
Definition: edge.c:70
void setnodeattributes(Agraph_t *g, Agnode_t *n, char *argv[], int argc)
Definition: tcldot-util.c:178
#define agfindedge(g, t, h)
Definition: types.h:610
char * agxget(void *obj, Agsym_t *sym)
Definition: attr.c:444
CGRAPH_API Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition: edge.c:40
#define agfindnodeattr(g, a)
Definition: types.h:613
#define CONST84
Definition: tcldot.h:25