Blame sample/callout.c

Packit Service bd74e6
/*
Packit Service bd74e6
 * callout.c
Packit Service bd74e6
 */
Packit Service bd74e6
#include <stdlib.h>
Packit Service bd74e6
#include <stdio.h>
Packit Service bd74e6
#include <string.h>
Packit Service bd74e6
#include "oniguruma.h"
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
callout_body(OnigCalloutArgs* args, void* user_data)
Packit Service bd74e6
{
Packit Service bd74e6
  int r;
Packit Service bd74e6
  int i;
Packit Service bd74e6
  int n;
Packit Service bd74e6
  int begin, end;
Packit Service bd74e6
  int used_num;
Packit Service bd74e6
  int used_bytes;
Packit Service bd74e6
  OnigCalloutIn in;
Packit Service bd74e6
  int name_id;
Packit Service bd74e6
  const UChar* contents;
Packit Service bd74e6
  const UChar* start;
Packit Service bd74e6
  const UChar* current;
Packit Service bd74e6
  regex_t* regex;
Packit Service bd74e6
Packit Service bd74e6
  in            = onig_get_callout_in_by_callout_args(args);
Packit Service bd74e6
  name_id       = onig_get_name_id_by_callout_args(args);
Packit Service bd74e6
  start         = onig_get_start_by_callout_args(args);
Packit Service bd74e6
  current       = onig_get_current_by_callout_args(args);
Packit Service bd74e6
  regex         = onig_get_regex_by_callout_args(args);
Packit Service bd74e6
Packit Service bd74e6
  contents = onig_get_contents_by_callout_args(args);
Packit Service bd74e6
Packit Service bd74e6
  if (name_id != ONIG_NON_NAME_ID) {
Packit Service bd74e6
    UChar* name = onig_get_callout_name_by_name_id(name_id);
Packit Service bd74e6
    fprintf(stdout, "name: %s\n", name);
Packit Service bd74e6
  }
Packit Service bd74e6
  fprintf(stdout,
Packit Service bd74e6
          "%s %s: contents: \"%s\", start: \"%s\", current: \"%s\"\n",
Packit Service bd74e6
          contents != 0 ? "CONTENTS" : "NAME",
Packit Service bd74e6
          in == ONIG_CALLOUT_IN_PROGRESS ? "PROGRESS" : "RETRACTION",
Packit Service bd74e6
          contents, start, current);
Packit Service bd74e6
Packit Service bd74e6
  fprintf(stdout, "user_data: %s\n", (char* )user_data);
Packit Service bd74e6
Packit Service bd74e6
  (void )onig_get_used_stack_size_in_callout(args, &used_num, &used_bytes);
Packit Service bd74e6
  fprintf(stdout, "stack: used_num: %d, used_bytes: %d\n", used_num, used_bytes);
Packit Service bd74e6
Packit Service bd74e6
  n = onig_number_of_captures(regex);
Packit Service bd74e6
  for (i = 1; i <= n; i++) {
Packit Service bd74e6
    r = onig_get_capture_range_in_callout(args, i, &begin, &end;;
Packit Service bd74e6
    if (r != ONIG_NORMAL) return r;
Packit Service bd74e6
Packit Service bd74e6
    fprintf(stdout, "capture %d: (%d-%d)\n", i, begin, end);
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  fflush(stdout);
Packit Service bd74e6
  return ONIG_CALLOUT_SUCCESS;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
progress_callout_func(OnigCalloutArgs* args, void* user_data)
Packit Service bd74e6
{
Packit Service bd74e6
  return callout_body(args, user_data);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
retraction_callout_func(OnigCalloutArgs* args, void* user_data)
Packit Service bd74e6
{
Packit Service bd74e6
  return callout_body(args, user_data);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
foo(OnigCalloutArgs* args, void* user_data)
Packit Service bd74e6
{
Packit Service bd74e6
  return callout_body(args, user_data);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
bar(OnigCalloutArgs* args, void* user_data)
Packit Service bd74e6
{
Packit Service bd74e6
  int r;
Packit Service bd74e6
  int i;
Packit Service bd74e6
  int n;
Packit Service bd74e6
  OnigType type;
Packit Service bd74e6
  OnigValue val;
Packit Service bd74e6
Packit Service bd74e6
  fprintf(stdout, "bar called.\n");
Packit Service bd74e6
Packit Service bd74e6
  n = onig_get_args_num_by_callout_args(args);
Packit Service bd74e6
  if (n < 0) {
Packit Service bd74e6
    fprintf(stderr, "FAIL: onig_get_args_num_by_callout_args(): %d\n", n);
Packit Service bd74e6
    return n;
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  for (i = 0; i < n; i++) {
Packit Service bd74e6
    r = onig_get_arg_by_callout_args(args, i, &type, &val;;
Packit Service bd74e6
    if (r != 0) {
Packit Service bd74e6
      fprintf(stderr, "FAIL: onig_get_arg_by_callout_args(): %d\n", r);
Packit Service bd74e6
      return r;
Packit Service bd74e6
    }
Packit Service bd74e6
Packit Service bd74e6
    fprintf(stdout, "arg[%d]: ", i);
Packit Service bd74e6
    switch (type) {
Packit Service bd74e6
    case ONIG_TYPE_LONG:
Packit Service bd74e6
      fprintf(stdout, "%ld\n", val.l);
Packit Service bd74e6
      break;
Packit Service bd74e6
    case ONIG_TYPE_CHAR:
Packit Service bd74e6
      fprintf(stdout, "0x%06x\n", val.c);
Packit Service bd74e6
      break;
Packit Service bd74e6
    case ONIG_TYPE_STRING:
Packit Service bd74e6
      fprintf(stdout, "'%s'\n", val.s.start);
Packit Service bd74e6
      break;
Packit Service bd74e6
    default:
Packit Service bd74e6
      /* Never come here. But escape warning. */
Packit Service bd74e6
      break;
Packit Service bd74e6
    };
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  return ONIG_CALLOUT_SUCCESS;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
test(OnigEncoding enc, OnigMatchParam* mp, char* in_pattern, char* in_str)
Packit Service bd74e6
{
Packit Service bd74e6
  int r;
Packit Service bd74e6
  unsigned char *start, *range, *end;
Packit Service bd74e6
  regex_t* reg;
Packit Service bd74e6
  OnigErrorInfo einfo;
Packit Service bd74e6
  OnigRegion *region;
Packit Service bd74e6
  UChar* pattern;
Packit Service bd74e6
  UChar* str;
Packit Service bd74e6
Packit Service bd74e6
  pattern = (UChar* )in_pattern;
Packit Service bd74e6
  str = (UChar* )in_str;
Packit Service bd74e6
Packit Service bd74e6
  r = onig_new(&reg, pattern, pattern + strlen((char* )pattern),
Packit Service bd74e6
               ONIG_OPTION_DEFAULT, enc, ONIG_SYNTAX_DEFAULT, &einfo);
Packit Service bd74e6
  if (r != ONIG_NORMAL) {
Packit Service bd74e6
    char s[ONIG_MAX_ERROR_MESSAGE_LEN];
Packit Service bd74e6
    onig_error_code_to_str((UChar* )s, r, &einfo);
Packit Service bd74e6
    fprintf(stderr, "COMPILE ERROR: %d: %s\n", r, s);
Packit Service bd74e6
    return -1;
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  region = onig_region_new();
Packit Service bd74e6
Packit Service bd74e6
  end   = str + strlen((char* )str);
Packit Service bd74e6
  start = str;
Packit Service bd74e6
  range = end;
Packit Service bd74e6
  r = onig_search_with_param(reg, str, end, start, range, region,
Packit Service bd74e6
                             ONIG_OPTION_NONE, mp);
Packit Service bd74e6
  if (r >= 0) {
Packit Service bd74e6
    int i;
Packit Service bd74e6
Packit Service bd74e6
    fprintf(stderr, "match at %d\n", r);
Packit Service bd74e6
    for (i = 0; i < region->num_regs; i++) {
Packit Service bd74e6
      fprintf(stderr, "%d: (%d-%d)\n", i, region->beg[i], region->end[i]);
Packit Service bd74e6
    }
Packit Service bd74e6
  }
Packit Service bd74e6
  else if (r == ONIG_MISMATCH) {
Packit Service bd74e6
    fprintf(stderr, "search fail\n");
Packit Service bd74e6
  }
Packit Service bd74e6
  else { /* error */
Packit Service bd74e6
    char s[ONIG_MAX_ERROR_MESSAGE_LEN];
Packit Service bd74e6
    onig_error_code_to_str((UChar* )s, r);
Packit Service bd74e6
    fprintf(stderr, "SEARCH ERROR: %d: %s\n", r, s);
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
Packit Service bd74e6
  onig_free(reg);
Packit Service bd74e6
  return r;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
extern int main(int argc, char* argv[])
Packit Service bd74e6
{
Packit Service bd74e6
  int r;
Packit Service bd74e6
  int id;
Packit Service bd74e6
  void* user_data;
Packit Service bd74e6
  UChar* name;
Packit Service bd74e6
  OnigEncoding use_encs[1];
Packit Service bd74e6
  unsigned int arg_types[4];
Packit Service bd74e6
  OnigValue opt_defaults[4];
Packit Service bd74e6
  OnigEncoding enc;
Packit Service bd74e6
  OnigMatchParam* mp;
Packit Service bd74e6
Packit Service bd74e6
  enc = ONIG_ENCODING_UTF8;
Packit Service bd74e6
  use_encs[0] = enc;
Packit Service bd74e6
Packit Service bd74e6
  r = onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0]));
Packit Service bd74e6
  if (r != ONIG_NORMAL) return -1;
Packit Service bd74e6
Packit Service bd74e6
  /* monitor on */
Packit Service bd74e6
  r = onig_setup_builtin_monitors_by_ascii_encoded_name(stdout);
Packit Service bd74e6
  if (r != ONIG_NORMAL) return -1;
Packit Service bd74e6
Packit Service bd74e6
  name = (UChar* )"foo";
Packit Service bd74e6
  id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,
Packit Service bd74e6
                                name, name + strlen((char* )name),
Packit Service bd74e6
                                ONIG_CALLOUT_IN_BOTH, foo, 0, 0, 0, 0, 0);
Packit Service bd74e6
  if (id < 0) {
Packit Service bd74e6
    fprintf(stderr, "ERROR: fail to set callout of name: %s\n", name);
Packit Service bd74e6
    //return -1;
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  name = (UChar* )"bar";
Packit Service bd74e6
  arg_types[0] = ONIG_TYPE_LONG;
Packit Service bd74e6
  arg_types[1] = ONIG_TYPE_STRING;
Packit Service bd74e6
  arg_types[2] = ONIG_TYPE_CHAR;
Packit Service bd74e6
  opt_defaults[0].s.start = (UChar* )"I am a option argument's default value.";
Packit Service bd74e6
  opt_defaults[0].s.end   = opt_defaults[0].s.start +
Packit Service bd74e6
                                strlen((char* )opt_defaults[0].s.start);
Packit Service bd74e6
  opt_defaults[1].c = 0x4422;
Packit Service bd74e6
Packit Service bd74e6
  id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,
Packit Service bd74e6
                                name, name + strlen((char* )name),
Packit Service bd74e6
                                ONIG_CALLOUT_IN_PROGRESS, bar, 0,
Packit Service bd74e6
                                3, arg_types, 2, opt_defaults);
Packit Service bd74e6
  if (id < 0) {
Packit Service bd74e6
    fprintf(stderr, "ERROR: fail to set callout of name: %s\n", name);
Packit Service bd74e6
    //return -1;
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  (void)onig_set_progress_callout(progress_callout_func);
Packit Service bd74e6
  (void)onig_set_retraction_callout(retraction_callout_func);
Packit Service bd74e6
Packit Service bd74e6
  mp = onig_new_match_param();
Packit Service bd74e6
Packit Service bd74e6
  user_data = (void* )"something data";
Packit Service bd74e6
  r = onig_set_callout_user_data_of_match_param(mp, user_data);
Packit Service bd74e6
  if (r != ONIG_NORMAL) {
Packit Service bd74e6
    fprintf(stderr, "ERROR: fail onig_set_callout_user_data_of_match_param(): %d\n", r);
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  /* callout of contents */
Packit Service bd74e6
  test(enc, mp, "a+(?{foo bar baz...}X)$", "aaab");
Packit Service bd74e6
  test(enc, mp, "(?{{!{}#$%&'()=-~^|[_]`@*:+;<>?/.\\,}}[symbols])c", "abc");
Packit Service bd74e6
  test(enc, mp, "\\A(...)(?{{{booooooooooooo{{ooo}}ooooooooooz}}}<)", "aaab");
Packit Service bd74e6
  test(enc, mp, "\\A(?!a(?{in prec-read-not}[xxx]X)b)", "ac");
Packit Service bd74e6
  test(enc, mp, "(?
Packit Service bd74e6
Packit Service bd74e6
  // callout of name
Packit Service bd74e6
  test(enc, mp, "\\A(*foo)abc", "abc");
Packit Service bd74e6
  test(enc, mp, "abc(?:(*FAIL)|$)", "abcabc");
Packit Service bd74e6
  test(enc, mp, "abc(?:$|(*MISMATCH)|abc$)", "abcabc");
Packit Service bd74e6
  test(enc, mp, "abc(?:(*ERROR)|$)", "abcabc");
Packit Service bd74e6
  test(enc, mp, "ab(*foo{})(*FAIL)", "abc");
Packit Service bd74e6
  test(enc, mp, "abc(d|(*ERROR{-999}))", "abc");
Packit Service bd74e6
  test(enc, mp, "ab(*bar{372,I am a bar's argument,あ})c(*FAIL)", "abc");
Packit Service bd74e6
  test(enc, mp, "ab(*bar{1234567890})", "abc");
Packit Service bd74e6
  test(enc, mp, "(?:a(*MAX{2})|b)*", "abbabbabbabb");
Packit Service bd74e6
  test(enc, mp, "(?:(*MAX{2})a|b)*", "abbabbabbabb");
Packit Service bd74e6
  test(enc, mp, "(?:(*MAX{1})a|b)*", "bbbbbabbbbbabbbbb");
Packit Service bd74e6
  test(enc, mp, "(?:(*MAX{3})a|(*MAX{4})b)*", "bbbaabbab");
Packit Service bd74e6
  test(enc, mp, "(?:(*MAX[A]{3})a|(*MAX[B]{5})b)*(*CMP{A,<,B})", "abababc");
Packit Service bd74e6
  test(enc, mp, "(?:(*MAX[A]{7})a|(*MAX[B]{5})b)*(*CMP{A,>=,4})", "abababcabababaa");
Packit Service bd74e6
  test(enc, mp, "(?:(*MAX[T]{3})a)*(?:(*MAX{T})c)*", "aaccc");
Packit Service bd74e6
Packit Service bd74e6
  /* callouts in condition */
Packit Service bd74e6
  test(enc, mp, "\\A(?(?{in condition})then|else)\\z", "then");
Packit Service bd74e6
  test(enc, mp, "\\A(?(*FAIL)then|else)\\z", "else");
Packit Service bd74e6
Packit Service bd74e6
  /* monitor test */
Packit Service bd74e6
  test(enc, mp, "(?:(*MON{X})(*FAIL)|.{,3}(*MON[FOO])k)", "abcdefghijk");
Packit Service bd74e6
Packit Service bd74e6
  onig_free_match_param(mp);
Packit Service bd74e6
  onig_end();
Packit Service bd74e6
  return 0;
Packit Service bd74e6
}