Code Map

1 worgle.c

worgle.org:44

<<worgle-top_0>>=

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parg.h"

1.1 global_variables

worgle.org:2016

<<global_variables_0>>=

static int use_debug = 0;

worgle.org:2026

<<global_variables_1>>=

static int use_warnings = 0;

worgle.org:2088

<<global_variables_2>>=

static int map_source_code = 0;
static char *map_filename = NULL;

1.2 enums

worgle.org:468

<<enums_0>>=

enum {

1.2.1 parse_modes

worgle.org:476

<<parse_modes_0>>=

MODE_ORG,

worgle.org:573

<<parse_modes_1>>=

MODE_BEGINCODE,

worgle.org:721

<<parse_modes_2>>=

MODE_CODE

worgle.org:470

<<enums_2>>=

};

1.3 structs

1.3.1 worgle_string

worgle.org:1118

<<worgle_string_0>>=

typedef struct {
    char *str;
    size_t size;
} worgle_string;

1.3.2 worgle_segment

worgle.org:1184

<<worgle_segment_0>>=

enum {

1.3.2.1 worgle_segment_types

worgle.org:1212

<<worgle_segment_types_0>>=

SEGTYPE_TEXT,

worgle.org:1224

<<worgle_segment_types_1>>=

SEGTYPE_REFERENCE

worgle.org:1186

<<worgle_segment_2>>=

};
typedef struct worgle_segment {
    int type;
    worgle_string str;

1.3.2.2 worgle_segment_line_control

worgle.org:1201

<<worgle_segment_line_control_0>>=

size_t linum;
worgle_string *filename;

worgle.org:1191

<<worgle_segment_4>>=

    struct worgle_segment *nxt;
} worgle_segment;

1.3.3 worgle_block

worgle.org:1314

<<worgle_block_0>>=

typedef struct worgle_block {
    int nsegs;
    worgle_segment *head;
    worgle_segment *tail;
    worgle_string name;
    int am_i_used;
    struct worgle_block *nxt;
} worgle_block;

1.3.4 worgle_blocklist

worgle.org:1510

<<worgle_blocklist_0>>=

typedef struct {
    int nblocks;
    worgle_block *head;
    worgle_block *tail;
} worgle_blocklist;

1.3.5 worgle_hashmap

worgle.org:1589

<<worgle_hashmap_0>>=

#define HASH_SIZE 256
typedef struct {
    worgle_blocklist blk[HASH_SIZE];
    int nwords;
} worgle_hashmap;

1.3.6 worgle_file

worgle.org:1731

<<worgle_file_0>>=

typedef struct worgle_file {
    worgle_string filename;
    worgle_block *top;
    struct worgle_file *nxt;
} worgle_file;

1.3.7 worgle_filelist

worgle.org:1784

<<worgle_filelist_0>>=

typedef struct {
    worgle_file *head;
    worgle_file *tail;
    int nfiles;
} worgle_filelist;

1.3.8 worgle_textbuf

worgle.org:1898

<<worgle_textbuf_0>>=

typedef struct {
    char *buf;
    size_t size;
    worgle_string filename;
} worgle_textbuf;

1.3.9 worgle_struct

worgle.org:916

<<worgle_struct_0>>=

typedef struct {

1.3.9.1 worgle_struct_contents

worgle.org:966

<<worgle_struct_contents_0>>=

worgle_string block; /* TODO: rename */

worgle.org:982

<<worgle_struct_contents_1>>=

size_t curline;

worgle.org:997

<<worgle_struct_contents_2>>=

int block_started;

worgle.org:1012

<<worgle_struct_contents_3>>=

worgle_hashmap dict;

worgle.org:1034

<<worgle_struct_contents_4>>=

worgle_filelist flist;

worgle.org:1057

<<worgle_struct_contents_5>>=

worgle_textbuf *curbuf;
worgle_textbuf *buffers;
int nbuffers;

worgle.org:1087

<<worgle_struct_contents_6>>=

worgle_block *curblock;

worgle.org:1101

<<worgle_struct_contents_7>>=

size_t linum;

worgle.org:918

<<worgle_struct_2>>=

} worgle_d;

1.4 static_function_declarations

worgle.org:499

<<static_function_declarations_0>>=

static int parse_name(char *line, size_t len, worgle_string *str);

worgle.org:793

<<static_function_declarations_1>>=

static int check_for_reference(char *line , size_t size, worgle_string *str);

1.5 function_declarations

worgle.org:166

<<function_declarations_0>>=

static int loadfile(worgle_d *worg, int file);

worgle.org:267

<<function_declarations_1>>=

int parse_file(worgle_d *worg, int file);

worgle.org:411

<<function_declarations_2>>=

