We can now override the docfile location.
[super-star-trek.git] / sst.c
diff --git a/sst.c b/sst.c
index af932a0dbcb000c7e1e3ba7f260ece91f3cda2bc..ae209e2cb373ed7cf7ad8a156e1fbf01b55bc092 100644 (file)
--- a/sst.c
+++ b/sst.c
@@ -6,11 +6,16 @@
 #include <dos.h>\r
 #endif\r
 #include <time.h>\r
+\r
+#ifndef SSTDOC\r
+#define SSTDOC "sst.doc"\r
+#endif\r
        \r
 int getch(void);\r
 \r
 static char line[128], *linep = line;\r
 static int linecount;  /* for paging */\r
+static int screenheight = 24;\r
 \r
 static void clearscreen(void);\r
 \r
@@ -103,7 +108,7 @@ static void listCommands(int x) {
 \r
 static void helpme(void) {\r
        int i, j;\r
-       char cmdbuf[32];\r
+       char cmdbuf[32], *cp;\r
        char linebuf[132];\r
        FILE *fp;\r
        /* Give help on commands */\r
@@ -130,37 +135,42 @@ static void helpme(void) {
                strcpy(cmdbuf, " ABBREV");\r
        }\r
        else {\r
-               strcpy(cmdbuf, "  Mnemonic:  ");\r
-               j = 0;\r
-               while ((cmdbuf[j+13] = toupper(commands[i][j])) != 0) j++;\r
+           for (j = 0; commands[i][j]; j++)\r
+               cmdbuf[j] = toupper(commands[i][j]);\r
+           cmdbuf[j] = '\0';\r
        }\r
-       fp = fopen("sst.doc", "r");\r
+       fp = fopen(SSTDOC, "r");\r
        if (fp == NULL) {\r
                prout("Spock-  \"Captain, that information is missing from the");\r
                prout("   computer. You need to find SST.DOC and put it in the");\r
                prout("   current directory.\"");\r
                return;\r
        }\r
-       i = strlen(cmdbuf);\r
-       do {\r
-               if (fgets(linebuf, 132, fp) == NULL) {\r
+       for (;;) {\r
+           if (fgets(linebuf, sizeof(linebuf), fp) == NULL) {\r
                        prout("Spock- \"Captain, there is no information on that command.\"");\r
                        fclose(fp);\r
                        return;\r
                }\r
-       } while (strncmp(linebuf, cmdbuf, i) != 0);\r
+           if (linebuf[0] == '%' && linebuf[1] == '%'&& linebuf[2] == ' ') {\r
+               for (cp = linebuf+3; isspace(*cp); cp++)\r
+                       continue;\r
+               linebuf[strlen(linebuf)-1] = '\0';\r
+               if (strcmp(cp, cmdbuf) == 0)\r
+                   break;\r
+           }\r
+       }\r
 \r
        skip(1);\r
        prout("Spock- \"Captain, I've found the following information:\"");\r
        skip(1);\r
 \r
-       do {\r
-               if (linebuf[0]!=12) { // ignore page break lines \r
-                       linebuf[strlen(linebuf)-1] = '\0'; // No \n at end\r
-                       prout(linebuf);\r
-               }\r
-               fgets(linebuf,132,fp);\r
-       } while (strstr(linebuf, "******")==NULL);\r
+       while (fgets(linebuf, sizeof(linebuf),fp)) {\r
+               if (strstr(linebuf, "******"))\r
+                       break;\r
+               /* use fputs here to avoid % expansion */\r
+               fputs(linebuf, stdout);\r
+       }\r
        fclose(fp);\r
 }\r
 \r
@@ -350,6 +360,10 @@ int main(int argc, char **argv) {
        int hitme;\r
        char ch;\r
        prelim();\r
+       char *LINES = getenv("LINES");\r
\r
+       if (LINES)\r
+           screenheight = atoi(LINES);\r
 \r
        if (argc > 1) {\r
                fromcommandline = 1;\r
@@ -408,17 +422,20 @@ void cramen(int i) {
        proutn(s);\r
 }\r
 \r
-void cramlc(int key, int x, int y) {\r
-       if (key == 1) proutn(" Quadrant");\r
-       else if (key == 2) proutn(" Sector");\r
-       proutn(" %d - %d", x, y);\r
+char *cramlc(enum loctype key, int x, int y) {\r
+       static char buf[32];\r
+       buf[0] = '\0';\r
+       if (key == quadrant) strcpy(buf, "Quadrant ");\r
+       else if (key == sector) strcpy(buf, "Sector ");\r
+       sprintf(buf+strlen(buf), "%d-%d", x, y);\r
+       return buf;\r
 }\r
 \r
 void crmena(int i, int enemy, int key, int x, int y) {\r
        if (i == 1) proutn("***");\r
        cramen(enemy);\r
        proutn(" at");\r
-       cramlc(key, x, y);\r
+       proutn(cramlc(key, x, y));\r
 }\r
 \r
 void crmshp(void) {\r
@@ -528,12 +545,6 @@ int ja(void) {
        }\r
 }\r
 \r
-void cramf(double x, int w, int d) {\r
-       char buf[64];\r
-       sprintf(buf, "%*.*f", w, d, x);\r
-       proutn(buf);\r
-}\r
-\r
 double square(double i) { return i*i; }\r
                                                                        \r
 static void clearscreen(void) {\r
@@ -575,7 +586,7 @@ void pause(int i) {
 void skip(int i) {\r
        while (i-- > 0) {\r
                linecount++;\r
-               if (linecount >= 23)\r
+               if (linecount >= screenheight)\r
                        pause(0);\r
                else\r
                        putchar('\n');\r
@@ -680,7 +691,7 @@ void debugme(void) {
                                case FSCMOVE: proutn("SC Move         "); break;\r
                                case FSCDBAS: proutn("SC Base Destroy "); break;\r
                        }\r
-                       cramf(future[i]-game.state.date, 8, 2);\r
+                       proutn("%.2f", future[i]-game.state.date);\r
                        chew();\r
                        proutn("  ?");\r
                        key = scan();\r