From 41d50ae572f3622e4f6d397e25ad8c5aadcb16cb Mon Sep 17 00:00:00 2001
From: "David A. Mellis" <d.mellis@arduino.cc>
Date: Fri, 12 Jan 2007 17:58:39 +0000
Subject: [PATCH] Now appending main() to the user's sketch in preparation for
 making the Arduino core a library (.a) file.

---
 app/Sketch.java                  |  2 +-
 app/preproc/PdePreprocessor.java | 30 +++++++++++++++++-------------
 targets/arduino/main.cxx         | 12 ++++++++++++
 targets/arduino/wiring.c         | 11 ++---------
 targets/arduino/wiring.h         |  8 +++++---
 5 files changed, 37 insertions(+), 26 deletions(-)
 create mode 100755 targets/arduino/main.cxx

diff --git a/app/Sketch.java b/app/Sketch.java
index 53369da0f..b45725cca 100644
--- a/app/Sketch.java
+++ b/app/Sketch.java
@@ -1451,7 +1451,7 @@ public class Sketch {
       // java mode, since that's required
       String className =
         preprocessor.write(bigCode.toString(), buildPath,
-                           suggestedClassName, codeFolderPackages);
+                           suggestedClassName, codeFolderPackages, target);
       if (className == null) {
         throw new RunnerException("Could not find main class");
         // this situation might be perfectly fine,
diff --git a/app/preproc/PdePreprocessor.java b/app/preproc/PdePreprocessor.java
index e0b713f88..5690e3298 100755
--- a/app/preproc/PdePreprocessor.java
+++ b/app/preproc/PdePreprocessor.java
@@ -104,7 +104,8 @@ public class PdePreprocessor {
   //public String write(String program, String buildPath, String name,
   //                  String extraImports[]) throws java.lang.Exception {
   public String write(String program, String buildPath,
-                      String name, String codeFolderPackages[])
+                      String name, String codeFolderPackages[],
+		      Target target)
     throws java.lang.Exception {
     // if the program ends with no CR or LF an OutOfMemoryError will happen.
     // not gonna track down the bug now, so here's a hack for it:
@@ -344,7 +345,7 @@ public class PdePreprocessor {
     //emitter.print(rootNode);
     //emitter.translationUnit(parser.getAST());
 
-    writeFooter(stream);
+    writeFooter(stream, target);
     stream.close();
 
     // if desired, serialize the parse tree to an XML file.  can
@@ -457,20 +458,23 @@ public class PdePreprocessor {
    *
    * @param out         PrintStream to write it to.
    */
-  void writeFooter(PrintStream out) {
-    //out.print("}");
+  void writeFooter(PrintStream out, Target target) throws java.lang.Exception {
+    // Open the file main.cxx and copy its entire contents to the bottom of the
+    // generated sketch .cpp file...
 
-/*    if (programType == STATIC) {
-      // close off draw() definition
-      out.print("noLoop(); ");
-      out.print("}");
+    String mainFileName = target.getPath() + File.separator + "main.cxx";
+    FileReader reader = null;
+    reader = new FileReader(mainFileName);
+
+    LineNumberReader mainfile = new LineNumberReader(reader);
+
+    String line;
+    while ((line = mainfile.readLine()) != null) {
+        out.print(line + "\n");
     }
 
-    if (programType < JAVA) {
-      // close off the class definition
-      out.print("}");
-    }
-*/  }
+    mainfile.close();
+  }
 
 
   static String advClassName = "";
diff --git a/targets/arduino/main.cxx b/targets/arduino/main.cxx
new file mode 100755
index 000000000..52351e4c9
--- /dev/null
+++ b/targets/arduino/main.cxx
@@ -0,0 +1,12 @@
+int main(void)
+{
+	init();
+
+	setup();
+    
+	for (;;)
+		loop();
+        
+	return 0;
+}
+
diff --git a/targets/arduino/wiring.c b/targets/arduino/wiring.c
index b66178c55..7b9123665 100755
--- a/targets/arduino/wiring.c
+++ b/targets/arduino/wiring.c
@@ -549,7 +549,7 @@ void shiftOut(int dataPin, int clockPin, int bitOrder, byte val) {
 	}
 }
 
-int main(void)
+void init()
 {
 	// this needs to be called before setup() or some functions won't
 	// work there
@@ -626,11 +626,4 @@ int main(void)
 #else
 	UCSRB = 0;
 #endif
-
-	setup();
-	
-	for (;;)
-		loop();
-		
-	return 0;
-}
+}
\ No newline at end of file
diff --git a/targets/arduino/wiring.h b/targets/arduino/wiring.h
index 96e6ea6fb..f97230324 100755
--- a/targets/arduino/wiring.h
+++ b/targets/arduino/wiring.h
@@ -66,6 +66,8 @@ extern "C"{
 typedef uint8_t boolean;
 typedef uint8_t byte;
 
+void init(void);
+
 void pinMode(int, int);
 void digitalWrite(int, int);
 int digitalRead(int);
@@ -74,9 +76,9 @@ void analogWrite(int, int);
 
 void beginSerial(long);
 void serialWrite(unsigned char);
-int serialAvailable();
-int serialRead();
-void serialFlush();
+int serialAvailable(void);
+int serialRead(void);
+void serialFlush(void);
 void printMode(int);
 void printByte(unsigned char c);
 void printNewline();