From: handa Date: Fri, 15 Sep 2006 01:53:26 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=33c2484c76806e4e65fde05b07ae1b0ab278f012;p=m17n%2Fm17n-test.git *** empty log message *** --- diff --git a/m17n-imconf.c b/m17n-imconf.c new file mode 100644 index 0000000..d24aa26 --- /dev/null +++ b/m17n-imconf.c @@ -0,0 +1,322 @@ +#include +#include +#include +#include +#include + +MPlist *error_invalid_format; +MPlist *error_unknown_command; +MPlist *error_invalid_arg; +MPlist *error_command_fail; +MPlist *error_lock_fail; + +MSymbol Merror, Mvalue; +MSymbol Meval_function; + +#define ERROR(msg) \ + do { \ + m17n_object_ref (msg); \ + return msg; \ + } while (0) + +MPlist *eval (MPlist *); + +#define INTEGERP(plist) (mplist_key (plist) == Minteger) +#define SYMBOLP(plist) (mplist_key (plist) == Msymbol) +#define MTEXTP(plist) (mplist_key (plist) == Mtext) +#define PLISTP(plist) (mplist_key (plist) == Mplist) +#define TAILP(plist) (mplist_key (plist) == Mnil) + +MPlist *eval_help (MPlist *arg); + +MPlist * +eval_set (MPlist *arg) +{ + MSymbol var; + + if (! SYMBOLP (arg)) + ERROR (error_invalid_arg); + var = mplist_value (arg); + arg = mplist_next (arg); + if (TAILP (arg)) + msymbol_put (var, Mvalue, NULL); + else if (PLISTP (arg)) + msymbol_put (var, Mvalue, eval (mplist_value (arg))); + else + { + m17n_object_ref (arg); + msymbol_put (var, Mvalue, arg); + } + return NULL; +} + +MPlist * +eval_open (MPlist *arg) +{ + MSymbol lang, name; + MInputMethod *im;; + + if (! SYMBOLP (arg)) + ERROR (error_invalid_arg); + lang = mplist_value (arg); + arg = mplist_next (arg); + if (! SYMBOLP (arg)) + ERROR (error_invalid_arg); + name = mplist_value (arg); + im = minput_open_im (lang, name, Mnil); + arg = mplist (); + mplist_add (arg, Mt, im); + return arg; +} + +MPlist * +eval_close (MPlist *arg) +{ + MPlist *value; + + if (! SYMBOLP (arg)) + ERROR (error_invalid_arg); + value = msymbol_get (mplist_value (arg), Mvalue); + if (! value) + ERROR (error_invalid_arg); + minput_close_im (mplist_value (value)); + return NULL; +} + +MPlist * +parse_arg_common (MPlist *arg, MSymbol *lang, MSymbol *name, MSymbol *target) +{ + if (TAILP (arg)) + *lang = Mt, *name = Mnil, *target = Mnil; + else + { + if (! SYMBOLP (arg)) + return NULL; + *lang = mplist_value (arg); + arg = mplist_next (arg); + if (! SYMBOLP (arg)) + return NULL; + *name = mplist_value (arg); + arg = mplist_next (arg); + if (SYMBOLP (arg)) + { + *target = mplist_value (arg); + arg = mplist_next (arg); + } + else if (TAILP (arg)) + *target = Mnil; + else + return NULL; + } + return arg; +} + +MPlist * +call_get_common (MPlist *(*func) (MSymbol, MSymbol, MSymbol), MPlist *arg) +{ + MSymbol lang, name, target; + + arg = parse_arg_common (arg, &lang, &name, &target); + if (! arg) + ERROR (error_invalid_arg); + if (! TAILP (arg)) + return arg; + arg = func (lang, name, target); + if (! arg) + ERROR (error_command_fail); + /* Prevent it be freed. */ + m17n_object_ref (arg); + return arg; +} + +MPlist * +eval_get_command (MPlist *arg) +{ + return call_get_common (minput_get_command, arg); +} + +MPlist * +eval_get_variable (MPlist *arg) +{ + return call_get_common (minput_get_variable, arg); +} + +MPlist * +call_config_common (int (*func) (MSymbol, MSymbol, MSymbol, MPlist *), + MPlist *arg) +{ + MSymbol lang, name, target; + + arg = parse_arg_common (arg, &lang, &name, &target); + if (! arg) + ERROR (error_invalid_arg); + if (TAILP (arg)) + arg = NULL; + if (func (lang, name, target, arg) < 0) + ERROR (error_command_fail); + return NULL; +} + +MPlist * +eval_config_command (MPlist *arg) +{ + return call_config_common (minput_config_command, arg); +} + +MPlist * +eval_config_variable (MPlist *arg) +{ + return call_config_common (minput_config_variable, arg); +} + +MPlist * +eval_save_config (MPlist *arg) +{ + int ret = minput_save_config (); + + if (ret < 0) + ERROR (error_command_fail); + if (ret == 0) + ERROR (error_lock_fail); + return NULL; +} + +struct EvalFunc { + MPlist *(*eval_func) (MPlist *arg); + char *form; +} eval_functions[] = + { {eval_help, "help"}, + {eval_set, "set SYMBOL FORM"}, + {eval_open, "open LANG NAME"}, + {eval_close, "close SYMBOL"}, + {eval_get_command, "get-command [LANG NAME [COMMAND]]"}, + {eval_config_command, "config-command LANG NAME [COMMAND [(KEY ...) ...]]"}, + {eval_get_variable, "get-variable [LANG NAME [VARIABLE]]"}, + {eval_config_variable, "config-variable LANG NAME [VARIABLE [VALUE]]"}, + {eval_save_config, "save-config"} }; + +MPlist * +eval_help (MPlist *arg) +{ + int i; + + printf ("Valid FORMs are:\n"); + for (i = 0; i < (sizeof eval_functions) / (sizeof eval_functions[0]); i++) + printf (" %s\n", eval_functions[i].form); + printf (" SYMBOL -- print the value of SYMBOL\n"); + return NULL; +} + +MPlist * +eval (MPlist *plist) +{ + MSymbol cmd; + struct EvalFunc *eval_func; + + if (! SYMBOLP (plist)) + ERROR (error_invalid_format); + cmd = mplist_value (plist); + eval_func = msymbol_get (cmd, Meval_function); + if (! eval_func) + { + if (mplist_key (mplist_next (plist)) == Mnil) + { + MPlist *value = msymbol_get (cmd, Mvalue); + + if (value) + return value; + } + ERROR (error_unknown_command); + } + + return eval_func->eval_func (mplist_next (plist)); +} + +void +init (void) +{ + M17N_INIT (); + + Merror = msymbol ("error"); + Mvalue = msymbol ("value"); + + Meval_function = msymbol ("eval-function"); + msymbol_put (msymbol ("help"), Meval_function, eval_functions + 0); + msymbol_put (msymbol ("set"), Meval_function, eval_functions + 1); + msymbol_put (msymbol ("open"), Meval_function, eval_functions + 2); + msymbol_put (msymbol ("close"), Meval_function, eval_functions + 3); + msymbol_put (msymbol ("get-command"), Meval_function, eval_functions + 4); + msymbol_put (msymbol ("config-command"), Meval_function, eval_functions + 5); + msymbol_put (msymbol ("get-variable"), Meval_function, eval_functions + 6); + msymbol_put (msymbol ("config-variable"), Meval_function, eval_functions + 7); + msymbol_put (msymbol ("save-config"), Meval_function, eval_functions + 8); + + error_invalid_format = mplist (); + mplist_add (error_invalid_format, Msymbol, Merror); + mplist_add (error_invalid_format, Msymbol, msymbol ("invalid-format")); + + error_unknown_command = mplist (); + mplist_add (error_unknown_command, Msymbol, Merror); + mplist_add (error_unknown_command, Msymbol, msymbol ("unknwon-command")); + + error_invalid_arg = mplist (); + mplist_add (error_invalid_arg, Msymbol, Merror); + mplist_add (error_invalid_arg, Msymbol, msymbol ("invalid-argument")); + + error_command_fail = mplist (); + mplist_add (error_command_fail, Msymbol, Merror); + mplist_add (error_command_fail, Msymbol, msymbol ("command-fail")); + + error_lock_fail = mplist (); + mplist_add (error_lock_fail, Msymbol, Merror); + mplist_add (error_lock_fail, Msymbol, msymbol ("lock-fail")); +} + +void +fini (void) +{ + m17n_object_unref (error_invalid_format); + m17n_object_unref (error_unknown_command); + m17n_object_unref (error_invalid_arg); + m17n_object_unref (error_command_fail); + m17n_object_unref (error_lock_fail); + + M17N_FINI (); +} + +int +main () +{ + char line[256]; + + init (); + + while (printf ("> ") && fgets (line, 256, stdin) != NULL) + { + MText *mt; + MPlist *plist, *val; + int len = strlen (line); + + if (line[len - 1] == '\n') + line[--len] = '\0'; + if (len == 0) + continue; + + mt = mtext_from_data (line, len, MTEXT_FORMAT_US_ASCII); + plist = mplist_deserialize (mt); + val = eval (plist); + + if (val) + { + mdebug_dump_plist (val, 0); + putc ('\n', stderr); + fflush (stderr); + m17n_object_unref (val); + } + m17n_object_unref (plist); + m17n_object_unref (mt); + } + + fini (); + exit (0); +} diff --git a/run-im.c b/run-im.c deleted file mode 100644 index 63bbf3e..0000000 --- a/run-im.c +++ /dev/null @@ -1,346 +0,0 @@ -#include -#include -#include -#include -#include - -MPlist *error_invalid_format; -MPlist *error_unknown_command; -MPlist *error_invalid_arg; -MPlist *error_command_fail; -MPlist *error_lock_fail; - -MSymbol Merror, Mvalue; -MSymbol Meval_function; - -#define ERROR(msg) \ - do { \ - m17n_object_ref (msg); \ - return msg; \ - } while (0) - -MPlist *eval (MPlist *); - -#define INTEGERP(plist) (mplist_key (plist) == Minteger) -#define SYMBOLP(plist) (mplist_key (plist) == Msymbol) -#define MTEXTP(plist) (mplist_key (plist) == Mtext) -#define PLISTP(plist) (mplist_key (plist) == Mplist) -#define TAILP(plist) (mplist_key (plist) == Mnil) - -MPlist *eval_help (MPlist *arg); - -MPlist * -eval_set (MPlist *arg) -{ - MSymbol var; - - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - var = mplist_value (arg); - arg = mplist_next (arg); - if (TAILP (arg)) - msymbol_put (var, Mvalue, NULL); - else if (PLISTP (arg)) - msymbol_put (var, Mvalue, eval (mplist_value (arg))); - else - { - m17n_object_ref (arg); - msymbol_put (var, Mvalue, arg); - } - return NULL; -} - -MPlist * -eval_open (MPlist *arg) -{ - MSymbol lang, name; - MInputMethod *im;; - - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - lang = mplist_value (arg); - arg = mplist_next (arg); - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - name = mplist_value (arg); - im = minput_open_im (lang, name, Mnil); - arg = mplist (); - mplist_add (arg, Mt, im); - return arg; -} - -MPlist * -eval_close (MPlist *arg) -{ - MPlist *value; - - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - value = msymbol_get (mplist_value (arg), Mvalue); - if (! value) - ERROR (error_invalid_arg); - minput_close_im (mplist_value (value)); - return NULL; -} - -MPlist * -eval_get_command (MPlist *arg) -{ - MSymbol lang, name, command; - - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - lang = mplist_value (arg); - arg = mplist_next (arg); - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - name = mplist_value (arg); - arg = mplist_next (arg); - if (SYMBOLP (arg)) - command = mplist_value (arg); - else if (TAILP (arg)) - command = Mnil; - else - ERROR (error_invalid_arg); - arg = minput_get_command (lang, name, command); - if (arg) - m17n_object_ref (arg); - return arg; -} - -MPlist * -eval_config_command (MPlist *arg) -{ - MSymbol lang, name, command; - MPlist *keyseq; - - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - lang = mplist_value (arg); - arg = mplist_next (arg); - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - name = mplist_value (arg); - arg = mplist_next (arg); - if (SYMBOLP (arg)) - { - command = mplist_value (arg); - arg = mplist_next (arg); - } - else if (TAILP (arg)) - command = Mnil; - else - ERROR (error_invalid_arg); - if (PLISTP (arg)) - keyseq = arg; - else - keyseq = NULL; - if (minput_config_command (lang, name, command, keyseq) < 0) - ERROR (error_command_fail); - return NULL; -} - -MPlist * -eval_get_variable (MPlist *arg) -{ - MSymbol lang, name, variable; - - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - lang = mplist_value (arg); - arg = mplist_next (arg); - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - name = mplist_value (arg); - arg = mplist_next (arg); - if (SYMBOLP (arg)) - variable = mplist_value (arg); - else if (TAILP (arg)) - variable = Mnil; - else - ERROR (error_invalid_arg); - arg = minput_get_variable (lang, name, variable); - if (arg) - m17n_object_ref (arg); - return arg; -} - -MPlist * -eval_config_variable (MPlist *arg) -{ - MSymbol lang, name, variable; - - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - lang = mplist_value (arg); - arg = mplist_next (arg); - if (! SYMBOLP (arg)) - ERROR (error_invalid_arg); - name = mplist_value (arg); - arg = mplist_next (arg); - if (SYMBOLP (arg)) - { - variable = mplist_value (arg); - arg = mplist_next (arg); - } - else if (TAILP (arg)) - variable = Mnil; - else - ERROR (error_invalid_arg); - if (TAILP (arg)) - arg = NULL; - if (minput_config_variable (lang, name, variable, arg) < 0) - ERROR (error_command_fail); - return NULL; -} - -MPlist * -eval_save_config (MPlist *arg) -{ - int ret = minput_save_config (); - - if (ret < 0) - ERROR (error_command_fail); - if (ret == 0) - ERROR (error_lock_fail); - return NULL; -} - -struct EvalFunc { - MPlist *(*eval_func) (MPlist *arg); - char *form; -} eval_functions[] = - { {eval_help, "help"}, - {eval_set, "set SYMBOL FORM"}, - {eval_open, "open LANG NAME"}, - {eval_close, "close SYMBOL"}, - {eval_get_command, "get-command LANG NAME [COMMAND]"}, - {eval_config_command, "config-command LANG NAME [COMMAND [(KEY ...) ...]]"}, - {eval_get_variable, "get-variable LANG NAME [VARIABLE]"}, - {eval_config_variable, "config-variable LANG NAME [VARIABLE [VALUE]]"}, - {eval_save_config, "save-config"} }; - -MPlist * -eval_help (MPlist *arg) -{ - int i; - - printf ("Valid FORMs are:\n"); - for (i = 0; i < (sizeof eval_functions) / (sizeof eval_functions[0]); i++) - printf (" %s\n", eval_functions[i].form); - printf (" SYMBOL -- print the value of SYMBOL\n"); - return NULL; -} - -MPlist * -eval (MPlist *plist) -{ - MSymbol cmd; - struct EvalFunc *eval_func; - - if (! SYMBOLP (plist)) - ERROR (error_invalid_format); - cmd = mplist_value (plist); - eval_func = msymbol_get (cmd, Meval_function); - if (! eval_func) - { - if (mplist_key (mplist_next (plist)) == Mnil) - { - MPlist *value = msymbol_get (cmd, Mvalue); - - if (value) - return value; - } - ERROR (error_unknown_command); - } - - return eval_func->eval_func (mplist_next (plist)); -} - -void -init (void) -{ - M17N_INIT (); - - Merror = msymbol ("error"); - Mvalue = msymbol ("value"); - - Meval_function = msymbol ("eval-function"); - msymbol_put (msymbol ("help"), Meval_function, eval_functions + 0); - msymbol_put (msymbol ("set"), Meval_function, eval_functions + 1); - msymbol_put (msymbol ("open"), Meval_function, eval_functions + 2); - msymbol_put (msymbol ("close"), Meval_function, eval_functions + 3); - msymbol_put (msymbol ("get-command"), Meval_function, eval_functions + 4); - msymbol_put (msymbol ("config-command"), Meval_function, eval_functions + 5); - msymbol_put (msymbol ("get-variable"), Meval_function, eval_functions + 6); - msymbol_put (msymbol ("config-variable"), Meval_function, eval_functions + 7); - msymbol_put (msymbol ("save-config"), Meval_function, eval_functions + 8); - - error_invalid_format = mplist (); - mplist_add (error_invalid_format, Msymbol, Merror); - mplist_add (error_invalid_format, Msymbol, msymbol ("invalid-format")); - - error_unknown_command = mplist (); - mplist_add (error_unknown_command, Msymbol, Merror); - mplist_add (error_unknown_command, Msymbol, msymbol ("unknwon-command")); - - error_invalid_arg = mplist (); - mplist_add (error_invalid_arg, Msymbol, Merror); - mplist_add (error_invalid_arg, Msymbol, msymbol ("invalid-argument")); - - error_command_fail = mplist (); - mplist_add (error_command_fail, Msymbol, Merror); - mplist_add (error_command_fail, Msymbol, msymbol ("command-fail")); - - error_lock_fail = mplist (); - mplist_add (error_lock_fail, Msymbol, Merror); - mplist_add (error_lock_fail, Msymbol, msymbol ("lock-fail")); -} - -void -fini (void) -{ - m17n_object_unref (error_invalid_format); - m17n_object_unref (error_unknown_command); - m17n_object_unref (error_invalid_arg); - m17n_object_unref (error_command_fail); - - M17N_FINI (); -} - -int -main () -{ - char line[256]; - - init (); - - while (printf ("> ") && fgets (line, 256, stdin) != NULL) - { - MText *mt; - MPlist *plist, *val; - int len = strlen (line); - - if (line[len - 1] == '\n') - line[--len] = '\0'; - if (len == 0) - continue; - - mt = mtext_from_data (line, len, MTEXT_FORMAT_US_ASCII); - plist = mplist_deserialize (mt); - val = eval (plist); - - if (val) - { - mdebug_dump_plist (val, 0); - putc ('\n', stderr); - fflush (stderr); - m17n_object_unref (val); - } - m17n_object_unref (plist); - m17n_object_unref (mt); - } - - fini (); - exit (0); -}