aboutsummaryrefslogtreecommitdiff
path: root/src/context.c
blob: c8f36e1d708e9f2fda187109cea717519c8918e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h>
#include <string.h>

#include "env.h"
#include "parser.h"
#include "context.h"

struct context_t context;

void init_context()
{
    context.raw_path = getenv("PATH_INFO");
    if (!context.raw_path || !strlen(context.raw_path))
        context.raw_path = getenv_default("SCRIPT_NAME", "/");

    context.path = NULL;
    context.path_length = 0;

    context.project = NULL;
    context.action = NULL;
    context.index = NULL;
    context.extra = NULL;

    context.token = NULL;

    context.debug = 0;

    parse_path(context.raw_path);
    parse_query(getenv_default("QUERY_STRING", ""));

    for (size_t i = 0; i < context.path_length; ++i)
    {
        switch(i)
        {
            case 0:
                context.project = context.path[i];
                break;

            case 1:
                context.action = context.path[i];
                break;

            case 2:
                context.index = context.path[i];
                break;

            case 3:
                context.extra = context.path[i];
                break;

            default:
                break;
        }
    }
}

void deinit_context()
{
    if (context.path)
    {
        for (size_t i = 0; i < context.path_length; ++i)
        {
            free(context.path[i]);
        }

        free(context.path);
        free(context.token);
    }
}