/* parseopt.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "snoop.h"


int	parse_options(int N_options, char *options[]) {
	int i;
	int j;
	int status=0;
	int	hex;
	char* status_msg="";

	for (i = 0; i < N_options; i++) {
		switch(options[i][0]) {
		case '/':  												/* implies option */
		case '-':
			switch (options[i][1]) {
			case 'e':												/* implies ethernet option */
			case 'E':
				switch (options[i][2]) {
				case 's':
				case 'S':
					if (options[i][3] == '=') {
						for (j = 4; options[i][j]; j++) {
							if ((j-4)%3 < 2) {
								hex = hex2dec(options[i][j]);
								if (hex+1) {
									src_tbl[src_index][(int)((j-4)/3)] +=
										((j-4)%3)?hex:hex << 4;
								} else {
									status = 1;
									status_msg = "Parse error in ethernet source address";
								}
							}
						}
						if (!status) src_index++;
						else for (j=0; j < 6; j++) src_tbl[src_index][j] = 0;
					}
					break;

				case 'd':
				case 'D':
					if (options[i][3] == '=') {
						for (j = 4; options[i][j]; j++) {
							if ((j-4)%3 < 2) {
								hex = hex2dec(options[i][j]);
								if (hex+1) {
									dst_tbl[dst_index][(int)((j-4)/3)] +=
										((j-4)%3)?hex:hex << 4;
								} else {
									status = 1;
									status_msg = "Parse error in ethernet destination address";
								}
							}
						}
						if (!status) dst_index++;
						else for (j=0; j < 6; j++) dst_tbl[dst_index][j] = 0;
					}
					break;

				case 'a':
				case 'A':
					if (options[i][3] == '=') {
						for (j = 4; options[i][j]; j++) {
							if ((j-4)%3 < 2) {
								hex = hex2dec(options[i][j]);
								if (hex+1) {
									src_tbl[src_index][(int)((j-4)/3)] +=
										((j-4)%3)?hex:hex << 4;
									dst_tbl[dst_index][(int)((j-4)/3)] +=
										((j-4)%3)?hex:hex << 4;
								} else {
									status = 1;
									status_msg = "Parse error in ethernet address";
								}
							}
						}
						if (!status) {
							src_index++;
							dst_index++;
						} else for (j=0; j < 6; j++)
							src_tbl[src_index][j] = dst_tbl[dst_index][j] = 0;
					}
					break;

				default:
					status = 1;
					status_msg = "Parse error following ethernet option \"e\"";
				} /* end e switch */

				break; /* break out of case e */

			case 'h':
			case 'H':
			case '?':
				fprintf(stdout,"This is the help page. :)\n");
				break; /* break out of case h,? */

			case 'l':
			case 'L':
				fprintf(stdout,"Log files are currently unsupported\n");
				break; /* break out of case l */

			default:
				status = 1;
				fprintf(stderr,"Unrecognized option %c%c\n",options[i][0],
								options[i][1]);
			}

			break; /* break for - or / */

		default:
			status = 1;
			fprintf(stderr,"Sorry, unsupported command\n");
		} /* unrecognized symbol */
	} /* end for loop */

	if (status_msg) fprintf(stderr,"%s\n",status_msg);
	return(status);
}


