diff options
| author | Thomas Voss <mail@thomasvoss.com> | 2024-01-11 22:47:32 +0100 | 
|---|---|---|
| committer | Thomas Voss <mail@thomasvoss.com> | 2024-01-11 22:47:32 +0100 | 
| commit | 350478c94d6f112185fb3c007e6ac47c6ef80da6 (patch) | |
| tree | 6dfeaafb72fe101dd2e4fdc265c8bd3b5d74df5c | |
| parent | 699c389edf171eef3178dfeae989a31e83cc9b70 (diff) | |
Make REG_NEWLINE optional with -n
| -rw-r--r-- | grab.c | 20 | 
1 files changed, 14 insertions, 6 deletions
| @@ -64,6 +64,7 @@ static char *xstrchrnul(const char *, char);  static char delim = '\n';  static int rv = EXIT_SUCCESS; +static bool nflag;  static const cmd_func op_table[UCHAR_MAX] = {  	['g'] = cmdg, @@ -81,7 +82,7 @@ static void  usage(const char *s)  {  	fprintf(stderr, -	        "Usage: %s [-z] pattern [file ...]\n" +	        "Usage: %s [-nz] pattern [file ...]\n"  	        "       %s -h\n",  	        s, s);  	exit(EXIT_FAILURE); @@ -93,8 +94,9 @@ main(int argc, char **argv)  	int opt;  	struct ops ops;  	struct option longopts[] = { -		{"help", no_argument, 0, 'h'}, -		{"zero", no_argument, 0, 'z'}, +		{"help",    no_argument, 0, 'h'}, +		{"newline", no_argument, 0, 'n'}, +		{"zero",    no_argument, 0, 'z'},  	};  	argv[0] = basename(argv[0]); @@ -103,11 +105,14 @@ main(int argc, char **argv)  	setlocale(LC_ALL, ""); -	while ((opt = getopt_long(argc, argv, "hz", longopts, NULL)) != -1) { +	while ((opt = getopt_long(argc, argv, "hnz", longopts, NULL)) != -1) {  		switch (opt) {  		case 'h':  			execlp("man", "man", "1", argv[0], NULL);  			die("execlp: man 1 %s", argv[0]); +		case 'n': +			nflag = true; +			break;  		case 'z':  			delim = '\0';  			break; @@ -320,7 +325,7 @@ regex_t  mkregex(char *s, size_t n)  {  	char c = s[n]; -	int ret; +	int ret, cflags;  	regex_t r;  	for (size_t i = 0; i < n - 1; i++) { @@ -336,7 +341,10 @@ mkregex(char *s, size_t n)  	}  	s[n] = 0; -	if ((ret = regcomp(&r, s, REG_EXTENDED | REG_NEWLINE)) != 0) { +	cflags = REG_EXTENDED; +	if (nflag) +		cflags |= REG_NEWLINE; +	if ((ret = regcomp(&r, s, cflags)) != 0) {  		char emsg[128];  		regerror(ret, &r, emsg, sizeof(emsg));  		diex("Failed to compile regex: %s", emsg); |