summarylogtreecommitdiffstats
path: root/handle.c
blob: 1d18b8c2fc824643510588496c22a6a8dc716346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "handle.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

int error(char* message) {

	fprintf(stderr, "%s: %s\n", "ttyvideo", message);

	return 1;

}

static int numOptionsSpecified = 0;
static int numUniqueOptionsSpecified = 0;
static char** argumentCallMap = NULL;
static char** argumentMemStore = NULL;
static int*   argumentNumStore = NULL;
static char** helpMessages = NULL;
static char* defaultArg = NULL;
static char* defaultArgHelp = NULL;

#define MAX_ARG_SIZE 200

char* setDefaultArgument(char* helpText) {

	defaultArg = (char*)malloc(sizeof(char) * MAX_ARG_SIZE);
	defaultArgHelp = (char*)malloc(sizeof(char) * strlen(helpText));
	strcpy(defaultArgHelp, helpText);

	defaultArg[0] = '\0';

	return defaultArg;

}

#define MAX_ACCESS_MAP_SIZE 100
#define MAX_HELP_MESSAGE_ARRAY_SIZE 100

char* addArgument(char* helpText, int numArguments, char* firstCall, char* secondCall) {

	if(firstCall == NULL) {
		fprintf(stderr, "No option specified for parameter\n");
		return NULL;
	}
	if(firstCall[0] != '-') {
		fprintf(stderr, "Option '%s' does not start with a '-'\n", firstCall);
		return NULL;
	}
	if(secondCall != NULL && secondCall[0] != '-') {
		fprintf(stderr, "Option '%s' does not start with a '-'\n", secondCall);
	}

	if(argumentCallMap == NULL) {
		argumentCallMap = (char**)malloc(sizeof(char*) * MAX_ACCESS_MAP_SIZE);
	}
	if(argumentMemStore == NULL) {
		argumentMemStore = (char**)malloc(sizeof(char*) * MAX_ACCESS_MAP_SIZE);
	}
	if(argumentNumStore == NULL) {
		argumentNumStore = (int*)malloc(sizeof(int*) * MAX_ACCESS_MAP_SIZE);
	}
	if(helpMessages == NULL) {
		helpMessages = (char**)malloc(sizeof(char*) * MAX_HELP_MESSAGE_ARRAY_SIZE);
	}

	// TODO: Add ability to allocate lots of these
	char* argumentMemLocation = (char*)malloc(sizeof(char) * MAX_ARG_SIZE);
	if(argumentMemLocation == NULL) {
		fprintf(stderr, "Couldn't allocate memory to store argument to pass to %s", firstCall);
		return NULL;
	}
	argumentMemLocation[0] = '\0';

	int argumentAccessLocation = numOptionsSpecified++;
	argumentCallMap[argumentAccessLocation] = (char*)malloc(sizeof(char) * strlen(firstCall));
	strcpy(argumentCallMap[argumentAccessLocation], firstCall);
	argumentMemStore[argumentAccessLocation] = argumentMemLocation;
	argumentNumStore[argumentAccessLocation] = numArguments;


	if(secondCall == NULL) {

		helpMessages[numUniqueOptionsSpecified] = (char*)malloc(sizeof(char) * (strlen(helpText) + strlen(firstCall) + 4));

		strcpy(helpMessages[numUniqueOptionsSpecified], firstCall);
		strcat(helpMessages[numUniqueOptionsSpecified], ":\t");
		strcat(helpMessages[numUniqueOptionsSpecified], helpText);

		numUniqueOptionsSpecified++;

		return argumentMemLocation;
	}

	helpMessages[numUniqueOptionsSpecified] = (char*)malloc(sizeof(char) * (strlen(helpText) + strlen(firstCall) + 2 + 4 + strlen(secondCall)));

	strcpy(helpMessages[numUniqueOptionsSpecified], firstCall);
	strcat(helpMessages[numUniqueOptionsSpecified], ", ");
	strcat(helpMessages[numUniqueOptionsSpecified], secondCall);
	strcat(helpMessages[numUniqueOptionsSpecified], ":\t");
	strcat(helpMessages[numUniqueOptionsSpecified], helpText);

	numUniqueOptionsSpecified++;

	argumentAccessLocation = numOptionsSpecified++;
	argumentCallMap[argumentAccessLocation] = (char*)malloc(sizeof(char) * strlen(secondCall));
	strcpy(argumentCallMap[argumentAccessLocation], secondCall);
	argumentMemStore[argumentAccessLocation] = argumentMemLocation;
	argumentNumStore[argumentAccessLocation] = numArguments;

	return argumentMemLocation;
}

void printUsage() {

	if(helpMessages != NULL) {
		register int i;
		for(i = 0; i < numUniqueOptionsSpecified; ++i) {
			printf("%s\n", helpMessages[i]);
		}
	} else {
		printf("No options to display\n");
	}

}

int handle(int argc, char** argv) {

	register int i, j;
	int numArguments;
	for(i = 1; i < argc; ++i) {
		for(j = 0; j < numOptionsSpecified; ++j) {
			if(argv[i][0] != '-') {
				if(defaultArg == NULL) {
					fprintf(stderr, "Argument '%s' was passed without an option, and no default argument has been set\n", argv[0]);
					return 1;
				}
				if(defaultArg[0] != '\0') {
					fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
					return 1;
				}
				strcpy(defaultArg, argv[i]);
				break;
			}
			if(strcmp(argv[i], argumentCallMap[j]))
				continue;
			numArguments = argumentNumStore[j];
			if(numArguments) {
				if(i < argc - 1 && argv[i+1][0] != '-') {
					strcpy(argumentMemStore[j], argv[++i]);
				} else {
					fprintf(stderr, "Option '%s' requires an argument\n", argv[i]);
					return 1;
				}
			}
			else
				strcpy(argumentMemStore[j], "1"); // The option is present
			break;
		}
		if(j == numOptionsSpecified) {
			fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
			return 1;
		}
	}

	return 0;

}

void sig_handler(int signo) {
	if(signo == SIGINT)
		terminate = 1;
}