Skip to content

Commit

Permalink
builtin: support -d builtin_print_use_fprintf, make the C fn declar…
Browse files Browse the repository at this point in the history
…ations stricter (vlang#22137)
  • Loading branch information
spytheman authored Aug 31, 2024
1 parent 515b106 commit 217b191
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
16 changes: 16 additions & 0 deletions vlib/builtin/builtin.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ pub fn eprintln(s string) {
eprintln('eprintln(NIL)')
return
}
$if builtin_print_use_fprintf ? {
C.fprintf(C.stderr, c'%.*s\n', s.len, s.str)
return
}
$if freestanding {
// flushing is only a thing with C.FILE from stdio.h, not on the syscall level
bare_eprint(s.str, u64(s.len))
Expand All @@ -205,6 +209,10 @@ pub fn eprint(s string) {
eprint('eprint(NIL)')
return
}
$if builtin_print_use_fprintf ? {
C.fprintf(C.stderr, c'%.*s', s.len, s.str)
return
}
$if freestanding {
// flushing is only a thing with C.FILE from stdio.h, not on the syscall level
bare_eprint(s.str, u64(s.len))
Expand Down Expand Up @@ -267,6 +275,10 @@ pub fn unbuffer_stdout() {
// print prints a message to stdout. Note that unlike `eprint`, stdout is not automatically flushed.
@[manualfree]
pub fn print(s string) {
$if builtin_print_use_fprintf ? {
C.fprintf(C.stdout, c'%.*s', s.len, s.str)
return
}
$if android && !termux {
C.android_print(C.stdout, c'%.*s\n', s.len, s.str)
} $else $if ios {
Expand All @@ -289,6 +301,10 @@ pub fn println(s string) {
$if noprintln ? {
return
}
$if builtin_print_use_fprintf ? {
C.fprintf(C.stdout, c'%.*s\n', s.len, s.str)
return
}
$if android && !termux {
C.android_print(C.stdout, c'%.*s\n', s.len, s.str)
return
Expand Down
6 changes: 4 additions & 2 deletions vlib/builtin/builtin_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
module builtin

fn builtin_init() {
$if !gc_warn_on_stderr ? {
gc_set_warn_proc(internal_gc_warn_proc_none)
$if gcboehm ? {
$if !gc_warn_on_stderr ? {
gc_set_warn_proc(internal_gc_warn_proc_none)
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions vlib/builtin/builtin_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ const enable_wrap_at_eol_output = 2
const evable_virtual_terminal_processing = 4

fn builtin_init() {
$if !gc_warn_on_stderr ? {
gc_set_warn_proc(internal_gc_warn_proc_none)
$if gcboehm ? {
$if !gc_warn_on_stderr ? {
gc_set_warn_proc(internal_gc_warn_proc_none)
}
}
g_original_codepage = C.GetConsoleOutputCP()
C.SetConsoleOutputCP(cp_utf8)
Expand Down
40 changes: 20 additions & 20 deletions vlib/builtin/cfns.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ fn C.exit(code int)

fn C.qsort(base voidptr, items usize, item_size usize, cb C.qsort_callback_func)

fn C.sprintf(a ...voidptr) int

fn C.strlen(s &char) int

fn C.sscanf(&u8, &u8, ...&u8) int

@[trusted]
fn C.isdigit(c int) bool

Expand All @@ -43,21 +39,31 @@ fn C.popen(c &char, t &char) voidptr
// <libproc.h>
pub fn proc_pidpath(int, voidptr, int) int

fn C.realpath(&char, &char) &char
fn C.realpath(const_path &char, resolved_path &char) &char

// fn C.chmod(byteptr, mode_t) int
fn C.chmod(&char, u32) int
fn C.chmod(path &char, mode u32) int

fn C.printf(&char, ...voidptr) int
fn C.printf(const_format &char, opt ...voidptr) int
fn C.dprintf(fd int, const_format &char, opt ...voidptr) int
fn C.fprintf(fstream &C.FILE, const_format &char, opt ...voidptr) int
fn C.sprintf(str &char, const_format &char, opt ...voidptr) int
fn C.snprintf(str &char, size usize, const_format &char, opt ...voidptr) int
fn C.wprintf(const_format &u16, opt ...voidptr) int

// used by Android for (e)println to output to the Android log system / logcat
pub fn C.android_print(fstream voidptr, format &char, opt ...voidptr)

fn C.scanf(&char, ...voidptr) int
fn C.sscanf(str &char, const_format &char, opt ...voidptr) int
fn C.scanf(const_format &char, opt ...voidptr) int

fn C.puts(&char) int
fn C.puts(msg &char) int
@[trusted]
fn C.abs(f64) f64

fn C.fputs(str &char, stream &C.FILE) int
fn C.fputs(msg &char, fstream &C.FILE) int

fn C.fflush(&C.FILE) int
fn C.fflush(fstream &C.FILE) int

// TODO: define args in these functions
fn C.fseek(stream &C.FILE, offset int, whence int) int
Expand All @@ -81,8 +87,10 @@ fn C.strchr(s &char, c int) &char
@[trusted]
fn C.getpid() int

@[trusted]
fn C.getuid() int

@[trusted]
fn C.geteuid() int

fn C.system(cmd &char) int
Expand Down Expand Up @@ -203,10 +211,6 @@ fn C.strncmp(s &char, s2 &char, n int) int
@[trusted]
fn C.strerror(int) &char

fn C.snprintf(str &char, size usize, format &char, opt ...voidptr) int

fn C.fprintf(voidptr, &char, ...voidptr)

@[trusted]
fn C.WIFEXITED(status int) bool

Expand Down Expand Up @@ -295,8 +299,6 @@ fn C.GetConsoleMode(voidptr, &u32) bool
@[trusted]
fn C.GetCurrentProcessId() u32

fn C.wprintf()

// fn C.setbuf()
fn C.setbuf(voidptr, &char)

Expand Down Expand Up @@ -504,8 +506,6 @@ fn C.glTexImage2D()
// used by ios for println
fn C.WrappedNSLog(str &u8)

// used by Android for (e)println to output to the Android log system / logcat
pub fn C.android_print(voidptr, &char, ...voidptr)

// absolute value
@[trusted]
fn C.abs(number int) int

0 comments on commit 217b191

Please sign in to comment.