1. Evan Gates
  2. pcw

Commits

Evan Gates  committed b83b271

i add -d and -f flags to cw for cut(1) like behavior

  • Participants
  • Parent commits 5bfedfa
  • Branches default

Comments (0)

Files changed (3)

File cw.c

View file
 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
 
-static FILE *in, *out;
+static FILE *in = NULL, *out = NULL;
 static char buf[4096];
 static struct winsize ws;
-static char *margin = NULL;
+static char *delim = NULL, *margin = NULL;
+static int field = 0;
 
 static int break_len(char *start, char *end, int def)
 {
 static void print_line(char *line)
 {
 	char m, *p = NULL;
-	int len, wlen, w, cols = ws.ws_col - 1;
+	int len, wlen, w, i, cols = ws.ws_col - 1;
+
+	if (delim && field > 0)
+		for (i = 1, p = line; i < field && p; i++, p = strpbrk(p, delim))
+			if (*(p + 1))
+				p++;
+	if (p)
+		line = (*p == ' ') ? p + 1 : p;
 
 	len = strlen(line);
 	if (margin) p = strpbrk(line, margin);
 	fflush(stdout);
 }
 
+static void usage(void)
+{
+	printf("Usage: cw -i infile -o outfile [-d delim] [-f field] [-m margin] [-v]\n");
+	exit(1);
+}
+
 int main(int argc, char **argv)
 {
-	int evq, in_wd, out_wd, len;
+	int evq, len, i;
+	int in_wd = 0, out_wd = 0;
 	struct inotify_event *cur;
 	fd_set rd;
 
-	if (argc == 4) {
-		margin = argv[3];
-	} else if (argc != 3) {
-		printf("Usage: cw infile outfile [margin]\n");
-		printf("cw-"VERSION" © Evan Gates\n");
-		exit(1);
-	}
-	if (signal(SIGWINCH, sigwinch) == SIG_ERR)
-		err(1, "failed to install sigwinch handler");
-
-	if ((in = fopen(argv[1], "r")) == NULL)
-		err(1, "failed to fopen %s", argv[1]);
-
 	if ((evq = inotify_init()) < 0)
 		err(1, "failed inotify_init()");
 
-	if ((in_wd = inotify_add_watch(evq, argv[1], IN_MODIFY)) < 0)
-		err(1, "failed inotify_add_watch()");
+	for (i = 1; i + 1 < argc; i++) {
+		if (argv[i][0] != '-' || argv[i][1] == '\0' || argv[i][2] != '\0')
+			usage();
+		switch (argv[i][1]) {
+			case 'd' : delim  = argv[++i]; break;
+			case 'f' : field  = strtol(argv[++i], NULL, 10); break;
+			case 'i' :
+				if ((in = fopen(argv[++i], "r")) == NULL)
+					err(1, "failed to fopen %s", argv[i]);
+				if ((in_wd = inotify_add_watch(evq, argv[i], IN_MODIFY)) < 0)
+					err(1, "failed inotify_add_watch on %s", argv[i]);
+				break;
+			case 'm' : margin = argv[++i]; break;
+			case 'o' :
+				if ((out = fopen(argv[++i], "w")) == NULL)
+					err(1, "failed to fopen %s", argv[i]);
+				if ((out_wd = inotify_add_watch(evq, argv[i], IN_CLOSE_NOWRITE)) < 0)
+					err(1, "failed inotify_add_watch on %s", argv[i]);
+				break;
+			case 'v' : printf("cw-"VERSION" © Evan Gates\n"); exit(1);
+			default  : usage(); break;
+		}
+	}
+	if (i != argc || in == NULL || out == NULL || in_wd == 0 || out_wd == 0)
+		usage();
 
-	if ((out_wd = inotify_add_watch(evq, argv[2], IN_CLOSE_NOWRITE)) < 0)
-		err(1, "failed inotify_add_watch()");
+	if (signal(SIGWINCH, sigwinch) == SIG_ERR)
+		err(1, "failed to install sigwinch handler");
 
 	sigwinch(0); // set winsize and print what we start with
 
-	if ((out = fopen(argv[2], "w")) == NULL)
-		err(1, "failed to fopen %s", argv[2]);
-
 	for (;;) {
 		FD_ZERO(&rd);
 		FD_SET(evq, &rd);

File extras/cw_color.sh

View file
 
 NICK="$USER"
 MARGIN='>|!'
+FIELD=2
+DELIM=" "
 
 while [ $# -gt 0 ]; do
 	case $1 in
 			;;
 		-n) NICK="${2:-$NICK}"
 			;;
+		-f) FIELD="$2"
+			;;
+		-d) DELIM="$2"
+			;;
 		*)  echo "bad argument $1"
 			exit 2
 	esac
 # change lines that are as of yet unmatched: color time blue
 # color nick red and add bell
 
