Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ tests/running_report
.DS_Store
._*
.vscode/*
.vs/*
26 changes: 26 additions & 0 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "config.h"
#include "output.h"
#include "plugins.h"
#include "error_handling.h"

#define UNUSED(x) (void)(sizeof((x)))

Expand Down Expand Up @@ -85,3 +86,28 @@ void *calloc_s(size_t nmemb, size_t size);
plugins_unload_all(); \
pev_cleanup_config(config); \
} while (0)

static inline char* pev_strdup(const char* str)
{
const size_t len = strlen(str);
char* dest = malloc(len + 1);

if (!dest)
{
PEV_FATAL("it was not possible to allocate %zu bytes in the heap, "
"reason: memory exhausted", len + 1);
}

return memcpy(dest, str, len + 1);
}

static inline void pev_fclose(FILE * restrict fptr, bool on_exit)
{
if (fclose(fptr) == EOF)
{
if (on_exit)
PEV_FATAL("Cannot close file handle: %p", (void*)fptr);
else
PEV_WARN("Cannot close file handle: %p", (void*)fptr);
}
}
47 changes: 47 additions & 0 deletions include/error_handling.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef PEV_ERROR_HANDLING_H
#define PEV_ERROR_HANDLING_H

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

#define PEV_WARN_IO_HANDLE stderr
#define PEV_FATAL_IO_HANDLE stderr
#define PEV_INFO_IO_HANDLE stdout

#define PEV_FATAL(...) \
do \
{ \
fprintf(PEV_FATAL_IO_HANDLE, "[FATAL][%s:%d]: ", __FILE__, __LINE__); \
fprintf(PEV_FATAL_IO_HANDLE, __VA_ARGS__); \
fputc('\n', PEV_FATAL_IO_HANDLE); \
exit(EXIT_FAILURE); \
} while (0)

#define PEV_WARN(...) \
do \
{ \
fprintf(PEV_WARN_IO_HANDLE, "[WARNING][%s:%d]: ", __FILE__, __LINE__); \
fprintf(PEV_WARN_IO_HANDLE, __VA_ARGS__); \
fputc('\n', PEV_WARN_IO_HANDLE); \
} while (0)

#define PEV_INFO(...) \
do \
{ \
fprintf(PEV_INFO_IO_HANDLE, "[INFO][%s:%d]: ", __FILE__, __LINE__); \
fprintf(PEV_INFO_IO_HANDLE, __VA_ARGS__); \
fputc('\n', PEV_INFO_IO_HANDLE); \
} while (0)

#define PEV_FATAL_IF(cond, ...) \
do { if ((cond)) PEV_FATAL(__VA_ARGS__); } while (0)

#define GLUE(cond) #cond

#ifndef NDEBUG
#define PEV_ASSERT(cond) if (!(cond)) PEV_FATAL("Assertion \"%s\" failed", GLUE((cond)))
#else
#define PEV_ASSERT(cond)
#endif

#endif
37 changes: 21 additions & 16 deletions include/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#pragma once

#include <stdint.h>
#include "error_handling.h"

#define STACK_PASTE_2_(_1,_2) _1 ## _2
#define STACK_PASTE_2(_1,_2) STACK_PASTE_2_(_1, _2)
Expand Down Expand Up @@ -74,7 +75,7 @@ static int STACK_API(stack_peek)(STACK_TYPE *stack, STACK_ELEMENT_TYPE *element)
STACK_TYPE * STACK_API(stack_alloc)(uint16_t capacity) {
STACK_TYPE *stack = calloc(1, sizeof(STACK_TYPE));
if (stack == NULL) {
fprintf(stderr, "stack: failed to allocate\n");
PEV_WARN("failed to allocate %zu bytes", sizeof(STACK_TYPE));
return NULL;
}

Expand All @@ -90,10 +91,8 @@ STACK_TYPE * STACK_API(stack_alloc)(uint16_t capacity) {
}

void STACK_API(stack_dealloc)(STACK_TYPE *stack) {
assert(stack != NULL);

if (stack == NULL) {
fprintf(stderr, "stack: attempt to deallocate NULL stack\n");
PEV_WARN("attempt to deallocate NULL stack");
return;
}

Expand All @@ -105,16 +104,18 @@ void STACK_API(stack_dealloc)(STACK_TYPE *stack) {
}

uint16_t STACK_API(stack_count)(STACK_TYPE *stack) {
assert(stack != NULL);
PEV_ASSERT(stack != NULL);
return stack->used;
}

int STACK_API(stack_grow)(STACK_TYPE *stack, uint16_t capacity) {
assert(stack != NULL);
assert(capacity > stack->capacity);
PEV_ASSERT(stack != NULL);

// wtf??
//PEV_ASSERT(capacity > stack->capacity);

if (capacity <= stack->capacity) {
fprintf(stderr, "stack: capacity cannot be decreased\n");
PEV_WARN("capacity cannot be decreased");
return -1;
}

Expand All @@ -123,7 +124,7 @@ int STACK_API(stack_grow)(STACK_TYPE *stack, uint16_t capacity) {

STACK_ELEMENT_TYPE *temp = realloc(stack->elements, new_size);
if (temp == NULL) {
fprintf(stderr, "stack: failed to allocate requested capacity\n");
PEV_WARN("failed to allocate requested capacity");
return -2;
}

Expand All @@ -134,26 +135,30 @@ int STACK_API(stack_grow)(STACK_TYPE *stack, uint16_t capacity) {
}

int STACK_API(stack_push)(STACK_TYPE *stack, STACK_ELEMENT_TYPE element) {
assert(stack != NULL);
PEV_ASSERT(stack != NULL);

// Stack is full?
if (stack->used >= stack->capacity) {
// TODO(jweyrich): We could call `stack_grow` instead of failing miserably. Make this behavior adjustable?
fprintf(stderr, "stack: stack is full - failed to push\n");
return -1;
// TODO(garcia): run tests
int retcode = STACK_GROW(stack, stack->capacity << 1);
if (retcode != 0)
{
PEV_WARN("failed to push");
return -1;
}
}

stack->elements[stack->used++] = element;

return 0;
}

int STACK_API(stack_pop)(STACK_TYPE *stack, STACK_ELEMENT_TYPE *element) {
assert(stack != NULL);
PEV_ASSERT(stack != NULL);

// Stack is empty?
if (stack->used == 0) {
fprintf(stderr, "stack: stack is empty - failed to pop\n");
PEV_WARN("stack is empty - failed to pop");
return -1;
}

Expand All @@ -169,7 +174,7 @@ int STACK_API(stack_peek)(STACK_TYPE *stack, STACK_ELEMENT_TYPE *element) {

// Stack is empty?
if (stack->used == 0) {
fprintf(stderr, "stack: stack is empty - failed to peek\n");
PEV_WARN("stack is empty - failed to peek");
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/libpe
Submodule libpe updated from 6b6c86 to 19c4ea
93 changes: 71 additions & 22 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@
files in the program, then also delete it here.
*/

