poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3] libpoke, poke, testsuite: Make error handling of public API c


From: Mohammad-Reza Nabipoor
Subject: [PATCH v3] libpoke, poke, testsuite: Make error handling of public API consistent
Date: Wed, 11 Nov 2020 23:27:32 +0330

2020-11-11  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>

        * libpoke/libpoke.h (PK_ENOMEM): Define.
        (PK_EEOF): Likewise.
        (PK_EINVAL): Likewise.
        (PK_IOS_OK): Remove.
        (PK_IOS_ERROR): Likewise.
        (PK_IOS_INVAL): Define.
        (pk_errno): New prototype.
        (pk_compile_file): Use `PK_OK`/`PK_ERROR` for return value.
        (pk_compile_buffer): Likewise.
        (pk_compile_satement): Likewise.
        (pk_compile_expression): Likewise.
        (pk_load): Likewise.
        (pk_defvar): Likewise.
        (pk_call): Likewise.
        (pk_ios_open): Use `PK_IOS_INVAL` as invalid return value.
        * libpoke/libpoke.c (struct pk_compiler): Add new field: `status`.
        (PK_RETURN): Define.
        (pk_errno): New function.
        (pk_compile_file): Update `status` filed of `pk_compiler` and return
        that.
        (pk_compile_buffer): Likewise.
        (pk_compile_statement): Likewise.
        (pk_compile_expression): Likewise.
        (pk_load): Likewise.
        (pk_disassemble_function): Likewise.
        (pk_disassemble_expression): Likewise.
        (pk_defvar): Likewise.
        (pk_call): Likewise.
        (pk_set_quiet_p): Update `status` field of `pk_compiler`.
        (pk_set_lexical_cuckolding_p): Likewise.
        (pk_set_alien_toekn_fn): Likewise.
        (pk_ios_cur): Likewise.
        (pk_ios_set_cur): Likewise.
        (pk_ios_handler): Likewise.
        (pk_ios_flags): Likewise.
        (pk_ios_search): Likewise.
        (pk_ios_search_by_id): Likewise.
        (pk_ios_close): Likewise.
        (pk_ios_map): Likewise.
        (pk_decl_map): Likewise.
        (pk_decl_p): Likewise.
        (pk_decl_val): Likewise.
        (pk_obase): Likewise.
        (pk_set_obase): Likewise.
        (pk_oacutoff): Likewise.
        (pk_set_oacutoff): Likewise.
        (pk_odepth): Likewise.
        (pk_set_odepth): Likewise.
        (pk_oindent): Likewise.
        (pk_set_oindent): Likewise.
        (pk_omaps): Likewise.
        (pk_set_omaps): Likewise.
        (pk_omode): Likewise.
        (pk_set_omode): Likewise.
        (pk_error_on_warning): Likewise.
        (pk_set_error_on_warning): Likewise.
        (pk_endian): Likewise.
        (pk_set_endian): Likewise.
        (pk_nenc): Likewise.
        (pk_set_nenc): Likewise.
        (pk_pretty_print): Likewise.
        (pk_set_pretty_print): Likewise.
        (pk_print_val): Likewise.
        (pk_ios_open): Update the `status` field of `pk_compiler`. Return
        `PK_IOS_INVAL` on failure.
        * poke/pk-cmd-ios.c (pk_cmd_file): Use `PK_IOS_INVAL` instead of
        `PK_IOS_ERROR`.
        * poke/pk-ios.c (pk_open_ios): Likewise.
        (pk_cmd_mem): Likewise.
        (pk_cmd_nbd): Likewise.
        (pk_cmd_load_file): Check `pk_compile_file` against `PK_OK`.
        * poke/pk-cmd.c (pk_cmd_exec): Check `pk_compile_buffer` and
        `pk_compile_statement` against `PK_OK`.
        * poke/pk-map.c (pk_map_load_parsed_map): Check `pk_compile_buffer`
        and `pk_compile_expression` against `PK_OK`.
        * poke/poke.c (parse_args_2): Check `pk_compile_file` against
        `PK_OK`. Check `pk_open_ios` against `PK_IOS_INVAL`.
        * testsuite/poke.libpoke/values.c (compile_initial_poke_code): Use
        `PK_OK`/`PK_ERROR` as return value. Check `pk_compile_expression`
        against `PK_OK`.
        * testsuite/poke.mi-json/mi-json.c (compile_poke_expression): Likewise.
        (compile_initial_poke_code): Use `PK_OK`/`PK_ERROR` as return value.
        (test_json_file): Check `pk_compile_expression` against `PK_OK`.
---
 ChangeLog                        |  86 +++++++++++++++++++++++
 libpoke/libpoke.c                | 113 ++++++++++++++++++++++++-------
 libpoke/libpoke.h                |  40 +++++++----
 poke/pk-cmd-ios.c                |   9 +--
 poke/pk-cmd.c                    |   4 +-
 poke/pk-ios.c                    |   2 +-
 poke/pk-map.c                    |  20 +++---
 poke/poke.c                      |   8 +--
 testsuite/poke.libpoke/values.c  |   8 +--
 testsuite/poke.mi-json/mi-json.c |  22 +++---
 10 files changed, 236 insertions(+), 76 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0e2560ba..ce250ad7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,89 @@