static int worgle_getline(char *fullbuf,
                  char **line,
                  size_t *pos,
                  size_t *line_size,
                  size_t buf_size);

worgle.org:555

<<function_declarations_3>>=

void worgle_begin_block(worgle_d *worg, worgle_string *name);

worgle.org:609

<<function_declarations_4>>=

static int parse_begin(char *line, size_t len, worgle_string *str);

worgle.org:707

<<function_declarations_5>>=

void worgle_append_file(worgle_d *worg, worgle_string *filename);

worgle.org:759

<<function_declarations_6>>=

void worgle_append_string(worgle_d *worg);

worgle.org:776

<<function_declarations_7>>=

void worgle_append_reference(worgle_d *worg, worgle_string *ref);

worgle.org:864

<<function_declarations_8>>=

int worgle_generate(worgle_d *worg);

worgle.org:927

<<function_declarations_9>>=

void worgle_init(worgle_d *worg);

worgle.org:945

<<function_declarations_10>>=

void worgle_free(worgle_d *worg);

worgle.org:1129

<<function_declarations_11>>=

void worgle_string_reset(worgle_string *str);

worgle.org:1146

<<function_declarations_12>>=

void worgle_string_init(worgle_string *str);

worgle.org:1165

<<function_declarations_13>>=

int worgle_string_write(FILE *fp, worgle_string *str);

worgle.org:1231

<<function_declarations_14>>=

void worgle_segment_init(worgle_segment *s,
                        int type,
                        worgle_string *str,
                        worgle_string *filename,
                        size_t linum);

worgle.org:1263

<<function_declarations_15>>=

int worgle_segment_write(worgle_segment *s, worgle_hashmap *h, FILE *fp);

worgle.org:1329

<<function_declarations_16>>=

void worgle_block_init(worgle_block *b);

worgle.org:1352

<<function_declarations_17>>=

void worgle_block_free(worgle_block *lst);

worgle.org:1387

<<function_declarations_18>>=

void worgle_block_append_segment(worgle_block *b,
                                worgle_string *str,
                                int type,
                                size_t linum,
                                worgle_string *filename);

worgle.org:1425

<<function_declarations_19>>=

void worgle_block_append_string(worgle_block *b,
                               worgle_string *str,
                               size_t linum,
                               worgle_string *filename);

worgle.org:1448

<<function_declarations_20>>=

void worgle_block_append_reference(worgle_block *b,
                                  worgle_string *str,
                                  size_t linum,
                                  worgle_string *filename);

worgle.org:1480

<<function_declarations_21>>=

int worgle_block_write(worgle_block *b, worgle_hashmap *h, FILE *fp);

worgle.org:1522

<<function_declarations_22>>=

void worgle_blocklist_init(worgle_blocklist *lst);

worgle.org:1541

<<function_declarations_23>>=

void worgle_blocklist_free(worgle_blocklist *lst);

worgle.org:1567

<<function_declarations_24>>=

void worgle_blocklist_append(worgle_blocklist *lst, worgle_block *b);

worgle.org:1601

<<function_declarations_25>>=

void worgle_hashmap_init(worgle_hashmap *h);

worgle.org:1624

<<function_declarations_26>>=

void worgle_hashmap_free(worgle_hashmap *h);

worgle.org:1648

<<function_declarations_27>>=

int worgle_hashmap_find(worgle_hashmap *h, worgle_string *name, worgle_block **b);

worgle.org:1703

<<function_declarations_28>>=

worgle_block * worgle_hashmap_get(worgle_hashmap *h, worgle_string *name);

worgle.org:1745

<<function_declarations_29>>=

int worgle_file_write(worgle_file *f, worgle_hashmap *h);

worgle.org:1797

<<function_declarations_30>>=

void worgle_filelist_init(worgle_filelist *flist);

worgle.org:1816

<<function_declarations_31>>=

void worgle_filelist_free(worgle_filelist *flist);

worgle.org:1841

<<function_declarations_32>>=

void worgle_filelist_append(worgle_filelist *flist,
                           worgle_string *name,
                           worgle_block *top);

worgle.org:1874

<<function_declarations_33>>=

int worgle_filelist_write(worgle_filelist *flist, worgle_hashmap *h);

worgle.org:1911

<<function_declarations_34>>=

void worgle_textbuf_zero(worgle_textbuf *txt);

worgle.org:1927

<<function_declarations_35>>=

void worgle_textbuf_init(worgle_textbuf *txt,
                         char *buf,
                         size_t bufsize);

worgle.org:1951

<<function_declarations_36>>=

void worgle_textbuf_free(worgle_textbuf *txt);

worgle.org:2049

<<function_declarations_37>>=

int worgle_warn_unused(worgle_d *worg);

