forked from RudolfWeeber/espresso-virtual-sites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockfile_tcl.c
232 lines (219 loc) · 7.54 KB
/
blockfile_tcl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
Copyright (C) 2010 The ESPResSo project
Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010 Max-Planck-Institute for Polymer Research, Theory Group, PO Box 3148, 55021 Mainz, Germany
This file is part of ESPResSo.
ESPResSo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ESPResSo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file blockfile_tcl.c
Implementation of \ref blockfile_tcl.h "blockfile_tcl.h".
*/
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "utils.h"
#include "global.h"
#include "blockfile.h"
#include "blockfile_tcl.h"
#include "communication.h"
int blockfile(ClientData data, Tcl_Interp *interp,
int argc, char *argv[])
{
char title[MAXBLOCKTITLE];
char buffer[1024], *name;
int tcl_file_mode;
Tcl_Channel channel;
Tcl_CmdInfo cmdInfo;
int openbrackets, len, exists;
if (argc < 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " <file> read|write <what> <param>? ...\"",
(char *) NULL);
return (TCL_ERROR);
}
if ((channel = Tcl_GetChannel(interp, argv[1], &tcl_file_mode)) == NULL)
return (TCL_ERROR);
/* ------------------ the write commands ------------------------------------ */
if (!strncmp(argv[2], "write", strlen(argv[2]))) {
if (!(tcl_file_mode & TCL_WRITABLE)) {
Tcl_AppendResult(interp, "\"", argv[1], "\" not writeable", (char *) NULL);
return (TCL_ERROR);
}
/* ------------------ write start tag ------------------------------------ */
if (!strncmp(argv[3], "start", strlen(argv[3]))) {
if (argc != 5 || strlen(argv[4]) > MAXBLOCKTITLE - 1) {
Tcl_AppendResult(interp, "please give a short (< 63 bytes) title for your block and nothing more",
(char *) NULL);
return (TCL_ERROR);
}
if (block_writestart(channel, argv[4]) != 0) {
Tcl_AppendResult(interp, "\"", argv[1], "\" could not write start tag",
(char *) NULL);
return (TCL_ERROR);
}
return (TCL_OK);
}
/* ------------------ write end tag ------------------------------------ */
else if (!strncmp(argv[3], "end", strlen(argv[3]))) {
if (argc != 4) {
Tcl_AppendResult(interp, "\"", argv[0],
" write end\" takes no parameters",
(char *) NULL);
return (TCL_ERROR);
}
if (block_writeend(channel) != 0) {
Tcl_AppendResult(interp, "\"", argv[1], "\" could not write end tag",
(char *) NULL);
return (TCL_ERROR);
}
return (TCL_OK);
}
}
/* ------------------- the read commands ------------------------------------ */
else if (!strncmp(argv[2], "read", strlen(argv[2]))) {
if (!(tcl_file_mode & TCL_READABLE)) {
Tcl_AppendResult(interp, "\"", argv[1], "\" not readable", (char *) NULL);
return (TCL_ERROR);
}
/* ----------------- read start tag only ------------------------------- */
if (!strncmp(argv[3], "start", strlen(argv[3]))) {
if (argc != 4) {
Tcl_AppendResult(interp, "\"", argv[0],
" read start\" takes no parameters",
(char *) NULL);
return (TCL_ERROR);
}
if ((openbrackets = block_startread(channel, title)) == 1) {
Tcl_AppendResult(interp, title, (char *)NULL);
return (TCL_OK);
}
else {
Tcl_AppendResult(interp, "\"", argv[1], "\" could not read block start",
(char *) NULL);
return (TCL_ERROR);
}
}
/* -------------- read next block and auto execute or return data --*/
else if (!strncmp(argv[3], "auto", strlen(argv[3]))) {
if (argc != 4) {
Tcl_AppendResult(interp, "\"", argv[0],
" read auto\" takes no parameters",
(char *) NULL);
return (TCL_ERROR);
}
switch (openbrackets = block_startread(channel, title)) {
case 0:
case 1:
break;
case RETURN_CODE_EOF:
Tcl_AppendResult(interp, "eof", (char *)NULL);
return (TCL_OK);
case RETURN_CODE_FILE_FORMAT:
Tcl_AppendResult(interp, "illstring ", title, (char *)NULL);
return (TCL_OK);
default:
Tcl_AppendResult(interp, "\"", argv[1], "\" could not read block start",
(char *) NULL);
return (TCL_ERROR);
}
/* -------------- read unknown field ---------------------------- */
len = 21; /* blockfile_read_auto_ + \0 */
len += strlen(title);
name = malloc(len);
strcpy(name, "blockfile_read_auto_");
strcat(name, title);
exists = Tcl_GetCommandInfo(interp, name, &cmdInfo);
free(name);
if (exists) {
int err = cmdInfo.proc(cmdInfo.clientData, interp,
argc, argv);
return mpi_gather_runtime_errors(interp, err);
}
Tcl_AppendResult(interp, "usertag ", title, (char *) NULL);
if (openbrackets != 0) {
Tcl_AppendResult(interp, " ", (char *) NULL);
while ((openbrackets =
block_continueread(channel, openbrackets,
buffer, sizeof(buffer), 0)) > 0) {
Tcl_AppendResult(interp, buffer, (char *) NULL);
}
if (openbrackets < 0) {
Tcl_AppendResult(interp, "\"", argv[1], "\" could not read data",
(char *) NULL);
return (TCL_ERROR);
}
Tcl_AppendResult(interp, buffer, (char *) NULL);
}
return (TCL_OK);
}
/* ------------------- read until end tag ------------------------------ */
else if (!strncmp(argv[3], "toend", strlen(argv[3]))) {
if (argc != 4) {
Tcl_AppendResult(interp, "\"", argv[0],
" read toend\" takes no parameters",
(char *) NULL);
return (TCL_ERROR);
}
openbrackets = 1;
while ((openbrackets =
block_continueread(channel, openbrackets,
buffer, sizeof(buffer), 0)) > 0) {
Tcl_AppendResult(interp, buffer, (char *) NULL);
}
if (openbrackets < 0) {
Tcl_AppendResult(interp, "\"", argv[1], "\" could not read data",
(char *) NULL);
return (TCL_ERROR);
}
Tcl_AppendResult(interp, buffer, (char *) NULL);
return (TCL_OK);
}
/* -------- (probably) read any tag implemented in tcl ----------------- */
else {
len = 21; /* blockfile_read_auto_ + \0 */
len += strlen(argv[3]);
name = malloc(len);
strcpy(name, "blockfile_read_auto_");
strcat(name, argv[3]);
exists = Tcl_GetCommandInfo(interp, name, &cmdInfo);
free(name);
if (exists) {
int err;
if (!((openbrackets = block_startread(channel, title)) == 1) ||
strncmp(title,argv[3], strlen(argv[3]))) {
Tcl_AppendResult(interp, "\"", argv[1], "\" did not contain block\"", argv[3],"\" you indicated",
(char *) NULL);
return (TCL_ERROR);
}
err = cmdInfo.proc(cmdInfo.clientData, interp, argc, argv);
return mpi_gather_runtime_errors(interp, err);
}
}
}
/* not a native action, try script support */
len = 12; /* blockfile_ + _ + \0 */
len += strlen(argv[2]) + strlen(argv[3]);
name = malloc(len);
strcpy(name, "blockfile_");
strcat(name, argv[2]);
strcat(name, "_");
strcat(name, argv[3]);
exists = Tcl_GetCommandInfo(interp, name, &cmdInfo);
free(name);
if (exists) {
int err = cmdInfo.proc(cmdInfo.clientData, interp, argc, argv);
return mpi_gather_runtime_errors(interp, err);
}
Tcl_AppendResult(interp, "unknown action \"", argv[2], " ", argv[3],
"\"", (char *)NULL);
return (TCL_ERROR);
}