int vsb_len(struct vsb *);
int vsb_done(const struct vsb *);
void vsb_delete(struct vsb *);
+void vsb_quote(struct vsb *s, const char *p, int how);
#ifdef __cplusplus
};
#endif
void
cli_quote(struct cli *cli, const char *s)
{
- const char *q;
- int quote = 0;
- for (q = s; *q != '\0'; q++) {
- if (!isgraph(*q) || *q == '"') {
- quote++;
- break;
- }
- }
- if (!quote) {
- (void)vsb_cat(cli->sb, s);
- return;
- }
- (void)vsb_putc(cli->sb, '"');
- for (q = s; *q != '\0'; q++) {
- switch (*q) {
- case ' ':
- (void)vsb_putc(cli->sb, *q);
- break;
- case '\\':
- case '"':
- (void)vsb_putc(cli->sb, '\\');
- (void)vsb_putc(cli->sb, *q);
- break;
- case '\n':
- (void)vsb_cat(cli->sb, "\\n");
- break;
- case '\r':
- (void)vsb_cat(cli->sb, "\\r");
- break;
- case '\t':
- (void)vsb_cat(cli->sb, "\\t");
- break;
- default:
- if (isgraph(*q))
- (void)vsb_putc(cli->sb, *q);
- else
- (void)vsb_printf(cli->sb, "\\%o", *q);
- break;
- }
- }
- (void)vsb_putc(cli->sb, '"');
+ vsb_quote(cli->sb, s, 0);
}
void
return(VSB_ISFINISHED(s));
}
+
+/*
+ * Quote a string
+ */
+void
+vsb_quote(struct vsb *s, const char *p, int how)
+{
+ const char *q;
+ int quote = 0;
+
+ (void)how; /* For future enhancements */
+
+ for (q = p; *q != '\0'; q++) {
+ if (!isgraph(*q) || *q == '"') {
+ quote++;
+ break;
+ }
+ }
+ if (!quote) {
+ (void)vsb_cat(s, p);
+ return;
+ }
+ (void)vsb_putc(s, '"');
+ for (q = p; *q != '\0'; q++) {
+ switch (*q) {
+ case ' ':
+ (void)vsb_putc(s, *q);
+ break;
+ case '\\':
+ case '"':
+ (void)vsb_putc(s, '\\');
+ (void)vsb_putc(s, *q);
+ break;
+ case '\n':
+ (void)vsb_cat(s, "\\n");
+ break;
+ case '\r':
+ (void)vsb_cat(s, "\\r");
+ break;
+ case '\t':
+ (void)vsb_cat(s, "\\t");
+ break;
+ default:
+ if (isgraph(*q))
+ (void)vsb_putc(s, *q);
+ else
+ (void)vsb_printf(s, "\\%o", *q);
+ break;
+ }
+ }
+ (void)vsb_putc(s, '"');
+}