summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJérémie Courrèges-Anglas <jca@wxcvbn.org>2017-09-17 19:32:29 +0200
committerJérémie Courrèges-Anglas <jca@wxcvbn.org>2017-09-17 19:46:06 +0200
commit218e68c7327c94bd90b2c5b708203be84413db18 (patch)
treef531af4e1dae664a09c319da61fed07920ab3c8d
parent9b7cfb7c8d8d337554ad7c5995ce75fac52c7d54 (diff)
downloadratpoison-218e68c7327c94bd90b2c5b708203be84413db18.zip
Move fatal() and x* helpers to their own file
Should help use them from within tests.
-rw-r--r--src/Makefile.am4
-rw-r--r--src/globals.c143
-rw-r--r--src/globals.h8
-rw-r--r--src/ratpoison.h1
-rw-r--r--src/util.c167
-rw-r--r--src/util.h13
6 files changed, 184 insertions, 152 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index beb2278..d61c4b9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -71,6 +71,8 @@ ratpoison_SOURCES = actions.c \
split.h \
utf8.c \
utf8.h \
+ util.c \
+ util.h \
window.c \
window.h
@@ -85,6 +87,6 @@ check_PROGRAMS = format_test
format_test_SOURCES = actions.c bar.c completions.c editor.c events.c format.c \
frame.c globals.c group.c history.c hook.c input.c \
linkedlist.c manage.c number.c sbuf.c screen.c split.c \
- window.c xrandr.c utf8.c utf8.h
+ window.c xrandr.c utf8.c utf8.h util.c util.h
format_test_CPPFLAGS = $(AM_CPPFLAGS) -DRUN_FORMAT_TEST
format_test_LDADD = $(ratpoison_LDADD)
diff --git a/src/globals.c b/src/globals.c
index 5f4350d..55f97b9 100644
--- a/src/globals.c
+++ b/src/globals.c
@@ -362,149 +362,6 @@ 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)
diff --git a/src/globals.h b/src/globals.h
index 3851695..4f96667 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -210,14 +210,6 @@ 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));
diff --git a/src/ratpoison.h b/src/ratpoison.h
index fc12d7d..5dcb2e1 100644
--- a/src/ratpoison.h
+++ b/src/ratpoison.h
@@ -92,6 +92,7 @@ do { \
#include "xrandr.h"
#include "format.h"
#include "utf8.h"
+#include "util.h"
void set_extents_of_fontset (XFontSet font);
XFontSet load_query_font_set (Display *disp, const char *fontset_name);
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..78947d3
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,167 @@
+/* Ratpoison.
+ * Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts <sabetts@vcn.bc.ca>
+ *
+ * This file is part of ratpoison.
+ *
+ * ratpoison is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * ratpoison is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this software; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+
+#include "ratpoison.h"
+
+#include <ctype.h>
+
+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;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..ff8f085
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,13 @@
+#ifndef UTIL_H
+#define UTIL_H
+
+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);
+
+#endif