Skip to content

Commit

Permalink
Compiles under Windows, does not work under Windows though
Browse files Browse the repository at this point in the history
  • Loading branch information
howerj committed Oct 19, 2015
1 parent add7d38 commit 7e2e2d1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ line
*.html
linenoise_example
*.dSYM
history.txt
*.txt
*.exe
*.dll
43 changes: 41 additions & 2 deletions libline.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Richard Howe
* @license BSD (included as comment)
*
* @todo There are a few vi commands that should be added
* @todo There are a few vi commands that should be added (rR)
* @todo The vi section and the rest should be separated.
* @todo It would probably be best to split up this file into separate
* smaller ones.
Expand All @@ -34,7 +34,7 @@
*
* ADDITIONAL COPYRIGHT
*
* Copyright (c) 2014, Richard James Howe <howe.r.j.89@gmail.com>
* Copyright (c) 2015, Richard James Howe <howe.r.j.89@gmail.com>
*
* ORIGINAL COPYRIGHT HEADER
*
Expand Down Expand Up @@ -124,9 +124,17 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef __unix__
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#elif _WIN32
#include <windows.h>
#include <io.h>
#include <conio.h>
#else
#error "Unsupported system"
#endif

#define LINENOISE_DEFAULT_HISTORY_MAX_LEN (100)
#define LINENOISE_MAX_LINE (4096)
Expand All @@ -138,8 +146,10 @@ static char *unsupported_term[] = { "dumb", "cons25", "emacs", NULL };

static line_completion_callback *completion_callback = NULL;

#ifdef __unix__
static struct termios orig_termios; /* In order to restore at exit. */
static int rawmode = 0; /* For atexit() function to check if restore is needed */
#endif
static int atexit_registered = 0; /* Register atexit just 1 time. */
static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
static int history_len = 0;
Expand Down Expand Up @@ -258,6 +268,7 @@ static int is_unsupported_term(void)
**/
static int enable_raw_mode(int fd)
{
#ifdef __unix__
struct termios raw;

if (!isatty(STDIN_FILENO))
Expand Down Expand Up @@ -294,16 +305,28 @@ static int enable_raw_mode(int fd)
fatal:
errno = ENOTTY;
return -1;
#elif _WIN32
(void)fd; /* parameter not used */
if (!atexit_registered) {
atexit(line_at_exit);
atexit_registered = 1;
}
return 0;
#endif
}

/**
* @brief like enable_raw_mode but the exact opposite
**/
static void disable_raw_mode(int fd)
{
#ifdef __unix__
/* Don't even check the return value as it's too late. */
if (rawmode && tcsetattr(fd, TCSAFLUSH, &orig_termios) != -1)
rawmode = 0;
#elif _WIN32
(void)fd;
#endif
}

/**
Expand Down Expand Up @@ -345,6 +368,7 @@ static int get_cursor_position(int ifd, int ofd)
**/
static int get_columns(int ifd, int ofd)
{
#ifdef __unix__
struct winsize ws;

if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
Expand Down Expand Up @@ -378,16 +402,25 @@ static int get_columns(int ifd, int ofd)

failed:
return 80;
#elif _WIN32
(void)ifd;
(void)ofd;
return 80;
#endif
}

/**
* @brief Clear the screen. Used to handle ctrl+l
**/
void line_clearscreen(void)
{
#ifdef __unix__
if (write(STDOUT_FILENO, "\x1b[H\x1b[2J", 7) <= 0) {
/* nothing to do, just to avoid warning. */
}
#elif _WIN32
/*clrscr();*/
#endif
}

/**
Expand All @@ -396,8 +429,12 @@ void line_clearscreen(void)
**/
static void line_beep(void)
{
#ifdef __unix__
fputs("\x7",stderr);
fflush(stderr);
#elif _WIN32
/*beep();*/
#endif
}

/******************************** Completion *********************************/
Expand Down Expand Up @@ -1243,8 +1280,10 @@ static void free_history(void)
**/
static void line_at_exit(void)
{
#ifdef __unix__
if(rawmode)
disable_raw_mode(STDIN_FILENO);
#endif
free_history();
}

Expand Down
33 changes: 27 additions & 6 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
CC=gcc
CFLAGS=-Wall -Wextra -g -pedantic
TARGET_SYSTEM := $(subst -, ,$(shell $(CC) -dumpmachine))

CC?=gcc
CFLAGS?=-Wall -Wextra -g -pedantic

ifeq (mingw32, $(TARGET_SYSTEM))
DLL=dll
RM=del
RM_FLAGS= /Q
else
DLL=so
ADDITIONAL=
RM=rm
RM_FLAGS= -rf
endif


TARGET=line
.PHONY: all clean
all: $(TARGET) libline.so
all: $(TARGET) libline.a
doc: $(TARGET).htm
lib$(TARGET).a: lib$(TARGET).o
ar rcs $@ $<
lib$(TARGET).so: lib$(TARGET).c lib$(TARGET).o lib$(TARGET).h
lib$(TARGET).$(DLL): lib$(TARGET).c lib$(TARGET).o lib$(TARGET).h
$(CC) -shared -fPIC $< -o $@
lib$(TARGET).o: lib$(TARGET).c lib$(TARGET).h
$(CC) $(CFLAGS) $< -fPIC -c -o $@
$(TARGET): main.c lib$(TARGET).a
$(TARGET): main.c lib$(TARGET).$(DLL)
$(CC) $(CFLAGS) $^ -o $@
$(TARGET).htm: $(TARGET).md
markdown $^ > $@
run: $(TARGET)
./$^
clean:
rm -rf $(TARGET) *.a *.so *.o *.log *.htm doxygen
$(RM) $(RM_FLAGS) $(TARGET) *.a *.$(DLL) *.o *.log *.htm doxygen
$(RM) $(RM_FLAGS) $(TARGET) *.$(DLL)
$(RM) $(RM_FLAGS) $(TARGET) *.o
$(RM) $(RM_FLAGS) $(TARGET) *.log
$(RM) $(RM_FLAGS) $(TARGET) *.htm
$(RM) $(RM_FLAGS) $(TARGET) doxygen

0 comments on commit 7e2e2d1

Please sign in to comment.