Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic win support #67

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/lib/fy-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

#include <stdio.h>
#include <string.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably group all these together

#include <sys/mman.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <sys/ioctl.h>
#endif
#include <fcntl.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/fy-token.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static int fy_tag_token_format_internal(const struct fy_token *fyt, void *out, s
if (out) {
outsz = *outszp;
o = out;
oe = out + outsz;
oe = (char *)out + outsz;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get this why it needs to happen but I still dislike MSVC here.

}

if (!fyt->tag.fyt_td)
Expand Down Expand Up @@ -246,7 +246,7 @@ static int fy_tag_directive_token_format_internal(const struct fy_token *fyt,
if (out) {
outsz = *outszp;
o = out;
oe = out + outsz;
oe = (char *)out + outsz;
}

#define O_CPY(_src, _len) \
Expand Down
2 changes: 2 additions & 0 deletions src/lib/fy-types.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

#include <stdio.h>
#include <string.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
Expand Down
6 changes: 3 additions & 3 deletions src/lib/fy-utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ const void *fy_utf8_memchr_generic(const void *s, int c, size_t n)
int cc, w;
const void *e;

e = s + n;
while (s < e && (cc = fy_utf8_get(s, e - s, &w)) >= 0) {
e = (char*)s + n;
while (s < e && (cc = fy_utf8_get(s, (char*)e - (char*)s, &w)) >= 0) {
if (c == cc)
return s;
s += w;
s = (char*)s + w;
}

return NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/fy-utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int fy_utf8_get_right_generic(const void *ptr, int left, int *widthp);

static inline int fy_utf8_get_right(const void *ptr, int left, int *widthp)
{
const uint8_t *p = ptr + left;
const uint8_t *p = (const uint8_t*)ptr + left;

/* single byte (hot path) */
if (left > 0 && !(p[-1] & 0x80)) {
Expand Down Expand Up @@ -194,7 +194,7 @@ static inline const void *fy_utf8_strchr(const void *s, int c)

static inline int fy_utf8_count(const void *ptr, size_t len)
{
const uint8_t *s = ptr, *e = ptr + len;
const uint8_t *s = ptr, *e = (const uint8_t *)ptr + len;
int w, count;

count = 0;
Expand Down
87 changes: 87 additions & 0 deletions src/lib/fy-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,101 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#elif defined(_MSC_VER)
#include <windows.h>
#endif

#include "fy-utf8.h"
#include "fy-ctype.h"
#include "fy-utils.h"

int fy_get_pagesize() {
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
return sysconf(_SC_PAGESIZE);
#elif defined (_MSC_VER)
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwPageSize;
#endif
}

#if defined(_MSC_VER)
#ifndef VA_COPY
# ifdef HAVE_VA_COPY
# define VA_COPY(dest, src) va_copy(dest, src)
# else
# ifdef HAVE___VA_COPY
# define VA_COPY(dest, src) __va_copy(dest, src)
# else
# define VA_COPY(dest, src) (dest) = (src)
# endif
# endif
#endif

#define INIT_SZ 128

int
vasprintf(char **str, const char *fmt, va_list ap)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you're over doing it here.

IIRC vsnprintf with a NULL string returns the exact size, so you can just call it twice, first call to find the size, second call after allocating it.

{
int ret;
va_list ap2;
char *string, *newstr;
size_t len;

if ((string = malloc(INIT_SZ)) == NULL)
goto fail;

VA_COPY(ap2, ap);
ret = vsnprintf(string, INIT_SZ, fmt, ap2);
va_end(ap2);
if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
*str = string;
} else if (ret == INT_MAX || ret < 0) { /* Bad length */
free(string);
goto fail;
} else { /* bigger than initial, realloc allowing for nul */
len = (size_t)ret + 1;
if ((newstr = realloc(string, len)) == NULL) {
free(string);
goto fail;
}
VA_COPY(ap2, ap);
ret = vsnprintf(newstr, len, fmt, ap2);
va_end(ap2);
if (ret < 0 || (size_t)ret >= len) { /* failed with realloc'ed string */
free(newstr);
goto fail;
}
*str = newstr;
}
return (ret);

fail:
*str = NULL;
errno = ENOMEM;
return (-1);
}

int asprintf(char **str, const char *fmt, ...)
{
va_list ap;
int ret;

*str = NULL;
va_start(ap, fmt);
ret = vasprintf(str, fmt, ap);
va_end(ap);

return ret;
}
#endif

#if defined(__APPLE__) && (_POSIX_C_SOURCE < 200809L)

/*
Expand Down Expand Up @@ -397,6 +482,7 @@ int fy_tag_scan(const char *data, size_t len, struct fy_tag_scan_info *info)
return 0;
}

#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
/* simple terminal methods; mainly for getting size of terminal */
int fy_term_set_raw(int fd, struct termios *oldt)
{
Expand Down Expand Up @@ -632,3 +718,4 @@ int fy_term_query_size(int fd, int *rows, int *cols)

return ret;
}
#endif
9 changes: 9 additions & 0 deletions src/lib/fy-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@

#include <stdio.h>
#include <stdbool.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#include <termios.h>
#endif

#if defined(__APPLE__) && (_POSIX_C_SOURCE < 200809L)
FILE *open_memstream(char **ptr, size_t *sizeloc);
#endif

int fy_get_pagesize();

#if defined(_MSC_VER)
int vasprintf(char **strp, const char *fmt, va_list ap);
int asprintf(char **strp, const char *fmt, ...);
#endif

int fy_tag_handle_length(const char *data, size_t len);
bool fy_tag_uri_is_valid(const char *data, size_t len);
int fy_tag_uri_length(const char *data, size_t len);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/fy-walk.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif
#include <math.h>
#include <limits.h>

Expand Down