+2005-11-05 Carl Worth <cworth@cworth.org>
+
+ * src/Makefile.am: Add ttt-board.[ch] and x.[ch]
+
+ * src/ttt-board.h:
+ * src/ttt-board.c: (ttt_board_init), (ttt_board_init_from_string),
+ (ttt_board_to_string), (ttt_board_write): Add some stub functions
+ for Richard and Kevin to have some practice
+ implementing. Functions to represent a board and to go to/from a
+ string.
+
+ * src/x.h:
+ * src/x.c: (xasprintf), (xvasprintf), (xpipe), (xfork), (xmalloc),
+ (xcalloc), (xrealloc), (xfdopen), (xstrdup), (xfwrite): Several
+ utility functions that make system calls, check the result, and
+ exit on any error. This will simplify the error checking needed in
+ programs using these functions.
+
2005-11-05 Carl Worth <cworth@cworth.org>
* src/Makefile.am: Break things up for separate ttt-client and
ttt-command.c \
ttt-command.h \
ttt-error.c \
- ttt-error.h
+ ttt-error.h \
+ x.c \
+ x.h
ttt_client_SOURCES = \
$(ttt_common_sources) \
--- /dev/null
+/* ttt.c - client-server tic-tac-toe game
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program 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.
+ *
+ * This program 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 program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#include "ttt-board.h"
+
+/* Initialize an empty board. */
+void
+ttt_board_init (ttt_board_t *board)
+{
+ /* XXX: NYI */
+}
+
+/* Initialize a board from its string representation.
+ *
+ * Returns: TTT_BOARD_SUCCESS or TTT_BOARD_INVALID_BOARD if the board
+ * string could not be properly parsed.
+ */
+void
+ttt_board_init_from_string (ttt_board_t *board,
+ const char *s)
+{
+ /* XXX: NYI */
+}
+
+/* Return the string representation of a board.
+ *
+ * The return value is a malloc()ed string that should be free()ed
+ * when no longer needed.
+ *
+ * Errors: If out-of-memory occurs, this function will not return.
+ */
+char *
+ttt_board_to_string (ttt_board_t *board)
+{
+ /* XXX: NYI */
+}
+
+/* Write a string representation of a board to the provided file.
+ *
+ * Errors: If out-of-memory or a file IO error occurs, this function
+ * will not return.
+ */
+void
+ttt_board_write (ttt_board_t *board, FILE *file)
+{
+ char *s;
+
+ s = ttt_board_to_string (board);
+
+ xfwrite (s, 1, strlen (s) + 1, file);
+
+ free (s);
+}
+
--- /dev/null
+/* ttt.c - client-server tic-tac-toe game
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program 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.
+ *
+ * This program 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 program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#ifndef _TTT_BOARD_H_
+#define _TTT_BOARD_H_
+
+typedef struct _ttt_board {
+ /* XXX: Fill this out with appropriate fields. */
+} ttt_board_t;
+
+#endif
+
--- /dev/null
+/* x.c - Some wrappers for various functions to check and exit on out-of-memory
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program 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.
+ *
+ * This program 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 program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <carl@theworths.org>
+ */
+
+#include "ttt.h"
+
+#include <stdarg.h>
+
+void
+xasprintf (char **strp, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ xvasprintf (strp, fmt, ap);
+ va_end (ap);
+}
+
+void
+xvasprintf (char **strp, const char *fmt, va_list ap)
+{
+ int ret;
+
+ ret = vasprintf (strp, fmt, ap);
+
+ if (ret == -1) {
+ fprintf (stderr, "Error: vasprintf failed: Out of memory? Aborting.\n");
+ exit (1);
+ }
+}
+
+void
+xpipe (int filedes[2])
+{
+ int ret;
+
+ ret = pipe (filedes);
+
+ if (ret == -1) {
+ fprintf (stderr, "Error: pipe failed: %s. Aborting.\n", strerror (errno));
+ exit (1);
+ }
+}
+
+pid_t
+xfork (void)
+{
+ pid_t pid;
+
+ pid = fork ();
+
+ if (pid == -1) {
+ fprintf (stderr, "Error: fork failed: %s. Aborting.\n", strerror (errno));
+ exit (1);
+ }
+
+ return pid;
+}
+
+void *
+xmalloc (size_t size)
+{
+ void *ret;
+
+ ret = malloc (size);
+
+ if (ret == NULL) {
+ fprintf (stderr, "Error: malloc failed. Out of memory. Aborting.\n");
+ exit (1);
+ }
+
+ return ret;
+}
+
+void *
+xcalloc (size_t nmemb, size_t size)
+{
+ void *ret;
+
+ ret = calloc (nmemb, size);
+
+ if (ret == NULL) {
+ fprintf (stderr, "Error: calloc failed. Out of memory. Aborting.\n");
+ exit (1);
+ }
+
+ return ret;
+}
+
+void *
+xrealloc (void *ptr, size_t size)
+{
+ void *ret;
+
+ ret = realloc (ptr, size);
+
+ if (ret == NULL) {
+ fprintf (stderr, "Error: realloc failed: Out of memory. Aborting.\n");
+ exit (1);
+ }
+
+ return ret;
+}
+
+FILE *
+xfdopen (int filedes, const char *mode)
+{
+ FILE *ret;
+
+ ret = fdopen (filedes, mode);
+
+ if (ret == NULL) {
+ fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n", strerror (errno));
+ exit (1);
+ }
+
+ return ret;
+}
+
+char *
+xstrdup (const char *s)
+{
+ char *ret;
+
+ ret = strdup (s);
+
+ if (ret == NULL) {
+ fprintf (stderr, "Error: strdup failed: %s. Aborting.\n",
+ strerror (errno));
+ exit (1);
+ }
+
+ return ret;
+}
+
+void
+xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ int ret;
+
+ ret = fwrite (ptr, size, nmemb, stream);
+ if (ret != nmemb) {
+ fprintf (stderr, "Error: error occured during fwrite. Aborting.\n");
+ exit (1);
+ }
+}
--- /dev/null
+/* x.c - Some wrappers for various functions to check and exit on out-of-memory
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program 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.
+ *
+ * This program 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 program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <carl@theworths.org>
+ */
+
+#ifndef _X_H_
+#define _X_H_
+
+void
+xasprintf (char **strp, const char *fmt, ...) WDO_PRINTF_FORMAT(2, 3);
+
+void
+xvasprintf (char **strp, const char *fmt, va_list ap);
+
+void
+xpipe (int filedes[2]);
+
+pid_t
+xfork (void);
+
+void *
+xmalloc (size_t size);
+
+void *
+xcalloc (size_t nmemb, size_t size);
+
+void *
+xrealloc (void *ptr, size_t size);
+
+FILE *
+xfdopen (int filedes, const char *mode);
+
+char *
+xstrdup (const char *s);
+
+#endif /* _X_H_ */