1
0
Fork 0
forked from fun/fun

Some more Tk fun. (0.37.18)

This commit is contained in:
Johannes Findeisen 2025-12-23 23:19:20 +01:00
commit 689a716499
13 changed files with 282 additions and 12 deletions

49
src/vm/os/list_dir.c Normal file
View file

@ -0,0 +1,49 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-23
*/
case OP_OS_LIST_DIR: {
/* pops path string; pushes array of strings */
Value pathv = pop_value(vm);
char *path = value_to_string_alloc(&pathv);
free_value(pathv);
Value arr = make_array_from_values(NULL, 0);
if (path) {
/*
* Using 'ls -1' as a fallback to avoid dirent.h conflicts on some systems.
* We escape the path minimally for the shell.
*/
size_t slen = strlen(path) + 16;
char *cmd = (char*)malloc(slen);
if (cmd) {
snprintf(cmd, slen, "ls -1 \"%s\"", path);
FILE *fp = popen(cmd, "r");
if (fp) {
char line[1024];
while (fgets(line, sizeof(line), fp)) {
/* Strip trailing newline */
size_t l = strlen(line);
if (l > 0 && line[l-1] == '\n') line[l-1] = '\0';
if (l > 1 && line[l-2] == '\r') line[l-2] = '\0';
if (line[0] != '\0') {
array_push(&arr, make_string(line));
}
}
pclose(fp);
}
free(cmd);
}
free(path);
}
push_value(vm, arr);
break;
}

54
src/vm/tk/bind.c Normal file
View file

@ -0,0 +1,54 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-23
*/
/* TK_BIND */
case OP_TK_BIND: {
/* stack: ..., id, event, command -> rc */
Value cmdv = pop_value(vm);
Value eventv = pop_value(vm);
Value idv = pop_value(vm);
char *cmd = value_to_string_alloc(&cmdv);
char *event = value_to_string_alloc(&eventv);
char *id = value_to_string_alloc(&idv);
free_value(cmdv);
free_value(eventv);
free_value(idv);
if (!id || !event || !cmd) {
if (id) free(id);
if (event) free(event);
if (cmd) free(cmd);
push_value(vm, make_int(-1));
break;
}
/*
* Construct: bind .id <event> {command}
* Note: for now, command is just raw Tcl as well,
* but could be extended to call Fun functions if we had a callback mechanism.
*/
size_t slen = strlen(id) + strlen(event) + strlen(cmd) + 32;
char *script = (char*)malloc(slen);
if (!script) {
free(id); free(event); free(cmd);
push_value(vm, make_int(-1));
break;
}
snprintf(script, slen, "bind .%s %s {%s}", id, event, cmd);
int rc = fun_tk_eval_script(script);
free(script);
free(id);
free(event);
free(cmd);
push_value(vm, make_int(rc));
break;
}