Ejemplo n.º 1
0
static void
test_lex(const char *input)
{
    struct ds output;

    ds_init(&output);
    struct lexer lexer;

    lexer_init(&lexer, input);
    ds_clear(&output);
    while (lexer_get(&lexer) != LEX_T_END) {
        size_t len = output.length;
        lex_token_format(&lexer.token, &output);

        /* Check that the formatted version can really be parsed back
         * losslessly. */
        if (lexer.token.type != LEX_T_ERROR) {
            const char *s = ds_cstr(&output) + len;
            struct lexer l2;

            lexer_init(&l2, s);
            lexer_get(&l2);
            compare_token(&lexer.token, &l2.token);
            lexer_destroy(&l2);
        }
        ds_put_char(&output, ' ');
    }
    lexer_destroy(&lexer);

    ds_chomp(&output, ' ');
    puts(ds_cstr(&output));
    ds_destroy(&output);
}
Ejemplo n.º 2
0
/** Resets the message factory for reuse.
 *  \relates adbus_MsgFactory
 */
void adbus_msg_reset(adbus_MsgFactory* m)
{
    m->messageType      = ADBUS_MSG_INVALID;
    m->flags            = 0;
    m->serial           = -1;
    m->argumentOffset   = 0;
    m->replySerial      = 0;
    m->hasReplySerial   = 0;
    adbus_buf_reset(m->buf);
    adbus_buf_reset(m->argbuf);
    ds_clear(&m->interface);
    ds_clear(&m->member);
    ds_clear(&m->error);
    ds_clear(&m->destination);
    ds_clear(&m->sender);
}
Ejemplo n.º 3
0
static void
flush_help_string(struct ds *ds)
{
    if (ds->length > 2 ) {
        ds->length -= 2;
        printf ("%s\n", ds_cstr(ds));
        ds_clear(ds);
    }
}
Ejemplo n.º 4
0
/* Expands tabs in the current line into the equivalent number of
   spaces, if appropriate for this kind of file.  Aborts if
   reading from the file is necessary or at end of file, so call
   dfm_eof() first.*/
void
dfm_expand_tabs (struct dfm_reader *r)
{
  size_t ofs, new_pos, tab_width;

  assert ((r->flags & DFM_ADVANCE) == 0);
  assert (r->eof_cnt == 0);

  if (r->flags & DFM_TABS_EXPANDED)
    return;
  r->flags |= DFM_TABS_EXPANDED;

  if (r->fh != fh_inline_file ()
      && (fh_get_mode (r->fh) != FH_MODE_TEXT
          || fh_get_tab_width (r->fh) == 0
          || ds_find_byte (&r->line, '\t') == SIZE_MAX))
    return;

  /* Expand tabs from r->line into r->scratch, and figure out
     new value for r->pos. */
  tab_width = fh_get_tab_width (r->fh);
  ds_clear (&r->scratch);
  new_pos = SIZE_MAX;
  for (ofs = 0; ofs < ds_length (&r->line); ofs++)
    {
      unsigned char c;

      if (ofs == r->pos)
        new_pos = ds_length (&r->scratch);

      c = ds_data (&r->line)[ofs];
      if (c != '\t')
        ds_put_byte (&r->scratch, c);
      else
        {
          do
            ds_put_byte (&r->scratch, ' ');
          while (ds_length (&r->scratch) % tab_width != 0);
        }
    }
  if (new_pos == SIZE_MAX)
    {
      /* Maintain the same relationship between position and line
         length that we had before.  DATA LIST uses a
         beyond-the-end position to deal with an empty field at
         the end of the line. */
      assert (r->pos >= ds_length (&r->line));
      new_pos = (r->pos - ds_length (&r->line)) + ds_length (&r->scratch);
    }

  /* Swap r->line and r->scratch and set new r->pos. */
  ds_swap (&r->line, &r->scratch);
  r->pos = new_pos;
}
Ejemplo n.º 5
0
/* Breaks 'words' into words at white space, respecting shell-like quoting
 * conventions, and appends the words to 'svec'. */
