473,701 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Command Line Interface (CLI): your recommendations

Hello, All!

I'm implementing simple CLI (flat model, no tree-style menu etc.). Command
line looks like this: <command> <param1> <param2> ... <paramN> (where
N=1..4)
And idea is pretty simple:

1) get whole string of input line
2) preset table of strings matching <command>
3) preset table of function calls
4) scan <command> table for string, if match - call matching function (I
declare ALL callback functions as having the same format)

Here is code implementing part of this algorithm. The problem I came across
is if pressing 'Enter' continuously then sometimes output of PROMPT turnes
out clumsy, i.e. it's printed repeatedly in a line.

Before I went too far, I would like to ask your opinion about concept I'm
using and recommendations if possible:

#define _GNU_SOURCE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define COUNT(a) (sizeof(a) / sizeof(a[0]))

const char *PROMPT = "CLI>";

enum errCodes { RC_SUCCESS = 0, RC_ERROR = -1 };

/* table of <commands> */
const char *commands[] = {
"show",
"version",
"help",
"port",
"exit"
};

/* callback function */
typedef int (*handler_ptr_t )(int, char**);

/* define command line: <command> <param1> <param2> ... <paramN> */
typedef struct cmdLineEntry_s {
#define MAX_NAME 20
char command[MAX_NAME];
#define MAX_ARGS 4
char params[MAX_ARGS];
unsigned int params_num;
handler_ptr_t cmd_handler;
} cmdLineEntry_t;

int cliShow(int argc, char *argv[])
{
puts("cliShow() stub");
return 0;
}

int cliVersion(int argc, char *argv[])
{
puts("cliVersio n() stub");
return 0;
}

int cliHelp(int argc, char *argv[])
{
puts("cliHelp() stub");
return 0;
}

int cliPort(int argc, char *argv[])
{
puts("cliPort() stub");
return 0;
}

int cliExit(int argc, char *argv[])
{
puts("cliExit() stub");
exit(EXIT_SUCCE SS);
}

/* define table functions pointers */
handler_ptr_t ftable[] = { cliShow, cliVersion, cliHelp, cliPort, cliExit };

/* parse command line and fill structure */
static int
cliParseCommand Line(char *buf, cmdLineEntry_t *cli)
{
const char delim[] = " \t\n";
unsigned int i;
char *token;

memset(cli, 0, sizeof *cli);

token = strtok(buf, delim);
for (i = 0; i < COUNT(commands) ; i++) {
if (!strcmp(token, commands[i])) {
strcpy(cli->command, token);
cli->cmd_handler = ftable[i];
i = 0;
for (token = strtok(NULL, delim); token != NULL; ) {
cli->params[i++] = token;
token = strtok(NULL, delim);
}
cli->params_num = i;
return 0;
}
}
return -1;
}

int main(void)
{
char buf[BUFSIZ] = {0};
cmdLineEntry_t cli = { {0}, {NULL}, 0, NULL };

while (1) {
printf("%s", PROMPT);
if ( fgets(buf, BUFSIZ, stdin) ) {
/* skip LF/CR/TAB/SP */
if (buf[0] == '\n' || buf[0] == ' ' || buf[0] == '\r' || buf[0]
== '\t')
continue;

/* parse stream */
if ( cliParseCommand Line(buf, &cli) < 0 ) {
printf("Error : invalid command!\n");
continue;
}
else {
cli.cmd_handler (cli.params_num , cli.params);
}
}
else continue; /* EOL */
} /* while */

return 0;
}

TIA!
With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Feb 11 '06
34 6855
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define UC unsigned char
#define U unsigned

#define BUFSIZ_ BUFSIZ

static const char *pc = "CLI[\"vexit\" to end]> ";

struct you{
char* name;
char* pointer;
char* descrizione;
unsigned npar;
struct you* prev;
};

struct you *last=0;

/*limit sono i parametri + 1 (nome della funzione) */
int popola(char** v, char* buf, int limit);

void skip_line(FILE* pf)
{int c; while( (c=fgetc(pf))!= '\n' && c!=EOF );}

int cerca(const char* name, U par)
{U i;
struct you *p=last, *v;
while(1) {if(p==0) break;
v=p; p=p->prev;
if( v->npar==par && strcmp(v->name, name)==0 )
return 1;
}
return 0;
}

