5. Command Line Arguments

This section outlines command line arguments in Worgle.

5.1. Parsing command line flags

Command line argument parsing is done using the third-party library parg, included in this source distribution.

<<local_variables>>=
struct parg_state ps;
int c;
<<parse_cli_args>>=
parg_init(&ps);
while ((c = parg_getopt(&ps, argc, argv, "gW:m:d:p:n")) != -1) {
    switch(c) {
        case 1:
            filename = (char *)ps.optarg;
<<append_filename>>
            break;
        case 'g':
<<turn_on_debug_macros>>
            break;
        case 'W':
<<turn_on_warnings>>
            break;
        case 'm':
<<map_source_code>>
            break;
        case 'd':
#ifndef WORGLITE
<<generate_database>>
#else
            fprintf(stderr, "Database flag (-d) is  not enabled\n");
            return 1;
#endif
            break;
        case 'p':
<<set_program_id>>
            break;
        case 'n':
<<disable_tangling>>
            break;
        default:
            fprintf(stderr, "Unknown option -%c\n", c);
            return 1;
    }
}

5.2. Turning on debug macros (-g)

Worgle has the ability to generate debug macros when generating C files.

This will turn on a boolean flag called use_debug inside the worgle struct.

<<turn_on_debug_macros>>=
use_debug = 1;

By default, use_debug is set to be false in order to allow other non-C languages to be used.

<<global_variables>>=
static int use_debug = 0;

5.3. Turning on Warnings (-W)

Worgle can print out warnings about things like unused sections of code. By default, this is turned off.

There are right now two options for warnings. "soft" will print warnings. "error" will print warnings as errors and break out of the program.

<<global_variables>>=
static int use_warnings = 0;
<<turn_on_warnings>>=
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;
}

5.3.1. Checking for unused blocks

One thing that warnings can do is check for unused blocks. This is done after the files are generated with the function worgle_warn_unused.

<<function_declarations>>=
int worgle_warn_unused(worgle_d *worg);
<<functions>>=
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");
                fprintf(stderr, "First declared in ");
                worgle_string_write(stderr,
                                    blk->head->filename);
                fprintf(stderr, ", line %lu\n",
                        blk->head->linum);
                if(use_warnings == 2) rc = 1;
            }
            blk = blk->nxt;
        }
    }
    return rc;
}

5.4. Map Source Code (-m)

This flag will turn on source code mappings. It will dump the source code map to a specified file. The filename is stored in a global variable.

<<global_variables>>=
static int map_source_code = 0;
static char *map_filename = NULL;
<<map_source_code>>=
map_source_code = 1;
map_filename = (char *)ps.optarg;

5.5. Appending filenames

<<append_filename>>=
append_filename(&worg, (char *)ps.optarg);
<<static_function_declarations>>=
static void append_filename(worgle_d *worg, char *filename);

For now, just append the file name to the first item on the list.

<<functions>>=
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);
#ifndef WORGLITE
<<append_org_file>>
#endif
}

5.6. Generate Database (-d)

This flag will turn on database generation, assuming that it is enabled.

<<global_variables>>=
#ifndef WORGLITE
static int generate_db = 0;
static char *db_filename = NULL;
#endif
<<generate_database>>=
generate_db = 1;
db_filename = (char *)ps.optarg;
<<static_function_declarations>>=
#ifndef WORGLITE
static void tangle_to_db(worgle_d *worg);
#endif
<<functions>>=
#ifndef WORGLITE
static void tangle_to_db(worgle_d *worg)
{
    sqlite3 *db;
    int rc;

    if(db_filename == NULL) return;

    rc = sqlite3_open(db_filename, &db);
    if(rc) {
        fprintf(stderr,
                "Could not open database: %s",
                sqlite3_errmsg(db));
        sqlite3_close(db);
        return;
    }
    worgle_db_clear(db, worg->prog);
    worgle_db_schemas(worg, db);
    worgle_db_generate(worg, db);
    sqlite3_close(db);
}
#endif
<<database>>=
#ifndef WORGLITE
if(generate_db) tangle_to_db(&worg);
#endif

5.7. Set program id (-p)

The p flag is used to set the program ID, which is then written to the database.

<<set_program_id>>=
worg.prog = atoi(ps.optarg);

5.8. Disable tangling (-n)

This flag will disable any tangling. This is useful with the -d flag for only generating the SQLite database.

<<global_variables>>=
static int tangle_code = 1;
<<disable_tangling>>=
tangle_code = 0;



prev | home | next