separate out libpsargs
This commit is contained in:
1
libpsargs/Makefile
Normal file
1
libpsargs/Makefile
Normal file
@@ -0,0 +1 @@
|
||||
libpsargs.o:
|
||||
58
libpsargs/libpsargs.c
Normal file
58
libpsargs/libpsargs.c
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <stddef.h> /* size_t */
|
||||
|
||||
#include "libpsargs.h"
|
||||
|
||||
/* 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
|
||||
check_arg(char **args, char l, char r){
|
||||
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'){
|
||||
s += (args[0][0] == l) - (args[0][0] == r);
|
||||
terms += (args[0][0] == r && s == NORMAL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* make sure nothing's open */
|
||||
return terms * (s == NORMAL);
|
||||
}
|
||||
|
||||
char **
|
||||
corresponding_arg(char **arg, char l, char r){
|
||||
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 */
|
||||
if(SCMPFLAT(*arg, l))
|
||||
++p;
|
||||
else if(SCMPFLAT(*arg, r))
|
||||
--p;
|
||||
}
|
||||
|
||||
return arg;
|
||||
}
|
||||
|
||||
#undef SCMPFLAT
|
||||
17
libpsargs/libpsargs.h
Normal file
17
libpsargs/libpsargs.h
Normal file
@@ -0,0 +1,17 @@
|
||||
/* 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 ']').
|
||||
* 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);
|
||||
|
||||
/* 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.
|
||||
* Returns the location of the argument with the corresponding left grouping
|
||||
* symbol within argv. */
|
||||
char **corresponding_arg(char **arg, char l, char r);
|
||||
Reference in New Issue
Block a user