parse_range

This commit is contained in:
joonicks 2025-11-20 14:45:38 +01:00
parent d7176f0a27
commit e1742475e7

View File

@ -1,7 +1,7 @@
/* /*
EnergyMech, IRC bot software EnergyMech, IRC bot software
Copyright (c) 1997-2020 proton Copyright (c) 1997-2025 proton
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -32,22 +32,20 @@
#include "mcmd.h" #include "mcmd.h"
#ifdef TEST #ifdef TEST
#include "debug.c" #include "debug.c"
char result[MSGLEN]; char result[MSGLEN];
char *input = "alias one two three four five six seven eight nine ten"; char *alias_test_input = "alias one two three four five -6- seven eight nine ten";
void *Calloc(int size) void run_debug(void) {}
{
return(calloc(1,size));
}
void testcase(const char *test, const char *expect) void testcase(const char *test, const char *expect)
{ {
afmt(result,test,input); afmt(result,test,alias_test_input);
if (strcmp(result,expect) == 0) debug("testcase SUCCESS: test \"%s\" -> got \"%s\"\n",test,expect); if (strcmp(result,expect) == 0)
else debug("testcase FAIL: test \"%s\", expected \"%s\", got \"%s\"\n",test,expect,result); debug("testcase SUCCESS: test \"%s\" -> got \"%s\"\n",test,expect);
else
debug("testcase FAIL: test \"%s\", expected \"%s\", got \"%s\"\n",test,expect,result);
} }
int main(int argc, char **argv, char **envp) int main(int argc, char **argv, char **envp)
@ -72,14 +70,16 @@ int main(int argc, char **argv, char **envp)
testcase("cmd $2 $1","cmd two one"); testcase("cmd $2 $1","cmd two one");
testcase("cmd $8-","cmd eight nine ten"); testcase("cmd $8-","cmd eight nine ten");
testcase("cmd $4-5","cmd four five"); testcase("cmd $4-5","cmd four five");
testcase("cmd $5-6","cmd five six"); testcase("cmd $5-6","cmd five -6-");
testcase("cmd $5-7","cmd five -6- seven");
testcase("cmd $~","cmd noob"); testcase("cmd $~","cmd noob");
testcase("cmd $~1234567890","cmd noob1234567890");
testcase("cmd $one $two","cmd $one $two"); testcase("cmd $one $two","cmd $one $two");
exit(0); exit(0);
} }
debug("input = %s\n",input); debug("input = %s\n",alias_test_input);
debug("format = %s\n",format); debug("format = %s\n",format);
afmt(result,format,input); afmt(result,format,alias_test_input);
debug("result = %s\n",result); debug("result = %s\n",result);
exit(0); exit(0);
} }
@ -90,46 +90,37 @@ int main(int argc, char **argv, char **envp)
* copy_to = buffer to put resulting new command into * copy_to = buffer to put resulting new command into
* src = Alias format string * src = Alias format string
* input = input from user * input = input from user
* size: 0x159
* size: 0x163
*/ */
void afmt(char *copy_to, const char *src, const char *input) #define OUTPUTEND (output+MSGLEN-1) /* avoid buffer overflows */
{ #define startnum n[0]
#define BUFTAIL (copy_to+MSGLEN-1) /* avoid buffer overflows */ #define endnum n[1]
const char *argstart,*argend;
char *dest; void afmt(char *output, const char *src, const char *input)
int startnum,endnum,spc; {
const char *argstart,*argend;
int spc,n[3];
dest = copy_to;
while(*src) while(*src)
{ {
if (src[0] == '$' && src[1] == '$') if (*src == '$' && src[1] == '$')
src++; src++;
else else
if (*src == '$' && src[1] == '~') if (*src == '$' && src[1] == '~')
{ {
src += 2; src += 2;
argstart = CurrentNick; argstart = CurrentNick;
while(*argstart && dest <= BUFTAIL) while(*argstart && output <= OUTPUTEND)
*(dest++) = *(argstart++); *(output++) = *(argstart++);
} }
else else
if (*src == '$' && (attrtab[(uchar)src[1]] & NUM) == NUM) if (*src == '$' && (src[1] >= '0' && src[1] <= '9'))
{ {
src++; src++;
startnum = endnum = 0; parse_range((char**)&src,n);
while(attrtab[(uchar)*src] & NUM) if (n[2] && n[1] == -1)
startnum = (startnum * 10) + (*(src++) - '0'); endnum = 9999;
if (*src == ' ' || *src == 0)
endnum = startnum;
else
if (*src == '-')
{
src++;
if ((attrtab[(uchar)*src] & NUM) != NUM)
endnum = 9999;
else
while(attrtab[(uchar)*src] & NUM)
endnum = (endnum * 10) + (*src++ - '0');
}
argstart = input; argstart = input;
if (startnum) if (startnum)
@ -154,25 +145,15 @@ void afmt(char *copy_to, const char *src, const char *input)
argend++; argend++;
} }
} }
#ifdef DEBUG while(*argstart && argstart < argend && output <= OUTPUTEND)
#ifndef TEST *(output++) = *(argstart++);
debug("(afmt) args #%i-#%i, characters %i-%i\n",startnum,endnum,argstart-input,argend-input);
#endif /* ifndef TEST */
#endif /* DEBUG */
while(*argstart && argstart < argend && dest <= BUFTAIL)
*(dest++) = *(argstart++);
continue; continue;
} }
if (dest >= BUFTAIL) if (output >= OUTPUTEND)
break; break;
*(dest++) = *(src++); *(output++) = *(src++);
} }
*dest = 0; *output = 0;
#ifdef DEBUG
#ifndef TEST
debug("(afmt) start %i end %i spc %i\n",startnum,endnum,spc);
#endif /* ifndef TEST */
#endif /* DEBUG */
} }
#ifndef TEST #ifndef TEST