worgle.org:2106

<<function_declarations_38>>=

static void append_filename(worgle_d *worg, char *filename);

worgle.org:2149

<<function_declarations_39>>=

void worgle_map(worgle_d *worg, worgle_block *b, int lvl, FILE *out);

worgle.org:2198

<<function_declarations_40>>=

void worgle_map_files(worgle_d *worg, char *filename);
void worgle_map_a_file(worgle_d *worg, worgle_file *file, FILE *out);

1.6 functions

worgle.org:171

<<functions_0>>=

static int loadfile(worgle_d *worg, int file)
{

1.6.1 loadfile_localvars

worgle.org:183

<<loadfile_localvars_0>>=

FILE *fp;
char *filename;
worgle_textbuf *txt;

worgle.org:205

<<loadfile_localvars_1>>=

size_t size;

worgle.org:219

<<loadfile_localvars_2>>=

char *buf;

1.6.2 loadfile

worgle.org:190

<<loadfile_0>>=

txt = &worg->buffers[file];
filename = txt->filename.str;
fp = fopen(filename, "r");
if(fp == NULL) {
    fprintf(stderr, "Could not find file %s\n", filename);
    return 1;
}

worgle.org:210

<<loadfile_1>>=

fseek(fp, 0, SEEK_END);
size = ftell(fp);

worgle.org:224

<<loadfile_2>>=

buf = calloc(1, size);
worgle_textbuf_init(&worg->buffers[file], buf, size);

worgle.org:233

<<loadfile_3>>=

fseek(fp, 0, SEEK_SET);
fread(buf, size, 1, fp);
fclose(fp);

worgle.org:175

<<functions_4>>=

    return 1;
}

worgle.org:272

<<functions_5>>=

int parse_file(worgle_d *worg, int file)
{
    char *buf;
    size_t size;
    worgle_textbuf *curbuf;
    curbuf = &worg->buffers[file];
    buf = curbuf->buf;
    size = curbuf->size;
    worg->curbuf = curbuf;

1.6.3 parser_local_variables

worgle.org:304

<<parser_local_variables_0>>=

worgle_string str;

worgle.org:316

<<parser_local_variables_1>>=

char *line;

worgle.org:328

<<parser_local_variables_2>>=

size_t pos;

worgle.org:340

<<parser_local_variables_3>>=

size_t read;

worgle.org:347

<<parser_local_variables_4>>=

int mode;

worgle.org:361

<<parser_local_variables_5>>=

int rc;

worgle.org:376

<<parser_local_variables_6>>=

int status;

1.6.4 parser_initialization

worgle.org:309

<<parser_initialization_0>>=

worgle_string_init(&str);

worgle.org:321

<<parser_initialization_1>>=

line = NULL;

worgle.org:333

<<parser_initialization_2>>=

pos = 0;

worgle.org:354

<<parser_initialization_3>>=

mode = MODE_ORG;

worgle.org:368

<<parser_initialization_4>>=

rc = 0;

worgle.org:383

<<parser_initialization_5>>=

status = 0;

worgle.org:284

<<functions_9>>=

    while(1) {

1.6.5 getline

worgle.org:404

<<getline_0>>=

worg->linum++;
status = worgle_getline(buf, &line, &pos, &read, size);
if(!status) break;

worgle.org:286

<<functions_11>>=

        if(mode == MODE_ORG) {

1.6.6 parse_mode_org

worgle.org:485

<<parse_mode_org_0>>=

if(read >= 7) {
    if(!strncmp(line, "#+NAME:",7)) {
        mode = MODE_BEGINCODE;
        parse_name(line, read, &str);
        worgle_begin_block(worg, &str);
    }
}

worgle.org:288

<<functions_13>>=

        } else if(mode == MODE_CODE) {

1.6.7 parse_mode_code

worgle.org:730

<<parse_mode_code_0>>=

if(read >= 9) {
    if(!strncmp(line, "#+END_SRC", 9)) {
        mode = MODE_ORG;
        worg->block_started = 0;
        worgle_append_string(worg);
        continue;
    }
}
if(check_for_reference(line, read, &str)) {
    worgle_append_string(worg);
    worgle_append_reference(worg, &str);
    worg->block_started = 1;
    worgle_string_reset(&worg->block);
    continue;
}
worg->block.size += read;
if(worg->block_started) {
    worg->block.str = line;
    worg->block_started = 0;
    worg->curline = worg->linum;
}

worgle.org:290

<<functions_15>>=

        } else if(mode == MODE_BEGINCODE) {

1.6.8 parse_mode_begincode

worgle.org:583

<<parse_mode_begincode_0>>=

if(read >= 11) {
    if(!strncmp(line, "#+BEGIN_SRC",11)) {

1.6.8.1 begin_the_code

worgle.org:697

<<begin_the_code_0>>=

mode = MODE_CODE;
worg->block_started = 1;
worgle_string_reset(&worg->block);

worgle.org:586

<<parse_mode_begincode_2>>=

        if(parse_begin(line, read, &str) == 2) {
            worgle_append_file(worg, &str);
        }
        continue;
    } else {
        fwrite(line, read, 1, stderr);
        fprintf(stderr, "line %lu: Expected #+BEGIN_SRC\n", worg->linum);
        rc = 1;
        break;
    }
}
fprintf(stderr, "line %lu: Expected #+BEGIN_SRC\n", worg->linum);
rc = 1;

worgle.org:292

<<functions_17>>=

        }
    }
    return rc;
}