int
inserisci(const char* name, char* pointer, U par, char*
descrizione)
{struct you *tm;
char *tmp;
unsigned sz;
///////////////////
if( name==0 || (sz=strlen(name ))==0 )
return 0;
if( cerca(name, par)==1 )
return 0;
if( (tm =malloc(sizeof( struct you)))==0 )
return 0;
if( (tmp=malloc(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;
tm->descrizione = descrizione;
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);
}
last=0;
}
void
insert_f(char* name, char* pointer, char* par, char* descrizione)
{char *pn=0;
U paru=0;
int k;

k=sscanf(pointe r, "%p", &pn);
if(k!=1) {printf("parame tro \"pointer\" non accettato\n"); return;}
k=sscanf(par , "%u", &paru);
if(k!=1) {printf("parame tro \"parametri\ " non accettato\n"); return;}
k=inserisci(nam e, pn, paru, descrizione);
if(k!=1){printf ("problemi di memoria o nome gia\' scelto\n");
return;
}
}

void show(void){puts ("cliShow() stub"); }
void version(void){p uts("cliVersion () stub");}
void help(void)
{U i;
struct you *p=last, *v;
puts("Comandi(p arametri)");
while(1) {if(p==0) break;
v=p; p=p->prev;
printf("%p %s(%u): %s\n",
(void*)(v->pointer), v->name, v->npar, v->descrizione) ;
}
}

void port(void){puts ("cliPort() stub");}
void vexit(void)
{puts("cliExit( ) stub");
free_list();
exit(0);
}

void add(char* a1, char* a2)
{int aa1=0, aa2=0;
sscanf(a1, "%d", &aa1);
sscanf(a2, "%d", &aa2);
printf("%d\n", aa1+aa2);
}

void sub(char* a1, char* a2)
{int aa1=0, aa2=0;
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, led;
/////////////////////////////////
inserisci("show ",(char*) show, 0, "mostra etc");
inserisci("vers ion", (char*) version, 0, "mostra versione");
inserisci("help ", (char*) help, 0, "help");
inserisci("h", (char*) help, 0, "help");
inserisci("-h", (char*) help, 0, "help");
inserisci("port ", (char*) port, 0, "port");
inserisci("vexi t",(char*) vexit, 0, "esce dal sistema");
inserisci("add" ,(char*) add, 2, "somma due numeri");
inserisci("sub" ,(char*) sub, 2, "fa differenza di due numeri");
inserisci("inse risci",(char*) insert_f, 4,
"inserisce una funzione:\ninse risci(name, pointer_to_fun,
parametri, descrizione)");

la:;
while (1)
{printf("%s", pc); fflush(stdout);
buf[BUFSIZ_-2]=0; /* massimo BUFSIZ_-1 chars */
if( fgets(buf, BUFSIZ_, stdin )!=0 )
{if(buf[BUFSIZ_-2]!=0 && buf[BUFSIZ_-2]!='\n')
{printf("Linea troppo lunga\n");
if(feof(stdin)) vexit();
skip_line(stdin );
if(feof(stdin)) vexit();
goto la;
}
cv=popola(a, buf, 8);
if(cv<=0 || cv>8) goto la1;
else --cv;
for(p=last, led=0; led==0 && p!=0 ;p=p->prev)
{if(strcmp(p->name, a[0])==0)
{if(p->npar!=(U)cv)
{la1:; printf("Paramet ri non corretti\n"); goto la;}
switch(p->npar)
{case 0:
( (void (*)(void))(p->pointer) )( );
led=1;
break;
case 1:
( (void (*)(char*))(p->pointer) )(a[1]);
led=1;
break;
case 2:
( (void (*)(char*, char*))(p->pointer) )
(a[1], a[2]);
led=1;
break;
case 3:
( (void (*)(char*, char*, char*))(p->pointer) )
(a[1], a[2], a[3]);
led=1;
break;
case 4:
( (void (*)(char*, char*, char*, char*) )
(p->pointer) ) (a[1], a[2], a[3], a[4]);
led=1;
break;
case 5:
( (void (*)(char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5]);
led=1;
break;
case 6:
( (void (*)(char*, char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5], a[6]);
led=1;
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]);
led=1;
break;
default:
break;
}//end switch
}//if
}//for
if(led!=1) printf("funzion e non presente\n");
}// if fgets
if(feof(stdin)) vexit();
}//while
return 0;
}
Feb 19 '06 #31
On Mon, 13 Feb 2006 10:57:06 -0500, "Rod Pemberton"
<do*********@so rry.bitbucket.c mm> wrote:

"Nick Keighley" <ni************ ******@hotmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .

To this, to specifically catch an ASCII carriage return and newline:
if buf[0]==0x0A||buf[0]==0x0D||buf[0]==' '||buf[0]=='\t')


why?


Because '\r' and '\n' map to environment specific values. Those values may
not match his character set creating an unexpected result. '\n' can map to
many things. For Windows and DOS machines using ASCII, it usually maps to
0x0D 0x0A. For Linux, Unix, Posix using ASCII, it usually maps to 0x0A.
For Macintoshes using ASCII, it maps to 0x0D. For IBM mainframes using
EBCDIC, it maps to 0x15.

Well, they certainly match the platform's character set (or more
precisely code), that's the purpose of the mapping. I think you're
saying his _data_ may not match his platform. Which is possible,
although it is possible in so many ways it's not clear to me that
fixing only this one is worth it.
Why don't you replace ' ' and \t with hex values?


Because they don't cause problems as noted above.

They are different and do cause this problem for EBCDIC vs ASCII,
whereas CR by coincidence is 0x0D in both and does not.
Or, to this:
if (strtok(buf,"\x 0D\x0A\t ")==NULL)


or strchr()


strchr() will only allow him to check for one char at a time. strtok() will
do all at once. He'll need to test to see if it's faster or slower than the
multiple if's.

And strspn() does it all at once without modifying the data, if that's
an issue, although in this (OP) case he definitely has a modifiable
buffer and AFAICS doesn't need the data afterwards.

Agree about testing, although in this case I very much doubt the
difference is even detectable much less significant.

- David.Thompson1 at worldnet.att.ne t
Feb 20 '06 #32

do you like my CLI (Command Line Interface)? :))

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define UC unsigned char
#define U unsigned

