poke-devel
[Top][All Lists]
Advanced

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

[PATCH 4/6] poke: Add new function pk_assert_alloc


From: Tim Rühsen
Subject: [PATCH 4/6] poke: Add new function pk_assert_alloc
Date: Thu, 7 May 2020 12:51:05 +0200

2020-05-07  Tim Rühsen  <address@hidden>

        * poke/poke.h: New function pk_assert_alloc.
        Include stdlib.h, gettext.h and pk-term.h.
        * poke/poke.c (initialize_user): Make use of pk_assert_alloc.
        Remove including gettext.h and pk-term.h.
---
 ChangeLog   |  7 +++++++
 poke/poke.c | 21 +++------------------
 poke/poke.h | 16 ++++++++++++++++
 3 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/poke/poke.c b/poke/poke.c
index aa82448c..f17de640 100644
--- a/poke/poke.c
+++ b/poke/poke.c
@@ -21,8 +21,6 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <gettext.h>
-#define _(str) dgettext (PACKAGE, str)
 #include <unistd.h>
 #include <string.h>
 #include <locale.h>
@@ -36,7 +34,6 @@
 #include "poke.h"
 #include "pk-cmd.h"
 #include "pk-repl.h"
-#include "pk-term.h"
 #include "pk-utils.h"

 /* poke can be run either interactively (from a tty) or in batch mode.
@@ -446,11 +443,7 @@ initialize_user (void)
   if (homedir != NULL)
     {
       char *pokerc = pk_str_concat (homedir, "/.pokerc", NULL);
-      if (!pokerc)
-        {
-          pk_printf (_("out of memory\n"));
-          exit (EXIT_FAILURE);
-        }
+      pk_assert_alloc (pokerc);

       if (pk_file_readable (pokerc) == NULL)
         {
@@ -484,11 +477,7 @@ initialize_user (void)
       xdg_config_dirs = "/etc/xdg";

     char *config_path = pk_str_concat (xdg_config_dirs, ":", xdg_config_home, 
NULL);
-    if (!config_path)
-      {
-        pk_printf (_("out of memory\n"));
-        exit (EXIT_FAILURE);
-      }
+    pk_assert_alloc (config_path);

     char *dir = strtok (config_path, ":");
     do
@@ -500,11 +489,7 @@ initialize_user (void)
         /* Mount the full path and determine whether the resulting
            file is readable. */
         char *config_filename = pk_str_concat (dir, "/poke/pokerc.conf", NULL);
-        if (!config_filename)
-          {
-            pk_printf (_("out of memory\n"));
-            exit (EXIT_FAILURE);
-          }
+        pk_assert_alloc (config_filename);

         if (pk_file_readable (config_filename) == NULL)
           {
diff --git a/poke/poke.h b/poke/poke.h
index d988cd6f..f9f58a0e 100644
--- a/poke/poke.h
+++ b/poke/poke.h
@@ -20,6 +20,12 @@
 #define POKE_H

 #include <config.h>
+
+#include <stdlib.h> /* EXIT_FAILURE */
+#include <gettext.h>
+#define _(str) dgettext (PACKAGE, str)
+
+#include "pk-term.h"
 #include "libpoke.h"

 extern int poke_interactive_p;
@@ -39,4 +45,14 @@ extern char *poke_doc_viewer;

 void pk_print_version (void);

+static inline void
+pk_assert_alloc(const void *m)
+{
+  if (!m)
+    {
+      pk_printf (_("out of memory\n"));
+      exit (EXIT_FAILURE);
+    }
+}
+
 #endif /* !POKE_H */
--
2.26.2




reply via email to

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