commit c36d43accf9b471d6cd0181c8c267b0d5352f6be from: Omar Polo date: Sun Apr 25 15:49:45 2021 UTC hash.c -> tofu.c commit - 3768e50f4730c0d2af1c97de6ee1c0f2677cb42e commit + c36d43accf9b471d6cd0181c8c267b0d5352f6be blob - 2dd32565543182a778285768f1903af4d9fce49b blob + 85ccf02e686af15e1ce5edde796fec0b0f96d284 --- Makefile.am +++ Makefile.am @@ -7,7 +7,6 @@ telescope_SOURCES = cmd.gen.h \ gemini.c \ gemtext.c \ gencmd.awk \ - hash.c \ hist.c \ keymap.c \ mime.c \ @@ -17,6 +16,7 @@ telescope_SOURCES = cmd.gen.h \ telescope.c \ telescope.h \ textplain.c \ + tofu.c \ ui.c \ utf8.c \ util.c \ blob - 1814b4ea5d3302c0cbd2f818d003bc8c72d64710 (mode 644) blob + /dev/null --- hash.c +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2021 Omar Polo - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include "telescope.h" - -#include -#include - -static void *hash_alloc(size_t, void*); -static void *hash_calloc(size_t, size_t, void*); -static void hash_free(void*, void*); - -static void * -hash_alloc(size_t len, void *d) -{ - if ((d = malloc(len)) == NULL) - abort(); - return d; -} - -static void * -hash_calloc(size_t nmemb, size_t size, void *d) -{ - if ((d = calloc(nmemb, size)) == NULL) - abort(); - return d; -} - -static void -hash_free(void *ptr, void *d) -{ - free(ptr); -} - -void -tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko) -{ - struct ohash_info info = { - .key_offset = ko, - .calloc = hash_calloc, - .free = hash_free, - .alloc = hash_alloc, - }; - - ohash_init(h, sz, &info); -} - -struct tofu_entry * -tofu_lookup(struct ohash *h, const char *domain, const char *port) -{ - char buf[GEMINI_URL_LEN]; - unsigned int slot; - - strlcpy(buf, domain, sizeof(buf)); - if (port != NULL && *port != '\0' && strcmp(port, "1965")) { - strlcat(buf, ":", sizeof(buf)); - strlcat(buf, port, sizeof(buf)); - } - - slot = ohash_qlookup(h, buf); - return ohash_find(h, slot); -} - -void -tofu_add(struct ohash *h, struct tofu_entry *e) -{ - unsigned int slot; - - slot = ohash_qlookup(h, e->domain); - ohash_insert(h, slot, e); -} blob - 87197992d46f7e14a98023462a0f530f600c2aa8 blob + d972e44d1820f75c484a8f39a511b7e3e3997bf0 --- telescope.h +++ telescope.h @@ -200,11 +200,6 @@ int client_main(struct imsgbuf*); /* gemtext.c */ void gemtext_initparser(struct parser*); - -/* hash.c */ -void tofu_init(struct ohash*, unsigned int, ptrdiff_t); -struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*); -void tofu_add(struct ohash*, struct tofu_entry*); /* hist.c */ void hist_clear_forward(struct histhead*, struct hist*); @@ -250,6 +245,11 @@ void save_session(void); /* textplain.c */ void textplain_initparser(struct parser*); +/* tofu.c */ +void tofu_init(struct ohash*, unsigned int, ptrdiff_t); +struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*); +void tofu_add(struct ohash*, struct tofu_entry*); + /* ui.c */ unsigned int tab_new_id(void); int ui_init(int, char * const*); blob - /dev/null blob + 1814b4ea5d3302c0cbd2f818d003bc8c72d64710 (mode 644) --- /dev/null +++ tofu.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2021 Omar Polo + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "telescope.h" + +#include +#include + +static void *hash_alloc(size_t, void*); +static void *hash_calloc(size_t, size_t, void*); +static void hash_free(void*, void*); + +static void * +hash_alloc(size_t len, void *d) +{ + if ((d = malloc(len)) == NULL) + abort(); + return d; +} + +static void * +hash_calloc(size_t nmemb, size_t size, void *d) +{ + if ((d = calloc(nmemb, size)) == NULL) + abort(); + return d; +} + +static void +hash_free(void *ptr, void *d) +{ + free(ptr); +} + +void +tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko) +{ + struct ohash_info info = { + .key_offset = ko, + .calloc = hash_calloc, + .free = hash_free, + .alloc = hash_alloc, + }; + + ohash_init(h, sz, &info); +} + +struct tofu_entry * +tofu_lookup(struct ohash *h, const char *domain, const char *port) +{ + char buf[GEMINI_URL_LEN]; + unsigned int slot; + + strlcpy(buf, domain, sizeof(buf)); + if (port != NULL && *port != '\0' && strcmp(port, "1965")) { + strlcat(buf, ":", sizeof(buf)); + strlcat(buf, port, sizeof(buf)); + } + + slot = ohash_qlookup(h, buf); + return ohash_find(h, slot); +} + +void +tofu_add(struct ohash *h, struct tofu_entry *e) +{ + unsigned int slot; + + slot = ohash_qlookup(h, e->domain); + ohash_insert(h, slot, e); +}