File: otclAppInit.c

package info (click to toggle)
otcl 1.14~RC2+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 792 kB
  • ctags: 182
  • sloc: sh: 2,825; ansic: 1,949; tcl: 825; makefile: 114; perl: 49
file content (254 lines) | stat: -rw-r--r-- 6,425 bytes parent folder | download | duplicates (5)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* 
 * tclAppInit.c --
 *
 *	Provides a default version of the Tcl_AppInit procedure.
 *
 * Copyright (c) 1993 The Regents of the University of California.
 * Copyright (c) 1994 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#ifndef lint
/* static char sccsid[] = "@(#) tclAppInit.c 1.11 94/12/17 16:14:03"; */
#endif /* not lint */

#include <tcl.h>
#include <otcl.h>

#if TCL_MAJOR_VERSION < 7
  #error Tcl distribution TOO OLD
#endif


#ifdef TESTCAPI

#include <stdlib.h>
#include <time.h>

typedef struct {
  time_t started;
  int prior;
} timerdata;


#ifdef STATIC_LIB
#include <tclWinInt.h>
#include <stdlib.h>
extern BOOL APIENTRY
Tcl_LibMain(HINSTANCE hInstance,DWORD reason,LPVOID reserved);

/* procedure to call before exiting to clean up */
void static_exit(void){
	HINSTANCE hInstance=TclWinGetTclInstance();
	Tcl_LibMain(hInstance, DLL_PROCESS_DETACH, NULL);
}
#endif

static int
TimerInit(ClientData cd, Tcl_Interp* in, int argc, char*argv[]) {
  struct OTclObject* timer = OTclAsObject(in, cd);
  struct OTclClass* tcl = OTclGetClass(in, "Timer");
  timerdata* data;

  if (!timer || !tcl) return TCL_ERROR;
  data = (timerdata*)ckalloc(sizeof(timerdata));
  data->started = time(0);
  data->prior = 0;
  (void)OTclSetObjectData(timer, tcl, (ClientData)data);
  if (!OTclSetInstVar(timer, in, "running", "0", TCL_LEAVE_ERR_MSG))
    return TCL_ERROR;
  return OTclNextMethod(timer, in, argc, argv);
}


static int
TimerDestroy(ClientData cd, Tcl_Interp* in, int argc, char*argv[]) {
  struct OTclObject* timer = OTclAsObject(in, cd);
  struct OTclClass* tcl = OTclGetClass(in, "Timer");
  timerdata* data;

  if (!timer || !tcl) return TCL_ERROR;
  if (!OTclGetObjectData(timer, tcl, (ClientData*)(&data))) return TCL_ERROR;

  (void)OTclUnsetObjectData(timer, tcl);
  ckfree(data);
  return OTclNextMethod(timer, in, argc, argv);
}


static int
TimerStart(ClientData cd, Tcl_Interp* in, int argc, char*argv[]) {
  struct OTclObject* timer = OTclAsObject(in, cd);
  struct OTclClass* tcl = OTclGetClass(in, "Timer");
  timerdata* data;

  if (!timer || !tcl || argc>5) return TCL_ERROR;
  if (!OTclGetObjectData(timer, tcl, (ClientData*)(&data))) return TCL_ERROR;  

  if (data->started == 0) data->started = time(0);
  if (!OTclSetInstVar(timer, in, "running", "1", TCL_LEAVE_ERR_MSG))
    return TCL_ERROR;  
  return TCL_OK;
}


static int
TimerRead(ClientData cd, Tcl_Interp* in, int argc, char*argv[]) {
  struct OTclObject* timer = OTclAsObject(in, cd);
  struct OTclClass* tcl = OTclGetClass(in, "Timer");
  timerdata* data;
  char val[20];
  int total;

  if (!timer || !tcl || argc>5) return TCL_ERROR;
  if (!OTclGetObjectData(timer, tcl, (ClientData*)(&data))) return TCL_ERROR;
  
  total = data->prior;
  if (data->started) total += (int)(time(0) - data->started);
  (void)sprintf(val, "%d", total);
  Tcl_SetResult(in, val, TCL_VOLATILE);
  return TCL_OK;
}