void
svec_parse_words(struct svec *svec, const char *words)
{
    struct ds word = DS_EMPTY_INITIALIZER;
    const char *p, *q;

    for (p = words; *p != '\0'; p = q) {
        int quote = 0;

        while (isspace((unsigned char) *p)) {
            p++;
        }
        if (*p == '\0') {
            break;
        }

        ds_clear(&word);
        for (q = p; *q != '\0'; q++) {
            if (*q == quote) {
                quote = 0;
            } else if (*q == '\'' || *q == '"') {
                quote = *q;
            } else if (*q == '\\' && (!quote || quote == '"')) {
                q++;
                if (*q == '\0') {
                    VLOG_WARN(LOG_MODULE, "%s: ends in trailing backslash", words);
                    break;
                }
                ds_put_char(&word, *q);
            } else if (isspace((unsigned char) *q) && !quote) {
                q++;
                break;
            } else {
                ds_put_char(&word, *q);
            }
        }
        svec_add(svec, ds_cstr(&word));
        if (quote) {
            VLOG_WARN(LOG_MODULE, "%s: word ends inside quoted string", words);
        }
    }
    ds_destroy(&word);
}
Ejemplo n.º 6
0
/* Checks that test state TS matches the test model TM and
   reports any mismatches via mc_error.  Then, adds SX to MC as a
   new state. */
static void
check_state (struct mc *mc, struct test_state *ts, const struct test_model *tm)
{
  const struct test_params *params = mc_get_aux (mc);
  int n_columns = params->n_columns;
  unsigned int hash;
  int i;

  for (i = 0; i < params->n_xarrays; i++)
    {
      const struct xarray_model *model = &tm->models[i];
      const struct sparse_xarray *sx = ts->xarrays[i];
      bool difference;
      int row, col;
      int n_rows;

      assert (n_columns < MAX_COLS);

      /* Check row count. */
      n_rows = 0;
      for (row = 0; row < params->max_rows; row++)
        if (model->contains_row[row])
          n_rows = row + 1;
      if (n_rows != sparse_xarray_get_n_rows (sx))
        mc_error (mc, "xarray %d: row count (%zu) does not match expected "
                  "(%d)", i, sparse_xarray_get_n_rows (sx), n_rows);

      /* Check row containment. */
      for (row = 0; row < params->max_rows; row++)
        {
          bool contains = sparse_xarray_contains_row (sx, row);
          if (contains && !model->contains_row[row])
            mc_error (mc, "xarray %d: row %d is contained by sparse_xarray "
                      "but should not be", i, row);
          else if (!contains && model->contains_row[row])
            mc_error (mc, "xarray %d: row %d is not contained by "
                      "sparse_xarray but should be", i, row);
        }

      /* Check contents. */
      difference = false;
      for (row = 0; row < params->max_rows; row++)
        {
          unsigned char data[MAX_COLS];

          if (!sparse_xarray_read (sx, row, 0, n_columns, data))
            NOT_REACHED ();
          for (col = 0; col < params->n_columns; col++)
            if (data[col] != model->data[row][col])
              {
                mc_error (mc, "xarray %d: element %d,%d (of %d,%d) "
                          "differs: %d should be %d",
                          i, row, col, n_rows, n_columns, data[col],
                          model->data[row][col]);
                difference = true;
              }
        }

      if (difference)
        {
          struct string ds;

          mc_error (mc, "xarray %d: expected:", i);
          ds_init_empty (&ds);
          for (row = 0; row < params->max_rows; row++)
            {
              ds_clear (&ds);
              for (col = 0; col < n_columns; col++)
                ds_put_format (&ds, " %d", model->data[row][col]);
              mc_error (mc, "xarray %d: row %d:%s", i, row, ds_cstr (&ds));
            }

          mc_error (mc, "xarray %d: actual:", i);
          ds_init_empty (&ds);
          for (row = 0; row < params->max_rows; row++)
            {
              unsigned char data[MAX_COLS];

              if (!sparse_xarray_read (sx, row, 0, n_columns, data))
                NOT_REACHED ();

              ds_clear (&ds);
              for (col = 0; col < n_columns; col++)
                ds_put_format (&ds, " %d", data[col]);
              mc_error (mc, "xarray %d: row %d:%s", i, row, ds_cstr (&ds));
            }

          ds_destroy (&ds);
        }
    }

  hash = 0;
  for (i = 0; i < params->n_xarrays; i++)
    hash = sparse_xarray_model_checker_hash (ts->xarrays[i], hash);
  if (mc_discard_dup_state (mc, hash))
    test_state_destroy (params, ts);
  else
    mc_add_state (mc, ts);
}
Ejemplo n.º 7
0
/* Reads a record from a disk file into R.
   Returns true if successful, false on error or at end of file. */
