9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

subr.c (1224B)


      1 #include "rc.h"
      2 #include "exec.h"
      3 #include "io.h"
      4 #include "fns.h"
      5 
      6 char*
      7 emalloc(long n)
      8 {
      9 	char *p = (char *)Malloc(n);
     10 	if(p==0)
     11 		panic("Can't malloc %d bytes", n);
     12 /*	if(err){ pfmt(err, "malloc %d->%p\n", n, p); flush(err); } /**/
     13 	memset(p, 0, n);
     14 	return p;
     15 }
     16 
     17 void
     18 efree(char *p)
     19 {
     20 /*	pfmt(err, "free %p\n", p); flush(err); /**/
     21 	if(p)
     22 		free(p);
     23 	else pfmt(err, "free 0\n");
     24 }
     25 extern int lastword, lastdol;
     26 
     27 void
     28 yyerror(char *m)
     29 {
     30 	pfmt(err, "rc: ");
     31 	if(runq->cmdfile && !runq->iflag)
     32 		pfmt(err, "%s:%d: ", runq->cmdfile, runq->lineno);
     33 	else if(runq->cmdfile)
     34 		pfmt(err, "%s: ", runq->cmdfile);
     35 	else if(!runq->iflag)
     36 		pfmt(err, "line %d: ", runq->lineno);
     37 	if(tok[0] && tok[0]!='\n')
     38 		pfmt(err, "token %q: ", tok);
     39 	pfmt(err, "%s\n", m);
     40 	flush(err);
     41 	lastword = 0;
     42 	lastdol = 0;
     43 	while(lastc!='\n' && lastc!=EOF) advance();
     44 	nerror++;
     45 	setvar("status", newword(m, (word *)0));
     46 }
     47 char *bp;
     48 
     49 static void
     50 iacvt(int n)
     51 {
     52 	if(n<0){
     53 		*bp++='-';
     54 		n=-n;	/* doesn't work for n==-inf */
     55 	}
     56 	if(n/10)
     57 		iacvt(n/10);
     58 	*bp++=n%10+'0';
     59 }
     60 
     61 void
     62 inttoascii(char *s, long n)
     63 {
     64 	bp = s;
     65 	iacvt(n);
     66 	*bp='\0';
     67 }
     68 
     69 void
     70 panic(char *s, int n)
     71 {
     72 	pfmt(err, "rc: ");
     73 	pfmt(err, s, n);
     74 	pchr(err, '\n');
     75 	flush(err);
     76 	Abort();
     77 }