diff options
author | Mattias Andrée <maandree@kth.se> | 2016-07-14 13:12:39 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2016-07-14 13:12:39 +0200 |
commit | 7d3b92ae82191bdc42598390e377b91fedda2be3 (patch) | |
tree | 64e8352cf423571a9c0f122ac325cf6029d27dde /src/server.c | |
parent | Add todo (diff) | |
download | coopgammad-7d3b92ae82191bdc42598390e377b91fedda2be3.tar.gz coopgammad-7d3b92ae82191bdc42598390e377b91fedda2be3.tar.bz2 coopgammad-7d3b92ae82191bdc42598390e377b91fedda2be3.tar.xz |
Add MAKE_MESSAGE macro
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | src/server.c | 186 |
1 files changed, 71 insertions, 115 deletions
diff --git a/src/server.c b/src/server.c index 1db4d61..34b6702 100644 --- a/src/server.c +++ b/src/server.c @@ -100,6 +100,30 @@ extern size_t outputs_n; /** + * Construct a message + * + * @param bufp:char** Output parameter for the buffer, must not have side-effects + * @param np:size_t* Output parameter for the size of the buffer sans `extra` + * @param extra:size_t The extra number for bytes to allocate to the buffer + * @param format:string-literal Message format string + * @param ... Message formatting arguments + */ +#define MAKE_MESSAGE(bufp, np, extra, format, ...) \ + do \ + { \ + ssize_t m__; \ + snprintf(NULL, 0, format "%zn", __VA_ARGS__, &m__); \ + *(bufp) = malloc((size_t)(extra) + (size_t)m__); \ + if (*(bufp) == NULL) \ + return -1; \ + sprintf(*(bufp), format, __VA_ARGS__); \ + *(np) = (size_t)m__; \ + } \ + while (0) + + + +/** * Destroy the state of the connections * * @param disconnect Disconnect all connections? @@ -458,7 +482,6 @@ static inline int continue_send(size_t conn) static int enumerate_crtcs(size_t conn, char* message_id) { size_t i, n = 0, len; - ssize_t m; char* buf; if (message_id == NULL) @@ -467,22 +490,11 @@ static int enumerate_crtcs(size_t conn, char* message_id) for (i = 0; i < outputs_n; i++) n += strlen(outputs[i].name) + 1; - snprintf(NULL, 0, - "In response to: %s\n" - "Length: %zu\n" - "\n%zn", - message_id, n, &m); - - if (!(buf = malloc(n + (size_t)m))) - return -1; - - sprintf(buf, - "In response to: %s\n" - "Length: %zu\n" - "\n", - message_id, n); - - n = (size_t)m; + MAKE_MESSAGE(&buf, &n, 0, + "In response to: %s\n" + "Length: %zu\n" + "\n", + message_id, n); for (i = 0; i < outputs_n; i++) { @@ -511,7 +523,7 @@ static int get_gamma_info(size_t conn, char* message_id, char* crtc) char* buf; char depth[3]; const char* supported; - ssize_t n; + size_t n; if ((message_id == NULL) || (crtc == NULL)) return fprintf(stderr, "%s: ignoring incomplete Command: get-gamma-info message\n", argv0), 0; @@ -519,28 +531,16 @@ static int get_gamma_info(size_t conn, char* message_id, char* crtc) output = output_find_by_name(crtc, outputs, outputs_n); if (output == NULL) { - snprintf(NULL, 0, - "Command: error\n" - "In response to: %s\n" - "Error: custom\n" - "Length: 13\n" - "\n" - "No such CRTC\n%zn", - message_id, &n); - - if (!(buf = malloc((size_t)n))) - return -1; + MAKE_MESSAGE(&buf, &n, 0, + "Command: error\n" + "In response to: %s\n" + "Error: custom\n" + "Length: 13\n" + "\n" + "No such CRTC\n", + message_id); - sprintf(buf, - "Command: error\n" - "In response to: %s\n" - "Error: custom\n" - "Length: 13\n" - "\n" - "No such CRTC\n", - message_id); - - return send_message(conn, buf, (size_t)n); + return send_message(conn, buf, n); } switch (output->depth) @@ -563,34 +563,19 @@ static int get_gamma_info(size_t conn, char* message_id, char* crtc) default: supported = "maybe"; break; } - snprintf(NULL, 0, - "In response to: %s\n" - "Cooperative: yes\n" - "Depth: %s\n" - "Red size: %zu\n" - "Green size: %zu\n" - "Blue size: %zu\n" - "Gamma support: %s\n" - "\n%zn", - message_id, depth, output->red_size, output->green_size, - output->blue_size, supported, &n); - - if (!(buf = malloc((size_t)n))) - return -1; - - sprintf(buf, - "In response to: %s\n" - "Cooperative: yes\n" - "Depth: %s\n" - "Red size: %zu\n" - "Green size: %zu\n" - "Blue size: %zu\n" - "Gamma support: %s\n" - "\n", - message_id, depth, output->red_size, output->green_size, - output->blue_size, supported); + MAKE_MESSAGE(&buf, &n, 0, + "In response to: %s\n" + "Cooperative: yes\n" + "Depth: %s\n" + "Red size: %zu\n" + "Green size: %zu\n" + "Blue size: %zu\n" + "Gamma support: %s\n" + "\n", + message_id, depth, output->red_size, output->green_size, + output->blue_size, supported); - return send_message(conn, buf, (size_t)n); + return send_message(conn, buf, n); } @@ -613,7 +598,6 @@ static int get_gamma(size_t conn, char* message_id, char* crtc, char* coalesce, int64_t high, low; int coal; char* buf; - ssize_t m; size_t start, end, len, n, i; char depth[3]; char tables[sizeof("Tables: \n") + 3 * sizeof(size_t)]; @@ -645,28 +629,16 @@ static int get_gamma(size_t conn, char* message_id, char* crtc, char* coalesce, if (error != NULL) { - snprintf(NULL, 0, - "Command: error\n" - "In response to: %s\n" - "Error: custom\n" - "Length: %zu\n" - "\n" - "%s\n%zn", - message_id, strlen(error) + 1, error, &m); + MAKE_MESSAGE(&buf, &n, 0, + "Command: error\n" + "In response to: %s\n" + "Error: custom\n" + "Length: %zu\n" + "\n" + "%s\n", + message_id, strlen(error) + 1, error); - if (!(buf = malloc((size_t)m))) - return -1; - - sprintf(buf, - "Command: error\n" - "In response to: %s\n" - "Error: custom\n" - "Length: %zu\n" - "\n" - "%s\n", - message_id, strlen(error) + 1, error); - - return send_message(conn, buf, (size_t)m); + return send_message(conn, buf, n); } for (start = 0; start < output->table_size; start++) @@ -703,34 +675,18 @@ static int get_gamma(size_t conn, char* message_id, char* crtc, char* coalesce, n += strlen(output->table_filters[i].class) + 1; } - snprintf(NULL, 0, - "In response to: %s\n" - "Depth: %s\n" - "Red size: %zu\n" - "Green size: %zu\n" - "Blue size: %zu\n" - "%s" - "Length: %zu\n" - "\n%zn", - message_id, depth, output->red_size, output->green_size, - output->blue_size, tables, n, &m); - - if (!(buf = malloc(n + (size_t)m))) - return -1; - - sprintf(buf, - "In response to: %s\n" - "Depth: %s\n" - "Red size: %zu\n" - "Green size: %zu\n" - "Blue size: %zu\n" - "%s" - "Length: %zu\n" - "\n", - message_id, depth, output->red_size, output->green_size, - output->blue_size, tables, n); + MAKE_MESSAGE(&buf, &n, 0, + "In response to: %s\n" + "Depth: %s\n" + "Red size: %zu\n" + "Green size: %zu\n" + "Blue size: %zu\n" + "%s" + "Length: %zu\n" + "\n", + message_id, depth, output->red_size, output->green_size, + output->blue_size, tables, n); - n = (size_t)m; if (coal) { if (start == 0) |