static int
TimerStop(ClientData cd, Tcl_Interp* in, int argc, char*argv[]) {
  struct OTclObject* timer = OTclAsObject(in, cd);
  struct OTclClass* tcl = OTclGetClass(in, "Timer");
  timerdata* data;

  if (!timer || !tcl || argc>5) return TCL_ERROR;
  if (!OTclGetObjectData(timer, tcl, (ClientData*)(&data))) return TCL_ERROR;

  if (data->started != 0) {
    data->prior += (int)(time(0) - data->started);
    data->started = 0;
  }
  return TCL_OK;
}


static int
TestCAPI_Init(Tcl_Interp* in) {
  struct OTclClass* class = OTclGetClass(in, "Class");
  struct OTclClass* object = OTclGetClass(in, "Object");
  struct OTclClass* timer;
  struct OTclObject* dawn;
 
  if (!class || !object) return TCL_ERROR;
  timer = OTclCreateClass(in, "Timer", class);
  if (!timer) return TCL_ERROR;
  OTclAddIMethod(timer, "start", TimerStart, 0, 0);
  OTclAddIMethod(timer, "read", TimerRead, 0, 0);
  OTclAddIMethod(timer, "stop", TimerStop, 0, 0);
  OTclAddIMethod(timer, "init", TimerInit, 0, 0);
  OTclAddIMethod(timer, "destroy", TimerDestroy, 0, 0);

  dawn = OTclCreateObject(in, "dawnoftime", timer);
  if (!dawn) return TCL_ERROR;
  if (Tcl_Eval(in, "dawnoftime start") != TCL_OK) return TCL_ERROR;
  return TCL_OK;
}

#endif


/*
 * The following variable is a special hack that is needed in order for
 * Sun shared libraries to be used for Tcl.
 */

#ifdef NEED_MATHERR
extern int matherr();
int *tclDummyMathPtr = (int *) matherr;
#endif

/*
 *----------------------------------------------------------------------
 *
 * main --
 *
 *	This is the main program for the application.
 *
 * Results:
 *	None: Tcl_Main never returns here, so this procedure never
 *	returns either.
 *
 * Side effects:
 *	Whatever the application does.
 *
 *----------------------------------------------------------------------
 */

#if TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION < 4

extern int main();
int *tclDummyMainPtr = (int *) main;

#else

int
main(argc, argv)
    int argc;			/* Number of command-line arguments. */
    char **argv;		/* Values of command-line arguments. */
{
#ifdef STATIC_LIB
    HINSTANCE hInstance=GetModuleInstance(NULL);
    Tcl_LibMain(hInstance, DLL_PROCESS_ATTACH, NULL);
    atexit(static_exit);
#endif

    Tcl_Main(argc, argv, Tcl_AppInit);
    return 0;			/* Needed only to prevent compiler warning. */
}

#endif


/*
 *----------------------------------------------------------------------
 *
 * Tcl_AppInit --
 *
 *	This procedure performs application-specific initialization.
 *	Most applications, especially those that incorporate additional
 *	packages, will have their own version of this procedure.
 *
 * Results:
 *	Returns a standard Tcl completion code, and leaves an error
 *	message in interp->result if an error occurs.
 *
 * Side effects:
 *	Depends on the startup script.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_AppInit(interp)
    Tcl_Interp *interp;		/* Interpreter for application. */
{
    if (Tcl_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
    }

    if (Otcl_Init(interp) == TCL_ERROR) {
      return TCL_ERROR;
    }

#ifdef TESTCAPI
    if (TestCAPI_Init(interp) == TCL_ERROR) {
      return TCL_ERROR;
    }
#endif

#if TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION < 5
    tcl_RcFileName = "~/.tclshrc";
#else
    Tcl_SetVar(interp, "tcl_rcFileName", "~/.tclshrc", TCL_GLOBAL_ONLY);
#endif

     return TCL_OK;
}