1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-17 02:40:00 +00:00

Remove file/popen - address #974

This commit is contained in:
Calvin Rose 2022-05-05 18:32:25 -05:00
parent b3aed13567
commit fe7d35171f
3 changed files with 7 additions and 66 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ??? ## Unreleased - ???
- Remove `file/popen`. Use `os/spawn` with the `:pipe` options instead.
- Fix bug in peg `thru` and `to` combinators. - Fix bug in peg `thru` and `to` combinators.
- Fix printing issue in `doc` macro. - Fix printing issue in `doc` macro.
- Numerous updates to function docstrings - Numerous updates to function docstrings

View File

@ -112,42 +112,6 @@ static void *makef(FILE *f, int32_t flags) {
return iof; 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, JANET_CORE_FN(cfun_io_temp,
"(file/temp)", "(file/temp)",
"Open an anonymous temporary file that is removed on close. " "Open an anonymous temporary file that is removed on close. "
@ -296,7 +260,6 @@ JANET_CORE_FN(cfun_io_fflush,
} }
#ifdef JANET_WINDOWS #ifdef JANET_WINDOWS
#define pclose _pclose
#define WEXITSTATUS(x) x #define WEXITSTATUS(x) x
#endif #endif
@ -304,14 +267,7 @@ JANET_CORE_FN(cfun_io_fflush,
int janet_file_close(JanetFile *file) { int janet_file_close(JanetFile *file) {
int ret = 0; int ret = 0;
if (!(file->flags & (JANET_FILE_NOT_CLOSEABLE | JANET_FILE_CLOSED))) { if (!(file->flags & (JANET_FILE_NOT_CLOSEABLE | JANET_FILE_CLOSED))) {
#ifndef JANET_NO_PROCESSES ret = fclose(file->file);
if (file->flags & JANET_FILE_PIPED) {
ret = pclose(file->file);
} else
#endif
{
ret = fclose(file->file);
}
file->flags |= JANET_FILE_CLOSED; file->flags |= JANET_FILE_CLOSED;
return ret; return ret;
} }
@ -331,30 +287,18 @@ JANET_CORE_FN(cfun_io_fclose,
"(file/close f)", "(file/close f)",
"Close a file and release all related resources. When you are " "Close a file and release all related resources. When you are "
"done reading a file, close it to prevent a resource leak and let " "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 " "other processes read the file.") {
"call, close waits for and returns the process exit status.") {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
JanetFile *iof = janet_getabstract(argv, 0, &janet_file_type); JanetFile *iof = janet_getabstract(argv, 0, &janet_file_type);
if (iof->flags & JANET_FILE_CLOSED) if (iof->flags & JANET_FILE_CLOSED)
return janet_wrap_nil(); return janet_wrap_nil();
if (iof->flags & (JANET_FILE_NOT_CLOSEABLE)) if (iof->flags & (JANET_FILE_NOT_CLOSEABLE))
janet_panic("file not closable"); janet_panic("file not closable");
if (iof->flags & JANET_FILE_PIPED) { if (fclose(iof->file)) {
#ifndef JANET_NO_PROCESSES iof->flags |= JANET_FILE_NOT_CLOSEABLE;
int status = pclose(iof->file); janet_panic("could not close 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;
} }
iof->flags |= JANET_FILE_CLOSED;
return janet_wrap_nil(); 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/write", cfun_io_fwrite),
JANET_CORE_REG("file/flush", cfun_io_fflush), JANET_CORE_REG("file/flush", cfun_io_fflush),
JANET_CORE_REG("file/seek", cfun_io_fseek), 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_REG_END
}; };
janet_core_cfuns_ext(env, NULL, io_cfuns); janet_core_cfuns_ext(env, NULL, io_cfuns);

View File

@ -1972,7 +1972,6 @@ extern JANET_API const JanetAbstractType janet_file_type;
#define JANET_FILE_CLOSED 32 #define JANET_FILE_CLOSED 32
#define JANET_FILE_BINARY 64 #define JANET_FILE_BINARY 64
#define JANET_FILE_SERIALIZABLE 128 #define JANET_FILE_SERIALIZABLE 128
#define JANET_FILE_PIPED 256
#define JANET_FILE_NONIL 512 #define JANET_FILE_NONIL 512
JANET_API Janet janet_makefile(FILE *f, int32_t flags); JANET_API Janet janet_makefile(FILE *f, int32_t flags);