Skip to content

Commit

Permalink
Separate app options from fossil options, and use real second based t…
Browse files Browse the repository at this point in the history
…imeout
  • Loading branch information
wwiv committed May 27, 2021
1 parent 1bacd96 commit 69ea631
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 33 deletions.
29 changes: 20 additions & 9 deletions dos/wwivfoss/fossil.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "fossil.h"

#include "dostypes.h"
#include "pipe.h"
#include "util.h"

#include <conio.h>
#include <ctype.h>
#include <dos.h>
Expand Down Expand Up @@ -45,7 +47,7 @@ static char m[20];
static Pipe __far * pipe = NULL;
static int char_avail = 0;

char __far * fossil_info_ident = "WWIV Named Pipes OS/2 FOSSIL Runner";
char __near* fossil_info_ident = "WWIV FOSSIL runner for Win32 and OS/2 Named Pipes";

#pragma pack(1)
struct fossil_info {
Expand All @@ -65,6 +67,9 @@ struct fossil_info {

static struct fossil_info info;

FossilOptions::FossilOptions() : comport(0), node_number(0), open_timeout_seconds(10) {}
FossilOptions::~FossilOptions() {}

static unsigned status() {
unsigned r = STATUS_BASE;
if (!pipe->is_open() || !carrier) {
Expand Down Expand Up @@ -232,14 +237,18 @@ void __interrupt __far int14_sig() {
char pad[10] = {0};
}

void enable_fossil(int nodenum, int comport) {
bool enable_fossil(const FossilOptions* options) {
if (!options) {
log("NULL options passed to enable_fossil.");
abort();
}
char pipename[200];
sprintf(pipename, "\\PIPE\\WWIV%d", nodenum);
pipe = new __far Pipe(pipename);
sprintf(pipename, "\\PIPE\\WWIV%d", options->node_number);
log("Opening pipe: '%s'", pipename);
pipe = new __far Pipe(pipename, options->open_timeout_seconds);
if (!pipe->is_open()) {
log("Unable to open FOSSIL log: wwivfoss.log");
} else {
log("Pipe Opened");
log("Failed to open pipe: '%s'", pipename);
return false;
}
// This seems to make it work consistently with the test app.
os_yield();
Expand Down Expand Up @@ -279,12 +288,13 @@ void enable_fossil(int nodenum, int comport) {
os_yield();

log("FOSSIL Enabled. Pipe Handle:[%d]", pipe_handle);
return true;
}

void disable_fossil() {
bool disable_fossil() {
if (!fossil_enabled) {
log("ERROR: disable_fossil called when not enabled.");
return;
return false;
}

_disable();
Expand All @@ -304,5 +314,6 @@ void disable_fossil() {
log("Closed pipe");
delete pipe;
pipe = NULL;
return true;
}

16 changes: 14 additions & 2 deletions dos/wwivfoss/fossil.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#ifndef INCLUDED_FOSSIL_H
#define INCLUDED_FOSSIL_H

#include "dostypes.h"

#define ENABLE_LOG
#define FOSSIL_BUFFER_SIZE 4000

void enable_fossil(int nodenum, int comport);
void disable_fossil();
class FossilOptions {
public:
FossilOptions();
~FossilOptions();

int comport;
int node_number;
int open_timeout_seconds;
};

bool enable_fossil(const FossilOptions* options);
bool disable_fossil();

#endif // INCLUDED_FOSSIL_H
17 changes: 8 additions & 9 deletions dos/wwivfoss/pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>

#pragma warning(disable : 4505)


// See http://www.delorie.com/djgpp/doc/rbinter/ix/21/5F.html
Expand Down Expand Up @@ -130,17 +133,16 @@ int DosPeekNmPipe(int handle) {
}


Pipe::Pipe(const char* fn) __far {
Pipe::Pipe(const char* fn, int timeout_secs) __far {
next_char_ = -1;
control_handle_ = -1;

int count = 0;
int h = -1;
while (handle_ == -1 && count++ < 100 ) {
clock_t start_time = clock();
while (handle_ == -1 && ((clock() - start_time) / CLOCKS_PER_SEC) < timeout_secs) {
if (_dos_open(fn, _O_RDWR, &h) != 0) {
log("ERROR: (Pipe) Unable to open pipe: '%s'", fn);
handle_ = -1;
os_yield();
sleep(100);
} else {
handle_ = h;
Expand All @@ -156,9 +158,9 @@ Pipe::Pipe(const char* fn) __far {

char control_fn[81];
sprintf(control_fn, "%sC", fn);
count = 0;
start_time = clock();
h = -1;
while (control_handle_ == -1 && count++ < 100 ) {
while (control_handle_ == -1 && ((clock() - start_time) / CLOCKS_PER_SEC) < timeout_secs) {
if (_dos_open(control_fn, _O_RDWR, &h) == 0) {
control_handle_ = h;
os_yield();
Expand Down Expand Up @@ -190,21 +192,18 @@ char Pipe::control_code() __far {
void Pipe::close() __far {
log("Pipe::close()");
if (handle_ != -1) {
log("Pipe::close(): closing data");
_dos_close(handle_);
handle_ = -1;
}

if (control_handle_ != -1) {
log("Pipe::close(): closing control");
_dos_close(control_handle_);
control_handle_ = -1;
}
os_yield();
}

Pipe::~Pipe() __far {
log("Pipe::~Pipe()");
close();
}

Expand Down
2 changes: 1 addition & 1 deletion dos/wwivfoss/pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class __far Pipe {
* N.B: The pipe is expected to be created before installing a TSR
* or interrupt handler.
*/
Pipe(const char* fn);
Pipe(const char* fn, int timeout_secs);
/** Destroys the pipe, closing if needed */
~Pipe();

Expand Down
28 changes: 16 additions & 12 deletions dos/wwivfoss/wwivfoss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

class App {
public:
App() : comport(0), node_number(0), cmdline(20) {}
App() : opts(), cmdline(20) {}
~App() {}

int comport;
int node_number;
FossilOptions opts;
Array cmdline;
};

Expand All @@ -38,10 +37,12 @@ static void show_help() {
cerr << endl;
}

extern char __near* fossil_info_ident;

int main(int argc, char** argv) {
cerr << "WWIVFOSS - WWIV FOSSIL runner for Win32 and OS/2 Named Pipes\r\n";
cerr << " Copyright (c) 2021, WWIV Software Services\r\n";
cerr << " Built: " << __DATE__ << ", " << __TIME__ << "\r\n" << endl;
cerr << "WWIVFOSS: " << ((char __near *)fossil_info_ident) << "\r\n";
cerr << " Copyright (c) 2021, WWIV Software Services\r\n";
cerr << " Built: " << __DATE__ << ", " << __TIME__ << "\r\n" << endl;

App app;
int had_positional = 0;
Expand All @@ -58,17 +59,17 @@ int main(int argc, char** argv) {
continue;
}
// Process switch
char schar = *(arg+1);
char schar = (char)toupper(*(arg+1));
const char* sval = (arg+2);
// cerr << "Switch: " << schar << "; value: '" << sval << "'" << endl;
switch (schar) {
case 'N': {
// Node number
app.node_number = atoi(sval);
app.opts.node_number = atoi(sval);
} break;
case 'P': {
// Node number
app.comport = atoi(sval);
app.opts.comport = atoi(sval);
} break;
case '?': {
// Help
Expand All @@ -84,19 +85,22 @@ int main(int argc, char** argv) {
}

// Args parsed, do something.
if (app.node_number < 1) {
if (app.opts.node_number < 1) {
cerr << "Node number not specified. Exiting.\r\n";
show_help();
return 1;
}

enable_fossil(app.node_number, app.comport);
if (!enable_fossil(&app.opts)) {
cerr << "Failed to initialize FOSSIL support." << endl;
return 2;
}
int ret = _spawnvp(_P_WAIT, app.cmdline.at(0),
(const char**) app.cmdline.items());
disable_fossil();
if (ret < 0) {
cerr << "Error spawning process. " << endl;
return 1;
return 3;
}
return 0;
}

0 comments on commit 69ea631

Please sign in to comment.