Kaynağa Gözat

Updated the mangler. Need terminal ANSI tracking.

We're screwing up ANSI/menus/etc.
Steve Thielemann 5 yıl önce
ebeveyn
işleme
3ae0328d1a
1 değiştirilmiş dosya ile 183 ekleme ve 37 silme
  1. 183 37
      mystic.c

+ 183 - 37
mystic.c

@@ -241,14 +241,17 @@ void console_ansi(const char *ansi) {
         } else {
           number = atoi(cp);
           if (number < 1) {
-            ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi), number);
+            ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
+                    number);
             number = 1;
           }
         };
         console.posy -= number;
         if (console.posy < 0) {
           console.posy = 0;
-          ZF_LOGD("console_ansi( %s ): attempt to move above top of screen (%d)", repr(ansi), number);            
+          ZF_LOGD(
+              "console_ansi( %s ): attempt to move above top of screen (%d)",
+              repr(ansi), number);
         }
         understood = 1;
         return;
@@ -259,7 +262,8 @@ void console_ansi(const char *ansi) {
         } else {
           number = atoi(cp);
           if (number < 1) {
-            ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi), number);
+            ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
+                    number);
             number = 1;
           }
         };
@@ -275,15 +279,16 @@ void console_ansi(const char *ansi) {
         } else {
           number = atoi(cp);
           if (number < 1) {
-            ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi), number);
+            ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
+                    number);
             number = 1;
           }
         };
         console.posx += number;
 
         // Well.  According to the "spec", the screen limits are hard
-        // If the cursor is already at the edge of the screen, this has no effect. 
-        // ^ This is *NOT* how any ANSI BBS terminal program acts!
+        // If the cursor is already at the edge of the screen, this has no
+        // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
 
         while (console.posx > 79) {
           console.posy++;
@@ -300,19 +305,20 @@ void console_ansi(const char *ansi) {
         } else {
           number = atoi(cp);
           if (number < 1) {
-            ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi), number);
+            ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
+                    number);
             number = 1;
           }
         };
         console.posx -= number;
 
         // Well.  According to the "spec", the screen limits are hard
