瀏覽代碼

Working on sleep. Who knew?

Steve Thielemann 5 年之前
父節點
當前提交
44ce7bac20
共有 1 個文件被更改,包括 318 次插入2 次删除
  1. 318 2
      mystic.c

+ 318 - 2
mystic.c

@@ -1,6 +1,6 @@
 #include <stdio.h>
 #include <string.h>
-#include <unistd.h>
+#include <unistd.h> // usleep()
 
 #include <pty.h>
 #include <termios.h>
@@ -15,11 +15,16 @@
 #include <strings.h>    // strcasecmp
 
 #include <stdlib.h>     // random()
+#include <ctype.h>
 
 #define TARGET "./mySTIC"
 #define BSIZE 1024
 #define LOGGING
 
+#ifdef LOGGING
+FILE * fp;
+#endif
+
 const char * it_is_now(void) {
     static char buffer[100];
     time_t timer;
@@ -39,8 +44,301 @@ void slow_write(int fd, int speed, char * buffer, int len) {
     }
 }
 
+struct render {
+    int speed;
+    int effect;
+} current_render;
+
+int render_overlimit = 0;
+
+void reset_render(void) {
+    current_render.speed = 0;
+    current_render.effect = 0;
+    render_overlimit = 0;
+}
+
+
+#define TRIGGER "^"
+#define SLEEP_LIMIT 30
+
+int ms_sleep(unsigned int ms) {
+    int result = 0;
+    struct timespec ts = {
+        ms / 1000, 
+        (ms % 1000) * 1000000L
+    };
+
+    do {
+        struct timespec ts_sleep = ts;
+        result = nanosleep(&ts_sleep, &ts);
+    } while ( (-1 == result));
+    return result;
+}
+
+void nsleep(long ns) {
+    struct timespec ts;
+    ts.tv_sec = 0;
+    ts.tv_nsec = ns;
+    nanosleep(&ts, &ts);
+}
+
+void render_sleep(void) {
+    if (render_overlimit)
+        return;
+
+    if (current_render.speed) {
+        // usleep(current_render.speed * 2500);
+        // nsleep( current_render.speed * 250000l);
+        ms_sleep(current_render.speed * 100);
+
+        // nanosleep(current_render.speed * 2500);
+    }
+}
+
+const char * process_trigger(int fd, const char * cp) {
+    char ch;
+    int i, x, y;
+    ch = toupper(*cp);
+    cp++;
+
+    switch(ch) {
+        case 'D':
+            i = 0;
+            if (isdigit(*cp)) {
+                i = (*cp) - '0';
+                cp++;
+            };
+            if (isdigit(*cp)) {
+                i *= 10;
+                i += (*cp) - '0';
+                cp++;
+            };
+
+            if ((i > 0) && (i < 80)) {
+#ifdef LOGGING
+                fprintf(fp, "DEL %02d\n", i);
+#endif                
+                for (x = 0; x < i; x++ ) {
+                    write(fd, "\b \b", 3);
+                }
+            };
+            break;
+
+        case 'C':
+            i = 0;
+            if (isdigit(*cp)) {
+                i = (*cp) - '0';
+                cp++;
+            };
+            if (isdigit(*cp)) {
+                i *= 10;
+                i += (*cp) - '0';
+                cp++;
+            };
+            break;
+
+        case 'G':
+            x = 0;
+            if (isdigit(*cp)) {
+                x = (*cp) - '0';
+                cp++;
+            };
+            if (isdigit(*cp)) {
+                x *= 10;
+                x += (*cp) - '0';
+                cp++;
+            };
+            y = 0;
+            if (isdigit(*cp)) {
+                y = (*cp) - '0';
+                cp++;
+            };
+            if (isdigit(*cp)) {
+                y *= 10;
+                y += (*cp) - '0';
+                cp++;
+            };
+            break;
+
+        case 'R':
+            i = 0;
+            if (isdigit(*cp)) {
+                i = (*cp) - '0';
+                cp++;
+            };
+            if ( ( i > 0 ) && ( i < 10) ) {
+#ifdef LOGGING
+                fprintf(fp, "RENDER %d\n", i);
+#endif                
+
+                current_render.effect = i;
+            } else {
+                current_render.effect = 0;
+            }
+            break;
+        case 'S':
+            i = 0;
+            if (isdigit(*cp)) {
+                i = (*cp) - '0';
+                cp++;
+            };
+            if ( ( i > 0 ) && ( i < 10) ) {
+#ifdef LOGGING
+                fprintf(fp, "SPEED %d\n", i);
+#endif                
+
+                current_render.speed = i;
+            } else {
+                current_render.speed = 0;
+            }
+            break;
+        case 'P':
+            i = 0;
+            if (isdigit(*cp)) {
+                i = (*cp) - '0';
+                cp++;
+            };
+            if ( ( i > 0 ) && ( i < 10) ) {
+#ifdef LOGGING
+                fprintf(fp, "PAWS %d\n", i);
+#endif                
+                // sleep(i);
+
+                if (!render_overlimit) {
+                    sleep(i);
+
+                    // nsleep(i * 10000l);
+                    /*
+                    struct timespec ts;
+                    ts.tv_sec = 0;
+                    ts.tv_nsec = i * 10000;
+                    nanosleep(&ts, &ts);*/
+                };
+            }
+            break;
+        
+    }
+    return cp;
+}
+
+void render_effect(int fd, char ch) {
+    int effect = current_render.effect;
+    
+    render_sleep();
+    int x = write(fd, &ch, 1);
+#ifdef LOGGING
+    fprintf(fp, "write %c to %d result: %d\n", ch, fd, x);
+    fflush(fp);
+#endif
+
+    return;
+
+    switch (effect) {
+        case 0: 
+        default:
+            write(fd, &ch, 1); 
+            break;
+    }
+}
+
+void render( int fd, const char * string_out ) {
+    const char * cp = string_out;
+    const char * trigger = cp;
+    time_t start = time(NULL);
+    int elapsed;
+    int over = 0;
+
+    reset_render();
+
+#ifdef LOGGING
+    fprintf(fp, "render(%d, %s)\n", fd, string_out);
+    fflush(fp);
+#endif
+
+    // Check our time from time to time.
+    // If we start running long, kill sleeps.
+
+    while ( (trigger = strstr(cp, TRIGGER)) != NULL) {
+        // There is special things to handle in here.
+        while ( cp != trigger ) {
+            elapsed = time(NULL)-start;
+            if (elapsed > SLEEP_LIMIT) {
+                render_overlimit = 1;
+                current_render.speed = 0;
+            };
+
+#ifdef LOGGING
+            fprintf(fp, "re(%c)\n", *cp);
+            fflush(fp);
+#endif
+            // write(fd, cp, 1 );
+            render_effect(fd, *cp);
+            cp++;
+        };
+
+#ifdef LOGGING
+        fprintf(fp, "at trigger: (%s)\n", cp);
+        fflush(fp);
+#endif
+
+        cp += strlen(TRIGGER);
+
+        // Ok, we're pointing at the trigger -- do something.
+        cp = process_trigger(fd, cp);
+
+#ifdef LOGGING
+        fprintf(fp, "after trigger: (%s)\n", cp);
+        fflush(fp);
+#endif
+
+    };
+
+    // We still might be under a rendering effect.
+    while (*cp != 0) {
+        // write(fd, cp, 1);
+        render_effect(fd, *cp);
+        cp++;
+    }
+}
+
 void harry_event(int fd) {
     // Make something happen
+    char buffer[100];
+    int r = random() % 5;
+
+    const char * phrases[] = 
+    {
+        "Hahaha",
+        "Snicker, snicker",
+        "Boo!",
+        "MeOW",
+        "I see U"
+    };
+    const char * cp;
+
+#ifdef LOGGING
+    fprintf(fp, "harry_event(%d)\n", fd);
+    fflush(fp);
+#endif
+
+    cp = phrases[r];
+
+    sprintf(buffer, "^S2%s^P2^D%02d", cp, (int)strlen(cp));
+    // write(fd, )  is working just fine here!
+    // write(fd, buffer, strlen(buffer));
+
+#ifdef LOGGING
+    fprintf(fp, "harry_event: render(%d, \"%s\")\n", fd, buffer);
+    fflush(fp);
+#endif
+
+    render(fd, buffer);
+    // render(fd, cp);
+
+    // This also works just fine!
+    //write(fd, buffer, strlen(buffer));
+
+    /*
     char clear[] = "\b \b";
     int x;
     char haha[] = "Hahaha hahaha!!!";
@@ -52,6 +350,7 @@ void harry_event(int fd) {
     for( x = 0; x < strlen(haha); x++ ) {
         write(fd, clear, strlen(clear) );
     };
+    */
 }
 
 /* 
@@ -109,7 +408,7 @@ int main(int argc, char * argv[])
     pid_t pid;
     
 #ifdef LOGGING    
-    FILE * fp;
+    // FILE * fp;
 
     fp = fopen("mystic_pipe.data", "wb");
     if ( fp == NULL ) {
@@ -120,6 +419,22 @@ int main(int argc, char * argv[])
     srandom(time(NULL));
 
     // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz -PUWISHPASSWORD
+    // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
+
+    // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz -PUP2LAT3
+    // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
+
+    // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
+    // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML0 -SL1 -ST0 -CUnknown
+
+    // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz -PUP2LAT3
+    // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML1 -SL1 -ST2 -CUnknown -Ubugz -PUP2LAT3
+
+    // SSH:  -ML1 -ST2
+    // Telnet: -ML0 -ST0
+
+
+    // 
 
     // -U<username>
     for (int x = 0; x < argc; x++) {
@@ -219,6 +534,7 @@ int main(int argc, char * argv[])
                 harry_event(STDOUT_FILENO);
 #ifdef LOGGING
                 fprintf(fp, "%s : TICK\n", it_is_now());
+                fprintf(fp, "STDOUT is %d\n", STDOUT_FILENO);
 #endif                
             }