File: txtc.sh

package info (click to toggle)
source-highlight 2.4-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,380 kB
  • ctags: 4,084
  • sloc: cpp: 4,313; sh: 3,858; ansic: 3,579; makefile: 968; lex: 876; yacc: 636; php: 187; sed: 164; perl: 145; ruby: 63; python: 54; ml: 38; java: 30; xml: 17; cs: 11
file content (115 lines) | stat: -rw-r--r-- 3,741 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 1998 Eleftherios Gkioulekas <lf@amath.washington.edu>
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# As a special exception to the GNU General Public License, if you 
# distribute this file as part of a program that contains a configuration 
# script generated by Autoconf, you may include it under the same 
# distribution terms that you use for the rest of that program.


# Usage:
# 1. To produce object files for a bunch of text files:
#      sh txtc.sh foo1.txt foo2.txt foo3.txt ...
# 2. To produce only C files for a bunch of text files:
#      sh txtc.sh -c foo1.txt foo2.txt foo3.txt ...
# 3. To produce pic object files object files
#      sh txtc.sh -l libtool foo1.txt foo2.txt foo3.txt ...

# ---------------------------------------------
# These definitions are imported from Autoconf
# ---------------------------------------------

CC="i486-linux-gnu-gcc"
AWK="gawk"

# --------------------
# Initialize the data 
# --------------------

# File names
text_files=""
path_to_libtool=""

# Flags
only_the_c_files="no"


# ------------------------------------------
# Now parse the options and update the state
# ------------------------------------------

while test -n "$1"
do
  argument="$1"
  case $argument in
  -c)  only_the_c_files="yes"
       ;;
  -l)  shift
       path_to_libtool="$1"
       ;;
  *)   text_files="$text_files $1"
       ;;
  esac
  shift
done

# ---------------------------------
# Now look over all the text files
# ---------------------------------

for file in $text_files
do
  # Get the filenames for C, object and pic object files
  c_file="`echo $file  | sed 's/\.[a-zA-Z]*$/.c/g'  | $AWK -F/ '{ print $NF }'`"
  o_file="`echo $file  | sed 's/\.[a-zA-Z]*$/.o/g'  | $AWK -F/ '{ print $NF }'`"
  lo_file="`echo $file | sed 's/\.[a-zA-Z]*$/.lo/g' | $AWK -F/ '{ print $NF }'`"

  # Get the symbol name for the file and the length
  symbol_name="`echo $file | sed 's/\./_/g' | $AWK -F/ '{ print $NF }'`"
  file_length="`cat $file | wc -l`"

  # Create the C file
  # First apply the following substitutions with sed
  # 1. Escape the escapes
  # 2. Escape the quotes
  # 3. Nuke whitespace after the last character
  # Then pipe through awk to make the C program
  # FIXME: perhaps another substitution needed for tabs?
  rm -f $c_file
  cat $file | sed 's/\\/\\\\/g
                   s/\"/\\\"/g
                   s/[[:blank:]]*$//g' | 
              $AWK -v symbol_name="$symbol_name" -v file_length="$file_length" \
                   -v filename="$file"  '
     BEGIN { print "#define NULL (char *) 0"
             print "int " symbol_name "_length = " file_length ";"
             print "char *" symbol_name "[] = { " 
             print "  \"" filename "\"," }

           { print "  \"" $0 "\"," }

     END   { print "  NULL }; " }
  ' > $c_file

  # If we also want to build an object file, do so here 
  # FIXME: At the moment I am not supporting shared libraries
  # Will do so once I get a stable version.
  if test "$only_the_c_files" = "no"
  then
    $CC $CFLAGS -c $c_file
    rm -f $c_file
  fi
done