Exemplo n.º 1
0
static void *allocNBytes(CICcontext *context, int size)
{
    void *result;
    if (context->pass == 1) {
        /* The first pass
	 * A more sophisticated scheme could reduce the number of mallocs.
	 */
        CICmallocs *mallocs = 
	  (CICmallocs *)sysCalloc(1, sizeof(CICmallocs) + size);
	if (mallocs == 0)
	    JAVA_ERROR(context, "out of memory");
	result = (void *)(mallocs + 1);
	mallocs->next = context->pass1.mallocs;
	ROUNDUP_SIZE(size);
	if (context->in_clinit)
	    context->clinit_size += size;
	else
	    context->malloc_size += size;
	context->pass1.mallocs = mallocs;
    } else {
        /* The second pass */

#define ALLOC_BLOCK(ptr,buf,sizelimit) \
	result = (ptr); \
	ROUNDUP_SIZE(size); \
	(ptr) += (size); \
	sysAssert((ptr) <= (buf) + (sizelimit))

        if (context->in_clinit) {
            ALLOC_BLOCK(context->pass2.clinit_ptr,
			context->pass2.clinit_buffer,
			context->clinit_size);
	} else {
            ALLOC_BLOCK(context->pass2.malloc_ptr,
			context->pass2.malloc_buffer,
			context->malloc_size);
	}
    }
    return result;
}
int main(int argc, char *argv[])
{
    int result = -1;
    FILE *in = stdin;
    FILE *out = stdout;
    block_t *first_block = 0;
    block_t *last_block = 0;
    block_t *new_block = 0;
    block_t *loop_block;
    block_t glue_block;
    block_t sundown_output_block;
    int code_opened = 0;
    char line[10240];
    int in_size = 0;
    char *code_pos;
    char *p;
    char *stop;
    while (fgets(line, sizeof(line)- 1, in) > 0)
    {
        int append_len;
        p = line;
        stop = p + strlen(line);
        while (p < stop)
        {
            if ((code_pos=strstr(p, CODE_TAG)))
            {
                if (code_opened)
                {
                    code_opened = 0;
                    if (code_pos > p)
                    {
                        APPEND_TO_LAST_BLOCK(p, code_pos - p);
                    }
                    ALLOC_BLOCK();
                    last_block->type = BLOCK_TYPE_TEXT;
                    p = stop;
                }
                else
                {
                    char *lexer;
                    int lexerlen;
                    code_opened = 1;
                    if (code_pos > p)
                    {
                        APPEND_TO_LAST_BLOCK(p, code_pos - p);
                    }
                    ALLOC_BLOCK();
                    last_block->type = BLOCK_TYPE_CODE;
                    lexer = code_pos + CODE_TAG_LEN;
                    p = lexer + strlen(lexer);
                    rtrim(lexer);
                    lexerlen = strlen(lexer);
                    if (lexerlen + 1 > sizeof(last_block->lexer))
                    {
                        fprintf(stderr, "lexel(%s) too long." NL "", lexer);
                        goto failed;
                    }
                    memcpy(last_block->lexer,
                        lexer, lexerlen + 1);
                }
                continue;
            }
            else if ((!code_opened) && (code_pos=strstr(p, INLINE_CODE_TAG)))
            {
                char *another_code_pos = strstr(code_pos + INLINE_CODE_TAG_LEN, INLINE_CODE_TAG);
                if (another_code_pos)
                {
                    APPEND_TO_LAST_BLOCK(p, 
                        code_pos - p);
                    //inline code
                    ALLOC_BLOCK();
                    last_block->is_inline = 1;
                    last_block->type = BLOCK_TYPE_CODE;
                    APPEND_TO_LAST_BLOCK(code_pos + INLINE_CODE_TAG_LEN, 
                        another_code_pos - (code_pos + INLINE_CODE_TAG_LEN));
                    ALLOC_BLOCK();
                    last_block->type = BLOCK_TYPE_TEXT;
                    p = another_code_pos + INLINE_CODE_TAG_LEN;
                    continue;
                }
            }
            append_len = strlen(p);
            APPEND_TO_LAST_BLOCK(p, append_len);
            p += append_len;
        }
    }
    
    /*
    for (loop_block = first_block;
        loop_block;
        loop_block = loop_block->next)
    {
        int i;
        fprintf(out, "block type:%d lexer:%s length:%d {",
            loop_block->type,
            loop_block->lexer,
            loop_block->length);
        for (i = 0;
            i < loop_block->length;
            ++i)
        {
            fprintf(out, "%c", loop_block->content[i]);
        }
        fprintf(out, "}" NL "");
    }
    */
    
    memset(&glue_block, 0, sizeof(glue_block));
    for (loop_block = first_block;
        loop_block;
        loop_block = loop_block->next)
    {
        if (BLOCK_TYPE_TEXT == loop_block->type)
        {
            APPEND_TO_BLOCK(&glue_block, 
                loop_block->content, loop_block->length);
        }
        else
        {
            char stub[50];
            sprintf(stub, SUNDOWN_PYGMENTIZE_GLUE_STUB_ "%08x",
                (unsigned long)loop_block);
            APPEND_TO_BLOCK(&glue_block, 
                stub, strlen(stub));
        }
    }
    
    //fprintf(out, "%s", glue_block.content);
    //fprintf(stderr, "begin:%s" NL "", SUNDOWN_CMD);
    memset(&sundown_output_block, 0, sizeof(sundown_output_block));
    result = dispatch_tool(SUNDOWN_CMD,
        &glue_block,
        &sundown_output_block);
    if (0 != result)
    {
        fprintf(stderr, "sundown failed." NL "");
        goto failed;
    }
    //fprintf(stderr, "end:%s" NL "", SUNDOWN_CMD);
    p = sundown_output_block.content;
    stop = p + sundown_output_block.length;
    
    //fwrite(p, stop - p, 1, out);
    //return 0;
    
    fprintf(out, "<html>" NL "");
    fprintf(out, "<head>" NL "");
    fprintf(out, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" NL "");
    write_css(out);
    fprintf(out, "</head>" NL "");
    fprintf(out, "<body>" NL "");
    
    while (p < stop)
    {
        block_t *pyg_in;
        block_t pyg_out;
        char cmdline[260];
        char numstr[9];
        code_pos = strstr(p, SUNDOWN_PYGMENTIZE_GLUE_STUB_);
        if (code_pos)
        {
            if (code_pos > p)
            {
                fwrite(p, code_pos - p, 1, out);
            }
            memcpy(numstr, code_pos + SUNDOWN_PYGMENTIZE_GLUE_STUB_LEN, 8);
            numstr[8] = '\0';
            pyg_in = (block_t *)strtoul(numstr, 0, 16);
            if (pyg_in->is_inline)
            {
                fprintf(out, "<code>");
                fwrite(pyg_in->content, pyg_in->length, 1, out);
                fprintf(out, "</code>");
            }
            else
            {
                result = -1;
                memset(&pyg_out, 0, sizeof(pyg_out));
                if (pyg_in->lexer[0])
                {
                    sprintf(cmdline,
                        PYGMENTIZE_CMD,
                        pyg_in->lexer);
                    //fprintf(stderr, "begin:%s" NL "", cmdline);
                    result = dispatch_tool(cmdline,
                        pyg_in,
                        &pyg_out);
                    //fprintf(stderr, "end:%s result:%d" NL "", cmdline, result);
                }
                if (0 == result)
                {
                    fwrite(pyg_out.content, pyg_out.length, 1, out);
                }
                else
                {
                    fprintf(out, "<pre>");
                    fwrite(pyg_in->content, pyg_in->length, 1, out);
                    fprintf(out, "</pre>");
                }
                if (pyg_out.content)
                {
                    free(pyg_out.content);
                }
            }
            p = code_pos + SUNDOWN_PYGMENTIZE_GLUE_STUB_LEN + 8;
        }
        else
        {
            fwrite(p, stop - p, 1, out);
            break;
        }
    }
    fprintf(out, "</body>");
    fprintf(out, "</html>");
    
failed:
    return result;
}