Hacker News new | ask | show | jobs
by keepamovin 1067 days ago
This is cool. I asked ChatGPT4 to explain to me what it does, and it gives a useful overview:

This is a minimal Gemini protocol client written in C. The Gemini protocol is an application-level internet protocol for serving hypertext documents over secure connections.

The code includes various standard C libraries and the OpenSSL library for secure connections. The `WARN` macro definition is used to print warning messages to `stderr` and jump to the `start:` label.

In the `main` function, it defines variables and initializes certain structs, like `struct sockaddr_in addr` that is used for internet socket addresses. It then opens a connection with the Gemini server on port 1965 (the standard port for the Gemini protocol). The connection is encrypted using the OpenSSL library functions. It also opens two file streams, one for history (`.gmi100`) and another for a temporary file. The file names and locations are generated by the `tmpnam()` function.

The main loop reads commands from `stdin`. Depending on the command, it can execute system commands (when the input starts with '!'), navigate through the history (when input starts with a digit), process a variety of single-letter commands (like 'q' for quit, 'r' for refresh, 'c' to print current URI, 'u' for up a level, 'b' for back in history), or interpret the input as a URL or a part of a URL to be navigated to.

For URL navigation, it creates a TCP socket connection with the Gemini server, sets up an SSL connection over that socket, and sends a request to the server for the resource identified by the URL. The client handles the server response depending on the status code. For example, if the status code is '1' it's a request for input and if it's '3' it's a redirect. It prints the response to `stdout` and also stores it in the temporary file.

For `text/*` type responses, it additionally parses any lines that start with '=>' as links and assigns them a number, storing this in the temporary file for later navigation. If the line starts with '=>' but doesn't have a description, it goes back to reading the rest of the description.

After processing the server response, it uses the `less` command to display the content of the temporary file, allowing the user to scroll through it if it's longer than the screen. Before each new iteration of the main loop, it flushes the temporary file and goes back to the `start:` label. If the user quits, the function returns 0 and the program terminates.

Overall, this program is a command-line based Gemini client that allows a user to navigate the geminispace, issue queries, and view text documents served over the Gemini protocol. It also keeps a history of visited URIs in the `.gmi100` file.

source: https://sharegpt.com/c/xscNWg6

1 comments

Wow, this is actually very good description of program flow. There are only few small mistakes. For example:

> navigate through the history (when input starts with a digit)

Should be more like: navigate to link from currently displayed page (when input starts with a digit).

> It prints the response to `stdout` and also stores it in the temporary file.

Response is not printed to `stdout` at all. It is only stored in temporary file.

Also the possibility to use first program argument to overwrite the `less` command was ignored.

But overall very good job.