aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/acidtypes/util.c
blob: 41883bbd6e64ddfc13ae69a08367bb9b9551c68d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>
#include "dat.h"

static int nwarn;

void
warn(char *fmt, ...)
{
	va_list arg;

	if(++nwarn < 5){
		va_start(arg, fmt);
		fprint(2, "warning: ");
		vfprint(2, fmt, arg);
		fprint(2, "\n");
		va_end(arg);
	}else if(nwarn == 5)
		fprint(2, "[additional warnings elided...]\n");
}

void*
erealloc(void *v, uint n)
{
	v = realloc(v, n);
	if(v == nil)
		sysfatal("realloc: %r");
	return v;
}

void*
emalloc(uint n)
{
	void *v;

	v = mallocz(n, 1);
	if(v == nil)
		sysfatal("malloc: %r");
	return v;
}

char*
estrdup(char *s)
{
	s = strdup(s);
	if(s == nil)
		sysfatal("strdup: %r");
	return s;
}

TypeList*
mktl(Type *hd, TypeList *tail)
{
	TypeList *tl;

	tl = emalloc(sizeof(*tl));
	tl->hd = hd;
	tl->tl = tail;
	return tl;
}

static int isBfrog[256];

int
Bfmt(Fmt *fmt)
{
	int i;
	char *s, *t;

	if(!isBfrog['.']){
		for(i=0; i<256; i++)
			if(i != '_' && i != '$' && i < Runeself && !isalnum(i))
				isBfrog[i] = 1;
	}

	s = va_arg(fmt->args, char*);
	for(t=s; *t; t++){
		if(isBfrog[(uchar)*t]){
			if(*t == ':' && *(t+1) == ':'){
				t++;
				continue;
			}
			goto encode;
		}
	}
	return fmtstrcpy(fmt, s);

encode:
	return fmtprint(fmt, "`%s`", s);
}