Use strtol instead of atoi in output command

This commit is contained in:
emersion 2017-12-14 02:23:33 +01:00
parent 9254c5a93f
commit cba592b3d2
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 76 additions and 44 deletions

View File

@ -34,8 +34,6 @@ struct cmd_results *cmd_output(int argc, char **argv) {
} }
output->name = strdup(name); output->name = strdup(name);
// TODO: atoi doesn't handle invalid numbers
int i; int i;
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
const char *command = argv[i]; const char *command = argv[i];
@ -80,9 +78,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
} }
} }
} else { } else {
// Format is 1234 4321 (legacy) // Format is 1234 4321
++i; if (++i >= argc) {
if (i >= argc) {
error = cmd_results_new(CMD_INVALID, "output", error = cmd_results_new(CMD_INVALID, "output",
"Missing mode argument (height)."); "Missing mode argument (height).");
goto fail; goto fail;
@ -97,41 +94,66 @@ struct cmd_results *cmd_output(int argc, char **argv) {
output->width = width; output->width = width;
output->height = height; output->height = height;
output->refresh_rate = refresh_rate; output->refresh_rate = refresh_rate;
} else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { } else if (strcasecmp(command, "position") == 0 ||
strcasecmp(command, "pos") == 0) {
if (++i >= argc) { if (++i >= argc) {
error = cmd_results_new(CMD_INVALID, "output", "Missing position argument."); error = cmd_results_new(CMD_INVALID, "output",
"Missing position argument.");
goto fail; goto fail;
} }
char *pos = argv[i];
char *c = strchr(pos, ',');
int x = -1, y = -1; int x = -1, y = -1;
if (c != NULL) {
char *end;
x = strtol(argv[i], &end, 10);
if (*end) {
// Format is 1234,4321 // Format is 1234,4321
*c = '\0'; if (*end != ',') {
x = atoi(pos); error = cmd_results_new(CMD_INVALID, "output",
y = atoi(c + 1); "Invalid position x.");
*c = ','; goto fail;
} else { }
// Format is 1234 4321 ++end;
x = atoi(pos); y = strtol(end, &end, 10);
if (++i >= argc) { if (*end) {
error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); error = cmd_results_new(CMD_INVALID, "output",
"Invalid position y.");
goto fail;
}
} else {
// Format is 1234 4321 (legacy)
if (++i >= argc) {
error = cmd_results_new(CMD_INVALID, "output",
"Missing position argument (y).");
goto fail;
}
y = strtol(argv[i], &end, 10);
if (*end) {
error = cmd_results_new(CMD_INVALID, "output",
"Invalid position y.");
goto fail; goto fail;
} }
pos = argv[i];
y = atoi(pos);
} }
output->x = x; output->x = x;
output->y = y; output->y = y;
} else if (strcasecmp(command, "scale") == 0) { } else if (strcasecmp(command, "scale") == 0) {
if (++i >= argc) { if (++i >= argc) {
error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter."); error = cmd_results_new(CMD_INVALID, "output",
"Missing scale parameter.");
goto fail;
}
char *end;
output->scale = strtol(argv[i], &end, 10);
if (*end) {
error = cmd_results_new(CMD_INVALID, "output",
"Invalid scale.");
goto fail; goto fail;
} }
output->scale = atoi(argv[i]);
} else if (strcasecmp(command, "transform") == 0) { } else if (strcasecmp(command, "transform") == 0) {
if (++i >= argc) { if (++i >= argc) {
error = cmd_results_new(CMD_INVALID, "output", "Missing transform parameter."); error = cmd_results_new(CMD_INVALID, "output",
"Missing transform parameter.");
goto fail; goto fail;
} }
char *value = argv[i]; char *value = argv[i];
@ -152,17 +174,21 @@ struct cmd_results *cmd_output(int argc, char **argv) {
} else if (strcmp(value, "flipped-270") == 0) { } else if (strcmp(value, "flipped-270") == 0) {
output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
} else { } else {
error = cmd_results_new(CMD_INVALID, "output", "Invalid output transform."); error = cmd_results_new(CMD_INVALID, "output",
"Invalid output transform.");
goto fail; goto fail;
} }
} else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { } else if (strcasecmp(command, "background") == 0 ||
strcasecmp(command, "bg") == 0) {
wordexp_t p; wordexp_t p;
if (++i >= argc) { if (++i >= argc) {
error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification."); error = cmd_results_new(CMD_INVALID, "output",
"Missing background file or color specification.");
goto fail; goto fail;
} }
if (i + 1 >= argc) { if (i + 1 >= argc) {
error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`."); error = cmd_results_new(CMD_INVALID, "output",
"Missing background scaling mode or `solid_color`.");
goto fail; goto fail;
} }
if (strcasecmp(argv[i + 1], "solid_color") == 0) { if (strcasecmp(argv[i + 1], "solid_color") == 0) {
@ -175,7 +201,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
size_t j; size_t j;
for (j = 0; j < (size_t) (argc - i); ++j) { for (j = 0; j < (size_t) (argc - i); ++j) {
mode = argv[i + j]; mode = argv[i + j];
for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) { size_t n = sizeof(bg_options) / sizeof(char *);
for (size_t k = 0; k < n; ++k) {
if (strcasecmp(mode, bg_options[k]) == 0) { if (strcasecmp(mode, bg_options[k]) == 0) {
valid = true; valid = true;
break; break;
@ -186,13 +213,15 @@ struct cmd_results *cmd_output(int argc, char **argv) {
} }
} }
if (!valid) { if (!valid) {
error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode."); error = cmd_results_new(CMD_INVALID, "output",
"Missing background scaling mode.");
goto fail; goto fail;
} }
char *src = join_args(argv + i, j); char *src = join_args(argv + i, j);
if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s).", src); error = cmd_results_new(CMD_INVALID, "output",
"Invalid syntax (%s).", src);
goto fail; goto fail;
} }
free(src); free(src);
@ -205,15 +234,18 @@ struct cmd_results *cmd_output(int argc, char **argv) {
if (src) { if (src) {
sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
} else { } else {
sway_log(L_ERROR, "Unable to allocate background source"); sway_log(L_ERROR,
"Unable to allocate background source");
} }
free(conf); free(conf);
} else { } else {
sway_log(L_ERROR, "Unable to allocate background source"); sway_log(L_ERROR,
"Unable to allocate background source");
} }
} }
if (!src || access(src, F_OK) == -1) { if (!src || access(src, F_OK) == -1) {
error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s).", src); error = cmd_results_new(CMD_INVALID, "output",
"Background file unreadable (%s).", src);
wordfree(&p); wordfree(&p);
goto fail; goto fail;
} }
@ -228,7 +260,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
i += j; i += j;
} }
} else { } else {
error = cmd_results_new(CMD_INVALID, "output", "Invalid output subcommand: %s.", command); error = cmd_results_new(CMD_INVALID, "output",
"Invalid output subcommand: %s.", command);
goto fail; goto fail;
} }
} }
@ -245,11 +278,10 @@ struct cmd_results *cmd_output(int argc, char **argv) {
} }
sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz " sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz "
"position %d,%d scale %d transform %d) (bg %s %s)", "position %d,%d scale %d transform %d) (bg %s %s)",
output->name, output->enabled, output->width, output->name, output->enabled, output->width, output->height,
output->height, output->refresh_rate, output->x, output->y, output->refresh_rate, output->x, output->y, output->scale,
output->scale, output->transform, output->transform, output->background, output->background_option);
output->background, output->background_option);
if (output->name) { if (output->name) {
// Try to find the output container and apply configuration now. If // Try to find the output container and apply configuration now. If
@ -258,11 +290,13 @@ struct cmd_results *cmd_output(int argc, char **argv) {
swayc_t *cont = NULL; swayc_t *cont = NULL;
for (int i = 0; i < root_container.children->length; ++i) { for (int i = 0; i < root_container.children->length; ++i) {
cont = root_container.children->items[i]; cont = root_container.children->items[i];
if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { if (cont->name && ((strcmp(cont->name, output->name) == 0) ||
(strcmp(output->name, "*") == 0))) {
apply_output_config(output, cont); apply_output_config(output, cont);
if (strcmp(output->name, "*") != 0) { if (strcmp(output->name, "*") != 0) {
// stop looking if the output config isn't applicable to all outputs // Stop looking if the output config isn't applicable to all
// outputs
break; break;
} }
} }

View File

@ -179,8 +179,6 @@ swayc_t *destroy_output(swayc_t *output) {
add_child(root_container.children->items[p], child); add_child(root_container.children->items[p], child);
} }
sort_workspaces(root_container.children->items[p]); sort_workspaces(root_container.children->items[p]);
// TODO WLR: is this needed anymore?
//update_visibility(root_container.children->items[p]);
arrange_windows(root_container.children->items[p], -1, -1); arrange_windows(root_container.children->items[p], -1, -1);
} }
} }