-cw "$IN" "$OUT" "$MARGIN" |
+cw -i "$IN" -o "$OUT" -m "$MARGIN" -d "$DELIM" -f "$FIELD" |
 	sed \
 		-e "s/^\( *!.*\)$/${blue}\1${reset}/" \
-		-e "s/^\([-0-9]\{10\}\) \([:0-9]\{5\}\)\( *-!-.*\)$/${blue}\1 \2\3${reset}/" \
-		-e "s/^\([-0-9]\{10\}\) \([:0-9]\{5\}\)\( *<\?\)\( *[^ $MARGIN]*\)\( \?[$MARGIN]\) ACTION\(.*\)$/${blue}\1 \2${reset}\3${cyan}${magenta}\4\6${reset}/" \
-		-e "s/^\([-0-9]\{10\}\) \([:0-9]\{5\}\)\( *<\?\)\( *[^ $MARGIN]*\)\( \?[$MARGIN]\)/${blue}\1 \2${reset}\3${cyan}\4${reset}\5/" \
-		-e "s/^\([-0-9]\{10\}\) \([:0-9]\{5\}\)/${blue}\1 \2${reset}/" \
+		-e "s/^\([-0-9]\{10\} \)\?\([:0-9]\{5\}\)\?\( *-!-.*\)$/${blue}\1\2\3${reset}/" \
+		-e "s/^\([-0-9]\{10\} \)\?\([:0-9]\{5\}\)\?\( *<\?\)\( *[^ $MARGIN]*\)\( \?[$MARGIN]\) ACTION\(.*\)$/${blue}\1\2${reset}\3${cyan}${magenta}\4\6${reset}/" \
+		-e "s/^\([-0-9]\{10\} \)\?\([:0-9]\{5\}\)\?\( *<\?\)\( *[^ $MARGIN]*\)\( \?[$MARGIN]\)/${blue}\1\2${reset}\3${cyan}\4${reset}\5/" \
+		-e "s/^\([-0-9]\{10\} \)\?\([:0-9]\{5\}\)\?/${blue}\1\2${reset}/" \
 		-e "s/$NICK/\a${red}&${reset}/g"
 
 # vim: set ts=4 sw=4 noexpandtab:

File pcw.1

View file
 .I dir
 .YS
 .SY cw
-.I infile
-.I outfile
-.OP margin
+.OP \-d delim
+.OP \-f field
+.OP \-i infile
+.OP \-m margin
+.OP \-o outfile
 .SY
 
 .SH OPTIONS
 .I dir
 the root of the directory tree to monitor (ii defaults to ~/irc)
 .TP
-.I infile
+.BI \-d " delim"
+a string containing all characters to use as delimiters when deciding which field to start printing
+.TP
+.BI \-f " field"
+a number representing which field to start printing at (0 and 1 both mean the first)
+.TP
+.BI \-i " infile"
 the file to monitor and print changes to stdout
 .TP
-.I outfile
-the file to write user input to
-.TP
-.B margin
+.BI \-m " margin"
 a string of the characters used to decide where to wrap (try '>!' for default ii, '|!' with the emg patch)
 .TP
 .BI \-n " nick"
 set the nickname for use with the external command
 .TP
+.BI \-o " outfile"
+the file to write user input to
+.TP
 .B \-v
 print version information and exit