1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*
Copyright (C) 2004, 2005 Stephen Bach
This file is part of the Viewglob package.
Viewglob is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Viewglob is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Viewglob; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "common.h"
#include "sanitize.h"
enum quote_type {
QT_DUMMY,
QT_SINGLE,
QT_DOUBLE,
QT_EXTGLOB_PAREN,
};
struct quote_list {
enum quote_type qt;
struct quote_list* next;
};
struct sane_cmd {
gboolean last_char_backslash;
gboolean last_char_exclamation;
gboolean last_char_dollar;
gboolean skip_word;
struct quote_list* ql;
GString* command;
};
static void add_char(struct sane_cmd* s, gchar c);
static void delete_current_word(struct sane_cmd* s);
static void backspace(struct sane_cmd* s);
static gboolean last_char(struct sane_cmd* s, gchar c);
static gboolean in_quote(struct sane_cmd* s, enum quote_type qt);
static enum quote_type ql_pop(struct sane_cmd* s);
static void ql_push(struct sane_cmd* s, enum quote_type);
gchar* sanitize(GString* string) {
struct sane_cmd s;
gint i;
gchar c;
s.last_char_backslash = FALSE;
s.last_char_exclamation = FALSE;
s.last_char_dollar = FALSE;
s.skip_word = FALSE;
s.command = g_string_sized_new(string->len);
s.ql = NULL;
for (i = 0; i < string->len; i++) {
c = *(string->str + i);
if (s.last_char_exclamation) {
/* Don't allow history expansion. */
if ( (c != '(') && (c != ' ') && (c != '\t') && (c != '\n') ) {
s.skip_word = TRUE;
s.last_char_exclamation = FALSE;
/* Remove the ! */
backspace(&s);
continue;
}
}
else if (s.last_char_dollar) {
/* Don't allow $ constructs (variables, command substitution,
etc. */
if ( (c != ' ') && (c != '\t') && (c != '\n') ) {
backspace(&s);
s.last_char_dollar = FALSE;
i = string->len; /* Break out of loop. */
continue;
}
else {
/* A lone $ is acceptable. */
s.last_char_dollar = FALSE;
}
}
if (s.skip_word) {
if ( (c == ' ') || (c == '\t') )
s.skip_word = FALSE;
else
continue;
}
switch (c) {
case ('\''):
if (in_quote(&s, QT_SINGLE)) {
ql_pop(&s);
add_char(&s, c);
}
else if (in_quote(&s, QT_DOUBLE))
add_char(&s, c);
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else {
ql_push(&s, QT_SINGLE);
add_char(&s, c);
}
break;
case ('\"'):
if (in_quote(&s, QT_SINGLE))
add_char(&s, c);
else if (in_quote(&s, QT_DOUBLE)) {
ql_pop(&s);
add_char(&s, c);
}
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else {
ql_push(&s, QT_DOUBLE);
add_char(&s, c);
}
break;
case ('\\'):
if (in_quote(&s, QT_SINGLE) || in_quote(&s, QT_DOUBLE))
add_char(&s, c);
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else {
s.last_char_backslash = TRUE;
add_char(&s, c);
}
break;
case ('$'):
if (in_quote(&s, QT_SINGLE))
add_char(&s, c);
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else {
s.last_char_dollar = TRUE;
add_char(&s, c);
}
break;
case ('!'): /* Gotta be careful about history expansion. */
if (in_quote(&s, QT_SINGLE))
add_char(&s, c);
else if (in_quote(&s, QT_EXTGLOB_PAREN))
/* No ! allowed in ?( ) constructs. */
break;
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else {
s.last_char_exclamation = TRUE;
add_char(&s, c);
}
break;
case (' '):
case ('\t'):
if (s.last_char_exclamation)
s.last_char_exclamation = FALSE;
else if (s.last_char_backslash)
s.last_char_backslash = FALSE;
else if (s.skip_word) {
/* Only ' ' and \t can turn off skip_word. */
s.skip_word = FALSE;
}
add_char(&s, c);
break;
/* Only allow ( in the *(blah), ?(blah), etc. forms, or when
quoted. */
case ('('):
if (in_quote(&s, QT_SINGLE))
add_char(&s, c);
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else if (s.last_char_exclamation) {
if (in_quote(&s, QT_DOUBLE)) {
/* It sucks that ! is such a multiuse character.
There's no good way to deal here, so just give
up. */
backspace(&s);
s.last_char_exclamation = FALSE;
i = string->len;
break;
}
s.last_char_exclamation = FALSE;
ql_push(&s, QT_EXTGLOB_PAREN);
add_char(&s, c);
}
else if (in_quote(&s, QT_DOUBLE))
add_char(&s, c);
else if (last_char(&s, '?') || last_char(&s, '*') ||
last_char(&s, '+') || last_char(&s, '@')) {
ql_push(&s, QT_EXTGLOB_PAREN);
add_char(&s, c);
}
else
i = string->len; /* Break out of loop. */
break;
case (')'):
if (in_quote(&s, QT_SINGLE) || in_quote(&s, QT_DOUBLE))
add_char(&s, c);
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else if (in_quote(&s, QT_EXTGLOB_PAREN)) {
ql_pop(&s);
add_char(&s, c);
}
/* Skip ) otherwise. */
break;
case ('`'): /* Backtick */
if (in_quote(&s, QT_SINGLE))
add_char(&s, c);
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else
i = string->len; /* Break out of loop. */
break;
case (';'): /* These are command finishers. */
case ('&'): /* Must be careful not to include one if not */
case ('|'): /* escaped, and to stop processing if so. */
if (in_quote(&s, QT_SINGLE) || in_quote(&s, QT_DOUBLE) ||
in_quote(&s, QT_EXTGLOB_PAREN))
add_char(&s, c);
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else
i = string->len; /* Break out of loop. */
break;
case ('\015'): /* Carriage return */
case ('\n'): /* Should never see this, but just in case. */
break; /* Skip 'em. */
case ('>'):
case ('<'):
if (in_quote(&s, QT_SINGLE) || in_quote(&s, QT_DOUBLE) ||
in_quote(&s, QT_EXTGLOB_PAREN))
add_char(&s, c);
else if (s.last_char_backslash) {
s.last_char_backslash = FALSE;
add_char(&s, c);
}
else { /* Break out of loop. */
/* Note: this isn't entirely correct, as bash only
interprets a few characters as being part of the
redirection construct. */
delete_current_word(&s);
i = string->len;
}
break;
default:
if (s.last_char_backslash)
s.last_char_backslash = FALSE;
add_char(&s, c);
break;
}
}
if (s.last_char_backslash) {
/* Can't have a trailing backslash. */
backspace(&s);
s.last_char_backslash = FALSE;
}
/* Close unclosed quotes. */
enum quote_type qt;
while ( (qt = ql_pop(&s)) != QT_DUMMY ) {
if (s.last_char_exclamation) {
/* This exclamation could be interpreted as special because of
the following quote characters. */
backspace(&s);
s.last_char_exclamation = FALSE;
}
switch (qt) {
case QT_SINGLE:
c = '\'';
break;
case QT_DOUBLE:
c = '\"';
break;
case QT_EXTGLOB_PAREN:
c = ')';
break;
default:
/* Should never get here, but just in case. */
c = ' ';
break;
}
add_char(&s, c);
}
gchar* retval = s.command->str;
g_string_free(s.command, FALSE);
return retval;
}
static gboolean last_char(struct sane_cmd* s, gchar c) {
if ( (s->command->len > 0) &&
(*(s->command->str + s->command->len - 1) == c) )
return TRUE;
else
return FALSE;
}
static void delete_current_word(struct sane_cmd* s) {
gchar c = *(s->command->str + s->command->len);
while (c != ' ' && c != '\t' && c != '\n' && s->command->len > 0) {
backspace(s);
c = *(s->command->str + s->command->len);
}
}
static void add_char(struct sane_cmd* s, gchar c) {
s->command = g_string_append_c(s->command, c);
}
static enum quote_type ql_pop(struct sane_cmd* s) {
enum quote_type popped;
if (s->ql) {
struct quote_list* tmp = s->ql->next;
popped = s->ql->qt;
g_free(s->ql);
s->ql = tmp;
}
else
popped = QT_DUMMY;
return popped;
}
static void ql_push(struct sane_cmd* s, enum quote_type new_qt) {
struct quote_list* new_ql;
new_ql = g_new(struct quote_list, 1);
new_ql->qt = new_qt;
new_ql->next = s->ql;
s->ql = new_ql;
}
static gboolean in_quote(struct sane_cmd* s, enum quote_type qt) {
if (s->ql) {
if (s->ql->qt == qt)
return TRUE;
else
return FALSE;
}
else
return FALSE;
}
static void backspace(struct sane_cmd* s) {
g_return_if_fail(s != NULL);
if (s->command->len > 0)
s->command = g_string_truncate(s->command, s->command->len - 1);
}
|