#define BUFSIZ_ BUFSIZ

static const char *pc_pro = "CLI[\"vexit\" to end]> ";

struct you{
char* name;
char* pointer;
char* descrizione;
unsigned npar;
struct you* prev;
};

struct you *last=0;

/*limit sono i parametri + 1 (nome della funzione) */
int popola(char** v, char* buf, int limit);

int skip_line(FILE* pf)
{int c; while( (c=fgetc(pf))!= '\n' && c!=EOF ); return c;}

int cmp_from_now(ch ar* a0, char* a1)
{int i;
for( i=0; a0[i]!=0 && a0[i]==a1[i] && !isspace(a0[i]); ++i);
if( (isspace(a0[i])||a0[i]==0) && (isspace(a1[i])||a1[i]==0))
return 1;
else return 0;
}

int cerca(const char* name, U par)
{U i;
struct you *p=last, *v;
while(1) {if(p==0) break;
v=p; p=p->prev;
if( v->npar==par && strcmp(v->name, name)==0 )
return 1;
}
return 0;
}

int
inserisci(const char* name, char* pointer, U par, char*
descrizione)
{struct you *tm;
char *tmp;
unsigned sz;
///////////////////
if( name==0 || (sz=strlen(name ))==0 )
return 0;
if( cerca(name, par)==1 )
return 0;
if( (tm =malloc(sizeof( struct you)))==0 )
return 0;
if( (tmp=malloc(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;
tm->descrizione = descrizione;
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);
}
last=0;
}
void
insert_f(char* name, char* pointer, char* par, char* descrizione)
{char *pn=0;
U paru=0;
int k;

k=sscanf(pointe r, "%p", &pn);
if(k!=1) {printf("parame tro \"pointer\" non accettato\n"); return;}
k=sscanf(par , "%u", &paru);
if(k!=1) {printf("parame tro \"parametri\ " non accettato\n"); return;}
k=inserisci(nam e, pn, paru, descrizione);
if(k!=1){printf ("problemi di memoria\n"); return;}
}
void sys(char* a1){if( system(a1)!=0 ) printf("\a"); }
void show(void){puts ("cliShow() stub"); }
void version(void){p uts("cliVersion () stub");}
void help(void)
{U i;
struct you *p=last, *v;
puts("Comandi(p arametri)");
while(1) {if(p==0) break;
v=p; p=p->prev;
printf("%p %s(%u): %s\n",
(void*)(v->pointer), v->name, v->npar, v->descrizione) ;
}
}

void port(void){puts ("cliPort() stub");}
void vexit(void)
{puts("cliExit( ) stub");
free_list();
exit(0);
}

void add(char* a1, char* a2)
{int aa1=0, aa2=0;
sscanf(a1, "%d", &aa1);
sscanf(a2, "%d", &aa2);
printf("%d\n", aa1+aa2);
}

void sub(char* a1, char* a2)
{int aa1=0, aa2=0;
sscanf(a1, "%d", &aa1);
sscanf(a2, "%d", &aa2);
printf("%d\n", aa1-aa2);
}
int main(void)
{char buf[BUFSIZ_] = {0}, *a[32], *pc;
struct you *p;
int cv, led;
/////////////////////////////////
inserisci("sys" ,(char*) sys, 1, "chiama sistema");
inserisci("show ",(char*) show, 0, "mostra etc");
inserisci("vers ion", (char*) version, 0, "mostra versione");
inserisci("help ", (char*) help, 0, "help");
inserisci("h", (char*) help, 0, "help");
inserisci("-h", (char*) help, 0, "help");
inserisci("port ", (char*) port, 0, "port");
inserisci("vexi t",(char*) vexit, 0, "esce dal sistema");
inserisci("add" ,(char*) add, 2, "somma due numeri");
inserisci("sub" ,(char*) sub, 2, "fa differenza di due numeri");
inserisci("inse risci",(char*) insert_f, 4,
"inserisce una funzione:\ninse risci(name, pointer_to_fun,
parametri, descrizione)");

la:;
while (1)
{printf("%s", pc_pro); fflush(stdout);
buf[BUFSIZ_-2]=0; /* massimo BUFSIZ_-1 chars */
if( fgets(buf, BUFSIZ_, stdin )!=0 )
{if(buf[BUFSIZ_-2]!=0 && buf[BUFSIZ_-2]!='\n')
{printf("Linea troppo lunga\n");
if(feof(stdin)) vexit();
if( skip_line(stdin ) == EOF );
vexit();
goto la;
}
for(pc=buf; isspace(*pc); ++pc);
if( cmp_from_now(pc , "sys")==1 )
{if(pc[3]==0) sys("\n");
else sys(pc+4);
goto la;
}
cv=popola(a, buf, 8);
if(cv<=0 || cv>8) goto la1;
else --cv;
for(p=last, led=0; led==0 && p!=0 ;p=p->prev)
{if(strcmp(p->name, a[0])==0)
{if(p->npar!=(U)cv)
{la1:; printf("Paramet ri non corretti\n"); goto la;}
switch(p->npar)
{case 0:
( (void (*)(void))(p->pointer) )( );
led=1;
break;
case 1:
( (void (*)(char*))(p->pointer) )(a[1]);
led=1;
break;
case 2:
( (void (*)(char*, char*))(p->pointer) )
(a[1], a[2]);
led=1;
break;
case 3:
( (void (*)(char*, char*, char*))(p->pointer) )
(a[1], a[2], a[3]);
led=1;
break;
case 4:
( (void (*)(char*, char*, char*, char*) )
(p->pointer) ) (a[1], a[2], a[3], a[4]);
led=1;
break;
case 5:
( (void (*)(char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5]);
led=1;
break;
case 6:
( (void (*)(char*, char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5], a[6]);
led=1;
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]);
led=1;
break;
default:
break;
}//end switch
}//if
}//for
if(led!=1) printf("funzion e non presente\n");
}// if fgets
if(feof(stdin)) vexit();
}//while
return 0;
}
Feb 20 '06 #33
On Mon, 20 Feb 2006 09:44:40 +0100, RSoIsCaIrLiIoA <zz@zz.z> wrote:

do you like my CLI (Command Line Interface)? :))

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>


int main(void)
{char buf[BUFSIZ_] = {0}, *a[32], *pc;
struct you *p;
int cv, led;
/////////////////////////////////
inserisci("sys" ,(char*) sys, 1, "chiama sistema");
inserisci("show ",(char*) show, 0, "mostra etc");
inserisci("vers ion", (char*) version, 0, "mostra versione");
inserisci("help ", (char*) help, 0, "help");
inserisci("h", (char*) help, 0, "help");
inserisci("-h", (char*) help, 0, "help");
inserisci("port ", (char*) port, 0, "port");
inserisci("vexi t",(char*) vexit, 0, "esce dal sistema");
inserisci("add" ,(char*) add, 2, "somma due numeri");
inserisci("sub" ,(char*) sub, 2, "fa differenza di due numeri");
inserisci("inse risci",(char*) insert_f, 4,
"inserisce una funzione:\ninse risci(name, pointer_to_fun,
parametri, descrizione)");

la:;
while (1)
{printf("%s", pc_pro); fflush(stdout);
buf[BUFSIZ_-2]=0; /* massimo BUFSIZ_-1 chars */
if( fgets(buf, BUFSIZ_, stdin )!=0 )
{if(buf[BUFSIZ_-2]!=0 && buf[BUFSIZ_-2]!='\n')
{printf("Linea troppo lunga\n");
if(feof(stdin)) vexit();
if( skip_line(stdin ) == EOF );
vexit();
goto la;
}
for(pc=buf; isspace(*pc); ++pc);
if( cmp_from_now(pc , "sys")==1 )
{if(pc[3]==0) sys("\n");
else sys(pc+4);
if(feof(stdin)) vexit();
goto la;
}
cv=popola(a, buf, 8);
if(cv<=0 || cv>8) goto la1;
else --cv;
for(p=last, led=0; led==0 && p!=0 ;p=p->prev)
{if(strcmp(p->name, a[0])==0)
{if(p->npar!=(U)cv)
{la1:; printf("Paramet ri non corretti\n"); goto la;}
switch(p->npar)
{case 0:
( (void (*)(void))(p->pointer) )( );
led=1;
break;
case 1:
( (void (*)(char*))(p->pointer) )(a[1]);
led=1;
break;
case 2:
( (void (*)(char*, char*))(p->pointer) )
(a[1], a[2]);
led=1;
break;
case 3:
( (void (*)(char*, char*, char*))(p->pointer) )
(a[1], a[2], a[3]);
led=1;
break;
case 4:
( (void (*)(char*, char*, char*, char*) )
(p->pointer) ) (a[1], a[2], a[3], a[4]);
led=1;
break;
case 5:
( (void (*)(char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5]);
led=1;
break;
case 6:
( (void (*)(char*, char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5], a[6]);
led=1;
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]);
led=1;
break;
default:
break;
}//end switch
}//if
}//for
if(led!=1) printf("funzion e non presente\n");
}// if fgets
if(feof(stdin)) vexit();
}//while
return 0;
}


Feb 20 '06 #34

Do you like my Command Line Interface?
your is better than this? Why?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define UC unsigned char
#define U unsigned

#define BUFSIZ_ BUFSIZ

static const char *pc_pro = "CLI[\"vexit\" to end]> ";

struct you{
char* name;
char* pointer;
char* descrizione;
unsigned npar;
struct you* prev;
};

struct you *last=0;

/*limit sono i parametri + 1 (nome della funzione) */
int popola(char** v, char* buf, int limit);

int skip_line(FILE* pf)
{int c; while( (c=fgetc(pf))!= '\n' && c!=EOF ); return c;}

int cmp_from_now(ch ar* a0, char* a1)
{int i;
for( i=0; a0[i]!=0 && a0[i]==a1[i] && !isspace(a0[i]); ++i);
if( (isspace(a0[i])||a0[i]==0) && (isspace(a1[i])||a1[i]==0))
return 1;
else return 0;
}

int cerca(const char* name, U par)
{U i;
struct you *p=last, *v;
while(1) {if(p==0) break;
v=p; p=p->prev;
if( v->npar==par && strcmp(v->name, name)==0 )
return 1;
}
return 0;
}

int
inserisci(const char* name, char* pointer, U par, char*
descrizione)
{struct you *tm;
char *tmp;
unsigned sz;
///////////////////
if( name==0 || (sz=strlen(name ))==0 )
return 0;
if( cerca(name, par)==1 )
return 0;
if( (tm =malloc(sizeof( struct you)))==0 )
return 0;
if( (tmp=malloc(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;
tm->descrizione = descrizione;
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);
}
last=0;
}
void
insert_f(char* name, char* pointer, char* par, char* descrizione)
{char *pn=0;
U paru=0;
int k;

k=sscanf(pointe r, "%p", &pn);
if(k!=1) {printf("parame tro \"pointer\" non accettato\n"); return;}
k=sscanf(par , "%u", &paru);
if(k!=1) {printf("parame tro \"parametri\ " non accettato\n"); return;}
k=inserisci(nam e, pn, paru, descrizione);
if(k!=1){printf ("problemi di memoria\n"); return;}
}
void sys(char* a1){if( system(a1)!=0 ) printf("\a"); }
void show(void){puts ("cliShow() stub"); }
void version(void){p uts("cliVersion () stub");}
void help(void)
{U i;
struct you *p=last, *v;
puts("Comandi(p arametri)");
while(1) {if(p==0) break;
v=p; p=p->prev;
printf("%p %s(%u): %s\n",
(void*)(v->pointer), v->name, v->npar, v->descrizione) ;
}
}

void port(void){puts ("cliPort() stub");}
void vexit(void)
{puts("cliExit( ) stub");
free_list();
exit(0);
}

void add(char* a1, char* a2)
{int aa1=0, aa2=0;
sscanf(a1, "%d", &aa1);
sscanf(a2, "%d", &aa2);
printf("%d\n", aa1+aa2);
}

void sub(char* a1, char* a2)
{int aa1=0, aa2=0;
sscanf(a1, "%d", &aa1);
sscanf(a2, "%d", &aa2);
printf("%d\n", aa1-aa2);
}
int main(void)
{char buf[BUFSIZ_] = {0}, *a[32], *pc;
struct you *p;
int cv, led;
/////////////////////////////////
inserisci("sys" ,(char*) sys, 1, "chiama sistema");
inserisci("show ",(char*) show, 0, "mostra etc");
inserisci("vers ion", (char*) version, 0, "mostra versione");
inserisci("help ", (char*) help, 0, "help");
inserisci("h", (char*) help, 0, "help");
inserisci("-h", (char*) help, 0, "help");
inserisci("port ", (char*) port, 0, "port");
inserisci("vexi t",(char*) vexit, 0, "esce dal sistema");
inserisci("add" ,(char*) add, 2, "somma due numeri");
inserisci("sub" ,(char*) sub, 2, "fa differenza di due numeri");
inserisci("inse risci",(char*) insert_f, 4,
"inserisce una funzione:\ninse risci(name, pointer_to_fun,
parametri, descrizione)");

la0:;
while (1)
{printf("%s", pc_pro); fflush(stdout);
buf[BUFSIZ_-2]=0; /* massimo BUFSIZ_-1 chars */
if( fgets(buf, BUFSIZ_, stdin )!=0 )
{if(buf[BUFSIZ_-2]!=0 && buf[BUFSIZ_-2]!='\n')
{printf("Linea troppo lunga\n");
if(feof(stdin)) vexit();
if( skip_line(stdin ) == EOF );
vexit();
goto la0;
}
for(pc=buf; isspace(*pc); ++pc);
if( cmp_from_now(pc , "sys")==1 )
{if(pc[3]==0) sys("\n");
else sys(pc+4);
goto la;
}
cv=popola(a, buf, 8);
if(cv<=0 || cv>8) goto la1;
else --cv;
for(p=last, led=0; led==0 && p!=0 ;p=p->prev)
{if(strcmp(p->name, a[0])==0)
{if(p->npar!=(U)cv)
{la1:;
printf("Paramet ri non corretti\n"); goto la;
}
switch(p->npar)
{case 0:
( (void (*)(void))(p->pointer) )( );
led=1;
break;
case 1:
( (void (*)(char*))(p->pointer) )(a[1]);
led=1;
break;
case 2:
( (void (*)(char*, char*))(p->pointer) )
(a[1], a[2]);
led=1;
break;
case 3:
( (void (*)(char*, char*, char*))(p->pointer) )
(a[1], a[2], a[3]);
led=1;
break;
case 4:
( (void (*)(char*, char*, char*, char*) )
(p->pointer) ) (a[1], a[2], a[3], a[4]);
led=1;
break;
case 5:
( (void (*)(char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5]);
led=1;
break;
case 6:
( (void (*)(char*, char*, char*, char*, char*, char*))
(p->pointer) )( a[1], a[2], a[3], a[4], a[5], a[6]);
led=1;
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]);
led=1;
break;
default:
break;
}//end switch
}//if
}//for
if(led!=1) printf("funzion e non presente\n");
}// if fgets
la:;
if(feof(stdin)) vexit();
}//while
return 0;
}

---------------------------

; nasmw -f obj this_file.asm
; bcc32 this_file.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
mov edi, -1
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
Feb 20 '06 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1867
by: djc | last post by:
I'm new to this and was wondering what the options are for interpreting the command line using a CLI program. Specifically methods for interpreting the parameters passed to the program on the command line. I noticed that visual studio adds the following for you when creating a new CLI program: void main(string args) { }
51
4131
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under Command Prompt or Browser or some other application? Ojas.
0
8736
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8649
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9229
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8977
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7824
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2398
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2035
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.