V67: #198 added process exit codes for pigs
authorjoan <joan@abyz.co.uk>
Sun, 11 Mar 2018 13:24:54 +0000 (13:24 +0000)
committerjoan <joan@abyz.co.uk>
Sun, 11 Mar 2018 13:24:54 +0000 (13:24 +0000)
MakeRemote
Makefile
README
pigpio.h
pigs.1
pigs.c
pigs.h [new file with mode: 0644]

index a3a235c39f1b82476aade1e9e306312c927a9990..250d40eda17fb75488f3ee883400ba4100a659f4 100644 (file)
@@ -96,6 +96,6 @@ command.o: command.c pigpio.h command.h
 pigpiod.o: pigpiod.c pigpio.h
 pigpiod_if.o: pigpiod_if.c pigpio.h pigpiod_if.h command.h
 pigpiod_if2.o: pigpiod_if2.c pigpio.h pigpiod_if2.h command.h
-pigs.o: pigs.c pigpio.h command.h
+pigs.o: pigs.c pigpio.h command.h pigs.h
 
 
index ac68064645a57680f612592ba16128a222ff16d0..3d94f4d32aa6a26712b5b84e543c750da55565d5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -135,7 +135,7 @@ $(LIB3):    $(OBJ3)
 
 pig2vcd.o: pig2vcd.c pigpio.h
 pigpiod.o: pigpiod.c pigpio.h
-pigs.o: pigs.c pigpio.h command.h
+pigs.o: pigs.c pigpio.h command.h pigs.h
 x_pigpio.o: x_pigpio.c pigpio.h
 x_pigpiod_if.o: x_pigpiod_if.c pigpiod_if.h pigpio.h
 x_pigpiod_if2.o: x_pigpiod_if2.c pigpiod_if2.h pigpio.h
diff --git a/README b/README
index 35b4789a87bb878dafd73430670b919497539f2a..ed453a902b630622a9722e2a68c1389c1935148f 100644 (file)
--- a/README
+++ b/README
@@ -23,6 +23,7 @@ o the library (libpigpiod_if2.so) in /usr/local/lib
 o the header file (pigpio.h) in /usr/local/include
 o the header file (pigpiod_if.h) in /usr/local/include
 o the header file (pigpiod_if2.h) in /usr/local/include
+o the header file (pigs.h) in /usr/local/include
 o the daemon (pigpiod) in /usr/local/bin
 o the socket interface (pigs) in /usr/local/bin
 o the utility pig2vcd in /usr/local/bin
@@ -35,7 +36,7 @@ TEST (optional)
 *                                                          *
 * All the tests make extensive use of gpio 25 (pin 22).    *
 * Ensure that either nothing or just a LED is connected to *
-* gpio 4 before running any of the tests.                  *
+* gpio 25 before running any of the tests.                 *
 *                                                          *
 * Some tests are statistical in nature and so may on       *
 * occasion fail.  Repeated failures on the same test or    *
index 65dc90276d10bceb9800b16fb55ebeab0cb2843a..11ee4b9ff7a02fc6f83157c73e40e40f2b3be5a9 100644 (file)
--- a/pigpio.h
+++ b/pigpio.h
@@ -31,7 +31,7 @@ For more information, please refer to <http://unlicense.org/>
 #include <stdint.h>
 #include <pthread.h>
 
-#define PIGPIO_VERSION 6701
+#define PIGPIO_VERSION 6702
 
 /*TEXT
 
diff --git a/pigs.1 b/pigs.1
index 51a9e08add741b86cfddbdfb457fa8393ab697d8..df8fcb52f07a93f9ac5f2a4f25a6aa70e969a886 100644 (file)
--- a/pigs.1
+++ b/pigs.1
@@ -96,6 +96,26 @@ echo "{command}+" >/dev/pigpio
 .br
 pigs will show the result of the command on screen.
 
+.br
+The pigs process returns an exit status (which can be displayed with
+the command echo $?).
+
+.br
+
+.EX
+PIGS_OK            0
+.br
+PIGS_CONNECT_ERR 255
+.br
+PIGS_OPTION_ERR  254
+.br
+PIGS_SCRIPT_ERR  253
+.br
+.br
+.br
+
+.EE
+
 .br
 The results of /dev/pigpio commands need to be read from /dev/pigout,
 e.g. cat /dev/pigout (try cat /dev/pigout& so that all subsequent
diff --git a/pigs.c b/pigs.c
index 24f1fee27da7ed0ef052c01927d7ca27bff835af..4ae97eb8b02d9e03d127817fb551bba429d68a3a 100644 (file)
--- a/pigs.c
+++ b/pigs.c
@@ -26,7 +26,7 @@ For more information, please refer to <http://unlicense.org/>
 */
 
 /*
-This version is for pigpio version 56+
+This version is for pigpio version 67+
 */
 
 #include <stdio.h>
