| 1 |
/* |
| 2 |
* pkgb.c -- |
| 3 |
* |
| 4 |
* This file contains a simple Tcl package "pkgb" that is intended for |
| 5 |
* testing the Tcl dynamic loading facilities. It can be used in both |
| 6 |
* safe and unsafe interpreters. |
| 7 |
* |
| 8 |
* Copyright (c) 1995 Sun Microsystems, Inc. |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution of |
| 11 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#undef STATIC_BUILD |
| 15 |
#include "tcl.h" |
| 16 |
#if defined(_WIN32) && defined(_MSC_VER) |
| 17 |
# define snprintf _snprintf |
| 18 |
#endif |
| 19 |
#if TCL_MAJOR_VERSION < 9 |
| 20 |
# define Tcl_Size int |
| 21 |
#endif |
| 22 |
|
| 23 |
/* |
| 24 |
*---------------------------------------------------------------------- |
| 25 |
* |
| 26 |
* Pkgb_SubObjCmd -- |
| 27 |
* |
| 28 |
* This procedure is invoked to process the "pkgb_sub" Tcl command. It |
| 29 |
* expects two arguments and returns their difference. |
| 30 |
* |
| 31 |
* Results: |
| 32 |
* A standard Tcl result. |
| 33 |
* |
| 34 |
* Side effects: |
| 35 |
* See the user documentation. |
| 36 |
* |
| 37 |
*---------------------------------------------------------------------- |
| 38 |
*/ |
| 39 |
|
| 40 |
static int |
| 41 |
Pkgb_SubObjCmd( |
| 42 |
void *dummy, /* Not used. */ |
| 43 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 44 |
int objc, /* Number of arguments. */ |
| 45 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 46 |
{ |
| 47 |
int first, second; |
| 48 |
(void)dummy; |
| 49 |
|
| 50 |
if (objc != 3) { |
| 51 |
Tcl_WrongNumArgs(interp, 1, objv, "num num"); |
| 52 |
return TCL_ERROR; |
| 53 |
} |
| 54 |
if ((Tcl_GetIntFromObj(interp, objv[1], &first) != TCL_OK) |
| 55 |
|| (Tcl_GetIntFromObj(interp, objv[2], &second) != TCL_OK)) { |
| 56 |
char buf[TCL_INTEGER_SPACE]; |
| 57 |
snprintf(buf, sizeof(buf), "%d", Tcl_GetErrorLine(interp)); |
| 58 |
Tcl_AppendResult(interp, " in line: ", buf, (char *)NULL); |
| 59 |
return TCL_ERROR; |
| 60 |
} |
| 61 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(first - second)); |
| 62 |
return TCL_OK; |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
*---------------------------------------------------------------------- |
| 67 |
* |
| 68 |
* Pkgb_UnsafeObjCmd -- |
| 69 |
* |
| 70 |
* This procedure is invoked to process the "pkgb_unsafe" Tcl command. It |
| 71 |
* just returns a constant string. |
| 72 |
* |
| 73 |
* Results: |
| 74 |
* A standard Tcl result. |
| 75 |
* |
| 76 |
* Side effects: |
| 77 |
* See the user documentation. |
| 78 |
* |
| 79 |
*---------------------------------------------------------------------- |
| 80 |
*/ |
| 81 |
|
| 82 |
static int |
| 83 |
Pkgb_UnsafeObjCmd( |
| 84 |
void *dummy, /* Not used. */ |
| 85 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 86 |
int objc, /* Number of arguments. */ |
| 87 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 88 |
{ |
| 89 |
(void)dummy; |
| 90 |
(void)objc; |
| 91 |
(void)objv; |
| 92 |
|
| 93 |
return Tcl_EvalEx(interp, "list unsafe command invoked", -1, TCL_EVAL_GLOBAL); |
| 94 |
} |
| 95 |
|
| 96 |
static int |
| 97 |
Pkgb_DemoObjCmd( |
| 98 |
void *dummy, /* Not used. */ |
| 99 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 100 |
int objc, /* Number of arguments. */ |
| 101 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 102 |
{ |
| 103 |
Tcl_WideInt numChars; |
| 104 |
int result; |
| 105 |
(void)dummy; |
| 106 |
|
| 107 |
if (objc != 4) { |
| 108 |
Tcl_WrongNumArgs(interp, 1, objv, "arg1 arg2 num"); |
| 109 |
return TCL_ERROR; |
| 110 |
} |
| 111 |
if (Tcl_GetWideIntFromObj(interp, objv[3], &numChars) != TCL_OK) { |
| 112 |
return TCL_ERROR; |
| 113 |
} |
| 114 |
result = Tcl_UtfNcmp(Tcl_GetString(objv[1]), Tcl_GetString(objv[2]), (size_t)numChars); |
| 115 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(result)); |
| 116 |
return TCL_OK; |
| 117 |
} |
| 118 |
|
| 119 |
/* |
| 120 |
*---------------------------------------------------------------------- |
| 121 |
* |
| 122 |
* Pkgb_Init -- |
| 123 |
* |
| 124 |
* This is a package initialization procedure, which is called by Tcl |
| 125 |
* when this package is to be added to an interpreter. |
| 126 |
* |
| 127 |
* Results: |
| 128 |
* None. |
| 129 |
* |
| 130 |
* Side effects: |
| 131 |
* None. |
| 132 |
* |
| 133 |
*---------------------------------------------------------------------- |
| 134 |
*/ |
| 135 |
|
| 136 |
DLLEXPORT int |
| 137 |
Pkgb_Init( |
| 138 |
Tcl_Interp *interp) /* Interpreter in which the package is to be |
| 139 |
* made available. */ |
| 140 |
{ |
| 141 |
int code; |
| 142 |
|
| 143 |
if (Tcl_InitStubs(interp, "8.6-", 0) == NULL) { |
| 144 |
return TCL_ERROR; |
| 145 |
} |
| 146 |
code = Tcl_PkgProvide(interp, "pkgb", "2.3"); |
| 147 |
if (code != TCL_OK) { |
| 148 |
return code; |
| 149 |
} |
| 150 |
Tcl_CreateObjCommand(interp, "pkgb_sub", Pkgb_SubObjCmd, NULL, NULL); |
| 151 |
Tcl_CreateObjCommand(interp, "pkgb_unsafe", Pkgb_UnsafeObjCmd, NULL, NULL); |
| 152 |
Tcl_CreateObjCommand(interp, "pkgb_demo", Pkgb_DemoObjCmd, NULL, NULL); |
| 153 |
return TCL_OK; |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
*---------------------------------------------------------------------- |
| 158 |
* |
| 159 |
* Pkgb_SafeInit -- |
| 160 |
* |
| 161 |
* This is a package initialization procedure, which is called by Tcl |
| 162 |
* when this package is to be added to a safe interpreter. |
| 163 |
* |
| 164 |
* Results: |
| 165 |
* None. |
| 166 |
* |
| 167 |
* Side effects: |
| 168 |
* None. |
| 169 |
* |
| 170 |
*---------------------------------------------------------------------- |
| 171 |
*/ |
| 172 |
|
| 173 |
DLLEXPORT int |
| 174 |
Pkgb_SafeInit( |
| 175 |
Tcl_Interp *interp) /* Interpreter in which the package is to be |
| 176 |
* made available. */ |
| 177 |
{ |
| 178 |
int code; |
| 179 |
|
| 180 |
if (Tcl_InitStubs(interp, "8.6-", 0) == NULL) { |
| 181 |
return TCL_ERROR; |
| 182 |
} |
| 183 |
code = Tcl_PkgProvide(interp, "pkgb", "2.3"); |
| 184 |
if (code != TCL_OK) { |
| 185 |
return code; |
| 186 |
} |
| 187 |
Tcl_CreateObjCommand(interp, "pkgb_sub", Pkgb_SubObjCmd, NULL, NULL); |
| 188 |
return TCL_OK; |
| 189 |
} |