Browse Source

Added word_wrangler.

It transposes letters.
I also added some random to word_mangler.
It now picks something to do to the words,
and it will randomly not process some letters.
bugz 4 years ago
parent
commit
5790d479fb
1 changed files with 88 additions and 128 deletions
  1. 88 128
      mystic.cpp

+ 88 - 128
mystic.cpp

@@ -1,10 +1,10 @@
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h> // usleep()
-
+#include <assert.h>
 #include <fcntl.h>
 #include <pty.h>
+#include <stdio.h>
+#include <string.h>
 #include <termios.h>
+#include <unistd.h> // usleep()
 
 #include <sys/select.h>
 #include <sys/wait.h>
@@ -150,39 +150,6 @@ const char *repr(const char *data) {
   return buffer;
 }
 
-#define CHUNKSIZE 40
-
-void zf_repr_chunk(int LOG_LEVEL, const char *chunk) {
-  const char *output = repr(chunk);
-  ZF_LOG_WRITE(LOG_LEVEL, _ZF_LOG_TAG, "r\"%s\"", output);
-}
-
-void zf_repr(int LOG_LEVEL, const char *string) {
-  // Chunks -- 32?
-  char buffer[CHUNKSIZE + 1];
-  int pos = 0;
-  int len = strlen(string);
-  while (len > CHUNKSIZE) {
-    strncpy(buffer, string + pos, CHUNKSIZE);
-    buffer[CHUNKSIZE] = 0;
-    pos += CHUNKSIZE;
-    len -= CHUNKSIZE;
-    zf_repr_chunk(LOG_LEVEL, buffer);
-  };
-  if (len > 0) {
-    strcpy(buffer, string + pos);
-    zf_repr_chunk(LOG_LEVEL, buffer);
-  }
-}
-
-// Here's my idea for debugging
-#define ZF_LOGV_REPR(str) zf_repr(ZF_LOG_VERBOSE, str)
-#define ZF_LOGD_REPR(str) zf_repr(ZF_LOG_DEBUG, str)
-#define ZF_LOGI_REPR(str) zf_repr(ZF_LOG_INFO, str)
-#define ZF_LOGW_REPR(str) zf_repr(ZF_LOG_WARN, str)
-#define ZF_LOGE_REPR(str) zf_repr(ZF_LOG_ERROR, str)
-#define ZF_LOGF_REPR(str) zf_repr(ZF_LOG_FATAL, str)
-
 // END LOGGING
 
 #include "lastseen.h"
