pike.git / src / module_support.c

version» Context lines:

pike.git/src/module_support.c:9:   #include "interpret.h"   #include "svalue.h"   #include "stralloc.h"   #include "pike_types.h"   #include "pike_error.h"   #include "mapping.h"   #include "object.h"   #include "operators.h"   #include "bignum.h"    - #define sp Pike_sp -  +    /* Checks that args_to_check arguments are OK.    * Returns 1 if everything worked ok, zero otherwise.    * If something went wrong, 'exepect_result' tells you what went wrong.    * Make sure to finish the argument list with a zero.    */   static int va_check_args(struct svalue *s,    int args_to_check,    struct expect_result *res,    va_list arglist)   {
pike.git/src/module_support.c:40:    {    res->error_type = ERR_TOO_MANY;    return 0;    }    }       if (!((1UL << TYPEOF(s[res->argno])) & res->expected) &&    !(res->expected & BIT_ZERO &&    TYPEOF(s[res->argno]) == T_INT && s[res->argno].u.integer == 0))    { -  res->got = DO_NOT_WARN((unsigned char)TYPEOF(s[res->argno])); +  res->got = (unsigned char)TYPEOF(s[res->argno]);    res->error_type = ERR_BAD_ARG;    return 0;    }    }       if(!(res->expected & BIT_MANY))    res->expected = va_arg(arglist, unsigned int);       if(!res->expected ||    (res->expected & BIT_VOID)) return 1;
pike.git/src/module_support.c:65:   /* Returns the number of the first bad argument,    * -X if there were too few arguments    * or 0 if all arguments were OK.    */   PMOD_EXPORT int check_args(int args, ...)   {    va_list arglist;    struct expect_result tmp;       va_start(arglist, args); -  va_check_args(sp - args, args, &tmp, arglist); +  va_check_args(Pike_sp - args, args, &tmp, arglist);    va_end(arglist);       if(tmp.error_type == ERR_NONE) return 0;    return tmp.argno+1;   }    -  + static const char* get_fname(const char *fname) + { +  if (fname) return fname; +  return (Pike_fp->context->prog->identifiers + Pike_fp->fun)->name->str; + } +    /* This function generates errors if any of the args first arguments    * is not OK.    */   PMOD_EXPORT void check_all_args(const char *fnname, int args, ... )   {    va_list arglist;    struct expect_result tmp;       va_start(arglist, args); -  va_check_args(sp - args, args, &tmp, arglist); +  va_check_args(Pike_sp - args, args, &tmp, arglist);    va_end(arglist);       switch(tmp.error_type)    {    case ERR_NONE: return;    case ERR_TOO_FEW: -  new_error(fnname, "Too few arguments.\n", sp, args, NULL, 0); +  new_error(get_fname(fnname), "Too few arguments.\n", Pike_sp, args); +  break;    case ERR_TOO_MANY: -  new_error(fnname, "Too many arguments.\n", sp, args, NULL, 0); +  new_error(get_fname(fnname), "Too many arguments.\n", Pike_sp, args); +  break;       case ERR_BAD_ARG:    {    char buf[1000];    int e;    buf[0]=0;    for(e=0;e<16;e++)    {    if(tmp.expected & (1<<e))    {
pike.git/src/module_support.c:114:    strcat(buf,", ");    else    strcat(buf," or ");    }    strcat(buf, get_name_of_type(e));    }    }       Pike_error("Bad argument %d to %s(), (expecting %s, got %s)\n",    tmp.argno+1, -  fnname, +  get_fname(fnname),    buf,    get_name_of_type(tmp.got)); -  +  break;    }    }   }      /* get_args and get_all_args type specifiers:    *    * %i: INT_TYPE    * %I: int or float -> INT_TYPE    * %d: int (the c type "int" which may vary from INT_TYPE)    * %D: int of float -> int    * %+: positive int -> INT_TYPE -  * %l: int or bignum -> LONGEST +  * %l: int or bignum -> INT64    * %c: char * Only narrow (8 bit) strings without NUL.    * %C: char * or NULL Only narrow (8 bit) strings without NUL.    * %n: struct pike_string * Only narrow (8 bit) strings.    * %N: struct pike_string * or NULL Only narrow (8 bit) strings.    * %t: struct pike_string * Any string width. (*)    * %T: struct pike_string * or NULL Any string width. (*)    * %a: struct array *    * %A: struct array * or NULL    * %f: float -> FLOAT_TYPE    * %F: float or int -> FLOAT_TYPE
pike.git/src/module_support.c:265:    /* FIXME: Error reporting for bignum objects. */    *cast_arg(ptr, INT_TYPE *)=s->u.integer;    break;       case 'D':    if(TYPEOF(*s) == T_INT)    /* FIXME: Range checks. */    *cast_arg(ptr, int *)=s->u.integer;    else if(TYPEOF(*s) == T_FLOAT)    /* FIXME: Range checks. */ -  *cast_arg(ptr, int *)= -  DO_NOT_WARN((int)s->u.float_number); +  *cast_arg(ptr, int *)=(int)s->u.float_number;    else    {    ref_push_type_value(int_type_string);    push_svalue( s );    f_cast( ); -  if(TYPEOF(sp[-1]) == T_INT) +  if(TYPEOF(Pike_sp[-1]) == T_INT)    /* FIXME: Range checks. */ -  *cast_arg(ptr, int *)=sp[-1].u.integer; +  *cast_arg(ptr, int *)=Pike_sp[-1].u.integer;    else if(TYPEOF(*s) == T_FLOAT)    /* FIXME: Range checks. Btw, does this case occur? */ -  *cast_arg(ptr, int *)= -  DO_NOT_WARN((int)sp[-1].u.float_number); +  *cast_arg(ptr, int *)=(int)Pike_sp[-1].u.float_number;    else    Pike_error("Cast to int failed.\n");    pop_stack();    }    break;       case 'I':    if(TYPEOF(*s) == T_INT)    *cast_arg(ptr, INT_TYPE *)=s->u.integer;    else if(TYPEOF(*s) == T_FLOAT)    /* FIXME: Range checks. */ -  *cast_arg(ptr, INT_TYPE *) = DO_NOT_WARN((INT_TYPE)s->u.float_number); +  *cast_arg(ptr, INT_TYPE *) = (INT_TYPE)s->u.float_number;    else    {    /* FIXME: Error reporting for bignum objects. */    ref_push_type_value(int_type_string);    push_svalue( s );    f_cast( ); -  if(TYPEOF(sp[-1]) == T_INT) -  *cast_arg(ptr, INT_TYPE *)=sp[-1].u.integer; +  if(TYPEOF(Pike_sp[-1]) == T_INT) +  *cast_arg(ptr, INT_TYPE *)=Pike_sp[-1].u.integer;    else if(TYPEOF(*s) == T_FLOAT)    /* FIXME: Range checks. Btw, does this case occur? */ -  *cast_arg(ptr, INT_TYPE *)= -  DO_NOT_WARN((INT_TYPE)sp[-1].u.float_number); +  *cast_arg(ptr, INT_TYPE *)=(INT_TYPE)Pike_sp[-1].u.float_number;    else    Pike_error("Cast to int failed.\n");    pop_stack();    }    break;       case 'l':    if (TYPEOF(*s) == T_INT) { -  *cast_arg(ptr, LONGEST *)=s->u.integer; +  *cast_arg(ptr, INT64 *)=s->u.integer;    break;    } else if (is_bignum_object_in_svalue(s) && -  int64_from_bignum(cast_arg(ptr, LONGEST *), s->u.object) == 1) { +  int64_from_bignum(cast_arg(ptr, INT64 *), s->u.object) == 1) {    break;    }    /* FIXME: Error reporting for bignum objects. */    goto type_err;       case 'C':    if(TYPEOF(*s) != T_STRING && UNSAFE_IS_ZERO (s)) {    *cast_arg(ptr, char **) = NULL;    break;    } -  /* FALL THROUGH */ +  /* FALLTHRU */    case 'c':    case 's':    if(TYPEOF(*s) != T_STRING) goto type_err;    if(s->u.string->size_shift) goto type_err;       if(string_has_null(s->u.string)) {    *info = ARGS_NUL_IN_STRING;    return ret;    }       *cast_arg(ptr, char **)=s->u.string->str;    break;       case 'N':    if(TYPEOF(*s) != T_STRING && UNSAFE_IS_ZERO (s)) {    *cast_arg(ptr, struct pike_string **) = NULL;    break;    } -  /* FALL THROUGH */ +  /* FALLTHRU */    case 'n':    case 'S':    if(TYPEOF(*s) != T_STRING) goto type_err;    if(s->u.string->size_shift) goto type_err;    *cast_arg(ptr, struct pike_string **)=s->u.string;    break;       case 'T':    if(TYPEOF(*s) != T_STRING && UNSAFE_IS_ZERO (s)) {    *cast_arg(ptr, struct pike_string **) = NULL;    break;    } -  /* FALL THROUGH */ +  /* FALLTHRU */    case 't':    case 'W':    if(TYPEOF(*s) != T_STRING) goto type_err;    *cast_arg(ptr, struct pike_string **)=s->u.string;    break;       case 'A':    if(TYPEOF(*s) != T_ARRAY && UNSAFE_IS_ZERO (s)) {    *cast_arg(ptr, struct array **) = NULL;    break;    } -  /* FALL THROUGH */ +  /* FALLTHRU */    case 'a':    if(TYPEOF(*s) != T_ARRAY) goto type_err;    *cast_arg(ptr, struct array **)=s->u.array;    break;       case 'f':    if(TYPEOF(*s) != T_FLOAT) goto type_err;    *cast_arg(ptr, FLOAT_TYPE *)=s->u.float_number;    break;    case 'F':    if(TYPEOF(*s) == T_FLOAT)    *cast_arg(ptr, FLOAT_TYPE *)=s->u.float_number;    else if(TYPEOF(*s) == T_INT)    *cast_arg(ptr, FLOAT_TYPE *)=(FLOAT_TYPE)s->u.integer;    else    {    ref_push_type_value(float_type_string);    push_svalue( s );    f_cast( ); -  *cast_arg(ptr, FLOAT_TYPE *)=sp[-1].u.float_number; +  *cast_arg(ptr, FLOAT_TYPE *)=Pike_sp[-1].u.float_number;    pop_stack();    }    break;       case 'G':    if(TYPEOF(*s) != T_MAPPING && UNSAFE_IS_ZERO (s)) {    *cast_arg(ptr, struct mapping **) = NULL;    break;    } -  /* FALL THROUGH */ +  /* FALLTHRU */    case 'm':    if(TYPEOF(*s) != T_MAPPING) goto type_err;    *cast_arg(ptr, struct mapping **)=s->u.mapping;    break;       case 'U':    if(TYPEOF(*s) != T_MULTISET && UNSAFE_IS_ZERO (s)) {    *cast_arg(ptr, struct multiset **) = NULL;    break;    } -  /* FALL THROUGH */ +  /* FALLTHRU */    case 'u':    case 'M':    if(TYPEOF(*s) != T_MULTISET) goto type_err;    *cast_arg(ptr, struct multiset **)=s->u.multiset;    break;       case 'O':    if(TYPEOF(*s) != T_OBJECT && UNSAFE_IS_ZERO (s)) {    *cast_arg(ptr, struct object **) = NULL;    break;    } -  /* FALL THROUGH */ +  /* FALLTHRU */    case 'o':    if(TYPEOF(*s) != T_OBJECT) goto type_err;    if (SUBTYPEOF(*s)) {    *info = ARGS_SUBTYPED_OBJECT;    return ret;    }    *cast_arg(ptr, struct object **)=s->u.object;    break;       case 'P':    case 'p':    switch(TYPEOF(*s))    {    case T_PROGRAM:    *cast_arg(ptr, struct program **)=s->u.program;    break;       case T_FUNCTION:    if((*cast_arg(ptr, struct program **)=program_from_svalue(s)))    break; -  +  /* FALLTHRU */       default:    if (*fmt == 'P' && UNSAFE_IS_ZERO(s))    *cast_arg(ptr, struct program **) = NULL;    goto type_err;    }    break;       case '*':    *cast_arg(ptr, struct svalue **)=s;
pike.git/src/module_support.c:487:    const char *fmt,    va_list ap)   {    int info;    return va_get_args_2 (s, num_args, fmt, ap, &info);   }      #ifdef NOT_USED   /* get_args does NOT generate errors, it simply returns how    * many arguments were actually matched. -  * usage: get_args(sp-args, args, "%i",&an_int) +  * usage: get_args(Pike_sp-args, args, "%i",&an_int)    */   PMOD_EXPORT int get_args(struct svalue *s,    INT32 num_args,    const char *fmt, ...)   {    va_list ptr;    int ret, info;    va_start(ptr, fmt);    ret=va_get_args_2(s, num_args, fmt, ptr, &info);    va_end(ptr);    return ret;   }   #endif      PMOD_EXPORT void get_all_args(const char *fname, INT32 args,    const char *format, ... )   {    va_list ptr;    int ret, info;    va_start(ptr, format); -  ret=va_get_args_2(sp-args, args, format, ptr, &info); +  ret=va_get_args_2(Pike_sp-args, args, format, ptr, &info);    va_end(ptr);       switch (info) {    case ARGS_OK:    case ARGS_OPT: -  break; -  +     case ARGS_LONG: - #if 0 -  /* Is this a good idea? */ -  if (!TEST_COMPAT (7, 4)) -  wrong_number_of_args_error (fname, args, ret); - #endif +     break; -  +     case ARGS_NUL_IN_STRING: -  bad_arg_error( -  fname, sp-args, args, -  ret+1, -  "string (8bit)", -  sp+ret-args, -  "Bad argument %d to %s(). NUL char in string not allowed.\n", +  fname = get_fname(fname); +  bad_arg_error(fname, args, ret+1, "string(1..255)", +  Pike_sp+ret-args, +  "Bad argument %d to %s(). NUL in string.\n",    ret+1, fname); -  /* NOT REACHED */ +  UNREACHABLE();       case ARGS_SUBTYPED_OBJECT: -  bad_arg_error(fname, sp-args, args, ret+1, "object", -  sp+ret-args, +  bad_arg_error(get_fname(fname), args, ret+1, "object", +  Pike_sp+ret-args,    "Subtyped objects are not supported.\n"); -  /* NOT_REACHED */ +  UNREACHABLE();       case ARGS_SHORT:    default: {    char *expected_type;       /* NB: For ARGS_SHORT we know there's no period in format that    * might offset the format specs. */    switch(info == ARGS_SHORT ? format[ret*2+1] : info) {    case 'd': case 'i': case 'l': expected_type = "int"; break;    case 'D': case 'I': expected_type = "int|float"; break;    case '+': expected_type = "int(0..)"; break; -  case 'c': case 's': case 'n': case 'S': -  expected_type = "string (8bit)"; break; -  case 'C': case 'N': expected_type = "string (8bit) or zero"; break; +  case 'c': case 's': +  expected_type = "string(1..255)"; break; +  case 'n': case 'S': +  expected_type = "string(8bit)"; break; +  case 'C': +  expected_type = "string(1..255) or zero"; break; +  case 'N': +  expected_type = "string(8bit) or zero"; break;    case 't': case 'W': expected_type = "string"; break;    case 'T': expected_type = "string or zero"; break;    case 'a': expected_type = "array"; break;    case 'A': expected_type = "array or zero"; break;    case 'f': expected_type = "float"; break;    case 'F': expected_type = "float|int"; break;    case 'm': expected_type = "mapping"; break;    case 'G': expected_type = "mapping or zero"; break;    case 'M': case 'u': expected_type = "multiset"; break;    case 'U': expected_type = "multiset or zero"; break;
pike.git/src/module_support.c:576:    case 'P': expected_type = "program or zero"; break;    case '*': expected_type = "mixed"; break;    default:   #ifdef PIKE_DEBUG    Pike_fatal ("get_all_args not in sync with low_va_get_args.\n");   #else    expected_type = NULL; /* To avoid warnings. */   #endif    }    +  fname = get_fname(fname);    if (info != ARGS_SHORT) { -  bad_arg_error( -  fname, sp-args, args, -  ret+1, -  expected_type, -  sp+ret-args, +  bad_arg_error(fname, args, ret+1, expected_type, +  Pike_sp+ret-args,    "Bad argument %d to %s(). Expected %s.\n",    ret+1, fname, expected_type);    } else {    const char *req_args_end = strchr (format, '.');    if (!req_args_end) req_args_end = strchr (format, 0); -  bad_arg_error( -  fname, sp-args, args, -  ret+1, -  expected_type, +  bad_arg_error(fname, args, ret+1, expected_type,    0,    "Too few arguments to %s(). Expected %"PRINTPTRDIFFT"d arguments, got %d.\n"    "The type of the next argument is expected to be %s.\n",    fname, (req_args_end - format) / 2, args, expected_type);    } -  /* NOT REACHED */ +  UNREACHABLE();    }    }   }      /* NOTA BENE:    * The code below assumes that dynamic modules are not    * unloaded from memory...    */      static struct mapping *exported_symbols;