WIP: scroll(1) #127
							
								
								
									
										98
									
								
								src/pg.c
									
									
									
									
									
								
							
							
						
						
									
										98
									
								
								src/pg.c
									
									
									
									
									
								
							@ -23,7 +23,7 @@
 | 
				
			|||||||
                    * fprintf(3), fputc(3), perror(3), stderr, stdin, stdout,
 | 
					                    * fprintf(3), fputc(3), perror(3), stderr, stdin, stdout,
 | 
				
			||||||
                    * EOF, FILE, NULL */
 | 
					                    * EOF, FILE, NULL */
 | 
				
			||||||
#include <stdlib.h> /* size_t */
 | 
					#include <stdlib.h> /* size_t */
 | 
				
			||||||
#include <string.h> /* strchr(3), strcmp(3), strerror(3), strtok(3) */
 | 
					#include <string.h> /* strchr(3), strcmp(3) */
 | 
				
			||||||
#include <unistd.h> /* getopt(3) */
 | 
					#include <unistd.h> /* getopt(3) */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Commands start with cmd_. They take an argc and NULL-terminated argv, like
 | 
					/* Commands start with cmd_. They take an argc and NULL-terminated argv, like
 | 
				
			||||||
@ -36,6 +36,14 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static char *whitespace = " \n\r\t\v";
 | 
					static char *whitespace = " \n\r\t\v";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//static
 | 
				
			||||||
 | 
					struct Tube {
 | 
				
			||||||
 | 
						char *name;   // command line
 | 
				
			||||||
 | 
						FILE *in;     // process stdin
 | 
				
			||||||
 | 
						FILE *out;    // process stdoout
 | 
				
			||||||
 | 
						size_t index; // in pipeline
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct {
 | 
					static struct {
 | 
				
			||||||
	size_t quantity;
 | 
						size_t quantity;
 | 
				
			||||||
	enum { LINES = 0, BYTES = 1 } type;
 | 
						enum { LINES = 0, BYTES = 1 } type;
 | 
				
			||||||
@ -45,57 +53,69 @@ static char *prompt = ": ";
 | 
				
			|||||||
static char *program_name = "pg";
 | 
					static char *program_name = "pg";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static char *
 | 
					static char *
 | 
				
			||||||
permute_out(char *str, size_t i) {
 | 
					permute_out(char *s, size_t i) {
 | 
				
			||||||
	for (
 | 
						for (
 | 
				
			||||||
		;
 | 
							;
 | 
				
			||||||
		str[i - 1] != '\0';
 | 
							s[i] != '\0';
 | 
				
			||||||
		str[i - 1] = str[i], ++i
 | 
							s[i] = s[i + 1], ++i
 | 
				
			||||||
	);
 | 
						);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return str;
 | 
						return s;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* strtok(3p), but supports double-quotes and escapes (but only for escaping
 | 
					/* strtok(3p), but supports double-quotes and escapes (but only for escaping
 | 
				
			||||||
 * quotes). UTF-8 is safe only in str. Unmatched quotes in str are considered
 | 
					 * quotes).  Unmatched quotes in str are considered literal. The behavior of
 | 
				
			||||||
 * literal. The behavior of strtok_quoted when '"' or '\\' are in sep is
 | 
					 * strtok_quoted when '\'', '"', or '\\' are in sep is undefined. Use of UTF-8
 | 
				
			||||||
 * undefined. */
 | 
					 * separators with strtok_quoted is undefined. */
 | 
				
			||||||
/* TODO: Seems to only ever return NULL. */
 | 
					 | 
				
			||||||
static char *
 | 
					static char *
 | 
				
			||||||
