From 6abf3db13ef2f2cce5a238e40c497a56d5e9b7a9 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 2 Jan 2012 17:02:30 -0800 Subject: [PATCH] Added internalize_scripts.py in preparation for ending reliance on "standard" scripts in /usr/local/share --- Makefile.cpp | 15 ++++++- internalize_scripts.py | 92 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100755 internalize_scripts.py diff --git a/Makefile.cpp b/Makefile.cpp index 451ceb548..c3c2a7211 100644 --- a/Makefile.cpp +++ b/Makefile.cpp @@ -86,7 +86,6 @@ HAVE_GETTEXT=0 COMMON_FILES := util.cpp halloc.cpp halloc_util.cpp fallback.cpp - # # All objects that the system needs to build fish, except fish.o # @@ -96,7 +95,7 @@ FISH_OBJS := function.o builtin.o complete.o env.o exec.o expand.o \ tokenizer.o wildcard.o wgetopt.o wutil.o input.o output.o intern.o \ env_universal.o env_universal_common.o input_common.o event.o \ signal.o io.o parse_util.o common.o screen.o path.o \ - parser_keywords.o iothread.o + parser_keywords.o iothread.o builtin_scripts.o FISH_INDENT_OBJS := fish_indent.o print_help.o common.o \ parser_keywords.o wutil.o tokenizer.o @@ -159,6 +158,17 @@ HDR_FILES_SRC := doc_src/index.hdr.in doc_src/commands.hdr.in doc_src/design.hdr HDR_FILES := $(subst .hdr.in,.hdr,$(HDR_FILES_SRC)) +# +# Internalized scripts +# + +GENERATED_INTERN_SCRIPT_FILES := builtin_scripts.h builtin_scripts.cpp + +# Use a pattern rule so that Make knows to only issue one invocation +# per http://www.gnu.org/software/make/manual/make.html#Pattern-Intro +builtin%scripts.h builtin%scripts.cpp: internalize_scripts.py + ./internalize_scripts.py share/functions/*.fish + # # Files containing documentation for external commands. # @@ -876,6 +886,7 @@ distclean: clean clean: rm -f *.o doc.h doc.tmp doc_src/*.doxygen doc_src/*.cpp doc_src/*.o doc_src/commands.hdr + rm -f $(GENERATED_INTERN_SCRIPT_FILES) rm -f tests/tmp.err tests/tmp.out tests/tmp.status tests/foo.txt rm -f $(PROGRAMS) fish_tests tokenizer_test key_reader rm -f share/config.fish etc/config.fish doc_src/index.hdr doc_src/commands.hdr diff --git a/internalize_scripts.py b/internalize_scripts.py new file mode 100755 index 000000000..8b447aa05 --- /dev/null +++ b/internalize_scripts.py @@ -0,0 +1,92 @@ +#!/usr/bin/python + +import string, sys, os.path + +escapes = {} +escapes['\a'] = r'\a' +escapes['\b'] = r'\b' +escapes['\f'] = r'\f' +escapes['\n'] = r'\n' +escapes['\r'] = r'\r' +#escapes['\t'] = r'\t' +# Let's replace tabs with four spaces +# so the text looks nicely indented in the C source +escapes['\t'] = r' ' +escapes['\v'] = r'\v' +# escapes['\''] = r'\'' +escapes['\"'] = r'\"' +escapes['\\'] = r'\\' + +def escape(c): + if c in escapes: + return escapes[c] + elif c not in string.printable: + return "\\x%x" % ord(c) + else: + return c + +def stringize(line): + newline = '"' + for c in line: + newline += escape(c) + newline += '"' + return newline + +class cfunc: + def __init__(self, name, lines): + self.name = name + self.lines = lines + + def cdef(self): + result = "" + result += "static const char * const %s = \n\t" % self.cfunc_name() + result += '\n\t'.join(self.lines) + result += ';\n' + return result + + def cfunc_name(self): + munged_name = string.replace(self.name, '-', '_') + return "function_%s" % munged_name + +funcs = [] +for file in sys.argv[1:]: + fd = open(file, 'r') + newlines = [] + for line in fd: + newlines.append(stringize(line)) + fd.close() + name = os.path.basename(file) + name, ext = os.path.splitext(name) + funcs.append(cfunc(name, newlines)) + +# Sort our functions by name +funcs.sort(key=cfunc.cfunc_name) + +# Output our header +fd = open('builtin_scripts.h', 'w') +fd.write('/* This file is generated by internalize_scripts.py */\n\n') +fd.write("""struct builtin_script_t { + const char *name; + const char *def; +};""") + +fd.write('\n') +fd.write('\n') +fd.write('extern const struct builtin_script_t builtin_scripts[%d];\n' % len(funcs)) +fd.write('\n') +fd.close() + +# Output the function definitions +fd = open('builtin_scripts.cpp', 'w') +fd.write('/* This file is generated by internalize_scripts.py */\n\n') +fd.write('#include "builtin_scripts.h"\n\n') +for func in funcs: + fd.write(func.cdef()) + fd.write('\n') + +# Output the refs +func_refs = ["{%s, %s}" % (stringize(func.name), func.cfunc_name()) for func in funcs] +fd.write('const struct builtin_script_t builtin_scripts[%d] =\n' % len(funcs)) +fd.write('{\n\t') +fd.write(',\n\t'.join(func_refs)) +fd.write('\n};\n')