params.c (1708B)
1 /* 2 * src/params.c from hirc 3 * 4 * Copyright (c) 2021-2022 hhvn <dev@hhvn.uk> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * 18 */ 19 20 #include <string.h> 21 #include "hirc.h" 22 23 void 24 param_free(char **params) { 25 char **p; 26 27 for (p = params; p && *p; p++) 28 pfree(&*p); 29 pfree(¶ms); 30 } 31 32 int 33 param_len(char **params) { 34 int i; 35 36 for (i=0; params && *params; i++, params++); 37 return i; 38 } 39 40 char ** 41 param_create(char *msg) { 42 char **ret, **rp; 43 char *params[PARAM_MAX]; 44 char tmp[2048]; 45 char *p, *cur; 46 int final = 0, i; 47 48 memset(params, 0, sizeof(params)); 49 strlcpy(tmp, msg, sizeof(tmp)); 50 51 for (p=cur=tmp, i=0; p && *p && i < PARAM_MAX; p++) { 52 if (!final && *p == ':' && *(p-1) == ' ') { 53 final = 1; 54 *(p-1) = '\0'; 55 params[i++] = cur; 56 cur = p + 1; 57 } 58 if (!final && *p == ' ' && *(p+1) != ':') { 59 *p = '\0'; 60 params[i++] = cur; 61 cur = p + 1; 62 } 63 } 64 *p = '\0'; 65 params[i] = cur; 66 67 ret = emalloc(sizeof(params)); 68 for (rp=ret, i=0; params[i]; i++, rp++) 69 *rp = estrdup(params[i]); 70 *rp = NULL; 71 72 return ret; 73 }