diff --git a/main.c b/main.c index 76e0810..c983e39 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,8 @@ #pragma optimize 79 #include +#include +#include #include #include @@ -16,10 +18,44 @@ enum { kLoaded = 1, kStarted = 2, - kConnected = 4 + kConnected = 4, + + kLoadError = -1, + kVersionError = -2 }; -Word StartUp(displayPtr fx) +int StartUpTZ(void) +{ + Word status; + Word flags = 0; + + status = tiStatus(); + + if (_toolErr) + { + LoadOneTool(0x38, 0x104); + if (_toolErr == toolVersionErr) return kVersionError; + if (_toolErr) return kLoadError; + + status = 0; + flags |= kLoaded; + } + + if (tiVersion() < 0x0104) + { + return kVersionError; + } + + if (!status) + { + tiStartUp(); + flags |= kStarted; + } + + return flags; +} + +int StartUpTCP(displayPtr fx) { word status; word flags = 0; @@ -31,7 +67,8 @@ Word StartUp(displayPtr fx) if (_toolErr) { LoadOneTool(54, 0x0300); - if (_toolErr) return -1; + if (_toolErr == toolVersionErr) return kVersionError; + if (_toolErr) return kLoadError; status = 0; flags |= kLoaded; @@ -43,14 +80,14 @@ Word StartUp(displayPtr fx) { if (flags & kLoaded) UnloadOneTool(54); - return -1; - } + return kVersionError; + } if (!status) { TCPIPStartUp(); - if (_toolErr) return -1; + if (_toolErr) return kLoadError; flags |= kStarted; } @@ -64,22 +101,32 @@ Word StartUp(displayPtr fx) return flags; } -void ShutDown(word flags, Boolean force, displayPtr fx) +void ShutDownTZ(int flags) { - if (flags & kConnected) - { - TCPIPDisconnect(force, fx); - if (_toolErr) return; - } - if (flags & kStarted) - { - TCPIPShutDown(); - if (_toolErr) return; - } - if (flags & kLoaded) - { - UnloadOneTool(54); - } + if (flags <= 0) return; + + if (flags & kStarted) tiShutDown(); + if (flags & kLoaded) UnloadOneTool(0x38); +} + +void ShutDownTCP(int flags, Boolean force, displayPtr fx) +{ + if (flags <= 0) return; + + if (flags & kConnected) + { + TCPIPDisconnect(force, fx); + if (_toolErr) return; + } + if (flags & kStarted) + { + TCPIPShutDown(); + if (_toolErr) return; + } + if (flags & kLoaded) + { + UnloadOneTool(54); + } } @@ -128,7 +175,8 @@ char *get_url_filename(const char *cp, URLComponents *components) int main(int argc, char **argv) { int i; - Word mf; + int mf; + int tf; int x; @@ -145,16 +193,29 @@ int main(int argc, char **argv) return 1; } - - mf = StartUp(NULL); - if (mf == -1) + tf = StartUpTZ(); + + if (tf < 0) { + fprintf(stderr, "Time Tool 1.0.4 or greater is required.\n"); + exit(1); + } + + + mf = StartUpTCP(NULL); + + if (mf < 0) + { + ShutDownTZ(tf); fprintf(stderr, "Marinetti 3.0b3 or greater is required.\n"); exit(1); } + + + if (argc == 1) { const char *url; @@ -189,7 +250,8 @@ int main(int argc, char **argv) } } - ShutDown(mf, false, NULL); + ShutDownTCP(mf, false, NULL); + ShutDownTZ(tf); return 0; } diff --git a/makefile.mk b/makefile.mk index f694fda..9aada29 100644 --- a/makefile.mk +++ b/makefile.mk @@ -1,6 +1,7 @@ CFLAGS += $(DEFINES) -v -w OBJS = main.o gopher.o url.o connection.o readline2.o scheme.o ftype.o setftype.o \ - s16debug.o common.o http.o http.utils.o dictionary.o flags.o + s16debug.o common.o http.o http.utils.o dictionary.o flags.o \ + time.o gopher: $(OBJS) $(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@ @@ -34,6 +35,8 @@ setftype.o: setftype.c scheme.o: scheme.c url.h ftype.o: ftype.c +time.o: time.c + s16debug.o: s16debug.c s16debug.h # tests diff --git a/prototypes.h b/prototypes.h index aefa764..05a9f9d 100644 --- a/prototypes.h +++ b/prototypes.h @@ -29,4 +29,10 @@ int do_gopher(const char *url, URLComponents *components); int do_http(const char *url, URLComponents *components); #endif +#ifdef __TYPES__ +void tiTimeRec2ISO8601(const TimeRecPtr t, char *str); +void tiTimeRec2GMTString(const TimeRecPtr t, char *str); +#endif + + #endif diff --git a/time.c b/time.c new file mode 100644 index 0000000..6a0c52e --- /dev/null +++ b/time.c @@ -0,0 +1,132 @@ +#pragma noroot +#pragma optimize -1 +#pragma lint -1 +#pragma debug 0x8000 + +#include +#include +#include + +/* + * From Silver Platter. + * + */ + +// yyyy-mm-ddThh:mm:ssZ +void tiTimeRec2ISO8601(const TimeRecPtr t, char *str) +{ +LongWord secs; +tiPrefRec tiPrefs; +TimeRec tr; + + + tiPrefs.pCount = 3; + tiGetTimePrefs(&tiPrefs); + + secs = ConvSeconds(TimeRec2Secs, 0, (Pointer)t); + secs += tiPrefs.secOffset; + + ConvSeconds(secs2TimeRec, secs, (Pointer)&tr); + + str[0] = 20; + + // yyyy- + Int2Dec(tr.year + 1900, &str[1], 4, 0); + str[5] = '-'; + + // mm- + Int2Dec(tr.month + 1, &str[6], 2, 0); + str[6] |= 0x10; // convert ' ' -> '0' + str[8] = '-'; + + // ddT + Int2Dec(tr.day + 1, &str[9], 2, 0); + str[9] |= 0x10; // convert ' ' -> '0' + str[11] = 'T'; + + // hh: + Int2Dec(tr.hour, &str[12], 2, 0); + str[12] |= 0x10; // convert ' ' -> '0' + str[14] = ':'; + + // mm: + Int2Dec(tr.minute, &str[15], 2, 0); + str[15] |= 0x10; // convert ' ' -> '0' + str[17] = ':'; + + // ss: + Int2Dec(tr.second, &str[18], 2, 0); + str[18] |= 0x10; // convert ' ' -> '0' + str[20] = 'Z'; + +} + +void tiTimeRec2GMTString(const TimeRecPtr t, char *str) +{ +static const char weekday[] = "Sun,Mon,Tue,Wed,Thu,Fri,Sat,"; +static const char month[] = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec "; + +int i; +LongWord secs; + +tiPrefRec tiPrefs; +TimeRec tr; + + tiPrefs.pCount = 3; + tiGetTimePrefs(&tiPrefs); + + secs = ConvSeconds(TimeRec2Secs, 0, (Pointer)t); + + secs += tiPrefs.secOffset; + +#if 0 + //add daylight savings time... + if (ReadBParam(0x5e) & 0x02 == 0) secs += 3600; +#endif + + ConvSeconds(secs2TimeRec, secs, (Pointer)&tr); + + str[0] = 29; + + i = (tr.weekDay - 1) << 2; + + // Day of week + str[1] = weekday[i++]; + str[2] = weekday[i++]; + str[3] = weekday[i++]; + str[4] = weekday[i++]; + str[5] = ' '; + + // day + Int2Dec(tr.day + 1, &str[6], 2, 0); + str[6] |= 0x10; + + str[8] = ' '; + + i = tr.month << 2; + str[9] = month[i++]; + str[10] = month[i++]; + str[11] = month[i++]; + str[12] = month[i++]; + + // year + Int2Dec(tr.year + 1900, &str[13], 4, 0); + str[17] = ' '; + + Int2Dec(tr.hour, &str[18], 2, 0); + str[18] |= 0x10; + str[20] = ':'; + + Int2Dec(tr.minute, &str[21], 2, 0); + str[21] |= 0x10; + str[23] = ':'; + + Int2Dec(tr.second, &str[24], 2, 0); + str[24] |= 0x10; + + str[26] = ' '; + str[27] = 'G'; + str[28] = 'M'; + str[29] = 'T'; + +}