worgle.org:432

<<functions_18>>=

static int worgle_getline(char *fullbuf,
                  char **line,
                  size_t *pos,
                  size_t *line_size,
                  size_t buf_size)
{
    size_t p;
    size_t s;
    *line_size = 0;
    p = *pos;
    *line = &fullbuf[p];
    s = 0;
    while(1) {
        s++;
        if(p >= buf_size) return 0;
        if(fullbuf[p] == '\n') {
            *pos = p + 1;
            *line_size = s;
            return 1;
        }
        p++;
    }
}

worgle.org:506

<<functions_19>>=

static int parse_name(char *line, size_t len, worgle_string *str)
{
    size_t n;
    size_t pos;
    int mode;
    line+=7;
    len-=7;
    /* *namelen = 0; */
    str->size = 0;
    str->str = NULL;
    if(len <= 0) return 1;
    pos = 0;
    mode = 0;
    for(n = 0; n < len; n++) {
        if(mode == 2) break;
        switch(mode) {
            case 0:
                if(line[n] == ' ') {
                } else {
                    str->str = &line[n];
                    str->size++;
                    pos++;
                    mode = 1;
                }
                break;
            case 1:
                if(line[n] == 0xa) {
                    mode = 2;
                    break;
                }
                pos++;
                str->size++;
                break;
            default:
                break;
        }
    }
    /* *namelen = pos; */
    return 1;
}

worgle.org:563

<<functions_20>>=

void worgle_begin_block(worgle_d *worg, worgle_string *name)
{
    worg->curblock = worgle_hashmap_get(&worg->dict, name);
}

worgle.org:625

<<functions_21>>=

static int parse_begin(char *line, size_t len, worgle_string *str)
{
    size_t n;
    int mode;
    int rc;
    line += 11;
    len -= 11;
    if(len <= 0) return 0;
    mode = 0;
    n = 0;
    rc = 1;
    str->str = NULL;
    str->size = 0;
    while(n < len) {
        switch(mode) {
            case 0: /* initial spaces after BEGIN_SRC */
                if(line[n] == ' ') {
                    n++;
                } else {
                    mode = 1;
                }
                break;
            case 1: /* look for :tangle */
                if(line[n] == ' ') {
                    mode = 0;
                    n++;
                } else {
                    if(line[n] == ':') {
                        if(!strncmp(line + n + 1, "tangle", 6)) {
                            n+=7;
                            mode = 2;
                            rc = 2;
                        }
                    }
                    n++;
                }
                break;
            case 2: /* save file name, spaces after tangle */
                if(line[n] != ' ') {
                    str->str = &line[n];
                    str->size++;
                    mode = 3;
                }
                n++;
                break;
            case 3: /* read up to next space or line break */
                if(line[n] == ' ' || line[n] == '\n') {
                    mode = 4;
                } else {
                    str->size++;
                }
                n++;
                break;
            case 4: /* countdown til end */
                n++;
                break;
        }
    }
    return rc;
}

worgle.org:712

<<functions_22>>=

void worgle_append_file(worgle_d *worg, worgle_string *filename)
{
    worgle_filelist_append(&worg->flist, filename, worg->curblock);
}

worgle.org:764

<<functions_23>>=

void worgle_append_string(worgle_d *worg)
{
    if(worg->curblock == NULL) return;
    worgle_block_append_string(worg->curblock,
                              &worg->block,
                              worg->curline,
                              &worg->curbuf->filename);
}

worgle.org:781

<<functions_24>>=

void worgle_append_reference(worgle_d *worg, worgle_string *ref)
{
    if(worg->curblock == NULL) return;
    worgle_block_append_reference(worg->curblock,
                                 ref,
                                 worg->linum,
                                 &worg->curbuf->filename);
}

worgle.org:798

<<functions_25>>=

