3 Commits 0623575917 ... c128c8efd3

Autor SHA1 Mensagem Data
  scuti c128c8efd3 return unmodified string if colors does not run 1 mês atrás
  scuti e3de4151e2 renamed function b to colorize_noalloc 2 meses atrás
  scuti b151a270ea added function for sanitizing input 2 meses atrás
3 arquivos alterados com 33 adições e 9 exclusões
  1. 3 1
      include/colors.h
  2. 5 2
      scripts/allmaps.py
  3. 25 6
      src/colors.c

+ 3 - 1
include/colors.h

@@ -18,7 +18,9 @@ static const char *decspan(const int);
 
 static void hexspan(char *, int, const char *);
 
-static void b(char * const);
+static void colorize_noalloc(char * const);
+
+static void sanitize(char *);
 
 void print_plname(const char*);
 

+ 5 - 2
scripts/allmaps.py

@@ -29,8 +29,11 @@ def run_cgi(query=None):
     return proc.communicate()[0].decode('utf-8')
 
 def run_colors(player_name):
+    ret    = player_name
     result = subprocess.run(['./colors', player_name], capture_output=True, text=True)
-    return result.stdout
+    if result.returncode == 0:
+        ret = result.stdout
+    return ret
 
 def get_speed_record(database, map_id):
     message = "{name} traveled the fastest at {speed} qu/s."
@@ -59,7 +62,7 @@ def main():
         template = fin.read()
         with open("output/index.html", 'w') as fout:
             fout.write(template % run_cgi())
-        # speed records uses something similar
+        # use same template for fastest-players
         query = {"QUERY_STRING" : "fastest-players"}
         with open("output/fastest-players.html", 'w') as fout:
             fout.write(template % run_cgi(query))

+ 25 - 6
src/colors.c

@@ -133,7 +133,7 @@ static void hexspan(char *buf, int bufsize, const char *str) {
 }
 
 #define TAG_LEN 40
-static void b(char * const str) {
+static void colorize_noalloc(char * const str) {
     char *token = strtok(str, "^");
     char c;
     while (token) {
@@ -162,6 +162,16 @@ static void b(char * const str) {
     }
 }
 
+static void sanitize(char *user_name) {
+    if (user_name == NULL) {
+        return;
+    }
+    char *pos  = user_name;
+    while (pos = strstr(pos, "^^")) {
+        strcpy(pos, (pos + 1));
+    }
+}
+
 void print_plname(const char* str) {
     //find instance of ^^
     //replace with ^
@@ -169,16 +179,17 @@ void print_plname(const char* str) {
     char *copy;
     copy = calloc(strlen(str) + 1, sizeof(char));
     strcpy(copy, str);
-    char *pos = copy;
-    while (pos = strstr(pos, "^^")) {
-        strcpy(pos, (pos + 1));
-    }
-    b(copy);
+    sanitize(copy);
+    colorize_noalloc(copy);
     fflush(stdout);
     free(copy);
 }
 
 static char* append_to_str(char *dest, const char *src) {
+    if (dest == NULL || src == NULL) {
+        fprintf(stderr, "append_to_str(): warning - received null ptr" );
+        return NULL;
+    }
     size_t new_len = strlen(dest) + strlen(src) + 1;
     char *new_str = realloc(dest, new_len);
     if (new_str != NULL) {
@@ -222,6 +233,13 @@ char* colorize_name(char *buf, /*int bufsize,*/ char * const str) {
     return buf;
 }
 
+/* test: 
+
+./colors ^9[^1S^9]^x469Kom^0ier^7
+./colors ^9[^1S^9]^^x469Kom^0ier^7
+
+*/
+
 #ifdef COLORS4PYTHON
 int main(int argc, const char **argv) {
     if (argc < 1) { 
@@ -230,6 +248,7 @@ int main(int argc, const char **argv) {
     char *colored = (char*)calloc(strlen(argv[1]) + 1, sizeof(char));
     char *player_name = (char*)calloc(strlen(argv[1]) + 1, sizeof(char));
     strcpy(player_name, argv[1]);
+    sanitize(player_name);
 
     colored = colorize_name(colored, /*sizeof(colored),*/ player_name);
     fprintf(stdout, "%s\n", colored);