ratpoison-devel
[Top][All Lists]
Advanced

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

[RP] [PATCH 2/3] Move several functions out of main.c into globals.c


From: Will Storey
Subject: [RP] [PATCH 2/3] Move several functions out of main.c into globals.c
Date: Sun, 27 Aug 2017 12:06:11 -0700

These functions are used in many parts of ratpoison. In order to compile
a separate program using the ratpoison files, we need them available
outside of main.c.

I want to create such a program for a set of unit tests.
---
 src/globals.c   | 317 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/globals.h   |  16 +++
 src/main.c      | 312 -------------------------------------------------------
 src/ratpoison.h |  17 ---
 4 files changed, 333 insertions(+), 329 deletions(-)

diff --git a/src/globals.c b/src/globals.c
index 621dcb6..c5166bd 100644
--- a/src/globals.c
+++ b/src/globals.c
@@ -20,6 +20,12 @@
 
 #include "ratpoison.h"
 
+#include <ctype.h>
+#include <errno.h>
+#include <pwd.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 int alarm_signalled = 0;
@@ -342,3 +348,314 @@ rp_text_width (rp_screen *s, char *string, int count)
 #endif
 }
 
+void
+fatal (const char *msg)
+{
+  fprintf (stderr, "ratpoison: %s", msg);
+  abort ();
+}
+
+void *
+xmalloc (size_t size)
+{
+  void *value;
+
+  value = malloc (size);
+  if (value == NULL)
+    fatal ("Virtual memory exhausted");
+  return value;
+}
+
+void *
+xrealloc (void *ptr, size_t size)
+{
+  void *value;
+
+  value = realloc (ptr, size);
+  if (value == NULL)
+    fatal ("Virtual memory exhausted");
+  return value;
+}
+
+char *
+xstrdup (const char *s)
+{
+  char *value;
+  value = strdup (s);
+  if (value == NULL)
+    fatal ("Virtual memory exhausted");
+  return value;
+}
+
+/* Return a new string based on fmt. */
+char *
+xvsprintf (char *fmt, va_list ap)
+{
+  int size, nchars;
+  char *buffer;
+  va_list ap_copy;
+
+  /* A reasonable starting value. */
+  size = strlen (fmt) + 1;
+  buffer = xmalloc (size);
+
+  while (1)
+    {
+#if defined(va_copy)
+      va_copy (ap_copy, ap);
+#elif defined(__va_copy)
+      __va_copy (ap_copy, ap);
+#else
+      /* If there is no copy macro then this MAY work. On some systems
+         this could fail because va_list is a pointer so assigning one
+         to the other as below wouldn't make a copy of the data, but
+         just the pointer to the data. */
+      ap_copy = ap;
+#endif
+      nchars = vsnprintf (buffer, size, fmt, ap_copy);
+#if defined(va_copy) || defined(__va_copy)
+      va_end (ap_copy);
+#endif
+
+      if (nchars > -1 && nchars < size)
+        return buffer;
+      else if (nchars > -1)
+        size = nchars + 1;
+      /* c99 says -1 is an error other than truncation,
+       * which thus will not go away with a larger buffer.
+       * To support older system but not making errors fatal
+       * (ratpoison will abort when trying to get too much memory otherwise),
+       * try to increase a bit but not too much: */
+      else if (size < MAX_LEGACY_SNPRINTF_SIZE)
+        size *= 2;
+      else
+       {
+         free(buffer);
+         break;
+       }
+
+      /* Resize the buffer and try again. */
+      buffer = xrealloc (buffer, size);
+    }
+
+  return xstrdup("<FAILURE>");
+}
+
+/* Return a new string based on fmt. */
+char *
+xsprintf (char *fmt, ...)
+{
+  char *buffer;
+  va_list ap;
+
+  va_start (ap, fmt);
+  buffer = xvsprintf (fmt, ap);
+  va_end (ap);
+
+  return buffer;
+}
+
+/* strtok but do it for whitespace and be locale compliant. */
+char *
+strtok_ws (char *s)
+{
+  char *nonws;
+  static char *last = NULL;
+
+  if (s != NULL)
+    last = s;
+  else if (last == NULL)
+    {
+      PRINT_ERROR (("strtok_ws() called but not initalized, this is a 
*BUG*\n"));
+      abort();
+    }
+
+  /* skip to first non-whitespace char. */
+  while (*last && isspace ((unsigned char)*last))
+    last++;
+
+  /* If we reached the end of the string here then there is no more
+     data. */
+  if (*last == '\0')
+    return NULL;
+
+  /* Now skip to the end of the data. */
+  nonws = last;
+  while (*last && !isspace ((unsigned char)*last))
+    last++;
+  if (*last)
+    {
+      *last = '\0';
+      last++;
+    }
+  return nonws;
+}
+
+/* A case insensitive strncmp. */
+int
+str_comp (char *s1, char *s2, size_t len)
+{
+  size_t i;
+
+  for (i = 0; i < len; i++)
+    if (toupper ((unsigned char)s1[i]) != toupper ((unsigned char)s2[i]))
+      return 0;
+
+  return 1;
+}
+
+/* Check for child processes that have quit but haven't been
+   acknowledged yet. Update their structure. */
+void
+check_child_procs (void)
+{
+  rp_child_info *cur;
+  int pid, status;
+  while (1)
+    {
+      pid = waitpid (WAIT_ANY, &status, WNOHANG);
+      if (pid <= 0)
+        break;
+
+      PRINT_DEBUG(("Child status: %d\n", WEXITSTATUS (status)));
+
+      /* Find the child and update its structure. */
+      list_for_each_entry (cur, &rp_children, node)
+        {
+          if (cur->pid == pid)
+            {
+              cur->terminated = 1;
+              cur->status = WEXITSTATUS (status);
+              break;
+            }
+        }
+
+      chld_signalled = 1;
+    }
+}
+
+void
+chld_handler (int signum UNUSED)
+{
+  int serrno;
+
+  serrno = errno;
+  check_child_procs();
+  errno = serrno;
+}
+
+void
+set_sig_handler (int sig, void (*action)(int))
+{
+  struct sigaction act;
+
+  memset (&act, 0, sizeof (act));
+  act.sa_handler = action;
+  sigemptyset (&act.sa_mask);
+  if (sigaction (sig, &act, NULL))
+    {
+      PRINT_ERROR (("Error setting signal handler: %s\n",
+                    strerror (errno)));
+    }
+}
+
+void
+set_close_on_exec (int fd)
+{
+  int flags = fcntl (fd, F_GETFD);
+  if (flags >= 0)
+    fcntl (fd, F_SETFD, flags | FD_CLOEXEC);
+}
+
+void
+read_rc_file (FILE *file)
+{
+  char *line;
+  size_t linesize = 256;
+
+  line = xmalloc (linesize);
+
+  while (getline (&line, &linesize, file) != -1)
+    {
+      line[strcspn (line, "\n")] = '\0';
+
+      PRINT_DEBUG (("rcfile line: %s\n", line));
+
+      if (*line != '\0' && *line != '#')
+        {
+          cmdret *result;
+          result = command (0, line);
+
+          /* Gobble the result. */
+          if (result)
+            cmdret_free (result);
+        }
+    }
+
+  free (line);
+}
+
+const char *
+get_homedir (void)
+{
+  char *homedir;
+
+  homedir = getenv ("HOME");
+  if (homedir != NULL && homedir[0] == '\0')
+    homedir = NULL;
+
+#if defined (HAVE_PWD_H) && defined (HAVE_GETPWUID)
+  if (homedir == NULL)
+    {
+      struct passwd *pw;
+
+      pw = getpwuid (getuid ());
+      if (pw != NULL)
+        homedir = pw->pw_dir;
+
+      if (homedir != NULL && homedir[0] == '\0')
+        homedir = NULL;
+    }
+#endif
+
+  return homedir;
+}
+
+void
+clean_up (void)
+{
+  rp_screen *cur;
+  struct list_head *iter, *tmp;
+
+  history_save ();
+
+  free_keymaps ();
+  free_aliases ();
+  free_user_commands ();
+  free_bar ();
+  free_window_stuff ();
+  free_groups ();
+
+  list_for_each_safe_entry (cur, iter, tmp, &rp_screens, node)
+    {
+      list_del (&cur->node);
+      screen_free (cur);
+      free (cur);
+    }
+
+  screen_free_final ();
+
+  /* Delete the undo histories */
+  clear_frame_undos ();
+
+  /* Free the global frame numset shared by all screens. */
+  numset_free (rp_frame_numset);
+
+#ifndef USE_XFT_FONT
+  XFreeFontSet (dpy, defaults.font);
+#endif
+  free (defaults.window_fmt);
+
+  XSetInputFocus (dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
+  XCloseDisplay (dpy);
+}
diff --git a/src/globals.h b/src/globals.h
index 144273d..3851695 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -210,4 +210,20 @@ char *get_selection (void);
 void rp_draw_string (rp_screen *s, Drawable d, int style, int x, int y, char 
*string, int length);
 int rp_text_width (rp_screen *s, char *string, int count);
 
+void fatal (const char *msg);
+void *xmalloc (size_t size);
+void *xrealloc (void *ptr, size_t size);
+char *xstrdup (const char *s);
+char *xvsprintf (char *fmt, va_list ap);
+char *xsprintf (char *fmt, ...);
+char *strtok_ws (char *s);
+int str_comp (char *s1, char *s2, size_t len);
+void check_child_procs (void);
+void chld_handler (int signum);
+void set_sig_handler (int sig, void (*action)(int));
+void set_close_on_exec (int fd);
+void read_rc_file (FILE *file);
+const char *get_homedir (void);
+void clean_up (void);
+
 #endif
diff --git a/src/main.c b/src/main.c
index da6fb9f..455a682 100644
--- a/src/main.c
+++ b/src/main.c
@@ -64,162 +64,6 @@ static struct option ratpoison_longopts[] =
 
 static char ratpoison_opts[] = "hvic:d:s:f:";
 
-void
-fatal (const char *msg)
-{
-  fprintf (stderr, "ratpoison: %s", msg);
-  abort ();
-}
-
-void *
-xmalloc (size_t size)
-{
-  void *value;
-
-  value = malloc (size);
-  if (value == NULL)
-    fatal ("Virtual memory exhausted");
-  return value;
-}
-
-void *
-xrealloc (void *ptr, size_t size)
-{
-  void *value;
-
-  value = realloc (ptr, size);
-  if (value == NULL)
-    fatal ("Virtual memory exhausted");
-  return value;
-}
-
-char *
-xstrdup (const char *s)
-{
-  char *value;
-  value = strdup (s);
-  if (value == NULL)
-    fatal ("Virtual memory exhausted");
-  return value;
-}
-
-/* Return a new string based on fmt. */
-char *
-xvsprintf (char *fmt, va_list ap)
-{
-  int size, nchars;
-  char *buffer;
-  va_list ap_copy;
-
-  /* A reasonable starting value. */
-  size = strlen (fmt) + 1;
-  buffer = xmalloc (size);
-
-  while (1)
-    {
-#if defined(va_copy)
-      va_copy (ap_copy, ap);
-#elif defined(__va_copy)
-      __va_copy (ap_copy, ap);
-#else
-      /* If there is no copy macro then this MAY work. On some systems
-         this could fail because va_list is a pointer so assigning one
-         to the other as below wouldn't make a copy of the data, but
-         just the pointer to the data. */
-      ap_copy = ap;
-#endif
-      nchars = vsnprintf (buffer, size, fmt, ap_copy);
-#if defined(va_copy) || defined(__va_copy)
-      va_end (ap_copy);
-#endif
-
-      if (nchars > -1 && nchars < size)
-        return buffer;
-      else if (nchars > -1)
-        size = nchars + 1;
-      /* c99 says -1 is an error other than truncation,
-       * which thus will not go away with a larger buffer.
-       * To support older system but not making errors fatal
-       * (ratpoison will abort when trying to get too much memory otherwise),
-       * try to increase a bit but not too much: */
-      else if (size < MAX_LEGACY_SNPRINTF_SIZE)
-        size *= 2;
-      else
-       {
-         free(buffer);
-         break;
-       }
-
-      /* Resize the buffer and try again. */
-      buffer = xrealloc (buffer, size);
-    }
-
-  return xstrdup("<FAILURE>");
-}
-
-/* Return a new string based on fmt. */
-char *
-xsprintf (char *fmt, ...)
-{
-  char *buffer;
-  va_list ap;
-
-  va_start (ap, fmt);
-  buffer = xvsprintf (fmt, ap);
-  va_end (ap);
-
-  return buffer;
-}
-
-/* strtok but do it for whitespace and be locale compliant. */
-char *
-strtok_ws (char *s)
-{
-  char *nonws;
-  static char *last = NULL;
-
-  if (s != NULL)
-    last = s;
-  else if (last == NULL)
-    {
-      PRINT_ERROR (("strtok_ws() called but not initalized, this is a 
*BUG*\n"));
-      abort();
-    }
-
-  /* skip to first non-whitespace char. */
-  while (*last && isspace ((unsigned char)*last))
-    last++;
-
-  /* If we reached the end of the string here then there is no more
-     data. */
-  if (*last == '\0')
-    return NULL;
-
-  /* Now skip to the end of the data. */
-  nonws = last;
-  while (*last && !isspace ((unsigned char)*last))
-    last++;
-  if (*last)
-    {
-      *last = '\0';
-      last++;
-    }
-  return nonws;
-}
-
-/* A case insensitive strncmp. */
-int
-str_comp (char *s1, char *s2, size_t len)
-{
-  size_t i;
-
-  for (i = 0; i < len; i++)
-    if (toupper ((unsigned char)s1[i]) != toupper ((unsigned char)s2[i]))
-      return 0;
-
-  return 1;
-}
-
 static void
 sighandler (int signum UNUSED)
 {
@@ -238,46 +82,6 @@ alrm_handler (int signum UNUSED)
   alarm_signalled++;
 }
 
-/* Check for child processes that have quit but haven't been
-   acknowledged yet. Update their structure. */
-void
-check_child_procs (void)
-{
-  rp_child_info *cur;
-  int pid, status;
-  while (1)
-    {
-      pid = waitpid (WAIT_ANY, &status, WNOHANG);
-      if (pid <= 0)
-        break;
-
-      PRINT_DEBUG(("Child status: %d\n", WEXITSTATUS (status)));
-
-      /* Find the child and update its structure. */
-      list_for_each_entry (cur, &rp_children, node)
-        {
-          if (cur->pid == pid)
-            {
-              cur->terminated = 1;
-              cur->status = WEXITSTATUS (status);
-              break;
-            }
-        }
-
-      chld_signalled = 1;
-    }
-}
-
-void
-chld_handler (int signum UNUSED)
-{
-  int serrno;
-
-  serrno = errno;
-  check_child_procs();
-  errno = serrno;
-}
-
 static int
 handler (Display *d, XErrorEvent *e)
 {
@@ -306,21 +110,6 @@ handler (Display *d, XErrorEvent *e)
   return 0;
 }
 
-void
-set_sig_handler (int sig, void (*action)(int))
-{
-  struct sigaction act;
-
-  memset (&act, 0, sizeof (act));
-  act.sa_handler = action;
-  sigemptyset (&act.sa_mask);
-  if (sigaction (sig, &act, NULL))
-    {
-      PRINT_ERROR (("Error setting signal handler: %s\n",
-                    strerror (errno)));
-    }
-}
-
 static void
 print_version (void)
 {
@@ -351,68 +140,6 @@ print_help (void)
 # define FD_CLOEXEC 1
 #endif
 
-void
-set_close_on_exec (int fd)
-{
-  int flags = fcntl (fd, F_GETFD);
-  if (flags >= 0)
-    fcntl (fd, F_SETFD, flags | FD_CLOEXEC);
-}
-
-void
-read_rc_file (FILE *file)
-{
-  char *line;
-  size_t linesize = 256;
-
-  line = xmalloc (linesize);
-
-  while (getline (&line, &linesize, file) != -1)
-    {
-      line[strcspn (line, "\n")] = '\0';
-
-      PRINT_DEBUG (("rcfile line: %s\n", line));
-
-      if (*line != '\0' && *line != '#')
-        {
-          cmdret *result;
-          result = command (0, line);
-
-          /* Gobble the result. */
-          if (result)
-            cmdret_free (result);
-        }
-    }
-
-  free (line);
-}
-
-const char *
-get_homedir (void)
-{
-  char *homedir;
-
-  homedir = getenv ("HOME");
-  if (homedir != NULL && homedir[0] == '\0')
-    homedir = NULL;
-
-#if defined (HAVE_PWD_H) && defined (HAVE_GETPWUID)
-  if (homedir == NULL)
-    {
-      struct passwd *pw;
-
-      pw = getpwuid (getuid ());
-      if (pw != NULL)
-        homedir = pw->pw_dir;
-
-      if (homedir != NULL && homedir[0] == '\0')
-        homedir = NULL;
-    }
-#endif
-
-  return homedir;
-}
-
 static int
 read_startup_files (const char *alt_rcfile)
 {
@@ -757,45 +484,6 @@ main (int argc, char *argv[])
 }
 
 void
-clean_up (void)
-{
-  rp_screen *cur;
-  struct list_head *iter, *tmp;
-
-  history_save ();
-
-  free_keymaps ();
-  free_aliases ();
-  free_user_commands ();
-  free_bar ();
-  free_window_stuff ();
-  free_groups ();
-
-  list_for_each_safe_entry (cur, iter, tmp, &rp_screens, node)
-    {
-      list_del (&cur->node);
-      screen_free (cur);
-      free (cur);
-    }
-
-  screen_free_final ();
-
-  /* Delete the undo histories */
-  clear_frame_undos ();
-
-  /* Free the global frame numset shared by all screens. */
-  numset_free (rp_frame_numset);
-
-#ifndef USE_XFT_FONT
-  XFreeFontSet (dpy, defaults.font);
-#endif
-  free (defaults.window_fmt);
-
-  XSetInputFocus (dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
-  XCloseDisplay (dpy);
-}
-
-void
 set_extents_of_fontset (XFontSet font)
 {
   XFontSetExtents *extent;
diff --git a/src/ratpoison.h b/src/ratpoison.h
index 58381ef..06cdfce 100644
--- a/src/ratpoison.h
+++ b/src/ratpoison.h
@@ -92,23 +92,6 @@ do {                                            \
 #include "xrandr.h"
 #include "format.h"
 
-void clean_up (void);
-
-void set_close_on_exec (int fd);
-const char *get_homedir (void);
-void read_rc_file (FILE *file);
-
-void fatal (const char *msg);
-void *xmalloc (size_t size);
-void *xrealloc (void *ptr, size_t size);
-char *xstrdup (const char *s);
-char *xsprintf (char *fmt, ...);
-char *xvsprintf (char *fmt, va_list ap);
-int str_comp (char *s1, char *s2, size_t len);
-char *strtok_ws (char *s);
-void check_child_procs (void);
-void chld_handler (int signum);
-void set_sig_handler (int sig, void (*action)(int));
 void set_extents_of_fontset (XFontSet font);
 XFontSet load_query_font_set (Display *disp, const char *fontset_name);
 
-- 
2.11.0




reply via email to

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