summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--args.c51
-rw-r--r--ccache.c4
-rw-r--r--test/test_args.c27
3 files changed, 48 insertions, 34 deletions
diff --git a/args.c b/args.c
index 068338c7..1eb9c263 100644
--- a/args.c
+++ b/args.c
@@ -75,44 +75,54 @@ args_init_from_gcc_atfile(const char *filename)
switch (*pos) {
case '\\':
pos++;
- if (*pos == '\0')
+ if (*pos == '\0') {
continue;
+ }
break;
- case '\"': case '\'':
+ case '\"':
+ case '\'':
if (quoting != '\0') {
if (quoting == *pos) {
quoting = '\0';
pos++;
continue;
- } else
+ } else {
break;
+ }
} else {
quoting = *pos;
pos++;
continue;
}
- case '\n': case '\t': case ' ':
- if (quoting)
+
+ case '\n':
+ case '\t':
+ case ' ':
+ if (quoting) {
break;
+ }
/* Fall through */
+
case '\0':
/* end of token */
*argpos = '\0';
if (argbuf[0] != '\0')
args_add(args, argbuf);
argpos = argbuf;
- if (*pos == '\0')
+ if (*pos == '\0') {
goto out;
- else {
+ } else {
pos++;
continue;
}
}
+
*argpos = *pos;
pos++;
argpos++;
}
+
out:
free(argbuf);
free(argtext);
@@ -136,21 +146,23 @@ void
args_insert(struct args *dest, int index, struct args *src, bool replace)
{
int offset;
- int j;
+ int i;
/* Adjustments made if we are replacing or shifting the element
* currently at dest->argv[index] */
offset = replace ? 1 : 0;
- if (replace)
+ if (replace) {
free(dest->argv[index]);
+ }
if (src->argc == 0) {
if (replace) {
/* Have to shift everything down by 1 since
* we replaced with an empty list */
- for (j = index; j < dest->argc; j++)
- dest->argv[j] = dest->argv[j + 1];
+ for (i = index; i < dest->argc; i++) {
+ dest->argv[i] = dest->argv[i + 1];
+ }
dest->argc--;
}
args_free(src);
@@ -165,17 +177,20 @@ args_insert(struct args *dest, int index, struct args *src, bool replace)
return;
}
- dest->argv = (char**)x_realloc(dest->argv,
- (src->argc + dest->argc + 1 - offset) *
- sizeof(char *));
+ dest->argv = (char**)x_realloc(
+ dest->argv,
+ (src->argc + dest->argc + 1 - offset) *
+ sizeof(char *));
/* Shift arguments over */
- for (j = dest->argc; j >= index + offset; j--)
- dest->argv[j + src->argc - offset] = dest->argv[j];
+ for (i = dest->argc; i >= index + offset; i--) {
+ dest->argv[i + src->argc - offset] = dest->argv[i];
+ }
/* Copy the new arguments into place */
- for (j = 0; j < src->argc; j++)
- dest->argv[j + index] = src->argv[j];
+ for (i = 0; i < src->argc; i++) {
+ dest->argv[i + index] = src->argv[i];
+ }
dest->argc += src->argc - offset;
src->argc = 0;
diff --git a/ccache.c b/ccache.c
index 22c56e17..5ead8107 100644
--- a/ccache.c
+++ b/ccache.c
@@ -1455,20 +1455,20 @@ cc_process_args(struct args *args, struct args **preprocessor_args,
goto out;
}
+ /* Handle "@file" argument. */
if (str_startswith(argv[i], "@")) {
char *argpath = argv[i] + 1;
struct args *file_args;
file_args = args_init_from_gcc_atfile(argpath);
if (!file_args) {
- cc_log("Coudln't read arg file %s", argpath);
+ cc_log("Couldn't read arg file %s", argpath);
stats_update(STATS_ARGS);
result = false;
goto out;
}
args_insert(args, i, file_args, true);
-
argc = args->argc;
argv = args->argv;
i--;
diff --git a/test/test_args.c b/test/test_args.c
index 8da83bd0..5c1b19d5 100644
--- a/test/test_args.c
+++ b/test/test_args.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010, 2012 Joel Rosdahl
*
* This program 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
@@ -22,6 +22,7 @@
#include "ccache.h"
#include "test/framework.h"
+#include "test/util.h"
TEST_SUITE(args)
@@ -61,14 +62,10 @@ TEST(args_init_from_string)
TEST(args_init_from_gcc_atfile)
{
- int fd;
struct args *args;
const char *argtext = "first sec\\\tond\tthi\\\\rd\nfourth \tfif\\ th \"si'x\\\" th\" 'seve\nth'\\";
- fd = open("gcc_atfile", O_CREAT | O_WRONLY, 0600);
- CHECK(fd >= 0);
- CHECK(write(fd, argtext, strlen(argtext)) == (ssize_t)strlen(argtext));
- close(fd);
+ create_file("gcc_atfile", argtext);
args = args_init_from_gcc_atfile("gcc_atfile");
CHECK(args);
@@ -182,28 +179,30 @@ TEST(args_insert)
args_insert(args, 2, src1, true);
CHECK_STR_EQ_FREE2("first second alpha beta gamma fourth fifth",
- args_to_string(args));
+ args_to_string(args));
CHECK_INT_EQ(7, args->argc);
args_insert(args, 2, src2, true);
CHECK_STR_EQ_FREE2("first second one beta gamma fourth fifth",
- args_to_string(args));
+ args_to_string(args));
CHECK_INT_EQ(7, args->argc);
args_insert(args, 2, src3, true);
CHECK_STR_EQ_FREE2("first second beta gamma fourth fifth",
- args_to_string(args));
+ args_to_string(args));
CHECK_INT_EQ(6, args->argc);
args_insert(args, 1, src4, false);
CHECK_STR_EQ_FREE2("first alpha beta gamma second beta gamma fourth fifth",
- args_to_string(args));
+ args_to_string(args));
CHECK_INT_EQ(9, args->argc);
args_insert(args, 1, src5, false);
- CHECK_STR_EQ_FREE2("first one alpha beta gamma second beta gamma fourth fifth",
- args_to_string(args));
+ CHECK_STR_EQ_FREE2(
+ "first one alpha beta gamma second beta gamma fourth fifth",
+ args_to_string(args));
CHECK_INT_EQ(10, args->argc);
args_insert(args, 1, src6, false);
- CHECK_STR_EQ_FREE2("first one alpha beta gamma second beta gamma fourth fifth",
- args_to_string(args));
+ CHECK_STR_EQ_FREE2(
+ "first one alpha beta gamma second beta gamma fourth fifth",
+ args_to_string(args));
CHECK_INT_EQ(10, args->argc);
args_free(args);