parse stdin as newline-separated
Newline-separated input is more reliable because most of the arguments we accept over stdin can validly contain spaces but not newlines. Resolves FS#52992 Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
parent
a138db3c07
commit
d9908fc1f2
1 changed files with 23 additions and 32 deletions
|
@ -1100,7 +1100,6 @@ static void cl_to_log(int argc, char *argv[])
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
size_t i;
|
|
||||||
uid_t myuid = getuid();
|
uid_t myuid = getuid();
|
||||||
|
|
||||||
install_segv_handler();
|
install_segv_handler();
|
||||||
|
@ -1148,44 +1147,36 @@ int main(int argc, char *argv[])
|
||||||
if(alpm_list_find_str(pm_targets, "-")) {
|
if(alpm_list_find_str(pm_targets, "-")) {
|
||||||
if(!isatty(fileno(stdin))) {
|
if(!isatty(fileno(stdin))) {
|
||||||
int target_found = 0;
|
int target_found = 0;
|
||||||
size_t current_size = PATH_MAX;
|
char *vdata, *line = NULL;
|
||||||
char *vdata, *line = malloc(current_size);
|
size_t line_size = 0;
|
||||||
int c;
|
ssize_t nread;
|
||||||
|
|
||||||
/* remove the '-' from the list */
|
/* remove the '-' from the list */
|
||||||
pm_targets = alpm_list_remove_str(pm_targets, "-", &vdata);
|
pm_targets = alpm_list_remove_str(pm_targets, "-", &vdata);
|
||||||
free(vdata);
|
free(vdata);
|
||||||
|
|
||||||
i = 0;
|
while((nread = getline(&line, &line_size, stdin)) != -1) {
|
||||||
do {
|
if(line[nread - 1] == '\n') {
|
||||||
c = fgetc(stdin);
|
/* remove trailing newline */
|
||||||
if(c == EOF || isspace(c)) {
|
line[nread - 1] = '\0';
|
||||||
/* avoid adding zero length arg when multiple spaces separate args */
|
}
|
||||||
if(i > 0) {
|
if(line[0] == '\0') {
|
||||||
line[i] = '\0';
|
/* skip empty lines */
|
||||||
pm_targets = alpm_list_add(pm_targets, strdup(line));
|
continue;
|
||||||
target_found = 1;
|
}
|
||||||
i = 0;
|
if(!alpm_list_append_strdup(&pm_targets, line)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
target_found = 1;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
line[i++] = (char)c;
|
|
||||||
/* we may be at the end of our allocated buffer now */
|
|
||||||
if(i >= current_size) {
|
|
||||||
char *new = realloc(line, current_size * 2);
|
|
||||||
if(new) {
|
|
||||||
line = new;
|
|
||||||
current_size *= 2;
|
|
||||||
} else {
|
|
||||||
free(line);
|
free(line);
|
||||||
|
|
||||||
|
if(ferror(stdin)) {
|
||||||
pm_printf(ALPM_LOG_ERROR,
|
pm_printf(ALPM_LOG_ERROR,
|
||||||
_("memory exhausted in argument parsing\n"));
|
_("failed to read arguments from stdin: (%s)\n"), strerror(errno));
|
||||||
cleanup(EXIT_FAILURE);
|
cleanup(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
} while(c != EOF);
|
|
||||||
|
|
||||||
free(line);
|
|
||||||
if(!freopen(ctermid(NULL), "r", stdin)) {
|
if(!freopen(ctermid(NULL), "r", stdin)) {
|
||||||
pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
|
pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
|
Loading…
Add table
Reference in a new issue