2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: alarm.c /main/3 1995/11/01 16:27:14 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF *
29 * AT&T BELL LABORATORIES *
30 * AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN *
31 * ACCORDANCE WITH APPLICABLE AGREEMENTS *
33 * Copyright (c) 1995 AT&T Corp. *
34 * Unpublished & Not for Publication *
35 * All Rights Reserved *
37 * The copyright notice above does not evidence any *
38 * actual or intended publication of such source code *
40 * This software was created by the *
41 * Advanced Software Technology Department *
42 * AT&T Bell Laboratories *
44 * For further information contact *
45 * {research,attmail}!dgk *
47 ***************************************************************/
49 /* : : generated by proto : : */
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__ "C"
59 #define __PROTO__(x) x
61 #define __PARAM__(n,o) n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
74 #define __PROTO__(x) ()
75 #define __OTORP__(x) x
76 #define __PARAM__(n,o) o
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__ ...
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a) va_start(p,a)
92 #define __VA_START__(p,a) va_start(p)
99 #include "FEATURE/time"
115 static const char ALARM[] = "alarm";
117 static void trap_timeout __PROTO__((__V_*));
120 * insert timeout item on current given list in sorted order
122 static __V_ *time_add __PARAM__((struct tevent *item, __V_ *list), (item, list)) __OTORP__(struct tevent *item; __V_ *list;){
123 register struct tevent *tp = (struct tevent*)list;
124 if(!tp || item->milli < tp->milli)
131 while(tp->next && item->milli > tp->next->milli)
133 item->next = tp->next;
137 tp->timeout = (__V_*)kshtimeradd(tp->milli,tp->flags&R_FLAG,trap_timeout,(__V_*)tp);
142 * delete timeout item from current given list, delete timer
144 static __V_ *time_delete __PARAM__((register struct tevent *item, __V_ *list), (item, list)) __OTORP__(register struct tevent *item; __V_ *list;){
145 register struct tevent *tp = (struct tevent*)list;
147 list = (__V_*)tp->next;
150 while(tp && tp->next != item)
153 tp->next = item->next;
156 timerdel((__V_*)item->timeout);
160 static void print_alarms __PARAM__((__V_ *list), (list)) __OTORP__(__V_ *list;){
161 register struct tevent *tp = (struct tevent*)list;
166 register char *name = nv_name(tp->node);
169 double d = tp->milli;
170 sfprintf(sfstdout,e_alrm1,name,d/1000.);
173 sfprintf(sfstdout,e_alrm2,name,nv_getnum(tp->node));
179 static void trap_timeout __PARAM__((__V_* handle), (handle)) __OTORP__(__V_* handle;){
180 register struct tevent *tp = (struct tevent*)handle;
181 sh.trapnote |= SH_SIGTRAP;
182 if(!(tp->flags&R_FLAG))
185 sh.sigflag[SIGALRM] |= SH_SIGTRAP;
186 if(sh_isstate(SH_TTYWAIT))
190 void sh_timetraps __PARAM__((void), ()){
191 register struct tevent *tp, *tpnext;
192 register struct tevent *tptop;
195 sh.sigflag[SIGALRM] &= ~SH_SIGTRAP;
196 tptop= (struct tevent*)sh.st.timetrap;
197 for(tp=tptop;tp;tp=tpnext)
202 tp->flags &= ~L_FLAG;
204 sh_fun(tp->action,tp->node);
205 tp->flags &= ~L_FLAG;
213 if(!(sh.sigflag[SIGALRM]&SH_SIGTRAP))
220 * This trap function catches "alarm" actions only
222 static char *setdisc __PARAM__((Namval_t *np, const char *event, Namval_t* action, Namfun_t
223 *fp), (np, event, action, fp)) __OTORP__(Namval_t *np; const char *event; Namval_t* action; Namfun_t
225 register struct tevent *tp = (struct tevent*)fp;
227 return(action?"":(char*)ALARM);
228 if(strcmp(event,ALARM)!=0)
230 /* try the next level */
231 return(nv_setdisc(np, event, action, fp));
237 return(action?(char*)action:"");
241 * catch assignments and set alarm traps
243 static void putval __PARAM__((Namval_t* np, const char* val, int flag, Namfun_t* fp), (np, val, flag, fp)) __OTORP__(Namval_t* np; const char* val; int flag; Namfun_t* fp;){
244 register struct tevent *tp;
249 #ifdef _lib_gettimeofday
251 gettimeofday(&tmp,NIL(void *));
252 now = tmp.tv_sec + 1.e-6*tmp.tv_usec;
254 now = (double)time(NIL(time_t*));
255 #endif /*_lib_gettimeofday */
256 nv_putv(np,val,flag,fp);
258 tp = (struct tevent*)fp;
262 nv_putv(np,(char*)&x,NV_INTEGER,fp);
266 tp->milli = 1000*(d+.0005);
268 sh.st.timetrap = time_delete(tp,sh.st.timetrap);
270 sh.st.timetrap = time_add(tp,sh.st.timetrap);
274 tp = (struct tevent*)nv_stack(np, (Namfun_t*)0);
275 sh.st.timetrap = time_delete(tp,sh.st.timetrap);
277 nv_close(tp->action);
283 static const Namdisc_t alarmdisc =
285 sizeof(struct tevent),
292 int b_alarm __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
293 register int n,rflag=0;
294 register Namval_t *np;
295 register struct tevent *tp;
297 while (n = optget(argv, sh_optalarm)) switch (n)
306 error(ERROR_usage(2), opt_arg);
311 if(error_info.errors)
312 error(ERROR_usage(2),optusage((char*)0));
315 print_alarms(sh.st.timetrap);
319 error(ERROR_usage(2),optusage((char*)0));
320 np = nv_open(argv[0],sh.var_tree,NV_ARRAY|NV_VARNAME|NV_NOASSIGN);
323 nv_setattr(np, NV_INTEGER|NV_DOUBLE);
324 if(!(tp = newof(NIL(struct tevent*),struct tevent,1,0)))
325 error(ERROR_exit(1),e_nospace);
326 tp->fun.disc = &alarmdisc;
329 nv_stack(np,(Namfun_t*)tp);
330 nv_putval(np, argv[1], 0);