nb

Non-blocking I/O for picoLisp
git clone https://logand.com/git/nb.git/
Log | Files | Refs | README

nb.l (1387B)


      1 (load "lib/gcc.l")
      2 
      3 (gcc "nb" NIL 'eagain 'block 'rdx 'wrx)
      4 
      5 //(eagain) -> 'cnt
      6 any eagain(any ex __attribute__((unused))) {
      7    return boxCnt(-EAGAIN);
      8 }
      9 
     10 //(block 'any 'flg) -> 'flg
     11 any block(any ex) {
     12    int sd = (int)evCnt(ex,cdr(ex));
     13    any y = EVAL(caddr(ex));
     14    bool flg = isNil(y) ? NO : YES;
     15    blocking(flg, ex, sd);
     16    return y;
     17 }
     18 
     19 //(rdx 'lst 'cnt ['cnt]) -> 'cnt|NIL
     20 any rdx(any ex) {
     21    any lst = EVAL(cadr(ex));
     22    int cnt = (int)evCnt(ex,cddr(ex));
     23    int off = isNil(cadddr(ex)) ? 0 : (int)evCnt(ex,cdddr(ex));
     24    int i = 0;
     25    int j = 0;
     26    NeedLst(ex,lst);
     27    byte buf[cnt];
     28    int n = read(InFile->fd, buf, cnt);
     29    if (0 < n) {
     30       for (; j < off && isCell(lst); lst = cdr(lst), j++);
     31       for (; i < n && i < cnt && isCell(lst); lst = cdr(lst), i++) {
     32          lst->car = boxCnt(buf[i]);
     33       }
     34    }
     35    return n == 0 ? Nil : boxCnt(n < 0 ? -errno : i);
     36 }
     37 
     38 //(wrx 'lst 'cnt ['cnt]) -> 'cnt|NIL
     39 any wrx(any ex) {
     40    any lst = EVAL(cadr(ex));
     41    int cnt = (int)evCnt(ex,cddr(ex));
     42    int off = isNil(cadddr(ex)) ? 0 : (int)evCnt(ex,cdddr(ex));
     43    int i = 0;
     44    int j = 0;
     45    NeedLst(ex,lst);
     46    byte buf[cnt];
     47    for (; j < off && isCell(lst); lst = cdr(lst), j++);
     48    for (; i < cnt && isCell(lst); lst = cdr(lst), i++) {
     49       buf[i] = (byte)evCnt(ex,lst);
     50    }
     51    int n = write(OutFile->fd, buf, i);
     52    return n == 0 ? Nil : boxCnt(n < 0 ? -errno : n);
     53 }
     54 /**/