xref: /plan9-contrib/sys/src/cmd/faces/util.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <plumb.h>
5 #include "faces.h"
6 
7 void*
8 emalloc(ulong sz)
9 {
10 	void *v;
11 	v = malloc(sz);
12 	if(v == nil) {
13 		fprint(2, "out of memory allocating %ld\n", sz);
14 		exits("mem");
15 	}
16 	memset(v, 0, sz);
17 	return v;
18 }
19 
20 void*
21 erealloc(void *v, ulong sz)
22 {
23 	v = realloc(v, sz);
24 	if(v == nil) {
25 		fprint(2, "out of memory allocating %ld\n", sz);
26 		exits("mem");
27 	}
28 	return v;
29 }
30 
31 char*
32 estrdup(char *s)
33 {
34 	char *t;
35 	if((t = strdup(s)) == nil) {
36 		fprint(2, "out of memory in strdup(%.10s)\n", s);
37 		exits("mem");
38 	}
39 	return t;
40 }
41 
42 Face*
43 faceunpack(Plumbmsg *m)
44 {
45 	char *p, *q;
46 	char *ep;
47 	int nstr;
48 	Face *f;
49 
50 	if(m == nil)
51 		return nil;
52 
53 	f = emalloc(sizeof *f);
54 	p = m->data;
55 	ep = p+m->ndata;
56 
57 	for(nstr=0; nstr<Nstring; nstr++) {
58 		if((q = memchr(p, '\n', ep-p)) == nil)
59 			break;
60 
61 		*q++ = 0;
62 		f->str[nstr] = estrdup(p);
63 		p = q;
64 	}
65 
66 	if(nstr < Nstring)
67 		while(nstr < Nstring)
68 			f->str[nstr++] = estrdup("");
69 
70 	f->ntimes = 1;
71 	return f;
72 }
73 
74