summaryrefslogtreecommitdiff
path: root/ext/iconv/charset_alias.rb
blob: e5210b4e4f0b40cdb3800b31e9398cf3893f9ad8 (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
#! /usr/bin/ruby
# :stopdoc:
require 'rbconfig'
require 'optparse'

# http://www.ctan.org/tex-archive/macros/texinfo/texinfo/intl/config.charset
# Fri, 30 May 2003 00:09:00 GMT'

OS = Config::CONFIG["target_os"]
SHELL = Config::CONFIG['SHELL']

class Hash::Ordered < Hash
  def [](key)
    val = super and val.last
  end
  def []=(key, val)
    ary = fetch(key) {return super(key, [self.size, key, val])} and
      ary.last = val
  end
  def each
    values.sort.each {|i, key, val| yield key, val}
  end
end

def charset_alias(config_charset, mapfile, target = OS)
  map = Hash::Ordered.new
  comments = []
  match = false
  open(config_charset) do |input|
    input.find {|line| /^case "\$os" in/ =~ line} or return
    input.find {|line|
      /^\s*([-\w\*]+(?:\s*\|\s*[-\w\*]+)*)(?=\))/ =~ line and
      $&.split('|').any? {|pattern| File.fnmatch?(pattern.strip, target)}
    } or return
    input.find do |line|
      case line
      when /^\s*echo "(?:\$\w+\.)?([-\w*]+)\s+([-\w]+)"/
        sys, can = $1, $2
        can.downcase!
        map[can] = sys
        false
      when /^\s*;;/
        true
      else
        false
      end
    end
  end
  case target
  when /linux|-gnu/
    map.delete('ascii')
  when /cygwin/
    # get rid of tilde/yen problem.
    map['shift_jis'] = 'cp932'
  end
  writer = proc do |f|
    f.puts("require 'iconv.so'")
    f.puts
    f.puts(comments)
    f.puts("class Iconv")
    map.each {|can, sys| f.puts("  charset_map['#{can}'.freeze] = '#{sys}'.freeze")}
    f.puts("end")
  end
  if mapfile
    open(mapfile, "w", &writer)
  else
    writer[STDOUT]
  end
end

target = OS
opt = nil
ARGV.options do |opt|
  opt.banner << " config.status map.rb"
  opt.on("--target OS") {|t| target = t}
  opt.parse! and (1..2) === ARGV.size
end or abort opt.to_s
charset_alias(ARGV[0], ARGV[1], target)