Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / cmd / ksh93 / bltins / alarm.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: alarm.c /main/3 1995/11/01 16:27:14 rswiston $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
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             *
32 *                                                              *
33 *                Copyright (c) 1995 AT&T Corp.                 *
34 *              Unpublished & Not for Publication               *
35 *                     All Rights Reserved                      *
36 *                                                              *
37 *       The copyright notice above does not evidence any       *
38 *      actual or intended publication of such source code      *
39 *                                                              *
40 *               This software was created by the               *
41 *           Advanced Software Technology Department            *
42 *                    AT&T Bell Laboratories                    *
43 *                                                              *
44 *               For further information contact                *
45 *                    {research,attmail}!dgk                    *
46 *                                                              *
47 ***************************************************************/
48
49 /* : : generated by proto : : */
50
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__      "C"
55 #else
56 #define __MANGLE__
57 #endif
58 #define __STDARG__
59 #define __PROTO__(x)    x
60 #define __OTORP__(x)
61 #define __PARAM__(n,o)  n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
64 #define const
65 #endif
66 #define signed
67 #define void            int
68 #define volatile
69 #define __V_            char
70 #else
71 #define __V_            void
72 #endif
73 #else
74 #define __PROTO__(x)    ()
75 #define __OTORP__(x)    x
76 #define __PARAM__(n,o)  o
77 #define __MANGLE__
78 #define __V_            char
79 #define const
80 #define signed
81 #define void            int
82 #define volatile
83 #endif
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__      ...
86 #else
87 #define __VARARG__
88 #endif
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a)       va_start(p,a)
91 #else
92 #define __VA_START__(p,a)       va_start(p)
93 #endif
94 #endif
95 #include        "defs.h"
96 #include        <error.h>
97 #include        <stak.h>
98 #include        "builtins.h"
99 #include        "FEATURE/time"
100
101 #define R_FLAG  1
102 #define L_FLAG  2
103
104 struct  tevent
105 {
106         Namfun_t        fun;
107         Namval_t        *node;
108         Namval_t        *action;
109         struct tevent   *next;
110         long            milli;
111         int             flags;
112         __V_            *timeout;
113 };
114
115 static const char ALARM[] = "alarm";
116
117 static void     trap_timeout __PROTO__((__V_*));
118
119 /*
120  * insert timeout item on current given list in sorted order
121  */
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)
125         {
126                 item->next = tp;
127                 list = (__V_*)item;
128         }
129         else
130         {
131                 while(tp->next && item->milli > tp->next->milli)
132                         tp = tp->next;
133                 item->next = tp->next;
134                 tp->next = item;
135         }
136         tp = item;
137         tp->timeout = (__V_*)timeradd(tp->milli,tp->flags&R_FLAG,trap_timeout,(__V_*)tp);
138         return(list);
139 }
140
141 /*
142  * delete timeout item from current given list, delete timer
143  */
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;
146         if(item==tp)
147                 list = (__V_*)tp->next;
148         else
149         {
150                 while(tp && tp->next != item)
151                         tp = tp->next;
152                 if(tp)
153                         tp->next = item->next;
154         }
155         if(item->timeout)
156                 timerdel((__V_*)item->timeout);
157         return(list);
158 }
159
160 static void     print_alarms __PARAM__((__V_ *list), (list)) __OTORP__(__V_ *list;){
161         register struct tevent *tp = (struct tevent*)list;
162         while(tp)
163         {
164                 if(tp->timeout)
165                 {
166                         register char *name = nv_name(tp->node);
167                         if(tp->flags&R_FLAG)
168                         {
169                                 double d = tp->milli;
170                                 sfprintf(sfstdout,e_alrm1,name,d/1000.);
171                         }
172                         else
173                                 sfprintf(sfstdout,e_alrm2,name,nv_getnum(tp->node));
174                 }
175                 tp = tp->next;
176         }
177 }
178
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))
183                 tp->timeout = 0;
184         tp->flags |= L_FLAG;
185         sh.sigflag[SIGALRM] |= SH_SIGTRAP;
186         if(sh_isstate(SH_TTYWAIT))
187                 sh_timetraps();
188 }
189
190 void    sh_timetraps __PARAM__((void), ()){
191         register struct tevent *tp, *tpnext;
192         register struct tevent *tptop;
193         while(1)
194         {
195                 sh.sigflag[SIGALRM] &= ~SH_SIGTRAP;
196                 tptop= (struct tevent*)sh.st.timetrap;
197                 for(tp=tptop;tp;tp=tpnext)
198                 {
199                         tpnext = tp->next;
200                         if(tp->flags&L_FLAG)
201                         {
202                                 tp->flags &= ~L_FLAG;
203                                 if(tp->action)
204                                         sh_fun(tp->action,tp->node);
205                                 tp->flags &= ~L_FLAG;
206                                 if(!tp->flags)
207                                 {
208                                         nv_unset(tp->node);
209                                         nv_close(tp->node);
210                                 }
211                         }
212                 }
213                 if(!(sh.sigflag[SIGALRM]&SH_SIGTRAP))
214                         break;
215         }
216 }
217
218
219 /*
220  * This trap function catches "alarm" actions only
221  */
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
224  *fp;){
225         register struct tevent *tp = (struct tevent*)fp;
226         if(!event)
227                 return(action?"":(char*)ALARM);
228         if(strcmp(event,ALARM)!=0)
229         {
230                 /* try the next level */
231                 return(nv_setdisc(np, event, action, fp));
232         }
233         if(action==np)
234                 action = tp->action;
235         else
236                 tp->action = action;
237         return(action?(char*)action:"");
238 }
239
240 /*
241  * catch assignments and set alarm traps
242  */
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;
245         register double d;
246         if(val)
247         {
248                 double now;
249 #ifdef _lib_gettimeofday
250                 struct timeval tmp;
251                 gettimeofday(&tmp,NIL(void *));
252                 now = tmp.tv_sec + 1.e-6*tmp.tv_usec;
253 #else
254                 now = (double)time(NIL(time_t*));
255 #endif /*_lib_gettimeofday */
256                 nv_putv(np,val,flag,fp);
257                 d = nv_getnum(np);
258                 tp = (struct tevent*)fp;
259                 if(*val=='+')
260                 {
261                         double x = d + now;
262                         nv_putv(np,(char*)&x,NV_INTEGER,fp);
263                 }
264                 else
265                         d -= now;
266                 tp->milli = 1000*(d+.0005);
267                 if(tp->timeout)
268                         sh.st.timetrap = time_delete(tp,sh.st.timetrap);
269                 if(tp->milli > 0)
270                         sh.st.timetrap = time_add(tp,sh.st.timetrap);
271         }
272         else
273         {
274                 tp = (struct tevent*)nv_stack(np, (Namfun_t*)0);
275                 sh.st.timetrap = time_delete(tp,sh.st.timetrap);
276                 if(tp->action)
277                         nv_close(tp->action);
278                 nv_unset(np);
279                 free((__V_*)fp);
280         }
281 }
282
283 static const Namdisc_t alarmdisc =
284 {
285         sizeof(struct tevent),
286         putval,
287         0,
288         0,
289         setdisc,
290 };
291
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;
296         NOT_USED(extra);
297         while (n = optget(argv, sh_optalarm)) switch (n)
298         {
299             case 'r':
300                 rflag = R_FLAG;
301                 break;
302             case ':':
303                 error(2, opt_arg);
304                 break;
305             case '?':
306                 error(ERROR_usage(2), opt_arg);
307                 break;
308         }
309         argc -= opt_index;
310         argv += opt_index;
311         if(error_info.errors)
312                 error(ERROR_usage(2),optusage((char*)0));
313         if(argc==0)
314         {
315                 print_alarms(sh.st.timetrap);
316                 return(0);
317         }
318         if(argc!=2)
319                 error(ERROR_usage(2),optusage((char*)0));
320         np = nv_open(argv[0],sh.var_tree,NV_ARRAY|NV_VARNAME|NV_NOASSIGN);
321         if(!nv_isnull(np))
322                 nv_unset(np);
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;
327         tp->flags = rflag;
328         tp->node = np;
329         nv_stack(np,(Namfun_t*)tp);
330         nv_putval(np, argv[1], 0);
331         return(0);
332 }
333