static int check_for_reference(char *line , size_t size, worgle_string *str)
{
    int mode;
    size_t n;
    mode = 0;
    str->size = 0;
    str->str = NULL;
    for(n = 0; n < size; n++) {
        if(mode < 0) break;
        switch(mode) {
            case 0: /* spaces */
                if(line[n] == ' ') continue;
                else if(line[n] == '<') mode = 1;
                else mode = -1;
                break;
            case 1: /* second < */
                if(line[n] == '<') mode = 2;
                else mode = -1;
                break;
            case 2: /* word setup */
                str->str = &line[n];
                str->size++;
                mode = 3;
                break;
            case 3: /* the word */
                if(line[n] == '>') {
                    mode = 4;
                    break;
                }
                str->size++;
                break;
            case 4: /* last > */
                if(line[n] == '>') mode = 5;
                else mode = -1;
                break;
        }
    }
    return (mode == 5);
}

worgle.org:869

<<functions_26>>=

int worgle_generate(worgle_d *worg)
{
    return worgle_filelist_write(&worg->flist, &worg->dict);
}

worgle.org:932

<<functions_27>>=

void worgle_init(worgle_d *worg)
{

1.6.9 worgle_init

worgle.org:973

<<worgle_init_0>>=

worgle_string_init(&worg->block);

worgle.org:989

<<worgle_init_1>>=

worg->curline = -1;

worgle.org:1004

<<worgle_init_2>>=

worg->block_started = 0;

worgle.org:1018

<<worgle_init_3>>=

worgle_hashmap_init(&worg->dict);

worgle.org:1041

<<worgle_init_4>>=

worgle_filelist_init(&worg->flist);

worgle.org:1067

<<worgle_init_5>>=

worg->curbuf = NULL;
worg->buffers = NULL;
worg->nbuffers = 0;

worgle.org:1093

<<worgle_init_6>>=

worg->curblock = NULL;

worgle.org:1109

<<worgle_init_7>>=

worg->linum = 0;

worgle.org:935

<<functions_29>>=

}

worgle.org:950

<<functions_30>>=

void worgle_free(worgle_d *worg)
{
    int i;

1.6.10 worgle_free

worgle.org:1026

<<worgle_free_0>>=

worgle_hashmap_free(&worg->dict);

worgle.org:1048

<<worgle_free_1>>=

worgle_filelist_free(&worg->flist);

worgle.org:1076

<<worgle_free_2>>=

for(i = 0; i < worg->nbuffers; i++) {
    worgle_textbuf_free(&worg->buffers[i]);
}
if(worg->nbuffers > 0) free(worg->buffers);

worgle.org:954

<<functions_32>>=

}

worgle.org:1134

<<functions_33>>=

void worgle_string_reset(worgle_string *str)
{
    str->str = NULL;
    str->size = 0;
}

worgle.org:1151

<<functions_34>>=

void worgle_string_init(worgle_string *str)
{
    worgle_string_reset(str);
}

worgle.org:1172

<<functions_35>>=

int worgle_string_write(FILE *fp, worgle_string *str)
{
    return fwrite(str->str, 1, str->size, fp);
}

worgle.org:1240

<<functions_36>>=

void worgle_segment_init(worgle_segment *s,
                        int type,
                        worgle_string *str,
                        worgle_string *filename,
                        size_t linum)
{
   s->type = type;
   s->str = *str;
   s->filename = filename;
   s->linum = linum;
}

worgle.org:1281

<<functions_37>>=

int worgle_segment_write(worgle_segment *s, worgle_hashmap *h, FILE *fp)
{
    worgle_block *b;
    if(s->type == SEGTYPE_TEXT) {
        if(use_debug) {
            fprintf(fp, "#line %lu \"", s->linum);
            worgle_string_write(fp, s->filename);
            fprintf(fp, "\"\n");
        }
        worgle_string_write(fp, &s->str);
    } else {
        if(!worgle_hashmap_find(h, &s->str, &b)) {
            fprintf(stderr, "Warning: could not find reference segment '");
            worgle_string_write(stderr, &s->str);
            fprintf(stderr, "'\n");
            if(use_warnings == 2) {
                return 0;
            } else {
                return 1;
            }
        }
        return worgle_block_write(b, h, fp);
    }
    return 1;
}

worgle.org:1337

<<functions_38>>=

void worgle_block_init(worgle_block *b)
{
    b->nsegs = 0;
    b->head = NULL;
    b->tail = NULL;
    b->nxt = NULL;
    b->am_i_used = 0;
    worgle_string_init(&b->name);
}

worgle.org:1361

<<functions_39>>=

void worgle_block_free(worgle_block *lst)
{
    worgle_segment *s;
    worgle_segment *nxt;
    int n;
    s = lst->head;
    for(n = 0; n < lst->nsegs; n++) {
        nxt = s->nxt;
        free(s);
        s = nxt;
    }
}

