From 63f636e7b775bc8d2881104248f9579a089c4240 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 6 Nov 2018 11:09:14 -0500 Subject: limit integer types to maximum bit width of 65535 closes #1541 --- src/os.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/os.cpp') diff --git a/src/os.cpp b/src/os.cpp index f01a99fc23..27937be164 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -793,7 +793,7 @@ Error os_file_exists(Buf *full_path, bool *result) { } #if defined(ZIG_OS_POSIX) -static int os_exec_process_posix(const char *exe, ZigList &args, +static Error os_exec_process_posix(const char *exe, ZigList &args, Termination *term, Buf *out_stderr, Buf *out_stdout) { int stdin_pipe[2]; @@ -872,7 +872,7 @@ static int os_exec_process_posix(const char *exe, ZigList &args, // LocalFree(messageBuffer); //} -static int os_exec_process_windows(const char *exe, ZigList &args, +static Error os_exec_process_windows(const char *exe, ZigList &args, Termination *term, Buf *out_stderr, Buf *out_stdout) { Buf command_line = BUF_INIT; @@ -983,7 +983,7 @@ static int os_exec_process_windows(const char *exe, ZigList &args, CloseHandle(piProcInfo.hProcess); CloseHandle(piProcInfo.hThread); - return 0; + return ErrorNone; } #endif @@ -1003,7 +1003,7 @@ Error os_execv(const char *exe, const char **argv) { #endif } -int os_exec_process(const char *exe, ZigList &args, +Error os_exec_process(const char *exe, ZigList &args, Termination *term, Buf *out_stderr, Buf *out_stdout) { #if defined(ZIG_OS_WINDOWS) @@ -1027,7 +1027,7 @@ void os_write_file(Buf *full_path, Buf *contents) { zig_panic("close failed"); } -int os_copy_file(Buf *src_path, Buf *dest_path) { +Error os_copy_file(Buf *src_path, Buf *dest_path) { FILE *src_f = fopen(buf_ptr(src_path), "rb"); if (!src_f) { int err = errno; @@ -1074,7 +1074,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) { if (feof(src_f)) { fclose(src_f); fclose(dest_f); - return 0; + return ErrorNone; } } } @@ -1197,7 +1197,7 @@ bool os_stderr_tty(void) { } #if defined(ZIG_OS_POSIX) -static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) { +static Error os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) { const char *tmp_dir = getenv("TMPDIR"); if (!tmp_dir) { tmp_dir = P_tmpdir; @@ -1221,12 +1221,12 @@ static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_pat if (fclose(f)) zig_panic("close failed"); - return 0; + return ErrorNone; } #endif #if defined(ZIG_OS_WINDOWS) -static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) { +static Error os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) { char tmp_dir[MAX_PATH + 1]; if (GetTempPath(MAX_PATH, tmp_dir) == 0) { zig_panic("GetTempPath failed"); @@ -1255,11 +1255,11 @@ static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_p if (fclose(f)) { zig_panic("fclose failed"); } - return 0; + return ErrorNone; } #endif -int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) { +Error os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) { #if defined(ZIG_OS_WINDOWS) return os_buf_to_tmp_file_windows(contents, suffix, out_tmp_path); #elif defined(ZIG_OS_POSIX) @@ -1269,17 +1269,17 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) { #endif } -int os_delete_file(Buf *path) { +Error os_delete_file(Buf *path) { if (remove(buf_ptr(path))) { return ErrorFileSystem; } else { - return 0; + return ErrorNone; } } -int os_rename(Buf *src_path, Buf *dest_path) { +Error os_rename(Buf *src_path, Buf *dest_path) { if (buf_eql_buf(src_path, dest_path)) { - return 0; + return ErrorNone; } #if defined(ZIG_OS_WINDOWS) if (!MoveFileExA(buf_ptr(src_path), buf_ptr(dest_path), MOVEFILE_REPLACE_EXISTING)) { @@ -1290,7 +1290,7 @@ int os_rename(Buf *src_path, Buf *dest_path) { return ErrorFileSystem; } #endif - return 0; + return ErrorNone; } double os_get_time(void) { -- cgit v1.2.3 From 8e69a18d8c01aaf2bf4f0c0f8495e47cb36c284f Mon Sep 17 00:00:00 2001 From: emekoi Date: Wed, 7 Nov 2018 23:36:36 -0600 Subject: made colored output more consistent (#1706) * made colored output more consistent * added os.supportsAnsiEscapeCodes --- src/os.cpp | 4 --- std/debug/index.zig | 87 +++++++++++++++++++++++++++++++------------------ std/os/index.zig | 12 +++++++ std/os/windows/util.zig | 2 +- 4 files changed, 69 insertions(+), 36 deletions(-) (limited to 'src/os.cpp') diff --git a/src/os.cpp b/src/os.cpp index 27937be164..ea88a8ef98 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1128,9 +1128,6 @@ Error os_get_cwd(Buf *out_cwd) { #define is_wprefix(s, prefix) \ (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0) static bool is_stderr_cyg_pty(void) { -#if defined(__MINGW32__) - return false; -#else HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE); if (stderr_handle == INVALID_HANDLE_VALUE) return false; @@ -1182,7 +1179,6 @@ static bool is_stderr_cyg_pty(void) { } free(nameinfo); return (p != NULL); -#endif } #endif diff --git a/std/debug/index.zig b/std/debug/index.zig index 0508c17feb..a0d2f65339 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -171,7 +171,9 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c os.abort(); } +const RED = "\x1b[31;1m"; const GREEN = "\x1b[32;1m"; +const CYAN = "\x1b[36;1m"; const WHITE = "\x1b[37;1m"; const DIM = "\x1b[2m"; const RESET = "\x1b[0m"; @@ -454,38 +456,61 @@ const TtyColor = enum.{ /// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt fn setTtyColor(tty_color: TtyColor) void { - const S = struct.{ - var attrs: windows.WORD = undefined; - var init_attrs = false; - }; - if (!S.init_attrs) { - S.init_attrs = true; - var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; - // TODO handle error - _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info); - S.attrs = info.wAttributes; - } + if (os.supportsAnsiEscapeCodes(stderr_file.handle)) { + switch (tty_color) { + TtyColor.Red => { + stderr_file.write(RED) catch return; + }, + TtyColor.Green => { + stderr_file.write(GREEN) catch return; + }, + TtyColor.Cyan => { + stderr_file.write(CYAN) catch return; + }, + TtyColor.White, TtyColor.Bold => { + stderr_file.write(WHITE) catch return; + }, + TtyColor.Dim => { + stderr_file.write(DIM) catch return; + }, + TtyColor.Reset => { + stderr_file.write(RESET) catch return; + }, + } + } else { + const S = struct.{ + var attrs: windows.WORD = undefined; + var init_attrs = false; + }; + if (!S.init_attrs) { + S.init_attrs = true; + var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; + // TODO handle error + _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info); + S.attrs = info.wAttributes; + } - // TODO handle errors - switch (tty_color) { - TtyColor.Red => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY); - }, - TtyColor.Green => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY); - }, - TtyColor.Cyan => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); - }, - TtyColor.White, TtyColor.Bold => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); - }, - TtyColor.Dim => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY); - }, - TtyColor.Reset => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs); - }, + // TODO handle errors + switch (tty_color) { + TtyColor.Red => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY); + }, + TtyColor.Green => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY); + }, + TtyColor.Cyan => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); + }, + TtyColor.White, TtyColor.Bold => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); + }, + TtyColor.Dim => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY); + }, + TtyColor.Reset => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs); + }, + } } } diff --git a/std/os/index.zig b/std/os/index.zig index d9e6c3c81d..c04baeeb91 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -2272,6 +2272,18 @@ pub fn isTty(handle: FileHandle) bool { } } +pub fn supportsAnsiEscapeCodes(handle: FileHandle) bool { + if (is_windows) { + return windows_util.windowsIsCygwinPty(handle); + } else { + if (builtin.link_libc) { + return c.isatty(handle) != 0; + } else { + return posix.isatty(handle); + } + } +} + pub const PosixSocketError = error.{ /// Permission to create a socket of the specified type and/or /// pro‐tocol is denied. diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig index 07d68b0a46..cb245f1190 100644 --- a/std/os/windows/util.zig +++ b/std/os/windows/util.zig @@ -91,7 +91,7 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool { @ptrCast(*c_void, &name_info_bytes[0]), @intCast(u32, name_info_bytes.len), ) == 0) { - return true; + return false; } const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]); -- cgit v1.2.3 From 398914eb713cf4c279edd4b30fc75e88a3a722b3 Mon Sep 17 00:00:00 2001 From: Vallentin Date: Fri, 16 Nov 2018 19:33:58 +0100 Subject: Fixed typos --- doc/langref.html.in | 6 +++--- src/cache_hash.cpp | 2 +- src/os.cpp | 6 +++--- std/base64.zig | 2 +- std/crypto/x25519.zig | 2 +- std/elf.zig | 2 +- std/fmt/errol/index.zig | 2 +- std/math/big/int.zig | 2 +- std/math/ilogb.zig | 2 +- std/os/time.zig | 6 +++--- 10 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src/os.cpp') diff --git a/doc/langref.html.in b/doc/langref.html.in index 2332841305..c61203dca6 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -954,7 +954,7 @@ a /= b{#endsyntax#}
  • {#link|Floats#}
  • - Divison. + Division.
    • Can cause {#link|overflow|Default Operations#} for integers.
    • Can cause {#link|Division by Zero#} for integers.
    • @@ -3474,7 +3474,7 @@ fn createFoo(param: i32) !Foo {
    • Since Zig understands error types, it can pre-weight branches in favor of - errors not occuring. Just a small optimization benefit that is not available + errors not occurring. Just a small optimization benefit that is not available in other languages.
    @@ -7614,7 +7614,7 @@ coding style. Open braces on same line, unless you need to wrap.
  • If a list of things is longer than 2, put each item on its own line and - exercise the abilty to put an extra comma at the end. + exercise the ability to put an extra comma at the end.
  • Line length: aim for 100; use common sense. diff --git a/src/cache_hash.cpp b/src/cache_hash.cpp index 0ac7615a3a..7a3c08bc27 100644 --- a/src/cache_hash.cpp +++ b/src/cache_hash.cpp @@ -294,7 +294,7 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { chf = &ch->files.at(file_i); } else if (any_file_changed) { // cache miss. - // keep the the manifest file open with the rw lock + // keep the manifest file open with the rw lock // reset the hash rc = blake2b_init(&ch->blake, 48); assert(rc == 0); diff --git a/src/os.cpp b/src/os.cpp index ea88a8ef98..5d43e73d8a 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1536,7 +1536,7 @@ int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_Arch buf_append_str(output_buf, "arm\\"); break; default: - zig_panic("Attemped to use vcruntime for non-supported platform."); + zig_panic("Attempted to use vcruntime for non-supported platform."); } Buf* tmp_buf = buf_alloc(); buf_init_from_buf(tmp_buf, output_buf); @@ -1585,7 +1585,7 @@ int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchTy buf_append_str(output_buf, "arm\\"); break; default: - zig_panic("Attemped to use vcruntime for non-supported platform."); + zig_panic("Attempted to use vcruntime for non-supported platform."); } Buf* tmp_buf = buf_alloc(); buf_init_from_buf(tmp_buf, output_buf); @@ -1608,7 +1608,7 @@ int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchTy buf_append_str(output_buf, "arm\\"); break; default: - zig_panic("Attemped to use vcruntime for non-supported platform."); + zig_panic("Attempted to use vcruntime for non-supported platform."); } Buf* tmp_buf = buf_alloc(); buf_init_from_buf(tmp_buf, output_buf); diff --git a/std/base64.zig b/std/base64.zig index 97b86e0713..bc0bdb1bd3 100644 --- a/std/base64.zig +++ b/std/base64.zig @@ -180,7 +180,7 @@ pub const Base64DecoderWithIgnore = struct { /// Invalid characters that are not ignored result in error.InvalidCharacter. /// Invalid padding results in error.InvalidPadding. /// Decoding more data than can fit in dest results in error.OutputTooSmall. See also ::calcSizeUpperBound. - /// Returns the number of bytes writen to dest. + /// Returns the number of bytes written to dest. pub fn decode(decoder_with_ignore: *const Base64DecoderWithIgnore, dest: []u8, source: []const u8) !usize { const decoder = &decoder_with_ignore.decoder; diff --git a/std/crypto/x25519.zig b/std/crypto/x25519.zig index bf78511f9a..281813b457 100644 --- a/std/crypto/x25519.zig +++ b/std/crypto/x25519.zig @@ -52,7 +52,7 @@ pub const X25519 = struct { // computes the actual scalar product (the result is in x2 and z2) // Montgomery ladder - // In projective coordinates, to avoid divisons: x = X / Z + // In projective coordinates, to avoid divisions: x = X / Z // We don't care about the y coordinate, it's only 1 bit of information Fe.init1(x2); Fe.init0(z2); // "zero" point diff --git a/std/elf.zig b/std/elf.zig index 904ddf1dad..e95222744d 100644 --- a/std/elf.zig +++ b/std/elf.zig @@ -490,7 +490,7 @@ pub const Elf = struct { if (sh_entry_size != 40) return error.InvalidFormat; for (elf.section_headers) |*elf_section| { - // TODO (multiple occurences) allow implicit cast from %u32 -> %u64 ? + // TODO (multiple occurrences) allow implicit cast from %u32 -> %u64 ? elf_section.name = try in.readInt(elf.endian, u32); elf_section.sh_type = try in.readInt(elf.endian, u32); elf_section.flags = u64(try in.readInt(elf.endian, u32)); diff --git a/std/fmt/errol/index.zig b/std/fmt/errol/index.zig index 9cc5b2c1cd..0fdb1c4ab8 100644 --- a/std/fmt/errol/index.zig +++ b/std/fmt/errol/index.zig @@ -164,7 +164,7 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal { // digit generation // We generate digits starting at index 1. If rounding a buffer later then it may be - // required to generate a preceeding digit in some cases (9.999) in which case we use + // required to generate a preceding digit in some cases (9.999) in which case we use // the 0-index for this extra digit. var buf_index: usize = 1; while (true) { diff --git a/std/math/big/int.zig b/std/math/big/int.zig index c3f6bbccef..cda2e1419f 100644 --- a/std/math/big/int.zig +++ b/std/math/big/int.zig @@ -169,7 +169,7 @@ pub const Int = struct { return self.fitsInTwosComp(T.is_signed, T.bit_count); } - // Returns the approximate size of the integer in the given base. Negative values accomodate for + // Returns the approximate size of the integer in the given base. Negative values accommodate for // the minus sign. This is used for determining the number of characters needed to print the // value. It is inexact and will exceed the given value by 1-2 digits. pub fn sizeInBase(self: Int, base: usize) usize { diff --git a/std/math/ilogb.zig b/std/math/ilogb.zig index c787ab68f9..e6bdb14012 100644 --- a/std/math/ilogb.zig +++ b/std/math/ilogb.zig @@ -19,7 +19,7 @@ pub fn ilogb(x: var) i32 { }; } -// NOTE: Should these be exposed publically? +// NOTE: Should these be exposed publicly? const fp_ilogbnan = -1 - i32(maxInt(u32) >> 1); const fp_ilogb0 = fp_ilogbnan; diff --git a/std/os/time.zig b/std/os/time.zig index 236111a31c..d9fe046a55 100644 --- a/std/os/time.zig +++ b/std/os/time.zig @@ -124,7 +124,7 @@ pub const s_per_week = s_per_day * 7; /// A monotonic high-performance timer. /// Timer.start() must be called to initialize the struct, which captures /// the counter frequency on windows and darwin, records the resolution, -/// and gives the user an oportunity to check for the existnece of +/// and gives the user an opportunity to check for the existnece of /// monotonic clocks without forcing them to check for error on each read. /// .resolution is in nanoseconds on all platforms but .start_time's meaning /// depends on the OS. On Windows and Darwin it is a hardware counter @@ -152,11 +152,11 @@ pub const Timer = struct { //}; const monotonic_clock_id = posix.CLOCK_MONOTONIC; /// Initialize the timer structure. - //This gives us an oportunity to grab the counter frequency in windows. + //This gives us an opportunity to grab the counter frequency in windows. //On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000. //On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not // supported, or if the timespec pointer is out of bounds, which should be - // impossible here barring cosmic rays or other such occurances of + // impossible here barring cosmic rays or other such occurrences of // incredibly bad luck. //On Darwin: This cannot fail, as far as I am able to tell. const TimerError = error{ -- cgit v1.2.3