142807Smsmith#!/usr/bin/awk -f
260962Sdcs#
342807Smsmith# Convert forth source files to a giant C string
460962Sdcs#
542807Smsmith# Joe Abley <jabley@patho.gen.nz>, 12 January 1999
660962Sdcs#
760962Sdcs# 02-oct-1999:  Cleaned up awk slightly; added some additional logic
860962Sdcs#               suggested by dcs to compress the stored forth program.
960962Sdcs#
1085934Sobrien# Note! This script uses strftime() which is a gawk-ism, and the
1185934Sobrien# POSIX [[:space:]] character class.
1285934Sobrien#
1351789Sdcs# $FreeBSD: releng/10.2/sys/boot/ficl/softwords/softcore.awk 167850 2007-03-23 22:26:01Z jkim $
1442807Smsmith
1542807SmsmithBEGIN \
1642807Smsmith{
17167850Sjkim  printf "/*******************************************************************\n";
1842807Smsmith  printf "** s o f t c o r e . c\n";
1942807Smsmith  printf "** Forth Inspired Command Language -\n";
2042807Smsmith  printf "** Words from CORE set written in FICL\n";
2142807Smsmith  printf "** Author: John Sadler (john_sadler@alum.mit.edu)\n";
2242807Smsmith  printf "** Created: 27 December 1997\n";
2368729Sobrien  printf "** Last update: %s\n", datestamp;
24167850Sjkim  printf "*******************************************************************/\n";
25167850Sjkim  printf "/*\n";
26167850Sjkim  printf "** DO NOT EDIT THIS FILE -- it is generated by softwords/softcore.awk\n";
27167850Sjkim  printf "** Make changes to the .fr files in ficl/softwords instead.\n";
2842807Smsmith  printf "** This file contains definitions that are compiled into the\n";
2942807Smsmith  printf "** system dictionary by the first virtual machine to be created.\n";
3042807Smsmith  printf "** Created automagically by ficl/softwords/softcore.awk\n";
3142807Smsmith  printf "*/\n";
32167850Sjkim  printf "/*\n";
33167850Sjkim  printf "** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)\n";
34167850Sjkim  printf "** All rights reserved.\n";
35167850Sjkim  printf "**\n";
36167850Sjkim  printf "** Get the latest Ficl release at http://ficl.sourceforge.net\n";
37167850Sjkim  printf "**\n";
38167850Sjkim  printf "** I am interested in hearing from anyone who uses ficl. If you have\n";
39167850Sjkim  printf "** a problem, a success story, a defect, an enhancement request, or\n";
40167850Sjkim  printf "** if you would like to contribute to the ficl release, please send\n";
41167850Sjkim  printf "** contact me by email at the address above.\n";
42167850Sjkim  printf "**\n";
43167850Sjkim  printf "** L I C E N S E  and  D I S C L A I M E R\n";
44167850Sjkim  printf "** \n";
45167850Sjkim  printf "** Redistribution and use in source and binary forms, with or without\n";
46167850Sjkim  printf "** modification, are permitted provided that the following conditions\n";
47167850Sjkim  printf "** are met:\n";
48167850Sjkim  printf "** 1. Redistributions of source code must retain the above copyright\n";
49167850Sjkim  printf "**    notice, this list of conditions and the following disclaimer.\n";
50167850Sjkim  printf "** 2. Redistributions in binary form must reproduce the above copyright\n";
51167850Sjkim  printf "**    notice, this list of conditions and the following disclaimer in the\n";
52167850Sjkim  printf "**    documentation and/or other materials provided with the distribution.\n";
53167850Sjkim  printf "**\n";
54167850Sjkim  printf "** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\n";
55167850Sjkim  printf "** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n";
56167850Sjkim  printf "** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n";
57167850Sjkim  printf "** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\n";
58167850Sjkim  printf "** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n";
59167850Sjkim  printf "** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n";
60167850Sjkim  printf "** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n";
61167850Sjkim  printf "** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n";
62167850Sjkim  printf "** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n";
63167850Sjkim  printf "** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n";
64167850Sjkim  printf "** SUCH DAMAGE.\n";
65167850Sjkim  printf "*/\n";
66167850Sjkim  printf "\n";
6742807Smsmith  printf "\n#include \"ficl.h\"\n";
6842807Smsmith  printf "\nstatic char softWords[] =\n";
69167850Sjkim  printf "#if FICL_WANT_SOFTWORDS\n";
7042807Smsmith
7142807Smsmith  commenting = 0;
7242807Smsmith}
7342807Smsmith
7442807Smsmith# some general early substitutions
7542807Smsmith{
7660962Sdcs  gsub(/\t/, "    ");			# replace each tab with 4 spaces
7760962Sdcs  gsub(/\"/, "\\\"");			# escape quotes
7885934Sobrien  gsub(/\\[[:space:]]+$/, "");		# toss empty comments
7942807Smsmith}
8042807Smsmith
8142807Smsmith# strip out empty lines
8242807Smsmith/^ *$/ \
8342807Smsmith{
8442807Smsmith  next;
8542807Smsmith}
8642807Smsmith
8742807Smsmith# emit / ** lines as multi-line C comments
8885934Sobrien/^\\[[:space:]]\*\*/ \
8942807Smsmith{
9085934Sobrien  sub(/^\\[[:space:]]/, "");
9160962Sdcs  if (commenting == 0) printf "/*\n";
9260962Sdcs  printf "%s\n", $0;
9342807Smsmith  commenting = 1;
9442807Smsmith  next;
9542807Smsmith}
9642807Smsmith
9760962Sdcs# strip blank lines
9885934Sobrien/^[[:space:]]*$/ \
9942807Smsmith{
10042807Smsmith  next;
10142807Smsmith}
10242807Smsmith
10342807Smsmith# function to close a comment, used later
10442807Smsmithfunction end_comments()
10542807Smsmith{
10642807Smsmith  commenting = 0;
10742807Smsmith  printf "*/\n";
10842807Smsmith}
10942807Smsmith
11042807Smsmith# pass commented preprocessor directives
11185934Sobrien/^\\[[:space:]]#/ \
11242807Smsmith{
11342807Smsmith  if (commenting) end_comments();
11485934Sobrien  sub(/^\\[[:space:]]/, "");
11542807Smsmith  printf "%s\n", $0;
11642807Smsmith  next;
11742807Smsmith}
11842807Smsmith
11960962Sdcs# toss all other full-line \ comments
12042807Smsmith/^\\/ \
12142807Smsmith{
12242807Smsmith  if (commenting) end_comments();
12342807Smsmith  next;
12442807Smsmith}
12542807Smsmith
12660962Sdcs# lop off trailing \ comments
12785934Sobrien/\\[[:space:]]+/ \
12860962Sdcs{
12985934Sobrien  sub(/\\[[:space:]]+.*$/, "");
13060962Sdcs}
13160962Sdcs
13260962Sdcs# expunge ( ) comments
13385934Sobrien/[[:space:]]+\([[:space:]][^)]*\)/ \
13460962Sdcs{
13585934Sobrien  sub(/[[:space:]]+\([[:space:]][^)]*\)/, "");
13660962Sdcs}
13760962Sdcs
13860962Sdcs# remove leading spaces
13985934Sobrien/^[[:space:]]+/ \
14060962Sdcs{
14185934Sobrien  sub(/^[[:space:]]+/, "");
14260962Sdcs}
14360962Sdcs
14460962Sdcs# removing trailing spaces
14585934Sobrien/[[:space:]]+$/ \
14660962Sdcs{
14785934Sobrien  sub(/[[:space:]]+$/, "");
14860962Sdcs}
14960962Sdcs
15060962Sdcs# strip out empty lines again (preceding rules may have generated some)
15185934Sobrien/^[[:space:]]*$/ \
15260962Sdcs{
15360962Sdcs  if (commenting) end_comments();
15460962Sdcs  next;
15560962Sdcs}
15660962Sdcs
15742807Smsmith# emit all other lines as quoted string fragments
15842807Smsmith{
15942807Smsmith  if (commenting) end_comments();
16042807Smsmith
16160962Sdcs  printf "    \"%s \"\n", $0;
16242807Smsmith  next;
16342807Smsmith}
16442807Smsmith
16542807SmsmithEND \
16642807Smsmith{
16742807Smsmith  if (commenting) end_comments();
168167850Sjkim  printf "#endif /* WANT_SOFTWORDS */\n";
16942807Smsmith  printf "    \"quit \";\n";
17076116Sdcs  printf "\n\nvoid ficlCompileSoftCore(FICL_SYSTEM *pSys)\n";
17142807Smsmith  printf "{\n";
17276116Sdcs  printf "    FICL_VM *pVM = pSys->vmList;\n";
173167850Sjkim  printf "    CELL id = pVM->sourceID;\n";
17476116Sdcs  printf "    int ret = sizeof (softWords);\n";
17576116Sdcs  printf "	  assert(pVM);\n";
176167850Sjkim  printf "    pVM->sourceID.i = -1;\n";
17776116Sdcs  printf "    ret = ficlExec(pVM, softWords);\n";
178167850Sjkim  printf "    pVM->sourceID = id;\n";
17976116Sdcs  printf "    if (ret == VM_ERREXIT)\n";
18076116Sdcs  printf "        assert(FALSE);\n";
18176116Sdcs  printf "    return;\n";
18242807Smsmith  printf "}\n";
18342807Smsmith}
184