aboutsummaryrefslogtreecommitdiff
path: root/src/engine.c
blob: 5f4c51a1949baa99bb55348e9bf236e8804e359f (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#define _GNU_SOURCE

#include <copy.h>
#include <engine.h>
#include <filehandler.h>
#include <lexer.h>
#include <list.h>
#include <main.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern msg_t *msg;

void
handle_include(char **buffer, key_match_t *match, directive_t *directive)
{
  char *operand = directive->operands;
  char *partial_path;
  asprintf(&partial_path, "%s/%s/%s", msg->base_directory, PARTIALS, operand);

  FILE *f = fopen(partial_path, "r");
  if (f == NULL) {
    printf("Could not open: %s\n", partial_path);
    return;
  }

  unsigned int size = fsize(f);
  char *partial_content = fcontent(f, size);

  char *temp_buffer = strdup(*buffer);

  free(*buffer);
  asprintf(buffer,
           "%.*s%s%s\n",
           match->offset,
           temp_buffer,
           partial_content,
           temp_buffer + match->offset + match->length);

  free(temp_buffer);
}

void
handle_contentfor(char **buffer,
                  key_match_t *match,
                  directive_t *directive,
                  list_t *content_headers)
{
  contentfor_operand_t *operand = directive->operands;
  list_add(content_headers, operand);

#ifdef DEBUG
  printf("CONTENTFOR: %s\n", operand->key);
  printf("CONTENT: %s\n", operand->content);
#endif

  char *temp_buffer = strdup(*buffer);

  free(*buffer);
  asprintf(buffer,
           "%.*s%s",
           match->offset,
           temp_buffer,
           temp_buffer + operand->length);

  free(temp_buffer);
  free(operand);
}

list_t *
ingest(char **buffer)
{
  key_match_t *match;
  list_t *content_headers = list_create(sizeof(contentfor_operand_t));
  if (content_headers == NULL) {
    printf("Could not create content_headers\n");
    return NULL;
  }

  size_t skip = 0;
  while (true) {
    match = find_next_key(*buffer, skip);
    if (match == NULL)
      break;

#ifdef DEBUG
    printf("Match: %.*s LENGTH(%d) OFFSET(%d)\n",
           match->length,
           *buffer + match->offset,
           match->length,
           match->offset);
#endif

    directive_t *directive = find_directive(*buffer, match);
    if (directive == NULL) {
      printf(
          "Unknown directive: %.*s\n", match->length, *buffer + match->offset);

      break;
    }

    switch (directive->type) {
    case INCLUDE:
      handle_include(buffer, match, directive);
      break;
    case CONTENTFOR:
      handle_contentfor(buffer, match, directive, content_headers);
      break;

    case BODY:
    case CONTENT:
    case ENDCONTENT:
    case _RAW:
      skip++;
      break;
    }

    if (directive != NULL)
      free(directive);

    if (match != NULL)
      free(match);
  }

  return content_headers;
}