Clean up library dependencies
Move stuff to untangle the ugly cyclic dependencies between the archives built for selected subdirectories of src/lib/: * Move common/io.c to empthread/ because it requires empthread stuff * Move parts of subs/nstr.c to common/nstreval.c to satisfy common/ef_verify.o * Move getstarg.c getstring.c onearg.c from gen/ to subs/ because they require stuff from there * Move bridgefall.c check.c damage.c empobj.c journal.c maps.c sectdamage.c from common/ to subs/ because they require stuff from there * Move cnumb.c from subs/ to common/ to satisfy common/type.o * Move log.c fsize.c from common/ to gen/ because they really belong there * Move emp_config.c mapdist.c from gen/ to common/ because they really belong there, and require stuff from libglobal.a Also package as/ as libas.a to satisfy common/path.o. Remaining dependencies: lib needs -------------------------------------------- libas.a libglobal.a libcommon.a libas.a libglobal.a libgen.a libgen.a libglobal.a liblwp.a libgen.a libw32.a[*] libgen.a [*] Except for service.o, which can only be linked into the server Link order now: liblwp.a libcommon.a libas.a libgen.a libglobal.a libw32.a. The position of libw32.a is not quite right, but works anyway.
This commit is contained in:
parent
1cbb37d4fb
commit
77e95bd788
20 changed files with 235 additions and 191 deletions
|
@ -1,246 +0,0 @@
|
|||
/*
|
||||
* Empire - A multi-player, client/server Internet based war game.
|
||||
* Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
|
||||
* Ken Stevens, Steve McClure
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* See files README, COPYING and CREDITS in the root of the source
|
||||
* tree for related information and legal notices. It is expected
|
||||
* that future projects/authors will amend these files as needed.
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* emp_config.c: Allows config file to control server config. from a file
|
||||
*
|
||||
* Known contributors to this file:
|
||||
* Julian Onions, 1995
|
||||
* Steve McClure, 1998-2000
|
||||
*/
|
||||
|
||||
/*
|
||||
* STILL TO DO
|
||||
*
|
||||
* Change other constants - such as MAXNOC etc.
|
||||
* Just requires variables to be assigned, then dynamic allocation in
|
||||
* a few places. Some checks needed in the server to check the world
|
||||
* hasn't changed size etc.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "file.h"
|
||||
#include "misc.h"
|
||||
#include "optlist.h"
|
||||
#include "prototypes.h"
|
||||
|
||||
/* Dummy one */
|
||||
static int emp_config_dummy;
|
||||
|
||||
/* things that can be changed */
|
||||
struct keymatch configkeys[] = {
|
||||
#define EMP_CONFIG_C_OUTPUT
|
||||
#include "econfig-spec.h"
|
||||
#undef EMP_CONFIG_C_OUTPUT
|
||||
};
|
||||
|
||||
static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
|
||||
static void set_paths(char *);
|
||||
|
||||
/*
|
||||
* read in empire configuration
|
||||
*/
|
||||
int
|
||||
emp_config(char *file)
|
||||
{
|
||||
FILE *fp;
|
||||
char scanspace[1024];
|
||||
char *av[128];
|
||||
char buf[1024];
|
||||
struct keymatch *kp;
|
||||
int lno = 0;
|
||||
int errors = 0;
|
||||
int i;
|
||||
|
||||
if (!file)
|
||||
file = dflt_econfig;
|
||||
errno = 0;
|
||||
if ((fp = fopen(file, "r")) == NULL) {
|
||||
if (file == dflt_econfig && errno == ENOENT)
|
||||
goto done;
|
||||
fprintf(stderr, "Can't open %s for reading (%s)\n",
|
||||
file, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
||||
++lno;
|
||||
for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
|
||||
if (!buf[i] || buf[i] == '#')
|
||||
continue;
|
||||
if (parse(buf, scanspace, av, NULL, NULL, NULL) < 0) {
|
||||
fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
|
||||
errors = 1;
|
||||
continue;
|
||||
}
|
||||
if ((kp = keylookup(av[0], configkeys)) == NULL) {
|
||||
fprintf(stderr, "%s:%d: Unknown config key %s\n",
|
||||
file, lno, av[0]);
|
||||
errors = 1;
|
||||
continue;
|
||||
}
|
||||
if (av[1] == NULL) {
|
||||
fprintf(stderr, "%s:%d: Config key %s needs a value\n",
|
||||
file, lno, av[0]);
|
||||
errors = 1;
|
||||
continue;
|
||||
}
|
||||
i = 2;
|
||||
switch (kp->km_type) {
|
||||
case NSC_INT:
|
||||
*(int *)kp->km_data = atoi(av[1]);
|
||||
break;
|
||||
case NSC_FLOAT:
|
||||
*(float *)kp->km_data = atof(av[1]);
|
||||
break;
|
||||
case NSC_DOUBLE:
|
||||
*(double *)kp->km_data = atof(av[1]);
|
||||
break;
|
||||
case NSC_LONG:
|
||||
*(long *)kp->km_data = atol(av[1]);
|
||||
break;
|
||||
case NSC_STRING:
|
||||
if (kp->km_flags & KM_ALLOC)
|
||||
free(*(char **)kp->km_data);
|
||||
*(char **)kp->km_data = strdup(av[1]);
|
||||
kp->km_flags |= KM_ALLOC;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
if (av[i] != NULL) {
|
||||
fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
|
||||
file, lno, av[0]);
|
||||
errors = 1;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
done:
|
||||
WORLD_X &= ~1; /* force even */
|
||||
set_paths(file);
|
||||
|
||||
return -errors;
|
||||
}
|
||||
|
||||
/* find the key in the table */
|
||||
static struct keymatch *
|
||||
keylookup(char *command, struct keymatch *tbl)
|
||||
{
|
||||
struct keymatch *kp;
|
||||
|
||||
if (command == 0 || *command == 0)
|
||||
return 0;
|
||||
for (kp = tbl; kp->km_key != 0; kp++) {
|
||||
if (strcmp(kp->km_key, command) == 0)
|
||||
return kp;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
set_paths(char *econfig)
|
||||
{
|
||||
char *slash;
|
||||
char *cwd = getcwd(NULL, 0);
|
||||
|
||||
#ifdef _WIN32
|
||||
/* normalize path separator to '\\', for easier searching: */
|
||||
econfig = _fullpath(NULL, econfig, 0);
|
||||
slash = strrchr(econfig, '\\');
|
||||
configdir = malloc(slash - econfig + 1);
|
||||
memcpy(configdir, econfig, slash - econfig);
|
||||
configdir[slash - econfig] = 0;
|
||||
#else
|
||||
if ((slash = strrchr(econfig, '/'))) {
|
||||
configdir = malloc(slash - econfig + 1);
|
||||
memcpy(configdir, econfig, slash - econfig);
|
||||
configdir[slash - econfig] = 0;
|
||||
} else
|
||||
configdir = strdup(cwd);
|
||||
|
||||
if (configdir[0] != '/') {
|
||||
char *tmp = configdir;
|
||||
size_t len = strlen(cwd);
|
||||
|
||||
configdir = malloc(len + 1 + strlen(tmp) + 1);
|
||||
sprintf(configdir, "%s/%s", cwd, tmp);
|
||||
free(tmp);
|
||||
}
|
||||
#endif /* !_WIN32 */
|
||||
|
||||
schedulefil = malloc(strlen(configdir) + 10);
|
||||
sprintf(schedulefil, "%s/schedule", configdir);
|
||||
|
||||
free(cwd);
|
||||
}
|
||||
|
||||
void
|
||||
print_config(FILE *fp)
|
||||
{
|
||||
struct keymatch *kp;
|
||||
|
||||
fprintf(fp, "# Empire Configuration File:\n");
|
||||
for (kp = configkeys; kp->km_key; kp++) {
|
||||
if (kp->km_comment) {
|
||||
if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
|
||||
fprintf(fp, "\n# ");
|
||||
fprintf(fp, "%s\n", kp->km_comment);
|
||||
}
|
||||
if (!kp->km_key[0])
|
||||
continue;
|
||||
switch (kp->km_type) {
|
||||
case NSC_STRING:
|
||||
fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
|
||||
break;
|
||||
case NSC_INT:
|
||||
fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
|
||||
break;
|
||||
case NSC_FLOAT:
|
||||
fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
|
||||
break;
|
||||
case NSC_DOUBLE:
|
||||
fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
|
||||
break;
|
||||
case NSC_LONG:
|
||||
fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(fp, "\n");
|
||||
}
|
|
@ -25,38 +25,56 @@
|
|||
*
|
||||
* ---
|
||||
*
|
||||
* getstring.c: get string, printing a prompt if there is one
|
||||
* fsize.c: BSD dependant file and block sizing routines
|
||||
*
|
||||
* Known contributors to this file:
|
||||
*
|
||||
* Dave Pare, 1986
|
||||
* Doug Hay, 1998
|
||||
* Steve McClure, 1998
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include "prototypes.h"
|
||||
|
||||
/*
|
||||
* Print sub-prompt PROMPT, receive a line of input into BUF[1024].
|
||||
* Return BUF on success, else NULL.
|
||||
* return the size of the file in bytes.
|
||||
*/
|
||||
char *
|
||||
getstring(char *prompt, char *buf)
|
||||
int
|
||||
fsize(int fd)
|
||||
{
|
||||
*buf = '\0';
|
||||
if (prmptrd(prompt, buf, 1024) < 0)
|
||||
return NULL;
|
||||
return buf;
|
||||
struct stat statb;
|
||||
|
||||
if (fstat(fd, &statb) < 0)
|
||||
return -1;
|
||||
return statb.st_size;
|
||||
}
|
||||
|
||||
/*
|
||||
* Print sub-prompt PROMPT, receive a line of UTF-8 input into BUF[1024].
|
||||
* Return BUF on success, else NULL.
|
||||
* Return the preferred block size for I/O on FD.
|
||||
*/
|
||||
char *
|
||||
ugetstring(char *prompt, char *buf)
|
||||
int
|
||||
blksize(int fd)
|
||||
{
|
||||
*buf = '\0';
|
||||
if (uprmptrd(prompt, buf, 1024) < 0)
|
||||
return NULL;
|
||||
return buf;
|
||||
#if defined(_WIN32)
|
||||
return 2048;
|
||||
#else /* !_WIN32 */
|
||||
struct stat statb;
|
||||
|
||||
if (fstat(fd, &statb) < 0)
|
||||
return 1024;
|
||||
return statb.st_blksize;
|
||||
#endif /* !_WIN32 */
|
||||
}
|
||||
|
||||
time_t
|
||||
fdate(int fd)
|
||||
{
|
||||
struct stat statb;
|
||||
|
||||
if (fstat(fd, &statb) < 0)
|
||||
return 0;
|
||||
return statb.st_mtime;
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* Empire - A multi-player, client/server Internet based war game.
|
||||
* Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
|
||||
* Ken Stevens, Steve McClure
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* See files README, COPYING and CREDITS in the root of the source
|
||||
* tree for related information and legal notices. It is expected
|
||||
* that future projects/authors will amend these files as needed.
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* getstarg.c: Get a string argument (ask if not there)
|
||||
*
|
||||
* Known contributors to this file:
|
||||
*
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <string.h>
|
||||
#include "misc.h"
|
||||
|
||||
/*
|
||||
* Get string argument.
|
||||
* If INPUT is not empty, use it, else prompt for more input using PROMPT.
|
||||
* Copy input to BUF[1024].
|
||||
* Return BUF on success, else NULL.
|
||||
*/
|
||||
char *
|
||||
getstarg(char *input, char *prompt, char *buf)
|
||||
{
|
||||
*buf = '\0';
|
||||
if (input == 0 || *input == 0) {
|
||||
if (getstring(prompt, buf) == 0)
|
||||
return 0;
|
||||
} else {
|
||||
strcpy(buf, input);
|
||||
}
|
||||
return buf;
|
||||
}
|
350
src/lib/gen/io.c
350
src/lib/gen/io.c
|
@ -1,350 +0,0 @@
|
|||
/*
|
||||
* Empire - A multi-player, client/server Internet based war game.
|
||||
* Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
|
||||
* Ken Stevens, Steve McClure
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* See files README, COPYING and CREDITS in the root of the source
|
||||
* tree for related information and legal notices. It is expected
|
||||
* that future projects/authors will amend these files as needed.
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* io.c: Arrange for input and output on a file descriptor to be queued.
|
||||
*
|
||||
* Known contributors to this file:
|
||||
* Doug Hay, 1998
|
||||
* Steve McClure, 1998
|
||||
*/
|
||||
|
||||
/*
|
||||
* Arrange for input and output on a file descriptor
|
||||
* to be queued. Provide main loop -- a mechanism for
|
||||
* blocking across all registered file descriptors, and
|
||||
* reading or writing when appropriate.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
#include "empio.h"
|
||||
#include "empthread.h"
|
||||
#include "ioqueue.h"
|
||||
#include "misc.h"
|
||||
#include "queue.h"
|
||||
#include "server.h"
|
||||
|
||||
struct iop {
|
||||
int fd;
|
||||
struct ioqueue *input;
|
||||
struct ioqueue *output;
|
||||
int flags;
|
||||
int bufsize;
|
||||
};
|
||||
|
||||
void
|
||||
io_init(void)
|
||||
{
|
||||
}
|
||||
|
||||
struct iop *
|
||||
io_open(int fd, int flags, int bufsize)
|
||||
{
|
||||
struct iop *iop;
|
||||
|
||||
flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
|
||||
if ((flags & (IO_READ | IO_WRITE)) == 0)
|
||||
return NULL;
|
||||
iop = malloc(sizeof(struct iop));
|
||||
if (!iop)
|
||||
return NULL;
|
||||
iop->fd = fd;
|
||||
iop->input = 0;
|
||||
iop->output = 0;
|
||||
iop->flags = 0;
|
||||
iop->bufsize = bufsize;
|
||||
if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
|
||||
iop->input = ioq_create(bufsize);
|
||||
if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
|
||||
iop->output = ioq_create(bufsize);
|
||||
if (flags & IO_NBLOCK)
|
||||
io_noblocking(iop, 1); /* FIXME check success */
|
||||
iop->flags = flags;
|
||||
return iop;
|
||||
}
|
||||
|
||||
void
|
||||
io_close(struct iop *iop)
|
||||
{
|
||||
|
||||
if (iop->input != 0)
|
||||
ioq_destroy(iop->input);
|
||||
if (iop->output != 0)
|
||||
ioq_destroy(iop->output);
|
||||
(void)close(iop->fd);
|
||||
free(iop);
|
||||
}
|
||||
|
||||
int
|
||||
io_input(struct iop *iop, int waitforinput)
|
||||
{
|
||||
char buf[IO_BUFSIZE];
|
||||
int cc;
|
||||
|
||||
/* Not a read IOP */
|
||||
if ((iop->flags & IO_READ) == 0)
|
||||
return -1;
|
||||
/* IOP is markes as in error. */
|
||||
if (iop->flags & IO_ERROR)
|
||||
return -1;
|
||||
/* Wait for the file to have input. */
|
||||
if (waitforinput) {
|
||||
empth_select(iop->fd, EMPTH_FD_READ);
|
||||
}
|
||||
/* Do the actual read. */
|
||||
cc = read(iop->fd, buf, sizeof(buf));
|
||||
if (cc < 0) {
|
||||
/* would block, so nothing to read. */
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
return 0;
|
||||
|
||||
/* Some form of file error occurred... */
|
||||
iop->flags |= IO_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* We eof'd */
|
||||
if (cc == 0) {
|
||||
iop->flags |= IO_EOF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Append the input to the IOQ. */
|
||||
ioq_append(iop->input, buf, cc);
|
||||
return cc;
|
||||
}
|
||||
|
||||
int
|
||||
io_inputwaiting(struct iop *iop)
|
||||
{
|
||||
return ioq_qsize(iop->input);
|
||||
}
|
||||
|
||||
int
|
||||
io_outputwaiting(struct iop *iop)
|
||||
{
|
||||
return ioq_qsize(iop->output);
|
||||
}
|
||||
|
||||
int
|
||||
io_output(struct iop *iop, int waitforoutput)
|
||||
{
|
||||
struct iovec iov[16];
|
||||
int cc;
|
||||
int n;
|
||||
int remain;
|
||||
|
||||
/* If there is no output waiting. */
|
||||
if (!io_outputwaiting(iop))
|
||||
return 0;
|
||||
|
||||
/* If the iop is not write enabled. */
|
||||
if ((iop->flags & IO_WRITE) == 0)
|
||||
return -1;
|
||||
|
||||
/* If the io is marked as in error... */
|
||||
if (iop->flags & IO_ERROR)
|
||||
return -1;
|
||||
|
||||
/* make the iov point to the data in the queue. */
|
||||
/* I.E., each of the elements in the queue. */
|
||||
/* returns the number of elements in the iov. */
|
||||
n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
|
||||
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* wait for the file to be output ready. */
|
||||
if (waitforoutput != IO_NOWAIT) {
|
||||
/* This waits for the file to be ready for writing, */
|
||||
/* and lets other threads run. */
|
||||
empth_select(iop->fd, EMPTH_FD_WRITE);
|
||||
}
|
||||
|
||||
/* Do the actual write. */
|
||||
cc = writev(iop->fd, iov, n);
|
||||
|
||||
/* if it failed.... */
|
||||
if (cc < 0) {
|
||||
/* Hmm, it would block. file is opened noblock, soooooo.. */
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
/* If there are remaining bytes, set the IO as remaining.. */
|
||||
remain = ioq_qsize(iop->output);
|
||||
return remain;
|
||||
}
|
||||
iop->flags |= IO_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If no bytes were written, something happened.. Like an EOF. */
|
||||
if (cc == 0) {
|
||||
iop->flags |= IO_EOF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Remove the number of written bytes from the queue. */
|
||||
ioq_dequeue(iop->output, cc);
|
||||
|
||||
return cc;
|
||||
}
|
||||
|
||||
int
|
||||
io_peek(struct iop *iop, char *buf, int nbytes)
|
||||
{
|
||||
if ((iop->flags & IO_READ) == 0)
|
||||
return -1;
|
||||
return ioq_peek(iop->input, buf, nbytes);
|
||||
}
|
||||
|
||||
int
|
||||
io_read(struct iop *iop, char *buf, int nbytes)
|
||||
{
|
||||
int cc;
|
||||
|
||||
if ((iop->flags & IO_READ) == 0)
|
||||
return -1;
|
||||
cc = ioq_peek(iop->input, buf, nbytes);
|
||||
if (cc > 0)
|
||||
ioq_dequeue(iop->input, cc);
|
||||
return cc;
|
||||
}
|
||||
|
||||
int
|
||||
io_write(struct iop *iop, char *buf, int nbytes, int doWait)
|
||||
{
|
||||
int len;
|
||||
|
||||
if ((iop->flags & IO_WRITE) == 0)
|
||||
return -1;
|
||||
ioq_append(iop->output, buf, nbytes);
|
||||
len = ioq_qsize(iop->output);
|
||||
if (len > iop->bufsize) {
|
||||
if (doWait) {
|
||||
io_output_all(iop);
|
||||
} else {
|
||||
/* only try a write every BUFSIZE characters */
|
||||
if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
|
||||
io_output(iop, IO_NOWAIT);
|
||||
}
|
||||
}
|
||||
return nbytes;
|
||||
}
|
||||
|
||||
int
|
||||
io_output_all(struct iop *iop)
|
||||
{
|
||||
int n;
|
||||
|
||||
/*
|
||||
* Mustn't block a player thread while update is pending, or else
|
||||
* a malicous player could delay the update indefinitely
|
||||
*/
|
||||
while ((n = io_output(iop, IO_NOWAIT)) > 0 && !play_wrlock_wanted)
|
||||
empth_select(iop->fd, EMPTH_FD_WRITE);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int
|
||||
io_gets(struct iop *iop, char *buf, int nbytes)
|
||||
{
|
||||
if ((iop->flags & IO_READ) == 0)
|
||||
return -1;
|
||||
return ioq_gets(iop->input, buf, nbytes);
|
||||
}
|
||||
|
||||
int
|
||||
io_puts(struct iop *iop, char *buf)
|
||||
{
|
||||
if ((iop->flags & IO_WRITE) == 0)
|
||||
return -1;
|
||||
return ioq_puts(iop->output, buf);
|
||||
}
|
||||
|
||||
int
|
||||
io_shutdown(struct iop *iop, int flags)
|
||||
{
|
||||
flags &= (IO_READ | IO_WRITE);
|
||||
if ((iop->flags & flags) != flags)
|
||||
return -1;
|
||||
if (flags & IO_READ) {
|
||||
shutdown(iop->fd, 0);
|
||||
ioq_drain(iop->input);
|
||||
}
|
||||
if (flags & IO_WRITE) {
|
||||
shutdown(iop->fd, 1);
|
||||
ioq_drain(iop->output);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
io_noblocking(struct iop *iop, int value)
|
||||
{
|
||||
int flags;
|
||||
|
||||
flags = fcntl(iop->fd, F_GETFL, 0);
|
||||
if (flags < 0)
|
||||
return -1;
|
||||
if (value == 0)
|
||||
flags &= ~O_NONBLOCK;
|
||||
else
|
||||
flags |= O_NONBLOCK;
|
||||
if (fcntl(iop->fd, F_SETFL, flags) < 0)
|
||||
return -1;
|
||||
if (value == 0)
|
||||
iop->flags &= ~IO_NBLOCK;
|
||||
else
|
||||
iop->flags |= IO_NBLOCK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
io_error(struct iop *iop)
|
||||
{
|
||||
return iop->flags & IO_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
io_eof(struct iop *iop)
|
||||
{
|
||||
return iop->flags & IO_EOF;
|
||||
}
|
||||
|
||||
int
|
||||
io_fileno(struct iop *iop)
|
||||
{
|
||||
return iop->fd;
|
||||
}
|
150
src/lib/gen/log.c
Normal file
150
src/lib/gen/log.c
Normal file
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* Empire - A multi-player, client/server Internet based war game.
|
||||
* Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
|
||||
* Ken Stevens, Steve McClure
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* See files README, COPYING and CREDITS in the root of the source
|
||||
* tree for related information and legal notices. It is expected
|
||||
* that future projects/authors will amend these files as needed.
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* log.c: Log an Empire error to a file
|
||||
*
|
||||
* Known contributors to this file:
|
||||
* Dave Pare, 1989
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include "misc.h"
|
||||
#include "optlist.h"
|
||||
#include "player.h"
|
||||
#include "prototypes.h"
|
||||
|
||||
/* Debugging? If yes call abort() on internal error. */
|
||||
int debug = 0;
|
||||
|
||||
static char logfile[32];
|
||||
static int logfd = -1;
|
||||
|
||||
static int logopen(void);
|
||||
|
||||
/*
|
||||
* Points log file at PROGRAM.log
|
||||
*/
|
||||
int
|
||||
loginit(char *program)
|
||||
{
|
||||
sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, program);
|
||||
logfd = logopen();
|
||||
return logfd;
|
||||
}
|
||||
|
||||
static int
|
||||
logopen(void)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRWUG);
|
||||
if (fd < 0)
|
||||
logerror("Can't open %s (%s)", logfile, strerror(errno));
|
||||
return fd;
|
||||
}
|
||||
|
||||
int
|
||||
logreopen(void)
|
||||
{
|
||||
int newfd, res;
|
||||
|
||||
if ((newfd = logopen()) < 0)
|
||||
return -1;
|
||||
res = close(logfd);
|
||||
logfd = newfd;
|
||||
if (res < 0)
|
||||
logerror("Can't close %s (%s)", logfile, strerror(errno));
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write a line to the log file and to stderr.
|
||||
* Messages are silently truncated after 512 characters or a newline.
|
||||
*/
|
||||
void
|
||||
logerror(char *format, ...)
|
||||
{
|
||||
enum {
|
||||
ctime_len = 24, /* output of ctime() less the newline */
|
||||
msg_space = 512 /* space for formatted message */
|
||||
};
|
||||
va_list list;
|
||||
time_t now;
|
||||
char buf[ctime_len + 1 + msg_space + 2];
|
||||
char *msg, *p;
|
||||
|
||||
va_start(list, format);
|
||||
msg = buf + ctime_len + 1;
|
||||
vsnprintf(msg, msg_space, format, list);
|
||||
buf[sizeof(buf)-2] = 0;
|
||||
p = msg + strlen(msg);
|
||||
p[0] = '\n';
|
||||
p[1] = 0;
|
||||
p = strchr(msg, '\n');
|
||||
p[1] = 0;
|
||||
fputs(msg, stderr);
|
||||
if (logfd >= 0) {
|
||||
time(&now);
|
||||
memcpy(buf, ctime(&now), ctime_len);
|
||||
buf[ctime_len] = ' ';
|
||||
write(logfd, buf, strlen(buf));
|
||||
}
|
||||
va_end(list);
|
||||
}
|
||||
|
||||
/*
|
||||
* Log internal error MSG occured in FILE:LINE.
|
||||
* If debugging, call abort(), else return 1.
|
||||
*/
|
||||
int
|
||||
oops(char *msg, char *file, int line)
|
||||
{
|
||||
logerror("Oops: %s in %s:%d", msg ? msg : "bug", file, line);
|
||||
if (debug) abort();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Report out-of-memory condition and terminate the program.
|
||||
* Use this with restraint! Clean error recovery is preferable, but
|
||||
* not always feasible (e.g. halfway through the update) or worthwhile
|
||||
* (during server startup).
|
||||
*/
|
||||
void
|
||||
exit_nomem(void)
|
||||
{
|
||||
logerror("Memory exhausted");
|
||||
exit(1);
|
||||
}
|
|
@ -1,110 +0,0 @@
|
|||
/*
|
||||
* Empire - A multi-player, client/server Internet based war game.
|
||||
* Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
|
||||
* Ken Stevens, Steve McClure
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* See files README, COPYING and CREDITS in the root of the source
|
||||
* tree for related information and legal notices. It is expected
|
||||
* that future projects/authors will amend these files as needed.
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* mapdist.c: Return the distance between two sectors
|
||||
*
|
||||
* Known contributors to this file:
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* mapdist returns (integer) distance between two sectors.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "optlist.h"
|
||||
#include "prototypes.h"
|
||||
|
||||
int
|
||||
diffx(int x1, int x2)
|
||||
{
|
||||
int dx;
|
||||
|
||||
dx = x1 - x2;
|
||||
dx = dx % WORLD_X;
|
||||
if (dx > WORLD_X / 2)
|
||||
dx = dx - WORLD_X;
|
||||
if (dx < -WORLD_X / 2)
|
||||
dx = dx + WORLD_X;
|
||||
return dx;
|
||||
}
|
||||
|
||||
int
|
||||
diffy(int y1, int y2)
|
||||
{
|
||||
int dy;
|
||||
|
||||
dy = y1 - y2;
|
||||
dy = dy % WORLD_Y;
|
||||
if (dy > WORLD_Y / 2)
|
||||
dy = dy - WORLD_Y;
|
||||
if (dy < -WORLD_Y / 2)
|
||||
dy = dy + WORLD_Y;
|
||||
return dy;
|
||||
}
|
||||
|
||||
int
|
||||
deltax(int x1, int x2)
|
||||
{
|
||||
int dx;
|
||||
|
||||
dx = abs(x1 - x2);
|
||||
dx = dx % WORLD_X;
|
||||
if (dx > WORLD_X / 2)
|
||||
dx = WORLD_X - dx;
|
||||
return dx;
|
||||
}
|
||||
|
||||
int
|
||||
deltay(int y1, int y2)
|
||||
{
|
||||
int dy;
|
||||
|
||||
dy = abs(y1 - y2);
|
||||
dy = dy % WORLD_Y;
|
||||
if (dy > WORLD_Y / 2)
|
||||
dy = WORLD_Y - dy;
|
||||
return dy;
|
||||
}
|
||||
|
||||
int
|
||||
mapdist(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
int dx, dy;
|
||||
|
||||
x1 = x1 % WORLD_X;
|
||||
y1 = y1 % WORLD_Y;
|
||||
x2 = x2 % WORLD_X;
|
||||
y2 = y2 % WORLD_Y;
|
||||
dx = deltax(x1, x2);
|
||||
dy = deltay(y1, y2);
|
||||
if (dx > dy)
|
||||
return (dx - dy) / 2 + dy;
|
||||
return dy;
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* Empire - A multi-player, client/server Internet based war game.
|
||||
* Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
|
||||
* Ken Stevens, Steve McClure
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* See files README, COPYING and CREDITS in the root of the source
|
||||
* tree for related information and legal notices. It is expected
|
||||
* that future projects/authors will amend these files as needed.
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* onearg.c: Get one argument, or ask if not there
|
||||
*
|
||||
* Known contributors to this file:
|
||||
*
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "prototypes.h"
|
||||
|
||||
int
|
||||
onearg(char *arg, char *prompt)
|
||||
{
|
||||
int n;
|
||||
char buf[1024];
|
||||
|
||||
if (arg == 0 || *arg == 0) {
|
||||
if ((arg = getstring(prompt, buf)) == 0)
|
||||
return -1;
|
||||
}
|
||||
n = atoi(arg);
|
||||
if (n < 0)
|
||||
return -1;
|
||||
return n;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue