ref: 7bc4d4f57bc35956c69039e92ebef1737f973e95
parent: 587970db0cef17219e895e5870d7e900166a76de
author: Julien Blanchard <julien@typed-hole.org>
date: Sun Jun 21 10:02:24 EDT 2020
Add a Query type
--- a/toe.c
+++ b/toe.c
@@ -2,8 +2,15 @@
#include <libc.h>
#include <bio.h>
+typedef struct Query
+{
+ char *domain;
+ char *user;
+} Query;
+
+
void
-visit(char *domain, char *user)
+toe(Query *query)
{
char *naddr;
char *line;
@@ -10,19 +17,19 @@
int fd;
Biobuf out, body;
- naddr = netmkaddr(domain, "tcp", "79");
+ naddr = netmkaddr(query->domain, "tcp", "79");
fd = dial(naddr, 0, 0, 0);
if(fd < 0){
- print("unable to connect to %s: %r", domain);
+ print("unable to connect to %s: %r", query->domain);
return;
}
- if (user == nil) {
+ if (query->user == nil) {
fprint(fd, "\r\n");
} else {
- fprint(fd, "%s\r\n", user);
+ fprint(fd, "%s\r\n", query->user);
}
-
+
Binit(&body, fd, OREAD);
Binit(&out, 1, OWRITE);
@@ -30,38 +37,47 @@
Bprint(&out, "%s", line);
free(line);
}
-
+
Bflush(&out);
Bflush(&body);
close(fd);
+ free(query);
}
+Query *
+mk_query(char *query_str)
+{
+ char *user, *domain;
+ Query *new_query = malloc(sizeof(Query));
+
+ if (query_str[0] == '@') {
+ user = nil;
+ domain = query_str + 1;
+ } else if (strstr(query_str, "@") == nil) {
+ user = query_str;
+ domain = "localhost";
+ } else {
+ user = strtok(query_str, "@");
+ domain = strtok(nil, "@");
+ }
+
+ new_query->user = user;
+ new_query->domain = domain;
+
+ return new_query;
+}
+
void
main(int argc, char **argv)
{
- char *user, *domain, *query;
-
if( argc == 2 ) {
- query = argv[1];
+ Query *query = mk_query(argv[1]);
- if (query[0] == '@') {
- user = nil;
- domain = query + 1;
- } else if (strstr(argv[1], "@") == nil) {
- user = argv[1];
- domain = "localhost";
- } else {
- user = strtok(argv[1], "@");
- domain = strtok(nil, "@");
- }
-
- visit(domain, user);
+ toe(query);
exits(0);
- }
- else if( argc > 2 ) {
- print("Too many arguments supplied.\n");
- }
- else {
- print("Usage: toe [user]@server.\n");
- }
+ } else if( argc > 2 ) {
+ print("Too many arguments supplied.\n");
+ } else {
+ print("Usage: toe [user]@server.\n");
+ }
}