introduced new type SHELL_ESCAPED_STR for more type safety

This commit is contained in:
Enrico Weigelt, metux IT service 2009-01-27 22:27:06 +01:00
parent 6674647676
commit 90763ba82f
4 changed files with 61 additions and 50 deletions

View File

@ -1,3 +1,8 @@
2009-0-27 Enrico Weigelt, metux ITS <weigelt@metux.de>
* mhl/escape.h, src/complete.c, vfs/fish.c: introduced new type
SHELL_ESCAPED_STR for more type safety
2009-01-25 Ilia Maslakov <il.smind@gmail.com>
* src/boxes.c, src/boxes.h, src/dir.c, src/dir.h:

View File

@ -18,6 +18,9 @@
#define mhl_shell_escape_nottoesc(x) \
(((x)!=0) && (!mhl_shell_escape_toesc((x))))
/* type for escaped string - just for a bit more type safety ;-p */
typedef struct { char* s; } SHELL_ESCAPED_STR;
/** To be compatible with the general posix command lines we have to escape
strings for the command line
@ -26,10 +29,10 @@
/returns
return escaped string (later need to free)
*/
static inline char* mhl_shell_escape_dup(const char* src)
static inline SHELL_ESCAPED_STR mhl_shell_escape_dup(const char* src)
{
if ((src==NULL)||(!(*src)))
return strdup("");
return (SHELL_ESCAPED_STR){ .s = strdup("") };
char* buffer = calloc(1, strlen(src)*2+2);
char* ptr = buffer;
@ -48,7 +51,7 @@ static inline char* mhl_shell_escape_dup(const char* src)
/* at this point we either have an \0 or an char to escape */
if (!c)
return buffer;
return (SHELL_ESCAPED_STR){ .s = buffer };
*ptr = '\\';
ptr++;

View File

@ -954,17 +954,18 @@ complete_engine (WInput *in, int what_to_do)
start++;
in->completions = try_complete (in->buffer, &start, &end, in->completion_flags);
}
if (in->completions){
if (what_to_do & DO_INSERTION || ((what_to_do & DO_QUERY) && !in->completions[1])) {
complete = mhl_shell_escape_dup(in->completions [0]);
if (insert_text (in, complete, strlen (complete))){
SHELL_ESCAPED_STR complete = mhl_shell_escape_dup(in->completions [0]);
if (insert_text (in, complete.s, strlen (complete.s))){
if (in->completions [1])
beep ();
else
free_completions (in);
} else
beep ();
mhl_mem_free(complete);
mhl_mem_free(complete.s);
}
if ((what_to_do & DO_QUERY) && in->completions && in->completions [1]) {
int maxlen = 0, i, count = 0;
@ -976,7 +977,8 @@ complete_engine (WInput *in, int what_to_do)
for (p=in->completions + 1; *p; count++, p++) {
q = *p;
*p = mhl_shell_escape_dup(*p);
SHELL_ESCAPED_STR esc = mhl_shell_escape_dup(*p);
*p = esc.s;
mhl_mem_free(q);
if ((i = strlen (*p)) > maxlen)
maxlen = i;

View File

@ -144,7 +144,7 @@ fish_command (struct vfs_class *me, struct vfs_s_super *super,
enable_interrupt_key ();
status = write (SUP.sockw, str, strlen (str));
g_free (str);
mhl_mem_free (str);
disable_interrupt_key ();
if (status < 0)
@ -168,10 +168,10 @@ fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
close (SUP.sockr);
SUP.sockw = SUP.sockr = -1;
}
g_free (SUP.host);
g_free (SUP.user);
g_free (SUP.cwdir);
g_free (SUP.password);
mhl_mem_free (SUP.host);
mhl_mem_free (SUP.user);
mhl_mem_free (SUP.cwdir);
mhl_mem_free (SUP.password);
}
static void
@ -251,7 +251,7 @@ fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
p = g_strconcat (_(" fish: Password required for "),
SUP.user, " ", (char *) NULL);
op = vfs_get_password (p);
g_free (p);
mhl_mem_free (p);
if (op == NULL)
ERRNOR (EPERM, -1);
SUP.password = op;
@ -314,7 +314,7 @@ fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
&password, 0, URL_NOSLASH);
g_free (p);
mhl_mem_free (p);
SUP.host = host;
SUP.user = user;
@ -341,12 +341,12 @@ fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
URL_NOSLASH);
g_free (op);
mhl_mem_free (op);
flags = ((strcmp (host, SUP.host) == 0)
&& (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
g_free (host);
g_free (user);
mhl_mem_free (host);
mhl_mem_free (user);
return flags;
}
@ -358,7 +358,7 @@ fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
char buffer[8192];
struct vfs_s_entry *ent = NULL;
FILE *logfile;
char *quoted_path;
SHELL_ESCAPED_STR quoted_path;
int reply_code;
#if 0
@ -453,8 +453,8 @@ fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
"else\n"
"echo '### 500'\n"
"fi\n",
quoted_path, quoted_path, quoted_path, quoted_path, quoted_path, quoted_path);
mhl_mem_free (quoted_path);
quoted_path.s, quoted_path.s, quoted_path.s, quoted_path.s, quoted_path.s, quoted_path.s);
mhl_mem_free (quoted_path.s);
ent = vfs_s_generate_entry(me, NULL, dir, 0);
while (1) {
int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
@ -585,7 +585,7 @@ fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
vfs_s_free_entry (me, ent);
reply_code = fish_decode_reply(buffer + 4, 0);
if (reply_code == COMPLETE) {
g_free (SUP.cwdir);
mhl_mem_free (SUP.cwdir);
SUP.cwdir = g_strdup (remote_path);
print_vfs_message (_("%s: done."), me->name);
return 0;
@ -609,7 +609,7 @@ fish_file_store(struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *loc
struct stat s;
int was_error = 0;
int h;
char *quoted_name;
SHELL_ESCAPED_STR quoted_name;
h = open (localname, O_RDONLY);
@ -650,7 +650,7 @@ fish_file_store(struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *loc
*/
quoted_name = mhl_shell_escape_dup(name);
print_vfs_message(_("fish: store %s: sending command..."), quoted_name );
print_vfs_message(_("fish: store %s: sending command..."), quoted_name.s );
/* FIXME: File size is limited to ULONG_MAX */
if (!fh->u.fish.append)
@ -674,8 +674,8 @@ fish_file_store(struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *loc
" rest=`expr $rest - $n`\n"
"done\n"
"}; echo '### 200'\n",
(unsigned long) s.st_size, quoted_name,
quoted_name, (unsigned long) s.st_size,
(unsigned long) s.st_size, quoted_name.s,
quoted_name.s, (unsigned long) s.st_size,
(unsigned long) s.st_size);
else
n = fish_command (me, super, WAIT_REPLY,
@ -691,8 +691,8 @@ fish_file_store(struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *loc
" rest=`expr $rest - $n`\n"
"done\n"
"}; echo '### 200'\n",
(unsigned long) s.st_size, quoted_name,
quoted_name, (unsigned long) s.st_size);
(unsigned long) s.st_size, quoted_name.s,
quoted_name.s, (unsigned long) s.st_size);
if (n != PRELIM) {
close (h);
@ -726,14 +726,14 @@ fish_file_store(struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *loc
(unsigned long) s.st_size);
}
close(h);
mhl_mem_free(quoted_name);
mhl_mem_free(quoted_name.s);
if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
ERRNOR (E_REMOTE, -1);
return 0;
error_return:
close(h);
fish_get_reply(me, SUP.sockr, NULL, 0);
mhl_mem_free(quoted_name);
mhl_mem_free(quoted_name.s);
return -1;
}
@ -741,7 +741,7 @@ static int
fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
{
char *name;
char *quoted_name;
SHELL_ESCAPED_STR quoted_name;
if (offset)
ERRNOR (E_NOTSUPP, 0);
name = vfs_s_fullpath (me, fh->ino);
@ -770,8 +770,8 @@ fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
"else\n"
"echo '### 500'\n"
"fi\n",
quoted_name, quoted_name, quoted_name, quoted_name );
g_free (quoted_name);
quoted_name.s, quoted_name.s, quoted_name.s, quoted_name.s );
mhl_mem_free (quoted_name.s);
if (offset != PRELIM) ERRNOR (E_REMOTE, 0);
fh->linear = LS_LINEAR_OPEN;
fh->u.fish.got = 0;
@ -880,17 +880,18 @@ fish_send_command(struct vfs_class *me, struct vfs_s_super *super, const char *c
#define PREFIX \
char buf[BUF_LARGE]; \
const char *crpath; \
char *rpath, *mpath = g_strdup (path); \
char *mpath = mhl_str_dup (path); \
SHELL_ESCAPED_STR rpath; \
struct vfs_s_super *super; \
if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) { \
g_free (mpath); \
mhl_mem_free (mpath); \
return -1; \
} \
rpath = mhl_shell_escape_dup(crpath); \
g_free (mpath);
mhl_mem_free (mpath);
#define POSTFIX(flags) \
g_free (rpath); \
mhl_mem_free (rpath.s); \
return fish_send_command(me, super, buf, flags);
static int
@ -910,24 +911,24 @@ static int fish_##name (struct vfs_class *me, const char *path1, const char *pat
{ \
char buf[BUF_LARGE]; \
const char *crpath1, *crpath2; \
char *rpath1, *rpath2, *mpath1, *mpath2; \
char *mpath1, *mpath2; \
struct vfs_s_super *super1, *super2; \
if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) { \
g_free (mpath1); \
mhl_mem_free (mpath1); \
return -1; \
} \
if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) { \
g_free (mpath1); \
g_free (mpath2); \
mhl_mem_free (mpath1); \
mhl_mem_free (mpath2); \
return -1; \
} \
rpath1 = mhl_shell_escape_dup (crpath1); \
g_free (mpath1); \
rpath2 = mhl_shell_escape_dup (crpath2); \
g_free (mpath2); \
g_snprintf(buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
mhl_mem_free (rpath1); \
mhl_mem_free (rpath2); \
SHELL_ESCAPED_STR rpath1 = mhl_shell_escape_dup (crpath1); \
mhl_mem_free (mpath1); \
SHELL_ESCAPED_STR rpath2 = mhl_shell_escape_dup (crpath2); \
mhl_mem_free (mpath2); \
g_snprintf(buf, sizeof(buf), string "\n", rpath1.s, rpath2.s, rpath1.s, rpath2.s); \
mhl_mem_free (rpath1.s); \
mhl_mem_free (rpath2.s); \
return fish_send_command(me, super2, buf, OPT_FLUSH); \
}
@ -940,7 +941,7 @@ FISH_OP(link, "#LINK /%s /%s\n"
static int fish_symlink (struct vfs_class *me, const char *setto, const char *path)
{
char *qsetto;
SHELL_ESCAPED_STR qsetto;
PREFIX
qsetto = mhl_shell_escape_dup (setto);
g_snprintf(buf, sizeof(buf),
@ -948,7 +949,7 @@ static int fish_symlink (struct vfs_class *me, const char *setto, const char *pa
"ln -s %s /%s 2>/dev/null\n"
"echo '### 000'\n",
qsetto, rpath, qsetto, rpath);
mhl_mem_free (qsetto);
mhl_mem_free (qsetto.s);
POSTFIX(OPT_FLUSH);
}
@ -1078,7 +1079,7 @@ fish_fill_names (struct vfs_class *me, fill_names_f func)
name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags,
"/", SUP.cwdir, (char *) NULL);
(*func)(name);
g_free (name);
mhl_mem_free (name);
super = super->next;
}
}