forked from bonsai/harakit
		
	dj(1): more refactor (get rid of ep pun)
This commit is contained in:
		
							parent
							
								
									b70b356ce5
								
							
						
					
					
						commit
						e65f6b650d
					
				
							
								
								
									
										101
									
								
								src/dj.c
									
									
									
									
									
								
							
							
						
						
									
										101
									
								
								src/dj.c
									
									
									
									
									
								
							@ -156,6 +156,9 @@ Io_fdseek(struct Io *io){
 | 
			
		||||
			|| (!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1))
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	if(io->fl == write_flags)
 | 
			
		||||
		memset(io->buf, '\0', io->bs);
 | 
			
		||||
 | 
			
		||||
	if(io->fl == write_flags){
 | 
			
		||||
		memset(io->buf, '\0', io->bs);
 | 
			
		||||
		/* We're going to cheat and use bufuse as the retval for write(2),
 | 
			
		||||
@ -273,7 +276,7 @@ usage(void){
 | 
			
		||||
int main(int argc, char *argv[]){
 | 
			
		||||
	int align; /* low 8b used, negative if no alignment is being done */
 | 
			
		||||
	int count; /* 0 if dj(1) runs until no more reads are possible */
 | 
			
		||||
	struct Io ep[2]; /* "engineered pipe"; also "extended play", for the DJ */
 | 
			
		||||
	struct Io io[2];
 | 
			
		||||
	char *fmt_output; /* fmt_asv (default) or fmt_human (-H) */
 | 
			
		||||
	char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
 | 
			
		||||
	int c;
 | 
			
		||||
@ -284,10 +287,10 @@ int main(int argc, char *argv[]){
 | 
			
		||||
	count = 0;
 | 
			
		||||
	fmt_output = fmt_asv;
 | 
			
		||||
	noerror = 0;
 | 
			
		||||
	ep[0].fl = read_flags;
 | 
			
		||||
	Io_setdefaults(&ep[0]);
 | 
			
		||||
	ep[1].fl = write_flags;
 | 
			
		||||
	Io_setdefaults(&ep[1]);
 | 
			
		||||
	io[0].fl = read_flags;
 | 
			
		||||
	Io_setdefaults(&io[0]);
 | 
			
		||||
	io[1].fl = write_flags;
 | 
			
		||||
	Io_setdefaults(&io[1]);
 | 
			
		||||
 | 
			
		||||
	if(argc > 0){
 | 
			
		||||
		program_name = argv[0];
 | 
			
		||||
@ -296,12 +299,12 @@ int main(int argc, char *argv[]){
 | 
			
		||||
			case 'i': case 'o':
 | 
			
		||||
				i = (c == 'o');
 | 
			
		||||
				if(optarg[0] == '-' && optarg[1] == '\0'){ /* optarg == "-" */
 | 
			
		||||
					ep[i].fd = (i == 0) ? STDIN_FILENO : STDOUT_FILENO;
 | 
			
		||||
					ep[i].fn = (i == 0) ? stdin_name   : stdout_name;
 | 
			
		||||
					io[i].fd = (i == 0) ? STDIN_FILENO : STDOUT_FILENO;
 | 
			
		||||
					io[i].fn = (i == 0) ? stdin_name   : stdout_name;
 | 
			
		||||
					break;
 | 
			
		||||
				}else if(Io_fdopen(&ep[i], optarg) != -1)
 | 
			
		||||
				}else if(Io_fdopen(&io[i], optarg) != -1)
 | 
			
		||||
					break;
 | 
			
		||||
				terminate(ep);
 | 
			
		||||
				terminate(io);
 | 
			
		||||
				return oserr(optarg);
 | 
			
		||||
			case 'A': align = '\0'; break;
 | 
			
		||||
			case 'n': noerror = 1; break;
 | 
			
		||||
@ -317,82 +320,82 @@ int main(int argc, char *argv[]){
 | 
			
		||||
					break;
 | 
			
		||||
				i = isupper(c);
 | 
			
		||||
				c = tolower(c);
 | 
			
		||||
				if((c == 'b' && (ep[i].bs = parse(optarg)) > 0)
 | 
			
		||||
						|| (c == 's' && (ep[i].seek = parse(optarg)) >= 0))
 | 
			
		||||
				if((c == 'b' && (io[i].bs = parse(optarg)) > 0)
 | 
			
		||||
						|| (c == 's' && (io[i].seek = parse(optarg)) >= 0))
 | 
			
		||||
					break;
 | 
			
		||||
				/* FALLTHROUGH */
 | 
			
		||||
			default:
 | 
			
		||||
				terminate(ep);
 | 
			
		||||
				terminate(io);
 | 
			
		||||
				return usage();
 | 
			
		||||
			}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if(argc > optind){
 | 
			
		||||
		terminate(ep);
 | 
			
		||||
		terminate(io);
 | 
			
		||||
		return usage();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for(i = 0; i <= 1; ++i){
 | 
			
		||||
		if(Io_bufalloc(&ep[i]) == NULL){
 | 
			
		||||
	for(i = 0; i < 2; ++i){
 | 
			
		||||
		if(Io_bufalloc(&io[i]) == NULL){
 | 
			
		||||
			fprintf(stderr, "%s: Failed to allocate %d bytes\n",
 | 
			
		||||
				program_name, ep[i].bs);
 | 
			
		||||
			terminate(ep);
 | 
			
		||||
				program_name, io[i].bs);
 | 
			
		||||
			terminate(io);
 | 
			
		||||
			return EX_OSERR;
 | 
			
		||||
		}else if(ep[i].seek > 0)
 | 
			
		||||
			Io_fdseek(&ep[i]);
 | 
			
		||||
			if(ep[i].seek > 0){
 | 
			
		||||
				terminate(ep);
 | 
			
		||||
				return oserr(ep[i].fn);
 | 
			
		||||
		}else if(io[i].seek > 0)
 | 
			
		||||
			Io_fdseek(&io[i]);
 | 
			
		||||
			if(io[i].seek > 0){
 | 
			
		||||
				terminate(io);
 | 
			
		||||
				return oserr(io[i].fn);
 | 
			
		||||
			}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	do{	/* read */
 | 
			
		||||
		Io_read(&ep[0]);
 | 
			
		||||
		if(!noerror && ep[0].bufuse == 0)
 | 
			
		||||
			Io_read(&ep[0]); /* second chance */
 | 
			
		||||
		if(ep[0].bufuse == 0) /* that's all she wrote */
 | 
			
		||||
		Io_read(&io[0]);
 | 
			
		||||
		if(!noerror && io[0].bufuse == 0)
 | 
			
		||||
			Io_read(&io[0]); /* second chance */
 | 
			
		||||
		if(io[0].bufuse == 0) /* that's all she wrote */
 | 
			
		||||
			break;
 | 
			
		||||
		else if(ep[0].bufuse < ep[0].bs){
 | 
			
		||||
			++ep[0].prec;
 | 
			
		||||
		else if(io[0].bufuse < io[0].bs){
 | 
			
		||||
			++io[0].prec;
 | 
			
		||||
			fprintf(stderr, "%s: Partial read:\n\t", program_name);
 | 
			
		||||
			output(ep, fmt_output);
 | 
			
		||||
			output(io, fmt_output);
 | 
			
		||||
			if(!noerror)
 | 
			
		||||
				count = 1;
 | 
			
		||||
			if(align >= 0)
 | 
			
		||||
				Io_bufrpad(&ep[0], align);
 | 
			
		||||
				Io_bufrpad(&io[0], align);
 | 
			
		||||
		}else
 | 
			
		||||
			++ep[0].rec;
 | 
			
		||||
			++io[0].rec;
 | 
			
		||||
 | 
			
		||||
		/* write */
 | 
			
		||||
		do{	if(ep[1].bs > ep[0].bs){ /* io[1].bs > io[0].bs */
 | 
			
		||||
				Io_bufxapp(&ep[1], &ep[0]);
 | 
			
		||||
				if(ep[0].bs + ep[1].bufuse <= ep[1].bs && count != 1)
 | 
			
		||||
		do{	if(io[1].bs > io[0].bs){ /* io[1].bs > io[0].bs */
 | 
			
		||||
				Io_bufxapp(&io[1], &io[0]);
 | 
			
		||||
				if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1)
 | 
			
		||||
					continue; /* we could write more */
 | 
			
		||||
			}else
 | 
			
		||||
				Io_bufxfer(&ep[1], &ep[0], MIN(ep[0].bufuse, ep[1].bs));
 | 
			
		||||
				Io_bufxfer(&io[1], &io[0], MIN(io[0].bufuse, io[1].bs));
 | 
			
		||||
 | 
			
		||||
			c = ep[1].bufuse;
 | 
			
		||||
			Io_write(&ep[1]);
 | 
			
		||||
			if(!noerror && ep[1].bufuse == c)
 | 
			
		||||
				Io_write(&ep[1]); /* second chance */
 | 
			
		||||
			if(c == ep[1].bufuse){ /* no more love */
 | 
			
		||||
			c = io[1].bufuse;
 | 
			
		||||
			Io_write(&io[1]);
 | 
			
		||||
			if(!noerror && io[1].bufuse == c)
 | 
			
		||||
				Io_write(&io[1]); /* second chance */
 | 
			
		||||
			if(c == io[1].bufuse){ /* no more love */
 | 
			
		||||
				count = 1;
 | 
			
		||||
				break;
 | 
			
		||||
			}else if(c > ep[1].bufuse && ep[1].bufuse > 0){
 | 
			
		||||
				ep[1].prec += 1;
 | 
			
		||||
			}else if(c > io[1].bufuse && io[1].bufuse > 0){
 | 
			
		||||
				io[1].prec += 1;
 | 
			
		||||
				fprintf(stderr, "%s: Partial write:\n\t", program_name);
 | 
			
		||||
				output(ep, fmt_output);
 | 
			
		||||
				output(io, fmt_output);
 | 
			
		||||
				if(!noerror)
 | 
			
		||||
					count = 1;
 | 
			
		||||
			}else if(ep[1].bufuse == 0 && c < ep[1].bs)
 | 
			
		||||
				++ep[1].prec;
 | 
			
		||||
			}else if(io[1].bufuse == 0 && c < io[1].bs)
 | 
			
		||||
				++io[1].prec;
 | 
			
		||||
			else
 | 
			
		||||
				++ep[1].rec;
 | 
			
		||||
		}while(ep[0].bufuse > 0);
 | 
			
		||||
				++io[1].rec;
 | 
			
		||||
		}while(io[0].bufuse > 0);
 | 
			
		||||
	}while(count == 0 || --count > 0);
 | 
			
		||||
 | 
			
		||||
	output(ep, fmt_output);
 | 
			
		||||
	terminate(ep);
 | 
			
		||||
	output(io, fmt_output);
 | 
			
		||||
	terminate(io);
 | 
			
		||||
 | 
			
		||||
	return EX_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user