gen_msgs.sh (1768B)
1 #!/bin/sh 2 3 protocol_version=0 4 msg_types=" 5 device_add 6 device_update 7 friend_add 8 friend_delete 9 list_add 10 list_update 11 list_join 12 list_leave 13 lists_get 14 lists_get_other 15 list_items_get 16 list_item_add 17 " 18 19 objc_path="ios/shlist/MsgTypes.h" 20 java_path="android/shlist/app/src/main/java/drsocto/shlist/MsgTypes.java" 21 perl_path="server/msgs.pl" 22 23 generated_at="generated `date`" 24 25 # Helper function to enumerate messages and make tables 26 # arg 1: path to output file 27 # arg 2: array/list/hash/whatever declaration 28 # arg 3: a string that's interpreted in the loop below, like "$msg => $i" 29 # arg 4: closing parenthesis/curly braces/whatever 30 print_table() { 31 echo "${2}" >> ${1} 32 33 i=0 34 for msg in $msg_types; do 35 eval "echo \" $3\"" >> ${1} 36 i=$((i + 1)) 37 done 38 39 # print footer 40 echo "${4}" >> ${1} 41 } 42 43 gen_objc() { 44 cat << EOF > $objc_path 45 /* ${generated_at} */ 46 47 int protocol_version = $protocol_version; 48 EOF 49 50 print_table $objc_path "enum msg_types {" "\$msg = \$i," "};" 51 print_table $objc_path "static const char *msg_strings[] = {" "\\\"\$msg\\\"," "};" 52 } 53 54 gen_java() { 55 cat << EOF > $java_path 56 /* ${generated_at} */ 57 58 package drsocto.shlist; 59 60 public final class MsgTypes { 61 62 public final static int protocol_version = $protocol_version; 63 EOF 64 65 print_table $java_path "" "public final static int \$msg = \$i;" "}" 66 } 67 68 gen_perl() { 69 cat << EOF > $perl_path 70 #!/usr/bin/perl 71 # ${generated_at} 72 use strict; 73 use warnings; 74 75 our \$protocol_ver = $protocol_version; 76 EOF 77 78 # We want message name to number map, number to name array, and function 79 # pointer array 80 print_table $perl_path "our %msg_num = (" "\$msg => \$i," ");" 81 print_table $perl_path "our @msg_str = (" "'\$msg'," ");" 82 print_table $perl_path "our @msg_func = (" "\\&msg_\$msg," ");" 83 } 84 85 gen_objc 86 gen_java 87 gen_perl