summaryrefslogtreecommitdiff
path: root/mkprototab.pl
blob: 15cadb10dab7b9d94cd42aaa21d97e9da539342a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#! /usr/bin/perl -w

use strict;

# librsync -- the library for network deltas
# $Id$
# 
# Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


                                     # I think if you've ordered
				     # somebody to do something you
				     # should probably resist the urge
				     # to thank them.
				     #    -- http://abc.net.au/thegames/


$#ARGV == 1 or die "Usage: mkprototab.pl TABLE HEADER";

open(TABLE, ">$ARGV[0]") or die "Can't open table $ARGV[0]: $!";
open(HEADER, ">$ARGV[1]") or die "Can't open header $ARGV[1]: $!";


my $cmd_byte = 0;


sub emit_cmd {
  my ($kind, $lit_val, $len1, $len2) = @_;
  my $op;
  
  if ($cmd_byte) {
    # not first
    print TABLE ",\n";
    print HEADER ",\n";
  }
  
  if ($len2) {
    $op = sprintf "RS_OP_%s_N%d_N%d", $kind, $len1, $len2;
  } elsif ($len1) {
    $op = sprintf "RS_OP_%s_N%d", $kind, $len1;
  } elsif ($lit_val) {
    $op = sprintf "RS_OP_%s_%d", $kind, $lit_val;
  } else {
    $op = sprintf "RS_OP_%s", $kind;
  }
  
  push(@_, 0) while @_ < 4; # Avoid run-time warnings.
  printf TABLE "    {RS_KIND_%-10s, %3d, %d, %d } ", @_;
  printf TABLE "     /* %20s = %#4x */", $op, $cmd_byte;
  printf HEADER "   %20s = %#4x", $op, $cmd_byte;
  $cmd_byte++;
}


print TABLE <<EOT;
/* AUTOGENERATED BY $0, DO NOT EDIT */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>

#include "librsync.h"
#include <protocol.h>
#include <command.h>
#include <prototab.h>

/* This file defines an array mapping command IDs to the operation kind,
 * implied literal value, and length of the first and second parameters.
 * The implied value is only used
 * if the first parameter length is zero. */

const struct rs_prototab_ent rs_prototab[] = {
EOT


print HEADER <<EOT;
/* AUTOGENERATED BY $0, DO NOT EDIT */

typedef struct rs_prototab_ent {
    enum rs_op_kind     kind;
    int                 immediate;
    size_t              len_1, len_2;
} rs_prototab_ent_t;

extern const rs_prototab_ent_t rs_prototab[];

enum {
EOT


emit_cmd('END');

my @int_lens = (1, 2, 4, 8);

my ($i, $j);

for ($i = 1; $i <= 64; $i++) {
  emit_cmd('LITERAL', $i);
}

foreach $i (@int_lens) {
  emit_cmd('LITERAL', 0, $i);
}

foreach $i (@int_lens) {
  foreach $j (@int_lens) {
    emit_cmd('COPY', 0, $i, $j);
  }
}

emit_cmd('RESERVED', $cmd_byte, 0, 0) while $cmd_byte <= 255;


print TABLE <<EOT;
};
/* END OF AUTOGENERATED CODE */
EOT

print HEADER <<EOT;
};
/* END OF AUTOGENERATED CODE */
EOT