]> git.sur5r.net Git - i3/i3/blobdiff - i3-config-wizard/main.c
i3-config-wizard: use fgetln on OpenBSD, too (Thanks David Coppa)
[i3/i3] / i3-config-wizard / main.c
index 84a7f77ee22f3f76b688c89aed0e8d0c7cfa9a1b..be042673f2c53a02a1bbe8855fa13f38ced2b9c0 100644 (file)
@@ -2,12 +2,25 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
  *
  * i3-config-wizard: Program to convert configs using keycodes to configs using
  *                   keysyms.
  *
  */
+#if defined(__FreeBSD__)
+#include <sys/param.h>
+#endif
+
+/* For systems without getline, fall back to fgetln */
+#if defined(__APPLE__) || (defined(__FreeBSD__) && __FreeBSD_version < 800000) || defined(__OpenBSD__)
+#define USE_FGETLN
+#elif defined(__FreeBSD__)
+/* Defining this macro before including stdio.h is necessary in order to have
+ * a prototype for getline in FreeBSD. */
+#define _WITH_GETLINE
+#endif
+
 #include <ev.h>
 #include <stdio.h>
 #include <sys/types.h>
@@ -155,8 +168,8 @@ static int handle_expose() {
         set_font(&bold_font);
         set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
         if (modifier == MOD_Mod4)
-            txt(31, 4, "<Win>");
-        else txt(31, 5, "<Alt>");
+            txt(10, 4, "-> <Win>");
+        else txt(10, 5, "-> <Alt>");
 
         /* green */
         set_font(&font);
@@ -282,10 +295,11 @@ static void finish() {
     FILE *ks_config = fopen(config_path, "w");
     if (ks_config == NULL)
         err(1, "Could not open output config file \"%s\"", config_path);
+    free(config_path);
 
     char *line = NULL;
     size_t len = 0;
-#if !defined(__APPLE__)
+#ifndef USE_FGETLN
     ssize_t read;
 #endif
     bool head_of_file = true;
@@ -298,10 +312,16 @@ static void finish() {
     fputs("# this file and re-run i3-config-wizard(1).\n", ks_config);
     fputs("#\n", ks_config);
 
-#if defined(__APPLE__)
-    while ((line = fgetln(kc_config, &len)) != NULL) {
+#ifdef USE_FGETLN
+    char *buf = NULL;
+    while ((buf = fgetln(kc_config, &len)) != NULL) {
+        /* fgetln does not return null-terminated strings */
+        FREE(line);
+        sasprintf(&line, "%.*s", len, buf);
 #else
-    while ((read = getline(&line, &len, kc_config)) != -1) {
+    size_t linecap = 0;
+    while ((read = getline(&line, &linecap, kc_config)) != -1) {
+        len = strlen(line);
 #endif
         /* skip the warning block at the beginning of the input file */
         if (head_of_file &&
@@ -312,8 +332,11 @@ static void finish() {
 
         /* Skip leading whitespace */
         char *walk = line;
-        while (isspace(*walk) && walk < (line + len))
+        while (isspace(*walk) && walk < (line + len)) {
+            /* Pre-output the skipped whitespaces to keep proper indentation */
+            fputc(*walk, ks_config);
             walk++;
+        }
 
         /* Set the modifier the user chose */
         if (strncmp(walk, "set $mod ", strlen("set $mod ")) == 0) {
@@ -326,7 +349,7 @@ static void finish() {
         /* Check for 'bindcode'. If it’s not a bindcode line, we
          * just copy it to the output file */
         if (strncmp(walk, "bindcode", strlen("bindcode")) != 0) {
-            fputs(line, ks_config);
+            fputs(walk, ks_config);
             continue;
         }
         char *result = rewrite_binding(walk);
@@ -338,7 +361,10 @@ static void finish() {
     fflush(ks_config);
     fsync(fileno(ks_config));
 
+#ifndef USE_FGETLN
     free(line);
+#endif
+
     fclose(kc_config);
     fclose(ks_config);
 
@@ -410,7 +436,7 @@ int main(int argc, char *argv[]) {
     unlink(config_path);
 
     if (socket_path == NULL)
-        socket_path = socket_path_from_x11();
+        socket_path = root_atom_contents("I3_SOCKET_PATH");
 
     if (socket_path == NULL)
         socket_path = "/tmp/i3-ipc.sock";