From 67650e22a75f5ffd90a74ad502f56d5c315ab2a2 Mon Sep 17 00:00:00 2001
From: Andreas Bierfert <[email protected]>
Date: Sun, 27 Jan 2013 17:11:01 +0100
Subject: [PATCH 1/2] WINGS: New function WMReadPropListFromPipe

This functions reads a proplist from a pipe instead of a file (like
WMReadPropListFromFile does). It uses a call to popen to open the desired
command, reads data into a buffer till EOF and passes the data to getPropList
for parsing.
---
 WINGs/WINGs/WUtil.h |  2 ++
 WINGs/proplist.c    | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/WINGs/WINGs/WUtil.h b/WINGs/WINGs/WUtil.h
index 30476e0..84f60b9 100644
--- a/WINGs/WINGs/WUtil.h
+++ b/WINGs/WINGs/WUtil.h
@@ -807,6 +807,8 @@ char* WMGetPropListDescription(WMPropList *plist, Bool 
indented);
 
 WMPropList* WMReadPropListFromFile(char *file);
 
+WMPropList* WMReadPropListFromPipe(char *command);
+
 Bool WMWritePropListToFile(WMPropList *plist, char *path);
 
 /* ---[ WINGs/userdefaults.c ]-------------------------------------------- */
diff --git a/WINGs/proplist.c b/WINGs/proplist.c
index 7b0f49d..04bd909 100644
--- a/WINGs/proplist.c
+++ b/WINGs/proplist.c
@@ -1545,6 +1545,60 @@ WMPropList *WMReadPropListFromFile(char *file)
        return plist;
 }
 
+WMPropList *WMReadPropListFromPipe(char *command)
+{
+       FILE *file;
+       WMPropList *plist;
+       PLData *pldata;
+       char line[1024];
+
+       file = popen(command, "r");
+
+       if (!file) {
+               werror(_("%s:could not open menu file"), command);
+               return NULL;
+       }
+
+       pldata = (PLData *) wmalloc(sizeof(PLData));
+       pldata->ptr = NULL;
+       pldata->filename = command;
+       pldata->lineNumber = 1;
+
+       /* read from file till EOF or OOM and fill proplist buffer*/
+       while(fgets(line, sizeof(line), file) != NULL) {
+               if(pldata->ptr == NULL) {
+                   pldata->ptr = wmalloc(strlen(line)+1);
+                   pldata->ptr[0] = '\0';
+               }
+               else
+                   pldata->ptr = wrealloc(pldata->ptr,
+                       strlen(line) + strlen(pldata->ptr) + 1);
+
+               pldata->ptr = strncat(pldata->ptr, line, strlen(line));
+       }
+
+       pclose(file);
+
+       plist = getPropList(pldata);
+
+       if (getNonSpaceChar(pldata) != 0 && plist) {
+               COMPLAIN(pldata, _("extra data after end of property list"));
+               /*
+                * We can't just ignore garbage after the end of the description
+                * (especially if the description was read from a file), because
+                * the "garbage" can be the real data and the real garbage is in
+                * fact in the beginning of the file (which is now inside plist)
+                */
+               WMReleasePropList(plist);
+               plist = NULL;
+       }
+
+       wfree(pldata->ptr);
+       wfree(pldata);
+
+       return plist;
+}
+
 /* TODO: review this function's code */
 
 Bool WMWritePropListToFile(WMPropList * plist, char *path)
-- 
1.8.1


--
To unsubscribe, send mail to [email protected].

Reply via email to