473,703 Members | 3,315 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 6857
Hello, Barry!
You wrote on Sun, 12 Feb 2006 11:08:48 -0800:

Thank you very much for useful tips.

[skip]
BS> But you are inconsistent. You actually copy the command but only
BS> store the argument addresses. What about the command is so special
BS> that you need to store the actual characters in cli->command rather
BS> than simply the address of the command in buf?
??>> while (1) {
??>> printf("%s", PROMPT);

BS> Use fflush here to force the output to your screen.

??>> if ( fgets(buf, BUFSIZ, stdin) ) {
??>> /* skip LF/CR/TAB/SP */
??>> if (buf[0] == '\n' || buf[0] == ' ' || buf[0] == '\r' ||
??>> buf[0] == '\t')

BS> Look up the isspace() function if you really want to do this. Are you
BS> sure you want to do this since the strtoken call in
BS> cliParseCommand Line() will very happily skip over leading white space
BS> for you?
Actually true, but my intention was to skip input consisting only of '\t',
'\n', ' ' and '\r'. That's isspace() is much better here.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Feb 13 '06 #21
Rod Pemberton wrote:
"Roman Mashak" <mr*@tusur.ru > wrote in message
news:ds******** ***@relay.tomsk .ru...
Hello, Rod!
Sorry I made typo in first post, right definition of "cmdLineEntry_t " is:

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

i.e. params is array of pointers to char.

Some suggestions:


the OP code was

/* skip LF/CR/TAB/SP */
if (buf[0] == '\n' || buf[0] == ' ' || buf[0] == '\r' ||
buf[0]
== '\t')
I would change this line, extra spaces deleted:
why remove the space, is that to reduce clarity or just for posting
purposes?
if (buf[0]=='\n'||buf[0]==' '||buf[0]=='\r'||buf[0]=='\t')


how is this different from the OP's code?
To this, to specifically catch an ASCII carriage return and newline:
if buf[0]==0x0A||buf[0]==0x0D||buf[0]==' '||buf[0]=='\t')
why? How do you know he's using ASCII? Your code is *less* clear
than the original! Why don't you replace ' ' and \t with hex values?
<rant> they were writing programming guides in '50's explaining why
this was a bad idea! <end rant>

ok, I exagerate... a little.
Or, to this:
if (strtok(buf,"\x 0D\x0A\t ")==NULL)


or strchr()

<snip>

--
Nick Keighley

The Elements of Programming Style (Paperback)
by Brian W. Kernighan, P. J. Plauger

ok so it was 1978... only 30 years ago

Feb 13 '06 #22

"Nick Keighley" <ni************ ******@hotmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Rod Pemberton wrote:
"Roman Mashak" <mr*@tusur.ru > wrote in message
news:ds******** ***@relay.tomsk .ru...
Hello, Rod!
Sorry I made typo in first post, right definition of "cmdLineEntry_t " is: Some suggestions:


the OP code was

/* skip LF/CR/TAB/SP */
if (buf[0] == '\n' || buf[0] == ' ' || buf[0] == '\r' ||
buf[0]
== '\t')
I would change this line, extra spaces deleted:


why remove the space, is that to reduce clarity or just for posting
purposes?


I was just letting him know I had removed the spaces and he needed to locate
the same line in his program. Nothing more.
if (buf[0]=='\n'||buf[0]==' '||buf[0]=='\r'||buf[0]=='\t')
how is this different from the OP's code?


It isn't. It's his line. It's the line he needs to locate, with spaces, to
implement the following suggestion.
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.
How do you know he's using ASCII?
I don't. But, to show him how to correctly code for the hexadecimal
sequences, I used ASCII as noted.
Your code is *less* clear
than the original!
Yes, but it corrects a common programming error: trapping C's value for '\r'
or '\n' instead of the character generated by the keyboard.
Why don't you replace ' ' and \t with hex values?
Because they don't cause problems as noted above.
<rant> they were writing programming guides in '50's explaining why
this was a bad idea! <end rant>


Does the code created by following the style guide work correctly for a
specific environment? Or, does it generate errors?
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.

Rod Pemberton
Feb 13 '06 #23
On 2006-02-13, Rod Pemberton <do*********@so rry.bitbucket.c mm> wrote:

"Nick Keighley" <ni************ ******@hotmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Rod Pemberton wrote:
> "Roman Mashak" <mr*@tusur.ru > wrote in message
> news:ds******** ***@relay.tomsk .ru...
> > Hello, Rod!
> > Sorry I made typo in first post, right definition of "cmdLineEntry_t " is: > >
> Some suggestions:
the OP code was

/* skip LF/CR/TAB/SP */
if (buf[0] == '\n' || buf[0] == ' ' || buf[0] == '\r' ||
buf[0]
== '\t')
> I would change this line, extra spaces deleted:


why remove the space, is that to reduce clarity or just for posting
purposes?


I was just letting him know I had removed the spaces and he needed to locate
the same line in his program. Nothing more.
> > if (buf[0]=='\n'||buf[0]==' '||buf[0]=='\r'||buf[0]=='\t')


how is this different from the OP's code?


It isn't. It's his line. It's the line he needs to locate, with spaces, to
implement the following suggestion.
> 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.


Yes, on text file output. No, anywhere else. '\n' does not map to a pair
of two character values. It may be returned by getchar when getchar sees
those two values, but nowhere else.
For Linux, Unix, Posix using ASCII, it usually maps to 0x0A. For Macintoshes using ASCII, it maps to 0x0D.
I doubt that [other than reading/writing to files opened in text mode as
noted above.]
For IBM mainframes using EBCDIC, it maps to 0x15.
Bullshit, as noted below.
How do you know he's using ASCII?


I don't. But, to show him how to correctly code for the hexadecimal
sequences, I used ASCII as noted.
Your code is *less* clear
than the original!


Yes, but it corrects a common programming error: trapping C's value for '\r'
or '\n' instead of the character generated by the keyboard.
Why don't you replace ' ' and \t with hex values?


Because they don't cause problems as noted above.


They do on "IBM mainframes using EBCDIC".
<rant> they were writing programming guides in '50's explaining why
this was a bad idea! <end rant>


Does the code created by following the style guide work correctly for a
specific environment? Or, does it generate errors?
> 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.


strpbrk(), then. strtok is evil for other reasons.


note: on ANY ascii-based system, \r is 13 and \n is 10. NO MATTER HOW
the system represents linebreaks in text files or as a keyboard code.

Also - reading from stdin - whenever the user presses enter, that will
appear to getchar() or fgets() or whatever else is used to read it as
'\n', regardless of whether the enter key actually sends 0x0D or 42 or
396.
Feb 13 '06 #24
On Sun, 12 Feb 2006 09:55:42 -0500, "Rod Pemberton"
<do*********@so rry.bitbucket.c mm> wrote:

"Roman Mashak" <mr*@tusur.ru > wrote in message
news:ds******* ****@relay.toms k.ru...
Hello, Rod!
Sorry I made typo in first post, right definition of "cmdLineEntry_t " is:

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

i.e. params is array of pointers to char.


Some suggestions:

I would change this line, extra spaces deleted:
if (buf[0]=='\n'||buf[0]==' '||buf[0]=='\r'||buf[0]=='\t')


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


Why be inconsistent? Use 0x20 for ' ' and 0x09 for '\t'

Why assume the program is executed on an ASCII system? \n and \r are
guaranteed to work on all systems.

Remove del for email
Feb 14 '06 #25
On Mon, 13 Feb 2006 00:16:32 +0100, RSoIsCaIrLiIoA <zz@zz.z> wrote:
On Sun, 12 Feb 2006 12:33:22 +0100, RSoIsCaIrLiIoA <zz@zz.z> wrote:

; 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(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);
}
last=0;
}
int show(void){puts ("cliShow() stub");return 0;}
int version(void){p uts("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=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;
/////////////////////////////////
inserisci("show ",(char*) show, 0);
inserisci("vers ion", (char*) version, 0);
inserisci("help ", (char*) help, 0);
inserisci("port ", (char*) port, 0);
inserisci("vexi t",(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:
break;
}
}
}
}
return 0;
}

