chiark / gitweb /
Fix memory and type management bugs.
[chiark-tcl.git] / base / parse.c
1 /*
2  */
3
4 #include "tables.h"
5
6 int pat_charfrom(Tcl_Interp *ip, Tcl_Obj *obj, int *val,
7                  const char *opts, const char *what) {
8   *val= enum1_lookup_cached_func(ip,obj,opts,what);
9   if (*val==-1) return TCL_ERROR;
10   return TCL_OK;
11 }
12
13 int pat_enum(Tcl_Interp *ip, Tcl_Obj *obj, const void **val,
14              const void *opts, size_t sz, const char *what) {
15   *val= enum_lookup_cached_func(ip,obj,opts,sz,what);
16   if (!*val) return TCL_ERROR;
17   return TCL_OK;
18 }
19   
20 int pat_obj(Tcl_Interp *ip, Tcl_Obj *obj, Tcl_Obj **val) {
21   *val= obj;
22   return TCL_OK;
23 }
24
25 int pat_int(Tcl_Interp *ip, Tcl_Obj *obj, int *val) {
26   return Tcl_GetIntFromObj(ip, obj, val);
27 }
28   
29 int pat_hbv(Tcl_Interp *ip, Tcl_Obj *var, HBytes_Var *agg) {
30   int rc;
31   Tcl_Obj *val;
32
33   agg->var= var;
34
35   val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
36   if (!val) return TCL_ERROR;
37
38   rc= Tcl_ConvertToType(ip,val,&hbytes_type);
39   if (rc) return rc;
40
41   Tcl_InvalidateStringRep(val);
42   if (Tcl_IsShared(val)) {
43     val= Tcl_DuplicateObj(val);
44     agg->copied= 1;
45   }
46   agg->obj= val;
47
48   agg->hb= OBJ_HBYTES(val);
49   return TCL_OK;
50 }
51
52 void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg) {
53   Tcl_Obj *ro;
54   
55   if (!rc) {
56     assert(agg->obj);
57     ro= Tcl_ObjSetVar2(ip,agg->var,0,agg->obj,TCL_LEAVE_ERR_MSG);
58     if (!ro) rc= TCL_ERROR;
59   }
60   if (rc && agg->copied)
61     Tcl_DecrRefCount(agg->obj);
62 }
63
64 int pat_hb(Tcl_Interp *ip, Tcl_Obj *obj, HBytes_Value *val) {
65   int rc;
66   rc= Tcl_ConvertToType(ip,obj,&hbytes_type);  if (rc) return rc;
67   *val= *OBJ_HBYTES(obj);
68   return TCL_OK;
69 }
70
71 Tcl_Obj *ret_hb(Tcl_Interp *ip, HBytes_Value val) {
72   Tcl_Obj *obj;
73   obj= Tcl_NewObj();
74   Tcl_InvalidateStringRep(obj);
75   *OBJ_HBYTES(obj)= val;
76   obj->typePtr= &hbytes_type;
77   return obj;
78 }
79
80 Tcl_Obj *ret_int(Tcl_Interp *ip, int val) {
81   return Tcl_NewIntObj(val);
82 }
83
84 Tcl_Obj *ret_obj(Tcl_Interp *ip, Tcl_Obj *val) {
85   return val;
86 }
87
88 void setstringresult(Tcl_Interp *ip, const char *m) {
89   Tcl_ResetResult(ip);
90   Tcl_AppendResult(ip, m, (char*)0);
91 }