<commit_before>/* ISC license. */

#include <sys/types.h>
#include <stdint.h>
#include <errno.h>
#include <skalibs/gccattributes.h>
#include <skalibs/error.h>
#include <skalibs/uint16.h>
#include <skalibs/genalloc.h>
#include <skalibs/gensetdyn.h>
#include <skalibs/unixmessage.h>
#include <skalibs/skaclient.h>
#include <s6/ftrigr.h>

static inline int appears (uint16_t, uint16_t const *, size_t) gccattr_pure ;

static inline int appears (uint16_t id, uint16_t const *list, size_t len)
{
  while (len) if (id == list[--len]) return 1 ;
  return 0 ;
}

static int msghandler (unixmessage_t const *m, void *context)
{
  ftrigr_t *a = (ftrigr_t *)context ;
  ftrigr1_t *p ;
  int addit = 1 ;
  uint16_t id ;
  if (m->len != 4 || m->nfds) return (errno = EPROTO, 0) ;
  uint16_unpack_big(m->s, &id) ;
  p = GENSETDYN_P(ftrigr1_t, &a->data, id) ;
  if (!p) return 1 ;
  if (p->state != FR1STATE_LISTENING) return (errno = EINVAL, 0) ;
  if (!genalloc_readyplus(uint16_t, &a->list, 1)) return 0 ;
  switch (m->s[2])
  {
    case 'd' :
      p->state = FR1STATE_WAITACK ;
      break ;
    case '!' :
      if (p->options & FTRIGR_REPEAT)
      {
        if (p->count++
         && appears(id+1, genalloc_s(uint16_t, &a->list), genalloc_len(uint16_t, &a->list)))
          addit = 0 ;
      }
      else p->state = FR1STATE_WAITACKDATA ;
      break ;
    default : return (errno = EPROTO, 0) ;
  }
  p->what = m->s[3] ;
  if (addit)
  {
    id++ ; genalloc_append(uint16_t, &a->list, &id) ;
  }
  return 1 ;
}

int ftrigr_update (ftrigr_t *a)
{
  genalloc_setlen(uint16_t, &a->list, 0) ;
  return skaclient_update(&a->connection, &msghandler, a) ;
}
<commit_msg> Actually fix the return value of ftrigr_update() (thanks jjk)<commit_after>/* ISC license. */

#include <sys/types.h>
#include <stdint.h>
#include <errno.h>
#include <skalibs/gccattributes.h>
#include <skalibs/error.h>
#include <skalibs/uint16.h>
#include <skalibs/genalloc.h>
#include <skalibs/gensetdyn.h>
#include <skalibs/unixmessage.h>
#include <skalibs/skaclient.h>
#include <s6/ftrigr.h>

static inline int appears (uint16_t, uint16_t const *, size_t) gccattr_pure ;

static inline int appears (uint16_t id, uint16_t const *list, size_t len)
{
  while (len) if (id == list[--len]) return 1 ;
  return 0 ;
}

static int msghandler (unixmessage_t const *m, void *context)
{
  ftrigr_t *a = (ftrigr_t *)context ;
  ftrigr1_t *p ;
  int addit = 1 ;
  uint16_t id ;
  if (m->len != 4 || m->nfds) return (errno = EPROTO, 0) ;
  uint16_unpack_big(m->s, &id) ;
  p = GENSETDYN_P(ftrigr1_t, &a->data, id) ;
  if (!p) return 1 ;
  if (p->state != FR1STATE_LISTENING) return (errno = EINVAL, 0) ;
  if (!genalloc_readyplus(uint16_t, &a->list, 1)) return 0 ;
  switch (m->s[2])
  {
    case 'd' :
      p->state = FR1STATE_WAITACK ;
      break ;
    case '!' :
      if (p->options & FTRIGR_REPEAT)
      {
        if (p->count++
         && appears(id+1, genalloc_s(uint16_t, &a->list), genalloc_len(uint16_t, &a->list)))
          addit = 0 ;
      }
      else p->state = FR1STATE_WAITACKDATA ;
      break ;
    default : return (errno = EPROTO, 0) ;
  }
  p->what = m->s[3] ;
  if (addit)
  {
    id++ ; genalloc_append(uint16_t, &a->list, &id) ;
  }
  return 1 ;
}

int ftrigr_update (ftrigr_t *a)
{
  int r ;
  genalloc_setlen(uint16_t, &a->list, 0) ;
  r = skaclient_update(&a->connection, &msghandler, a) ;
  return r < 0 ? r : (int)genalloc_len(uint16_t, &a->list) ;
}

{'fraction_non_alphanumeric': 0.16419213973799127, 'fraction_numerical': 0.027656477438136828, 'mean_word_length': 3.2472187886279356, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2728572', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1465, 'n_words': 325}