Feb 17 '06 #26
#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;
char* descrizione;
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, char*
descrizione)
{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(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 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("%s(%u): %s\n",
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;
/////////////////////////////////
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");

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:
break;
}
}
}
}
return 0;
}
Feb 17 '06 #27
On Fri, 17 Feb 2006 09:47:16 +0100, in comp.lang.c , RSoIsCaIrLiIoA
<zz@zz.z> wrote:

#define UC unsigned char
#define U unsigned


you are so killfiled its not true.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 17 '06 #28
On Fri, 17 Feb 2006 21:19:05 +0000, Mark McIntyre
<ma**********@s pamcop.net> wrote:
On Fri, 17 Feb 2006 09:47:16 +0100, in comp.lang.c , RSoIsCaIrLiIoA
<zz@zz.z> wrote:

#define UC unsigned char
#define U unsigned
you are so killfiled its not true.
Mark McIntyre


what does it means?
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan


this above means do you see any error?

Feb 18 '06 #29
#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;
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
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( (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 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-2 chars */
if( fgets(buf, BUFSIZ, stdin )!=0 )
{if(buf[BUFSIZ-2]!=0)
{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;
}

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

; 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 19 '06 #30

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
4135
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
8750
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
8662
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
9246
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
9005
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
7856
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
6585
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
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4425
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2440
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.