Added support for "~" expansion in the dotdesktop-path (and only in that place, in...
authorskeezix <skeezix@flotsam-vm.(none)>
Fri, 6 Mar 2009 04:35:28 +0000 (23:35 -0500)
committerskeezix <skeezix@flotsam-vm.(none)>
Fri, 6 Mar 2009 04:35:28 +0000 (23:35 -0500)
Makefile
apps/pndnotifyd.c
include/pnd_utility.h [new file with mode: 0644]
lib/pnd_utility.c [new file with mode: 0644]
test/discotest.c
testdata/conf/desktop

index 6260563..77a13ce 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,7 @@ LIB = libpnd.a
 SOLIB = libpnd.so.1         # canonicle name
 SOLIB1 = libpnd.so.1.0.1    # versioned name
 XMLOBJ = lib/tinyxml/tinystr.o lib/tinyxml/tinyxml.o lib/tinyxml/tinyxmlerror.o lib/tinyxml/tinyxmlparser.o
-ALLOBJ = pnd_conf.o pnd_container.o pnd_discovery.o pnd_pxml.o pnd_notify.o pnd_locate.o pnd_tinyxml.o pnd_pndfiles.o pnd_apps.o
+ALLOBJ = pnd_conf.o pnd_container.o pnd_discovery.o pnd_pxml.o pnd_notify.o pnd_locate.o pnd_tinyxml.o pnd_pndfiles.o pnd_apps.o pnd_utility.o
 
 all: ${SOLIB} ${LIB} conftest discotest notifytest locatetest pndnotifyd
 
index 45b204f..6e44c2b 100644 (file)
@@ -27,6 +27,7 @@
 #include "../lib/pnd_pathiter.h"
 #include "pnd_discovery.h"
 #include "pnd_locate.h"
+#include "pnd_utility.h"
 
 static unsigned char g_daemon_mode = 0;
 
@@ -177,6 +178,14 @@ int main ( int argc, char *argv[] ) {
     if ( pndrun ) printf ( "Default pndrun is %s\n", pndrun );
   }
 
+  /* handle globbing or variable substitution
+   */
+  dotdesktoppath = pnd_expand_tilde ( strdup ( dotdesktoppath ) );
+
+  /* validate paths
+   */
+  mkdir ( dotdesktoppath, 0777 );
+
   /* startup
    */
 
diff --git a/include/pnd_utility.h b/include/pnd_utility.h
new file mode 100644 (file)
index 0000000..257bfb7
--- /dev/null
@@ -0,0 +1,17 @@
+
+#ifndef h_pnd_utility_h
+#define h_pnd_utility_h
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// given a malloc'd pointer to a string, expand ~ to $HOME as often as it is found, returning a
+// new string; the old string is destroyed in the process, or returned as-is.
+char *pnd_expand_tilde ( char *freeable_buffer );
+
+#ifdef __cplusplus
+} /* "C" */
+#endif
+
+#endif
diff --git a/lib/pnd_utility.c b/lib/pnd_utility.c
new file mode 100644 (file)
index 0000000..11fbc97
--- /dev/null
@@ -0,0 +1,34 @@
+
+#include <stdio.h> /* for FILE etc */
+#include <stdlib.h> /* for malloc */
+#include <string.h> /* for making ftw.h happy */
+
+#include "pnd_utility.h"
+
+// a generalized variable-substitution routine might be nice; for now we need a quick tilde one,
+// so here goes. Brute force ftw!
+char *pnd_expand_tilde ( char *freeable_buffer ) {
+  char *p;
+  char *s = freeable_buffer;
+  char *home = getenv ( "HOME" );
+
+  if ( ! home ) {
+    return ( s ); // can't succeed
+  }
+
+  while ( ( p = strchr ( s, '~' ) ) ) {
+    char *temp = malloc ( strlen ( s ) + strlen ( home ) + 1 );
+    memset ( temp, '\0', strlen ( s ) + strlen ( home ) + 1 );
+    // copy in stuff prior to ~
+    strncpy ( temp, s, p - s );
+    // copy tilde in
+    strcat ( temp, home );
+    // copy stuff after tilde in
+    strcat ( temp, s + 1 );
+    // swap ptrs
+    free ( s );
+    s = temp;
+  } // while finding matches
+
+  return ( s );
+}
index 5bc7030..364af15 100644 (file)
@@ -9,6 +9,7 @@
 #include "pnd_pxml.h"
 #include "pnd_discovery.h"
 #include "pnd_locate.h"
+#include "pnd_utility.h"
 
 int main ( int argc, char *argv[] ) {
   char *configpath;
@@ -213,5 +214,10 @@ int main ( int argc, char *argv[] ) {
     pnd_box_delete ( apph );
   }
 
+  // extra testing - tilde-substitution
+  char *p = strdup ( "~/.applications" );
+  printf ( "Tilde substitution: in '%s'\n", p );
+  printf ( "                   out '%s'\n", pnd_expand_tilde ( p ) );
+
   return ( 0 );
 }
index 7e9ff76..c84db94 100644 (file)
@@ -3,4 +3,5 @@
 # Desktop configuration
 
 [dotfiles]
-dotdesktoppath ./testdata/dotdesktop   # path for pndnotifyd to spit .desktop files into
+#dotdesktoppath        ./testdata/dotdesktop   # path for pndnotifyd to spit .desktop files into
+dotdesktoppath ~/.applications         # path for pndnotifyd to spit .desktop files into