Example #1
0
static void print_header(parser_t *p, lang_c_ctx_t *ctx)
{
    static const char dfun_prefix[] = "page_";
    const char *file;
    char *dfun;
    int i;

    dbg_ifb (p == NULL) return;
    dbg_ifb (ctx == NULL) return;

    (void)ctx;

    io_printf(p->out, "%s", copyright_hdr);
    io_printf(p->out, "#include <klone/emb.h>\n");
    io_printf(p->out, "#include <klone/dypage.h>\n");

    file = ctx->ti->uri + strlen(ctx->ti->uri) - 1;

    for(; *file != '/' && file >= ctx->ti->uri; --file)
        ;

    io_printf(p->out, "static const char *SCRIPT_NAME = \"%s\";\n",
              ++file);

    dfun = ctx->ti->dfun; /* shortcut */

    /* we need a prefix to avoid errors with pages whose filename starts with
       a number (404-handler.kl1) or any other char not allowed by C as the
       first char of function names */
    (void) u_strlcpy(dfun, dfun_prefix, URI_BUFSZ);
    dbg_if (u_strlcat(dfun, file, URI_BUFSZ));

    for(i = 0; i < (int) strlen(dfun); ++i)
        if(!isalnum(dfun[i]))
            dfun[i] = '_'; /* just a-zA-Z0-9 allowed */

    /* add a dummy function that the user can use to set a breakpoint when
       entering the page code. the "static volatile" variable is used to avoid
       the compiler to optimize-out (inlining) the function call */
    io_printf(p->out,
              "static int %s (void) { "
              "static volatile int dummy; return dummy; }\n", dfun);

    io_printf(p->out,
              "static request_t *request = NULL;\n"
              "static response_t *response = NULL;\n"
              "static session_t *session = NULL;\n"
              "static io_t *in = NULL;\n"
              "static io_t *out = NULL;\n");

    return;
}
Example #2
0
/**
 *  \brief  Assemble an URI string starting from its atoms
 *
 *  Assemble an URI string at \p s, starting from its pieces stored in the
 *  supplied ::u_uri_t object \p u
 *      
 *  \param  u   reference to an already filled in ::u_uri_t object
 *  \param  s   reference to an already alloc'd string of size ::U_URI_STRMAX
 *
 *  \retval  0  on success
 *  \retval ~0  on error
 */ 
int u_uri_knead (u_uri_t *u, char s[U_URI_STRMAX])
{
    dbg_return_if (u == NULL, ~0);
    dbg_return_if (u->path == NULL, ~0);    /* path is mandatory */
    dbg_return_if (s == NULL, ~0);

    /* see RFC 3986, Section 5.3. Component Recomposition */

    *s = '\0';

    if (u->scheme)
    {
        dbg_err_if (u_strlcat(s, u->scheme, U_URI_STRMAX));
        dbg_err_if (u_strlcat(s, ":", U_URI_STRMAX));
    }

    if (u->authority)
    {
        dbg_err_if (u_strlcat(s, "//", U_URI_STRMAX));
        dbg_err_if (u_strlcat(s, u->authority, U_URI_STRMAX));
    }
    else /* try recompose authority through its atoms */
        dbg_err_if (knead_authority(u, s));

    dbg_err_if (u_strlcat(s, u->path, U_URI_STRMAX));

    if (u->query)
    {
        dbg_err_if (u_strlcat(s, "?", U_URI_STRMAX));
        dbg_err_if (u_strlcat(s, u->query, U_URI_STRMAX));
    }

    if (u->fragment)
    {
        dbg_err_if (u_strlcat(s, "#", U_URI_STRMAX));
        dbg_err_if (u_strlcat(s, u->fragment, U_URI_STRMAX));
    }
 
    return 0;
err:
    return ~0;
}