diff --git a/include/parrot/extend.h b/include/parrot/extend.h index 8c14ce6766..ccbefdc4ae 100644 --- a/include/parrot/extend.h +++ b/include/parrot/extend.h @@ -116,10 +116,23 @@ Parrot_Int Parrot_call_sub_ret_int(PARROT_INTERP, __attribute__nonnull__(1) __attribute__nonnull__(3); +PARROT_API +int Parrot_eprintf(NULLOK_INTERP, ARGIN(const char *s), ...) + __attribute__nonnull__(2); + PARROT_API PARROT_WARN_UNUSED_RESULT Parrot_Language Parrot_find_language(SHIM_INTERP, SHIM(char *language)); +PARROT_API +int Parrot_fprintf(PARROT_INTERP, + ARGIN(Parrot_PMC pio), + ARGIN(const char *s), + ...) + __attribute__nonnull__(1) + __attribute__nonnull__(2) + __attribute__nonnull__(3); + PARROT_API void Parrot_free_cstring(ARGIN_NULLOK(char *string)); @@ -359,6 +372,10 @@ Parrot_Int Parrot_PMC_typenum(PARROT_INTERP, ARGIN_NULLOK(const char *_class)) __attribute__nonnull__(1); +PARROT_API +int Parrot_printf(NULLOK_INTERP, ARGIN(const char *s), ...) + __attribute__nonnull__(2); + PARROT_API void Parrot_register_pmc(PARROT_INTERP, Parrot_PMC pmc) __attribute__nonnull__(1); @@ -385,6 +402,15 @@ PARROT_API void Parrot_unregister_pmc(PARROT_INTERP, Parrot_PMC pmc) __attribute__nonnull__(1); +PARROT_API +int Parrot_vfprintf(PARROT_INTERP, + ARGIN(Parrot_PMC pio), + ARGIN(const char *s), + va_list args) + __attribute__nonnull__(1) + __attribute__nonnull__(2) + __attribute__nonnull__(3); + /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */ /* HEADERIZER END: src/extend.c */ diff --git a/src/extend.c b/src/extend.c index 0d1c407ac2..64776ec09b 100644 --- a/src/extend.c +++ b/src/extend.c @@ -65,6 +65,103 @@ can. /* +=item C + +Writes a C string format with a varargs list to a PIO. + +=item C + +Writes a C string format with varargs to a PIO. + +=item C + +Writes a C string format with varargs to C. + +=item C + +Writes a C string format with varargs to C. + +*/ + +PARROT_API +int +Parrot_vfprintf(PARROT_INTERP, ARGIN(Parrot_PMC pio), + ARGIN(const char *s), va_list args) +{ + STRING * str; + INTVAL retval; + + PARROT_CALLIN_START(interp); + str = Parrot_vsprintf_c(interp, s, args); + retval = PIO_putps(interp, pio, str); + PARROT_CALLIN_END(interp); + + return retval; +} + +PARROT_API +int +Parrot_fprintf(PARROT_INTERP, ARGIN(Parrot_PMC pio), + ARGIN(const char *s), ...) +{ + va_list args; + INTVAL retval; + + va_start(args, s); + retval = Parrot_vfprintf(interp, pio, s, args); + va_end(args); + + return retval; +} + +PARROT_API +int +Parrot_printf(NULLOK_INTERP, ARGIN(const char *s), ...) +{ + va_list args; + INTVAL retval; + va_start(args, s); + + if (interp) { + retval = Parrot_vfprintf(interp, PIO_STDOUT(interp), s, args); + } + else { + /* Be nice about this... + ** XXX BD Should this use the default PIO_STDOUT or something? + */ + retval = vfprintf(stdout, s, args); + } + va_end(args); + + return retval; +} + +PARROT_API +int +Parrot_eprintf(NULLOK_INTERP, ARGIN(const char *s), ...) +{ + va_list args; + INTVAL retval; + + va_start(args, s); + + if (interp) { + retval = Parrot_vfprintf(interp, PIO_STDERR(interp), s, args); + } + else { + /* Be nice about this... + ** XXX BD Should this use the default PIO_STDOUT or something? + */ + retval=vfprintf(stderr, s, args); + } + + va_end(args); + + return retval; +} + +/* + =item C Return the integer keyed string value of the passed-in PMC diff --git a/t/src/extend.t b/t/src/extend.t index 588bcda82a..11bcbcae13 100644 --- a/t/src/extend.t +++ b/t/src/extend.t @@ -102,7 +102,7 @@ main(int argc, char* argv[]) { return 1; output = Parrot_new_string(interp, "Test", 4, "iso-8859-1", 0); - PIO_eprintf(interp, "%S\n", output); + Parrot_eprintf(interp, "%S\n", output); Parrot_exit(interp, 0); return 0; @@ -132,7 +132,7 @@ main(int argc, char* argv[]) { Parrot_set_strreg(interp, parrot_reg, value); new_value = Parrot_get_strreg(interp, parrot_reg); - PIO_eprintf(interp, "%S\n", new_value); + Parrot_eprintf(interp, "%S\n", new_value); Parrot_exit(interp, 0); return 0; @@ -309,7 +309,7 @@ main(int argc, char* argv[]) { Parrot_PMC_set_string(interp, testpmc, value); new_value = Parrot_PMC_get_string(interp, testpmc); - PIO_eprintf(interp, "%S\n", new_value); + Parrot_eprintf(interp, "%S\n", new_value); Parrot_exit(interp, 0); return 0; @@ -444,7 +444,7 @@ the_test(Parrot_Interp interp, opcode_t *cur_op, opcode_t *start) Parrot_loadbc(interp, pf); sub = Parrot_find_global_cur(interp, name); Parrot_call_sub(interp, sub, "v"); - PIO_eprintf(interp, "back\n"); + Parrot_eprintf(interp, "back\n"); /* win32 seems to buffer stderr ? */ PIO_flush(interp, PIO_STDERR(interp)); @@ -457,7 +457,7 @@ the_test(Parrot_Interp interp, opcode_t *cur_op, opcode_t *start) string_from_cstring(interp, "hello ", 0)); Parrot_call_sub(interp, sub, "vP", arg); - PIO_eprintf(interp, "back\n"); + Parrot_eprintf(interp, "back\n"); return NULL; } @@ -520,7 +520,7 @@ the_test(Parrot_Interp interp, opcode_t *cur_op, opcode_t *start) sub = Parrot_find_global_cur(interp, name); if (setjmp(jb.destination)) { - PIO_eprintf(interp, "caught\n"); + Parrot_eprintf(interp, "caught\n"); } else { /* pretend the EH was pushed by the sub call. */ @@ -530,7 +530,7 @@ the_test(Parrot_Interp interp, opcode_t *cur_op, opcode_t *start) Parrot_call_sub(interp, sub, "v"); } - PIO_eprintf(interp, "back\n"); + Parrot_eprintf(interp, "back\n"); return NULL; }