worgle.org:1399

<<functions_40>>=

void worgle_block_append_segment(worgle_block *b,
                                worgle_string *str,
                                int type,
                                size_t linum,
                                worgle_string *filename)
{
    worgle_segment *s;
    s = malloc(sizeof(worgle_segment));
    if(b->nsegs == 0) {
        b->head = s;
        b->tail = s;
    }
    worgle_segment_init(s, type, str, filename, linum);
    b->tail->nxt = s;
    b->tail = s;
    b->nsegs++;
}

worgle.org:1433

<<functions_41>>=

void worgle_block_append_string(worgle_block *b,
                               worgle_string *str,
                               size_t linum,
                               worgle_string *filename)
{
    worgle_block_append_segment(b, str, SEGTYPE_TEXT, linum, filename);
}

worgle.org:1456

<<functions_42>>=

void worgle_block_append_reference(worgle_block *b,
                                  worgle_string *str,
                                  size_t linum,
                                  worgle_string *filename)
{
    worgle_block_append_segment(b, str, SEGTYPE_REFERENCE, linum, filename);
}

worgle.org:1489

<<functions_43>>=

int worgle_block_write(worgle_block *b, worgle_hashmap *h, FILE *fp)
{
    worgle_segment *s;
    int n;
    s = b->head;
    b->am_i_used = 1;
    for(n = 0; n < b->nsegs; n++) {
        if(!worgle_segment_write(s, h, fp)) return 0;
        s = s->nxt;
    }
    return 1;
}

worgle.org:1527

<<functions_44>>=

void worgle_blocklist_init(worgle_blocklist *lst)
{
    lst->head = NULL;
    lst->tail = NULL;
    lst->nblocks = 0;
}

worgle.org:1546

<<functions_45>>=

void worgle_blocklist_free(worgle_blocklist *lst)
{
    worgle_block *b;
    worgle_block *nxt;
    int n;
    b = lst->head;
    for(n = 0; n < lst->nblocks; n++) {
        nxt = b->nxt;
        worgle_block_free(b);
        free(b);
        b = nxt;
    }
}

worgle.org:1572

<<functions_46>>=

void worgle_blocklist_append(worgle_blocklist *lst, worgle_block *b)
{
    if(lst->nblocks == 0) {
        lst->head = b;
        lst->tail = b;
    }
    lst->tail->nxt = b;
    lst->tail = b;
    lst->nblocks++;
}

worgle.org:1608

<<functions_47>>=

void worgle_hashmap_init(worgle_hashmap *h)
{
    int n;
    h->nwords = 0;
    for(n = 0; n < HASH_SIZE; n++) {
        worgle_blocklist_init(&h->blk[n]);
    }
}

worgle.org:1631

<<functions_48>>=

void worgle_hashmap_free(worgle_hashmap *h)
{
    int n;
    for(n = 0; n < HASH_SIZE; n++) {
        worgle_blocklist_free(&h->blk[n]);
    }
}

1.6.11 hashmap_hasher

worgle.org:1682

<<hashmap_hasher_0>>=

static int hash(const char *str, size_t size)
{
    unsigned int h = 5381;
    size_t i = 0;
    for(i = 0; i < size; i++) {
        h = ((h << 5) + h) ^ str[i];
        h %= 0x7FFFFFFF;
    }
    return h % HASH_SIZE;
}

worgle.org:1654

<<functions_51>>=

int worgle_hashmap_find(worgle_hashmap *h, worgle_string *name, worgle_block **b)
{
    int pos;
    worgle_blocklist *lst;
    int n;
    worgle_block *blk;
    pos = hash(name->str, name->size);
    lst = &h->blk[pos];
    blk = lst->head;
    for(n = 0; n < lst->nblocks; n++) {
        if(name->size == blk->name.size) {
            if(!strncmp(name->str, blk->name.str, name->size)) {
                *b = blk;
                return 1;
            }
        }
        blk = blk->nxt;
    }
    return 0;
}

worgle.org:1708

<<functions_52>>=

worgle_block * worgle_hashmap_get(worgle_hashmap *h, worgle_string *name)
{
    worgle_block *b;
    worgle_blocklist *lst;
    int pos;
    if(worgle_hashmap_find(h, name, &b)) return b;
    pos = hash(name->str, name->size);
    b = NULL;
    b = malloc(sizeof(worgle_block));
    worgle_block_init(b);
    b->name = *name;
    lst = &h->blk[pos];
    worgle_blocklist_append(lst, b);
    return b;
}

worgle.org:1757

<<functions_53>>=

