2 * base code for various Tcl extensions
3 * Copyright 2006-2012 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "chiark-tcl-base.h"
21 int cht_pat_charfrom(Tcl_Interp *ip, Tcl_Obj *obj, int *val,
22 const char *opts, const char *what) {
23 *val= cht_enum1_lookup_cached_func(ip,obj,opts,what);
24 if (*val==-1) return TCL_ERROR;
28 int cht_pat_int(Tcl_Interp *ip, Tcl_Obj *obj, int *val) {
29 return Tcl_GetIntFromObj(ip, obj, val);
32 int cht_pat_long(Tcl_Interp *ip, Tcl_Obj *obj, long *val) {
33 return Tcl_GetLongFromObj(ip, obj, val);
36 int cht_pat_string(Tcl_Interp *ip, Tcl_Obj *obj, const char **val) {
37 *val= Tcl_GetString(obj);
41 int cht_pat_constv(Tcl_Interp *ip, Tcl_Obj *var,
42 Tcl_Obj **val_r, Tcl_ObjType *type) {
46 val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
47 if (!val) return TCL_ERROR;
50 rc= Tcl_ConvertToType(ip,val,type);
58 void cht_init_somethingv(Something_Var *sth) {
59 sth->obj=0; sth->var=0; sth->copied=0;
62 int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
63 Something_Var *sth, Tcl_ObjType *type) {
69 val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
70 if (!val) return TCL_ERROR;
72 rc= Tcl_ConvertToType(ip,val,type);
75 if (Tcl_IsShared(val)) {
76 val= Tcl_DuplicateObj(val);
79 Tcl_InvalidateStringRep(val);
85 void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth) {
90 ro= Tcl_ObjSetVar2(ip,sth->var,0,sth->obj,TCL_LEAVE_ERR_MSG);
91 if (!ro) rc= TCL_ERROR;
93 if (rc && sth->copied)
94 Tcl_DecrRefCount(sth->obj);
97 Tcl_Obj *cht_ret_long(Tcl_Interp *ip, long val) {
98 return Tcl_NewLongObj(val);
101 Tcl_Obj *cht_ret_string(Tcl_Interp *ip, const char *val) {
102 return Tcl_NewStringObj(val,-1);