Example #1
0
void
execshift(void)
{
	int n;
	word *a;
	var *star;
	switch(count(runq->argv->words)){
	default:
		pfmt(err, "Usage: shift [n]\n");
		setstatus("shift usage");
		poplist();
		return;
	case 2:
		n = atoi(runq->argv->words->next->word);
		break;
	case 1:
		n = 1;
		break;
	}
	star = vlook("*");
	for(;n && star->val;--n){
		a = star->val->next;
		efree(star->val->word);
		efree((char *)star->val);
		star->val = a;
		star->changed = 1;
	}
	setstatus("");
	poplist();
}
Example #2
0
void
Xsimple(void)
{
	word *a;
	thread *p = runq;
	var *v;
	struct builtin *bp;
	int pid;
	globlist();
	a = runq->argv->words;
	if(a==0){
		Xerror1("empty argument list");
		return;
	}
	if(flag['x'])
		pfmt(err, "%v\n", p->argv->words); /* wrong, should do redirs */
	v = gvlook(a->word);
	if(v->fn)
		execfunc(v);
	else{
		if(strcmp(a->word, "builtin")==0){
			if(count(a)==1){
				pfmt(err, "builtin: empty argument list\n");
				setstatus("empty arg list");
				poplist();
				return;
			}
			a = a->next;
			popword();
		}
		for(bp = Builtin;bp->name;bp++)
			if(strcmp(a->word, bp->name)==0){
				(*bp->fnc)();
				return;
			}
		if(exitnext()){
			/* fork and wait is redundant */
			pushword("exec");
			execexec();
			Xexit();
		}
		else{
			flush(err);
			Updenv();	/* necessary so changes don't go out again */
			if((pid = execforkexec()) < 0){
				Xerror("try again");
				return;
			}

			/* interrupts don't get us out */
			poplist();
			while(Waitfor(pid, 1) < 0)
				;
		}
	}
}
Example #3
0
void
Xlocal(void)
{
	if(count(runq->argv->words)!=1){
		Xerror1("variable name must be singleton\n");
		return;
	}
	deglob(runq->argv->words->word);
	runq->local = newvar(strdup(runq->argv->words->word), runq->local);
	runq->local->val = copywords(runq->argv->next->words, (word *)0);
	runq->local->changed = 1;
	poplist();
	poplist();
}
Example #4
0
File: plan9.c Project: aahud/harvey
void
execnewpgrp(void)
{
	int arg;
	char *s;
	switch(count(runq->argv->words)){
	case 1:
		arg = RFENVG|RFNAMEG|RFNOTEG;
		break;
	case 2:
		arg = 0;
		for(s = runq->argv->words->next->word;*s;s++) switch(*s){
		default:
			goto Usage;
		case 'n':
			arg|=RFNAMEG;  break;
		case 'N':
			arg|=RFCNAMEG;
			break;
		case 'm':
			arg|=RFNOMNT;  break;
		case 'e':
			arg|=RFENVG;   break;
		case 'E':
			arg|=RFCENVG;  break;
		case 's':
			arg|=RFNOTEG;  break;
		case 'f':
			arg|=RFFDG;    break;
		case 'F':
			arg|=RFCFDG;   break;
		}
		break;
	default:
	Usage:
		pfmt(err, "Usage: %s [fnesFNEm]\n", runq->argv->words->word);
		setstatus("rfork usage");
		poplist();
		return;
	}
	if(rfork(arg)==-1){
		pfmt(err, "rc: %s failed\n", runq->argv->words->word);
		setstatus("rfork failed");
	}
	else
		setstatus("");
	poplist();
}
Example #5
0
FrTextSpans::FrTextSpans(const FrTextSpans *old)
{
   clear() ;
   if (old)
      {
      m_text = FrDupString(old->m_text) ;
      setPositionMap() ;
      if (m_text && m_positions)
	 {
	 FrList *fields = old->m_metadata.fieldNames() ;
	 while (fields)
	    {
	    FrSymbol *key = (FrSymbol*)poplist(fields) ;
	    setMetaData(key,old->metaData(key)) ;
	    }
	 }
      else
	 {
	 FrFree(m_text) ;	m_text = 0 ;
	 FrFree(m_positions) ;	m_positions = 0 ;
	 m_textlength = 0 ;
	 }
      }
   return ;
}
Example #6
0
void
Xcount(void)
{
	word *a;
	char *s, *t;
	int n;
	char num[12];
	if(count(runq->argv->words)!=1){
		Xerror1("variable name not singleton!");
		return;
	}
	s = runq->argv->words->word;
	deglob(s);
	n = 0;
	for(t = s;'0'<=*t && *t<='9';t++) n = n*10+*t-'0';
	if(n==0 || *t){
		a = vlook(s)->val;
		inttoascii(num, count(a));
	}
	else{
		a = vlook("*")->val;
		inttoascii(num, a && 1<=n && n<=count(a)?1:0);
	}
	poplist();
	pushword(num);
}
Example #7
0
void
execflag(void)
{
	char *letter, *val;
	switch(count(runq->argv->words)){
	case 2:
		setstatus(flag[(uchar)runq->argv->words->next->word[0]]?"":"flag not set");
		break;
	case 3:
		letter = runq->argv->words->next->word;
		val = runq->argv->words->next->next->word;
		if(strlen(letter)==1){
			if(strcmp(val, "+")==0){
				flag[(uchar)letter[0]] = flagset;
				break;
			}
			if(strcmp(val, "-")==0){
				flag[(uchar)letter[0]] = 0;
				break;
			}
		}
	default:
		Xerror1("Usage: flag [letter] [+-]");
		return;
	}
	poplist();
}
Example #8
0
void
Xwrite(void)
{
	char *file;
	int f;
	switch(count(runq->argv->words)){
	default:
		Xerror1("> requires singleton\n");
		return;
	case 0:
		Xerror1("> requires file\n");
		return;
	case 1:
		break;
	}
	file = runq->argv->words->word;
	if((f = Creat(file))<0){
		pfmt(err, "%s: ", file);
		Xerror("can't open");
		return;
	}
	pushredir(ROPEN, f, runq->code[runq->pc].i);
	runq->pc++;
	poplist();
}
Example #9
0
void
Xmatch(void)
{
	word *p;
	char *subject;
	subject = list2str(runq->argv->words);
	setstatus("no match");
	for(p = runq->argv->next->words;p;p = p->next)
		if(match(subject, p->word, '\0')){
			setstatus("");
			break;
		}
	efree(subject);
	poplist();
	poplist();
}
Example #10
0
FrTextSpan *FrTextSpans::newSpan(const FrList *span_spec, const FrStruct *meta)
{
   if (!span_spec)
      return 0 ;
   FrTextSpan *new_spans = FrNewR(FrTextSpan,m_spans,m_spancount+1) ;
   if (new_spans)
      {
      m_spans = new_spans ;
      new (&m_spans[m_spancount]) FrTextSpan ;
      if (meta)
	 {
	 FrList *fields = meta->fieldNames() ;
	 while (fields)
	    {
	    FrSymbol *key = (FrSymbol*)poplist(fields) ;
	    m_spans[m_spancount].setMetaData(key,meta->get(key)) ;
	    }
	 }
      // any metadata in the span_spec will override the separately-passed
      //   metadata; this allows e.g. copying over existing data from an old
      //   span that has been processed into the new spec but may not have
      //   carried along all the metadata
      FrTextSpan *span = &m_spans[m_spancount] ;
      m_spans[m_spancount++].parse(span_spec,this) ;
      m_sorted = false ;
      return span ;
      }
   return 0 ;
}
Example #11
0
FrList *FrTextSpan::printable() const
{
   FrList *span = 0 ;
   const FrStruct *meta = metaData() ;
   FrList *fields = meta ? meta->fieldNames() : 0 ;
   while (fields)
      {
      FrSymbol *key = (FrSymbol*)poplist(fields) ;
      FrObject *data = meta->get(key) ;
      if (data)
	 {
	 data = data->deepcopy() ;
	 if (!data->consp())
	    data = new FrList(data) ;
	 FrList *datalist = (FrList*)data ;
	 pushlist(key,datalist) ;
	 pushlist(datalist,span) ;
	 }
      }
   pushlist(new FrFloat(weight()),span) ;
   pushlist(new FrFloat(score()),span) ;
   pushlist(new FrString(text()),span) ;
   pushlist(new FrInteger(end()),span) ;
   pushlist(new FrInteger(start()),span) ;
   return span ;
}
Example #12
0
void
Xqdol(void)
{
	word *a, *p;
	char *s;
	int n;
	if(count(runq->argv->words)!=1){
		Xerror1("variable name not singleton!");
		return;
	}
	s = runq->argv->words->word;
	deglob(s);
	a = vlook(s)->val;
	poplist();
	n = count(a);
	if(n==0){
		pushword("");
		return;
	}
	for(p = a;p;p = p->next) n+=strlen(p->word);
	s = emalloc(n);
	if(a){
		strcpy(s, a->word);
		for(p = a->next;p;p = p->next){
			strcat(s, " ");
			strcat(s, p->word);
		}
	}
	else
		s[0]='\0';
	pushword(s);
	efree(s);
}
Example #13
0
void
Xdol(void)
{
	word *a, *star;
	char *s, *t;
	int n;
	if(count(runq->argv->words)!=1){
		Xerror1("variable name not singleton!");
		return;
	}
	s = runq->argv->words->word;
	deglob(s);
	n = 0;
	for(t = s;'0'<=*t && *t<='9';t++) n = n*10+*t-'0';
	a = runq->argv->next->words;
	if(n==0 || *t)
		a = copywords(vlook(s)->val, a);
	else{
		star = vlook("*")->val;
		if(star && 1<=n && n<=count(star)){
			while(--n) star = star->next;
			a = newword(star->word, a);
		}
	}
	poplist();
	runq->argv->words = a;
}
Example #14
0
FrObject *FrQueue::pop()
{
   FrObject *item = poplist(qhead) ;
   if (!qhead)
      qtail = 0 ;
   qlength-- ;
   return item ;
}
Example #15
0
void
Xsub(void)
{
	word *a, *v;
	char *s;
	if(count(runq->argv->next->words)!=1){
		Xerror1("variable name not singleton!");
		return;
	}
	s = runq->argv->next->words->word;
	deglob(s);
	a = runq->argv->next->next->words;
	v = vlook(s)->val;
	a = subwords(v, count(v), runq->argv->words, a);
	poplist();
	poplist();
	runq->argv->words = a;
}
Example #16
0
void
Xassign(void)
{
	var *v;
	if(count(runq->argv->words)!=1){
		Xerror1("variable name not singleton!");
		return;
	}
	deglob(runq->argv->words->word);
	v = vlook(runq->argv->words->word);
	poplist();
	globlist();
	freewords(v->val);
	v->val = runq->argv->words;
	v->changed = 1;
	if(v->changefn)
		v->changefn(v);
	runq->argv->words = 0;
	poplist();
}
Example #17
0
void
Xreturn(void)
{
	struct thread *p = runq;
	turfredir();
	while(p->argv) poplist();
	codefree(p->code);
	runq = p->ret;
	efree((char *)p);
	if(runq==0)
		Exit(getstatus());
}
Example #18
0
void
execfunc(var *func)
{
	word *starval;
	popword();
	starval = runq->argv->words;
	runq->argv->words = 0;
	poplist();
	start(func->fn, func->pc, runq->local);
	runq->local = newvar(strdup("*"), runq->local);
	runq->local->val = starval;
	runq->local->changed = 1;
}
Example #19
0
void
Xconc(void)
{
	word *lp = runq->argv->words;
	word *rp = runq->argv->next->words;
	word *vp = runq->argv->next->next->words;
	int lc = count(lp), rc = count(rp);
	if(lc!=0 || rc!=0){
		if(lc==0 || rc==0){
			Xerror1("null list in concatenation");
			return;
		}
		if(lc!=1 && rc!=1 && lc!=rc){
			Xerror1("mismatched list lengths in concatenation");
			return;
		}
		vp = conclist(lp, rp, vp);
	}
	poplist();
	poplist();
	runq->argv->words = vp;
}
Example #20
0
void
Xdelfn(void)
{
	var *v;
	word *a;
	for(a = runq->argv->words;a;a = a->next){
		v = gvlook(a->word);
		if(v->fn)
			codefree(v->fn);
		v->fn = 0;
		v->fnchanged = 1;
	}
	poplist();
}
Example #21
0
void
globlist(void)
{
	word *a;
	globv = 0;
	globlist1(runq->argv->words);
	poplist();
	pushlist();
	if(globv){
		for(a = globv;a->next;a = a->next);
		a->next = runq->argv->words;
		runq->argv->words = globv;
	}
}
Example #22
0
bool FrTextSpans::addMetaData(const FrStruct *meta)
{
   bool success = false ;
   if (meta)
      {
      FrList *fields = meta->fieldNames() ;
      while (fields)
	 {
	 FrSymbol *key = (FrSymbol*)poplist(fields) ;
	 addMetaData(key,meta->get(key)) ;
	 success = true ;
	 }
      }
   return success ;
}
Example #23
0
void
execwait(void)
{
	switch(count(runq->argv->words)){
	default:
		Xerror1("Usage: wait [pid]");
		return;
	case 2:
		Waitfor(atoi(runq->argv->words->next->word), 0);
		break;
	case 1:
		Waitfor(-1, 0);
		break;
	}
	poplist();
}
Example #24
0
bool FrTextSpan::updateMetaData(const FrStruct *meta)
{
   bool changed = false ;
   if (meta)
      {
      FrList *fields = meta->fieldNames() ;
      while (fields)
	 {
	 FrSymbol *field = (FrSymbol*)poplist(fields) ;
	 FrObject *value = meta->get(field) ;
	 if (value &&addMetaData(field,value))
	    changed = true ;
	 }
      }
   return changed ;
}
Example #25
0
void
Xfor(void)
{
	if(runq->argv->words==0){
		poplist();
		runq->pc = runq->code[runq->pc].i;
	}
	else{
		freelist(runq->local->val);
		runq->local->val = runq->argv->words;
		runq->local->changed = 1;
		runq->argv->words = runq->argv->words->next;
		runq->local->val->next = 0;
		runq->pc++;
	}
}
Example #26
0
FrRegExp::~FrRegExp()
{
   delete regex ; regex = 0 ;
   FrFree(replacement) ; replacement = 0 ;
   while (_classes)
      {
      FrCons *cl = (FrCons*)poplist(_classes) ;
      if (cl)
	 {
	 FrRegExClass *re_class = (FrRegExClass*)cl->consCdr() ;
	 cl->freeObject() ;
	 delete re_class ;
	 }
      }
   _token = 0 ;
   return ;
}
Example #27
0
void
execumask(void)
{
	int n, argc;
	char **argv, **oargv, *p;
	char *argv0;

	argv0 = nil;
	setstatus("");
	oargv = mkargv(runq->argv->words);
	argv = oargv+1;
	for(argc=0; argv[argc]; argc++)
		;

	ARGBEGIN{
	default:
	usage:
		fprint(mapfd(2), "usage: umask [mode]\n");
		goto out;
	}ARGEND

	if(argc > 1)
		goto usage;

	if(argc == 1){
		n = strtol(argv[0], &p, 8);
		if(*p != 0 || p == argv[0])
			goto usage;
		umask(n);
		goto out;
	}

	n = umask(0);
	umask(n);
	if(n < 0){
		fprint(mapfd(2), "umask: %r\n");
		goto out;
	}

	fprint(mapfd(1), "umask %03o\n", n);

out:
	free(oargv);
	poplist();
	flush(err);
}
Example #28
0
void
execcd(void)
{
	word *a = runq->argv->words;
	word *cdpath;
	char dir[512];
	setstatus("can't cd");
	cdpath = vlook("cdpath")->val;
	switch(count(a)){
	default:
		pfmt(err, "Usage: cd [directory]\n");
		break;
	case 2:
		if(a->next->word[0]=='/' || cdpath==0)
			cdpath=&nullpath;
		for(;cdpath;cdpath = cdpath->next){
			strcpy(dir, cdpath->word);
			if(dir[0])
				strcat(dir, "/");
			strcat(dir, a->next->word);
			if(dochdir(dir)>=0){
				if(strlen(cdpath->word)
				&& strcmp(cdpath->word, ".")!=0)
					pfmt(err, "%s\n", dir);
				setstatus("");
				break;
			}
		}
		if(cdpath==0)
			pfmt(err, "Can't cd %s: %r\n", a->next->word);
		break;
	case 1:
		a = vlook("home")->val;
		if(count(a)>=1){
			if(dochdir(a->word)>=0)
				setstatus("");
			else
				pfmt(err, "Can't cd %s: %r\n", a->word);
		}
		else
			pfmt(err, "Can't cd -- $home empty\n");
		break;
	}
	poplist();
}
Example #29
0
void
Xfn(void)
{
	var *v;
	word *a;
	int end;
	end = runq->code[runq->pc].i;
	for(a = runq->argv->words;a;a = a->next){
		v = gvlook(a->word);
		if(v->fn)
			codefree(v->fn);
		v->fn = codecopy(runq->code);
		v->pc = runq->pc+2;
		v->fnchanged = 1;
	}
	runq->pc = end;
	poplist();
}
Example #30
0
void
Xcase(void)
{
	word *p;
	char *s;
	int ok = 0;
	s = list2str(runq->argv->next->words);
	for(p = runq->argv->words;p;p = p->next){
		if(match(s, p->word, '\0')){
			ok = 1;
			break;
		}
	}
	efree(s);
	if(ok)
		runq->pc++;
	else
		runq->pc = runq->code[runq->pc].i;
	poplist();
}