libdraw: consistent use of nil for pointers, error handling

This commit is contained in:
cinap_lenrek 2015-06-09 01:42:59 +02:00
parent 0fc761bc84
commit e2a5d674d9
15 changed files with 93 additions and 89 deletions

View file

@ -5,10 +5,10 @@
Image*
allocimage(Display *d, Rectangle r, ulong chan, int repl, ulong val)
{
Image* i;
Image *i;
i = _allocimage(nil, d, r, chan, repl, val, 0, 0);
if (i)
i = _allocimage(nil, d, r, chan, repl, val, 0, 0);
if(i != nil)
setmalloctag(i, getcallerpc(&d));
return i;
}
@ -23,8 +23,8 @@ _allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val,
int id;
int depth;
err = 0;
i = 0;
err = nil;
i = nil;
if(badrect(r)){
werrstr("bad rectangle");
@ -39,18 +39,18 @@ _allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val,
if(depth == 0){
err = "bad channel descriptor";
Error:
if(err)
if(err != nil)
werrstr("allocimage: %s", err);
else
werrstr("allocimage: %r");
free(i);
return 0;
return nil;
}
/* flush pending data so we don't get error allocating the image */
flushimage(d, 0);
a = bufimage(d, 1+4+4+1+4+1+4*4+4*4+4);
if(a == 0)
if(a == nil)
goto Error;
d->imageid++;
id = d->imageid;
@ -77,13 +77,13 @@ _allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val,
if(flushimage(d, 0) < 0)
goto Error;
if(ai)
if(ai != nil)
i = ai;
else{
i = malloc(sizeof(Image));
if(i == nil){
a = bufimage(d, 1+4);
if(a){
if(a != nil){
a[0] = 'f';
BPLONG(a+1, id);
flushimage(d, 0);
@ -98,8 +98,8 @@ _allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val,
i->r = r;
i->clipr = clipr;
i->repl = repl;
i->screen = 0;
i->next = 0;
i->screen = nil;
i->next = nil;
return i;
}
@ -112,25 +112,24 @@ namedimage(Display *d, char *name)
int id, n;
ulong chan;
err = 0;
i = 0;
err = nil;
i = nil;
n = strlen(name);
if(n >= 256){
err = "name too long";
Error:
if(err)
if(err != nil)
werrstr("namedimage: %s", err);
else
werrstr("namedimage: %r");
if(i)
free(i);
return 0;
free(i);
return nil;
}
/* flush pending data so we don't get error allocating the image */
flushimage(d, 0);
a = bufimage(d, 1+4+1+n);
if(a == 0)
if(a == nil)
goto Error;
d->imageid++;
id = d->imageid;
@ -149,7 +148,7 @@ namedimage(Display *d, char *name)
if(i == nil){
Error1:
a = bufimage(d, 1+4);
if(a){
if(a != nil){
a[0] = 'f';
BPLONG(a+1, id);
flushimage(d, 0);
@ -173,8 +172,8 @@ namedimage(Display *d, char *name)
i->clipr.min.y = atoi(buf+9*12);
i->clipr.max.x = atoi(buf+10*12);
i->clipr.max.y = atoi(buf+11*12);
i->screen = 0;
i->next = 0;
i->screen = nil;
i->next = nil;
return i;
}
@ -186,7 +185,7 @@ nameimage(Image *i, char *name, int in)
n = strlen(name);
a = bufimage(i->display, 1+4+1+1+n);
if(a == 0)
if(a == nil)
return 0;
a[0] = 'N';
BPLONG(a+1, i->id);
@ -205,23 +204,16 @@ _freeimage1(Image *i)
Display *d;
Image *w;
if(i == 0 || i->display == 0)
if(i == nil || i->display == nil)
return 0;
/* make sure no refresh events occur on this if we block in the write */
d = i->display;
/* flush pending data so we don't get error deleting the image */
flushimage(d, 0);
a = bufimage(d, 1+4);
if(a == 0)
return -1;
a[0] = 'f';
BPLONG(a+1, i->id);
if(i->screen){
if(i->screen != nil){
w = d->windows;
if(w == i)
d->windows = i->next;
else
while(w){
while(w != nil){
if(w->next == i){
w->next = i->next;
break;
@ -229,7 +221,12 @@ _freeimage1(Image *i)
w = w->next;
}
}
if(flushimage(d, i->screen!=0) < 0)
a = bufimage(d, 1+4);
if(a == nil)
return -1;
a[0] = 'f';
BPLONG(a+1, i->id);
if(flushimage(d, i->screen!=nil) < 0)
return -1;
return 0;

View file

@ -54,7 +54,7 @@ creadimage(Display *d, int fd, int dolock)
return nil;
}
if(d){
if(d != nil){
if(dolock)
lockdisplay(d);
i = allocimage(d, r, chan, 0, 0);
@ -97,7 +97,7 @@ creadimage(Display *d, int fd, int dolock)
}
if(readn(fd, buf, nb)!=nb)
goto Errout;
if(d){
if(d != nil){
if(dolock)
lockdisplay(d);
a = bufimage(i->display, 21+nb);

View file

@ -7,7 +7,7 @@ drawsetdebug(int v)
{
uchar *a;
a = bufimage(display, 1+1);
if(a == 0){
if(a == nil){
fprint(2, "drawsetdebug: %r\n");
return;
}

View file

@ -9,7 +9,7 @@ _setdrawop(Display *d, Drawop op)
if(op != SoverD){
a = bufimage(d, 1+1);
if(a == 0)
if(a == nil)
return;
a[0] = 'O';
a[1] = op;
@ -24,7 +24,7 @@ draw1(Image *dst, Rectangle *r, Image *src, Point *p0, Image *mask, Point *p1, D
_setdrawop(dst->display, op);
a = bufimage(dst->display, 1+4+4+4+4*4+2*4+2*4);
if(a == 0)
if(a == nil)
return;
if(src == nil)
src = dst->display->black;

View file

@ -11,7 +11,7 @@ doellipse(int cmd, Image *dst, Point *c, int xr, int yr, int thick, Image *src,
_setdrawop(dst->display, op);
a = bufimage(dst->display, 1+4+4+2*4+4+4+4+2*4+2*4);
if(a == 0){
if(a == nil){
fprint(2, "image ellipse: %r\n");
return;
}

View file

@ -24,7 +24,7 @@ cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **s
rp = *rr;
}
wid = 0;
*subfontname = 0;
*subfontname = nil;
for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
if(ss){
r = *(uchar*)sp;
@ -153,7 +153,7 @@ cf2subfont(Cachefont *cf, Font *f)
name = cf->subfontname;
if(name == nil){
if(f->display && f->display->screenimage)
if(f->display != nil && f->display->screenimage != nil)
depth = f->display->screenimage->depth;
else
depth = 8;
@ -298,7 +298,7 @@ loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
return 1;
flushimage(f->display, 0); /* flush any pending errors */
b = bufimage(f->display, 37);
if(b == 0)
if(b == nil)
return 0;
b[0] = 'l';
BPLONG(b+1, f->cacheimage->id);
@ -342,7 +342,7 @@ fontresize(Font *f, int wid, int ncache, int depth)
}
flushimage(d, 0); /* flush any pending errors */
b = bufimage(d, 1+4+4+1);
if(b == 0){
if(b == nil){
freeimage(new);
goto Return;
}

View file

@ -10,7 +10,7 @@ int _drawdebug = 0;
static char deffontname[] = "*default*";
Screen *_screen;
int debuglockdisplay = 0;
int debuglockdisplay = 0;
static void _closedisplay(Display*, int);
@ -21,7 +21,7 @@ drawshutdown(void)
Display *d;
d = display;
if(d){
if(d != nil){
display = nil;
_closedisplay(d, 1);
}
@ -86,7 +86,7 @@ geninitdraw(char *devdir, void(*error)(Display*, char*), char *fontname, char *l
/*
* Write label; ignore errors (we might not be running under rio)
*/
if(label){
if(label != nil){
snprint(buf, sizeof buf, "%s/label", display->windir);
fd = open(buf, OREAD);
if(fd >= 0){
@ -148,7 +148,7 @@ retry:
close(fd);
buf[n] = '\0';
image = namedimage(d, buf);
if(image == 0){
if(image == nil){
/*
* theres a race where the winname can change after
* we read it, so keep trying as long as the name
@ -166,7 +166,7 @@ retry:
freescreen(*scrp);
*scrp = nil;
}
if(image == 0){
if(image == nil){
*winp = nil;
d->screenimage = nil;
return -1;
@ -221,9 +221,9 @@ initdisplay(char *dev, char *win, void(*error)(Display*, char*))
fmtinstall('P', Pfmt);
fmtinstall('R', Rfmt);
if(dev == 0)
if(dev == nil)
dev = "/dev";
if(win == 0)
if(win == nil)
win = "/dev";
if(strlen(dev)>sizeof buf-25 || strlen(win)>sizeof buf-25){
werrstr("initdisplay: directory name too long");
@ -240,7 +240,7 @@ initdisplay(char *dev, char *win, void(*error)(Display*, char*))
Error1:
free(t);
werrstr("initdisplay: %s: %r", buf);
return 0;
return nil;
}
ctlfd = open(buf, ORDWR|OCEXEC);
}
@ -269,7 +269,7 @@ initdisplay(char *dev, char *win, void(*error)(Display*, char*))
goto Error2;
}
disp = mallocz(sizeof(Display), 1);
if(disp == 0){
if(disp == nil){
Error4:
close(reffd);
goto Error3;
@ -418,8 +418,8 @@ drawerror(Display *d, char *s)
{
char err[ERRMAX];
if(d && d->error)
d->error(d, s);
if(d != nil && d->error != nil)
(*d->error)(d, s);
else{
errstr(err, sizeof err);
fprint(2, "draw: %s: %s\n", s, err);
@ -469,11 +469,11 @@ bufimage(Display *d, int n)
if(n<0 || n>d->bufsize){
werrstr("bad count in bufimage");
return 0;
return nil;
}
if(d->bufp+n > d->buf+d->bufsize)
if(doflush(d) < 0)
return 0;
return nil;
p = d->bufp;
d->bufp += n;
return p;

View file

@ -16,7 +16,7 @@ lineop(Image *dst, Point p0, Point p1, int end0, int end1, int radius, Image *sr
_setdrawop(dst->display, op);
a = bufimage(dst->display, 1+4+2*4+2*4+4+4+4+4+2*4);
if(a == 0){
if(a == nil){
fprint(2, "image line: %r\n");
return;
}

View file

@ -38,7 +38,7 @@ loadimage(Image *i, Rectangle r, uchar *data, int ndata)
n = dy*bpl;
a = bufimage(i->display, 21+n);
if(a == nil){
werrstr("bufimage failed");
werrstr("loadimage: %r");
return -1;
}
a[0] = 'y';

View file

@ -44,7 +44,7 @@ dopoly(int cmd, Image *dst, Point *pp, int np, int end0, int end1, int radius, I
_setdrawop(dst->display, op);
a = bufimage(dst->display, 1+4+2+4+4+4+4+2*4+(u-t));
if(a == 0){
if(a == nil){
free(t);
fprint(2, "image poly: %r\n");
return;

View file

@ -27,7 +27,7 @@ readimage(Display *d, int fd, int dolock)
if(readn(fd, hdr+11, 5*12-11) != 5*12-11)
return nil;
if(d)
if(d != nil)
chunk = d->bufsize - 32; /* a little room for header */
else
chunk = 8192;
@ -78,7 +78,7 @@ readimage(Display *d, int fd, int dolock)
l = bytesperline(r, chantodepth(chan));
if(l > chunk)
chunk = l;
if(d){
if(d != nil){
if(dolock)
lockdisplay(d);
i = allocimage(d, r, chan, 0, -1);
@ -116,7 +116,7 @@ readimage(Display *d, int fd, int dolock)
for(j=0; j<chunk; j++)
tmp[j] ^= 0xFF;
if(d){
if(d != nil){
if(dolock)
lockdisplay(d);
if(loadimage(i, Rect(r.min.x, miny, r.max.x, miny+dy), tmp, chunk) <= 0)

View file

@ -8,6 +8,10 @@ replclipr(Image *i, int repl, Rectangle clipr)
uchar *b;
b = bufimage(i->display, 22);
if(b == nil){
fprint(2, "replclipr: %r\n");
return;
}
b[0] = 'c';
BPLONG(b+1, i->id);
repl = repl!=0;

View file

@ -112,7 +112,7 @@ _string(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, Rune *r, i
if(bg)
m += 4+2*4;
b = bufimage(dst->display, m);
if(b == 0){
if(b == nil){
fprint(2, "string: %r\n");
break;
}

View file

@ -37,7 +37,7 @@ unloadimage(Image *i, Rectangle r, uchar *data, int ndata)
n = bpl*dy;
}
a = bufimage(d, 1+4+4*4);
if(a == 0){
if(a == nil){
werrstr("unloadimage: %r");
return -1;
}

View file

@ -17,17 +17,17 @@ allocscreen(Image *image, Image *fill, int public)
d = image->display;
if(d != fill->display){
werrstr("allocscreen: image and fill on different displays");
return 0;
return nil;
}
s = malloc(sizeof(Screen));
if(s == 0)
return 0;
if(s == nil)
return nil;
if(!screenid)
screenid = getpid();
for(try=0; try<25; try++){
/* loop until find a free id */
a = bufimage(d, 1+4+4+4+1);
if(a == 0)
if(a == nil)
break;
id = ++screenid & 0xffff; /* old devdraw bug */
a[0] = 'A';
@ -39,13 +39,13 @@ allocscreen(Image *image, Image *fill, int public)
goto Found;
}
free(s);
return 0;
return nil;
Found:
s->display = d;
s->id = id;
s->image = image;
assert(s->image && s->image->chan != 0);
assert(s->image != nil && s->image->chan != 0);
s->fill = fill;
return s;
@ -58,13 +58,13 @@ publicscreen(Display *d, int id, ulong chan)
Screen *s;
s = malloc(sizeof(Screen));
if(s == 0)
return 0;
if(s == nil)
return nil;
a = bufimage(d, 1+4+4);
if(a == 0){
Error:
if(a == nil){
Error:
free(s);
return 0;
return nil;
}
a[0] = 'S';
BPLONG(a+1, id);
@ -74,8 +74,8 @@ publicscreen(Display *d, int id, ulong chan)
s->display = d;
s->id = id;
s->image = 0;
s->fill = 0;
s->image = nil;
s->fill = nil;
return s;
}
@ -85,12 +85,15 @@ freescreen(Screen *s)
uchar *a;
Display *d;
if(s == 0)
if(s == nil)
return 0;
d = s->display;
a = bufimage(d, 1+4);
if(a == 0)
if(a == nil){
Error:
free(s);
return -1;
}
a[0] = 'F';
BPLONG(a+1, s->id);
/*
@ -98,7 +101,7 @@ freescreen(Screen *s)
* window, and want it to disappear visually.
*/
if(flushimage(d, 1) < 0)
return -1;
goto Error;
free(s);
return 1;
}
@ -116,8 +119,8 @@ _allocwindow(Image *i, Screen *s, Rectangle r, int ref, ulong val)
d = s->display;
i = _allocimage(i, d, r, d->screenimage->chan, 0, val, s->id, ref);
if(i == 0)
return 0;
if(i == nil)
return nil;
i->screen = s;
i->next = s->display->windows;
s->display->windows = i;
@ -157,6 +160,8 @@ topbottom(Image **w, int n, int top)
if(n==0)
return;
b = bufimage(d, 1+1+2+4*n);
if(b == nil)
return;
b[0] = 't';
b[1] = top;
BPSHORT(b+2, n);
@ -167,17 +172,15 @@ topbottom(Image **w, int n, int top)
void
bottomwindow(Image *w)
{
if(w->screen == 0)
return;
topbottom(&w, 1, 0);
if(w->screen != nil)
topbottom(&w, 1, 0);
}
void
topwindow(Image *w)
{
if(w->screen == 0)
return;
topbottom(&w, 1, 1);
if(w->screen != nil)
topbottom(&w, 1, 1);
}
void