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

View File

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

View File

@ -1,16 +1,18 @@
CFLAGS += -g -I../libpsargs
all: pscat all: pscat
pscat: pscat.o ../libpsargs/libpsargs.o pscat: pscat.o ../libpsargs/libpsargs.o
$(CC) $(CFLAGS) -g -o pscat ../libpsargs/libpsargs.o pscat.o $(CC) $(CFLAGS) -o $@ pscat.o ../libpsargs/libpsargs.o
pscat.o: pscat.c pscat.o: pscat.c psops.c
$(CC) $(CFLAGS) -c -DPSCAT=1 -I../libpsargs -o pscat.o pscat.c $(CC) $(CFLAGS) -DPSCAT=1 -c -o $@ pscat.c
pspipe: pspipe.o ../libpsargs/libpsargs.o pspipe: pspipe.o ../libpsargs/libpsargs.o
$(CC) $(CFLAGS) -g -o pspipe ../libpsargs/libpsargs.o pspipe.o $(CC) $(CFLAGS) -o $@ pspipe.o ../libpsargs/libpsargs.o
pspipe.o: pscat.c pspipe.o: pscat.c psops.c
$(CC) $(CFLAGS) -c -DPSPIPE=1 -I../libpsargs -o pspipe.o pscat.c $(CC) $(CFLAGS) -DPSPIPE=1 -c -o $@ pscat.c
../libpsargs/libpsargs.o: ../libpsargs/libpsargs.o:
$(MAKE) -C ../libpsargs $(MAKE) -C ../libpsargs

View File

@ -1,17 +1,13 @@
#include <errno.h> /* errno */ #include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), stderr */ #include <stdio.h> /* fprintf(3), stderr, "psops.c" */
#include <string.h> /* strerror(3) */ #include <string.h> /* strerror(3) */
#include <sysexits.h> /* EX_USAGE */ #include <sysexits.h> /* EX_OSERR, "psops.c" */
#include <unistd.h> /* execvp(3) */ #include <unistd.h> /* execvp(3) */
#include <sys/types.h> /* <sys/wait.h> */ #include <sys/types.h> /* <sys/wait.h> */
#include <sys/wait.h> /* wait(2) */ #include <sys/wait.h> /* wait(2) */
#include "libpsargs.h" /* check_arg(3), corresponding_arg(3) */ #include "libpsargs.h" /* corresponding_arg(3), "psops.c" */
#define L_PAREN '['
#define R_PAREN ']'
#if defined(PSCAT)
static char *program_name = "pscat"; static char *program_name = "pscat";
int int
@ -19,7 +15,7 @@ f(char **argv){
int child; int child;
char **corr; char **corr;
*(corr = corresponding_arg(argv, L_PAREN, R_PAREN)) = NULL; *(corr = corresponding_arg(argv)) = NULL;
switch(fork()){ switch(fork()){
case -1: case -1:
fprintf(stderr, "%s: %s\n", program_name, strerror(errno)); fprintf(stderr, "%s: %s\n", program_name, strerror(errno));
@ -34,61 +30,4 @@ f(char **argv){
} }
} }
#elif defined(PSPIPE) #include "psops.c"
static char *program_name = "pspipe";
/* At the start of the loop argv[0] is { '[', '\0' } and file descriptor 0 is
* the intended standard input */
int
f(char **argv){
int child;
char **corr;
static int fd[2];
int r;
*(corr = corresponding_arg(argv, L_PAREN, R_PAREN)) = NULL;
if(corr[1] != NULL){
if(pipe(fd) != 0){
fprintf(stderr,
"%s: %s: %s\n",
program_name, argv[1], strerror(errno)
);
return EX_OSERR;
}
}
if((r = fork()) == -1){
printf(stderr, "%s: %s\n", program_name, strerror(errno));
return EX_OSERR;
}
if(r == 0)
dup2(fd[0], 0);
else
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
if(r == 1)
execvp(argv[1], argv+1);
else{
argv = corr;
return *++argv == NULL
? WIFEXITED(child) * WEXITSTATUS(child)
: f(argv);
}
}
#endif
int main(int argc, char *argv[]){
if(argc != 0)
program_name = argv[0];
if(check_arg(++argv, L_PAREN, R_PAREN) == 0){
fprintf(stderr,
"Usage: %s \"[\" [utility [argument...]] \"]\" ...\n",
program_name
);
return EX_USAGE;
}else return f(argv);
}

20
pscat/psops.c Normal file
View File

@ -0,0 +1,20 @@
/* #include <stdio.h> /* fprintf(3), stderr */
#ifndef EX_USAGE
# define EX_USAGE 64 /* NetBSD sysexits(3) compat */
#endif
/* #include "libpsargs.h" /* check_arg(3) */
int main(int argc, char *argv[]){
if(argc != 0)
program_name = argv[0];
if(check_arg(++argv) == 0){
fprintf(stderr,
"Usage: %s \"[\" [utility [argument...]] \"]\" ...\n",
program_name
);
return EX_USAGE;
}else return f(argv);
}

53
pscat/pspipe.c Normal file
View File

@ -0,0 +1,53 @@
#include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), stderr, "psops.c" */
#include <string.h> /* strerror(3) */
#include <sysexits.h> /* EX_OSERR */
#include <unistd.h> /* execvp(3) */
#include <sys/types.h> /* <sys/wait.h> */
#include <sys/wait.h> /* wait(2) */
#include "libpsargs.h" /* corresponding_arg(3), "psops.c" */
static char *program_name = "pspipe";
/* At the start of the loop argv[0] is { '[', '\0' } and file descriptor 0 is
* the intended standard input */
int
f(char **argv){
static int child;
char **corr;
int fd[2];
int r;
*(corr = corresponding_arg(argv)) = NULL;
if(corr[1] != NULL){
if(pipe(fd) != 0){
fprintf(stderr,
"%s: %s: %s\n",
program_name, argv[1], strerror(errno)
);
return EX_OSERR;
}
if((r = fork()) == -1){
fprintf(stderr, "%s: %s\n",
program_name, strerror(errno)
);
return EX_OSERR;
}
if(r == 0)
dup2(fd[0], 0);
else
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
if(r == 1)
execvp(argv[1], argv+1);
}
argv = corr;
return *++argv == NULL
? WEXITSTATUS(child)
: f(corr + 1);
}
#include "psops.c"