Skip to content

Commit f2b7498

Browse files
Elliott HirdElliott Hird
Elliott Hird
authored and
Elliott Hird
committed
Portability redux.
1 parent 4e76745 commit f2b7498

17 files changed

+288
-414
lines changed

Makefile

+1-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1 @@
1-
ifeq ($(origin objdir), undefined)
2-
ifeq ($(origin OBJDIR), undefined)
3-
objdir := $(if $(debug),_debug,_build)
4-
endif
5-
endif
6-
7-
include useful.make
8-
9-
libs := gthread-2.0 sdl
10-
11-
cc.flags := -Wall -Werror -std=gnu99
12-
cc.flags += $(shell pkg-config --cflags $(libs))
13-
ld.flags += $(shell pkg-config --libs $(libs)) -lz -lreadline
14-
15-
# Fuck Apple.
16-
ifeq ($(shell uname),Darwin)
17-
cc.flags += -fnested-functions
18-
endif
19-
20-
cc.flags += $(if $(debug),-g,-O3)
21-
22-
all: $(objdir)/mcmap
23-
24-
$(call c-program, mcmap, cmd.c console.c main.c map.c nbt.c protocol.c world.c)
1+
include Makefile.posix

Makefile.common

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# mcmap/Makefile.common -*- mode: makefile -*-
2+
3+
sources += cmd.c console.c main.c map.c nbt.c protocol.c world.c
4+
5+
ifdef debug
6+
CFLAGS ?= -g
7+
OBJDIR ?= build-debug
8+
else
9+
CFLAGS ?= -O3
10+
OBJDIR ?= build
11+
endif
12+
13+
objs += $(sources:%.c=$(OBJDIR)/%.o)
14+
deps += $(sources:%.c=$(OBJDIR)/%.d)
15+
16+
CFLAGS += -Wall -Werror -std=gnu99
17+
18+
.PHONY: all clean
19+
20+
all: $(OBJDIR)/mcmap$(EXE)
21+
22+
$(OBJDIR)/mcmap$(EXE): $(objs)
23+
$(CC) $(LDFLAGS) -o $@ $(objs)
24+
25+
$(OBJDIR):
26+
mkdir $@
27+
28+
$(OBJDIR)/%.o: %.c $(OBJDIR)
29+
$(CC) $(CFLAGS) -c -o $@ $<
30+
31+
-include $(deps)
32+
33+
clean:
34+
rm -f $(OBJDIR)/mcmap$(EXE) $(objs) $(deps)
35+
rmdir $(OBJDIR); true
36+
37+
$(OBJDIR)/%.d: %.c $(OBJDIR)
38+
@$(CC) -MM -MF $@ -MT $(@:.d=.o) -MT $@ $(CFLAGS) $<

Makefile.posix

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# mcmap/Makefile.posix -*- mode: makefile -*-
2+
3+
sources = posix.c
4+
libs := gthread-2.0 sdl
5+
6+
include Makefile.common
7+
8+
CFLAGS += -DPLATFORM=posix
9+
CFLAGS += $(shell pkg-config --cflags $(libs))
10+
11+
# Apple's gcc is braindead by default.
12+
ifeq ($(shell uname),Darwin)
13+
CFLAGS += -fnested-functions
14+
endif
15+
16+
LDFLAGS ?= $(shell pkg-config --libs $(libs)) -lz -lreadline

Makefile.win

-40
This file was deleted.

Makefile.win32

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# mcmap/Makefile.win32 -*- mode: makefile -*-
2+
3+
sources = win32.c
4+
objs = win32-res.o
5+
6+
include Makefile.common
7+
8+
CC := i586-mingw32msvc-gcc
9+
WINDRES ?= i586-mingw32msvc-windres
10+
11+
CFLAGS += -DWINVER=0x0501 -DPLATFORM=win32
12+
CFLAGS += -Iwin/glib/include -Iwin/glib/include/glib-2.0 -Iwin/glib/lib/glib-2.0/include
13+
CFLAGS += -Iwin/SDL-1.2.14/include/SDL
14+
15+
LDFLAGS += -lws2_32 -Wl,--subsystem,console
16+
LDFLAGS += -Lwin/glib/lib -lglib-2.0 -lgobject-2.0 -lgthread-2.0 -lz
17+
LDFLAGS += -Lwin/SDL-1.2.14/lib -lSDL
18+
19+
EXE := .exe
20+
21+
win32-res.o: win32.rc win32-res.h
22+
$(WINDRES) -O coff $< $@

common.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef MCMAP_COMMON_H
22
#define MCMAP_COMMON_H
33

4+
#include "platform.h"
45
#include "protocol.h"
56

67
#include <glib.h>

console.c

+2-148
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,12 @@
55

66
#include <glib.h>
77

