1
0

refactor psops(1)

This commit is contained in:
dtb
2023-11-18 11:56:26 -07:00
parent 0164344c52
commit b139a29e2f
6 changed files with 109 additions and 88 deletions

View File

@@ -7,7 +7,7 @@
#define SCMPFLAT(a, b) (*(a) != '\0' && *((a)+1) == '\0' && *(a) == (b))
int
check_arg(char **args, char l, char r){
check_arg(char **args){
enum {
UNINITIALIZED = 0,
NORMAL = 1,
@@ -27,8 +27,10 @@ check_arg(char **args, char l, char r){
/* decrement s if on an rparen, increment if on lparen
* increment terms if on an rparen */
if(args[0][1] == '\0'){
s += (args[0][0] == l) - (args[0][0] == r);
terms += (args[0][0] == r && s == NORMAL);
s += (args[0][0] == L_PAREN)
- (args[0][0] == R_PAREN);
terms +=
(args[0][0] == R_PAREN && s == NORMAL);
}
break;
}
@@ -38,7 +40,7 @@ check_arg(char **args, char l, char r){
}
char **
corresponding_arg(char **arg, char l, char r){
corresponding_arg(char **arg){
size_t p;
for(p = 1; p > 0;){
@@ -46,9 +48,9 @@ corresponding_arg(char **arg, char l, char r){
/* branching here potentially saves a comparison.
* this seems like the most optimal way to do this,
* maybe it isn't, i don't care too much */
if(SCMPFLAT(*arg, l))
if(SCMPFLAT(*arg, L_PAREN))
++p;
else if(SCMPFLAT(*arg, r))
else if(SCMPFLAT(*arg, R_PAREN))
--p;
}

View File

@@ -1,17 +1,22 @@
/* pscat(1) and pspipe(1) arguments are flanked by { '[', '\0' } and
* { ']', '\0' }. Nesting is allowed to facilitate nesting of pscat(1) and
* pspipe(1). This checks to make sure grouping symbols in args are balanced.
* args is the argv that was passed to main, incremented. l is the left
* grouping character (usually '[') and r is the right grouping character
* (usually ']').
#ifndef L_PAREN
# define L_PAREN '['
#endif
#ifndef R_PAREN
# define R_PAREN ']'
#endif
/* psops(1) arguments are flanked by { '[', '\0' } and { ']', '\0' }. Nesting
* is allowed to facilitate nesting of psops(1) programs. This checks to make
* sure grouping symbols in args are balanced.
* args is the argv that was passed to main, incremented (so args[1] is "[").
* Returns 0 in the events of a syntax error or there being no arguments.
* Otherwise, returns the number of top-level right grouping arguments
* (i.e. the number of terms). */
int check_arg(char **args, char l, char r);
int check_arg(char **args);
/* arg is the location of the argument with the left grouping symbol within
* argv, l is the left grouping character itself, r is the right grouping
* character itself.
* argv.
* Returns the location of the argument with the corresponding left grouping
* symbol within argv. */
char **corresponding_arg(char **arg, char l, char r);
char **corresponding_arg(char **arg);