+2020-11-11  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>
+
+       * libpoke/libpoke.h (PK_ENOMEM): Define.
+       (PK_EEOF): Likewise.
+       (PK_EINVAL): Likewise.
+       (PK_IOS_OK): Remove.
+       (PK_IOS_ERROR): Likewise.
+       (PK_IOS_INVAL): Define.
+       (pk_errno): New prototype.
+       (pk_compile_file): Use `PK_OK`/`PK_ERROR` for return value.
+       (pk_compile_buffer): Likewise.
+       (pk_compile_satement): Likewise.
+       (pk_compile_expression): Likewise.
+       (pk_load): Likewise.
+       (pk_defvar): Likewise.
+       (pk_call): Likewise.
+       (pk_ios_open): Use `PK_IOS_INVAL` as invalid return value.
+       * libpoke/libpoke.c (struct pk_compiler): Add new field: `status`.
+       (PK_RETURN): Define.
+       (pk_errno): New function.
+       (pk_compile_file): Update `status` filed of `pk_compiler` and return
+       that.
+       (pk_compile_buffer): Likewise.
+       (pk_compile_statement): Likewise.
+       (pk_compile_expression): Likewise.
+       (pk_load): Likewise.
+       (pk_disassemble_function): Likewise.
+       (pk_disassemble_expression): Likewise.
+       (pk_defvar): Likewise.
+       (pk_call): Likewise.
+       (pk_set_quiet_p): Update `status` field of `pk_compiler`.
+       (pk_set_lexical_cuckolding_p): Likewise.
+       (pk_set_alien_toekn_fn): Likewise.
+       (pk_ios_cur): Likewise.
+       (pk_ios_set_cur): Likewise.
+       (pk_ios_handler): Likewise.
+       (pk_ios_flags): Likewise.
+       (pk_ios_search): Likewise.
+       (pk_ios_search_by_id): Likewise.
+       (pk_ios_close): Likewise.
+       (pk_ios_map): Likewise.
+       (pk_decl_map): Likewise.
+       (pk_decl_p): Likewise.
+       (pk_decl_val): Likewise.
+       (pk_obase): Likewise.
+       (pk_set_obase): Likewise.
+       (pk_oacutoff): Likewise.
+       (pk_set_oacutoff): Likewise.
+       (pk_odepth): Likewise.
+       (pk_set_odepth): Likewise.
+       (pk_oindent): Likewise.
+       (pk_set_oindent): Likewise.
+       (pk_omaps): Likewise.
+       (pk_set_omaps): Likewise.
+       (pk_omode): Likewise.
+       (pk_set_omode): Likewise.
+       (pk_error_on_warning): Likewise.
+       (pk_set_error_on_warning): Likewise.
+       (pk_endian): Likewise.
+       (pk_set_endian): Likewise.
+       (pk_nenc): Likewise.
+       (pk_set_nenc): Likewise.
+       (pk_pretty_print): Likewise.
+       (pk_set_pretty_print): Likewise.
+       (pk_print_val): Likewise.
+       (pk_ios_open): Update the `status` field of `pk_compiler`. Return
+       `PK_IOS_INVAL` on failure.
+       * poke/pk-cmd-ios.c (pk_cmd_file): Use `PK_IOS_INVAL` instead of
+       `PK_IOS_ERROR`.
+       * poke/pk-ios.c (pk_open_ios): Likewise.
+       (pk_cmd_mem): Likewise.
+       (pk_cmd_nbd): Likewise.
+       (pk_cmd_load_file): Check `pk_compile_file` against `PK_OK`.
+       * poke/pk-cmd.c (pk_cmd_exec): Check `pk_compile_buffer` and
+       `pk_compile_statement` against `PK_OK`.
+       * poke/pk-map.c (pk_map_load_parsed_map): Check `pk_compile_buffer`
+       and `pk_compile_expression` against `PK_OK`.
+       * poke/poke.c (parse_args_2): Check `pk_compile_file` against
+       `PK_OK`. Check `pk_open_ios` against `PK_IOS_INVAL`.
+       * testsuite/poke.libpoke/values.c (compile_initial_poke_code): Use
+       `PK_OK`/`PK_ERROR` as return value. Check `pk_compile_expression`
+       against `PK_OK`.
+       * testsuite/poke.mi-json/mi-json.c (compile_poke_expression): Likewise.
+       (compile_initial_poke_code): Use `PK_OK`/`PK_ERROR` as return value.
+       (test_json_file): Check `pk_compile_expression` against `PK_OK`.
+
 2020-11-11  Jose E. Marchesi  <jemarch@gnu.org>
 
        * libpoke/pvm.jitter (mm,map,unmap): New instructions.
diff --git a/libpoke/libpoke.c b/libpoke/libpoke.c
index 819d99d8..7339fcde 100644
--- a/libpoke/libpoke.c
+++ b/libpoke/libpoke.c
@@ -35,11 +35,14 @@ struct pk_compiler
   pvm vm;
 
   pkl_ast_node complete_type;
