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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* alarm [-r] [varname [+]when]
*
* David Korn
* AT&T Labs
*
*/
#include "defs.h"
#include <error.h>
#include <stak.h>
#include "builtins.h"
#include "FEATURE/time"
#define R_FLAG 1
#define L_FLAG 2
struct tevent
{
Namfun_t fun;
Namval_t *node;
Namval_t *action;
struct tevent *next;
long milli;
int flags;
void *timeout;
Shell_t *sh;
};
static const char ALARM[] = "alarm";
static void trap_timeout(void*);
/*
* insert timeout item on current given list in sorted order
*/
static void *time_add(struct tevent *item, void *list)
{
register struct tevent *tp = (struct tevent*)list;
if(!tp || item->milli < tp->milli)
{
item->next = tp;
list = (void*)item;
}
else
{
while(tp->next && item->milli > tp->next->milli)
tp = tp->next;
item->next = tp->next;
tp->next = item;
}
tp = item;
tp->timeout = (void*)sh_timeradd(tp->milli,tp->flags&R_FLAG,trap_timeout,(void*)tp);
return(list);
}
/*
* delete timeout item from current given list, delete timer
*/
static void *time_delete(register struct tevent *item, void *list)
{
register struct tevent *tp = (struct tevent*)list;
if(item==tp)
list = (void*)tp->next;
else
{
while(tp && tp->next != item)
tp = tp->next;
if(tp)
tp->next = item->next;
}
if(item->timeout)
timerdel((void*)item->timeout);
return(list);
}
static void print_alarms(void *list)
{
register struct tevent *tp = (struct tevent*)list;
while(tp)
{
if(tp->timeout)
{
register char *name = nv_name(tp->node);
if(tp->flags&R_FLAG)
{
double d = tp->milli;
sfprintf(sfstdout,e_alrm1,name,d/1000.);
}
else
sfprintf(sfstdout,e_alrm2,name,nv_getnum(tp->node));
}
tp = tp->next;
}
}
static void trap_timeout(void* handle)
{
register struct tevent *tp = (struct tevent*)handle;
tp->sh->trapnote |= SH_SIGALRM;
if(!(tp->flags&R_FLAG))
tp->timeout = 0;
tp->flags |= L_FLAG;
tp->sh->sigflag[SIGALRM] |= SH_SIGALRM;
if(sh_isstate(SH_TTYWAIT))
sh_timetraps();
}
void sh_timetraps(void)
{
register struct tevent *tp, *tpnext;
register struct tevent *tptop;
while(1)
{
sh.sigflag[SIGALRM] &= ~SH_SIGALRM;
tptop= (struct tevent*)sh.st.timetrap;
for(tp=tptop;tp;tp=tpnext)
{
tpnext = tp->next;
if(tp->flags&L_FLAG)
{
tp->flags &= ~L_FLAG;
if(tp->action)
sh_fun(tp->action,tp->node,(char**)0);
tp->flags &= ~L_FLAG;
if(!tp->flags)
{
nv_unset(tp->node);
nv_close(tp->node);
}
}
}
if(!(sh.sigflag[SIGALRM]&SH_SIGALRM))
break;
}
}
/*
* This trap function catches "alarm" actions only
*/
static char *setdisc(Namval_t *np, const char *event, Namval_t* action, Namfun_t
*fp)
{
register struct tevent *tp = (struct tevent*)fp;
if(!event)
return(action?"":(char*)ALARM);
if(strcmp(event,ALARM)!=0)
{
/* try the next level */
return(nv_setdisc(np, event, action, fp));
}
if(action==np)
action = tp->action;
else
tp->action = action;
return(action?(char*)action:"");
}
/*
* catch assignments and set alarm traps
*/
static void putval(Namval_t* np, const char* val, int flag, Namfun_t* fp)
{
register struct tevent *tp;
register double d;
if(val)
{
double now;
#ifdef timeofday
struct timeval tmp;
timeofday(&tmp);
now = tmp.tv_sec + 1.e-6*tmp.tv_usec;
#else
now = (double)time(NIL(time_t*));
#endif /* timeofday */
nv_putv(np,val,flag,fp);
d = nv_getnum(np);
tp = (struct tevent*)fp;
if(*val=='+')
{
double x = d + now;
nv_putv(np,(char*)&x,NV_INTEGER,fp);
}
else
d -= now;
tp->milli = 1000*(d+.0005);
if(tp->timeout)
sh.st.timetrap = time_delete(tp,sh.st.timetrap);
if(tp->milli > 0)
sh.st.timetrap = time_add(tp,sh.st.timetrap);
}
else
{
tp = (struct tevent*)nv_stack(np, (Namfun_t*)0);
sh.st.timetrap = time_delete(tp,sh.st.timetrap);
if(tp->action)
nv_close(tp->action);
nv_unset(np);
free((void*)fp);
}
}
static const Namdisc_t alarmdisc =
{
sizeof(struct tevent),
putval,
0,
0,
setdisc,
};
int b_alarm(int argc,char *argv[],void *extra)
{
register int n,rflag=0;
register Namval_t *np;
register struct tevent *tp;
register Shell_t *shp = ((Shbltin_t*)extra)->shp;
while (n = optget(argv, sh_optalarm)) switch (n)
{
case 'r':
rflag = R_FLAG;
break;
case ':':
errormsg(SH_DICT,2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT,ERROR_usage(2), "%s", opt_info.arg);
break;
}
argc -= opt_info.index;
argv += opt_info.index;
if(error_info.errors)
errormsg(SH_DICT,ERROR_usage(2),optusage((char*)0));
if(argc==0)
{
print_alarms(shp->st.timetrap);
return(0);
}
if(argc!=2)
errormsg(SH_DICT,ERROR_usage(2),optusage((char*)0));
np = nv_open(argv[0],shp->var_tree,NV_NOARRAY|NV_VARNAME|NV_NOASSIGN);
if(!nv_isnull(np))
nv_unset(np);
nv_setattr(np, NV_DOUBLE);
if(!(tp = newof(NIL(struct tevent*),struct tevent,1,0)))
errormsg(SH_DICT,ERROR_exit(1),e_nospace);
tp->fun.disc = &alarmdisc;
tp->flags = rflag;
tp->node = np;
tp->sh = shp;
nv_stack(np,(Namfun_t*)tp);
nv_putval(np, argv[1], 0);
return(0);
}
|