From cc69d2aae38b5563d39d3348cec805ed8b3d753d Mon Sep 17 00:00:00 2001 From: Andreas Bierfert Date: Thu, 31 Jan 2013 22:44:28 +0100 Subject: [PATCH] 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. v2: code cleanup --- 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 30476e09..84f60b96 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 7b0f49d1..7a80f500 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) -- 2.11.4.GIT