+  int status;  /* Status of last API function call. Initialized with PK_OK */
 };
 
 struct pk_term_if libpoke_term_if
 __attribute__ ((visibility ("hidden")));
 
+#define PK_RETURN(code) do { return pkc->status = (code); } while (0)
+
 pk_compiler
 pk_compiler_new (const char *rtpath,
                  struct pk_term_if *term_if)
@@ -58,6 +61,7 @@ pk_compiler_new (const char *rtpath,
       if (pkc->compiler == NULL)
         goto error;
       pkc->complete_type = NULL;
+      pkc->status = PK_OK;
 
       pvm_set_compiler (pkc->vm, pkc->compiler);
     }
@@ -81,19 +85,29 @@ pk_compiler_free (pk_compiler pkc)
   free (pkc);
 }
 
+int
+pk_errno (pk_compiler pkc)
+{
+  if (pkc)
+    return pkc->status;
+  return PK_ERROR;
+}
+
 int
 pk_compile_file (pk_compiler pkc, const char *filename,
                  int *exit_status)
 {
-  return pkl_execute_file (pkc->compiler, filename,
-                           exit_status);
+  PK_RETURN (pkl_execute_file (pkc->compiler, filename, exit_status)
+                 ? PK_OK
+                 : PK_ERROR);
 }
 
 int
 pk_compile_buffer (pk_compiler pkc, const char *buffer,
                    const char **end)
 {
-  return pkl_execute_buffer (pkc->compiler, buffer, end);
+  PK_RETURN (pkl_execute_buffer (pkc->compiler, buffer, end) ? PK_OK
+                                                             : PK_ERROR);
 }
 
 int
@@ -103,12 +117,12 @@ pk_compile_statement (pk_compiler pkc, const char *buffer,
   pvm_val val;
 
   if (!pkl_execute_statement (pkc->compiler, buffer, end, &val))
-    return 0;
+    PK_RETURN (PK_ERROR);
 
   if (valp)
     *valp = val;
 
-  return 1;
+  PK_RETURN (PK_OK);
 }
 
 int
@@ -118,36 +132,39 @@ pk_compile_expression (pk_compiler pkc, const char 
*buffer,
   pvm_val val;
 
   if (!pkl_execute_expression (pkc->compiler, buffer, end, &val))
-    return 0;
+    PK_RETURN (PK_ERROR);
 
   if (valp)
     *valp = val;
 
-  return 1;
+  PK_RETURN (PK_OK);
 }
 
 int
 pk_load (pk_compiler pkc, const char *module)
 {
-  return pkl_load (pkc->compiler, module);
+  PK_RETURN (pkl_load (pkc->compiler, module) == 0 ? PK_OK : PK_ERROR);
 }
 
 void
 pk_set_quiet_p (pk_compiler pkc, int quiet_p)
 {
   pkl_set_quiet_p (pkc->compiler, quiet_p);
+  pkc->status = PK_OK;
 }
 
 void
 pk_set_lexical_cuckolding_p (pk_compiler pkc, int lexical_cuckolding_p)
 {
   pkl_set_lexical_cuckolding_p (pkc->compiler, lexical_cuckolding_p);
+  pkc->status = PK_OK;
 }
 
 void
 pk_set_alien_token_fn (pk_compiler pkc, pk_alien_token_handler_fn cb)
 {
   pkl_set_alien_token_fn (pkc->compiler, cb);
+  pkc->status = PK_OK;
 }
 
 static char *
