2023-09-12 20:05:20 -06:00
|
|
|
#include <stddef.h> /* size_t */
|
2022-11-20 18:23:39 -07:00
|
|
|
|
2023-09-12 20:05:20 -06:00
|
|
|
#include "libpsargs.h"
|
2022-11-20 18:23:39 -07:00
|
|
|
|
|
|
|
/* Test string containing { c, '\0' } without iteration.
|
|
|
|
* Theoretically saves a little bit of time compared to strcmp(3). */
|
|
|
|
#define SCMPFLAT(a, b) (*(a) != '\0' && *((a)+1) == '\0' && *(a) == (b))
|
|
|
|
|
|
|
|
int
|
2023-11-18 11:56:26 -07:00
|
|
|
check_arg(char **args){
|
2022-11-20 18:23:39 -07:00
|
|
|
enum {
|
|
|
|
UNINITIALIZED = 0,
|
|
|
|
NORMAL = 1,
|
|
|
|
INLPAREN = 2
|
|
|
|
} s;
|
|
|
|
int terms;
|
|
|
|
|
|
|
|
for(s = UNINITIALIZED, terms = 0; *args != NULL; ++args)
|
|
|
|
switch(s){
|
|
|
|
case UNINITIALIZED: case NORMAL:
|
|
|
|
if(SCMPFLAT(*args, l))
|
|
|
|
s = INLPAREN;
|
|
|
|
else
|
|
|
|
return 0; /* syntax error */
|
|
|
|
break;
|
|
|
|
default: /* >= INLPAREN */
|
|
|
|
/* decrement s if on an rparen, increment if on lparen
|
|
|
|
* increment terms if on an rparen */
|
|
|
|
if(args[0][1] == '\0'){
|
2023-11-18 11:56:26 -07:00
|
|
|
s += (args[0][0] == L_PAREN)
|
|
|
|
- (args[0][0] == R_PAREN);
|
|
|
|
terms +=
|
|
|
|
(args[0][0] == R_PAREN && s == NORMAL);
|
2022-11-20 18:23:39 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure nothing's open */
|
|
|
|
return terms * (s == NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
2023-11-18 11:56:26 -07:00
|
|
|
corresponding_arg(char **arg){
|
2022-11-20 18:23:39 -07:00
|
|
|
size_t p;
|
|
|
|
|
|
|
|
for(p = 1; p > 0;){
|
|
|
|
++arg;
|
|
|
|
/* 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 */
|
2023-11-18 11:56:26 -07:00
|
|
|
if(SCMPFLAT(*arg, L_PAREN))
|
2022-11-20 18:23:39 -07:00
|
|
|
++p;
|
2023-11-18 11:56:26 -07:00
|
|
|
else if(SCMPFLAT(*arg, R_PAREN))
|
2022-11-20 18:23:39 -07:00
|
|
|
--p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
2023-09-12 20:05:20 -06:00
|
|
|
|
|
|
|
#undef SCMPFLAT
|