8-
#ifndef WIN32
9-
#include <poll.h>
10-
#include <readline/readline.h>
11-
#include <readline/history.h>
12-
#endif
13-
148
#include "common.h"
159
#include "console.h"
1610
#include "protocol.h"
1711
#include "world.h"
1812

19-
static int console_outfd = 1;
20-
21-
#ifndef WIN32
22-
static int console_readline = 0;
23-
static int opipe_read, opipe_write;
24-
#endif
25-
26-
/* setting up */
27-
28-
#ifndef WIN32
29-
static gpointer console_thread(gpointer userdata);
30-
static void console_cleanup(void);
31-
#endif
32-
33-
void console_init(void)
34-
{
35-
#ifndef WIN32
36-
if (isatty(0) && isatty(1))
37-
{
38-
int pipefd[2];
39-
if (pipe(pipefd) != 0)
40-
goto no_terminal;
41-
42-
opipe_read = pipefd[0];
43-
opipe_write = pipefd[1];
44-
45-
rl_readline_name = "mcmap";
46-
47-
console_readline = 1;
48-
console_outfd = opipe_write;
49-
atexit(console_cleanup);
50-
51-
g_thread_create(console_thread, NULL, FALSE, 0);
52-
}
53-
54-
no_terminal:
55-
#endif
56-
return;
57-
}
13+
int console_outfd = 1;
5814

5915
/* log-printing methods in common.h */
6016

@@ -90,10 +46,8 @@ void log_print(char *fmt, ...)
9046

9147
void log_die(char *file, int line, int is_stop, char *fmt, ...)
9248
{
93-
#ifndef WIN32
94-
if (console_readline && !is_stop)
49+
if (!is_stop)
9550
console_cleanup();
96-
#endif
9751

9852
GString *msg = tstamp();
9953
g_string_append_printf(msg, "[DIED] %s: %d: ", file, line);
@@ -112,103 +66,3 @@ void log_die(char *file, int line, int is_stop, char *fmt, ...)
11266

11367
exit(1);
11468
}
115-
116-
/* readline input and log output interlacing */
117-
118-
#ifndef WIN32
119-
static void console_line_ready(char *line)
120-
{
121-
if (line)
122-
{
123-
add_history(line);
124-
inject_to_server(packet_new(PACKET_TO_SERVER, PACKET_CHAT, line));
125-
}
126-
else /* ^D */
127-
exit(0);
128-
129-
free(line);
130-
131-
rl_callback_handler_install("> ", console_line_ready);
132-
}
133-
134-
static gpointer console_thread(gpointer userdata)
135-
{
136-
struct pollfd pfds[2] = {
137-
{ .fd = opipe_read, .events = POLLIN },
138-
{ .fd = 0, .events = POLLIN }
139-
};
140-
141-
GIOChannel *och = g_io_channel_unix_new(opipe_read);
142-
rl_callback_handler_install("> ", console_line_ready);
143-
144-
while (1)
145-
{
146-
/* wait for input (stdin) or output (pipe) */
147-
148-
int i = poll(pfds, 2, -1);
149-
if (i < 0 && errno == EINTR)
150-
continue;
151-
152-
if (pfds[0].revents & POLLIN)
153-
{
154-
/* clean up the prompt and its contents */
155-
156-
int old_point = rl_point, old_mark = rl_mark;
157-
char *old_text = strdup(rl_line_buffer);
158-
159-
rl_replace_line("", 0);
160-
rl_redisplay();
161-
162-
fputs("\r\x1b[K", stdout);
163-
164-
/* display pending output lines (TODO: more than one) */
165-
166-
gchar *line = 0;
167-
gsize line_eol = 0;
168-
169-
g_io_channel_read_line(och, &line, 0, &line_eol, 0);
170-
if (!line || !*line)
171-
{
172-
rl_callback_handler_remove();
173-
console_cleanup();
174-
return NULL;
175-
}
176-
177-
fputs(line, stdout);
178-
179-
g_free(line);
180-
181-
/* restore the readline prompt and contents */
182-
183-
rl_insert_text(old_text);
184-
rl_point = old_point;
185-
rl_mark = old_mark;
186-
free(old_text);
187-
188-
rl_forced_update_display();
189-
}
190-
191-
if (pfds[1].revents & POLLIN)
192-
{
193-
/* let readline eat from stdin */
194-
rl_callback_read_char();
195-
}
196-
}
197-
198-
exit(0);
199-
}
200-
201-
static void console_cleanup(void)
202-
{
203-
if (console_readline)
204-
{
205-
rl_deprep_terminal();
206-
putchar('\n');
207-
208-
close(opipe_write);
209-
}
210-
211-
console_readline = 0;
212-
console_outfd = 1;
213-
}
214-
#endif

console.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#define MCMAP_CONSOLE_H 1
33

44
void console_init(void);
5+
int console_outfd;
56

67
#endif /* MCMAP_CONSOLE_H */

0 commit comments

Comments
 (0)