Esempio n. 1
0
int main (int argc, char const *const *argv)
{
  stralloc sa = STRALLOC_ZERO ;
  for (;;)
  {
    struct tm tm ;
    uint64 tt ;
    time_t t ;
    char *p ;
    int r ;
    char fmt[UINT64_FMT] ;
    sa.len = 0 ;
    r = skagetln(buffer_0, &sa, '\n') ;
    if (r < 0) strerr_diefu1sys(111, "read from stdin") ;
    if (!r) break ;
    sa.s[sa.len-1] = 0 ;
    if (!strptime(sa.s, "+%Y-%m-%d", &tm)) continue ;
    tm.tm_sec = 59 ;
    tm.tm_min = 59 ;
    tm.tm_hour = 23 ;
    t = mktime(&tm) ;
    if (t < 0) strerr_diefu1sys(111, "mktime") ;
    tt = t + 10 ;
    add_leapsecs(&tt) ;
    if (!genalloc_append(uint64, &table, &tt))
      strerr_diefu1sys(111, "genalloc_append") ;
    fmt[uint64_fmt(fmt, tt)] = 0 ;
    buffer_puts(buffer_1, "  TAI_MAGIC + ") ;
    buffer_puts(buffer_1, fmt) ;
    buffer_puts(buffer_1, ",\n") ;
  }
  buffer_unput(buffer_1, 2) ;
  buffer_putsflush(buffer_1, "\n") ;
  return 0 ;
}
Esempio n. 2
0
int main (void)
{
  PROG = "s6-tai64nlocal" ;
  for (;;)
  {
    unsigned int p = 0 ;
    int r = skagetln(buffer_0f1, &satmp, '\n') ;
    if (r == -1)
      if (errno != EPIPE)
        strerr_diefu1sys(111, "read from stdin") ;
      else r = 1 ;
    else if (!r) break ;
    if (satmp.len > TIMESTAMP)
    {
      tain_t a ;
      p = timestamp_scan(satmp.s, &a) ;
      if (p)
      {
        char fmt[LOCALTMN_FMT+1] ;
        localtmn_t local ;
        unsigned int len ;
        localtmn_from_tain(&local, &a, 1) ;
        len = localtmn_fmt(fmt, &local) ;
        if (buffer_put(buffer_1, fmt, len) < 0)
          strerr_diefu1sys(111, "write to stdout") ;
      }
    }
    if (buffer_put(buffer_1, satmp.s + p, satmp.len - p) < 0)
      strerr_diefu1sys(111, "write to stdout") ;
    satmp.len = 0 ;
  }
  return 0 ;
}
Esempio n. 3
0
static int slurplines (genalloc *lines, char sep)
{
  unsigned int i = 0 ;
  for (;; i++)
  {
    stralloc sa = STRALLOC_ZERO ;
    int r = skagetln(buffer_0, &sa, sep) ;
    if (!r) break ;
    if ((r < 0) && ((errno != EPIPE) || !stralloc_catb(&sa, &sep, 1)))
      return -1 ;
    stralloc_shrink(&sa) ;
    if (!genalloc_append(stralloc, lines, &sa)) return -1 ;
  }
  return (int)i ;
}
Esempio n. 4
0
int opengetlnclose_at (int dirfd, char const *fn, stralloc *sa, int sep)
{
  char buf[BUFFER_INSIZE] ;
  buffer b ;
  register int r ;
  register int e ;
  int fd = open_readatb(dirfd, fn) ;
  if (fd < 0) return -1 ;
  buffer_init(&b, &fd_readsv, fd, buf, BUFFER_INSIZE) ;
  r = skagetln(&b, sa, sep) ;
  e = errno ;
  fd_close(fd) ;
  errno = e ;
  return r ;
}
int main (int argc, char const *const *argv)
{
  stralloc src = STRALLOC_ZERO ;
  PROG = "s6-unquote-filter" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "qQvwd:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'q': strictness = 0 ; break ;
        case 'Q': strictness = 1 ; break ;
        case 'v': strictness = 2 ; break ;
        case 'w': strictness = 3 ; break ;
        case 'd': delim = l.arg ; break ;
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  delimlen = str_len(delim) ;
  for (;;)
  {
    int r ;
    src.len = 0 ;
    r = skagetln(buffer_0f1, &src, '\n') ;
    if (!r) break ;
    if (r < 0)
    {
      if (errno != EPIPE) strerr_diefu1sys(111, "read from stdin") ;
    }
    else src.len-- ;
    if (!doit(src.s, src.len))
    {
      if (buffer_putalign(buffer_1, src.s, src.len) < (int)src.len)
        strerr_diefu1sys(111, "write to stdout") ;
    }
    if (r > 0)
    {
      if (buffer_putalign(buffer_1, "\n", 1) < 1)
        strerr_diefu1sys(111, "write to stdout") ;
    }
  }
  return 0 ;
}
Esempio n. 6
0
int main (int argc, char const *const *argv)
{
  unsigned int count = 0 ;
  flags_t flags = FLAGS_ZERO ;
  PROG = "s6-grep" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "Fcnqv", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'F': flags.fixed = 1 ; break ;
        case 'c': flags.count = 1 ; break ;
        case 'n': flags.num = 1 ; break ;
        case 'q': flags.quiet = 1 ; break ;
        case 'v': flags.not = 1 ; break ;
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) strerr_dieusage(100, USAGE) ;
  {
    stralloc line = STRALLOC_ZERO ;
    struct sredfa *re = 0 ;
    unsigned int num = 0 ;
    unsigned int arglen = 0 ;
    if (flags.fixed) arglen = str_len(argv[0]) ;
    else
    {
      re = sredfa_new() ;
      if (!re) strerr_diefu1sys(111, "sredfa_new") ;
      if (!sredfa_from_regexp(re, argv[0]))
        strerr_diefu1sys(111, "compile regular expression") ;
    }

    for (;;)
    {
      register int r ;
      line.len = 0 ;
      r = skagetln(buffer_0f1, &line, '\n') ;
      if (!r) break ;
      if (r < 0)
      {
        if ((errno != EPIPE) || !stralloc_catb(&line, "\n", 1))
          strerr_diefu1sys(111, "read from stdin") ;
      }
      num++ ;
      r = flags.fixed ?
        (str_strn(line.s, line.len-1, argv[0], arglen) < line.len - 1) :
        sredfa_match(re, line.s, line.len-1) ;
      if (r < 0)
      {
        int e = errno ;
        buffer_flush(buffer_1) ;
        errno = e ;
        strerr_diefu1sys(111, "match line against pattern") ;
      }
      if (r ^ flags.not)
      {
        count++ ;
        if (!flags.quiet && !flags.count)
        {
          if (flags.num)
          {
            char fmt[UINT_FMT] ;
            register unsigned int n = uint_fmt(fmt, num) ;
            fmt[n++] = ':' ;
            if (buffer_putalign(buffer_1, fmt, n) < (int)n)
              strerr_diefu1sys(111, "write to stdout") ;
          }
          if (buffer_putalign(buffer_1, line.s, line.len) < (int)line.len)
            strerr_diefu1sys(111, "write to stdout") ;
        }
      }
    }
    if (flags.quiet) return !count ;
    stralloc_free(&line) ;
    if (!flags.fixed) sredfa_delete(re) ;
  }
  if (flags.count)
  {
    char fmt[UINT_FMT] ;
    register unsigned int n = uint_fmt(fmt, count) ;
    fmt[n++] = '\n' ;
    if (buffer_putalign(buffer_1, fmt, n) < (int)n)
      strerr_diefu1sys(111, "write to stdout") ;
  }
  return !count ;
}
Esempio n. 7
0
static int doit (int fd, diuint const *s, unsigned int len, unsigned long flags, char delim)
{
  char buf[BUFFER_INSIZE] ;
  buffer b = BUFFER_INIT(&buffer_flush1read, fd, buf, BUFFER_INSIZE) ;
  for (;;)
  {
    int r ;
    satmp.len = 0 ;
    r = skagetln(&b, &satmp, '\n') ;
    if ((r == -1) && (errno != EPIPE)) return 0 ;
    if (!r) break ;
    if (flags & 2)
    {
      register unsigned int i = 0 ;
      for (; i < len ; i++)
      {
        register unsigned int j = s[i].right ;
        if (s[i].left >= satmp.len) break ;
        if (!j || (j > satmp.len))
        {
          j = satmp.len ;
          r = 0 ;
        }
        if (buffer_putalign(buffer_1, satmp.s + s[i].left - 1, j + 1 - s[i].left) == -1)
          return 0 ;
      }
    }
    else
    {
      register unsigned int i = 0, j = 0, count = 1 ;
      for (; i < len ; i++)
      {
        for (; count < s[i].left ; count++)
        {
          j += byte_chr(satmp.s + j, satmp.len - j, delim) ;
          if (j == satmp.len) break ;
          j++ ;
        }
        if (j == satmp.len)
        {
          if (count == 1)
          {
            if ((flags & 1) && (buffer_putalign(buffer_1, satmp.s, satmp.len) == -1))
              return 0 ;
            r = 0 ;
          }
          break ;
        }
        for (; !s[i].right || (count <= s[i].right) ; count++)
        {
          register unsigned int k = byte_chr(satmp.s + j, satmp.len - j, delim) ;
          if ((count > s[0].left) && (buffer_putalign(buffer_1, &delim, 1) == -1)) return 0 ;
          if (buffer_putalign(buffer_1, satmp.s + j, k) == -1) return 0 ;
          j += k ;
          if (j == satmp.len)
          {
            r = 0 ;
            break ;
          }
          j++ ;
        }
        if (j == satmp.len) break ;
      }
    }
    if ((r > 0) && (buffer_putalign(buffer_1, "\n", 1) == -1)) return 0 ;
  }
  return 1 ;
}