ref: e2a5d674d9d22492220200824a9ec28f95a02253
parent: 0fc761bc845bd9795f05206e24621c0f5e76423e
author: cinap_lenrek <[email protected]>
date: Mon Jun 8 21:42:59 EDT 2015
libdraw: consistent use of nil for pointers, error handling
--- a/sys/src/libdraw/alloc.c
+++ b/sys/src/libdraw/alloc.c
@@ -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 @@
int id;
int depth;
- err = 0;
- i = 0;
+ err = nil;
+ i = nil;
if(badrect(r)){
werrstr("bad rectangle");
@@ -39,18 +39,18 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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;
--- a/sys/src/libdraw/creadimage.c
+++ b/sys/src/libdraw/creadimage.c
@@ -54,7 +54,7 @@
return nil;
}
- if(d){
+ if(d != nil){
if(dolock)
lockdisplay(d);
i = allocimage(d, r, chan, 0, 0);
@@ -97,7 +97,7 @@
}
if(readn(fd, buf, nb)!=nb)
goto Errout;
- if(d){
+ if(d != nil){
if(dolock)
lockdisplay(d);
a = bufimage(i->display, 21+nb);
--- a/sys/src/libdraw/debug.c
+++ b/sys/src/libdraw/debug.c
@@ -7,7 +7,7 @@
{
uchar *a;
a = bufimage(display, 1+1);
- if(a == 0){
+ if(a == nil){
fprint(2, "drawsetdebug: %r\n");
return;
}
--- a/sys/src/libdraw/draw.c
+++ b/sys/src/libdraw/draw.c
@@ -9,7 +9,7 @@
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 @@
_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;
--- a/sys/src/libdraw/ellipse.c
+++ b/sys/src/libdraw/ellipse.c
@@ -11,7 +11,7 @@
_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;
}
--- a/sys/src/libdraw/font.c
+++ b/sys/src/libdraw/font.c
@@ -24,7 +24,7 @@
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 @@
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 @@
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 @@
}
flushimage(d, 0); /* flush any pending errors */
b = bufimage(d, 1+4+4+1);
- if(b == 0){
+ if(b == nil){
freeimage(new);
goto Return;
}
--- a/sys/src/libdraw/init.c
+++ b/sys/src/libdraw/init.c
@@ -10,7 +10,7 @@
static char deffontname[] = "*default*";
Screen *_screen;
-int debuglockdisplay = 0;
+int debuglockdisplay = 0;
static void _closedisplay(Display*, int);
@@ -21,7 +21,7 @@
Display *d;
d = display;
- if(d){
+ if(d != nil){
display = nil;
_closedisplay(d, 1);
}
@@ -86,7 +86,7 @@
/*
* 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 @@
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 @@
freescreen(*scrp);
*scrp = nil;
}
- if(image == 0){
+ if(image == nil){
*winp = nil;
d->screenimage = nil;
return -1;
@@ -221,9 +221,9 @@
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 @@
Error1:
free(t);
werrstr("initdisplay: %s: %r", buf);
- return 0;
+ return nil;
}
ctlfd = open(buf, ORDWR|OCEXEC);
}
@@ -269,7 +269,7 @@
goto Error2;
}
disp = mallocz(sizeof(Display), 1);
- if(disp == 0){
+ if(disp == nil){
Error4:
close(reffd);
goto Error3;
@@ -418,8 +418,8 @@
{
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 @@
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;
--- a/sys/src/libdraw/line.c
+++ b/sys/src/libdraw/line.c
@@ -16,7 +16,7 @@
_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;
}
--- a/sys/src/libdraw/loadimage.c
+++ b/sys/src/libdraw/loadimage.c
@@ -38,7 +38,7 @@
n = dy*bpl;
a = bufimage(i->display, 21+n);
if(a == nil){
- werrstr("bufimage failed");
+ werrstr("loadimage: %r");
return -1;
}
a[0] = 'y';
--- a/sys/src/libdraw/poly.c
+++ b/sys/src/libdraw/poly.c
@@ -44,7 +44,7 @@
_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;
--- a/sys/src/libdraw/readimage.c
+++ b/sys/src/libdraw/readimage.c
@@ -27,7 +27,7 @@
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 @@
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 @@
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)
--- a/sys/src/libdraw/replclipr.c
+++ b/sys/src/libdraw/replclipr.c
@@ -8,6 +8,10 @@
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;
--- a/sys/src/libdraw/string.c
+++ b/sys/src/libdraw/string.c
@@ -112,7 +112,7 @@
if(bg)
m += 4+2*4;
b = bufimage(dst->display, m);
- if(b == 0){
+ if(b == nil){
fprint(2, "string: %r\n");
break;
}
--- a/sys/src/libdraw/unloadimage.c
+++ b/sys/src/libdraw/unloadimage.c
@@ -37,7 +37,7 @@
n = bpl*dy;
}
a = bufimage(d, 1+4+4*4);
- if(a == 0){
+ if(a == nil){
werrstr("unloadimage: %r");
return -1;
}
--- a/sys/src/libdraw/window.c
+++ b/sys/src/libdraw/window.c
@@ -17,17 +17,17 @@
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 @@
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 @@
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 @@
s->display = d;
s->id = id;
- s->image = 0;
- s->fill = 0;
+ s->image = nil;
+ s->fill = nil;
return s;
}
@@ -85,12 +85,15 @@
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 @@
* window, and want it to disappear visually.
*/
if(flushimage(d, 1) < 0)
- return -1;
+ goto Error;
free(s);
return 1;
}
@@ -116,8 +119,8 @@
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 @@
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 @@
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