@@ -334,7 +351,7 @@ pk_disassemble_function (pk_compiler pkc,
   if (decl == NULL
       || PKL_AST_DECL_KIND (decl) != PKL_AST_DECL_KIND_FUNC)
     /* Function not found.  */
-    return PK_ERROR;
+    PK_RETURN (PK_ERROR);
 
   val = pvm_env_lookup (runtime_env, back, over);
   program = pvm_val_cls_program (val);
@@ -344,7 +361,7 @@ pk_disassemble_function (pk_compiler pkc,
   else
     pvm_disassemble_program (program);
 
-  return PK_OK;
+  PK_RETURN (PK_OK);
 }
 
 int
@@ -362,12 +379,12 @@ pk_disassemble_expression (pk_compiler pkc, const char 
*str,
 
   if (program == NULL)
     /* Invalid expression.  */
-    return PK_ERROR;
+    PK_RETURN (PK_ERROR);
 
   if (*end != '\0')
     {
       pvm_destroy_program (program);
-      return PK_ERROR;
+      PK_RETURN (PK_ERROR);
     }
 
   if (native_p)
@@ -375,12 +392,13 @@ pk_disassemble_expression (pk_compiler pkc, const char 
*str,
   else
     pvm_disassemble_program (program);
 
-  return PK_OK;
+  PK_RETURN (PK_OK);
 }
 
 pk_ios
 pk_ios_cur (pk_compiler pkc)
 {
+  pkc->status = PK_OK;
   return (pk_ios) ios_cur ();
 }
 
@@ -389,23 +407,27 @@ pk_ios_set_cur (pk_compiler pkc, pk_ios io)
 {
   /* XXX use pkc */
   ios_set_cur ((ios) io);
+  pkc->status = PK_OK;
 }
 
 const char *
 pk_ios_handler (pk_ios io)
 {
+  pkc->status = PK_OK;
   return ios_handler ((ios) io);
 }
 
 uint64_t
 pk_ios_flags (pk_ios io)
 {
+  pkc->status = PK_OK;
   return ios_flags ((ios) io);
 }
 
 pk_ios
 pk_ios_search (pk_compiler pkc, const char *handler)
 {
+  pkc->status = PK_OK;
   /* XXX use pkc */
   return (pk_ios) ios_search (handler);
 }
@@ -413,6 +435,7 @@ pk_ios_search (pk_compiler pkc, const char *handler)
 pk_ios
 pk_ios_search_by_id (pk_compiler pkc, int id)
 {
+  pkc->status = PK_OK;
   /* XXX use pkc */
   return (pk_ios) ios_search_by_id (id);
 }
@@ -421,8 +444,25 @@ int
 pk_ios_open (pk_compiler pkc,
              const char *handler, uint64_t flags, int set_cur_p)
 {
+  int ret;
+
   /* XXX use pkc */
-  return ios_open (handler, flags, set_cur_p);
+  if ((ret = ios_open (handler, flags, set_cur_p)) >= 0)
+    return ret;
+
+  switch (ret)
+    {
+    case IOS_ERROR: pkc->status = PK_ERROR; break;
+    case IOS_ENOMEM: pkc->status = PK_ENOMEM; break;
+    case IOS_EOF: pkc->status = PK_EEOF; break;
+    case IOS_EINVAL:
+    case IOS_EOPEN:
+      pkc->status = PK_EINVAL;
+      break;
+    default:
+      pkc->status = PK_ERROR;
+    }
+  return PK_IOS_INVAL;
 }
 
 void
@@ -430,6 +470,7 @@ pk_ios_close (pk_compiler pkc, pk_ios io)
 {
   /* XXX use pkc */
   ios_close ((ios) io);
+  pkc->status = PK_OK;
 }
 
 int
@@ -470,6 +511,7 @@ pk_ios_map (pk_compiler pkc,
   struct ios_map_fn_payload payload = { cb, data };
   /* XXX use pkc */
   ios_map (my_ios_map_fn, (void *) &payload);
+  pkc->status = PK_OK;
 }
 
 struct decl_map_fn_payload
@@ -526,6 +568,8 @@ pk_decl_map (pk_compiler pkc, int kind,
   pkl_env compiler_env = pkl_get_env (pkc->compiler);
   int pkl_kind;
 
+  pkc->status = PK_OK;
+
   switch (kind)
     {
     case PK_DECL_KIND_VAR: pkl_kind = PKL_AST_DECL_KIND_VAR; break;
@@ -543,12 +587,13 @@ int
 pk_decl_p (pk_compiler pkc, const char *name, int kind)
 {
   pkl_env compiler_env = pkl_get_env (pkc->compiler);
-
   pkl_ast_node decl = pkl_env_lookup (compiler_env,
                                       PKL_ENV_NS_MAIN,
                                       name,
                                       NULL, NULL);
 
+  pkc->status = PK_OK;
+
   int pkl_kind;
   switch (kind)
     {
@@ -571,12 +616,13 @@ pk_decl_val (pk_compiler pkc, const char *name)
   pkl_env compiler_env = pkl_get_env (pkc->compiler);
   pvm_env runtime_env = pvm_get_env (pkc->vm);
   int back, over;
-
   pkl_ast_node decl = pkl_env_lookup (compiler_env,
                                       PKL_ENV_NS_MAIN,
                                       name,
                                       &back, &over);
 
+  pkc->status = PK_OK;
+
   if (decl == NULL
       || PKL_AST_DECL_KIND (decl) != PKL_AST_DECL_KIND_VAR)
     return PK_NULL;
@@ -590,10 +636,10 @@ pk_defvar (pk_compiler pkc, const char *varname, pk_val 
val)
   pvm_env runtime_env = pvm_get_env (pkc->vm);
 
   if (!pkl_defvar (pkc->compiler, varname, val))
-    return 0;
+    PK_RETURN (PK_ERROR);
   pvm_env_register (runtime_env, val);
 
-  return 1;
+  PK_RETURN (PK_OK);
 }
 
 int
@@ -608,19 +654,20 @@ pk_call (pk_compiler pkc, pk_val cls, pk_val *ret, ...)
   program = pkl_compile_call (pkc->compiler, cls, ret, ap);
   va_end (ap);
   if (!program)
-    return 0;
+    PK_RETURN (PK_ERROR);
 
   /* Run the program in the poke VM.  */
   pvm_program_make_executable (program);
   rret = pvm_run (pkc->vm, program, ret);
 
   pvm_destroy_program (program);
-  return (rret == PVM_EXIT_OK);
+  PK_RETURN (rret == PVM_EXIT_OK ? PK_OK : PK_ERROR);
 }
 
 int
 pk_obase (pk_compiler pkc)
 {
+  pkc->status = PK_OK;
   return pvm_obase (pkc->vm);
 }
 
@@ -628,22 +675,26 @@ void
 pk_set_obase (pk_compiler pkc, int obase)
 {
   pvm_set_obase (pkc->vm, obase);
+  pkc->status = PK_OK;
 }
 
 unsigned int
 pk_oacutoff (pk_compiler pkc)
 {
+  pkc->status = PK_OK;
   return pvm_oacutoff (pkc->vm);
 }
 
 void pk_set_oacutoff (pk_compiler pkc, unsigned int oacutoff)
 {
   pvm_set_oacutoff (pkc->vm, oacutoff);
+  pkc->status = PK_OK;
 }
 
 unsigned int
 pk_odepth (pk_compiler pkc)
 {
+  pkc->status = PK_OK;
   return pvm_odepth (pkc->vm);
 }
 
@@ -651,11 +702,13 @@ void
 pk_set_odepth (pk_compiler pkc, unsigned int odepth)
 {
   pvm_set_odepth (pkc->vm, odepth);
+  pkc->status = PK_OK;
 }
 
 unsigned int
 pk_oindent (pk_compiler pkc)
 {
+  pkc->status = PK_OK;
   return pvm_oindent (pkc->vm);
 }
 
@@ -663,11 +716,13 @@ void
 pk_set_oindent (pk_compiler pkc, unsigned int oindent)
 {
   pvm_set_oindent (pkc->vm, oindent);
+  pkc->status = PK_OK;
 }
 
 int
 pk_omaps (pk_compiler pkc)
 {
+  pkc->status = PK_OK;
   return pvm_omaps (pkc->vm);
 }
 
@@ -675,6 +730,7 @@ void
 pk_set_omaps (pk_compiler pkc, int omaps_p)
 {
   pvm_set_omaps (pkc->vm, omaps_p);
+  pkc->status = PK_OK;
 }
 
 enum pk_omode
@@ -689,7 +745,7 @@ pk_omode (pk_compiler pkc)
     default:
       assert (0);
     }
-
+  pkc->status = PK_OK;
   return omode;
 }
 
@@ -705,13 +761,14 @@ pk_set_omode (pk_compiler pkc, enum pk_omode omode)
     default:
       assert (0);
     }
-
   pvm_set_omode (pkc->vm, mode);
+  pkc->status = PK_OK;
 }
 
 int
 pk_error_on_warning (pk_compiler pkc)
 {
+  pkc->status = PK_OK;
   return pkl_error_on_warning (pkc->compiler);
 }
 
