onig2posix_error_code(int code)
{
  static const O2PERR o2p[] = {
    { ONIG_MISMATCH,                                      REG_NOMATCH },
    { ONIG_NO_SUPPORT_CONFIG,                             REG_EONIG_INTERNAL },
    { ONIGERR_MEMORY,                                     REG_ESPACE  },
    { ONIGERR_MATCH_STACK_LIMIT_OVER,                     REG_EONIG_INTERNAL },
    { ONIGERR_TYPE_BUG,                                   REG_EONIG_INTERNAL },
    { ONIGERR_PARSER_BUG,                                 REG_EONIG_INTERNAL },
    { ONIGERR_STACK_BUG,                                  REG_EONIG_INTERNAL },
    { ONIGERR_UNDEFINED_BYTECODE,                         REG_EONIG_INTERNAL },
    { ONIGERR_UNEXPECTED_BYTECODE,                        REG_EONIG_INTERNAL },
    { ONIGERR_DEFAULT_ENCODING_IS_NOT_SETTED,             REG_EONIG_BADARG },
    { ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR, REG_EONIG_BADARG },
    { ONIGERR_INVALID_ARGUMENT,                           REG_EONIG_BADARG },
    { ONIGERR_END_PATTERN_AT_LEFT_BRACE,                  REG_EBRACE  },
    { ONIGERR_END_PATTERN_AT_LEFT_BRACKET,                REG_EBRACK  },
    { ONIGERR_EMPTY_CHAR_CLASS,                           REG_ECTYPE  },
    { ONIGERR_PREMATURE_END_OF_CHAR_CLASS,                REG_ECTYPE  },
    { ONIGERR_END_PATTERN_AT_ESCAPE,                      REG_EESCAPE },
    { ONIGERR_END_PATTERN_AT_META,                        REG_EESCAPE },
    { ONIGERR_END_PATTERN_AT_CONTROL,                     REG_EESCAPE },
    { ONIGERR_META_CODE_SYNTAX,                           REG_BADPAT  },
    { ONIGERR_CONTROL_CODE_SYNTAX,                        REG_BADPAT  },
    { ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE,           REG_ECTYPE  },
    { ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE,         REG_ECTYPE  },
    { ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS,    REG_ECTYPE  },
    { ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED,    REG_BADRPT  },
    { ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID,          REG_BADRPT  },
    { ONIGERR_NESTED_REPEAT_OPERATOR,                     REG_BADRPT  },
    { ONIGERR_UNMATCHED_CLOSE_PARENTHESIS,                REG_EPAREN  },
    { ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS,     REG_EPAREN  },
    { ONIGERR_END_PATTERN_IN_GROUP,                       REG_BADPAT  },
    { ONIGERR_UNDEFINED_GROUP_OPTION,                     REG_BADPAT  },
    { ONIGERR_INVALID_POSIX_BRACKET_TYPE,                 REG_BADPAT  },
    { ONIGERR_INVALID_LOOK_BEHIND_PATTERN,                REG_BADPAT  },
    { ONIGERR_INVALID_REPEAT_RANGE_PATTERN,               REG_BADPAT  },
    { ONIGERR_TOO_BIG_NUMBER,                             REG_BADPAT  },
    { ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE,            REG_BADBR   },
    { ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE,   REG_BADBR   },
    { ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS,                  REG_ECTYPE  },
    { ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE,        REG_ECTYPE  },
    { ONIGERR_TOO_MANY_MULTI_BYTE_RANGES,                 REG_ECTYPE  },
    { ONIGERR_TOO_SHORT_MULTI_BYTE_STRING,                REG_BADPAT  },
    { ONIGERR_TOO_BIG_BACKREF_NUMBER,                     REG_ESUBREG },
    { ONIGERR_INVALID_BACKREF,                            REG_ESUBREG },
    { ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED,       REG_BADPAT  },
    { ONIGERR_TOO_BIG_WIDE_CHAR_VALUE,                    REG_EONIG_BADWC },
    { ONIGERR_TOO_LONG_WIDE_CHAR_VALUE,                   REG_EONIG_BADWC },
    { ONIGERR_INVALID_CODE_POINT_VALUE,                   REG_EONIG_BADWC },
    { ONIGERR_EMPTY_GROUP_NAME,                           REG_BADPAT },
    { ONIGERR_INVALID_GROUP_NAME,                         REG_BADPAT },
    { ONIGERR_INVALID_CHAR_IN_GROUP_NAME,                 REG_BADPAT },
    { ONIGERR_UNDEFINED_NAME_REFERENCE,                   REG_BADPAT },
    { ONIGERR_UNDEFINED_GROUP_REFERENCE,                  REG_BADPAT },
    { ONIGERR_MULTIPLEX_DEFINED_NAME,                     REG_BADPAT },
    { ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL,             REG_BADPAT },
    { ONIGERR_NEVER_ENDING_RECURSION,                     REG_BADPAT },
    { ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY,      REG_BADPAT },
    { ONIGERR_INVALID_CHAR_PROPERTY_NAME,                 REG_BADPAT },
    { ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION,         REG_EONIG_BADARG },
    { ONIGERR_OVER_THREAD_PASS_LIMIT_COUNT,               REG_EONIG_THREAD }

  };

  int i;

  if (code >= 0) return 0;

  for (i = 0; i < (int )(sizeof(o2p) / sizeof(o2p[0])); i++) {
    if (code == o2p[i].onig_err)
      return o2p[i].posix_err;
  }

  return REG_EONIG_INTERNAL;  /* but, unknown error code */
}