diff --git a/CMakeLists.txt b/CMakeLists.txt index d60ed03..a395cc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.16) -project(fun VERSION 0.21.0 LANGUAGES C) +project(fun VERSION 0.21.1 LANGUAGES C) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/examples/tcp_http_get_class.fun b/examples/tcp_http_get_class.fun index 9cfd10b..b40c63d 100755 --- a/examples/tcp_http_get_class.fun +++ b/examples/tcp_http_get_class.fun @@ -1,3 +1,5 @@ +#!/usr/bin/env fun + /* * This file is part of the Fun programming language. * https://hanez.org/project/fun/ diff --git a/examples/try_catch_finally.fun b/examples/try_catch_finally.fun index 37f53ba..1369a5b 100755 --- a/examples/try_catch_finally.fun +++ b/examples/try_catch_finally.fun @@ -1,3 +1,5 @@ +#!/usr/bin/env fun + /* * This file is part of the Fun programming language. * https://hanez.org/project/fun/ diff --git a/examples/try_catch_with_error.fun b/examples/try_catch_with_error.fun index 0cf05be..5e289aa 100755 --- a/examples/try_catch_with_error.fun +++ b/examples/try_catch_with_error.fun @@ -1,3 +1,5 @@ +#!/usr/bin/env fun + /* * This file is part of the Fun programming language. * https://hanez.org/project/fun/ diff --git a/examples/unix_socket_echo.fun b/examples/unix_socket_echo.fun new file mode 100755 index 0000000..a95d30e --- /dev/null +++ b/examples/unix_socket_echo.fun @@ -0,0 +1,68 @@ +#!/usr/bin/env fun + +/** + * This file is part of the Fun programming language. + * https://hanez.org/project/fun/ + * + * Copyright 2025 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2025-10-04 + */ + +/* + * UNIX domain socket echo demo. + * - On Unix-like systems: demonstrates unix_listen + tcp_accept + unix_connect + sock_send/recv. + * - On Windows: prints a friendly message and exits (since AF_UNIX is not available here). + */ + +fun server_thread(listen_fd) + // Accept a single client, read message, echo back, close. + cfd = tcp_accept(to_number(listen_fd)) + if (cfd <= 0) + // accept failed + return 0 + msg = sock_recv(cfd, 4096) + // Simple echo prefix + _ = sock_send(cfd, join(["echo:", msg], "")) + _ = sock_close(cfd) + return 1 + +// Cross-platform guard: Windows sets OS=Windows_NT +os = env("OS") +if (os == "Windows_NT") + print("UNIX domain sockets are not supported on Windows in this build. Skipping demo.") +else + // Path for the UNIX socket + path = "/tmp/fun_unix_echo.sock" + + // Start a UNIX listening socket + lfd = unix_listen(path, 1) + if (lfd <= 0) + print(join(["unix_listen failed at ", path], "")) + else + // Spawn the server thread which will accept exactly one client + tid = thread_spawn(server_thread, lfd) + + // Small delay to ensure the server is accepting + sleep(50) + + // Create a client using raw built-ins (no stdlib dependency) + cfd = unix_connect(path) + if (cfd <= 0) + print("unix_connect failed") + else + // Send a message and read the reply + _ = sock_send(cfd, "hello over AF_UNIX") + reply = sock_recv(cfd, 4096) + print(reply) + _ = sock_close(cfd) + + // Join the server thread and close the listening socket + _ = thread_join(tid) + _ = sock_close(lfd) + +/* Expected output: +echo:hello over AF_UNIX +*/