mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
So far we've been handling AST release build and git commit flags
and ksh SHOPT_* compile time options in the generic package build
script. That was a hack that was necessary before I had sufficient
understanding of the build system. Some of it did not work very
well, e.g. the correct git commit did not show up in ${.sh.version}
when compiling from a git repo.
As of this commit, this is properly included in the mamake
dependency tree by handling it from the libast and ksh93 Mamfiles,
guaranteeing they are properly up to date.
For a release build, the _AST_ksh_release macro is renamed to
_AST_release, because some aspects of libast also use this.
This commit also adds my first attempt at documenting the (very
simple, six-command) mamake language as it is currently implemented
-- which is significantly different from Glenn Fowler's original
paper. This is mostly based on reading the mamake.c source code.
src/cmd/INIT/README-mamake.md:
- Added.
bin/package, src/cmd/INIT/package.sh:
- Delete the hack.
**/Mamfile:
- Remove KSH_RELFLAGS and KSH_SHOPTFLAGS, which supported the hack.
- Delete 'meta' commands. They were no-ops; mamake.c ignores them.
They also did not add any informative value.
src/lib/libast/Mamfile:
- Add a 'virtual' target that obtains the current git commit,
examines the git branch, and decides whether to auto-set an
_AST_git_commit and/or or _AST_release #define to a new
releaseflags.h header file. This is overwritten on each run.
- Add code to the install target that copies limit.h to
include/ast, but only if it doesn't exist or the content of the
original changed. This allows '#include <releaseflags.h>' from
any program using libast while avoiding needless recompiles.
- When there are uncommitted changes, add /MOD (modified) to the
commit hash instead of not defining it at all.
src/cmd/ksh93/**:
- Mamfile: Add a shopt.h target that reads SHOPT.sh and converts it
into a new shopt.h header file in the object code directory. The
shopt.h header only contains SHOPT_* directives that have a value
in SHOPT.sh (not the empty/probe ones). They also do not redefine
the macros if they already exist, so overriding with something
like CCFLAGS+=' -DSHOPT_FOO=1' remains possible.
- **.c: Every c file now #includes "shopt.h" first. So SHOPT_*
macros are no longer passed via environment/MAM variables.
* SHOPT.sh: The AUDITFILE and CMDLIB_DIR macros no longer need an
extra backslash-escape for the double quotes in their values.
(The old way required this because mamake inserts MAM variables
directly into shell scripts as literals without quoting. :-/ )
src/cmd/INIT/mamake.c:
- Import the two minor changes between from 93u+ and 93v-: bind()
is renamed to bindfile() and there is a tweak to detecting an
"improper done statement".
- Allow arbitrary whitespace (isspace()) everywhere, instead of
spaces only. This obsoletes my earlier indentation workaround
from 6cc2f6a0
; turns out mamake always supported indentation, but
with spaces only.
- Do not skip line numbers at the beginning of each line. This
undocumented feature is not and (AFAICT) never has been used.
- Throw an error on unknown command or rule attribute. Quite an
important feature for manual maintenance: catches typos, etc.
281 lines
6.7 KiB
C
281 lines
6.7 KiB
C
/***********************************************************************
|
|
* *
|
|
* This software is part of the ast package *
|
|
* Copyright (c) 1982-2012 AT&T Intellectual Property *
|
|
* Copyright (c) 2020-2022 Contributors to ksh 93u+m *
|
|
* and is licensed under the *
|
|
* Eclipse Public License, Version 1.0 *
|
|
* by AT&T Intellectual Property *
|
|
* *
|
|
* A copy of the License is available at *
|
|
* http://www.eclipse.org/org/documents/epl-v10.html *
|
|
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
|
|
* *
|
|
* Information and Software Systems Research *
|
|
* AT&T Research *
|
|
* Florham Park NJ *
|
|
* *
|
|
* David Korn <dgk@research.att.com> *
|
|
* *
|
|
***********************************************************************/
|
|
/*
|
|
* alarm [-r] [varname [+]when]
|
|
*
|
|
* David Korn
|
|
* AT&T Labs
|
|
*
|
|
*/
|
|
|
|
/*
|
|
* TODO: 2014 email from David Korn cited at <https://bugzilla.redhat.com/1176670>:
|
|
*
|
|
* > I never documented the alarm builtin because it is problematic. The
|
|
* > problem is that traps can't safely be handled asynchronously. What should
|
|
* > happen is that the trap is marked for execution (sh.trapnote) and run after
|
|
* > the current command completes. The time trap should wake up the shell if
|
|
* > it is blocked and it should return and then handle the trap.
|
|
*/
|
|
|
|
#include "shopt.h"
|
|
#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;
|
|
};
|
|
|
|
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;
|
|
sh.trapnote |= SH_SIGALRM;
|
|
if(!(tp->flags&R_FLAG))
|
|
tp->timeout = 0;
|
|
tp->flags |= L_FLAG;
|
|
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);
|
|
}
|
|
}
|
|
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 ? Empty : (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 : Empty);
|
|
}
|
|
|
|
/*
|
|
* catch assignments and set alarm traps
|
|
*/
|
|
static void putval(Namval_t* np, const char* val, int flag, Namfun_t* fp)
|
|
{
|
|
register struct tevent *tp = (struct tevent*)fp;
|
|
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);
|
|
if(*val=='+')
|
|
{
|
|
double x = d + now;
|
|
nv_putv(np,(char*)&x,NV_INTEGER|NV_DOUBLE,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);
|
|
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[],Shbltin_t *context)
|
|
{
|
|
register int n,rflag=0;
|
|
register Namval_t *np;
|
|
register struct tevent *tp;
|
|
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);
|
|
UNREACHABLE();
|
|
}
|
|
argc -= opt_info.index;
|
|
argv += opt_info.index;
|
|
if(error_info.errors)
|
|
{
|
|
errormsg(SH_DICT,ERROR_usage(2),optusage((char*)0));
|
|
UNREACHABLE();
|
|
}
|
|
if(argc==0)
|
|
{
|
|
print_alarms(sh.st.timetrap);
|
|
return(0);
|
|
}
|
|
if(argc!=2)
|
|
{
|
|
errormsg(SH_DICT,ERROR_usage(2),optusage((char*)0));
|
|
UNREACHABLE();
|
|
}
|
|
np = nv_open(argv[0],sh.var_tree,NV_NOARRAY|NV_VARNAME|NV_NOASSIGN);
|
|
if(!nv_isnull(np))
|
|
nv_unset(np);
|
|
nv_setattr(np, NV_DOUBLE);
|
|
tp = sh_newof(NIL(struct tevent*),struct tevent,1,0);
|
|
tp->fun.disc = &alarmdisc;
|
|
tp->flags = rflag;
|
|
tp->node = np;
|
|
nv_stack(np,(Namfun_t*)tp);
|
|
nv_putval(np, argv[1], 0);
|
|
return(0);
|
|
}
|