commit 7329da8ebf0b09b246ababdb6f67def7aa38b15b
parent 1b2393bd77100a301514a086794e7f1ed16fbe25
Author: Remy Noulin <loader2x@gmail.com>
Date: Tue, 6 Aug 2019 07:53:53 +0200
add command, commandf and commandNFree to run shell command and return the exit value
completion.txt | 11 +++++++
documentation.md | 72 ++++++++++++++++++++++++++++++++++++++++++
release/libsheepy.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++
release/libsheepy.h | 42 ++++++++++++++++++++++---
src/libsheepy.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++
src/libsheepy.h | 42 ++++++++++++++++++++++---
6 files changed, 337 insertions(+), 10 deletions(-)
Diffstat:
6 files changed, 337 insertions(+), 10 deletions(-)
diff --git a/completion.txt b/completion.txt
@@ -811,6 +811,17 @@ logSystemOutf(fmt, ...)
logExecOutf
logSystemf(fmt, ...)
logExecf
+command(cmd)
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName)
+commandf(cmd, ...)
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...)
+commandNFree(cmd)
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName)
+commandOut
+logCommand(cmd)
+logCommandf(fmt, ...)
+logCommandNFree(cmd)
+logCommandOut
bool listEqS(char **list1, char **list2)
bool listHasS(char **list, const char *string)
bool listHasCharS(char **list, char c)
diff --git a/documentation.md b/documentation.md
@@ -840,6 +840,17 @@ logSystemOutf(fmt, ...)
logExecOutf
logSystemf(fmt, ...)
logExecf
+command(cmd)
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName)
+commandf(cmd, ...)
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...)
+commandNFree(cmd)
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName)
+commandOut
+logCommand(cmd)
+logCommandf(fmt, ...)
+logCommandNFree(cmd)
+logCommandOut
bool listEqS(char **list1, char **list2)
bool listHasS(char **list, const char *string)
bool listHasCharS(char **list, char c)
@@ -3204,6 +3215,19 @@ int systemf(const char *fmt, ...);
#define logExecOutf logSystemOutf
#define logSystemf(fmt, ...) ({\
#define logExecf logSystemf
+// run command and return exit code from command (not system return value like system, systemf and systemNFree)
+#define command(cmd) commandF(cmd, __LINE__, __func__, __FILE__)
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName);
+#define commandf(cmd, ...) commandfF(__LINE__, __func__, __FILE__, cmd, __VA_ARGS__)
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...);
+#define commandNFree(cmd) commandNFreeF(cmd, __LINE__, __func__, __FILE__)
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName);
+#define commandOut execOut
+// log then run command and return exit code from command (not system return value like system, systemf and systemNFree)
+#define logCommand(cmd) funcbegin\
+#define logCommandf(fmt, ...) funcbegin\
+#define logCommandNFree(cmd) funcbegin\
+#define logCommandOut logExecOut
// compare lists
bool listEqS(char **list1, char **list2);
// has
@@ -13837,6 +13861,54 @@ int systemf(const char *fmt, ...)
/*
+ * run command in default shell
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * cmd command string
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName)
+
+
+/*
+ * run command with formatting in default shell
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * formatting
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...)
+
+
+/*
+ * run command in default shell and free the cmd parameter
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * cmd command string
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName)
+
+
+/*
* list Equal String
* compare each element of list1 and list2
*
diff --git a/release/libsheepy.c b/release/libsheepy.c
@@ -660,6 +660,9 @@ bool appendText(const char *filePath, char **list);
char **execOut(const char *cmd);
char **systemOutf(const char *fmt, ...);
int systemf(const char *fmt, ...);
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName);
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...);
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName);
bool listEqS(char **list1, char **list2);
bool listHasS(char **list, const char *string);
bool listHasCharS(char **list, char c);
@@ -51565,6 +51568,93 @@ int systemf(const char *fmt, ...) {
}
/**
+ * run command in default shell
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * cmd command string
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName) {
+
+ if (!cmd) {
+ shEPrintfS("\nSystem Error at %d, %s, %s. The command was: NULL\n", line, thisFunc, thisFileName);
+ return(-1);
+ }
+
+ int r = system(cmd);
+
+ if (!WIFEXITED(r)) {
+ shEPrintfS("\nSystem Error at %d, %s, %s. The command was: \"%s\"\n", line, thisFunc, thisFileName, cmd);
+ return(-1);
+ }
+
+ r = WEXITSTATUS(r);;
+
+ if (r == 127) {
+ shEPrintfS("\nSystem Error at %d, %s, %s. The command was: \"%s\"\n", line, thisFunc, thisFileName, cmd);
+ return(-1);
+ }
+
+ return(r);
+}
+
+/**
+ * run command with formatting in default shell
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * formatting
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...) {
+ char *cmd = NULL;
+ va_list pl;
+
+ if (!fmt) {
+ return(-1);
+ }
+ va_start(pl, fmt);
+ if (vasprintf(&cmd, fmt, pl) == -1) {
+ return(-1);
+ }
+ va_end(pl);
+
+ return(commandNFreeF(cmd, line, thisFunc, thisFileName));
+}
+
+/**
+ * run command in default shell and free the cmd parameter
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * cmd command string
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName) {
+ int r;
+
+ r = commandF(cmd, line, thisFunc, thisFileName);
+ free(cmd);
+ return(r);
+}
+
+/**
* list Equal String
* compare each element of list1 and list2
*
diff --git a/release/libsheepy.h b/release/libsheepy.h
@@ -96,7 +96,7 @@
// version accoring to the version package: Release.Major.minor.patch
// https://noulin.net/version/file/README.md.html
-#define LIBSHEEPY_VERSION "1.0.7"
+#define LIBSHEEPY_VERSION "1.0.8"
#ifndef SH_PREFIX
#define SH_PREFIX(NAME) NAME
@@ -2648,13 +2648,16 @@ int systemf(const char *fmt, ...);
// system commands and log
#define logSystem(cmd) do{\
- logI (cmd);\
- system(cmd);\
+ var UNIQVAR(cm) = cmd;\
+ logI (UNIQVAR(cm));\
+ system(UNIQVAR(cm));\
} while(0)
#define logExec logSystem
#define logSystemOut(cmd) ({\
- logI (cmd);\
- systemOut(cmd);})
+ var UNIQVAR(cm) = cmd;\
+ logI (UNIQVAR(cm));\
+ systemOut(UNIQVAR(cm));\
+ })
#define logExecOut logSystemOut
#define logSystemOutf(fmt, ...) ({\
logI (fmt, __VA_ARGS__);\
@@ -2665,6 +2668,35 @@ int systemf(const char *fmt, ...);
systemf(fmt, __VA_ARGS__);})
#define logExecf logSystemf
+// run command and return exit code from command (not system return value like system, systemf and systemNFree)
+#define command(cmd) commandF(cmd, __LINE__, __func__, __FILE__)
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName);
+#define commandf(cmd, ...) commandfF(__LINE__, __func__, __FILE__, cmd, __VA_ARGS__)
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...);
+#define commandNFree(cmd) commandNFreeF(cmd, __LINE__, __func__, __FILE__)
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName);
+#define commandOut execOut
+
+// log then run command and return exit code from command (not system return value like system, systemf and systemNFree)
+#define logCommand(cmd) funcbegin\
+ var UNIQVAR(cm) = cmd;\
+ logI (UNIQVAR(cm));\
+ command(UNIQVAR(cm));\
+ funcend
+
+#define logCommandf(fmt, ...) funcbegin\
+ logI (fmt, __VA_ARGS__);\
+ commandf(fmt, __VA_ARGS__);\
+ funcend
+
+#define logCommandNFree(cmd) funcbegin\
+ var UNIQVAR(cm) = cmd;\
+ logI (UNIQVAR(cm));\
+ commandNFree(UNIQVAR(cm));\
+ funcend
+
+#define logCommandOut logExecOut
+
// compare lists
bool listEqS(char **list1, char **list2);
diff --git a/src/libsheepy.c b/src/libsheepy.c
@@ -662,6 +662,9 @@ bool appendText(const char *filePath, char **list);
char **execOut(const char *cmd);
char **systemOutf(const char *fmt, ...);
int systemf(const char *fmt, ...);
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName);
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...);
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName);
bool listEqS(char **list1, char **list2);
bool listHasS(char **list, const char *string);
bool listHasCharS(char **list, char c);
@@ -51621,6 +51624,93 @@ int systemf(const char *fmt, ...) {
}
/**
+ * run command in default shell
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * cmd command string
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName) {
+
+ if (!cmd) {
+ shEPrintfS("\nSystem Error at %d, %s, %s. The command was: NULL\n", line, thisFunc, thisFileName);
+ return(-1);
+ }
+
+ int r = system(cmd);
+
+ if (!WIFEXITED(r)) {
+ shEPrintfS("\nSystem Error at %d, %s, %s. The command was: \"%s\"\n", line, thisFunc, thisFileName, cmd);
+ return(-1);
+ }
+
+ r = WEXITSTATUS(r);;
+
+ if (r == 127) {
+ shEPrintfS("\nSystem Error at %d, %s, %s. The command was: \"%s\"\n", line, thisFunc, thisFileName, cmd);
+ return(-1);
+ }
+
+ return(r);
+}
+
+/**
+ * run command with formatting in default shell
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * formatting
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...) {
+ char *cmd = NULL;
+ va_list pl;
+
+ if (!fmt) {
+ return(-1);
+ }
+ va_start(pl, fmt);
+ if (vasprintf(&cmd, fmt, pl) == -1) {
+ return(-1);
+ }
+ va_end(pl);
+
+ return(commandNFreeF(cmd, line, thisFunc, thisFileName));
+}
+
+/**
+ * run command in default shell and free the cmd parameter
+ *
+ * Exit value equals to 127 is considered an error
+ * because if a shell could not be executed in the child process, then
+ * the return value from system() is 127 (man 3 system).
+ *
+ * \param
+ * cmd command string
+ * \return
+ * command exit code
+ * -1 when cmd is NULL or system exited with errors
+ */
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName) {
+ int r;
+
+ r = commandF(cmd, line, thisFunc, thisFileName);
+ free(cmd);
+ return(r);
+}
+
+/**
* list Equal String
* compare each element of list1 and list2
*
diff --git a/src/libsheepy.h b/src/libsheepy.h
@@ -96,7 +96,7 @@
// version accoring to the version package: Release.Major.minor.patch
// https://noulin.net/version/file/README.md.html
-#define LIBSHEEPY_VERSION "1.0.7"
+#define LIBSHEEPY_VERSION "1.0.8"
#ifndef SH_PREFIX
#define SH_PREFIX(NAME) NAME
@@ -2648,13 +2648,16 @@ int systemf(const char *fmt, ...);
// system commands and log
#define logSystem(cmd) do{\
- logI (cmd);\
- system(cmd);\
+ var UNIQVAR(cm) = cmd;\
+ logI (UNIQVAR(cm));\
+ system(UNIQVAR(cm));\
} while(0)
#define logExec logSystem
#define logSystemOut(cmd) ({\
- logI (cmd);\
- systemOut(cmd);})
+ var UNIQVAR(cm) = cmd;\
+ logI (UNIQVAR(cm));\
+ systemOut(UNIQVAR(cm));\
+ })
#define logExecOut logSystemOut
#define logSystemOutf(fmt, ...) ({\
logI (fmt, __VA_ARGS__);\
@@ -2665,6 +2668,35 @@ int systemf(const char *fmt, ...);
systemf(fmt, __VA_ARGS__);})
#define logExecf logSystemf
+// run command and return exit code from command (not system return value like system, systemf and systemNFree)
+#define command(cmd) commandF(cmd, __LINE__, __func__, __FILE__)
+int commandF(const char *cmd, int line, const char *thisFunc, const char *thisFileName);
+#define commandf(cmd, ...) commandfF(__LINE__, __func__, __FILE__, cmd, __VA_ARGS__)
+int commandfF(int line, const char *thisFunc, const char *thisFileName, const char *fmt, ...);
+#define commandNFree(cmd) commandNFreeF(cmd, __LINE__, __func__, __FILE__)
+int commandNFreeF(char *cmd, int line, const char *thisFunc, const char *thisFileName);
+#define commandOut execOut
+
+// log then run command and return exit code from command (not system return value like system, systemf and systemNFree)
+#define logCommand(cmd) funcbegin\
+ var UNIQVAR(cm) = cmd;\
+ logI (UNIQVAR(cm));\
+ command(UNIQVAR(cm));\
+ funcend
+
+#define logCommandf(fmt, ...) funcbegin\
+ logI (fmt, __VA_ARGS__);\
+ commandf(fmt, __VA_ARGS__);\
+ funcend
+
+#define logCommandNFree(cmd) funcbegin\
+ var UNIQVAR(cm) = cmd;\
+ logI (UNIQVAR(cm));\
+ commandNFree(UNIQVAR(cm));\
+ funcend
+
+#define logCommandOut logExecOut
+
// compare lists
bool listEqS(char **list1, char **list2);