int worgle_file_write(worgle_file *f, worgle_hashmap *h)
{
    FILE *fp;
    char tmp[128];
    size_t n;
    size_t size;
    int rc;
    if(f->filename.size > 128) size = 127;
    else size = f->filename.size;
    for(n = 0; n < size; n++) tmp[n] = f->filename.str[n];
    tmp[size] = 0;
    fp = fopen(tmp, "w");
    rc = worgle_block_write(f->top, h, fp);
    fclose(fp);
    return rc;
}

worgle.org:1802

<<functions_54>>=

void worgle_filelist_init(worgle_filelist *flist)
{
    flist->head = NULL;
    flist->tail = NULL;
    flist->nfiles = 0;
}

worgle.org:1821

<<functions_55>>=

void worgle_filelist_free(worgle_filelist *flist)
{
    worgle_file *f;
    worgle_file *nxt;
    int n;
    f = flist->head;
    for(n = 0; n < flist->nfiles; n++) {
        nxt = f->nxt;
        free(f);
        f = nxt;
    }
}

worgle.org:1848

<<functions_56>>=

void worgle_filelist_append(worgle_filelist *flist,
                           worgle_string *name,
                           worgle_block *top)
{
    worgle_file *f;
    f = malloc(sizeof(worgle_file));
    f->filename = *name;
    f->top = top;
    if(flist->nfiles == 0) {
        flist->head = f;
        flist->tail = f;
    }
    flist->tail->nxt = f;
    flist->tail = f;
    flist->nfiles++;
}

worgle.org:1879

<<functions_57>>=

int worgle_filelist_write(worgle_filelist *flist, worgle_hashmap *h)
{
    worgle_file *f;
    int n;
    f = flist->head;
    for(n = 0; n < flist->nfiles; n++) {
        if(!worgle_file_write(f, h)) return 0;
        f = f->nxt;
    }
    return 1;
}

worgle.org:1916

<<functions_58>>=

void worgle_textbuf_zero(worgle_textbuf *txt)
{
    txt->buf = NULL;
    worgle_string_init(&txt->filename);
    txt->size = 0;
}

worgle.org:1934

<<functions_59>>=

void worgle_textbuf_init(worgle_textbuf *txt,
                         char *buf,
                         size_t bufsize)
{
    txt->buf = buf;
    txt->size = bufsize;
}

worgle.org:1956

<<functions_60>>=

void worgle_textbuf_free(worgle_textbuf *txt)
{
    if(txt->buf != NULL) free(txt->buf);
    worgle_textbuf_zero(txt);
}

worgle.org:2054

<<functions_61>>=

int worgle_warn_unused(worgle_d *worg)
{
    worgle_hashmap *dict;
    worgle_block *blk;
    worgle_blocklist *lst;
    int n;
    int b;
    int rc;
    dict = &worg->dict;
    rc = 0;
    for(n = 0; n < HASH_SIZE; n++) {
        lst = &dict->blk[n];
        blk = lst->head;
        for(b = 0; b < lst->nblocks; b++) {
            if(blk->am_i_used == 0) {
                fprintf(stderr, "Warning: block '");
                worgle_string_write(stderr, &blk->name);
                fprintf(stderr, "' unused.\n");
                if(use_warnings == 2) rc = 1;
            }
            blk = blk->nxt;
        }
    }
    return rc;
}

worgle.org:2113

<<functions_62>>=

static void append_filename(worgle_d *worg, char *filename)
{
    worgle_string *str;
    int pos;
    pos = worg->nbuffers;
    worg->nbuffers++;
    if(worg->nbuffers == 1) {
        worg->buffers = calloc(1, sizeof(worgle_textbuf));
    } else {
        worg->buffers = realloc(worg->buffers,
                                sizeof(worgle_textbuf) * worg->nbuffers);
    }
    str = &worg->buffers[pos].filename;
    str->str = filename;
    str->size = strlen(filename);
}

worgle.org:2154

<<functions_63>>=

void worgle_map(worgle_d *worg, worgle_block *b, int lvl, FILE *out)
{
    int i;
    worgle_segment *s;
    worgle_block *newblk;
    worgle_hashmap *h;
    h = &worg->dict;
    if(lvl != 0) {
        for(i = 0; i <= lvl; i++) {
            fputc('*', out);
        }
        fputc(' ', out);
        worgle_string_write(out, &b->name);
        fputc('\n', out);
    }
    s = b->head;
    newblk = NULL;
    for(i = 0; i < b->nsegs; i++) {
        if(s->type == SEGTYPE_TEXT) {
            if(s->str.size > 0) {
                worgle_string_write(out, s->filename);
                fprintf(out, ":%zu\n", s->linum);
                fprintf(out, "#+NAME: ");
                worgle_string_write(out, &b->name);
                fprintf(out, "_%d\n", i);
                fprintf(out, "#+BEGIN_SRC\n");
                worgle_string_write(out, &s->str);
                fprintf(out, "#+END_SRC");
            }
            fprintf(out, "\n");
        } else if(worgle_hashmap_find(h, &s->str, &newblk)) {
            worgle_map(worg, newblk, lvl + 1, out);
        }
        s = s->nxt;
    }
}