-        // If the cursor is already at the edge of the screen, this has no effect. 
-        // ^ This is *NOT* how any ANSI BBS terminal program acts!
-        
+        // If the cursor is already at the edge of the screen, this has no
+        // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
+
         while (console.posx < 0) {
           console.posy--;
-          if (console.posy < 0 ) {
+          if (console.posy < 0) {
             console.posy = 0;
           }
           console.posx += 79;
@@ -338,7 +344,7 @@ void console_ansi(const char *ansi) {
           number = atoi(cp);
 
           cp = strchr(cp, ';');
-          if ( cp == NULL) {
+          if (cp == NULL) {
             // Missing 2nd number
             number2 = 1;
           } else {
@@ -359,7 +365,7 @@ void console_ansi(const char *ansi) {
         understood = 1;
         break;
 
-      case 'J':   
+      case 'J':
         // clear
         if (cp == last) {
           number = 0;
@@ -368,7 +374,7 @@ void console_ansi(const char *ansi) {
         };
 
         // clears ... part of the screen.
-        if (number == 2 ) {
+        if (number == 2) {
           console.posx = 0;
           console.posy = 0;
         };
@@ -377,7 +383,7 @@ void console_ansi(const char *ansi) {
 
       default:
         // unsure -- possibly not important
-        ZF_LOGD("console_ansi( %s ): ???", repr(ansi));        
+        ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
         understood = 0;
       }
     }
@@ -897,7 +903,9 @@ void harry_event(int fd) {
 
   cp = phrases[r];
   int color = random() % 16;
-  if(color == 0){ color++; } // If it's 0 let's make it 1.
+  if (color == 0) {
+    color++;
+  } // If it's 0 let's make it 1.
   sprintf(buffer, "^S2^C%02d%s^P2^D%02d", color, cp, (int)strlen(cp));
 
   ZF_LOGD("harry_event: render(%d, \"%s\")", fd, buffer);
@@ -1047,6 +1055,18 @@ Do I need this??
 
 */
 
+/**
+ * random_activate()
+ *
+ * Is a weight (1-10),
+ * tests if random number is < weight * 10.
+ *
+ * So random_activate(9) happens more frequently
+ * then random_activate(8) or lower.
+ *
+ * This probably needs to be fixed.
+ * We need a better randint(RANGE) code.
+ */
 int random_activate(int w) {
   int r = random() % 100;
   if (r <= (w * 10)) {
@@ -1055,6 +1075,109 @@ int random_activate(int w) {
   return 0;
 }
 
+/*
+  word_state():
+
+  -1 only lower
+  +1 only upper
+   0 mixed
+*/
+int word_state(const char *buffer, int len) {
+  int p;
+  int upper = 0;
+  int lower = 0;
+  int ret;
+  float pct;
+
+  for (p = 0; p < len; p++) {
+    char c = buffer[p];
+    if (isalpha(c)) {
+      if (isupper(c)) {
+        upper++;
+      };
+      if (islower(c)) {
+        lower++;
+      };
+    }
+  }
+
+  if (upper == lower) {
+    return 0;
+  }
+
+  if (upper > lower) {
+    ret = 1;
+    pct = ((float)lower / (float)upper) * 100.0;
+  } else {
+    ret = -1;
+    pct = ((float)upper / (float)lower) * 100.0;
+  }
+
+  // ZF_LOGD("So far %d with %f %%", ret, pct);
+
+  // They must be > 75%, otherwise return "mixed".
+  if (pct < 40.0) {
+    return ret;
+  }
+  return 0;
+}
+
+/*
+Given a buffer and length, mangle away.
+
+We *REALLY* want this to do something, so better run some tests.
+
+Or, maybe we don't.  This treats words consistently the same way.
+HMM.
+
+toupper, tolower, flipper
+ */
+int word_mangler(char *buffer, int len) {
+  int p;
+  int count = 0;
+  int state;
+  char c;
+
+  state = word_state(buffer, len);
+  // ZF_LOGD("word_state(%.*s) %d", len, buffer, state);
+
+  // TODO:  Transposer
+
+  for (p = 0; p < len; p++) {
+    c = buffer[p];
+
+    switch (state) {
+    case -1:
+      // upper
+      if (islower(c)) {
+        count++;
+        buffer[p] = toupper(c);
+      }
+      break;
+    case 1:
+      // lower
+      if (isupper(c)) {
+        count++;
+        buffer[p] = tolower(c);
+      }
+      break;
+    case 0:
+      // flipper
+      if (islower(c)) {
+        count++;
+        buffer[p] = toupper(c);
+      } else {
+        if (isupper(c)) {
+          count++;
+          buffer[p] = tolower(c);
+        }
+      }
+      break;
+    }
+  }
+  return count;
+}
+
 /*
  * The buffer that we've been given is much larger now.
  *
@@ -1063,6 +1186,7 @@ int mangle(int fd, char *buffer) {
   int x, i;
   int need_render = 0; // changing word case around doesn't need the render
   int mangled = 0;
+  int mangled_chars = 0;
 
   char *cp;
 
@@ -1097,9 +1221,12 @@ int mangle(int fd, char *buffer) {
       int r;
       int color;
       char display[100] = "";
-      const char* phrasing[] = {"HeHeHe", "Poof!", "Got U",
-                                "Anyone there?",
-                                };
+      const char *phrasing[] = {
+          "HeHeHe",
+          "Poof!",
+          "Got U",
+          "Anyone there?",
+      };
 
       ZF_LOGI("mangle(ANSI_CLS)");
       // sprintf( display, "^P2...");
@@ -1109,11 +1236,13 @@ int mangle(int fd, char *buffer) {
       // strcpy(display, "^P2^S301234^P15^S0^P2");
 
       r = random() % 10;
-      if(r == 8){
+      if (r == 8) {
         // Add in random text, plus color!
         r = random() % ((sizeof(phrasing) / sizeof(char *)) - 1);
         color = random() % 16;
-        if(color == 0){ color++; }
+        if (color == 0) {
+          color++;
+        }
         sprintf(display, "^P1^S3^C%02d%s^S0^P1", color, phrasing[r]);
         ZF_LOGI("mangle(ANSI_CLS): Inserted (%02d) %s", color, phrasing[r]);
       } else {
@@ -1123,7 +1252,7 @@ int mangle(int fd, char *buffer) {
       // Move the buffer so there's room for the display string.
       memmove(cp + strlen(display), cp, strlen(cp) + 1);
       strncpy(cp, display, strlen(display));
-      ZF_LOGI("mangle(ANSI_CLS): (%d) %s", (int)strlen(buffer), repr(buffer));
+      ZF_LOGI("mangle(ANSI_CLS): [%s]", repr(buffer));
       need_render = 1;
 
       /*
@@ -1147,7 +1276,7 @@ int mangle(int fd, char *buffer) {
       memset(work + rxmatch[i].rm_so, replace_with,
              rxmatch[i].rm_eo - rxmatch[i].rm_so);
     };
-    ZF_LOGD("Work Now : (%d) %s", (int)strlen(work), repr(work));
+    ZF_LOGD("Work Now: [%s]", repr(work));
   }
 
   // ZF_LOGI("mangle: %s", repr(work));
@@ -1157,29 +1286,45 @@ int mangle(int fd, char *buffer) {
    Transpose words.  Transpose case.  Transpose letters.
    */
   x = rx_match(&WORDS, work);
-  ZF_LOGD("found %d WORDS", x);
+  ZF_LOGD("found %d word groups", x);
 
   if (x > 0) {
     for (i = 0; i < x; i++) {
-      // Do things here.
-      if (i % 3 == 0) {
-        for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
-          buffer[p] = tolower(buffer[p]);
+
+      // Yes!  Be random!
+      if (random_activate(8)) {
+        int c = word_mangler(buffer + rxmatch[i].rm_so,
+                             rxmatch[i].rm_eo - rxmatch[i].rm_so);
+        if (c) {
           mangled++;
-        }
-      } else {
-        if (i % 3 == 1) {
-          for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
-            buffer[p] = toupper(buffer[p]);
-            mangled++;
-          }
+          mangled_chars += c;
         }
       }
+      // Do things here.
+
+      /*
+            // NO! This is predictable
+            if (i % 3 == 0) {
+              mangled++;
+              for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
+                buffer[p] = tolower(buffer[p]);
+                mangled_chars++;
+              }
+            } else {
+              if (i % 3 == 1) {
+                mangled++;
+                for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
+                  buffer[p] = toupper(buffer[p]);
+                  mangled_chars++;
+                }
+              }
+            }
+      */
     }
   }
 
   /*
-   (random) Locate single words, and transpose them.  Transpose case.
+   (random) Locate single words, and transpose words.
    Transpose letters.
    */
 
@@ -1192,7 +1337,8 @@ int mangle(int fd, char *buffer) {
     ZF_LOGD("HH %d : (%d) %s", need_render, (int)strlen(buffer), repr(buffer));
   } else {
     if (mangled) {
-      ZF_LOGD("Mangled %d : %s", mangled, repr(buffer));
+      ZF_LOGD("Mangled %d words, %d chars : %s", mangled, mangled_chars,
+              repr(buffer));
     }
   }