@@ -719,6 +776,7 @@ void
 pk_set_error_on_warning (pk_compiler pkc, int error_on_warning_p)
 {
   pkl_set_error_on_warning (pkc->compiler, error_on_warning_p);
+  pkc->status = PK_OK;
 }
 
 enum pk_endian
@@ -733,7 +791,7 @@ pk_endian (pk_compiler pkc)
     default:
       assert (0);
     }
-
+  pkc->status = PK_OK;
   return endian;
 }
 
@@ -749,8 +807,8 @@ pk_set_endian (pk_compiler pkc, enum pk_endian endian)
     default:
       assert (0);
     }
-
   pvm_set_endian (pkc->vm, ios_endian);
+  pkc->status = PK_OK;
 }
 
 enum pk_nenc
@@ -765,7 +823,7 @@ pk_nenc (pk_compiler pkc)
     default:
       assert (0);
     }
-
+  pkc->status = PK_OK;
   return nenc;
 }
 
@@ -781,13 +839,14 @@ pk_set_nenc (pk_compiler pkc, enum pk_nenc nenc)
     default:
       assert (0);
     }
-
   pvm_set_nenc (pkc->vm, ios_nenc);
+  pkc->status = PK_OK;
 }
 
 int
 pk_pretty_print (pk_compiler pkc)
 {
+  pkc->status = PK_OK;
   return pvm_pretty_print (pkc->vm);
 }
 
@@ -795,10 +854,12 @@ void
 pk_set_pretty_print (pk_compiler pkc, int pretty_print_p)
 {
   pvm_set_pretty_print (pkc->vm, pretty_print_p);
+  pkc->status = PK_OK;
 }
 
 void
 pk_print_val (pk_compiler pkc, pk_val val)
 {
   pvm_print_val (pkc->vm, val);
+  pkc->status = PK_OK;
 }
diff --git a/libpoke/libpoke.h b/libpoke/libpoke.h
index 96ea0da4..50a1e05b 100644
--- a/libpoke/libpoke.h
+++ b/libpoke/libpoke.h
@@ -37,6 +37,9 @@ typedef uint64_t pk_val;
 
 #define PK_OK 0
 #define PK_ERROR 1
+#define PK_ENOMEM 2
+#define PK_EEOF 3
+#define PK_EINVAL 4
 
 /* Terminal output callbacks.  */
 
