int loaddevconf(Dev *d, int n) { uchar *buf; int nr; int type; if(n >= nelem(d->usb->conf)){ werrstr("loaddevconf: bug: out of configurations in device"); fprint(2, "%s: %r\n", argv0); return -1; } buf = emallocz(Maxdevconf, 0); type = Rd2h|Rstd|Rdev; nr = usbcmd(d, type, Rgetdesc, Dconf<<8|n, 0, buf, Maxdevconf); if(nr < Dconflen){ free(buf); return -1; } if(d->usb->conf[n] == nil) d->usb->conf[n] = emallocz(sizeof(Conf), 1); nr = parseconf(d->usb, d->usb->conf[n], buf, nr); free(buf); return nr; }
static void parserule(const char *s, Rule * r) { r->prop = emallocz(128); r->tags = emallocz(64); sscanf(s, "%s %s %d %d", r->prop, r->tags, &r->isfloating, &r->hastitle); }
Dev* opendev(char *fn) { Dev *d; int l; if(access("/dev/usb", AEXIST) < 0 && bind("#u", "/dev", MBEFORE) < 0) return nil; d = emallocz(sizeof(Dev), 1); incref(d); l = strlen(fn); d->dfd = -1; /* * +30 to allocate extra size to concat "/<epfilename>" * we should probably remove that feature from the manual * and from the code after checking out that nobody relies on * that. */ d->dir = emallocz(l + 30, 0); strcpy(d->dir, fn); strcpy(d->dir+l, "/ctl"); d->cfd = open(d->dir, ORDWR|OCEXEC); d->dir[l] = 0; d->id = nameid(fn); if(d->cfd < 0){ werrstr("can't open endpoint %s: %r", d->dir); free(d->dir); free(d); return nil; } dprint(2, "%s: opendev %#p %s\n", argv0, d, fn); return d; }
PaqDir * paqFile(char *name, Dir *dir) { int fd, n, nn, nb; vlong tot; uchar *block, *pointer; ulong offset; fd = open(name, OREAD); if(fd < 0) { warn("could not open file: %s: %r", name); return nil; } block = emallocz(blocksize); pointer = emallocz(blocksize); nb = 0; n = 0; tot = 0; for(;;) { nn = read(fd, block+n, blocksize-n); if(nn < 0) { warn("read failed: %s: %r", name); goto Err; } tot += nn; if(nn == 0) { if(n == 0) break; /* pad out last block */ memset(block+n, 0, blocksize-n); nn = blocksize - n; } n += nn; if(n < blocksize) continue; if(nb >= blocksize/OffsetSize) { warn("file too big for blocksize: %s", name); goto Err; } offset = writeBlock(block, DataBlock); putl(pointer+nb*OffsetSize, offset); nb++; n = 0; } offset = writeBlock(pointer, PointerBlock); close(fd); free(pointer); free(block); dir->length = tot; return paqDirAlloc(dir, offset); Err: close(fd); free(pointer); free(block); return nil; }
void exclusions(void) { Biobuf *f; int ni, nmaxi, ne, nmaxe; char *line; if(patternfile == nil) return; f = Bopen(patternfile, OREAD); if(f == nil) fatal("cannot open patternfile"); ni = 0; nmaxi = 100; include = emallocz(nmaxi*sizeof(*include)); include[0] = nil; ne = 0; nmaxe = 100; exclude = emallocz(nmaxe*sizeof(*exclude)); exclude[0] = nil; while(line = Brdline(f, '\n')){ line[Blinelen(f) - 1] = 0; if(strlen(line) < 2 || line[1] != ' ') continue; switch(line[0]){ case '+': if(ni+1 >= nmaxi){ nmaxi = 2*nmaxi; include = realloc(include, nmaxi*sizeof(*include)); if(include == nil) fatal("out of memory"); } DEBUG(DFD, "\tinclude %s\n", line+2); include[ni] = regcomp(line+2); include[++ni] = nil; break; case '-': if(ne+1 >= nmaxe){ nmaxe = 2*nmaxe; exclude = realloc(exclude, nmaxe*sizeof(*exclude)); if(exclude == nil) fatal("out of memory"); } DEBUG(DFD, "\texclude %s\n", line+2); exclude[ne] = regcomp(line+2); exclude[++ne] = nil; break; default: DEBUG(DFD, "ignoring pattern %s\n", line); break; } } Bterm(f); }
static int plinit(Serialport *p) { char *st; uint8_t *buf; uint32_t csp, maxpkt, dno; Serial *ser; ser = p->s; buf = emallocz(VendorReqSz, 1); dsprint(2, "plinit\n"); csp = ser->dev->usb->csp; maxpkt = ser->dev->maxpkt; dno = ser->dev->usb->dno; if((ser->type = revid(dno)) == TypeUnk) ser->type = heuristicid(csp, maxpkt); dsprint(2, "serial: type %d\n", ser->type); vendorread(p, 0x8484, 0, buf); vendorwrite(p, 0x0404, 0); vendorread(p, 0x8484, 0, buf); vendorread(p, 0x8383, 0, buf); vendorread(p, 0x8484, 0, buf); vendorwrite(p, 0x0404, 1); vendorread(p, 0x8484, 0, buf); vendorread(p, 0x8383, 0, buf); vendorwrite(p, Dcr0Idx|DcrSet, Dcr0Init); vendorwrite(p, Dcr1Idx|DcrSet, Dcr1Init); if(ser->type == TypeHX) vendorwrite(p, Dcr2Idx|DcrSet, Dcr2InitX); else vendorwrite(p, Dcr2Idx|DcrSet, Dcr2InitH); plgetparam(p); qunlock(ser); free(buf); st = emallocz(255, 1); qlock(ser); if(serialdebug) serdumpst(p, st, 255); dsprint(2, st); free(st); /* p gets freed by closedev, the process has a reference */ incref(ser->dev); proccreate(statusreader, p, 8*1024); return 0; }
void ixp_pending_respond(Ixp9Req *req) { IxpFileId *file; IxpPendingLink *p; IxpRequestLink *req_link; IxpQueue *queue; file = req->fid->aux; assert(file->pending); p = file->p; if(p->queue) { queue = p->queue; p->queue = queue->link; req->ofcall.io.data = queue->dat; req->ofcall.io.count = queue->len; if(req->aux) { req_link = req->aux; req_link->next->prev = req_link->prev; req_link->prev->next = req_link->next; free(req_link); } ixp_respond(req, nil); free(queue); }else { req_link = emallocz(sizeof *req_link); req_link->req = req; req_link->next = &p->pending->req; req_link->prev = req_link->next->prev; req_link->next->prev = req_link; req_link->prev->next = req_link; req->aux = req_link; } }
static void addfunc(Sym *s, void* p) { int h; List *l; USED(p); if(s->type != 'T') return; if (((s->name[0] == '.' || s->name[0] == '$'))) return; if(s->name[0] == '_') return; h = hash(s->name, Nhash); for(l = Functab[h]; l; l = l->next) if(strcmp(l->name, s->name) == 0) return; l = emallocz(sizeof(List) +strlen(s->name) +1, 0); l->name = (char *)&l[1]; strcpy(l->name, s->name); l->next = Functab[h]; Functab[h] = l; }
static Buf* allocbuf(Ether *e) { Buf *bp; bp = nbrecvp(e->bc); if(bp == nil){ qlock(e); if(e->nabufs < Nbufs){ bp = emallocz(sizeof(Buf), 1); e->nabufs++; setmalloctag(bp, getcallerpc(&e)); deprint(2, "%s: %d buffers\n", argv0, e->nabufs); } qunlock(e); } if(bp == nil){ deprint(2, "%s: blocked waiting for allocbuf\n", argv0); bp = recvp(e->bc); } bp->rp = bp->data + Hdrsize; bp->ndata = 0; if(0)deprint(2, "%s: allocbuf %#p\n", argv0, bp); qlock(e); e->nbufs++; qunlock(e); return bp; }
Frame* frame_create(Client *c, View *v) { static ushort id = 1; Frame *f; f = emallocz(sizeof *f); f->id = id++; f->client = c; f->view = v; if(c->sel) { f->floatr = c->sel->floatr; f->r = c->sel->r; }else if(c->sel) { f->floatr = c->frame->floatr; f->r = c->frame->r; }else { f->r = client_grav(c, c->r); f->floatr = f->r; c->sel = f; } f->collapsed = false; f->screen = -1; f->oldarea = -1; f->oldscreen = -1; return f; }
static void configroothub(Hub *h) { Dev *d; char buf[128]; char *p; int nr; d = h->dev; h->nport = 2; h->maxpkt = 8; seek(d->cfd, 0, 0); nr = read(d->cfd, buf, sizeof(buf)-1); if(nr < 0) goto Done; buf[nr] = 0; p = strstr(buf, "ports "); if(p == nil) fprint(2, "%s: %s: no port information\n", argv0, d->dir); else h->nport = atoi(p+6); p = strstr(buf, "maxpkt "); if(p == nil) fprint(2, "%s: %s: no maxpkt information\n", argv0, d->dir); else h->maxpkt = atoi(p+7); Done: h->port = emallocz((h->nport+1)*sizeof(Port), 1); dprint(2, "%s: %s: ports %d maxpkt %d\n", argv0, d->dir, h->nport, h->maxpkt); }
static Conn* newconn(Ether *e) { int i; Conn *c; qlock(e); for(i = 0; i < nelem(e->conns); i++){ c = e->conns[i]; if(c == nil || c->ref == 0){ if(c == nil){ c = emallocz(sizeof(Conn), 1); c->rc = chancreate(sizeof(Buf*), 16); c->nb = i; } c->ref = 1; if(i == e->nconns) e->nconns++; e->conns[i] = c; deprint(2, "%s: newconn %d\n", argv0, i); qunlock(e); return c; } } qunlock(e); return nil; }
static int cmdreq(Dev *d, int type, int req, int value, int index, uchar *data, int count) { int ndata, n; uchar *wp; uchar buf[8]; char *hd, *rs; assert(d != nil); if(data == nil){ wp = buf; ndata = 0; }else{ ndata = count; wp = emallocz(8+ndata, 0); } wp[0] = type; wp[1] = req; PUT2(wp+2, value); PUT2(wp+4, index); PUT2(wp+6, count); if(data != nil) memmove(wp+8, data, ndata); if(usbdebug>2){ hd = hexstr(wp, ndata+8); rs = reqstr(type, req); fprint(2, "%s: %s val %d|%d idx %d cnt %d out[%d] %s\n", d->dir, rs, value>>8, value&0xFF, index, count, ndata+8, hd); free(hd); }
void setstruts(Bool autohide) { int *struts; Atom net_wm_strut_partial; Atom net_wm_window_type; Atom net_wm_window_type_dock; XWindowAttributes wa; net_wm_strut_partial = XInternAtom(dpy, "_NET_WM_STRUT_PARTIAL", False); net_wm_window_type = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); net_wm_window_type_dock = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DOCK", False); struts = emallocz(12*sizeof(int)); XGetWindowAttributes(dpy, win, &wa); int x, y, w, h, b, d; XGetGeometry(dpy, win, &root, &x, &y, &w, &h, &b, &d); fprintf(stderr, "%d %d\n", x, y); fprintf(stderr, "x:%d y:%d ht: %d\n", wa.x, wa.y, DisplayHeight(dpy, screen)); if(bottom) { struts[Bottom] = DisplayHeight(dpy, screen) - my + mh; fprintf(stderr, "BotStrut=%d\n", struts[Bottom]); struts[BotStartX] = mx; struts[BotEndX] = mx+mw; } else { struts[Top] = mh + my; struts[TopStartX] = mx; struts[TopEndX] = mx+mw; } XChangeProperty(dpy, win, net_wm_strut_partial, XA_CARDINAL, 32, PropModeReplace, (unsigned char*)struts, 12); XChangeProperty(dpy, win, net_wm_window_type, XA_ATOM, 32, PropModeReplace, (unsigned char*)&net_wm_window_type_dock, 1); free(struts); }
Ep* mkep(Usbdev *d, int id) { Ep *ep; d->ep[id] = ep = emallocz(sizeof(Ep), 1); ep->id = id; return ep; }
static int confighub(Hub *h) { int type; uint8_t buf[128]; /* room for extra descriptors */ int i; Usbdev *d; DHub *dd; Port *pp; int nr; int nmap; uint8_t *PortPwrCtrlMask; int offset; int mask; d = h->dev->usb; for(i = 0; i < nelem(d->ddesc); i++) if(d->ddesc[i] == nil) break; else if(d->ddesc[i]->data.bDescriptorType == Dhub){ dd = (DHub*)&d->ddesc[i]->data; nr = Dhublen; goto Config; } type = Rd2h|Rclass|Rdev; nr = usbcmd(h->dev, type, Rgetdesc, Dhub<<8|0, 0, buf, sizeof buf); if(nr < Dhublen){ dprint(2, "%s: %s: getdesc hub: %r\n", argv0, h->dev->dir); return -1; } dd = (DHub*)buf; Config: h->nport = dd->bNbrPorts; nmap = 1 + h->nport/8; if(nr < 7 + 2*nmap){ fprint(2, "%s: %s: descr. too small\n", argv0, h->dev->dir); return -1; } h->port = emallocz((h->nport+1)*sizeof(Port), 1); h->pwrms = dd->bPwrOn2PwrGood*2; if(h->pwrms < Powerdelay) h->pwrms = Powerdelay; h->maxcurrent = dd->bHubContrCurrent; h->pwrmode = dd->wHubCharacteristics[0] & 3; h->compound = (dd->wHubCharacteristics[0] & (1<<2))!=0; h->leds = (dd->wHubCharacteristics[0] & (1<<7)) != 0; PortPwrCtrlMask = dd->DeviceRemovable + nmap; for(i = 1; i <= h->nport; i++){ pp = &h->port[i]; offset = i/8; mask = 1<<(i%8); pp->removable = (dd->DeviceRemovable[offset] & mask) != 0; pp->pwrctl = (PortPwrCtrlMask[offset] & mask) != 0; } return 0; }
XftColor* xftcolor(Image *i, Color *c) { XftColor *xc; xc = emallocz(sizeof *c); *xc = (XftColor) { pixelvalue(i, c), c->red, c->green, c->blue, c->alpha }; return freelater(xc); }
static Key* getkey(const char *name) { Key *k, *r; char buf[128]; char *seq[8]; char *kstr; int mask; uint i, toks; static ushort id = 1; r = nil; if((k = name2key(name))) { ungrabkey(k); return k; } utflcpy(buf, name, sizeof buf); toks = tokenize(seq, 8, buf, ','); for(i = 0; i < toks; i++) { if(!k) r = k = emallocz(sizeof *k); else { k->next = emallocz(sizeof *k); k = k->next; } utflcpy(k->name, name, sizeof k->name); if(parsekey(seq[i], &mask, &kstr)) { k->key = keycode(kstr); k->mod = mask; } if(k->key == 0) { freekey(r); return nil; } } if(r) { r->id = id++; r->lnext = key; key = r; } return r; }
void setcmd(int argc, char *argv[]) { int i; cmd = emallocz((argc+3) * sizeof(*cmd)); if (argc == 0) return; for(i = 0; i < argc; i++) cmd[i] = argv[i]; cmd[argc] = cmd[argc+1] = NULL; }
static void compileregs(void) { unsigned int i; regex_t *reg; for (i = 0; i < nrules; i++) { if (rules[i]->prop) { reg = emallocz(sizeof(regex_t)); if (regcomp(reg, rules[i]->prop, REG_EXTENDED)) free(reg); else rules[i]->propregex = reg; } if (rules[i]->tags) { reg = emallocz(sizeof(regex_t)); if (regcomp(reg, rules[i]->tags, REG_EXTENDED)) free(reg); else rules[i]->tagregex = reg; } } }
void parse_cpp(State *s) { int l, alloc; char *p, *buf; struct { char *name; void (*func)(State *, char *); } *d, dir[] = { { "include", doinclude }, { "define", dodefine }, { "ifndef", doifndef }, { "undef", doundef }, { "ifdef", doifdef }, { "endif", doendif }, { "elif", doelif }, { "else", doelse }, { "if", doif }, { "pragma", dopragma }, }; alloc = Memchunk; buf = emallocz(alloc, 0); while((buf = pass1(s, buf, &alloc)) != nil){ p = buf; while(isspace(*p)) p++; if(*p++ != '#') continue; while(isspace(*p)) p++; for(d = dir; d < &dir[nelem(dir)]; d++){ l = strlen(d->name); if(strncmp(p, d->name, l) == 0){ p += l; break; } } if(d >= &dir[nelem(dir)]) continue; while(isspace(*p)) p++; //print("%s:%d %s %s [%d -> ", s->file, s->line, d->name, p, peek(s)); d->func(s, p); //print("%d]\n", peek(s)); } free(buf); }
void manage(Window w, XWindowAttributes *wa) { Client *c, *t; Window trans; c = emallocz(sizeof(Client)); c->win = w; c->x = wa->x; c->y = wa->y; c->w = wa->width; c->h = wa->height; if(c->w == sw && c->h == sh) { c->border = 0; c->x = sx; c->y = sy; } else { c->border = BORDERPX; if(c->x + c->w + 2 * c->border > sx + sw) c->x = sx + sw - c->w - 2 * c->border; if(c->y + c->h + 2 * c->border > sy + sh) c->y = sy + sh - c->h - 2 * c->border; if(c->x < sx) c->x = sx; if(c->y < sy) c->y = sy; } updatesizehints(c); XSelectInput(dpy, c->win, StructureNotifyMask | PropertyChangeMask | EnterWindowMask); XGetTransientForHint(dpy, c->win, &trans); XSetWindowBorder(dpy, c->win, normcol); updatetitle(c); if((t = getclient(trans))) c->view = t->view; else c->view = view; if(clients) clients->prev = c; c->next = clients; c->snext = stack; stack = clients = c; XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); XMapWindow(dpy, c->win); setclientstate(c, NormalState); if(c->view == view) focus(c); arrange(); }
char * hexstr(void *a, int n) { int i; char *dbuff, *s, *e; uint8_t *b; b = a; dbuff = s = emallocz(1024, 0); *s = 0; e = s + 1024; for(i = 0; i < n; i++) s = seprint(s, e, " %.2ux", b[i]); if(s == e) fprint(2, "%s: usb/lib: hexdump: bug: small buffer\n", argv0); return dbuff; }
/** * Function: ixp_listen * * Params: * fs - The file descriptor on which to listen. * aux - A piece of data to store in the connection's * T<IxpConn> data structure. * read - The function to call when the connection has * data available to read. * close - A cleanup function to call when the * connection is closed. * * Starts the server P<s> listening on P<fd>. The optional * callbacks are called as described, with the connections * T<IxpConn> data structure as their arguments. * * Returns: * Returns the connection's new T<IxpConn> data * structure. * * S<IxpConn> */ IxpConn* ixp_listen(IxpServer *s, int fd, void *aux, void (*read)(IxpConn *c), void (*close)(IxpConn *c) ) { IxpConn *c; c = emallocz(sizeof *c); c->fd = fd; c->aux = aux; c->srv = s; c->read = read; c->close = close; c->next = s->conn; s->conn = c; return c; }
static void statusreader(void *u) { Areader *a; Channel *c; Packser *pk; Serialport *p; Serial *ser; int cl; p = u; ser = p->s; threadsetname("statusreader thread"); /* big buffering, fewer bytes lost */ c = chancreate(sizeof(Packser *), 128); a = emallocz(sizeof(Areader), 1); a->p = p; a->c = c; incref(ser->dev); proccreate(epreader, a, 16*1024); while((pk = recvp(c)) != nil){ memmove(p->data, pk->b, pk->nb); p->ndata = pk->nb; free(pk); dsprint(2, "serial %p: status reader %d \n", p, p->ndata); /* consume it all */ while(p->ndata != 0){ dsprint(2, "serial %p: status reader to consume: %d\n", p, p->ndata); cl = recvul(p->w4data); if(cl < 0) break; cl = sendul(p->gotdata, 1); if(cl < 0) break; } } shutdownchan(c); devctl(ser->dev, "detach"); closedev(ser->dev); usbfsdel(&p->fs); }
PaqDir * paqDirAlloc(Dir *dir, ulong offset) { PaqDir *pd; static ulong qid = 1; pd = emallocz(sizeof(PaqDir)); pd->name = strdup(dir->name); pd->qid = qid++; pd->mode = dir->mode & (DMDIR|DMAPPEND|0777); pd->mtime = dir->mtime; pd->length = dir->length; pd->uid = strdup(dir->uid); pd->gid = strdup(dir->gid); pd->offset = offset; return pd; }
void ewmh_update_net_current_desktop(void *p) { Monitor *m; unsigned long *seltags; unsigned int i; seltags = emallocz(ntags * sizeof(unsigned long)); for (m = monitors; m != NULL; m = m->next) { for (i = 0; i < ntags; i++) seltags[i] |= m->seltags[i]; } XChangeProperty(dpy, root, atom[ESelTags], XA_CARDINAL, 32, PropModeReplace, (unsigned char *) seltags, ntags); XChangeProperty(dpy, root, atom[CurDesk], XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &curmontag, 1); update_echinus_layout_name(NULL); free(seltags); }
static char* mkstr(uchar *b, int n) { Rune r; char *us; char *s; char *e; if(n <= 2 || (n & 1) != 0) return strdup("none"); n = (n - 2)/2; b += 2; us = s = emallocz(n*UTFmax+1, 0); e = s + n*UTFmax+1; for(; --n >= 0; b += 2){ r = GET2(b); s = seprint(s, e, "%C", r); } return us; }
static int wait4write(Serialport *p, uchar *data, int count) { int off, fd; uchar *b; Serial *ser; ser = p->s; b = emallocz(count+ser->outhdrsz, 1); off = ftsetouthdr(p, b, count); memmove(b+off, data, count); fd = p->epout->dfd; qunlock(ser); count = write(fd, b, count+off); qlock(ser); free(b); return count; }
int loaddevdesc(Dev *d) { uchar buf[Ddevlen+255]; int nr; int type; Ep *ep0; type = Rd2h|Rstd|Rdev; nr = sizeof(buf); memset(buf, 0, Ddevlen); if((nr=usbcmd(d, type, Rgetdesc, Ddev<<8|0, 0, buf, nr)) < 0) return -1; /* * Several hubs are returning descriptors of 17 bytes, not 18. * We accept them and leave number of configurations as zero. * (a get configuration descriptor also fails for them!) */ if(nr < Ddevlen){ print("%s: %s: warning: device with short descriptor\n", argv0, d->dir); if(nr < Ddevlen-1){ werrstr("short device descriptor (%d bytes)", nr); return -1; } } d->usb = emallocz(sizeof(Usbdev), 1); ep0 = mkep(d->usb, 0); ep0->dir = Eboth; ep0->type = Econtrol; ep0->maxpkt = d->maxpkt = 8; /* a default */ nr = parsedev(d, buf, nr); if(nr >= 0){ d->usb->vendor = loaddevstr(d, d->usb->vsid); if(strcmp(d->usb->vendor, "none") != 0){ d->usb->product = loaddevstr(d, d->usb->psid); d->usb->serial = loaddevstr(d, d->usb->ssid); } } return nr; }