Added Changes to make packet compile with EMCC so it can be run with
Added Changes to make packet compile with EMCC so it can be run with
node and compiled to js

file:a/makefile -> file:b/makefile
--- a/makefile
+++ b/makefile
@@ -9,7 +9,9 @@
 ###############################################################################
 
 CC=gcc
-CFLAGS=-O3 -Wall
+CFLAGS=-O2 -Wall
+
+EXT=
 
 SOUND_DEVICE_MODE=-D__OSS__
 SOUND_DEVICE_LIB=
@@ -55,6 +57,11 @@
 	$(eval SOUND_DEVICE_MODE=-D__PULSE__)
 	$(eval SOUND_DEVICE_LIB=-lpulse -lpulse-simple)
 
+# Build with EmScripten
+compiler-emscripten:
+	@echo "Building with emscripten."
+	$(eval CC=emcc)
+
 ###############################################################################
 # Mixes 2 raw audio files                                                     #
 ###############################################################################
@@ -153,8 +160,11 @@
 # AFSK Demodulator / APRSPacket Decoder                                       #
 ###############################################################################
 
-bin/packet: build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o
-	$(CC) $(CFLAGS) -lm $(DEFINES) -o $@ build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o
+bin/packet: build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o build/obj/argp.o
+	$(CC) $(CFLAGS) -lm $(DEFINES) -o $@ build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o build/obj/argp.o
+
+bin/packet.js: build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o build/obj/argp.o
+	$(CC) $(CFLAGS) $(DEFINES) -o $@ build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o build/obj/argp.o
 
 build/obj/packet_main.o: src/packet/packet_main.c
 	$(CC) $(CFLAGS) $(DEFINES) -o $@ -c src/packet/packet_main.c
@@ -191,6 +201,9 @@
 ###############################################################################
 # Utility functions                                                           #
 ###############################################################################
+
+build/obj/argp.o: src/util/argp.c
+	$(CC) $(CFLAGS) -o $@ -c src/util/argp.c
 
 build/obj/Util.o: src/util/Util.cpp
 	g++ -O3 -o $@ -c src/util/Util.cpp

--- a/src/packet/packet_main.c
+++ b/src/packet/packet_main.c
@@ -168,6 +168,14 @@
 
 int main(int argc, char **argv){
 
+	#ifdef EMSCRIPTEN
+		EM_ASM(
+				if(typeof window === 'undefined'){
+					FS.mount(NODEFS, { root: '.' }, '/');
+				}
+		);
+	#endif
+
 	struct arguments arguments;
 
 	arguments.filename = NULL;
@@ -184,7 +192,10 @@
 	arguments.offset = 0.0925;
 	arguments.noise_floor = 0;
 
-	argp_parse(&argp, argc, argv, 0, 0, &arguments);
+	error_t parsed = argp_parse(&argp, argc, argv, 0, 0, &arguments);
+
+	if(parsed != 0)
+		return parsed;
 
 	FILE *input_file = NULL;
 

--- a/src/packet/packet_main.h
+++ b/src/packet/packet_main.h
@@ -15,6 +15,10 @@
  * frequency of the two AFSK tones, offset multiplier, and noise floor.
  */
 
+#ifdef EMSCRIPTEN
+	#include <emscripten.h>
+#endif
+
 #include <stdlib.h>
 
 #include <stddef.h>
@@ -22,7 +26,7 @@
 #include <ctype.h>
 #include <stdio.h>
 
-#include <argp.h>
+#include "../util/argp.h"
 
 #include "AFSK_Demodulator.h"
 

file:b/src/util/argp.c (new)
--- /dev/null
+++ b/src/util/argp.c
@@ -1,1 +1,93 @@
 
+#include "argp.h"
+
+error_t argp_parse (struct argp *argp, int argc, char **argv, unsigned flags, int *arg_index,  void *input){
+
+	struct argp_state state;
+
+	state.input = input;
+
+	int i;
+
+	for(i = 1; i < argc; i++){
+
+		if(argv[i][0] == '-'){
+
+			if(argv[i][1] == '-'){
+
+				struct argp_option *op = argp->options;
+
+				bool found = false;
+
+				int c = 0;
+
+				while(op->arg[0] != 0){
+
+					c = 0;
+
+					while(true){
+
+						// They are equal!
+						if(op->name[c] == '\0' && (argv[i][c+2] == '\0' || argv[i][c+2] == '=')){
+							found = true;
+							break;
+						}
+
+						// Finish if one string is a different length or if they have different values
+						else if(op->name[c] == '\0' || argv[i][c+2] == '\0' || argv[i][c+2] == '=' || op->name[c] != argv[i][c+2]){
+							break;
+						}
+
+						c++;
+
+					}
+
+					// If we found the option match the parameter
+					if(found){
+						break;
+					}
+
+					op++;
+
+				}
+
+				if(found){
+					argp->parser(op->key, &argv[i][c+3], &state);
+				}
+
+				// Invalid Option
+				else {
+					return -1;
+				}
+
+			} else {
+
+				// single character option
+				struct argp_option *op = argp->options;
+
+				while(op->arg[0] != 0){
+
+					if(op->key == argv[i][1] && argv[i][2] == '\0'){
+						argp->parser(op->key, NULL, &state);
+						break;
+					}
+
+					op++;
+
+				}
+
+			}
+
+		}
+
+		// Invalid Option
+		else {
+			return -1;
+		}
+
+	}
+
+	return 0;
+
+}
+

file:b/src/util/argp.h (new)
--- /dev/null
+++ b/src/util/argp.h
@@ -1,1 +1,72 @@
 
+#include <stdio.h>
+#include <stdbool.h>
+
+#define OPTION_ARG_OPTIONAL 0
+
+#define ARGP_ERR_UNKNOWN 0
+
+struct argp_option {
+
+	const char *name;
+
+	int key;
+
+	const char *arg;
+
+	int flags;
+
+	const char *doc;
+	int group;
+
+};
+
+#ifndef __error_t_defined
+typedef int error_t;
+#endif
+
+struct argp_state {
+
+	const struct argp *root_argp;
+
+	int argc;
+	char **argv;
+
+	int next;
+
+	unsigned flags;
+
+	unsigned arg_num;
+
+	int quoted;
+
+	void *input;
+	void **child_inputs;
+
+	void *hook;
+
+	char *name;
+
+	FILE *err_stream;
+	FILE *out_stream;
+
+	void *pstate;
+
+};
+
+typedef error_t (*argp_parser_t) (int __key, char *__arg, struct argp_state *__state);
+
+struct argp {
+
+	struct argp_option *options;
+
+	argp_parser_t parser;
+
+	const char *args_doc;
+
+	const char *doc;
+
+};
+
+error_t argp_parse (struct argp *argp, int argc, char **argv, unsigned flags, int *arg_index,  void *input);
+