static bool
read_file_record (struct dfm_reader *r)
{
  assert (r->fh != fh_inline_file ());

  ds_clear (&r->line);
  switch (fh_get_mode (r->fh))
    {
    case FH_MODE_TEXT:
      if (ds_read_line (&r->line, r->file, SIZE_MAX))
        {
          ds_chomp_byte (&r->line, '\n');
          return true;
        }
      else
        {
          if (ferror (r->file))
            read_error (r);
          return false;
        }

    case FH_MODE_FIXED:
      if (ds_read_stream (&r->line, 1, fh_get_record_width (r->fh), r->file))
        return true;
      else
        {
          if (ferror (r->file))
            read_error (r);
          else if (!ds_is_empty (&r->line))
            partial_record (r);
          return false;
        }

    case FH_MODE_VARIABLE:
      {
        size_t leading_size;
        size_t trailing_size;
        int status;

        /* Read leading record size. */
        status = read_size (r, &leading_size);
        if (status <= 0)
          return false;

        /* Read record data. */
        if (!ds_read_stream (&r->line, leading_size, 1, r->file))
          {
            if (ferror (r->file))
              read_error (r);
            else
              partial_record (r);
            return false;
          }

        /* Read trailing record size and check that it's the same
           as the leading record size. */
        status = read_size (r, &trailing_size);
        if (status <= 0)
          {
            if (status == 0)
              partial_record (r);
            return false;
          }
        if (leading_size != trailing_size)
          {
            corrupt_size (r);
            return false;
          }

        return true;
      }

    case FH_MODE_360_VARIABLE:
    case FH_MODE_360_SPANNED:
      for (;;)
        {
          size_t record_size;
          int segment;
          int status;

          /* If we've exhausted our current block, start another
             one by reading the new block descriptor word. */
          if (r->block_left == 0)
            {
              status = read_descriptor_word (r, BLOCK, &r->block_left, NULL);
              if (status < 0)
                return false;
              else if (status == 0)
                return !ds_is_empty (&r->line);
            }

          /* Read record descriptor. */
          if (r->block_left < 4)
            {
              partial_record (r);
              return false;
            }
          r->block_left -= 4;
          status = read_descriptor_word (r, RECORD, &record_size, &segment);
          if (status <= 0)
            {
              if (status == 0)
                partial_record (r);
              return false;
            }
          if (record_size > r->block_left)
            {
              msg (ME, _("Record exceeds remaining block length."));
              return false;
            }

          /* Read record data. */
          if (!ds_read_stream (&r->line, record_size, 1, r->file))
            {
              if (ferror (r->file))
                read_error (r);
              else
                partial_record (r);
              return false;
            }
          r->block_left -= record_size;

          /* In variable mode, read only a single record.
             In spanned mode, a segment value of 0 should
             designate a whole record without spanning, 1 the
             first segment in a record, 2 the last segment in a
             record, and 3 an intermediate segment in a record.
             For compatibility, though, we actually pay attention
             only to whether the segment value is even or odd. */
          if (fh_get_mode (r->fh) == FH_MODE_360_VARIABLE
              || (segment & 1) == 0)
            return true;
        }
    }

  NOT_REACHED ();
}
Ejemplo n.º 8
0
static void
table_print_table_line__(struct ds *line)
{
    puts(ds_cstr(line));
    ds_clear(line);
}
Ejemplo n.º 9
0
/**
 * Clear the data structure.
 * The packet container is freed using the memory object. If the memory object
 * is NULL then the packet container memory is not freed.
 * container needs to be freed elsewhere.
 * @param dlPtr
 * @param mObj The memory object reference that will be used for freeing the
 * packet containers in the data structure.
 * @return The \c error-code if any error during memory operations else
 * success is returned.
 */
