2017-12-08 02:07:30 +00:00
|
|
|
/*
|
|
|
|
* renderer-msvc.c
|
|
|
|
* MSVC library syntax renderer
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 pkgconf authors (see AUTHORS).
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <libpkgconf/libpkgconf.h>
|
|
|
|
#include "renderer-msvc.h"
|
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
static inline bool
|
|
|
|
fragment_should_quote(const pkgconf_fragment_t *frag)
|
2017-12-08 02:07:30 +00:00
|
|
|
{
|
2018-01-05 17:31:43 +00:00
|
|
|
const char *src;
|
|
|
|
|
|
|
|
if (frag->data == NULL)
|
|
|
|
return false;
|
2017-12-08 02:07:30 +00:00
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
for (src = frag->data; *src; src++)
|
2017-12-08 02:07:30 +00:00
|
|
|
{
|
|
|
|
if (((*src < ' ') ||
|
2018-01-05 17:31:43 +00:00
|
|
|
(*src >= (' ' + (frag->merged ? 1 : 0)) && *src < '$') ||
|
2017-12-08 02:07:30 +00:00
|
|
|
(*src > '$' && *src < '(') ||
|
|
|
|
(*src > ')' && *src < '+') ||
|
|
|
|
(*src > ':' && *src < '=') ||
|
|
|
|
(*src > '=' && *src < '@') ||
|
|
|
|
(*src > 'Z' && *src < '^') ||
|
|
|
|
(*src == '`') ||
|
|
|
|
(*src > 'z' && *src < '~') ||
|
2018-01-05 17:31:43 +00:00
|
|
|
(*src > '~')))
|
|
|
|
return true;
|
2017-12-08 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
return false;
|
2017-12-08 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t
|
2018-01-05 17:31:43 +00:00
|
|
|
fragment_len(const pkgconf_fragment_t *frag)
|
2017-12-08 02:07:30 +00:00
|
|
|
{
|
|
|
|
size_t len = 1;
|
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
if (frag->type)
|
|
|
|
len += 2;
|
|
|
|
|
|
|
|
if (frag->data != NULL)
|
2017-12-08 02:07:30 +00:00
|
|
|
{
|
2018-01-05 17:31:43 +00:00
|
|
|
len += strlen(frag->data);
|
|
|
|
|
|
|
|
if (fragment_should_quote(frag))
|
|
|
|
len += 2;
|
2017-12-08 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
allowed_fragment(const pkgconf_fragment_t *frag)
|
|
|
|
{
|
2018-01-06 00:05:47 +00:00
|
|
|
return !(!frag->type || frag->data == NULL || strchr("DILl", frag->type) == NULL);
|
2017-12-08 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
msvc_renderer_render_len(const pkgconf_list_t *list, bool escape)
|
|
|
|
{
|
2018-01-05 17:31:43 +00:00
|
|
|
(void) escape;
|
|
|
|
|
2017-12-08 02:07:30 +00:00
|
|
|
size_t out = 1; /* trailing nul */
|
|
|
|
pkgconf_node_t *node;
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
|
|
|
|
{
|
|
|
|
const pkgconf_fragment_t *frag = node->data;
|
|
|
|
|
|
|
|
if (!allowed_fragment(frag))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (frag->type)
|
|
|
|
{
|
|
|
|
case 'L':
|
|
|
|
out += 9; /* "/libpath:" */
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
out += 4; /* ".lib" */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
out += fragment_len(frag);
|
2017-12-08 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
msvc_renderer_render_buf(const pkgconf_list_t *list, char *buf, size_t buflen, bool escape)
|
|
|
|
{
|
|
|
|
pkgconf_node_t *node;
|
|
|
|
char *bptr = buf;
|
|
|
|
|
|
|
|
memset(buf, 0, buflen);
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
|
|
|
|
{
|
|
|
|
const pkgconf_fragment_t *frag = node->data;
|
|
|
|
size_t buf_remaining = buflen - (bptr - buf);
|
2018-01-05 17:31:43 +00:00
|
|
|
size_t cnt;
|
2017-12-08 02:07:30 +00:00
|
|
|
|
|
|
|
if (!allowed_fragment(frag))
|
|
|
|
continue;
|
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
if (fragment_len(frag) > buf_remaining)
|
2017-12-08 02:07:30 +00:00
|
|
|
break;
|
|
|
|
|
2018-01-06 00:05:47 +00:00
|
|
|
switch(frag->type) {
|
|
|
|
case 'D':
|
|
|
|
case 'I':
|
|
|
|
*bptr++ = '/';
|
|
|
|
*bptr++ = frag->type;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
cnt = pkgconf_strlcpy(bptr, "/libpath:", buf_remaining);
|
2017-12-08 02:07:30 +00:00
|
|
|
bptr += cnt;
|
|
|
|
buf_remaining -= cnt;
|
2018-01-06 00:05:47 +00:00
|
|
|
break;
|
2017-12-08 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
escape = fragment_should_quote(frag);
|
2017-12-08 02:07:30 +00:00
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
if (escape)
|
|
|
|
*bptr++ = '"';
|
|
|
|
|
|
|
|
cnt = pkgconf_strlcpy(bptr, frag->data, buf_remaining);
|
|
|
|
bptr += cnt;
|
|
|
|
buf_remaining -= cnt;
|
2017-12-08 02:07:30 +00:00
|
|
|
|
|
|
|
if (frag->type == 'l')
|
|
|
|
{
|
2018-01-06 00:05:47 +00:00
|
|
|
cnt = pkgconf_strlcpy(bptr, ".lib", buf_remaining);
|
2017-12-08 02:07:30 +00:00
|
|
|
bptr += cnt;
|
|
|
|
buf_remaining -= cnt;
|
|
|
|
}
|
|
|
|
|
2018-01-05 17:31:43 +00:00
|
|
|
if (escape)
|
|
|
|
*bptr++ = '"';
|
|
|
|
|
2017-12-08 02:07:30 +00:00
|
|
|
*bptr++ = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
*bptr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static const pkgconf_fragment_render_ops_t msvc_renderer_ops = {
|
|
|
|
.render_len = msvc_renderer_render_len,
|
|
|
|
.render_buf = msvc_renderer_render_buf
|
|
|
|
};
|
|
|
|
|
|
|
|
const pkgconf_fragment_render_ops_t *
|
|
|
|
msvc_renderer_get(void)
|
|
|
|
{
|
|
|
|
return &msvc_renderer_ops;
|
|
|
|
}
|