; nasmw -f obj this_file.asm
; bcc32 file.c this.obj
section _DATA public align=4 class=DATA use32
extern _isspace
global _popola
; global _main
section _TEXT public align=1 class=CODE use32
; int popola(char** v, char* buf, int limit)
; s=0j, 4i, 8b, 12ra, 16@v, 20@buf, 24@limit
_popola:
push ebx
push esi
push edi
%define @v esp+16
%define @buf esp+20
%define @limit esp+24
xor edi, edi
cmp dword[@v], 0
je .fn
cmp dword[@buf], 0
je .fn
cmp dword[@limit], 0
jle .fn
mov esi, [@buf]
xor ebx, ebx
..a0:
mov bl, [esi]
push ebx
call _isspace
add esp, 4
cmp eax, 0
je .a1
inc esi
jmp short .a0
..a1:
cmp ebx, 0
je .fn
cmp edi, [@limit]
jne .a2
inc edi
jmp short .fn
..a2:
mov eax, [@v]
mov dword[eax+4*edi], esi
inc edi
..a3:
mov bl, [esi]
cmp ebx, 0
je .a4
push ebx
call _isspace
add esp, 4
cmp eax, 0
jne .a4
inc esi
jmp short .a3
..a4:
cmp ebx, 0
je .fn
mov byte[esi], 0
inc esi
jmp short .a0
..fn:
mov eax, edi
%undef @v
%undef @buf
%undef @limit
pop edi
pop esi
pop ebx
ret
---------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define UC unsigned char
#define U unsigned
static const char *pc = "CLI[\"vexit\" to end]> ";
struct you{
char* name;
char* pointer;
unsigned npar;
struct you* prev;
};
struct you *last=0;
int popola(char** v, char* buf, int limit);
int
inserisci(const char* name, char* pointer, U par)
{struct you *tm;
char *tmp;
unsigned sz;
///////////////////
if( name==0 || (sz=strlen(name))==0 )
return 0;
if( (tm =malloc(sizeof(struct you)))==0 )
return 0;
if( (tmp=malloc(sizeof(sz+2)))==0 )
{free(tm); return 0;}
tm->name=tmp; strcpy(tm->name, name);
tm->pointer= pointer; tm->npar=par;
tm->prev=last; last=tm;
return 1;
}
void free_list(void)
{struct you *p=last, *v;
while(1) {if(p==0) break;
v=p; p=p->prev;
free(v->name); free(v);
}
}
int show(void){puts("cliShow() stub");return 0;}
int version(void){puts("cliVersion() stub");return 0;}
int help(void){puts("cliHelp() stub");return 0;}
int port(void){puts("cliPort() stub"); return 0;}
void vexit(void)
{puts("cliExit() stub");
free_list();
exit(0);
}
void add(char* a1, char* a2)
{int aa1, aa2;
sscanf(a1, "%d", &aa1);
sscanf(a2, "%d", &aa2);
printf("%d\n", aa1+aa2);
}
void sub(char* a1, char* a2)
{int aa1, aa2;
sscanf(a1, "%d", &aa1);
sscanf(a2, "%d", &aa2);
printf("%d\n", aa1-aa2);
}
int main(void)
{char buf[BUFSIZ] = {0}, *a[32];
struct you *p;
int cv;
/////////////////////////////////
inserisci("show",(char*) show, 0);
inserisci("version", (char*) version, 0);
inserisci("help", (char*) help, 0);
inserisci("port", (char*) port, 0);
inserisci("vexit",(char*) vexit, 0);
inserisci("add",(char*) add, 2);
inserisci("sub",(char*) sub, 2);
la:;
while (1)
{printf("%s", pc); fflush(stdout);
if( fgets(buf, BUFSIZ, stdin)!=0 )
{cv=popola(a, buf, 7);
if(cv<=0) goto la;
else --cv;
for(p=last; p ;p=p->prev)
if(strcmp(p->name, a[0])==0)
{if(p->npar!=(U)cv) goto la;
switch(p->npar)
{case 0:
( (void (*)(void))(p->pointer) )( );
break;
case 1:
( (void (*)(char*))(p->pointer) )(a[1]);
break;
case 2:
( (void (*)(char*, char*))(p->pointer) )
(a[1], a[2]);
break;
case 3:
( (void (*)(char*, char*, char*))(p->pointer) )
(a[1], a[2], a[3]);
break;
case 4:
( (void (*)(char*, char*, char*, char*) )
(p->pointer) ) (a[1], a[2], a[3], a[4]);
break;
case 5:
( (void (*)(char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5]);
break;
case 6:
( (void (*)(char*, char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5], a[6]);
break;
case 7:
( (void (*)(char*, char*, char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
break;
default:
goto la;
}
}
}
}
return 0;
}