strtok_quoted(char *str, char *sep) {
 | 
					strtok_quoted(char *str, char *sep) {
 | 
				
			||||||
	static char *s;
 | 
						static char *s;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (str != NULL) { s = str; }
 | 
						if (str != NULL) { s = str; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	while (strchr(sep, *s) == NULL) {
 | 
						while (strchr(sep, *s) != NULL) { // skip beginning whitespace
 | 
				
			||||||
		if(*++s == '\0') { return NULL; } /* no remaining except seps */
 | 
							if(*++s == '\0') { return NULL; } // no remaining except seps
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		bool in_escape = 0;
 | 
							bool in_escape = 0; // previous char was '\\'
 | 
				
			||||||
		int in_quotes = -1; /* index of quote, or -1 if none */
 | 
							char quote = '\0';  // quotation mark used, or '\0' if none
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for (size_t i = 0; s[i] != '\0'; ++i)
 | 
							for (int i = 0; s[i] != '\0'; ++i)
 | 
				
			||||||
			switch (s[i]) {
 | 
								switch (s[i]) {
 | 
				
			||||||
				case '\\':
 | 
									case '\\':
 | 
				
			||||||
					/* if literal \\, permute out a backslash */
 | 
										// if literal "\\", permute out a backslash
 | 
				
			||||||
					if (in_escape) { permute_out(s, i--); }
 | 
										if (in_escape) { (void)permute_out(s, i--); }
 | 
				
			||||||
					in_escape = !in_escape;
 | 
										in_escape = !in_escape;
 | 
				
			||||||
					break;
 | 
										break;
 | 
				
			||||||
				case '"':
 | 
									case '\'': case '"':
 | 
				
			||||||
					if (in_escape) { // \"
 | 
										if (in_escape) { // \"
 | 
				
			||||||
						s[i] = s[i - 1];
 | 
											s[i] = s[i - 1];
 | 
				
			||||||
						(void)permute_out(s, i--); // permute out backslash
 | 
											(void)permute_out(s, i--); // permute out backslash
 | 
				
			||||||
					} else if (in_quotes != -1) {
 | 
										} else if (s[i] == quote) {
 | 
				
			||||||
						(void)permute_out(s, in_quotes); --i; // first "
 | 
											quote = '\0';
 | 
				
			||||||
						(void)permute_out(s, i--); // second "
 | 
											(void)permute_out(s, i--); // second quote
 | 
				
			||||||
						in_quotes = -1;
 | 
										} else {
 | 
				
			||||||
					} else { in_quotes = i; }
 | 
											quote = s[i];
 | 
				
			||||||
 | 
											if (strchr(&s[i + 1], quote) != NULL) { // has a match
 | 
				
			||||||
 | 
												(void)permute_out(s, i--); // permute out lquote
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
					break;
 | 
										break;
 | 
				
			||||||
				case '\0': return s;
 | 
									case '\0': return s;
 | 
				
			||||||
				default:
 | 
									default:
 | 
				
			||||||
					if (!in_escape && strchr(sep, s[i]) != NULL) {
 | 
										if (!in_escape
 | 
				
			||||||
						s[i] = '\0';
 | 
										    	&& quote == '\0'
 | 
				
			||||||
						return s;
 | 
										    	&& (strchr(sep, s[i]) != NULL || s[i] == '\0')) {
 | 
				
			||||||
 | 
											char *t; // start of current token
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
											t = s;
 | 
				
			||||||
 | 
											s = s[i] != '\0'
 | 
				
			||||||
 | 
												? &t[i + 1] // store start of next token,
 | 
				
			||||||
 | 
												: &t[i];    // or the address of the nul if found
 | 
				
			||||||
 | 
											s[i] = '\0'; // NUL terminate current token
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
											return t;
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -173,19 +193,16 @@ builtins[] = {
 | 
				
			|||||||
 * command line. */
 | 
					 * command line. */
 | 
				
			||||||
static int
 | 
					static int
 | 
				
			||||||
cmdline_exec(struct CmdMap *map, char *cmdline, char **envp) {
 | 
					cmdline_exec(struct CmdMap *map, char *cmdline, char **envp) {
 | 
				
			||||||
	/* Command line word splitting is naive and based on whitespace ONLY; no
 | 
					 | 
				
			||||||
	 * fancy quoting or escaping here. Adding that would (ideally) entail
 | 
					 | 
				
			||||||
	 * replacing strtok(3) with something specific to this task. */
 | 
					 | 
				
			||||||
	static int argc;
 | 
						static int argc;
 | 
				
			||||||
	static char *argv[ARGV_MAX];
 | 
						static char *argv[ARGV_MAX];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ((argv[(argc = 0)] = strtok(cmdline, whitespace)) == NULL) {
 | 
						if ((argv[(argc = 0)] = strtok_quoted(cmdline, whitespace)) == NULL) {
 | 
				
			||||||
		while (cmdline[0] != '\0') { cmdline = &cmdline[1]; }
 | 
							while (cmdline[0] != '\0') { cmdline = &cmdline[1]; }
 | 
				
			||||||
		argv[argc] = cmdline;
 | 
							argv[argc] = cmdline;
 | 
				
			||||||
		argv[++argc] = NULL;
 | 
							argv[++argc] = NULL;
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		while (
 | 
							while (
 | 
				
			||||||
			(argv[++argc] = strtok(NULL, whitespace)) != NULL
 | 
								(argv[++argc] = strtok_quoted(NULL, whitespace)) != NULL
 | 
				
			||||||
			&& argc < ARGV_MAX
 | 
								&& argc < ARGV_MAX
 | 
				
			||||||
		);
 | 
							);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -231,33 +248,28 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if(argc > optind) { return usage(program_name); }
 | 
						if (argc > optind) { return usage(program_name); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ((t = fopen("/dev/tty", "rb")) == NULL) {
 | 
						if ((t = fopen("/dev/tty", "rb")) == NULL) {
 | 
				
			||||||
		(void)fprintf(
 | 
							perror(program_name);
 | 
				
			||||||
			stderr,
 | 
					 | 
				
			||||||
			"%s: /dev/tty: %s\n",
 | 
					 | 
				
			||||||
			program_name,
 | 
					 | 
				
			||||||
			strerror(errno)
 | 
					 | 
				
			||||||
		);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return EX_OSERR;
 | 
							return EX_OSERR;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (;;) {
 | 
						for (;;) {
 | 
				
			||||||
		if ( /* prompt and receive */
 | 
							if (fputs(prompt, stderr) == EOF) { return ioerr(program_name); }
 | 
				
			||||||
			fputs(prompt, stderr) == EOF
 | 
					 | 
				
			||||||
		    || fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t) == NULL
 | 
					 | 
				
			||||||
		) { return ioerr(program_name); }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (strchr((char *)cmd, '\n') == NULL) { /* fast-forward stream */
 | 
							// if the line...
 | 
				
			||||||
 | 
							if (fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t) != NULL) {
 | 
				
			||||||
 | 
								if (strchr((char *)cmd, '\n') == NULL) { // was taken incompletely
 | 
				
			||||||
				int c;
 | 
									int c;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			while ((c = fgetc(t)) != '\n') {
 | 
									while ((c = fgetc(t)) != '\n') { // ...fast-forward stream
 | 
				
			||||||
					if (c == EOF) { break; }
 | 
										if (c == EOF) { break; }
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							} else { fputc('\n', stdout); } // EOF at start of line; finish prompt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (feof(t)) { return EX_OK; }
 | 
							if (feof(t)) { return EX_OK; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user