From e2a5d674d9d22492220200824a9ec28f95a02253 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Tue, 9 Jun 2015 01:42:59 +0200 Subject: [PATCH] libdraw: consistent use of nil for pointers, error handling --- sys/src/libdraw/alloc.c | 65 +++++++++++++++++------------------ sys/src/libdraw/creadimage.c | 4 +-- sys/src/libdraw/debug.c | 2 +- sys/src/libdraw/draw.c | 4 +-- sys/src/libdraw/ellipse.c | 2 +- sys/src/libdraw/font.c | 8 ++--- sys/src/libdraw/init.c | 26 +++++++------- sys/src/libdraw/line.c | 2 +- sys/src/libdraw/loadimage.c | 2 +- sys/src/libdraw/poly.c | 2 +- sys/src/libdraw/readimage.c | 6 ++-- sys/src/libdraw/replclipr.c | 4 +++ sys/src/libdraw/string.c | 2 +- sys/src/libdraw/unloadimage.c | 2 +- sys/src/libdraw/window.c | 51 ++++++++++++++------------- 15 files changed, 93 insertions(+), 89 deletions(-) diff --git a/sys/src/libdraw/alloc.c b/sys/src/libdraw/alloc.c index 256378f36..7cb6e2d5c 100644 --- 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 @@ _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; diff --git a/sys/src/libdraw/creadimage.c b/sys/src/libdraw/creadimage.c index 351177e8d..67fd59ba4 100644 --- a/sys/src/libdraw/creadimage.c +++ b/sys/src/libdraw/creadimage.c @@ -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); diff --git a/sys/src/libdraw/debug.c b/sys/src/libdraw/debug.c index 3a6788a83..c90da09e8 100644 --- a/sys/src/libdraw/debug.c +++ b/sys/src/libdraw/debug.c @@ -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; } diff --git a/sys/src/libdraw/draw.c b/sys/src/libdraw/draw.c index d3f83e885..966d30c24 100644 --- a/sys/src/libdraw/draw.c +++ b/sys/src/libdraw/draw.c @@ -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; diff --git a/sys/src/libdraw/ellipse.c b/sys/src/libdraw/ellipse.c index 7a063f115..799cd6766 100644 --- a/sys/src/libdraw/ellipse.c +++ b/sys/src/libdraw/ellipse.c @@ -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; } diff --git a/sys/src/libdraw/font.c b/sys/src/libdraw/font.c index ed996e448..20eec17d3 100644 --- a/sys/src/libdraw/font.c +++ b/sys/src/libdraw/font.c @@ -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; isubfontname; 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; } diff --git a/sys/src/libdraw/init.c b/sys/src/libdraw/init.c index b65ade357..cc88ff6b0 100644 --- a/sys/src/libdraw/init.c +++ b/sys/src/libdraw/init.c @@ -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; diff --git a/sys/src/libdraw/line.c b/sys/src/libdraw/line.c index a06b5927e..f62921cfb 100644 --- a/sys/src/libdraw/line.c +++ b/sys/src/libdraw/line.c @@ -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; } diff --git a/sys/src/libdraw/loadimage.c b/sys/src/libdraw/loadimage.c index d5d599a2a..9f1938d54 100644 --- a/sys/src/libdraw/loadimage.c +++ b/sys/src/libdraw/loadimage.c @@ -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'; diff --git a/sys/src/libdraw/poly.c b/sys/src/libdraw/poly.c index 96effaec8..15febe09b 100644 --- a/sys/src/libdraw/poly.c +++ b/sys/src/libdraw/poly.c @@ -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; diff --git a/sys/src/libdraw/readimage.c b/sys/src/libdraw/readimage.c index 5745bad9b..66fac9656 100644 --- a/sys/src/libdraw/readimage.c +++ b/sys/src/libdraw/readimage.c @@ -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; jdisplay, 22); + if(b == nil){ + fprint(2, "replclipr: %r\n"); + return; + } b[0] = 'c'; BPLONG(b+1, i->id); repl = repl!=0; diff --git a/sys/src/libdraw/string.c b/sys/src/libdraw/string.c index c15bf2777..1918a7f09 100644 --- a/sys/src/libdraw/string.c +++ b/sys/src/libdraw/string.c @@ -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; } diff --git a/sys/src/libdraw/unloadimage.c b/sys/src/libdraw/unloadimage.c index 741c73b6b..107a3a786 100644 --- a/sys/src/libdraw/unloadimage.c +++ b/sys/src/libdraw/unloadimage.c @@ -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; } diff --git a/sys/src/libdraw/window.c b/sys/src/libdraw/window.c index d36b132bd..3f006405c 100644 --- a/sys/src/libdraw/window.c +++ b/sys/src/libdraw/window.c @@ -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