expr patch

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

expr patch

Kevin Massey-2
here is a small patch i wrote for expr that allows it to support command
line arguments and has a '--help' argument.

------------------------------------------------

@@ -23,6 +23,8 @@
 int              is_zero_or_null(struct val *);
 void             nexttoken(int);
 __dead void      error(void);
+void            usage();
+void            usageEXT();
 struct val      *eval6(void);
 struct val      *eval5(void);
 struct val      *eval4(void);
@@ -493,7 +495,23 @@
 {
         struct val     *vp;

-        (void) setlocale(LC_ALL, "");
+        int c;
+
+       if(argc < 2)
+               usage();
+
+       while (( c = getopt (argc, argv, "help")) != -1)
+       {
+         switch(c)
+         {
+               case 'h':
+                       usageEXT();
+               default:
+                       break;
+         }
+       }
+
+       (void) setlocale(LC_ALL, "");
         av = argv + 1;

         nexttoken(0);
@@ -511,3 +529,63 @@

         exit(is_zero_or_null(vp));
 }
+
+void usage()
+{
+        printf("Usage: expr EXPRESSION\n"
+                "or:  expr OPTION\n"
+                "Try expr --help  for more information\n");
+        exit(0);
+}
+
+void usageEXT()
+{
+        printf("Usage: expr EXPRESSION \n"
+                "or:  expr OPTION \n"
+                "--help     display this help and exit \n\n"
+
+                "Print the value of EXPRESSION to standard output.  A blank
line below \n"
+                "separates increasing precedence groups.  EXPRESSION may
be: \n\n"
+
+                "ARG1 | ARG2       ARG1 if it is neither null nor 0,
otherwise ARG2 \n"
+
+                "ARG1 & ARG2       ARG1 if neither argument is null or 0,
otherwise 0 \n"
+
+                "ARG1 < ARG2       ARG1 is less than ARG2 \n"
+                "ARG1 <= ARG2      ARG1 is less than or equal to ARG2 \n"
+                "ARG1 = ARG2       ARG1 is equal to ARG2 \n"
+                "ARG1 != ARG2      ARG1 is unequal to ARG2 \n"
+                "ARG1 >= ARG2      ARG1 is greater than or equal to ARG2
\n"
+                "ARG1 > ARG2       ARG1 is greater than ARG2 \n"
+
+                "ARG1 + ARG2       arithmetic sum of ARG1 and ARG2 \n"
+                "ARG1 - ARG2       arithmetic difference of ARG1 and ARG2
\n"
+
+                "ARG1 * ARG2       arithmetic product of ARG1 and ARG2 \n"
+                "ARG1 / ARG2       arithmetic quotient of ARG1 divided by
ARG2\n"
+                "ARG1 % ARG2       arithmetic remainder of ARG1 divided by
ARG2\n"
+
+                "STRING : REGEXP   anchored pattern match of REGEXP in
STRING\n"
+
+                "match STRING REGEXP        same as STRING : REGEXP\n"
+                "substr STRING POS LENGTH   substring of STRING, POS
counted from 1\n"
+                "index STRING CHARS         index in STRING where any CHARS
is found, or
0\n"
+                "length STRING              length of STRING\n"
+                "+ TOKEN                    interpret TOKEN as a string,
even if it is a\n"
+                "               keyword like `match' or an operator like
`/'\n\n"
+
+                "( EXPRESSION )             value of EXPRESSION\n\n"
+
+                "Beware that many operators need to be escaped or quoted
for shells.\n"
+                "Comparisons are arithmetic if both ARGs are numbers, else
lexicographical.\n"
+                "Pattern matches return the string matched between ( and )
or null; if\n"
+                "( and ) are not used, they return the number of characters
matched or 0.\n\n"
+
+                "expr exits with:\n"
+                        "0 if the expression is neither null nor 0;\n"
+                        "1 if the expression is null or 0; or\n"
+                        "2 for invalid expressions.\n\n");
+
+        exit(0);
+}