Skip to content
This repository has been archived by the owner on Oct 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #30 from localytics/refactor
Browse files Browse the repository at this point in the history
Allow NOT actions
  • Loading branch information
kddnewton authored Jun 2, 2017
2 parents 638040b + af5f8e7 commit c6afeaa
Show file tree
Hide file tree
Showing 18 changed files with 358 additions and 206 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ON
You can link against `libcogito` after it has been installed by including `cogito.h` in your C program. This will give you a `buffer` struct:

```c
typedef struct cg_buf {
typedef struct {
size_t length;
size_t capacity;
char *content;
Expand All @@ -86,8 +86,8 @@ typedef struct cg_buf {
and two functions: `cg_to_json` and `cg_to_iam`. The call signature for these functions is:

```c
int cg_to_json(cg_buf_t *buffer, char *input);
int cg_to_iam(cg_buf_t *buffer, char *input);
int cg_to_json(cg_buf_t *buffer, char *str);
int cg_to_iam(cg_buf_t *buffer, char *str);
```
where the return value will be a 0 in the case of success and an error code otherwise.
Expand Down
5 changes: 4 additions & 1 deletion amazon/libcogito.spec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: libcogito
Version: 0.1.1
Version: 0.2.0
Release: 1
Summary: Define your AWS IAM policies using an easy-to-read format.
License: MIT
Expand All @@ -10,6 +10,9 @@ Source0: %{name}-%{version}.tar.gz
Define your AWS IAM policies using an easy-to-read format.

%changelog
* Fri Jun 02 2017 Localytics <oss@localytics.com> - 0.2.0-1
- Allow "NotAction" and "NotResource" keys.

* Mon May 15 2017 Localytics <oss@localytics.com> - 0.1.1-1
- Fix issue when the "Action" or "Resource" keys are not provided.

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([cogito], [0.1.1], [oss@localytics.com])
AC_INIT([cogito], [0.2.0], [oss@localytics.com])

AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
libcogito (0.2.0-1) unstable; urgency=low

* Allow "NotAction" and "NotResource" keys.

-- Localytics <oss@localytics.com> Fri, 02 Jun 2017 00:22:47 +0000

libcogito (0.1.1-1) unstable; urgency=low

* Fix issue when the "Action" or "Resource" keys are not provided.
Expand Down
27 changes: 24 additions & 3 deletions src/buffer.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
#ifndef COGITO_BUF
#define COGITO_BUF
#ifndef COGITO_BUFFER
#define COGITO_BUFFER

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct cg_buf {
/**
* A struct representing an expandable string. Contains the current length of
* the string, the current capacity of the string, and a pointer to the string
* itself.
*/
typedef struct {
size_t length;
size_t capacity;
char *content;
} cg_buf_t;

/**
* Initialize and return a new cg_buf_t struct.
* @return A newly allocated and initialized buffer struct
*/
cg_buf_t* cg_buf_build(void);

/**
* Copy a given string onto the given buffer, expanding if necessary.
* @param buffer The buffer on which to append
* @param str The string to append onto the buffer
* @return An integer describing success (0 for success, 1 for failure)
*/
int cg_buf_append(cg_buf_t *buffer, const char *str);

/**
* Free the allocated buffer and its associated string.
* @param buffer The buffer to free
*/
void cg_buf_free(cg_buf_t *buffer);

#endif
13 changes: 13 additions & 0 deletions src/cogito.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@
#include "cogito/statement.h"
#include "cogito/parser.h"

/**
* Convert the given input string to JSON and copy it onto the given buffer.
* @param buffer The buffer on which to copy the converted JSON
* @param input The input string in IAM format
* @return An integer describing success (0 for success, error code for failure)
*/
int cg_to_json(cg_buf_t *buffer, char *input);

/**
* Convert the given input string to IAM and copy it onto the given buffer.
* @param buffer The buffer on which to copy the converted JSON
* @param input The input string in JSON format
* @return An integer describing success (0 for success, error code for failure)
*/
int cg_to_iam(cg_buf_t *buffer, char *input);

#endif
2 changes: 2 additions & 0 deletions src/cogito.l
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ allow (?i:allow)
deny (?i:deny)
macro {allow}|{deny}
on (?i:on)
not (?i:not)
item [A-Za-z0-9:/\*\.\-\$\{\}_]+

%option noinput
Expand All @@ -21,6 +22,7 @@ item [A-Za-z0-9:/\*\.\-\$\{\}_]+
{white} {}
{macro} { yylval.str = strdup(yytext); return MACRO; }
{on} { yylval.str = "ON"; return ON; }
{not} { yylval.str = "NOT"; return NOT; }
{item} { yylval.str = strdup(yytext); return ITEM; }

";" return END;
Expand Down
75 changes: 31 additions & 44 deletions src/linked_list.c
Original file line number Diff line number Diff line change
@@ -1,73 +1,60 @@
#include "linked_list.h"

// Append a node to the end of the list
void cg_ll_append(cg_node_t *head, char *val) {
cg_node_t *tail = cg_ll_build(val);
cg_node_t *ptr = head;
static cg_node_t* cg_ll_build_node(char *value) {
cg_node_t *node = (cg_node_t *) malloc(sizeof(cg_node_t));
node->value = value;
node->next = NULL;
return node;
}

void cg_ll_append(cg_list_t *list, char *value) {
cg_node_t *tail = cg_ll_build_node(value);
cg_node_t *ptr = list->head;

while(ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = tail;
}

// Build the first node or append a node to the list
cg_node_t* cg_ll_update(cg_node_t *head, char *val) {
if (head == NULL) {
return cg_ll_build(val);
cg_list_t* cg_ll_update(cg_list_t *list, char *value) {
if (list == NULL) {
return cg_ll_build(value);
}
cg_ll_append(head, val);
return head;
cg_ll_append(list, value);
return list;
}

// Build a list node
cg_node_t* cg_ll_build(char *val) {
cg_node_t *node = (cg_node_t*) malloc(sizeof(cg_node_t));
node->val = val;
node->next = NULL;
return node;
}

// Print out the list starting at the given node
void cg_ll_print(cg_node_t *node) {
cg_node_t *ptr;

cg_ll_foreach(node, ptr) {
printf("[%s]\n", ptr->val);
}
printf("\n");
cg_list_t* cg_ll_build(char *value) {
cg_list_t *list = (cg_list_t *) malloc(sizeof(cg_list_t));
list->negated = 0;
list->head = cg_ll_build_node(value);
return list;
}

// The number of nodes in the list
int cg_ll_size(cg_node_t *head) {
int size = 0;
size_t cg_ll_value_size_sum(cg_list_t *list) {
size_t size = 0;
cg_node_t *ptr;

cg_ll_foreach(head, ptr) {
size += 1;
cg_ll_foreach(list, ptr) {
size += strlen(ptr->value);
}
return size;
}

// The sum of the size of all of the values in the list
size_t cg_ll_val_size_sum(cg_node_t *head) {
size_t size = 0;
cg_node_t *ptr;

cg_ll_foreach(head, ptr) {
size += strlen(ptr->val);
}
return size;
void cg_ll_negate(cg_list_t *list) {
list->negated = 1 - list->negated;
}

// Free the memory for the entire list
void cg_ll_free(cg_node_t *head) {
cg_node_t *previous = head;
cg_node_t *current = head;
void cg_ll_free(cg_list_t *list) {
cg_node_t *previous = list->head;
cg_node_t *current = list->head;

while(current != NULL) {
previous = current;
current = current->next;
free(previous);
}

free(list);
}
73 changes: 61 additions & 12 deletions src/linked_list.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
#ifndef COGITO_LL
#define COGITO_LL
#ifndef COGITO_LINKED_LIST
#define COGITO_LINKED_LIST

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
* A struct representing a node in the list. Contains a pointer to a string
* representing the AWS identifier and a pointer to the next node.
*/
typedef struct cg_node {
char *val;
char *value;
struct cg_node *next;
} cg_node_t;

#define cg_ll_foreach(head, ptr) \
for ((ptr) = head; (ptr) != NULL; (ptr) = (ptr)->next)
/**
* A struct representing the list. Contains a pointer to the head node of the
* list and an integer flag representing whether or not this list is negated.
*/
typedef struct {
struct cg_node *head;
int negated;
} cg_list_t;

void cg_ll_append(cg_node_t *head, char *val);
cg_node_t* cg_ll_update(cg_node_t *head, char *val);
cg_node_t* cg_ll_build(char *val);
void cg_ll_print(cg_node_t *node);
int cg_ll_size(cg_node_t *head);
size_t cg_ll_val_size_sum(cg_node_t *head);
void cg_ll_free(cg_node_t *head);
/**
* A macro for looping through the nodes in the list.
*/
#define cg_ll_foreach(list, ptr) \
for ((ptr) = list->head; (ptr) != NULL; (ptr) = (ptr)->next)

/**
* Append a node to the end of the list.
* @param list The list on which to append the new value
* @param value The new value that should be appended to the list
*/
void cg_ll_append(cg_list_t *list, char *value);

/**
* Build the first node or append a node to the list.
* @param list The list that should be updated (or created if NULL)
* @param value The new value that should be appended to the list
* @return The newly created or updated list
*/
cg_list_t* cg_ll_update(cg_list_t *list, char *value);

/**
* Build the first list node and return the list.
* @param value The value to be used to create the first node
* @return The newly created list
*/
cg_list_t* cg_ll_build(char *value);

/**
* The sum of the size of all of the values in the list.
* @param list The list to with which to compute the sum
* @return The size_t representing the sum
*/
size_t cg_ll_value_size_sum(cg_list_t *list);

/**
* Negate the given list.
* @param list The list on which to flip the negated flag
*/
void cg_ll_negate(cg_list_t *list);

/**
* Free the memory for the entire list.
* @param list The list to free
*/
void cg_ll_free(cg_list_t *list);

#endif
Loading

0 comments on commit c6afeaa

Please sign in to comment.