hirc

IRC client
Log | Files | Refs

commit a9e8d13cfc199a7681672cdf0e0918e8b6309ebd
parent 3849ae5de713440e0381e17ac4b08179fe6a0054
Author: hhvn <dev@hhvn.uk>
Date:   Thu, 21 Apr 2022 20:41:36 +0100

Move param functions into own file

Diffstat:
MMakefile | 2+-
Msrc/hirc.h | 9+++++----
Msrc/main.c | 52----------------------------------------------------
Asrc/params.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 79 insertions(+), 57 deletions(-)

diff --git a/Makefile b/Makefile @@ -21,7 +21,7 @@ BIN = hirc SRC = src/main.c src/mem.c src/handle.c src/hist.c \ src/nick.c src/chan.c src/serv.c src/ui.c \ src/format.c src/complete.c src/commands.c \ - src/config.c src/str.c + src/config.c src/str.c src/params.c OBJ = $(SRC:.c=.o) MAN = doc/hirc.1 COMMIT = $(shell git log HEAD...HEAD~1 --pretty=format:%h) diff --git a/src/hirc.h b/src/hirc.h @@ -42,14 +42,15 @@ size_t wcslcpy(wchar_t *, const wchar_t *, size_t); /* main.c */ void cleanup(char *quitmsg); -void param_free(char **params); -int param_len(char **params); -char ** param_create(char *msg); -char ** param_dup(char **p); int read_line(int fd, char *buf, size_t buf_len); int ircgets(struct Server *server, char *buf, size_t buf_len); int ircprintf(struct Server *server, char *format, ...); +/* params.c */ +void param_free(char **params); +int param_len(char **params); +char ** param_create(char *msg); + /* str.c */ char * homepath(char *path); int strcmp_n(const char *s1, const char *s2); diff --git a/src/main.c b/src/main.c @@ -49,58 +49,6 @@ cleanup(char *quitmsg) { ui_deinit(); } -void -param_free(char **params) { - char **p; - - for (p = params; p && *p; p++) - pfree(&*p); - pfree(&params); -} - -int -param_len(char **params) { - int i; - - for (i=0; params && *params; i++, params++); - return i; -} - -char ** -param_create(char *msg) { - char **ret, **rp; - char *params[PARAM_MAX]; - char tmp[2048]; - char *p, *cur; - int final = 0, i; - - memset(params, 0, sizeof(params)); - strlcpy(tmp, msg, sizeof(tmp)); - - for (p=cur=tmp, i=0; p && *p && i < PARAM_MAX; p++) { - if (!final && *p == ':' && *(p-1) == ' ') { - final = 1; - *(p-1) = '\0'; - params[i++] = cur; - cur = p + 1; - } - if (!final && *p == ' ' && *(p+1) != ':') { - *p = '\0'; - params[i++] = cur; - cur = p + 1; - } - } - *p = '\0'; - params[i] = cur; - - ret = emalloc(sizeof(params)); - for (rp=ret, i=0; params[i]; i++, rp++) - *rp = estrdup(params[i]); - *rp = NULL; - - return ret; -} - int read_line(int fd, char *buf, size_t buf_len) { size_t i = 0; diff --git a/src/params.c b/src/params.c @@ -0,0 +1,73 @@ +/* + * src/params.c from hirc + * + * Copyright (c) 2021-2022 hhvn <dev@hhvn.uk> + * + * 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 <string.h> +#include "hirc.h" + +void +param_free(char **params) { + char **p; + + for (p = params; p && *p; p++) + pfree(&*p); + pfree(&params); +} + +int +param_len(char **params) { + int i; + + for (i=0; params && *params; i++, params++); + return i; +} + +char ** +param_create(char *msg) { + char **ret, **rp; + char *params[PARAM_MAX]; + char tmp[2048]; + char *p, *cur; + int final = 0, i; + + memset(params, 0, sizeof(params)); + strlcpy(tmp, msg, sizeof(tmp)); + + for (p=cur=tmp, i=0; p && *p && i < PARAM_MAX; p++) { + if (!final && *p == ':' && *(p-1) == ' ') { + final = 1; + *(p-1) = '\0'; + params[i++] = cur; + cur = p + 1; + } + if (!final && *p == ' ' && *(p+1) != ':') { + *p = '\0'; + params[i++] = cur; + cur = p + 1; + } + } + *p = '\0'; + params[i] = cur; + + ret = emalloc(sizeof(params)); + for (rp=ret, i=0; params[i]; i++, rp++) + *rp = estrdup(params[i]); + *rp = NULL; + + return ret; +}