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