plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

util.c (1223B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <bio.h>
      4 #include <mach.h>
      5 #include "dat.h"
      6 
      7 static int nwarn;
      8 
      9 void
     10 warn(char *fmt, ...)
     11 {
     12 	va_list arg;
     13 
     14 	if(++nwarn < 5){
     15 		va_start(arg, fmt);
     16 		fprint(2, "warning: ");
     17 		vfprint(2, fmt, arg);
     18 		fprint(2, "\n");
     19 		va_end(arg);
     20 	}else if(nwarn == 5)
     21 		fprint(2, "[additional warnings elided...]\n");
     22 }
     23 
     24 void*
     25 erealloc(void *v, uint n)
     26 {
     27 	v = realloc(v, n);
     28 	if(v == nil)
     29 		sysfatal("realloc: %r");
     30 	return v;
     31 }
     32 
     33 void*
     34 emalloc(uint n)
     35 {
     36 	void *v;
     37 
     38 	v = mallocz(n, 1);
     39 	if(v == nil)
     40 		sysfatal("malloc: %r");
     41 	return v;
     42 }
     43 
     44 char*
     45 estrdup(char *s)
     46 {
     47 	s = strdup(s);
     48 	if(s == nil)
     49 		sysfatal("strdup: %r");
     50 	return s;
     51 }
     52 
     53 TypeList*
     54 mktl(Type *hd, TypeList *tail)
     55 {
     56 	TypeList *tl;
     57 
     58 	tl = emalloc(sizeof(*tl));
     59 	tl->hd = hd;
     60 	tl->tl = tail;
     61 	return tl;
     62 }
     63 
     64 static int isBfrog[256];
     65 
     66 int
     67 Bfmt(Fmt *fmt)
     68 {
     69 	int i;
     70 	char *s, *t;
     71 
     72 	if(!isBfrog['.']){
     73 		for(i=0; i<256; i++)
     74 			if(i != '_' && i != '$' && i < Runeself && !isalnum(i))
     75 				isBfrog[i] = 1;
     76 	}
     77 
     78 	s = va_arg(fmt->args, char*);
     79 	for(t=s; *t; t++){
     80 		if(isBfrog[(uchar)*t]){
     81 			if(*t == ':' && *(t+1) == ':'){
     82 				t++;
     83 				continue;
     84 			}
     85 			goto encode;
     86 		}
     87 	}
     88 	return fmtstrcpy(fmt, s);
     89 
     90 encode:
     91 	return fmtprint(fmt, "`%s`", s);
     92 }