From b20b2e8d7f0cabe5f5ab16385b48476397991e5d Mon Sep 17 00:00:00 2001 From: Thomas Walker Lynch Date: Fri, 22 Nov 2024 12:15:35 +0800 Subject: [PATCH] cp assembling environment --- README.md | 5 +- developer/C/server.cli.c | 30 ++++ developer/C/server.lib.c | 102 ++++++++++++ developer/a.out | Bin 0 -> 21800 bytes .../document/RT C coding conventions.html | 153 ++++++++++++++++++ developer/tool/make | 6 + developer/tool/makefile | 18 +++ 7 files changed, 310 insertions(+), 4 deletions(-) create mode 100644 developer/C/server.cli.c create mode 100644 developer/C/server.lib.c create mode 100755 developer/a.out create mode 100644 developer/document/RT C coding conventions.html create mode 100755 developer/tool/make create mode 100644 developer/tool/makefile diff --git a/README.md b/README.md index e505925..c739da3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,3 @@ -# subu -subservient user tools -Create and run sub-users that share the desktop, but are limited to by unix permission to viewing their own files. +release directory has some resources that are shared among projects -see development/documents/subu.html for a more info. \ No newline at end of file diff --git a/developer/C/server.cli.c b/developer/C/server.cli.c new file mode 100644 index 0000000..78a56ee --- /dev/null +++ b/developer/C/server.cli.c @@ -0,0 +1,30 @@ +/* + * This command implements a simple Unix domain socket server. + * + * Usage: + * server + * + * Description: + * This program creates a Unix domain socket at the specified path + * (defined in `server.lib.c`) and listens for incoming connections. + * When a client sends data, the server logs it to a file and echoes + * a receipt. The server runs until interrupted or an error occurs. + * + * Command-line Options and Arguments: + * None: The program is self-contained and does not accept any + * command-line arguments. All configuration is handled within + * the implementation. + */ + +#define IFACE +#include +#include +#include "server.lib.c" + +int main(int argc, char *argv[]) { + (void)argc; // Suppress unused variable warnings + (void)argv; + + // Call the core server function + return Server·run(); +} diff --git a/developer/C/server.lib.c b/developer/C/server.lib.c new file mode 100644 index 0000000..144c3de --- /dev/null +++ b/developer/C/server.lib.c @@ -0,0 +1,102 @@ +#ifndef IFACE +#define Server·IMPLEMENTATION +#define IFACE +#endif + +#ifndef Server·IFACE +#define Server·IFACE + + // Necessary interface includes + #include + #include + #include + + // Interface prototypes + int Server·run(); + +#endif // Server·IFACE + +#ifdef Server·IMPLEMENTATION + + // Implementation-specific includes + #include + #include + #include + #include + #include + + // Constants + #define Server·SOCKET_PATH "mockup/subu_server_home/subu_server.sock" + #define Server·LOG_PATH "mockup/subu_server_home/server.log" + #define Server·BUFFER_SIZE 256 + + int Server·run() { + int server_fd, client_fd; + struct sockaddr_un address; + char buffer[Server·BUFFER_SIZE]; + FILE *log_file; + + // Open the log file + log_file = fopen(Server·LOG_PATH, "a"); + if (log_file == NULL) { + perror("Error opening log file"); + return EXIT_FAILURE; + } + + // Create the socket + if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + perror("Error creating socket"); + fclose(log_file); + return EXIT_FAILURE; + } + + // Configure socket address + memset(&address, 0, sizeof(address)); + address.sun_family = AF_UNIX; + strncpy(address.sun_path, Server·SOCKET_PATH, sizeof(address.sun_path) - 1); + + // Bind the socket + unlink(Server·SOCKET_PATH); // Remove existing file if present + if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) == -1) { + perror("Error binding socket"); + fclose(log_file); + close(server_fd); + return EXIT_FAILURE; + } + + // Listen for connections + if (listen(server_fd, 5) == -1) { + perror("Error listening on socket"); + fclose(log_file); + close(server_fd); + return EXIT_FAILURE; + } + + printf("Server running, waiting for connections...\n"); + + // Accept and handle client connections + while ((client_fd = accept(server_fd, NULL, NULL)) != -1) { + ssize_t bytes_read; + + memset(buffer, 0, Server·BUFFER_SIZE); + bytes_read = read(client_fd, buffer, Server·BUFFER_SIZE - 1); + if (bytes_read > 0) { + fprintf(log_file, "Received: %s\n", buffer); + fflush(log_file); + } else if (bytes_read == -1) { + perror("Error reading from client"); + } + + close(client_fd); + } + + // Clean up + perror("Error accepting connection"); + fclose(log_file); + close(server_fd); + unlink(Server·SOCKET_PATH); + + return EXIT_FAILURE; + } + +#endif // Server·IMPLEMENTATION diff --git a/developer/a.out b/developer/a.out new file mode 100755 index 0000000000000000000000000000000000000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q(lYwrllo5-2PR?2slTm^{%3oI{J%RC94L#G(ZA**p?{}C!GW@j z)OW`Jr>lhiS5(IkXtDju&W&aC@8}Z>XDGok`ou1dT~da?Kx@Wd^?P;w ktOm{DA}EaQSjPeTM~88TJGj@KS^u<4h5p?R1qaIh4|$cJUjP6A literal 0 HcmV?d00001 diff --git a/developer/document/RT C coding conventions.html b/developer/document/RT C coding conventions.html new file mode 100644 index 0000000..5c0005d --- /dev/null +++ b/developer/document/RT C coding conventions.html @@ -0,0 +1,153 @@ + + + + + + + RT C coding conventions + + + + +
+
+

Reasoning Technology (RT) C coding conventions

+

