/* u2_cf_flat_save(): save `som` as `mod` at `pas`.
*/
u2_bean
u2_cf_flat_save(u2_noun mod,
                u2_noun pas,
                u2_noun som)
{
  c3_assert(c3__atom == mod);
  {
    c3_c*       pas_c = u2_cr_string(pas);
    c3_i    fid_i;
    c3_w    fln_w;
    c3_y*   fil_y;

    fid_i = open(pas_c, O_WRONLY | O_CREAT, 0666);
    free(pas_c);
    u2_cz(pas);

    if ( fid_i < 0 ) {
      perror(pas_c);
      u2_cz(som);
      return u2_no;
    }

    fln_w = u2_met(3, som);
    fil_y = c3_malloc(fln_w);
    u2_cr_bytes(0, fln_w, fil_y, som);
    u2_cz(som);

    if ( fln_w != write(fid_i, fil_y, fln_w) ) {
      return u2_no;
    }
    close(fid_i);

    return u2_yes;
  }
}
/* u2_cka_lte(): a * b.
*/
u2_noun
u2_cka_lte(u2_noun a, u2_noun b)
{
  u2_noun c = j2_mbc(Pt1, lte)(u2_Wire, a, b);

  u2_cz(a); u2_cz(b);
  return c;
}
/* u2_cka_mul(): a * b.
*/
u2_noun
u2_cka_mul(u2_noun a, u2_noun b)
{
  u2_noun c = j2_mbc(Pt1, mul)(u2_Wire, a, b);

  u2_cz(a); u2_cz(b);
  return c;
}
/* u2_cka_gth(): a + b.
*/
u2_noun
u2_cka_gth(u2_noun a, u2_noun b)
{
  u2_noun c = j2_mbc(Pt1, gth)(u2_Wire, a, b);

  u2_cz(a); u2_cz(b);
  return c;
}
/* u2_cka_sub(): a + b.
*/
u2_noun
u2_cka_sub(u2_noun a, u2_noun b)
{
  u2_noun c = j2_mbc(Pt1, sub)(u2_Wire, a, b);

  u2_cz(a); u2_cz(b);
  return c;
}
/* u2_cka_add(): a + b.
*/
u2_noun
u2_cka_add(u2_noun a, u2_noun b)
{
  u2_noun c = j2_mbc(Pt1, add)(u2_Wire, a, b);

  u2_cz(a); u2_cz(b);
  return c;
}
/* u2_ckc_rsh(): right shift.
*/
u2_noun
u2_ckc_rsh(u2_noun a, u2_noun b, u2_noun c)
{
  u2_noun d = j2_mbc(Pt3, rsh)(u2_Wire, a, b, c);

  u2_cz(a); u2_cz(b); u2_cz(c);
  return d;
}
/* u2_ckb_weld(): concatenate lists `a` before `b`.
*/
u2_noun
u2_ckb_weld(u2_noun a, u2_noun b)
{
  u2_noun c = j2_mbc(Pt2, weld)(u2_Wire, a, b);

  u2_cz(a); u2_cz(b);
  return c;
}
/* u2_ckd_in_tap(): map/set convert to list.  (solves by_tap also.)
*/
u2_noun
u2_ckd_in_tap(u2_noun a, u2_noun b)
{
  u2_weak c = _coal_in_tap(u2_Wire, a, b);

  u2_cz(a); u2_cz(b);
  if ( u2_none == c ) {
    return u2_cm_bail(c3__exit);
  }
  else return c;
}
/* u2_ckd_by_put(): map put for key `b`, value `c` in map `a`.
*/
u2_weak
u2_ckd_by_put(u2_noun a, u2_noun b, u2_noun c)
{
  // Bizarre asymmetry in old jets.
  //
  // (Mysterious comment in old glue code.)
  //
  u2_noun pro = _coal_by_put(u2_Wire, a, b, c);

  u2_cz(a); u2_cz(b); u2_cz(c);
  return pro;
}
/* u2_cf_flat_date(): date for `pas`.  Unix time * 10^6, or 0.
*/
c3_d
u2_cf_flat_date(u2_noun pas)
{
  c3_c*       pas_c = u2_cr_string(pas);
  struct stat pas_s;

  u2_cz(pas);
  if ( stat(pas_c, &pas_s) < 0 ) {
    free(pas_c);

    return 0;
  } else {
    free(pas_c);
#if defined(U2_OS_linux)
    return ( ((c3_d)pas_s.st_mtime) );
#elif defined(U2_OS_osx)
    return ( ((c3_d)pas_s.st_mtimespec.tv_sec) +
             ((c3_d)(pas_s.st_mtimespec.tv_nsec / 1000)) );
#elif defined(U2_OS_bsd)
    return ( ((c3_d)pas_s.st_mtim.tv_sec) +
             ((c3_d)(pas_s.st_mtim.tv_nsec / 1000)) );
#else
    #error "port: filetime"
#endif
  }
}
/* u2_ckd_by_get(): map get for key `b` in map `a` with u2_none.
*/
u2_weak
u2_ckd_by_get(u2_noun a, u2_noun b)
{
  u2_noun c = _coal_by_get(u2_Wire, a, b);

  u2_cz(a); u2_cz(b);
  if ( u2_no == u2_cr_du(c) ) {
    u2_cz(c);
    return u2_none;
  } else {
    u2_noun pro = u2_ct(u2t(c));

    u2_cz(c);
    return pro;
  }
}
u2_noun
u2_cf_path(c3_c* top_c,
           c3_c* ext_c,
           u2_noun tah)
{
  c3_w    top_w = strlen(top_c);
  c3_w    len_w = _cf_path_1(0, (top_w + 1), tah);
  c3_w    buf_w = len_w + (ext_c ? (1 + strlen(ext_c)) : 0);
  c3_c*   buf_c = c3_malloc(buf_w + 1);
  c3_w    pos_w;
  u2_noun pas;

  strncpy(buf_c, top_c, buf_w);
  buf_c[buf_w] = '\0';
  pos_w = top_w;
  buf_c[pos_w++] = '/';

  pos_w = _cf_path_1(buf_c, pos_w, tah);

  if ( ext_c ) {
    buf_c[pos_w++] = '.';
    strncpy(buf_c + pos_w, ext_c, buf_w - pos_w);
  } else {
    buf_c[pos_w] = 0;
  }

  pas = u2_ci_string(buf_c);
  free(buf_c);

  u2_cz(tah);
  return pas;
}
/* u2_ckb_flop(): reverse list `a`.
*/
u2_noun
u2_ckb_flop(u2_noun a)
{
  u2_noun b = j2_mbc(Pt2, flop)(u2_Wire, a);

  u2_cz(a);
  return b;
}
/* u2_ckb_lent(): length of list `a`.
*/
u2_noun
u2_ckb_lent(u2_noun a)
{
  u2_noun b = j2_mbc(Pt2, lent)(u2_Wire, a);

  u2_cz(a);
  return b;
}
示例#16
0
u2_noun
u2_cke_cue(u2_atom a)
{
  u2_noun b = _cue_internal(u2_Wire, a);

  u2_cz(a);
  return b;
}
/* u2_cke_trip(): atom to tape.
*/
u2_atom
u2_cke_trip(u2_noun a)
{
  u2_atom b = _coal_trip(u2_Wire, a);

  u2_cz(a);
  return b;
}
u2_noun
u2_cke_cue(u2_atom a)
{
  u2_noun b = _coal_cue(u2_Wire, a);

  u2_cz(a);
  return b;
}
/* u2_cn_mung():
**
**   Call `(function sample)`.
*/
u2_noun
u2_cn_mung(u2_noun fun,
           u2_noun sam)
{
  u2_noun pro = u2_bn_mong(u2_Wire, fun, sam);

  u2_cz(fun);
  return pro;
}
/* u2_cm_drop(): drop from meaning stack.
*/
void
u2_cm_drop()
{
  u2_noun tax = u2_wire_tax(u2_Wire);

  c3_assert(u2_nul != tax);
  u2_wire_tax(u2_Wire) = u2_ct(u2t(tax));
  u2_cz(tax);
}
/* u2_cm_bury(): store fresh or volatile noun `som` to freezer.
*/
u2_weak
u2_cm_bury(u2_weak som)
{
  if ( u2_none == som ) return som;

  if ( u2_no == u2_rl_junior(u2_wire_bas_r(u2_Wire), som) ) {
    return som;
  }
  else {
    u2_noun pro = u2_rl_take(u2_wire_bas_r(u2_Wire), som);

    u2_cz(som);
    return pro;
  }
}
/* u2_cf_flat_load(): load `mod` at `pas`.  Bail on error.
*/
u2_weak
u2_cf_flat_load(u2_noun mod,
                u2_noun pas)
{
  c3_assert(c3__atom == mod);
  {
    c3_c*       pas_c = u2_cr_string(pas);
    c3_i        fid_i;
    struct stat sat_s;
    c3_w        fln_w;
    c3_c*       fil_c;
    u2_atom     fil;

    fid_i = open(pas_c, O_RDONLY, 0644);
    free(pas_c);
    u2_cz(pas);

    if ( (fid_i < 0) || (fstat(fid_i, &sat_s) < 0) ) {
      perror(pas_c);
      return u2_none;
    }

    fln_w = sat_s.st_size;
    fil_c = c3_malloc(sat_s.st_size);

    if ( fln_w != read(fid_i, fil_c, fln_w) ) {
      return u2_none;
    }
    close(fid_i);

    fil = u2_ci_bytes(fln_w, (c3_y *)fil_c);
    free(fil_c);

    return fil;
  }
}
示例#23
0
文件: nock.c 项目: sillsm/urbit
/* _nock_cool(): nock, transferring arguments.
*/
static u2_noun
_nock_cool(u2_noun bus,
           u2_noun fol)
{
    u2_noun hib, gal;

    while ( 1 ) {
        u2_tx_did_hop(u2_Wire, 1);

        if ( u2_no == u2du(fol) ) {
            return u2_cm_bail(c3__exit);
        }
        else {
            hib = u2fh(fol);
            gal = u2ft(fol);
        }

        if ( u2_yes == u2du(hib) ) {
            u2_noun poz, riv;

            poz = _nock_cool(u2k(bus), u2k(hib));
            riv = _nock_cool(bus, u2k(gal));

            u2z(fol);
            return u2_cn_cell(poz, riv);
        }
        else switch ( hib ) {
            default:
                return u2_cm_bail(c3__exit);

            case 0: {
                if ( u2_no == u2_cr_ud(gal) ) {
                    return u2_cm_bail(c3__exit);
                }
                else {
                    u2_noun pro = u2k(u2at(gal, bus));

                    u2z(bus);
                    u2z(fol);
                    return pro;
                }
            }
            case 1: {
                u2_noun pro = u2k(gal);

                u2z(bus);
                u2z(fol);
                return pro;
            }
            c3_assert(!"not reached");

            case 2: {
                if ( u2_no == u2du(gal) ) {
                    return u2_cm_bail(c3__exit);
                }
                else {
                    u2_noun nex = _nock_cool(u2k(bus), u2k(u2ft(gal)));
                    u2_noun seb = _nock_cool(bus, u2k(u2fh(gal)));

                    u2z(fol);
                    bus = seb;
                    fol = nex;
                    continue;
                }
            }
            c3_assert(!"not reached");

            case 3: {
                u2_noun gof, pro;

                gof = _nock_cool(bus, u2k(gal));
                pro = u2du(gof);

                u2z(gof);
                u2z(fol);
                return pro;
            }
            c3_assert(!"not reached");

            case 4: {
                u2_noun gof, pro;

                gof = _nock_cool(bus, u2k(gal));
                if ( (u2_none == (pro = u2_rl_vint(u2_Wire, gof))) ) {
                    return u2_cm_bail(c3__exit);
                }

                u2z(gof);
                u2z(fol);
                return pro;
            }
            c3_assert(!"not reached");

            case 5: {
                if ( u2_no == u2du(gal) ) {
                    return u2_cm_bail(c3__exit);
                }
                else {
                    u2_noun wim = _nock_cool(bus, u2k(gal));
                    u2_noun pro;

                    if ( u2_no == u2du(wim) ) {
                        return u2_cm_bail(c3__exit);
                    }
                    else pro = u2_cr_sing(u2h(wim), u2t(wim));

                    u2z(wim);
                    u2z(fol);
                    return pro;
                }
            }
            c3_assert(!"not reached");

            case 6: {
                u2_noun b_gal, c_gal, d_gal;

                u2_cx_trel(gal, &b_gal, &c_gal, &d_gal);
                {
                    u2_noun tys = _nock_cool(u2k(bus), u2k(b_gal));
                    u2_noun nex;

                    if ( 0 == tys ) {
                        nex = u2k(c_gal);
                    } else if ( 1 == tys ) {
                        nex = u2k(d_gal);
                    } else return u2_cm_bail(c3__exit);

                    u2z(fol);
                    fol = nex;
                    continue;
                }
            }
            c3_assert(!"not reached");

            case 7: {
                u2_noun b_gal, c_gal;

                u2_cx_cell(gal, &b_gal, &c_gal);
                {
                    u2_noun bod = _nock_cool(bus, u2k(b_gal));
                    u2_noun nex = u2k(c_gal);

                    u2z(fol);
                    bus = bod;
                    fol = nex;
                    continue;
                }
            }
            c3_assert(!"not reached");

            case 8: {
                u2_noun b_gal, c_gal;

                // c3_assert(!"got 8 (nock)!");
                u2_cx_cell(gal, &b_gal, &c_gal);
                {
                    u2_noun bod = u2nc(_nock_cool(u2k(bus), u2k(b_gal)), bus);
                    u2_noun nex = u2k(c_gal);

                    u2z(fol);
                    bus = bod;
                    fol = nex;
                    continue;
                }
            }
            c3_assert(!"not reached");

            case 9: {
                u2_noun b_gal, c_gal;

                u2_cx_cell(gal, &b_gal, &c_gal);
                if ( u2_no == u2ud(b_gal) ) {
                    return u2_cm_bail(c3__exit);
                }
                else {
                    u2_noun seb = _nock_cool(bus, u2k(c_gal));
                    u2_weak xip;

                    u2_tx_sys_bit(u2_Wire, u2_yes);
                    xip = u2_ds_find(u2_Wire, seb);

                    if ( u2_none != xip ) {
                        u2_noun pro = u2_ho_kick(u2_Wire, xip, seb, b_gal);

                        u2_tx_sys_bit(u2_Wire, u2_no);
                        if ( u2_none == pro ) {
                            return u2_cm_bail(c3__exit);
                        }
                        else {
                            u2z(seb);
                            u2z(fol);
                            return pro;
                        }
                    }
                    else {
                        u2_tx_sys_bit(u2_Wire, u2_no);
                        {
                            u2_noun nex = u2_ct(u2at(b_gal, seb));

                            u2z(fol);
                            bus = seb;
                            fol = nex;
                            continue;
                        }
                    }
                }
            }
            c3_assert(!"not reached");

            case 10: {
                u2_noun p_gal, q_gal;

                u2_cx_cell(gal, &p_gal, &q_gal);
                {
                    u2_noun zep, hod, nex, pro;

                    if ( u2_yes == u2du(p_gal) ) {
                        u2_noun b_gal = u2fh(p_gal);
                        u2_noun c_gal = u2ft(p_gal);
                        u2_noun d_gal = q_gal;

                        zep = u2k(b_gal);
                        hod = _nock_cool(u2k(bus), u2_ct(c_gal));
                        nex = u2_ct(d_gal);
                    }
                    else {
                        u2_noun b_gal = p_gal;
                        u2_noun c_gal = q_gal;

                        zep = u2k(b_gal);
                        hod = u2_nul;
                        nex = u2_ct(c_gal);
                    }

                    u2_cz(fol);
                    pro = _nock_hint(zep, hod, bus, nex, 0);
                    return pro;
                }
            }

            case 11: {
                u2_noun gof, pro;

                gof = _nock_cool(bus, u2k(gal));
                pro = _nock_pray_cool(gof);

                u2z(fol);
                return pro;
            }
            c3_assert(!"not reached");
            }
    }
}