chiark / gitweb /
Many bugfixes.
[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_long(Tcl_Interp *ip, Tcl_Obj *obj, long *val) {
30   return Tcl_GetLongFromObj(ip, obj, val);
31 }
32   
33 int pat_hbv(Tcl_Interp *ip, Tcl_Obj *var, HBytes_Var *agg) {
34   int rc;
35   Tcl_Obj *val;
36
37   agg->var= var;
38
39   val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
40   if (!val) return TCL_ERROR;
41
42   rc= Tcl_ConvertToType(ip,val,&hbytes_type);
43   if (rc) return rc;
44
45   Tcl_InvalidateStringRep(val);
46   if (Tcl_IsShared(val)) {
47     val= Tcl_DuplicateObj(val);
48     agg->copied= 1;
49   }
50   agg->obj= val;
51
52   agg->hb= OBJ_HBYTES(val);
53   return TCL_OK;
54 }
55
56 void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg) {
57   Tcl_Obj *ro;
58   
59   if (!rc) {
60     assert(agg->obj);
61     ro= Tcl_ObjSetVar2(ip,agg->var,0,agg->obj,TCL_LEAVE_ERR_MSG);
62     if (!ro) rc= TCL_ERROR;
63   }
64   if (rc && agg->copied)
65     Tcl_DecrRefCount(agg->obj);
66 }
67
68 int pat_hb(Tcl_Interp *ip, Tcl_Obj *obj, HBytes_Value *val) {
69   int rc;
70   rc= Tcl_ConvertToType(ip,obj,&hbytes_type);  if (rc) return rc;
71   *val= *OBJ_HBYTES(obj);
72   return TCL_OK;
73 }
74
75 Tcl_Obj *ret_hb(Tcl_Interp *ip, HBytes_Value val) {
76   Tcl_Obj *obj;
77   obj= Tcl_NewObj();
78   Tcl_InvalidateStringRep(obj);
79   *OBJ_HBYTES(obj)= val;
80   obj->typePtr= &hbytes_type;
81   return obj;
82 }
83
84 Tcl_Obj *ret_int(Tcl_Interp *ip, int val) {
85   return Tcl_NewIntObj(val);
86 }
87
88 Tcl_Obj *ret_long(Tcl_Interp *ip, long val) {
89   return Tcl_NewLongObj(val);
90 }
91
92 Tcl_Obj *ret_obj(Tcl_Interp *ip, Tcl_Obj *val) {
93   return val;
94 }
95
96 void setstringresult(Tcl_Interp *ip, const char *m) {
97   Tcl_ResetResult(ip);
98   Tcl_AppendResult(ip, m, (char*)0);
99 }