© 2024 Thomas Walker Lynch - All Rights Reserved.

+
+ +

Introduction

+ +

This document summarizes some of the coding conventions used in RT C projects. Discussed here are conventions for integrated header designs, ad hoc namespaces, and a structured approach to source file extensions. The document also outlines the associated build process using a standardized makefile.

+ +

Header file integration

+ +

RT C projects adopt an innovative approach by integrating headers directly into source files. This ensures consistency between interfaces and implementations, eliminating mismatches. Each file contains both an interface and an implementation section, gated by preprocessor directives.

+ +

Each RT C source file integrates its header directly into the source file. This locality makes header content easier to maintain as everything is found in a single file. It also eliminates the need to maintain two files for each module.

+ +

Each file has two sections

+
    +
  • Interface section: Contains declarations, macros, and #includes needed for the interface. Ensures consistency by defining the interface exactly once, even when the file is included multiple times.
  • +
  • Implementation section: Contains function definitions and additional includes needed for the implementation. This section is compiled only when the file is used as an implementation.
  • +
+ +

Each section is turned on and off with the CPP macro IFACE.

+ +

Example

+

+// If not an IFACE, then an IMPLEMENTATION
+#ifndef IFACE
+  #define MyModule·IMPLEMENTATION
+  // Ensures included files are processed for their interfaces.
+  #define IFACE
+#endif
+
+// Define the interface exactly once.
+#ifndef MyModule·IFACE
+#define MyModule·IFACE
+  // Interface-only includes go here.
+  void MyModule·function();
+#endif
+
+#ifdef MyModule·IMPLEMENTATION
+  // Additional includes for implementation go here.
+  #include 
+  void MyModule·function() {
+    printf("Hello, World!\n");
+  }
+#endif
+  
+ +

Explanation

+

The example above demonstrates the structure and purpose of each block:

+

First block: Ensures that the file operates correctly based on the value of IFACE. If IFACE is undefined, it defines MyModule·IMPLEMENTATION to enable the implementation section and sets IFACE to ensure subsequent includes process interface sections.

+

Second block: Defines the interface, including declarations and interface-specific includes. The #ifndef MyModule·IFACE macro ensures the interface is defined exactly once, regardless of how many times the file is included.

+

Third block: Contains implementation-specific includes and function definitions. Guarded by MyModule·IMPLEMENTATION, it is only included when compiling the implementation.

+

Interface includes are placed in the interface block, ensuring they are available wherever the interface is used. Implementation includes are isolated in the implementation block, minimizing unnecessary dependencies in other files.

+ +

Namespace conventions

+

RT projects use ad hoc namespaces to maintain clarity and prevent naming conflicts. This is achieved by prefixing exported identifiers with a module-specific name followed by the · (cdot) character.

+ +

Conventions

+
    +
  • Prefix: The module name serves as the prefix, ensuring all identifiers are unique across the program.
  • +
  • Separator: The · character visually separates the prefix from the identifier name, maintaining readability and avoiding conflicts.
  • +
+ +

Example

+

+void Server·run();
+  
+ +

Source file extensions

+

RT projects use standardized extensions to distinguish between library and command-line interface (CLI) source files:

+
    +
  • .lib.c: Files implementing library functions.
  • +
  • .cli.c: Files implementing command-line tools.
  • +
+ +

The .lib.c files compile into libraries, while .cli.c files compile into standalone executables. The makefile processes these files automatically, ensuring a clear separation of functionality.

+ +

Build process

+

The build process follows these steps:

+
    +
  1. Dependency generation: Run make dependency to create dependencies. This step is only required when the dependency structure changes.
  2. +
  3. Compilation: Run make cli to compile CLI sources and link them against the library. The makefile automatically manages targets and dependencies.
  4. +
+ +

Benefits

+
    +
  • Consistency: Integrated headers ensure interface and implementation are always in sync.
  • +
  • Modularity: Each file encapsulates its interface and implementation, reducing coupling.
  • +
  • Clarity: Ad hoc namespaces and standardized extensions improve readability and organization.
  • +
  • Efficiency: The makefile automates builds, minimizing errors and streamlining development.
  • +
+ +

Conclusion

+

This document outlines the conventions and practices for writing and building RT C projects. By integrating headers, adopting namespaces, and standardizing extensions, RT ensures its projects are robust, modular, and easy to maintain.

+
+ + diff --git a/developer/tool/make b/developer/tool/make new file mode 100755 index 0000000..5ab34eb --- /dev/null +++ b/developer/tool/make @@ -0,0 +1,6 @@ +#!/bin/env /bin/bash + +set -e +cd ${REPO_HOME}/developer +/bin/make -f tool/makefile $@ + diff --git a/developer/tool/makefile b/developer/tool/makefile new file mode 100644 index 0000000..e2af188 --- /dev/null +++ b/developer/tool/makefile @@ -0,0 +1,18 @@ +# /bin/make must be run from $REPO_HOME/developer + +RESOURCE := $(REPO_HOME)/tool_shared/third_party/resource/release/make +include $(RESOURCE)/makefile-environment + +# overriding defaults +EXECDIR=mockup/subu_server_home +LIBDIR=scratchpad + +# assign a compiler to use +C=gcc +CFLAGS= -include "$(RESOURCE)/environment.h" -std=gnu18 -Werror -O0 -ggdb -DDEBUG -DDEBUGDB -I$(INCDIR) +LINKFLAGS=-L$(LIBDIR) -L/usr/lib -L/usr/lib64 -l$(PROJECT) + +# Import the rules. The dash prefix means to ignore include errors. This is +# required because the DEPFILE might not exist yet. +-include $(DEPFILE) +include $(RESOURCE)/makefile -- 2.20.1