Resurect the HTML trace. Now generates a single HTML file containing the SVG.
[tatoo.git] / remake.cpp
index b8387be..30440a8 100644 (file)
@@ -724,6 +724,12 @@ static std::string prefix_dir;
  */
 static bool propagate_vars = false;
 
+
+/**
+ * Whether to print that we changed working directory (to be nice with emacs)
+ */
+static bool print_change_working_dir = false;
+
 #ifndef WINDOWS
 static volatile sig_atomic_t got_SIGCHLD = 0;
 
@@ -865,13 +871,28 @@ static void init_working_dir()
  */
 static void init_prefix_dir()
 {
+       std::string old_prefix_dir = prefix_dir;
        for (;;)
        {
                struct stat s;
                if (stat((prefix_dir + "/Remakefile").c_str(), &s) == 0)
                {
-                       chdir(prefix_dir.c_str());
-                       return;
+                       if (0 == chdir(prefix_dir.c_str()))
+                       {
+                               if (old_prefix_dir != prefix_dir)
+                               {
+                                       std::cout << "remake: Entering directory `"
+                                                 << prefix_dir << "'"
+                                                 << std::endl;
+                                       print_change_working_dir = true;
+                               }
+                               return;
+                       }
+                       else
+                       {
+                               std::cerr << "Cannot change working directory to '" << prefix_dir << "'";
+                               exit(EXIT_FAILURE);
+                       }
                }
                size_t pos = prefix_dir.find_last_of('/');
                if (pos == std::string::npos)
@@ -2539,7 +2560,28 @@ static void create_server()
        if (sigaction(SIGINT, &sa, NULL) == -1) goto error;
 
        // Prepare a named unix socket in temporary directory.
-       socket_name = tempnam(NULL, "rmk-");
+       struct stat tmpstat;
+       char * tmpdir;
+       if ((0 == stat(tmpdir = getenv("TMPDIR"), &tmpstat)
+            && (S_ISDIR(tmpstat.st_mode)))
+           || ((0 == stat(tmpdir = (char*)P_tmpdir, &tmpstat))
+               && (S_ISDIR(tmpstat.st_mode)))
+           || ((0 == stat(tmpdir = (char*)"/tmp", &tmpstat))
+               && (S_ISDIR(tmpstat.st_mode))));
+       else goto error;
+       std::stringstream tmpname;
+       long int rnd = now ^ getpid();
+       do
+       {
+               srandom(rnd);
+               rnd = random();
+               tmpname << tmpdir << "/rmk-";
+               tmpname.fill('0');
+               tmpname.width(8);
+               tmpname << rnd;
+       }
+       while (access (tmpname.str().c_str(), F_OK) == 0);
+       socket_name = strdup(tmpname.str().c_str());
        if (!socket_name) goto error2;
        struct sockaddr_un socket_addr;
        size_t len = strlen(socket_name);
@@ -2792,6 +2834,9 @@ static void server_mode(std::string const &remakefile, string_list const &target
        free(socket_name);
 #endif
        save_dependencies();
+       if (print_change_working_dir)
+               std::cout << "remake: Leaving directory `" << prefix_dir 
+                         << "'" <<std::endl;
        exit(build_failure ? EXIT_FAILURE : EXIT_SUCCESS);
 }