home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / v9t9 / linux / sources / V9t9 / source / command.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-19  |  7.2 KB  |  254 lines

  1. #ifndef __COMMAND_H__
  2. #define __COMMAND_H__
  3.  
  4. #include "OSLib.h"
  5.  
  6. #include "centry.h"
  7.  
  8. enum
  9. {
  10.     caa_READ, caa_WRITE
  11. };    /* tasks for action */
  12.  
  13. struct command_arg;
  14. typedef int (*command_arg_action)(struct command_arg *self, int task);
  15.  
  16. #define DECL_ARG_ACTION(name) int name(struct command_arg *arg, int task)
  17.  
  18. //    Flags for commands:  bitmask
  19. typedef enum
  20. {
  21.     c_NONE = 0,
  22.     c_STATIC = 0,            // static value for saving
  23.     c_DYNAMIC = 1,            // dynamically determines config, 
  24.                             // pass csa_READ multiple times with increasing iter
  25.     c_DONT_SAVE = 2,        // don't save value back to config
  26.     c_SESSION_ONLY = 4,        // if set, item should be saved for sessions but 
  27.                             // not in general config
  28.     c_CONFIG_ONLY = 8        // if set, item should only be saved in basic
  29.                             // config file, not in session
  30. }    command_symbol_flags;
  31.  
  32. //    Types of command arguments
  33. typedef enum 
  34. {
  35.     ca_VOID, ca_SYM, ca_NUM, ca_STRING,
  36.     ca_SPEC, ca_PATHSPEC, ca_NAMESPEC,
  37.     ca_TOGGLE
  38. }    command_arg_type;
  39.  
  40. extern char *ca_types[];
  41.  
  42. #define ca_ISSPEC(x) ((x)==ca_SPEC || (x)==ca_PATHSPEC || (x)==ca_NAMESPEC)
  43.  
  44. typedef struct command_arg {
  45.     char    *name;                    /* name of argument */
  46.     char    *help;                    /* usage info */
  47.     command_arg_action action;        /* action associated with argument */
  48.     
  49.     command_arg_type type;            /* ca_NUM, ca_STRING */
  50.     union    
  51.     {
  52.         struct    
  53.         {
  54.             int        sz;                /* 1,2,4 = size of pointed var */
  55.             void    *mem;
  56.         }            num;            /* ca_NUM */
  57.         struct    
  58.         {
  59.             int        sz;                /* 1,2,4 = size of pointed var */
  60.             void    *mem;
  61.             int        flag;
  62.         }            toggle;            /* ca_TOGGLE */
  63.         struct
  64.         {
  65.             int        maxlen;            /* max len of string [memory] */
  66.             union
  67.             {
  68.                 char    *mem;        /* if maxlen >= 0 */
  69.                 char    **ptr;        /* if maxlen < 0, we store ptr to buffer */
  70.             }        m;
  71.         }            string;            /* ca_STRING */
  72.         struct
  73.         {
  74.             OSSpec    *mem;
  75.         }            spec;            /* ca_SPEC */
  76.         struct
  77.         {
  78.             OSPathSpec    *mem;
  79.         }            pathspec;        /* ca_PATHSPEC */
  80.         struct
  81.         {
  82.             OSNameSpec    *mem;
  83.         }            namespec;        /* ca_NAMESPEC */
  84.     }        u;
  85.     struct     command_arg *next;
  86. }    command_arg;
  87.  
  88. enum
  89. {
  90.     csa_READ, csa_WRITE
  91. };    /* tasks for action */
  92.  
  93. struct command_symbol;
  94. typedef int (*command_symbol_action)(struct command_symbol *self, int task, int iter);
  95.  
  96. // task is caa_XXX, iter=0..x for caa_READ_CONFIG
  97. #define DECL_SYMBOL_ACTION(name) int name(struct command_symbol *sym, int task, int iter)
  98.  
  99. #define SYM_ARG_1st    (sym->args)
  100. #define SYM_ARG_2nd (SYM_ARG_1st ? sym->args->next : 0L)
  101. #define SYM_ARG_3rd (SYM_ARG_2nd ? sym->args->next->next : 0L)
  102. #define SYM_ARG_4th (SYM_ARG_3rd ? sym->args->next->next->next : 0L)
  103. #define SYM_ARG_5th (SYM_ARG_4th ? sym->args->next->next->next->next : 0L)
  104. #define SYM_ARG_6th (SYM_ARG_5th ? sym->args->next->next->next->next->next : 0L)
  105.  
  106. #define SYMBOL_ACTION_BIT_TOGGLE(var, bit) \
  107.     do {                                            \
  108.         int flag;                                    \
  109.         if (task == caa_WRITE) {                    \
  110.             command_arg_get_num(sym->args, &flag);    \
  111.             if (flag)                                \
  112.                 var |= bit;                            \
  113.             else                                    \
  114.                 var &= ~bit;                        \
  115.         } else {                                    \
  116.             flag = (var & bit) != 0;                \
  117.             command_arg_set_num(sym->args, flag);    \
  118.         }                                            \
  119.     }    while (0)
  120.  
  121.  
  122. typedef struct command_symbol {
  123.     char    *name;                    /* name (case-insensitive) */
  124.     char    *help;                    /* description of use */
  125.     command_symbol_flags flags;        /* flags for symbol */
  126.     command_symbol_action action;    /* action associated with command */
  127.     struct    command_arg *ret;        /* value if used on RHS */
  128.     struct    command_arg    *args;        /* arguments we take */
  129.     struct     command_symbol *next;
  130. }    command_symbol;
  131.  
  132. typedef struct command_symbol_table {
  133.     char    *name;                    /* name of table */
  134.     char    *help;                    /* more info */
  135.     struct     command_symbol_table *sub;    /* table of subcommands */
  136.     struct     command_symbol *list;        /* list of symbols */
  137.     struct     command_symbol_table *next;    /* next table */
  138. }    command_symbol_table;
  139.  
  140. extern command_symbol_table *universe;
  141.  
  142. command_symbol_table *
  143. command_symbol_table_new(char *name, char *help, 
  144.         command_symbol *list, command_symbol_table *sub, 
  145.         command_symbol_table *next);
  146.  
  147. command_symbol_table *
  148. command_symbol_table_add_subtable(command_symbol_table *parent,
  149.         command_symbol_table *table);
  150.  
  151. command_symbol_table *
  152. command_symbol_table_add(command_symbol_table *parent, command_symbol *list);
  153.  
  154. /*    Any symbol may define ret==RET_FIRST_ARG to mean return value of first argument. */
  155.  
  156. #define RET_FIRST_ARG (command_arg *)(-1)
  157. command_symbol *
  158. command_symbol_new(char *name, char *help, command_symbol_flags flags,
  159.         command_symbol_action action, command_arg *ret, command_arg *args, 
  160.         command_symbol *next);
  161.  
  162. #define ARG_NUM(x)    sizeof(x), &x
  163. #define NEW_ARG_NUM(t) sizeof(t), xmalloc(sizeof(t))
  164. #define NEW_ARG_CONST_NUM(x)    sizeof(int), xintdup(x)
  165.  
  166. command_arg *
  167. command_arg_new_num(char *name, char *help, command_arg_action action,
  168.         int sz, void *mem, command_arg *next);
  169.  
  170. /*    You may specify an unbounded string array by passing
  171.     maxlen < 0 and treating 'str' as a pointer to a pointer to
  172.     the string. */
  173. #define ARG_STR(x)    sizeof(x), x
  174. #define NEW_ARG_STR(l) l, xmalloc(l)
  175. #define NEW_ARG_STRBUF(p) -1, p
  176. #define NEW_ARG_NEW_STRBUF -1, xcalloc(sizeof(void *))
  177.  
  178. command_arg *
  179. command_arg_new_string(char *name, char *help, command_arg_action action,
  180.         int maxlen, void *str, command_arg *next);
  181.  
  182. command_arg *
  183. command_arg_new_spec(char *name, char *help, command_arg_action action,
  184.         OSSpec *spec, command_arg *next);
  185.  
  186. command_arg *
  187. command_arg_new_pathspec(char *name, char *help, command_arg_action action,
  188.         OSPathSpec *pathspec, command_arg *next);
  189.  
  190. command_arg *
  191. command_arg_new_namespec(char *name, char *help, command_arg_action action,
  192.         OSNameSpec *namespec, command_arg *next);
  193.  
  194. command_arg *
  195. command_arg_new_toggle(char *name, char *help, command_arg_action action,
  196.         int sz, void *mem, int val, command_arg *next);
  197.  
  198. /*****************/
  199.  
  200. void    command_arg_read_num(command_arg *arg, int *val);
  201. int    command_arg_get_num(command_arg *arg, int *val);
  202. int    command_arg_set_num(command_arg *arg, int val);
  203.  
  204. void    command_arg_read_string(command_arg *arg, char **str);
  205. int    command_arg_get_string(command_arg *arg, char **str);
  206. int    command_arg_set_string(command_arg *arg, const char *str);
  207.  
  208. void    command_arg_read_spec(command_arg *arg, char **str);
  209. int    command_arg_get_spec(command_arg *arg, char **str);
  210. int    command_arg_set_spec(command_arg *arg, const char *str);
  211.  
  212. void    command_arg_read_toggle(command_arg *arg, int *val);
  213. int    command_arg_get_toggle(command_arg *arg, int *val);
  214. int    command_arg_set_toggle(command_arg *arg, int val);
  215.  
  216. int    command_match_symbol(const command_symbol_table *table,
  217.                 const char *name, command_symbol **sym);
  218. void
  219. command_match_symbols(const command_symbol_table *table,
  220.                     const char *name, 
  221.                     command_symbol ***matches, int *nmatches);
  222.  
  223. struct command_exprval;
  224. int command_set_args(struct command_exprval *ret, command_symbol *sym, ... /* command_exprval */);
  225.  
  226. int command_get_val(command_symbol *sym, struct command_exprval *val);
  227.  
  228. /*    Interface to lexer  */
  229.  
  230. int    command_lexer_setup_text(char *name, char *text, int len);
  231. int    command_lexer_setup_file(char *filename);
  232.  
  233. /*    Interface to parser */
  234.  
  235. int    command_parse(command_symbol_table *universe);
  236.  
  237. /*    Interface to emulator */
  238.  
  239. void    command_init(void);
  240. int        command_parse_file(char *name);
  241. int        command_parse_text(char *str);
  242.  
  243. /*    Filter execution of session-only commands */
  244.  
  245. bool    command_get_session_filter(void);
  246. void    command_set_session_filter(bool allow_session_only);
  247.  
  248. /*    Interface to help */
  249. void    command_help(void);
  250.  
  251. #include "cexit.h"    
  252.  
  253. #endif
  254.