#include "config.h"
#include <libpe/utils.h>
#include <libpe/error.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "config.h"
#include "../include/common.h"

#if defined(__linux__)
#include <linux/limits.h> // FIXME: Why?
#include <linux/limits.h> // FIXME: Why?
#elif defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
#include <limits.h>
#include <limits.h>
#endif

#define DEFAULT_CONFIG_FILENAME "pev.conf"
Expand All @@ -57,11 +59,19 @@
#define DEFAULT_PLUGINS_PATH PLUGINSDIR
#endif

#ifndef PEV_VA_COPY
#if (__STDC_VERSION__ >= 199901L) || defined(_MSC_VER)
#define PEV_VA_COPY(dest, src) va_copy(dest, src)
#else
#define PEV_VA_COPY(dest, src) (dest) = (src)
#endif
#endif

static bool _load_config_cb(pev_config_t * const config, const char *name, const char *value) {
//fprintf(stderr, "DEBUG: %s=%s\n", name, value);

if (!strcmp("plugins_dir", name)) {
config->plugins_path = strdup(value);
config->plugins_path = pev_strdup(value);
return true;
}

Expand All @@ -74,6 +84,12 @@ static int _load_config_and_parse(pev_config_t * const config, const char *path,
if (fp == NULL)
return 0;

if (ferror(fp))
{
pev_fclose(fp, false); // (on_exit = false) -> print only the warning
return 0;
}

char *p, *line = NULL;
size_t size = 0;

Expand Down Expand Up @@ -104,36 +120,67 @@ static int _load_config_and_parse(pev_config_t * const config, const char *path,
size = 0;
}

fclose(fp);

// do we need to exit the program if it is not possible to close the file handle?
// by default, is set to true (print error message and exit)
pev_fclose(fp, true);
return 1;
}

#ifdef USE_MY_ASPRINTF
int asprintf( char **pp, char *fmt, ... )
static int asprintf(char** ppstr, const char* fmt, ...)
{
char *p;
int size;
PEV_ASSERT(ppstr && fmt);

va_list args;
va_list args2;
bool failed = false;

va_start( args, fmt );
va_start(args, fmt);

// Just get the string size.
if ( ( size = vsnprintf( NULL, 0, fmt, args ) ) < 0 )
PEV_VA_COPY(args2, args);
int size = vsnprintf(NULL, 0, fmt, args2);
va_end(args2);

if (size < 0)
{
va_end( args );
va_end(args);
return -1;
}

if ( ! ( p = malloc( size + 1 ) ) )
if (*ppstr)
{
size_t len = strlen(*ppstr);
if (len < (size_t)size)
{
char* temp = (char*) realloc(*ppstr, size + 1);

if (!temp)
{
failed = true;
free(*ppstr);
}
else
*ppstr = temp;
}
}
else
{
va_end( args );
if (!(*ppstr = (char*) malloc(size + 1)))
failed = true;
}

if (failed)
{
errno = ENOMEM;
va_end(args);
return -1;
}
}

vsprintf( *pp = p, fmt, args );
int writted = vsprintf(*ppstr, fmt, args);
PEV_ASSERT(writted == size);

va_end( args );
*(ppstr + size) = 0;
va_end(args);

return size;
}
Expand All @@ -155,19 +202,21 @@ int pev_load_config(pev_config_t * const config) {

ret = pe_utils_is_file_readable(buff);
if (ret == LIBPE_E_OK)
if ( ! _load_config_and_parse(config, buff, _load_config_cb) )
{
if (!_load_config_and_parse(config, buff, _load_config_cb))
{
free( buff );
free(buff);
return -1;
}
}

free( buff );
free(buff);

//
// Default values
//
if (config->plugins_path == NULL)
config->plugins_path = strdup(DEFAULT_PLUGINS_PATH);
config->plugins_path = pev_strdup(DEFAULT_PLUGINS_PATH);

return 0;
}
Expand Down
Loading