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
|
// Copyright 2007-2013, various authors
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <tcl.h>
#include "halcmd.h"
Tcl_Interp *target_interp = NULL;
static int pending_cr = 0;
static void halError(Tcl_Interp *interp, int result) {
// Usually, halcmd leaves a good string message via halcmd_error()
// but just in case, fall back to using the result of the halcmd api call as
// a negative errno value...
if(!*Tcl_GetStringResult(interp))
Tcl_SetResult(interp, strerror(-result), TCL_VOLATILE);
}
static int refcount = 0;
static void shutdown(void) {
if(refcount > 0) {
refcount --;
if(refcount == 0) halcmd_shutdown();
}
}
static int init() {
int result = 0;
if(refcount == 0) {
result = halcmd_startup(0);
atexit(shutdown);
}
if(result == 0) {
refcount ++;
}
return result;
}
static void halExit(ClientData d) {
shutdown();
}
static int halCmd(ClientData cd, Tcl_Interp *interp, int argc, const char **argv) {
int result;
Tcl_ResetResult(interp);
if(argc < 2) {
Tcl_AppendResult(interp,
"wrong # args: should be \"", argv[0], " command ...\"", NULL);
return TCL_ERROR;
}
if(strcmp(argv[1], "--commands") == 0)
{
int i;
Tcl_ResetResult(interp);
for(i=0; i<halcmd_ncommands; i++)
Tcl_AppendElement(interp, halcmd_commands[i].name);
return TCL_OK;
}
target_interp = interp;
pending_cr = 0;
result = halcmd_parse_cmd((char **)argv+1);
target_interp = NULL;
if(result == 0) return TCL_OK;
halError(interp, result);
return TCL_ERROR;
}
int Hal_Init(Tcl_Interp *interp) {
int result = init();
if(result < 0) {
Tcl_ResetResult(interp);
halError(interp, result);
return TCL_ERROR;
}
if (Tcl_InitStubs(interp, "8.1", 0) == NULL)
{
return TCL_ERROR;
}
Tcl_CreateCommand(interp, "hal", halCmd, 0, halExit);
Tcl_PkgProvide(interp, "Hal", "1.0");
return TCL_OK;
}
#ifndef BUFFERLEN
#define BUFFERLEN 1024
#endif
void halcmd_output(const char *format, ...) {
char buf[BUFFERLEN + 1];
va_list ap;
int len;
va_start(ap, format);
vsnprintf(buf, BUFFERLEN, format, ap);
va_end(ap);
if(pending_cr)
Tcl_AppendResult(target_interp, "\n", NULL);
len = strlen(buf);
if(buf[len-1] == '\n') {
buf[len-1] = 0;
pending_cr = 1;
} else {
pending_cr = 0;
}
Tcl_AppendResult(target_interp, buf, NULL);
}
void halcmd_error(const char *format, ...) {
char buf[BUFFERLEN + 1];
va_list ap;
int len;
va_start(ap, format);
vsnprintf(buf, BUFFERLEN, format, ap);
va_end(ap);
if(pending_cr)
Tcl_AppendResult(target_interp, "\n", NULL);
len = strlen(buf);
if(buf[len-1] == '\n') {
buf[len-1] = 0;
pending_cr = 1;
} else {
pending_cr = 0;
}
Tcl_AppendResult(target_interp, buf, NULL);
}
void halcmd_warning(const char *format, ...) {
char buf[BUFFERLEN + 1];
va_list ap;
int len;
va_start(ap, format);
vsnprintf(buf, BUFFERLEN, format, ap);
va_end(ap);
if(pending_cr)
Tcl_AppendResult(target_interp, "\n", NULL);
len = strlen(buf);
if(buf[len-1] == '\n') {
buf[len-1] = 0;
pending_cr = 1;
} else {
pending_cr = 0;
}
Tcl_AppendResult(target_interp, buf, NULL);
}
void halcmd_info(const char *format, ...) {
char buf[BUFFERLEN + 1];
va_list ap;
int len;
va_start(ap, format);
vsnprintf(buf, BUFFERLEN, format, ap);
va_end(ap);
if(pending_cr)
Tcl_AppendResult(target_interp, "\n", NULL);
len = strlen(buf);
if(buf[len-1] == '\n') {
buf[len-1] = 0;
pending_cr = 1;
} else {
pending_cr = 0;
}
Tcl_AppendResult(target_interp, buf, NULL);
}
|