~bouncepaw/agidel

9ed954d66d11d7d8d38878d410431a5c038cc211 — Timur Ismagilov 5 years ago b669d0d
Add wrap_string syntrans
1 files changed, 50 insertions(+), 0 deletions(-)

A wrap_string.c
A wrap_string.c => wrap_string.c +50 -0
@@ 0,0 1,50 @@
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>

enum State { SOMEWHERE, STRING, ESCAPING } state = SOMEWHERE;
char ch;

int main() {
  while(read(STDIN_FILENO, &ch, 1) > 0) {
    switch (state) {
    case STRING:
      switch (ch) {
      case '\\':
        state = ESCAPING;
        putchar(ch);
        break;
      case '\"':
        state = SOMEWHERE;
        printf("\\\"\""); // \""
        break;
      default:
        putchar(ch);
      }
      break;

    case ESCAPING:
      putchar(ch);
      state = STRING;
      break;

    case SOMEWHERE:
    default:
      switch (ch) {
      case '\"':
        state = STRING;
        printf("\"\\\""); // "\"
        break;
      default:
        putchar(ch);
      }
    }
  }

  if (state != SOMEWHERE) {
    fprintf(stderr, "Agidel: unfinished string.\n");
    return 1;
  }

  return 0;
}