@@ -90,13 +93,22 @@ pk_compiler pk_compiler_new (const char *rtpath,
 
 void pk_compiler_free (pk_compiler pkc) LIBPOKE_API;
 
+/* Error code of last error.
+
+   If PKC is not NULL, return the status of the last API function invocation.
+   One of the following values:
+     PK_OK, PK_ERROR, PK_ENOMEM, PK_EEOF, PK_EINVAL
+   Otherwise, return PK_ERROR.  */
+
+int pk_errno (pk_compiler pkc) LIBPOKE_API;
+
 /* Compile a Poke program from the given file FILENAME.
 
    If not NULL, *EXIT_STATUS is set to the status resulting from the
    execution of the program.
 
-   Return 0 in case of a compilation error, a non-zero value
-   otherwise.  */
+   Return PK_ERROR in case of a compilation error.  Otherwise,
+   return PK_OK.  */
 
 int pk_compile_file (pk_compiler pkc, const char *filename,
                      int *exit_status) LIBPOKE_API;
@@ -108,8 +120,8 @@ int pk_compile_file (pk_compiler pkc, const char *filename,
    If not NULL, *END is set to the first character in BUFFER that is
    not part of the compiled entity.
 
-   Return 0 in case of a compilation error, a non-zero value
-   otherwise.  */
+   Return PK_ERROR in case of a compilation error.  Otherwise,
+   return PK_OK.  */
 
 int pk_compile_buffer (pk_compiler pkc, const char *buffer,
                        const char **end) LIBPOKE_API;
@@ -135,8 +147,8 @@ int pk_compile_expression (pk_compiler pkc, const char 
*buffer,
 
 /* Load a module using the given compiler.
 
-   If the module cannot be loaded, return 1.
-   Otherwise, return 0.  */
+   If the module cannot be loaded, return PK_ERROR.  Otherwise,
+   return PK_OK.  */
 
 int pk_load (pk_compiler pkc, const char *module) LIBPOKE_API;
 
@@ -268,9 +280,10 @@ uint64_t pk_ios_size (pk_ios ios) LIBPOKE_API;
 uint64_t pk_ios_flags (pk_ios ios) LIBPOKE_API;
 
 /* Open an IO space using a handler and if set_cur is set to 1, make
-   the newly opened IO space the current space.  Return PK_IOS_ERROR
+   the newly opened IO space the current space.  Return PK_IOS_INVAL
    if there is an error opening the space (such as an unrecognized
-   handler), the ID of the new IOS otherwise.
+   handler). Otherwise, the ID of the new IOS.
+   The error code can be retrieved by PK_ERRNO function.
 
    FLAGS is a bitmask.  The least significant 32 bits are reserved for
    common flags (the PK_IOS_F_* above).  The most significant 32 bits
@@ -279,8 +292,7 @@ uint64_t pk_ios_flags (pk_ios ios) LIBPOKE_API;
    If no PK_IOS_F_READ or PK_IOS_F_WRITE flags are specified, then the
    IOS will be opened in whatever mode makes more sense.  */
 
-#define PK_IOS_OK     0
-#define PK_IOS_ERROR -1
+#define PK_IOS_INVAL (-1)
 
 int pk_ios_open (pk_compiler pkc,
                  const char *handler, uint64_t flags, int set_cur_p) 
LIBPOKE_API;
@@ -336,8 +348,8 @@ pk_val pk_decl_val (pk_compiler pkc, const char *name) 
LIBPOKE_API;
 /* Declare a variable in the global environment of the given
    incremental compiler.
 
-   If the operation is successful, return 1.  If a variable with name
-   VARNAME already exists in the environment, return 0.  */
+   If the operation is successful, return PK_OK.  If a variable with name
+   VARNAME already exists in the environment, return PK_ERROR.  */
 
 int pk_defvar (pk_compiler pkc, const char *varname, pk_val val) LIBPOKE_API;
 
@@ -351,9 +363,9 @@ int pk_defvar (pk_compiler pkc, const char *varname, pk_val 
val) LIBPOKE_API;
    A variable number of function arguments follow, terminated by
    PK_NULL.
 
-   Return 0 if there is a problem performing the operation, or if the
+   Return PK_ERROR if there is a problem performing the operation, or if the
    execution of the function results in an unhandled exception.
-   Return 1 otherwise.  */
+   Return PK_OK otherwise.  */
 
 int pk_call (pk_compiler pkc, pk_val cls, pk_val *ret, ...)
   __attribute__ ((sentinel)) LIBPOKE_API;
diff --git a/poke/pk-cmd-ios.c b/poke/pk-cmd-ios.c
index 0e706fc2..19a26275 100644
--- a/poke/pk-cmd-ios.c
+++ b/poke/pk-cmd-ios.c
@@ -88,7 +88,7 @@ pk_cmd_file (int argc, struct pk_cmd_arg argv[], uint64_t 
uflags)
       return 0;
     }
 
-  if (PK_IOS_ERROR == pk_open_ios (filename, 1 /* set_cur_p */))
+  if (PK_IOS_INVAL == pk_open_ios (filename, 1 /* set_cur_p */))
     {
       pk_term_class ("error");
       pk_puts ("error: ");
@@ -240,7 +240,8 @@ pk_cmd_load_file (int argc, struct pk_cmd_arg argv[], 
uint64_t uflags)
   else
     goto no_file;
 
-  if (!pk_compile_file (poke_compiler, filename, NULL /* exit_status */))
+  if (pk_compile_file (poke_compiler, filename, NULL /* exit_status */)
+      != PK_OK)
     /* Note that the compiler emits its own error messages.  */
     goto error;
 
@@ -282,7 +283,7 @@ pk_cmd_mem (int argc, struct pk_cmd_arg argv[], uint64_t 
uflags)
       return 0;
     }
 
-  if (PK_IOS_ERROR == pk_ios_open (poke_compiler, mem_name, 0, 1))
+  if (PK_IOS_INVAL == pk_ios_open (poke_compiler, mem_name, 0, 1))
     {
       pk_printf (_("Error creating memory IOS %s\n"), mem_name);
       free (mem_name);
@@ -319,7 +320,7 @@ pk_cmd_nbd (int argc, struct pk_cmd_arg argv[], uint64_t 
uflags)
       return 0;
     }
 
-  if (PK_IOS_ERROR == pk_ios_open (poke_compiler, nbd_name, 0, 1))
+  if (PK_IOS_INVAL == pk_ios_open (poke_compiler, nbd_name, 0, 1))
     {
       pk_printf (_("Error creating NBD IOS %s\n"), nbd_name);
       free (nbd_name);
diff --git a/poke/pk-cmd.c b/poke/pk-cmd.c
index 9d4132c7..c53db6ec 100644
--- a/poke/pk-cmd.c
+++ b/poke/pk-cmd.c
@@ -611,7 +611,7 @@ pk_cmd_exec (const char *str)
       if (what == 0)
         {
           /* Declaration.  */
-          if (!pk_compile_buffer (poke_compiler, ecmd, &end))
+          if (pk_compile_buffer (poke_compiler, ecmd, &end) != PK_OK)
             {
               retval = 0;
               goto cleanup;
@@ -622,7 +622,7 @@ pk_cmd_exec (const char *str)
           /* Statement.  */
           pk_val val;
 
-          if (!pk_compile_statement (poke_compiler, ecmd, &end, &val))
+          if (pk_compile_statement (poke_compiler, ecmd, &end, &val) != PK_OK)
             {
               retval = 0;
               goto cleanup;
diff --git a/poke/pk-ios.c b/poke/pk-ios.c
index 0a3caafc..470ece5a 100644
--- a/poke/pk-ios.c
+++ b/poke/pk-ios.c
@@ -30,7 +30,7 @@ pk_open_ios (const char *handler, int set_cur_p)
   int ios_id;
 
   ios_id = pk_ios_open (poke_compiler, handler, 0, 1);
-  if (ios_id == PK_IOS_ERROR)
+  if (ios_id == PK_IOS_INVAL)
     return ios_id;
 
   if (poke_auto_map_p)
diff --git a/poke/pk-map.c b/poke/pk-map.c
index 08e96ae8..980cb285 100644
--- a/poke/pk-map.c
+++ b/poke/pk-map.c
@@ -469,9 +469,9 @@ pk_map_load_parsed_map (int ios_id, const char *mapname,
   /* First, compile the prologue.  */
   /* XXX set error location and disable verbose error messages in
      poke_compiler.  */
-  if (!pk_compile_buffer (poke_compiler,
-                          PK_MAP_PARSED_MAP_PROLOGUE (map),
-                          NULL))
+  if (pk_compile_buffer (poke_compiler,
+                         PK_MAP_PARSED_MAP_PROLOGUE (map),
+                         NULL) != PK_OK)
     return 0;
 
   /* Process the map entries and create the mapped global
@@ -489,10 +489,10 @@ pk_map_load_parsed_map (int ios_id, const char *mapname,
       if (condition)
         {
           /* XXX set error location... */
-          if (!pk_compile_expression (poke_compiler,
-                                      condition,
-                                      NULL /* end */,
-                                      &val))
+          if (pk_compile_expression (poke_compiler,
+                                     condition,
+                                     NULL /* end */,
+                                     &val) != PK_OK)
             goto error;
 
           if (pk_type_code (pk_typeof (val)) != PK_INT
@@ -525,9 +525,9 @@ pk_map_load_parsed_map (int ios_id, const char *mapname,
                              " = ", type, " @ ", offset, ";", NULL);
 
           /* XXX what about constraints?  */
-          if (!pk_compile_buffer (poke_compiler,
-                                  defvar_str,
-                                  NULL /* end */))
+          if (pk_compile_buffer (poke_compiler,
+                                 defvar_str,
+                                 NULL /* end */) != PK_OK)
             goto error;
         }
     }
diff --git a/poke/poke.c b/poke/poke.c
index d94ea603..4906fb88 100644
--- a/poke/poke.c
+++ b/poke/poke.c
@@ -409,8 +409,8 @@ parse_args_2 (int argc, char *argv[])
           break;
         case 'l':
         case LOAD_ARG:
-          if (!pk_compile_file (poke_compiler, optarg,
-                                NULL /* exit_status */))
+          if (pk_compile_file (poke_compiler, optarg,
+                               NULL /* exit_status */) != PK_OK)
             goto exit_success;
           break;
         case 'c':
@@ -437,7 +437,7 @@ parse_args_2 (int argc, char *argv[])
                command-line arguments.  Then execute the script and
                return.  */
             set_script_args (argc, argv);
-            if (!pk_compile_file (poke_compiler, optarg, &exit_status))
+            if (pk_compile_file (poke_compiler, optarg, &exit_status) != PK_OK)
               goto exit_success;
 
             finalize ();
@@ -465,7 +465,7 @@ parse_args_2 (int argc, char *argv[])
       int xxx = poke_auto_map_p;
 
       poke_auto_map_p = 0; /* XXX */
-      if (pk_open_ios (filename, 1 /* set_cur_p */) == PK_IOS_ERROR)
+      if (pk_open_ios (filename, 1 /* set_cur_p */) == PK_IOS_INVAL)
         {
           if (!poke_quiet_p)
             pk_printf (_("cannot open file %s\n"), filename);
diff --git a/testsuite/poke.libpoke/values.c b/testsuite/poke.libpoke/values.c
index 2fe3cd75..d0ea8a0c 100644
--- a/testsuite/poke.libpoke/values.c
+++ b/testsuite/poke.libpoke/values.c
@@ -70,7 +70,7 @@ compile_initial_poke_code (FILE *ifp, pk_compiler pkc)
   ssize_t nread, s_nread = 0;
   char *line = NULL, *poke_code = NULL;
   size_t len = 0;
-  int error = 1;
+  int error = PK_OK;
 
   while (1)
     {
@@ -179,8 +179,8 @@ compile_poke_expressions (FILE *ifp, pk_compiler pkc, 
pk_val *val1,
       copy_line_to_expression (&expr2, line, s_read);
     }
 
-  if (pk_compile_expression (pkc, (const char *) expr1, NULL, val1) == 0
-      || pk_compile_expression (pkc, (const char *) expr2, NULL, val2) == 0)
+  if (pk_compile_expression (pkc, (const char *) expr1, NULL, val1) != PK_OK
+      || pk_compile_expression (pkc, (const char *) expr2, NULL, val2) != 
PK_OK)
     goto error;
 
   free (expr1);
@@ -214,7 +214,7 @@ test_pk_equal_file (const char *filename, FILE *ifp)
   if (!pkc)
     goto error;
 
-  if (compile_initial_poke_code (ifp, pkc) == 0)
+  if (compile_initial_poke_code (ifp, pkc) != PK_OK)
     goto error;
 
   if (compile_poke_expressions (ifp, pkc, &val1, &val2) == 0)
diff --git a/testsuite/poke.mi-json/mi-json.c b/testsuite/poke.mi-json/mi-json.c
index 8e23aa95..c67c7eee 100644
--- a/testsuite/poke.mi-json/mi-json.c
+++ b/testsuite/poke.mi-json/mi-json.c
@@ -387,7 +387,7 @@ compile_initial_poke_code (FILE *ifp, pk_compiler pkc)
   ssize_t nread, s_nread = 0;
   char *line = NULL, *poke_code = NULL;
   size_t len = 0;
-  int error = 1;
+  int error = PK_OK;
 
   while (1)
     {
@@ -395,7 +395,7 @@ compile_initial_poke_code (FILE *ifp, pk_compiler pkc)
       /* That should not happen on a correct file.
          We should prolly check ferror (ifp), postpone it for now.  */
       if (nread == -1)
-        return 0;
+        return PK_ERROR;
 
       /* If we reached the next section of the file, break.  */
       if (nread == 3 && line[0] == '#' && line[1] == '#')
@@ -439,23 +439,23 @@ compile_poke_expression (FILE *ifp, pk_compiler pkc, 
pk_val *val)
     {
       nread = getline (&line, &len, ifp);
       if (nread == -1)
-          return 0;
+        return PK_ERROR;
 
       if (nread == 3 && line[0] == '#' && line[1] == '#')
-          break;
+        break;
 
       line[nread - 1] = '\0';
 
-      if (pk_compile_expression (pkc, (const char *) line, NULL, val) == 0)
+      if (pk_compile_expression (pkc, (const char *)line, NULL, val) != PK_OK)
         goto error;
     }
 
   free (line);
-  return 1;
+  return PK_OK;
 
-  error:
-    free (line);
-    return 0;
+error:
+  free (line);
+  return PK_ERROR;
 }
 
 const char *
@@ -562,10 +562,10 @@ test_json_file (const char *filename, FILE *ifp)
   if (pkc == NULL)
     goto error;
 
-  if (compile_initial_poke_code (ifp, pkc) == 0)
+  if (compile_initial_poke_code (ifp, pkc) != PK_OK)
     goto error;
 
-  if (compile_poke_expression (ifp, pkc, &val) == 0)
+  if (compile_poke_expression (ifp, pkc, &val) != PK_OK)
     goto error;
 
   /* Right now, pk_arrays' boffsets are not correctly calculated unless
-- 
2.29.2



reply via email to

[Prev in Thread] Current Thread [Next in Thread]