-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcl_ex.cpp
More file actions
122 lines (95 loc) · 2.94 KB
/
Copy pathtcl_ex.cpp
File metadata and controls
122 lines (95 loc) · 2.94 KB
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
// tcl_ex.cpp - How to embed TCL library - example
// EXIT_* constants
#include <stdlib.h>
// printf(3)....
#include <stdio.h>
// uname(2)
#include <sys/utsname.h>
// sysinfo(2) - uptime
#include <sys/sysinfo.h>
#include <tcl.h>
// custom command uname -m - returns machine name
static int
UnameMachineCmd(
ClientData dummy, /* Not used. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
struct utsname un;
if (uname(&un)){
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"error calling uname(): %s",
Tcl_PosixError(interp)));
return TCL_ERROR;
} else {
Tcl_SetObjResult(interp, Tcl_NewStringObj(un.machine, -1) );
return TCL_OK;
}
}
static int UptimeSecondsCmd(
ClientData dummy, /* Not used. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
struct sysinfo in;
if (sysinfo(&in)){
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"error calling sysinfo(): %s",
Tcl_PosixError(interp)));
return TCL_ERROR;
} else {
Tcl_SetObjResult(interp, Tcl_NewLongObj(in.uptime) );
return TCL_OK;
}
}
// add custom commands to Tcl
static int Ex_ExtendTcl (Tcl_Interp *interp) {
// WARNING! Tcl_CreateObjCommand() no longer returns error code - but rather
// command token.
// Original example is wrong at: https://wiki.tcl-lang.org/page/How+to+embed+Tcl+in+C+applications
Tcl_CreateObjCommand(
interp, "::ex::uname_machine", UnameMachineCmd, NULL, NULL);
Tcl_CreateObjCommand(
interp, "::ex::uptime_seconds", UptimeSecondsCmd, NULL, NULL);
return TCL_OK;
}
// runs TCL commands in tclCommands, returns EXIT_SUCESS or EXIT_FAILURE
static int Ex_RunTcl(const char *tclCommands){
int rc = EXIT_FAILURE;
int err = TCL_OK;
Tcl_Interp *interp = Tcl_CreateInterp();
if (interp == NULL){
fprintf(stderr,"Tcl_CreateInterp() returned NULL");
goto exit0;
}
err = Ex_ExtendTcl(interp);
if ( err != TCL_OK ){
fprintf(stderr,"Error calling Ex_ExtendTcl(): %s\n",Tcl_GetStringResult(interp));
goto exit2;
}
err = Tcl_Eval(interp,tclCommands);
if ( err != TCL_OK ){
fprintf(stderr,"Error calling Tcl_Eval(): %s\n",Tcl_GetStringResult(interp));
goto exit2;
}
rc = EXIT_SUCCESS;
exit2:
Tcl_FreeResult(interp);
// exit1:
Tcl_DeleteInterp(interp);
exit0:
return rc;
}
int main(int argc, char **argv)
{
int rc = EXIT_SUCCESS;
Tcl_FindExecutable(argv[0]);
rc = Ex_RunTcl("puts \"Hello, world on [::ex::uname_machine]!\"");
if (rc == EXIT_SUCCESS){
rc = Ex_RunTcl("puts \"System uptime is [::ex::uptime_seconds] seconds.\"");
}
Tcl_Finalize();
return rc;
}