1 #!/usr/bin/python
 2 
 3 import re
 4 import sys
 5 
 6 def print_uninterpreted(string):
 7     result = string
 8     result = re.sub(r"\\", r"\\\\", result)
 9     result = re.sub(r"\"", r"\"", result)
10     result = re.sub(r"\n", r"\\n", result)
11     return result
12 
13 def print_delimited(list, d):
14     result = ""
15     for l in range(len(list)):
16         result += print_uninterpreted(list[l])
17         if l < len(list)-1:
18             result += d
19     return result
20 
21 code = [
22 "#!/usr/bin/python\n\nimport re\nimport sys\n\ndef print_uninterpreted(string):\n    result = string\n    result = re.sub(r\"\\\\\", r\"\\\\\\\\\", result)\n    result = re.sub(r\"\\\"\", r\"\\\"\", result)\n    result = re.sub(r\"\\n\", r\"\\\\n\", result)\n    return result\n\ndef print_delimited(list, d):\n    result = \"\"\n    for l in range(len(list)):\n        result += print_uninterpreted(list[l])\n        if l < len(list)-1:\n            result += d\n    return result\n\ncode = [\n\"%s\"\n]\ndelimiter = [\n\"%s\"\n]\n\nlang = 0\nif len(sys.argv) > 1:\n    lang = int(sys.argv[1])\n\nprint code[lang] %% (print_delimited(code, delimiter[lang]), print_delimited(delimiter, delimiter[lang]))"
23 ,
24 "#!/bin/sh\n\nLANG=1\nif [ $# -ne 0 ]; then\n LANG=$1\nfi\n\nCOUNT_LANG=$(( $(grep -n \"^#DELIMITER\" $0 | sed -e 's/:.*//') - $(grep -n \"^#CODE\" $0 | sed -e 's/:.*//') -1 ))\n\nArray() {\n grep \"^#$1\" $0 -A $COUNT_LANG | tail -n $COUNT_LANG | sed -e 's/^ #//'\n}\nArray_value() {\n Array $1 | head -n $(($2 +1)) | tail -n 1 | tr -d \"\\n\"\n}\n\nPrint_delimited() {\n for line in $(seq 0 $(( $COUNT_LANG -1 )) ); do\n  echo -n \"$( Array_value $1 $line )\"\n  if [ $line -lt $(( $COUNT_LANG -1 )) ]; then\n   printf \"$( Array_value \"DELIMITER\" $LANG )\"\n  fi\n done\n}\n\nprintf \"$(Array_value \"CODE\" $LANG)\\n\" \"$(Print_delimited \"CODE\")\" \"$(Print_delimited \"DELIMITER\")\"\n\n#CODE\n #%s\n#DELIMITER\n #%s"
25 ,
26 "#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n\n\nchar* code[] = { \n\"%s\"\n};\nchar* delimiter[] = {\n\"%s\"\n};\n\n\nint lang = 2;\n\n\nchar* escape(char* string) {\n    char* s = string;\n    char* new = malloc(2*strlen(s)+1);\n    unsigned char c;\n    while ((c = *s))\n    {\n        switch(c) {\n            case '\\\\' : strcat(new, \"\\\\\\\\\"); break;\n            case '\\\"' : strcat(new, \"\\\\\\\"\"); break;\n            case '\\n' : strcat(new, \"\\\\n\" ); break;\n            default   : strncat(new, s, 1 ); break;\n        }\n        *s++;\n    }\n    return new;\n}\n\n\nchar* print_delimited(char* array[], char* delim) {\n    char* result = malloc(0);\n    strcat(result, escape(array[0]));\n    int i;\n    for(i=1; i<sizeof(code)/sizeof(code[0]); i++) {\n        strcat(result, delim);\n        strcat(result, escape(array[i]));\n    }\n    return result;\n}\n\n\nint main(int argc, char* argv[]) {\n    \n    if(argc > 1)\n        lang = atoi(argv[1]);\n\n    printf( code[lang], print_delimited(code, delimiter[lang]), print_delimited(delimiter, delimiter[lang]) );\n    \n    printf(\"\\n\");\n    return 0;\n}"
27 ,
28 ".globl code_s\n .section .rodata\n .align 8\n.LC0:\n .string \"%s\"\n .string \"\"\n .data\n .align 8\n .type code_s, @object\n .size code_s, 8\ncode_s:\n .quad .LC0\n.globl delimiter_s\n .section .rodata\n.LC1:\n .string \"%s\"\n .string \"\"\n .data\n .align 8\n .type delimiter_s, @object\n .size delimiter_s, 8\ndelimiter_s:\n .quad .LC1\n.globl lang\n .align 4\n .type lang, @object\n .size lang, 4\nlang:\n .long 3\n .text\n.globl escape\n .type escape, @function\nescape:\n.LFB5:\n pushq %%rbp\n.LCFI0:\n movq %%rsp, %%rbp\n.LCFI1:\n subq $80, %%rsp\n.LCFI2:\n movq %%rdi, -40(%%rbp)\n movq -40(%%rbp), %%rax\n movq %%rax, -24(%%rbp)\n movq -24(%%rbp), %%rax\n movq $-1, %%rcx\n movq %%rax, -56(%%rbp)\n movl $0, %%eax\n cld\n movq -56(%%rbp), %%rdi\n repnz\n scasb\n movq %%rcx, %%rax\n notq %%rax\n decq %%rax\n addq %%rax, %%rax\n leaq 1(%%rax), %%rdi\n call malloc\n movq %%rax, -16(%%rbp)\n jmp .L2\n.L3:\n movzbl -1(%%rbp), %%eax\n movl %%eax, -44(%%rbp)\n cmpl $34, -44(%%rbp)\n je .L6\n cmpl $92, -44(%%rbp)\n je .L7\n cmpl $10, -44(%%rbp)\n je .L5\n jmp .L4\n.L7:\n movq -16(%%rbp), %%rax\n movq $-1, %%rcx\n movq %%rax, -64(%%rbp)\n movl $0, %%eax\n cld\n movq -64(%%rbp), %%rdi\n repnz\n scasb\n movq %%rcx, %%rax\n notq %%rax\n decq %%rax\n addq -16(%%rbp), %%rax\n movw $23644, (%%rax)\n movb $0, 2(%%rax)\n jmp .L8\n.L6:\n movq -16(%%rbp), %%rax\n movq $-1, %%rcx\n movq %%rax, -72(%%rbp)\n movl $0, %%eax\n cld\n movq -72(%%rbp), %%rdi\n repnz\n scasb\n movq %%rcx, %%rax\n notq %%rax\n decq %%rax\n addq -16(%%rbp), %%rax\n movw $8796, (%%rax)\n movb $0, 2(%%rax)\n jmp .L8\n.L5:\n movq -16(%%rbp), %%rax\n movq $-1, %%rcx\n movq %%rax, -80(%%rbp)\n movl $0, %%eax\n cld\n movq -80(%%rbp), %%rdi\n repnz\n scasb\n movq %%rcx, %%rax\n notq %%rax\n decq %%rax\n addq -16(%%rbp), %%rax\n movw $28252, (%%rax)\n movb $0, 2(%%rax)\n jmp .L8\n.L4:\n movq -24(%%rbp), %%rsi\n movq -16(%%rbp), %%rdi\n movl $1, %%edx\n call strncat\n.L8:\n incq -24(%%rbp)\n.L2:\n movq -24(%%rbp), %%rax\n movzbl (%%rax), %%eax\n movb %%al, -1(%%rbp)\n cmpb $0, -1(%%rbp)\n jne .L3\n movq -16(%%rbp), %%rax\n leave\n ret\n.LFE5:\n .size escape, .-escape\n.globl print_delimited\n .type print_delimited, @function\nprint_delimited:\n.LFB6:\n pushq %%rbp\n.LCFI3:\n movq %%rsp, %%rbp\n.LCFI4:\n subq $48, %%rsp\n.LCFI5:\n movq %%rdi, -24(%%rbp)\n movq %%rsi, -32(%%rbp)\n movl $0, %%edi\n call malloc\n movq %%rax, -16(%%rbp)\n movq -24(%%rbp), %%rax\n movq %%rax, -8(%%rbp)\n movq -8(%%rbp), %%rdi\n call escape\n movq %%rax, %%rsi\n movq -16(%%rbp), %%rdi\n call strcat\n movq -8(%%rbp), %%rax\n movq $-1, %%rcx\n movq %%rax, -40(%%rbp)\n movl $0, %%eax\n cld\n movq -40(%%rbp), %%rdi\n repnz\n scasb\n movq %%rcx, %%rax\n notq %%rax\n decq %%rax\n addq -8(%%rbp), %%rax\n incq %%rax\n movq %%rax, -8(%%rbp)\n jmp .L12\n.L13:\n movq -32(%%rbp), %%rsi\n movq -16(%%rbp), %%rdi\n call strcat\n movq -8(%%rbp), %%rdi\n call escape\n movq %%rax, %%rsi\n movq -16(%%rbp), %%rdi\n call strcat\n movq -8(%%rbp), %%rax\n movq $-1, %%rcx\n movq %%rax, -48(%%rbp)\n movl $0, %%eax\n cld\n movq -48(%%rbp), %%rdi\n repnz\n scasb\n movq %%rcx, %%rax\n notq %%rax\n decq %%rax\n addq -8(%%rbp), %%rax\n incq %%rax\n movq %%rax, -8(%%rbp)\n.L12:\n movq -8(%%rbp), %%rax\n movzbl (%%rax), %%eax\n testb %%al, %%al\n jne .L13\n movq -16(%%rbp), %%rax\n leave\n ret\n.LFE6:\n .size print_delimited, .-print_delimited\n.globl array_index\n .type array_index, @function\narray_index:\n.LFB7:\n pushq %%rbp\n.LCFI6:\n movq %%rsp, %%rbp\n.LCFI7:\n movq %%rdi, -24(%%rbp)\n movl %%esi, -28(%%rbp)\n movq -24(%%rbp), %%rax\n movq %%rax, -16(%%rbp)\n movl $0, -4(%%rbp)\n jmp .L17\n.L18:\n movq -16(%%rbp), %%rax\n movq $-1, %%rcx\n movq %%rax, -40(%%rbp)\n movl $0, %%eax\n cld\n movq -40(%%rbp), %%rdi\n repnz\n scasb\n movq %%rcx, %%rax\n notq %%rax\n decq %%rax\n addq -16(%%rbp), %%rax\n incq %%rax\n movq %%rax, -16(%%rbp)\n incl -4(%%rbp)\n.L17:\n movl -4(%%rbp), %%eax\n cmpl -28(%%rbp), %%eax\n jl .L18\n movq -16(%%rbp), %%rax\n leave\n ret\n.LFE7:\n .size array_index, .-array_index\n.globl main\n .type main, @function\nmain:\n.LFB8:\n pushq %%rbp\n.LCFI8:\n movq %%rsp, %%rbp\n.LCFI9:\n pushq %%r12\n.LCFI10:\n pushq %%rbx\n.LCFI11:\n subq $16, %%rsp\n.LCFI12:\n movl %%edi, -20(%%rbp)\n movq %%rsi, -32(%%rbp)\n cmpl $1, -20(%%rbp)\n jle .L22\n movq -32(%%rbp), %%rax\n addq $8, %%rax\n movq (%%rax), %%rdi\n call atoi\n movl %%eax, lang(%%rip)\n.L22:\n movl lang(%%rip), %%esi\n movq delimiter_s(%%rip), %%rdi\n call array_index\n movq %%rax, %%rsi\n movq delimiter_s(%%rip), %%rdi\n call print_delimited\n movq %%rax, %%r12\n movl lang(%%rip), %%esi\n movq delimiter_s(%%rip), %%rdi\n call array_index\n movq %%rax, %%rsi\n movq code_s(%%rip), %%rdi\n call print_delimited\n movq %%rax, %%rbx\n movl lang(%%rip), %%esi\n movq code_s(%%rip), %%rdi\n call array_index\n movq %%rax, %%rdi\n movq %%r12, %%rdx\n movq %%rbx, %%rsi\n movl $0, %%eax\n call printf\n movl $10, %%edi\n call putchar\n movl $0, %%eax\n addq $16, %%rsp\n popq %%rbx\n popq %%r12\n leave\n ret\n.LFE8:\n .size main, .-main\n .section .eh_frame,\"a\",@progbits\n.Lframe1:\n .long .LECIE1-.LSCIE1\n.LSCIE1:\n .long 0x0\n .byte 0x1\n .string \"zR\"\n .uleb128 0x1\n .sleb128 -8\n .byte 0x10\n .uleb128 0x1\n .byte 0x3\n .byte 0xc\n .uleb128 0x7\n .uleb128 0x8\n .byte 0x90\n .uleb128 0x1\n .align 8\n.LECIE1:\n.LSFDE1:\n .long .LEFDE1-.LASFDE1\n.LASFDE1:\n .long .LASFDE1-.Lframe1\n .long .LFB5\n .long .LFE5-.LFB5\n .uleb128 0x0\n .byte 0x4\n .long .LCFI0-.LFB5\n .byte 0xe\n .uleb128 0x10\n .byte 0x86\n .uleb128 0x2\n .byte 0x4\n .long .LCFI1-.LCFI0\n .byte 0xd\n .uleb128 0x6\n .align 8\n.LEFDE1:\n.LSFDE3:\n .long .LEFDE3-.LASFDE3\n.LASFDE3:\n .long .LASFDE3-.Lframe1\n .long .LFB6\n .long .LFE6-.LFB6\n .uleb128 0x0\n .byte 0x4\n .long .LCFI3-.LFB6\n .byte 0xe\n .uleb128 0x10\n .byte 0x86\n .uleb128 0x2\n .byte 0x4\n .long .LCFI4-.LCFI3\n .byte 0xd\n .uleb128 0x6\n .align 8\n.LEFDE3:\n.LSFDE5:\n .long .LEFDE5-.LASFDE5\n.LASFDE5:\n .long .LASFDE5-.Lframe1\n .long .LFB7\n .long .LFE7-.LFB7\n .uleb128 0x0\n .byte 0x4\n .long .LCFI6-.LFB7\n .byte 0xe\n .uleb128 0x10\n .byte 0x86\n .uleb128 0x2\n .byte 0x4\n .long .LCFI7-.LCFI6\n .byte 0xd\n .uleb128 0x6\n .align 8\n.LEFDE5:\n.LASFDE7:\n .long .LASFDE7-.Lframe1\n .long .LFB8\n .long .LFE8-.LFB8\n .uleb128 0x0\n .byte 0x4\n .long .LCFI8-.LFB8\n .byte 0xe\n .uleb128 0x10\n .byte 0x86\n .uleb128 0x2\n .byte 0x4\n .long .LCFI9-.LCFI8\n .byte 0xd\n .uleb128 0x6\n .byte 0x4\n .long .LCFI12-.LCFI9\n .byte 0x83\n .uleb128 0x4\n .byte 0x8c\n .uleb128 0x3\n .align 8"
29 ]
30 delimiter = [
31 "\"\n,\n\""
32 ,
33 "\n #"
34 ,
35 "\"\n,\n\""
36 ,
37 "\"\n .string \""
38 ]
39 
40 lang = 0
41 if len(sys.argv) > 1:
42     lang = int(sys.argv[1])
43 
44 print code[lang] % (print_delimited(code, delimiter[lang]), print_delimited(delimiter, delimiter[lang]))