void CRB_set_command_line_args(CRB_Interpreter *interpreter, int argc, char **argv) { int i; CRB_Char *wc_str; CRB_Value args; CRB_Value elem; args.type = CRB_ARRAY_VALUE; args.u.object = crb_create_array_i(interpreter, argc); CRB_push_value(interpreter, &args); for (i = 0; i < argc; i++) { wc_str = CRB_mbstowcs_alloc(interpreter, NULL, 0, argv[i]); if (wc_str == NULL) { fprintf(stderr, "bad command line argument(%dth)", i); return; } elem.type = CRB_STRING_VALUE; elem.u.object = crb_create_crowbar_string_i(interpreter, wc_str); CRB_array_set(interpreter, NULL, args.u.object, i, &elem); } CRB_add_global_variable(interpreter, "ARGS", &args, CRB_TRUE); CRB_pop_value(interpreter); }
static CRB_Value nv_fgets_proc(CRB_Interpreter *interpreter, CRB_LocalEnvironment *env, int arg_count, CRB_Value *args) { CRB_Value value; FILE *fp; char buf[LINE_BUF_SIZE]; char *mb_buf = NULL; int ret_len = 0; CRB_Char *wc_str; CRB_check_argument_count(interpreter, env, arg_count, 1); if (args[0].type != CRB_NATIVE_POINTER_VALUE || (!CRB_check_native_pointer_type(args[0].u.object, &st_file_type_info))) { CRB_error(interpreter, env, &st_lib_info, __LINE__, (int)FGETS_ARGUMENT_TYPE_ERR, CRB_MESSAGE_ARGUMENT_END); } check_file_pointer(interpreter,env, args[0].u.object); fp = CRB_object_get_native_pointer(args[0].u.object); while (fgets(buf, LINE_BUF_SIZE, fp)) { int new_len; new_len = ret_len + strlen(buf); mb_buf = MEM_realloc(mb_buf, new_len + 1); if (ret_len == 0) { strcpy(mb_buf, buf); } else { strcat(mb_buf, buf); } ret_len = new_len; if (mb_buf[ret_len-1] == '\n') break; } if (ret_len > 0) { wc_str = CRB_mbstowcs_alloc(interpreter, env, __LINE__, mb_buf); if (wc_str == NULL) { MEM_free(mb_buf); CRB_error(interpreter, env, &st_lib_info, __LINE__, (int)FGETS_BAD_MULTIBYTE_CHARACTER_ERR, CRB_MESSAGE_ARGUMENT_END); } value.type = CRB_STRING_VALUE; value.u.object = CRB_create_crowbar_string(interpreter, env, wc_str); } else { value.type = CRB_NULL_VALUE; } MEM_free(mb_buf); return value; }
CRB_Char * CRB_value_to_string(CRB_Interpreter *inter, CRB_LocalEnvironment *env, int line_number, CRB_Value *value) { VString vstr; char buf[LINE_BUF_SIZE]; CRB_Char wc_buf[LINE_BUF_SIZE]; int i; crb_vstr_clear(&vstr); switch (value->type) { case CRB_BOOLEAN_VALUE: if (value->u.boolean_value) { CRB_mbstowcs("true", wc_buf); } else { CRB_mbstowcs("false", wc_buf); } crb_vstr_append_string(&vstr, wc_buf); break; case CRB_INT_VALUE: sprintf(buf, "%d", value->u.int_value); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_DOUBLE_VALUE: sprintf(buf, "%f", value->u.double_value); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_STRING_VALUE: crb_vstr_append_string(&vstr, value->u.object->u.string.string); break; case CRB_NATIVE_POINTER_VALUE: sprintf(buf, "%s(%p)", value->u.object->u.native_pointer.info->name, value->u.object->u.native_pointer.pointer); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_NULL_VALUE: CRB_mbstowcs("null", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_ARRAY_VALUE: CRB_mbstowcs("(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); for (i = 0; i < value->u.object->u.array.size; i++) { CRB_Char *new_str; if (i > 0) { CRB_mbstowcs(", ", wc_buf); crb_vstr_append_string(&vstr, wc_buf); } new_str = CRB_value_to_string(inter, env, line_number, &value->u.object->u.array.array[i]); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_ASSOC_VALUE: CRB_mbstowcs("(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); for (i = 0; i < value->u.object->u.assoc.member_count; i++) { CRB_Char *new_str; if (i > 0) { CRB_mbstowcs(", ", wc_buf); crb_vstr_append_string(&vstr, wc_buf); } new_str = CRB_mbstowcs_alloc(inter, env, line_number, value->u.object->u.assoc.member[i].name); DBG_assert(new_str != NULL, ("new_str is null.\n")); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); CRB_mbstowcs("=>", wc_buf); crb_vstr_append_string(&vstr, wc_buf); new_str = CRB_value_to_string(inter, env, line_number, &value->u.object ->u.assoc.member[i].value); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_CLOSURE_VALUE: CRB_mbstowcs("closure(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); if (value->u.closure.function->name == NULL) { CRB_mbstowcs("null", wc_buf); crb_vstr_append_string(&vstr, wc_buf); } else { CRB_Char *new_str; new_str = CRB_mbstowcs_alloc(inter, env, line_number, value->u.closure.function->name); DBG_assert(new_str != NULL, ("new_str is null.\n")); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_FAKE_METHOD_VALUE: CRB_mbstowcs("fake_method(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); { CRB_Char *new_str; new_str = CRB_mbstowcs_alloc(inter, env, line_number, value->u.fake_method.method_name); DBG_assert(new_str != NULL, ("new_str is null.\n")); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_SCOPE_CHAIN_VALUE: /* FALLTHRU*/ default: DBG_panic(("value->type..%d\n", value->type)); } return vstr.string; }
static void format_message(int line_number, MessageFormat *format, VString *v, va_list ap) { int i; char buf[LINE_BUF_SIZE]; CRB_Char wc_buf[LINE_BUF_SIZE]; int arg_name_index; char arg_name[LINE_BUF_SIZE]; MessageArgument arg[MESSAGE_ARGUMENT_MAX]; MessageArgument cur_arg; CRB_Char *wc_format; create_message_argument(arg, ap); wc_format = CRB_mbstowcs_alloc(NULL, NULL, line_number, format->format); DBG_assert(wc_format != NULL, ("wc_format is null.\n")); for (i = 0; wc_format[i] != L'\0'; i++) { if (wc_format[i] != L'$') { crb_vstr_append_character(v, wc_format[i]); continue; } assert(wc_format[i+1] == L'('); i += 2; for (arg_name_index = 0; wc_format[i] != L')'; arg_name_index++, i++) { arg_name[arg_name_index] = CRB_wctochar(wc_format[i]); } arg_name[arg_name_index] = '\0'; assert(wc_format[i] == L')'); search_argument(arg, arg_name, &cur_arg); switch (cur_arg.type) { case INT_MESSAGE_ARGUMENT: sprintf(buf, "%d", cur_arg.u.int_val); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(v, wc_buf); break; case DOUBLE_MESSAGE_ARGUMENT: sprintf(buf, "%f", cur_arg.u.double_val); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(v, wc_buf); break; case STRING_MESSAGE_ARGUMENT: CRB_mbstowcs(cur_arg.u.string_val, wc_buf); crb_vstr_append_string(v, wc_buf); break; case POINTER_MESSAGE_ARGUMENT: sprintf(buf, "%p", cur_arg.u.pointer_val); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(v, wc_buf); break; case CHARACTER_MESSAGE_ARGUMENT: sprintf(buf, "%c", cur_arg.u.character_val); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(v, wc_buf); break; case MESSAGE_ARGUMENT_END: assert(0); break; default: assert(0); } } MEM_free(wc_format); }