parent
3dea0ac8e1
commit
fa925f3092
|
@ -8,7 +8,7 @@ import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = '0.1.3'
|
Version = '0.1.4'
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO no caps
|
// TODO no caps
|
||||||
|
@ -389,9 +389,9 @@ string _STR_TMP(const char *fmt, ...) {
|
||||||
if os.args.len > 3 {
|
if os.args.len > 3 {
|
||||||
cmd += ' ' + os.args.right(3).join(' ')
|
cmd += ' ' + os.args.right(3).join(' ')
|
||||||
}
|
}
|
||||||
ret := os.system2(cmd)
|
ret := os.system(cmd)
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
s := os.system(cmd)
|
s := os.exec(cmd)
|
||||||
println(s)
|
println(s)
|
||||||
println('ret not 0, exiting')
|
println('ret not 0, exiting')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
@ -496,7 +496,7 @@ mut args := ''
|
||||||
println('\n==========\n$cmd\n=========\n')
|
println('\n==========\n$cmd\n=========\n')
|
||||||
}
|
}
|
||||||
// Run
|
// Run
|
||||||
res := os.system(cmd)
|
res := os.exec(cmd)
|
||||||
// println('C OUTPUT:')
|
// println('C OUTPUT:')
|
||||||
if res.contains('error: ') {
|
if res.contains('error: ') {
|
||||||
println(res)
|
println(res)
|
||||||
|
@ -507,7 +507,7 @@ mut args := ''
|
||||||
c.out_name = c.out_name.replace('.o', '')
|
c.out_name = c.out_name.replace('.o', '')
|
||||||
obj_file := c.out_name + '.o'
|
obj_file := c.out_name + '.o'
|
||||||
println('linux obj_file=$obj_file out_name=$c.out_name')
|
println('linux obj_file=$obj_file out_name=$c.out_name')
|
||||||
ress := os.system('/usr/local/Cellar/llvm/8.0.0/bin/ld.lld --sysroot=$sysroot ' +
|
ress := os.exec('/usr/local/Cellar/llvm/8.0.0/bin/ld.lld --sysroot=$sysroot ' +
|
||||||
'-v -o $c.out_name ' +
|
'-v -o $c.out_name ' +
|
||||||
'-m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 ' +
|
'-m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 ' +
|
||||||
'/usr/lib/x86_64-linux-gnu/crt1.o ' +
|
'/usr/lib/x86_64-linux-gnu/crt1.o ' +
|
||||||
|
@ -795,7 +795,7 @@ fn new_v(args[]string) *V {
|
||||||
} else {
|
} else {
|
||||||
println('V repo not found. Cloning...')
|
println('V repo not found. Cloning...')
|
||||||
os.mv('v', 'v.bin')
|
os.mv('v', 'v.bin')
|
||||||
os.system('git clone https://github.com/vlang/v')
|
os.exec('git clone https://github.com/vlang/v')
|
||||||
if !os.dir_exists('v') {
|
if !os.dir_exists('v') {
|
||||||
println('failed to clone github.com/vlang/v')
|
println('failed to clone github.com/vlang/v')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
@ -872,7 +872,7 @@ fn run_repl() []string {
|
||||||
os.write_file(file, source_code)
|
os.write_file(file, source_code)
|
||||||
mut v := new_v( ['v', '-repl', file])
|
mut v := new_v( ['v', '-repl', file])
|
||||||
v.compile()
|
v.compile()
|
||||||
s := os.system(TmpPath + '/vrepl')
|
s := os.exec(TmpPath + '/vrepl')
|
||||||
println(s)
|
println(s)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -138,6 +138,7 @@ fn new_table(obfuscate bool) *Table {
|
||||||
t.register_const('stdin', 'int', 'main', false)
|
t.register_const('stdin', 'int', 'main', false)
|
||||||
t.register_const('stdout', 'int', 'main', false)
|
t.register_const('stdout', 'int', 'main', false)
|
||||||
t.register_const('stderr', 'int', 'main', false)
|
t.register_const('stderr', 'int', 'main', false)
|
||||||
|
t.register_const('errno', 'int', 'main', false)
|
||||||
t.register_type_with_parent('map_string', 'map')
|
t.register_type_with_parent('map_string', 'map')
|
||||||
t.register_type_with_parent('map_int', 'map')
|
t.register_type_with_parent('map_int', 'map')
|
||||||
return t
|
return t
|
||||||
|
|
80
os/os.v
80
os/os.v
|
@ -16,6 +16,11 @@ struct File {
|
||||||
cfile *FILE
|
cfile *FILE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct FileInfo {
|
||||||
|
name string
|
||||||
|
size int
|
||||||
|
}
|
||||||
|
|
||||||
import const (
|
import const (
|
||||||
SEEK_SET
|
SEEK_SET
|
||||||
SEEK_END
|
SEEK_END
|
||||||
|
@ -64,14 +69,17 @@ pub fn read_file(path string) ?string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
// TODO
|
||||||
fn (f File) read_rune() string {
|
fn (f File) read_rune() string {
|
||||||
# if (!f.cfile) return tos("", 0);
|
# if (!f.cfile) return tos("", 0);
|
||||||
c := malloc(1)
|
c := malloc(1)
|
||||||
C.fread(c, 1, 1, f.cfile)
|
C.fread(c, 1, 1, f.cfile)
|
||||||
return tos(c, 1)
|
return tos(c, 1)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// `file_size` returns the size of the file located in `path`.
|
// file_size returns the size of the file located in `path`.
|
||||||
pub fn file_size(path string) int {
|
pub fn file_size(path string) int {
|
||||||
# struct stat s;
|
# struct stat s;
|
||||||
# stat(path.str, &s);
|
# stat(path.str, &s);
|
||||||
|
@ -92,6 +100,7 @@ pub fn mv(old, new string) {
|
||||||
C.rename(old.cstr(), new.cstr())
|
C.rename(old.cstr(), new.cstr())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
pub fn file_last_mod_unix(path string) int {
|
pub fn file_last_mod_unix(path string) int {
|
||||||
# struct stat attr;
|
# struct stat attr;
|
||||||
# stat(path.str, &attr);
|
# stat(path.str, &attr);
|
||||||
|
@ -99,7 +108,6 @@ pub fn file_last_mod_unix(path string) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
pub fn file_last_mod_time(path string) time.Time {
|
pub fn file_last_mod_time(path string) time.Time {
|
||||||
return time.now()
|
return time.now()
|
||||||
q := C.tm{}
|
q := C.tm{}
|
||||||
|
@ -138,7 +146,7 @@ pub fn read_lines(path string) []string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_file_into_ulines(path string) []ustring {
|
fn read_ulines(path string) []ustring {
|
||||||
lines := read_lines(path)
|
lines := read_lines(path)
|
||||||
// mut ulines := new_array(0, lines.len, sizeof(ustring))
|
// mut ulines := new_array(0, lines.len, sizeof(ustring))
|
||||||
mut ulines := []ustring
|
mut ulines := []ustring
|
||||||
|
@ -149,25 +157,12 @@ fn read_file_into_ulines(path string) []ustring {
|
||||||
return ulines
|
return ulines
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
/*
|
||||||
BUF_SIZE = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
fn append_to_file(file, s string) {
|
|
||||||
# FILE* fp = fopen(file.str, "a");
|
|
||||||
# fputs(s.str, fp);
|
|
||||||
# fputs("\n", fp);
|
|
||||||
# fclose(fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Reader {
|
struct Reader {
|
||||||
fp *FILE
|
fp *FILE
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
struct FileInfo {
|
|
||||||
name string
|
|
||||||
size int
|
|
||||||
}
|
|
||||||
|
|
||||||
// fn open(file string) File? {
|
// fn open(file string) File? {
|
||||||
// return open_file(file)
|
// return open_file(file)
|
||||||
|
@ -222,11 +217,11 @@ fn (f File) append(s string) {
|
||||||
// convert any value to []byte (LittleEndian) and write it
|
// convert any value to []byte (LittleEndian) and write it
|
||||||
// for example if we have write(7, 4), "07 00 00 00" gets written
|
// for example if we have write(7, 4), "07 00 00 00" gets written
|
||||||
// write(0x1234, 2) => "34 12"
|
// write(0x1234, 2) => "34 12"
|
||||||
fn (f File) write(data voidptr, size int) {
|
fn (f File) write_bytes(data voidptr, size int) {
|
||||||
C.fwrite(data, 1, size, f.cfile)
|
C.fwrite(data, 1, size, f.cfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (f File) write_at(data voidptr, size, pos int) {
|
fn (f File) write_bytes_at(data voidptr, size, pos int) {
|
||||||
C.fseek(f.cfile, pos, SEEK_SET)
|
C.fseek(f.cfile, pos, SEEK_SET)
|
||||||
C.fwrite(data, 1, size, f.cfile)
|
C.fwrite(data, 1, size, f.cfile)
|
||||||
C.fseek(f.cfile, 0, SEEK_END)
|
C.fseek(f.cfile, 0, SEEK_END)
|
||||||
|
@ -252,11 +247,9 @@ fn close_file(fp *FILE) {
|
||||||
C.fclose(fp)
|
C.fclose(fp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// `system2` starts the specified command, waits for it to complete, and returns its code.
|
// system starts the specified command, waits for it to complete, and returns its code.
|
||||||
pub fn system2(cmd string) int {
|
pub fn system(cmd string) int {
|
||||||
cstr := cmd.clone()
|
ret := C.system(cmd.cstr())
|
||||||
ret := int(C.system(cstr.cstr()))
|
|
||||||
// println(' system2 ret=$ret cmd="$s"')
|
|
||||||
if ret == -1 {
|
if ret == -1 {
|
||||||
os.print_c_errno()
|
os.print_c_errno()
|
||||||
}
|
}
|
||||||
|
@ -273,27 +266,25 @@ fn popen(path string) *FILE {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO rename to run or exec (system doesnt return a string)
|
// exec starts the specified command, waits for it to complete, and returns its output.
|
||||||
// `system` starts the specified command, waits for it to complete, and returns its output.
|
pub fn exec(cmd string) string {
|
||||||
// TODO merge the two functions.
|
cmd = '$cmd 2>&1'
|
||||||
pub fn system(cmd string) string {
|
f := popen(cmd)
|
||||||
// println('OS SYSTEM($s)')
|
|
||||||
res := ''
|
|
||||||
ss := '$cmd 2>&1'
|
|
||||||
_ := 0// TODO DOLLAR TOKEN
|
|
||||||
f := popen(ss)// cmd)
|
|
||||||
// # if (!f)
|
|
||||||
if isnil(f) {
|
if isnil(f) {
|
||||||
|
// TODO optional or error code
|
||||||
println('popen $cmd failed')
|
println('popen $cmd failed')
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
buf := [1000]byte
|
||||||
|
mut res := ''
|
||||||
|
for C.fgets(buf, 1000, f) != 0 {
|
||||||
|
res += tos(buf, strlen(buf))
|
||||||
}
|
}
|
||||||
max := 1000
|
|
||||||
# char buf[max];
|
|
||||||
# while (fgets(buf, max, f) != NULL) {
|
|
||||||
# res = string_add(res, tos(buf, strlen(buf)));
|
|
||||||
# }
|
|
||||||
return res.trim_space()
|
return res.trim_space()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
// TODO
|
||||||
fn system_into_lines(s string) []string {
|
fn system_into_lines(s string) []string {
|
||||||
mut res := []string
|
mut res := []string
|
||||||
cmd := '$s 2>&1'
|
cmd := '$s 2>&1'
|
||||||
|
@ -314,6 +305,7 @@ fn system_into_lines(s string) []string {
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// `getenv` returns the value of the environment variable named by the key.
|
// `getenv` returns the value of the environment variable named by the key.
|
||||||
pub fn getenv(key string) string {
|
pub fn getenv(key string) string {
|
||||||
|
@ -326,7 +318,6 @@ pub fn getenv(key string) string {
|
||||||
|
|
||||||
// `file_exists` returns true if `path` exists.
|
// `file_exists` returns true if `path` exists.
|
||||||
pub fn file_exists(path string) bool {
|
pub fn file_exists(path string) bool {
|
||||||
// # return access( path.str, F_OK ) != -1 ;
|
|
||||||
res := false
|
res := false
|
||||||
$if windows {
|
$if windows {
|
||||||
# res = _access( path.str, 0 ) != -1 ;
|
# res = _access( path.str, 0 ) != -1 ;
|
||||||
|
@ -383,7 +374,7 @@ fn rmdir(path, guard string) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fn print_c_errno() {
|
fn print_c_errno() {
|
||||||
# printf("errno=%d err='%s'\n", errno, strerror(errno));
|
C.printf('errno=%d err="%s"\n', errno, C.strerror(errno))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -416,7 +407,7 @@ pub fn filename(path string) string {
|
||||||
return path.all_after('/')
|
return path.all_after('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get_line returns a one-line string from stdin
|
||||||
pub fn get_line() string {
|
pub fn get_line() string {
|
||||||
max := 256
|
max := 256
|
||||||
buf := malloc(max)
|
buf := malloc(max)
|
||||||
|
@ -440,7 +431,7 @@ pub fn user_os() string {
|
||||||
return 'unknown'
|
return 'unknown'
|
||||||
}
|
}
|
||||||
|
|
||||||
// `home_dir` returns path to user's home directory.
|
// home_dir returns path to user's home directory.
|
||||||
pub fn home_dir() string {
|
pub fn home_dir() string {
|
||||||
mut home := os.getenv('HOME')
|
mut home := os.getenv('HOME')
|
||||||
$if windows {
|
$if windows {
|
||||||
|
@ -451,6 +442,7 @@ pub fn home_dir() string {
|
||||||
return home
|
return home
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write_file writes text data to a file in `path`.
|
||||||
pub fn write_file(path, text string) {
|
pub fn write_file(path, text string) {
|
||||||
f := os.create(path)
|
f := os.create(path)
|
||||||
f.appendln(text)
|
f.appendln(text)
|
||||||
|
|
Loading…
Reference in New Issue