mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 00:56:10 +00:00 
			
		
		
		
	Add q: format prefix to escape sh(1) special characters. Suggested by
someone ages ago and then more recently in GitHub issue 1449.
This commit is contained in:
		
							
								
								
									
										29
									
								
								format.c
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								format.c
									
									
									
									
									
								
							@@ -94,6 +94,7 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2)
 | 
				
			|||||||
#define FORMAT_BASENAME 0x2
 | 
					#define FORMAT_BASENAME 0x2
 | 
				
			||||||
#define FORMAT_DIRNAME 0x4
 | 
					#define FORMAT_DIRNAME 0x4
 | 
				
			||||||
#define FORMAT_SUBSTITUTE 0x8
 | 
					#define FORMAT_SUBSTITUTE 0x8
 | 
				
			||||||
 | 
					#define FORMAT_QUOTE 0x10
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Entry in format tree. */
 | 
					/* Entry in format tree. */
 | 
				
			||||||
struct format_entry {
 | 
					struct format_entry {
 | 
				
			||||||
@@ -754,6 +755,23 @@ format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
 | 
				
			|||||||
	fe->value = NULL;
 | 
						fe->value = NULL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Quote special characters in string. */
 | 
				
			||||||
 | 
					static char *
 | 
				
			||||||
 | 
					format_quote(const char *s)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char	*cp;
 | 
				
			||||||
 | 
						char		*out, *at;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						at = out = xmalloc(strlen(s) * 2 + 1);
 | 
				
			||||||
 | 
						for (cp = s; *cp != '\0'; cp++) {
 | 
				
			||||||
 | 
							if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
 | 
				
			||||||
 | 
								*at++ = '\\';
 | 
				
			||||||
 | 
							*at++ = *cp;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						*at = '\0';
 | 
				
			||||||
 | 
						return (out);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Find a format entry. */
 | 
					/* Find a format entry. */
 | 
				
			||||||
static char *
 | 
					static char *
 | 
				
			||||||
format_find(struct format_tree *ft, const char *key, int modifiers)
 | 
					format_find(struct format_tree *ft, const char *key, int modifiers)
 | 
				
			||||||
@@ -836,6 +854,11 @@ found:
 | 
				
			|||||||
		copy = xstrdup(dirname(saved));
 | 
							copy = xstrdup(dirname(saved));
 | 
				
			||||||
		free(saved);
 | 
							free(saved);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if (modifiers & FORMAT_QUOTE) {
 | 
				
			||||||
 | 
							saved = copy;
 | 
				
			||||||
 | 
							copy = xstrdup(format_quote(saved));
 | 
				
			||||||
 | 
							free(saved);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return (copy);
 | 
						return (copy);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -976,6 +999,12 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
 | 
				
			|||||||
		modifiers |= FORMAT_TIMESTRING;
 | 
							modifiers |= FORMAT_TIMESTRING;
 | 
				
			||||||
		copy += 2;
 | 
							copy += 2;
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
 | 
						case 'q':
 | 
				
			||||||
 | 
							if (copy[1] != ':')
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							modifiers |= FORMAT_QUOTE;
 | 
				
			||||||
 | 
							copy += 2;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
	case 's':
 | 
						case 's':
 | 
				
			||||||
		sep = copy[1];
 | 
							sep = copy[1];
 | 
				
			||||||
		if (sep == ':' || !ispunct((u_char)sep))
 | 
							if (sep == ':' || !ispunct((u_char)sep))
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								tmux.1
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								tmux.1
									
									
									
									
									
								
							@@ -3676,6 +3676,10 @@ prefixes are
 | 
				
			|||||||
and
 | 
					and
 | 
				
			||||||
.Xr dirname 3
 | 
					.Xr dirname 3
 | 
				
			||||||
of the variable respectively.
 | 
					of the variable respectively.
 | 
				
			||||||
 | 
					.Ql q:
 | 
				
			||||||
 | 
					will escape
 | 
				
			||||||
 | 
					.Xr sh 1
 | 
				
			||||||
 | 
					special characters.
 | 
				
			||||||
A prefix of the form
 | 
					A prefix of the form
 | 
				
			||||||
.Ql s/foo/bar/:
 | 
					.Ql s/foo/bar/:
 | 
				
			||||||
will substitute
 | 
					will substitute
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user