genErr_t
ds_dl_clear (ds_dlist dlPtr, bs_mmodCls mObj)
{
  return ds_clear (&dlPtr->base, mObj);
}
Ejemplo n.º 10
0
/**
 * Clear the data structure.
 * The packet container is freed using the memory object. If the memory object
 * is NULL then the packet container memory is not freed.
 * container needs to be freed elsewhere.
 * @param clPtr
 * @param mObj The memory object reference that will be used for freeing the
 * packet containers in the data structure.
 * @return The \c error-code if any error during memory operations else
 * success is returned.
 */
genErr_t
ds_cl_clear (ds_clist clPtr, bs_mmodCls mObj)
{
  return ds_clear (&clPtr->base, mObj);
}
Ejemplo n.º 11
0
/**
 * Clear the data structure.
 * The packet container is freed using the memory object. If the memory object
 * is NULL then the packet container memory is not freed.
 * container needs to be freed elsewhere.
 * @param slPtr
 * @param mObj The memory object reference that will be used for freeing the
 * packet containers in the data structure.
 * @return The \c error-code if any error during memory operations else
 * success is returned.
 */
genErr_t
ds_sl_clear (ds_slist slPtr, bs_mmodCls mObj)
{
  return ds_clear (&slPtr->base, mObj);
}
Ejemplo n.º 12
0
/**
 * Clear the data structure.
 * The packet container is freed using the memory object. If the memory object
 * is NULL then the packet container memory is not freed.
 * container needs to be freed elsewhere.
 * @param cqPtr
 * @param mObj The memory object reference that will be used for freeing the
 * packet containers in the data structure.
 * @return The \c error-code if any error during memory operations else
 * success is returned.
 */
genErr_t
ds_cq_clear (ds_cqueue cqPtr, bs_mmodCls mObj)
{
  return ds_clear (&cqPtr->base, mObj);
}
Ejemplo n.º 13
0
/**
 * Clear the data structure.
 * The packet container is freed using the memory object. If the memory object
 * is NULL then the packet container memory is not freed.
 * container needs to be freed elsewhere.
 * @param quPtr
 * @param mObj The memory object reference that will be used for freeing the
 * packet containers in the data structure.
 * @return The \c error-code if any error during memory operations else
 * success is returned.
 */
genErr_t
ds_qu_clear (ds_queue quPtr, bs_mmodCls mObj)
{
  return ds_clear (&quPtr->base, mObj);
}
Ejemplo n.º 14
0
/**
 * Clear the data structure.
 * The packet container is freed using the memory object. If the memory object
 * is NULL then the packet container memory is not freed.
 * container needs to be freed elsewhere.
 * @param stPtr
 * @param mObj The memory object reference that will be used for freeing the
 * packet containers in the data structure.
 * @return The \c error-code if any error during memory operations else
 * success is returned.
 */
genErr_t
ds_st_clear (ds_stack stPtr, bs_mmodCls mObj)
{
  return ds_clear (&stPtr->base, mObj);
}
Ejemplo n.º 15
0
/**
 * Clear the data structure.
 * The packet container is freed using the memory object. If the memory object
 * is NULL then the packet container memory is not freed.
 * container needs to be freed elsewhere.
 * @param cdPtr
 * @param mObj The memory object reference that will be used for freeing the
 * packet containers in the data structure.
 * @return The \c error-code if any error during memory operations else
 * success is returned.
 */
genErr_t
ds_cd_clear (ds_cdlist cdPtr, bs_mmodCls mObj)
{
  return ds_clear (&cdPtr->base, mObj);
}