You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 lines
4.2 KiB

  1. #!/bin/bash
  2. # restart using a Tcl shell \
  3. exec sh -c 'for tclshell in tclsh tclsh83 cygtclsh80 ; do \
  4. ( echo | $tclshell ) 2> /dev/null && exec $tclshell "`( cygpath -w \"$0\" ) 2> /dev/null || echo $0`" "$@" ; \
  5. done ; \
  6. echo "file2c.tcl: cannot find Tcl shell" ; exit 1' "$0" "$@"
  7. #===============================================================================
  8. #
  9. # file2c.tcl
  10. #
  11. # Convert a file into a header that can be #included from C.
  12. #
  13. #===============================================================================
  14. #####ECOSGPLCOPYRIGHTBEGIN####
  15. ## -------------------------------------------
  16. ## This file is part of eCos, the Embedded Configurable Operating System.
  17. ## Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
  18. ##
  19. ## eCos is free software; you can redistribute it and/or modify it under
  20. ## the terms of the GNU General Public License as published by the Free
  21. ## Software Foundation; either version 2 or (at your option) any later version.
  22. ##
  23. ## eCos is distributed in the hope that it will be useful, but WITHOUT ANY
  24. ## WARRANTY; without even the implied warranty of MERCHANTABILITY or
  25. ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  26. ## for more details.
  27. ##
  28. ## You should have received a copy of the GNU General Public License along
  29. ## with eCos; if not, write to the Free Software Foundation, Inc.,
  30. ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  31. ##
  32. ## As a special exception, if other files instantiate templates or use macros
  33. ## or inline functions from this file, or you compile this file and link it
  34. ## with other works to produce a work based on this file, this file does not
  35. ## by itself cause the resulting work to be covered by the GNU General Public
  36. ## License. However the source code for this file must still be made available
  37. ## in accordance with section (3) of the GNU General Public License.
  38. ##
  39. ## This exception does not invalidate any other reasons why a work based on
  40. ## this file might be covered by the GNU General Public License.
  41. ##
  42. ## Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
  43. ## at http://sources.redhat.com/ecos/ecos-license/
  44. ## -------------------------------------------
  45. #####ECOSGPLCOPYRIGHTEND####
  46. #===============================================================================
  47. ######DESCRIPTIONBEGIN####
  48. #
  49. # Author(s): jlarmour,bartv
  50. # Contact(s):
  51. # Date: 2001-07-20
  52. # Purpose:
  53. # Description:
  54. # Usage: file2c.tcl <file to encode> <output C header file>
  55. #
  56. #####DESCRIPTIONEND####
  57. #===============================================================================
  58. if { $argc != 2 } {
  59. puts "Usage: file2c.tcl <file to encode> <output C file>"
  60. exit 1
  61. }
  62. set infile [lindex $argv 0]
  63. set outfile [lindex $argv 1]
  64. set label [string range $outfile [expr 1+[string last / $outfile]] [expr [string last . $outfile]-1]]
  65. set status [ catch {
  66. set infilefd [open $infile "r"]
  67. fconfigure $infilefd -translation binary
  68. set data [read $infilefd]
  69. close $infilefd
  70. } message]
  71. if { $status != 0 } {
  72. error "Unable to read file $infile: $message"
  73. }
  74. set result ""
  75. set status [ catch {
  76. set outfilefd [ open $outfile "w" ]
  77. } message ]
  78. if { $status != 0 } {
  79. error "Unable to create file $outfile: $message"
  80. }
  81. append result "/* This is a generated file. Do not edit. */\n\n"
  82. append result "const unsigned char filedata_$label\[\] = {\n"
  83. set datalength [ string length $data ]
  84. set aligned_datalength [expr $datalength - ($datalength % 8)]
  85. for { set i 0 } {$i < $aligned_datalength} {incr i 8} {
  86. binary scan $data "@[set i]H16" var0
  87. append result [format " 0x%2s, 0x%2s, 0x%2s, 0x%2s, 0x%2s, 0x%2s, 0x%2s, 0x%2s,\n" \
  88. [string range $var0 0 1] \
  89. [string range $var0 2 3] \
  90. [string range $var0 4 5] \
  91. [string range $var0 6 7] \
  92. [string range $var0 8 9] \
  93. [string range $var0 10 11] \
  94. [string range $var0 12 13] \
  95. [string range $var0 14 15]]
  96. }
  97. if { $aligned_datalength != $datalength } {
  98. append result " "
  99. for { set i $aligned_datalength } {$i < $datalength} {incr i} {
  100. binary scan $data "@[set i]H2" var0
  101. append result [format "0x%2s, " $var0]
  102. }
  103. }
  104. # Remove either comma+newline or comma+space from the end
  105. set result [string range $result 0 [expr [string length $result] - 3]]
  106. append result "\n};"
  107. puts $outfilefd $result
  108. close $outfilefd