* (c) 1999 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
- * This file is part of the `fw' port forwarder.
+ * This file is part of the `fwd' port forwarder.
*
- * `fw' is free software; you can redistribute it and/or modify
+ * `fwd' 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.
- *
- * `fw' is distributed in the hope that it will be useful,
+ *
+ * `fwd' 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 `fw'; if not, write to the Free Software Foundation,
+ * along with `fwd'; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-/*----- Header files ------------------------------------------------------*/
-
-#include "config.h"
-
-#include <ctype.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <mLib/alloc.h>
-#include <mLib/dstr.h>
-#include <mLib/fdflags.h>
-#include <mLib/sub.h>
-
-#include "conf.h"
-#include "endpt.h"
-#include "fattr.h"
-#include "file.h"
-#include "fw.h"
-#include "reffd.h"
-#include "scan.h"
-#include "source.h"
-#include "target.h"
+#include "fwd.h"
/*----- Data structures ---------------------------------------------------*/
typedef struct fsource {
source s;
fdata f;
-} fsource;
+} fsource;
/* --- File target block --- */
else {
int n;
if ((n = open("/dev/null", O_RDWR)) < 0) {
- fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno));
+ fw_log(NOW, "couldn't open `/dev/null': %s", strerror(errno));
return (0);
}
fdflags(n, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
reffd_handler(rnull, file_null, 0);
}
return (rnull);
-}
+}
/* --- @file_fspec@ --- *
*
else if (fd) {
f->type = FTYPE_FD;
f->u.fd = fd;
- token(sc);
+ token(sc);
} else if (type == FTYPE_FD) {
if (sc->t == CTOK_WORD)
error(sc, "bad file descriptor `%s'", sc->d.buf);
error(sc, "parse error, expected file descriptor");
} else {
dstr d = DSTR_INIT;
- conf_name(sc, '/', &d);
+ conf_fname(sc, &d);
f->type = FTYPE_NAME;
f->u.name = xstrdup(d.buf);
dstr_destroy(&d);
static void file_read(fdata *f, scanner *sc)
{
file_fspec(&f->in, sc);
- if (sc->t != ',') {
+ if (sc->t == ',') {
+ token(sc);
+ file_fspec(&f->out, sc);
+ } else {
f->out = f->in;
if (f->out.type == FTYPE_FD && f->out.u.fd == rstdin)
f->out.u.fd = rstdout;
- } else {
- token(sc);
- file_fspec(&f->out, sc);
+ else if (f->out.type == FTYPE_NAME)
+ f->out.u.name = xstrdup(f->in.u.name);
}
f->fa = fattr_global;
f->fo = file_opts;
case FTYPE_NULL:
dstr_puts(d, "null");
break;
- }
+ }
}
static void file_desc(fdata *f, dstr *d)
case FTYPE_NAME: {
int fd;
if ((fd = open(f->in.u.name, O_RDONLY | O_NONBLOCK)) < 0) {
- fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
+ fw_log(NOW, "[%s] couldn't open `%s' for reading: %s",
desc, f->in.u.name, strerror(errno));
return (0);
}
if ((m & O_CREAT) &&
(fd = open(f->out.u.name, m | O_EXCL, f->fa.mode)) < 0 &&
(errno != EEXIST || (m & O_EXCL))) {
- fw_log(-1, "[%s] couldn't create `%s': %s",
+ fw_log(NOW, "[%s] couldn't create `%s': %s",
desc, f->out.u.name, strerror(errno));
REFFD_DEC(in);
return (0);
if (fd != -1) {
if (fattr_apply(f->out.u.name, &f->fa)) {
- fw_log(-1, "[%s] couldn't apply file attributes to `%s': %s",
+ fw_log(NOW, "[%s] couldn't apply file attributes to `%s': %s",
desc, f->out.u.name, strerror(errno));
}
} else if ((fd = open(f->out.u.name, m & ~O_CREAT)) < 0) {
- fw_log(-1, "[%s] couldn't open `%s': %s",
+ fw_log(NOW, "[%s] couldn't open `%s': %s",
desc, f->out.u.name, strerror(errno));
REFFD_DEC(in);
return (0);
return (0);
fs = CREATE(fsource);
fs->s.ops = &fsource_ops;
+ fs->s.ref = 1;
+ fs->s.f = 0;
fs->s.desc = 0;
file_read(&fs->f, sc);
return (&fs->s);
/* --- And then create the endpoints --- */
if ((ee = t->ops->create(t, fs->s.desc)) == 0)
- goto tidy;
+ return;
if ((e = file_endpt(&fs->f, fs->s.desc)) == 0) {
ee->ops->close(ee);
- goto tidy;
+ return;
}
- endpt_join(e, ee);
-
- /* --- Dispose of the source and target now --- */
-
-tidy:
- t->ops->destroy(t);
- fsource_destroy(&fs->s);
+ endpt_join(e, ee, fs->s.desc);
}
/* --- @destroy@ --- */
source_ops fsource_ops = {
"file",
- fsource_option, fsource_read, fsource_attach, fsource_destroy
+ fsource_option, fsource_read, fsource_attach, 0, fsource_destroy
};
/*----- File target description -------------------------------------------*/
return (0);
ft = CREATE(ftarget);
ft->t.ops = &ftarget_ops;
+ ft->t.ref = 1;
file_read(&ft->f, sc);
file_desc(&ft->f, &d);
ft->t.desc = xstrdup(d.buf);