From fe7d35171f0f74338ecbadf4aa5e0dbf76b4becf Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 5 May 2022 18:32:25 -0500 Subject: [PATCH] Remove file/popen - address #974 --- CHANGELOG.md | 1 + src/core/io.c | 71 ++++----------------------------------------- src/include/janet.h | 1 - 3 files changed, 7 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbab5ad7..dce36dd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Remove `file/popen`. Use `os/spawn` with the `:pipe` options instead. - Fix bug in peg `thru` and `to` combinators. - Fix printing issue in `doc` macro. - Numerous updates to function docstrings diff --git a/src/core/io.c b/src/core/io.c index a932371c..13721e5f 100644 --- a/src/core/io.c +++ b/src/core/io.c @@ -112,42 +112,6 @@ static void *makef(FILE *f, int32_t flags) { return iof; } -/* Open a process */ -#ifndef JANET_NO_PROCESSES -JANET_CORE_FN(cfun_io_popen, - "(file/popen command &opt mode) (DEPRECATED for os/spawn)", - "Open a file that is backed by a process. The file must be opened in either " - "the :r (read) or the :w (write) mode. In :r mode, the stdout of the " - "process can be read from the file. In :w mode, the stdin of the process " - "can be written to. Returns the new file.") { - janet_arity(argc, 1, 2); - const uint8_t *fname = janet_getstring(argv, 0); - const uint8_t *fmode = NULL; - int32_t flags; - if (argc == 2) { - fmode = janet_getkeyword(argv, 1); - flags = JANET_FILE_PIPED | checkflags(fmode); - if (flags & (JANET_FILE_UPDATE | JANET_FILE_BINARY | JANET_FILE_APPEND)) { - janet_panicf("invalid popen file mode :%S, expected :r or :w", fmode); - } - fmode = (const uint8_t *)((fmode[0] == 'r') ? "r" : "w"); - } else { - fmode = (const uint8_t *)"r"; - flags = JANET_FILE_PIPED | JANET_FILE_READ; - } -#ifdef JANET_WINDOWS -#define popen _popen -#endif - FILE *f = popen((const char *)fname, (const char *)fmode); - if (!f) { - if (flags & JANET_FILE_NONIL) - janet_panicf("failed to popen %s: %s", fname, strerror(errno)); - return janet_wrap_nil(); - } - return janet_makefile(f, flags); -} -#endif - JANET_CORE_FN(cfun_io_temp, "(file/temp)", "Open an anonymous temporary file that is removed on close. " @@ -296,7 +260,6 @@ JANET_CORE_FN(cfun_io_fflush, } #ifdef JANET_WINDOWS -#define pclose _pclose #define WEXITSTATUS(x) x #endif @@ -304,14 +267,7 @@ JANET_CORE_FN(cfun_io_fflush, int janet_file_close(JanetFile *file) { int ret = 0; if (!(file->flags & (JANET_FILE_NOT_CLOSEABLE | JANET_FILE_CLOSED))) { -#ifndef JANET_NO_PROCESSES - if (file->flags & JANET_FILE_PIPED) { - ret = pclose(file->file); - } else -#endif - { - ret = fclose(file->file); - } + ret = fclose(file->file); file->flags |= JANET_FILE_CLOSED; return ret; } @@ -331,30 +287,18 @@ JANET_CORE_FN(cfun_io_fclose, "(file/close f)", "Close a file and release all related resources. When you are " "done reading a file, close it to prevent a resource leak and let " - "other processes read the file. If the file is the result of a file/popen " - "call, close waits for and returns the process exit status.") { + "other processes read the file.") { janet_fixarity(argc, 1); JanetFile *iof = janet_getabstract(argv, 0, &janet_file_type); if (iof->flags & JANET_FILE_CLOSED) return janet_wrap_nil(); if (iof->flags & (JANET_FILE_NOT_CLOSEABLE)) janet_panic("file not closable"); - if (iof->flags & JANET_FILE_PIPED) { -#ifndef JANET_NO_PROCESSES - int status = pclose(iof->file); - iof->flags |= JANET_FILE_CLOSED; - if (status == -1) janet_panic("could not close file"); - return janet_wrap_integer(WEXITSTATUS(status)); -#else - return janet_wrap_nil(); -#endif - } else { - if (fclose(iof->file)) { - iof->flags |= JANET_FILE_NOT_CLOSEABLE; - janet_panic("could not close file"); - } - iof->flags |= JANET_FILE_CLOSED; + if (fclose(iof->file)) { + iof->flags |= JANET_FILE_NOT_CLOSEABLE; + janet_panic("could not close file"); } + iof->flags |= JANET_FILE_CLOSED; return janet_wrap_nil(); } @@ -802,9 +746,6 @@ void janet_lib_io(JanetTable *env) { JANET_CORE_REG("file/write", cfun_io_fwrite), JANET_CORE_REG("file/flush", cfun_io_fflush), JANET_CORE_REG("file/seek", cfun_io_fseek), -#ifndef JANET_NO_PROCESSES - JANET_CORE_REG("file/popen", cfun_io_popen), -#endif JANET_REG_END }; janet_core_cfuns_ext(env, NULL, io_cfuns); diff --git a/src/include/janet.h b/src/include/janet.h index 651f0740..5952ed1b 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -1972,7 +1972,6 @@ extern JANET_API const JanetAbstractType janet_file_type; #define JANET_FILE_CLOSED 32 #define JANET_FILE_BINARY 64 #define JANET_FILE_SERIALIZABLE 128 -#define JANET_FILE_PIPED 256 #define JANET_FILE_NONIL 512 JANET_API Janet janet_makefile(FILE *f, int32_t flags);