@@ -230,6 +197,10 @@ that we'll be executing and mangling?
  * This safely checks to make sure the buffer isn't overrun.
  */
 int string_insert(char *buffer, int max_length, int pos, const char *insert) {
+  assert(max_length > pos);
+  assert(buffer != NULL);
+  assert(insert != NULL);
+
   if (strlen(buffer) + strlen(insert) >= max_length) {
     ZF_LOGD("string_insert() failed inserting [%s]", repr(insert));
     return 0;
@@ -1305,7 +1276,7 @@ Do I need this??
  * We need a better randint(RANGE) code.
  */
 int random_activate(int w) {
-  int r = random() % 100;
+  int r = randint(100);
   if (r <= (w * 10)) {
     return 1;
   };
@@ -1373,14 +1344,18 @@ int word_mangler(char *buffer, int len) {
   int count = 0;
   int state;
 
-  state = word_state(buffer, len);
+  // state = word_state(buffer, len);
   // ZF_LOGD("word_state(%.*s) %d", len, buffer, state);
+  state = randrange(-1, 1);
 
   // TODO:  Transposer
 
   for (p = 0; p < len; p++) {
     char c = buffer[p];
 
+    if (randint(len) == p) {
+      break;
+    }
     switch (state) {
     case -1:
       // upper
@@ -1413,6 +1388,38 @@ int word_mangler(char *buffer, int len) {
   return count;
 }
 
+int word_wrangler(char *buffer, int len) {
+  int p;
+  int count;
+  int state;
+
+  // state = word_state(buffer, len);
+  // ZF_LOGD("word_state(%.*s) %d", len, buffer, state);
+  if (len < 5) {
+    return 0;
+  }
+
+  p = randint(len - 4) + 2;
+
+  for (count = 0; count < 4; count++) {
+    if (!isalpha(buffer[p + count]))
+      break;
+  }
+
+  ZF_LOGV_MEM(buffer, len, "wrangler %d len %d:", p, count);
+
+  if (count >= 2) {
+    for (int x = 0; x < count / 2; x++) {
+      char ch = buffer[p + x];
+      buffer[p + x] = buffer[p + count - 1 - x];
+      buffer[p + count - 1 - x] = ch;
+    }
+    ZF_LOGV_MEM(buffer, len, "word now:");
+    return 1;
+  } else
+    return 0;
+}
+
 int buffer_insert(char *buffer, int len, int max_length, int pos,
                   const char *insert) {
   if (len + strlen(insert) > max_length) {
@@ -1435,17 +1442,17 @@ int mangle(int fd, const char *buffer, int len) {
   int need_render = 0; // changing word case around doesn't need the render
   int mangled = 0;
   int mangled_chars = 0;
-  char play[2048];
+  char play[BSIZE * 2];
   const char *cp;
 
+  // Make a copy of buffer, since it can no longer be changed
+  // inserted into.
+
   memcpy(play, buffer, len);
 
   // NEVER reference buffer from this point on!
 
-  // Ok, we want to look for words to:
-  // MaNGlE , or transpose (both?!)
-  // or possibly transpose words
-  char work[2048];
+  char work[BSIZE * 2];
 
   // Use terminal - clean out ANSI
 
@@ -1500,10 +1507,6 @@ int mangle(int fd, const char *buffer, int len) {
             {"wolf", 1, 0},       {"skull", 0, 1},    {"skull2", 0, 1},
             {"guy", 0, 1},        {"blinkman", 0, 1}, {"ghost", 1, 0}};
 
-        //          bat.ans       creature.ans  goofy_head.ans  panther.ans
-        //          skull.ans       ghost.ans
-        // blinkman.ans  dog.ans       guy.ans         skull2.ans   wolf.ans
-
         static LastSeen last_files(2);
         int r;
 
@@ -1610,7 +1613,7 @@ int mangle(int fd, const char *buffer, int len) {
           ZF_LOGI_MEM(play, len, "mangle(ANSI_CLS) :");
 
           // Move the buffer so there's room for the display string.
-          if (buffer_insert(play, len, 1024, cp - play, display)) {
+          if (buffer_insert(play, len, sizeof(play), cp - play, display)) {
             len += strlen(display);
 
             // if (string_insert(buffer, BSIZE * 4, cp - buffer, display)) {
@@ -1654,35 +1657,19 @@ int mangle(int fd, const char *buffer, int len) {
     }
   }
   // fixup "work" buffer so it's a valid c string
+  // (required for regex to work.)
   work[len] = 0;
 
   ZF_LOGI_MEM(work, len, "Work now:");
-  /*
-  ZF_LOGD("work now:");
-  ZF_LOGD_REPR(work);
-  ZF_LOGD("Work Now: [%s]", repr(work));
-  */
-
-#ifdef OLD_WAY
-
-  /* work -- clear out ANSI so we don't mangle ANSI codes. */
-  x = rx_match(&ANSI, work);
-
-  if (x > 0) {
-    ZF_LOGD("found %d ANSI", x);
-    for (i = 0; i < x; i++) {
-      memset(work + rxmatch[i].rm_so, replace_with,
-             rxmatch[i].rm_eo - rxmatch[i].rm_so);
-    };
-    ZF_LOGD("Work Now: [%s]", repr(work));
-  }
-#endif
-
-  // ZF_LOGI("mangle: %s", repr(work));
 
   /*
    (random) Locate words (in work), and possibly flip them around.
    Transpose words.  Transpose case.  Transpose letters.
+
+   Ok, what would be interesting, is if we could find
+   W\x1[0;34mORDS with color changes in them, and work with them.
+   without screwing up the color changes, of course.  :P
+
    */
   x = rx_match(&WORDS, work);
   ZF_LOGD("found %d word groups", x);
@@ -1699,6 +1686,11 @@ int mangle(int fd, const char *buffer, int len) {
           mangled_chars += c;
         }
       }
+
+      if (random_activate(4)) {
+        word_wrangler(play + rxmatch[i].rm_so,
+                      rxmatch[i].rm_eo - rxmatch[i].rm_so);
+      }
     }
   }
 
@@ -1714,7 +1706,8 @@ int mangle(int fd, const char *buffer, int len) {
 
   if (need_render) {
     ZF_LOGD_MEM(play, len, "Ready to render:");
-    // ZF_LOGD("HH %d : (%d) %s", need_render, (int)strlen(buffer), repr(buffer));
+    // ZF_LOGD("HH %d : (%d) %s", need_render, (int)strlen(buffer),
+    // repr(buffer));
   } else {
     if (mangled) {
       ZF_LOGD_MEM(play, len, "Mangled %d words, %d chars:", mangled,
@@ -1938,7 +1931,7 @@ int main(int argc, char *argv[]) {
     // https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
     tcsetattr(1, TCSAFLUSH, &tios);
 
-    char buffer[1024];
+    char buffer[BSIZE + 1];
     int size = 0;
 
     for (;;) {
@@ -1983,12 +1976,12 @@ int main(int argc, char *argv[]) {
       }
 
       if (select(master + 1, &read_fd, &write_fd, &except_fd, &timeout) == 0) {
+        ZF_LOGI("TIMEOUT");
         // This means timeout!
         if (time_idle) {
-          ZF_LOGI("TIMEOUT");
           harry_idle_event(STDOUT_FILENO);
         } else {
-          ZF_LOGI_MEM(buffer, size, "TIMEOUT size=%d", size);
+          ZF_LOGV_MEM(buffer, size, "TIMEOUT buffer size=%d", size);
           console_receive(&console, buffer, size);
           write(STDOUT_FILENO, buffer, size);
           size = 0;
@@ -2008,17 +2001,10 @@ int main(int argc, char *argv[]) {
 
         if ((total = read(master, buffer + size, BSIZE - size)) != -1) {
           // Ok, we've read more into the buffer.
-          // ZF_LOGD("total = %d", total);
-          /*
-          // this ... isn't working.
-          ZF_LOGD_MEM(buffer, total, "Buffer now:");
-          write(STDOUT_FILENO, buffer, total);
-          size = 0;
-          */
 
-          ZF_LOGD_MEM(buffer + size, total, "Read %d bytes:", total);
+          ZF_LOGV_MEM(buffer + size, total, "Read %d bytes:", total);
           size += total;
-          ZF_LOGD_MEM(buffer, size, "Buffer now:");
+          ZF_LOGV_MEM(buffer, size, "Buffer now:");
           int pos = rstrnstr(buffer, size, "\r\n");
           if (pos >= 0) {
             // found something!
@@ -2028,59 +2014,33 @@ int main(int argc, char *argv[]) {
             memmove(buffer, buffer + pos, size - pos);
             size -= pos;
           } else {
-            ZF_LOGD("position of /r/n not found.");
+            ZF_LOGV("position of /r/n not found.");
           }
 
         } else
           break;
       }
 
-#ifdef OLD_CRUFTY
-      if ((total = read(master, &output, BSIZE)) != -1) {
-        // e escreva isso na saida padrao
-        output[total] = 0;
-
-        // if ( harry_happens( &last_event, 5)) {
-        if (1) {
-          ZF_LOGI("harry_happens");
-          if (mangle(STDOUT_FILENO, output) == 0) {
-            // failed, so.  Try again.
-            last_event = 0;
-          }
-        } else {
-          write(STDOUT_FILENO, &output, total);
-          // This is OUTPUT from the BBS
-          // ZF_LOGI( ">> %s", repr(output));
-          ZF_LOGI(">> %d chars", (int)strlen(output));
-          //   I think repr is flipping out here.  :(
-          // ZF_LOGI( ">> %d", (int)strlen(repr(output)));
-        }
+      // read_fd esta atribuido com a entrada padrao?
+      if (FD_ISSET(STDIN_FILENO, &read_fd)) {
+        // leia a entrada padrao
+        char input[BSIZE];
+        int r = read(STDIN_FILENO, &input, BSIZE);
+        input[r] = 0;
+        // e escreva no bc
+        ZF_LOGI("<< %s", repr(input));
 
-      } else
-        break;
-    }
-#endif
-
-    // read_fd esta atribuido com a entrada padrao?
-    if (FD_ISSET(STDIN_FILENO, &read_fd)) {
-      // leia a entrada padrao
-      char input[BSIZE];
-      int r = read(STDIN_FILENO, &input, BSIZE);
-      input[r] = 0;
-      // e escreva no bc
-      ZF_LOGI("<< %s", repr(input));
+        write(master, &input, r);
 
-      write(master, &input, r);
-
-      // This is INPUT from the USER
-      // ZF_LOGI_MEM( input, strlen(input), "<< ");
+        // This is INPUT from the USER
+        // ZF_LOGI_MEM( input, strlen(input), "<< ");
+      }
     }
-  }
 
-  // Restore terminal
-  tcsetattr(1, TCSAFLUSH, &orig1);
-  ZF_LOGD("exit");
-}
+    // Restore terminal
+    tcsetattr(1, TCSAFLUSH, &orig1);
+    ZF_LOGD("exit");
+  }
 
-return 0;
+  return 0;
 }