File: parse.c

package info (click to toggle)
chiark-tcl 1.3.7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 592 kB
  • sloc: ansic: 4,542; perl: 415; makefile: 129; tcl: 106; sh: 38
file content (103 lines) | stat: -rw-r--r-- 2,617 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * base code for various Tcl extensions
 * Copyright 2006-2012 Ian Jackson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "chiark-tcl-base.h"

int cht_pat_charfrom(Tcl_Interp *ip, Tcl_Obj *obj, int *val,
		 const char *opts, const char *what) {
  *val= cht_enum1_lookup_cached_func(ip,obj,opts,what);
  if (*val==-1) return TCL_ERROR;
  return TCL_OK;
}

int cht_pat_int(Tcl_Interp *ip, Tcl_Obj *obj, int *val) {
  return Tcl_GetIntFromObj(ip, obj, val);
}
  
int cht_pat_long(Tcl_Interp *ip, Tcl_Obj *obj, long *val) {
  return Tcl_GetLongFromObj(ip, obj, val);
}
  
int cht_pat_string(Tcl_Interp *ip, Tcl_Obj *obj, const char **val) {
  *val= Tcl_GetString(obj);
  return TCL_OK;
}

int cht_pat_constv(Tcl_Interp *ip, Tcl_Obj *var,
	       Tcl_Obj **val_r, Tcl_ObjType *type) {
  int rc;
  Tcl_Obj *val;
  
  val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
  if (!val) return TCL_ERROR;

  if (type) {
    rc= Tcl_ConvertToType(ip,val,type);
    if (rc) return rc;
  }

  *val_r= val;
  return TCL_OK;
}

void cht_init_somethingv(Something_Var *sth) {
  sth->obj=0; sth->var=0; sth->copied=0;
}

int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
		   Something_Var *sth, Tcl_ObjType *type) {
  int rc;
  Tcl_Obj *val;

  sth->var= var;

  val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
  if (!val) return TCL_ERROR;

  rc= Tcl_ConvertToType(ip,val,type);
  if (rc) return rc;

  if (Tcl_IsShared(val)) {
    val= Tcl_DuplicateObj(val);
    sth->copied= 1;
  }
  Tcl_InvalidateStringRep(val);
  sth->obj= val;

  return TCL_OK;
}

void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth) {
  Tcl_Obj *ro;
  
  if (!rc) {
    assert(sth->obj);
    ro= Tcl_ObjSetVar2(ip,sth->var,0,sth->obj,TCL_LEAVE_ERR_MSG);
    if (!ro) rc= TCL_ERROR;
  }
  if (rc && sth->copied)
    Tcl_DecrRefCount(sth->obj);
}

Tcl_Obj *cht_ret_long(Tcl_Interp *ip, long val) {
  return Tcl_NewLongObj(val);
}

Tcl_Obj *cht_ret_string(Tcl_Interp *ip, const char *val) {
  return Tcl_NewStringObj(val,-1);
}