worgle.org:2204

<<functions_64>>=

void worgle_map_files(worgle_d *worg, char *filename)
{
    int n;
    worgle_file *f;
    FILE *fp;
    fp = fopen(filename, "w");
    if(fp == NULL) return;
    f = worg->flist.head;
    fprintf(fp, "#+TITLE: Code Map\n");
    for(n = 0; n < worg->flist.nfiles; n++) {
        worgle_map_a_file(worg, f, fp);
        f = f->nxt;
    }
    fclose(fp);
}
void worgle_map_a_file(worgle_d *worg, worgle_file *file, FILE *out)
{
    fprintf(out, "* ");
    worgle_string_write(out, &file->filename);
    fprintf(out, "\n");
    worgle_map(worg, file->top, 0, out);
}

worgle.org:55

<<worgle-top_12>>=

int main(int argc, char *argv[])
{

1.7 local_variables

worgle.org:81

<<local_variables_0>>=

worgle_d worg;

worgle.org:96

<<local_variables_1>>=

char *filename;

worgle.org:127

<<local_variables_2>>=

int rc;

worgle.org:251

<<local_variables_3>>=

int i;

worgle.org:1971

<<local_variables_4>>=

struct parg_state ps;
int c;

1.8 initialization

worgle.org:86

<<initialization_0>>=

worgle_init(&worg);

worgle.org:101

<<initialization_1>>=

filename = NULL;
if(argc < 2) {
    fprintf(stderr, "Usage: %s filename.org\n", argv[0]);
    return 1;
}

1.8.1 parse_cli_args

worgle.org:1977

<<parse_cli_args_0>>=

parg_init(&ps);
while((c = parg_getopt(&ps, argc, argv, "gW:m:")) != -1) {
    switch(c) {
        case 1:
            filename = (char *)ps.optarg;

1.8.1.1 append_filename

worgle.org:2101

<<append_filename_0>>=

append_filename(&worg, (char *)ps.optarg);

worgle.org:1983

<<parse_cli_args_2>>=

            break;
        case 'g':

1.8.1.2 turn_on_debug_macros

worgle.org:2008

<<turn_on_debug_macros_0>>=

use_debug = 1;

worgle.org:1986

<<parse_cli_args_4>>=

            break;
        case 'W':

1.8.1.3 turn_on_warnings

worgle.org:2031

<<turn_on_warnings_0>>=

if(!strncmp(ps.optarg, "soft", 4)) {
    use_warnings = 1;
} else if(!strncmp(ps.optarg, "error", 5)) {
    use_warnings = 2;
} else {
    fprintf(stderr, "Unidentified warning mode '%s'\n", ps.optarg);
    return 1;
}

worgle.org:1989

<<parse_cli_args_6>>=

            break;
        case 'm':

1.8.1.4 map_source_code

worgle.org:2094

<<map_source_code_0>>=

map_source_code = 1;
map_filename = (char *)ps.optarg;

worgle.org:1992

<<parse_cli_args_8>>=

            break;
        default:
            fprintf(stderr, "Unknown option -%c\n", c);
            return 1;
    }
}

1.8.2 check_filename

worgle.org:115

<<check_filename_0>>=

if(filename == NULL) {
    fprintf(stderr, "No filename specified\n");
    return 1;
}

worgle.org:134

<<initialization_6>>=

rc = 0;

1.9 loading

worgle.org:151

<<loading_0>>=

for(i = 0; i < worg.nbuffers; i++) {
    rc = loadfile(&worg, i);
    if(!rc) goto cleanup;
}

1.10 parsing

worgle.org:256

<<parsing_0>>=

for(i = 0; i < worg.nbuffers; i++) {
    rc = parse_file(&worg, i);
    if(rc) goto cleanup;
}

1.11 generation

worgle.org:859

<<generation_0>>=

if(!rc) if(!worgle_generate(&worg)) rc = 1;

worgle.org:880

<<generation_1>>=

if(use_warnings) rc = worgle_warn_unused(&worg);

1.12 mapping

worgle.org:2136

<<mapping_0>>=

if(map_source_code && map_filename != NULL) {
    worgle_map_files(&worg, map_filename);
}

1.13 cleanup

worgle.org:887

<<cleanup_0>>=

cleanup:
worgle_free(&worg);
return rc;

worgle.org:65

<<worgle-top_26>>=

}