Facciamo cosi': posto l'header ed il main e vediamo cosa non va:
header: cld.h
codice:
#ifndef __CLD_H_
#define __CLD_H_
#define CLD_OK 1
#define CLD_FAIL 0
#define CLD_RESULT int
#define CLD_SUCCEDE(a) (a == CLD_OK)
#define CLD_FAILED(a) (a == CLD_FAIL)
#define CLD_MAX_COMMANDS 32
CLD_RESULT (*COMMAND)(int, char[][]);
struct command_node{
int index;
char* name;
char* doc;
int args_num;
COMMAND command;
};
struct command_node commands[CLD_MAX_COMMANDS];
int cindex = 0;
int register_command(char*, char*, int, COMMAND);
CLD_RESULT run_command(char*, int, char[][]);
#endif
main: cld.c
codice:
#include <stdio.h>
#include "cld.h"
int register_command(char* name, char* doc, int an, COMMAND cmd){
commands[cindex] = {cindex, name, doc, an, cmd};
cindex++;
}
//definizione di un comando test
//Comando Print: stampa gli argomenti
CLD_RESULT cmd_print(int argc, char[] *args){
int i;
for(i = 0; i < argc; i++){
printf("%s\n", args[i]);
}
return CLD_OK;
}
CLD_RESULT run_command(char* name, int argc, char[][] args){
struct command_node *tmp;
COMMAND cmd = NULL;
int i;
for(i = 0; i < cindex; i++){
tmp = &commands[i];
if(tmp->name == name){
if(tmp->args_num == -1 || tmp->args_num == argc){
cmd = tmp->command;
break;
}
}
}
if(cmd == NULL){
return CLD_FAIL;
}
return cmd(argc, args);
}
int main(int argc, char[] *args){
char[] *args = {
"primo",
"secondo"
};
register_command("print", "prints all the arguments passed", -1, cmd_print);
run_command("print", 2, args):
return 0;
}
Ripeto che e' la prima volta che uso C. Il programmino dovrebbe registrare un puntatore a funzione e richiamare la funzione tramite run_command(). La compilazione da errori anche sulle variabili cindex e commands, che dovrebbero essere globali (forse devo usare extern???)
grazie per l'aiuto