@@ -42,6 +42,7 @@ This version is for pigpio version 56+
 
 #include "pigpio.h"
 #include "command.h"
+#include "pigs.h"
 
 /*
 This program provides a socket interface to some of
@@ -53,16 +54,20 @@ char response_buf[CMD_MAX_EXTENSION];
 
 int printFlags = 0;
 
+int status = PIGS_OK;
+
 #define SOCKET_OPEN_FAILED -1
 
 #define PRINT_HEX 1
 #define PRINT_ASCII 2
 
-void fatal(char *fmt, ...)
+void report(int err, char *fmt, ...)
 {
    char buf[128];
    va_list ap;
 
+   if (err > status) status = err;
+
    va_start(ap, fmt);
    vsnprintf(buf, sizeof(buf), fmt, ap);
    va_end(ap);
@@ -76,6 +81,8 @@ static int initOpts(int argc, char *argv[])
 {
    int opt, args;
 
+   opterr = 0;
+
    args = 1;
 
    while ((opt = getopt(argc, argv, "ax")) != -1)
@@ -91,7 +98,11 @@ static int initOpts(int argc, char *argv[])
             printFlags |= PRINT_HEX;
             args++;
             break;
-        }
+
+         default:
+            args++;
+            report(PIGS_OPTION_ERR, "ERROR: bad option %c", optopt);
+      }
     }
    return args;
 }
@@ -150,13 +161,13 @@ void print_result(int sock, int rv, cmdCmd_t cmd)
          if (r < 0)
          {
             printf("%d\n", r);
-            fatal("ERROR: %s", cmdErrStr(r));
+            report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
          }
          break;
 
       case 2:
          printf("%d\n", r);
-         if (r < 0) fatal("ERROR: %s", cmdErrStr(r));
+         if (r < 0) report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
          break;
 
       case 3:
@@ -176,7 +187,7 @@ void print_result(int sock, int rv, cmdCmd_t cmd)
                  I2CZ  SERR  SLR  SPIX  SPIR
               */
          printf("%d", r);
-         if (r < 0) fatal("ERROR: %s", cmdErrStr(r));
+         if (r < 0) report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
          if (r > 0)
          {
             if (printFlags == PRINT_ASCII) printf(" ");
@@ -203,7 +214,7 @@ void print_result(int sock, int rv, cmdCmd_t cmd)
          if (r != (4 + (4*PI_MAX_SCRIPT_PARAMS)))
          {
             printf("%d", r);
-            fatal("ERROR: %s", cmdErrStr(r));
+            report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
          }
          else
          {
@@ -223,7 +234,7 @@ void print_result(int sock, int rv, cmdCmd_t cmd)
          if (r < 0)
          {
             printf("%d", r);
-            fatal("ERROR: %s", cmdErrStr(r));
+            report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
          }
 
          p = (uint32_t *)response_buf;
@@ -354,26 +365,29 @@ int main(int argc , char *argv[])
 
                         print_result(sock, cmdInfo[idx].rv, cmd);
                      }
-                     else fatal("socket receive failed");
+                     else report(PIGS_CONNECT_ERR, "socket receive failed");
                   }
-                  else fatal("socket send failed");
+                  else report(PIGS_CONNECT_ERR, "socket send failed");
                }
-               else fatal("socket connect failed");
+               else report(PIGS_CONNECT_ERR, "socket connect failed");
             }
          }
-         else fatal("%s only allowed within a script", cmdInfo[idx].name);
+         else report(PIGS_SCRIPT_ERR,
+                 "%s only allowed within a script", cmdInfo[idx].name);
       }
       else
       {
          if (idx == CMD_UNKNOWN_CMD)
-            fatal("%s? unknown command, pigs h for help", cmdStr());
+            report(PIGS_SCRIPT_ERR,
+               "%s? unknown command, pigs h for help", cmdStr());
          else
-            fatal("%s: bad parameter, pigs h for help", cmdStr());
+            report(PIGS_SCRIPT_ERR,
+               "%s: bad parameter, pigs h for help", cmdStr());
       }
    }
 
    if (sock >= 0) close(sock);
 
-   return 0;
+   return status;
 }
 
diff --git a/pigs.h b/pigs.h
new file mode 100644 (file)
index 0000000..16cfcdd
--- /dev/null
+++ b/pigs.h
@@ -0,0 +1,41 @@
+/*
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org/>
+*/
+
+/*
+This version is for pigpio version 67+
+*/
+
+#ifndef PIGS_H
+#define PIGS_H
+
+#define PIGS_OK            0
+#define PIGS_CONNECT_ERR 255
+#define PIGS_OPTION_ERR  254
+#define PIGS_SCRIPT_ERR  253
+
+#endif
+