Commits

Steven! Ragnarök  committed a5d8a02

Free memory allocated to AST after each command is run.

  • Participants
  • Parent commits a32e33a

Comments (0)

Files changed (2)

File include/interpreter.h

 int *exitstatus_head;
 int exitstatuses[MAX_SAVED_EXITSTATUSES];
 void set_exitstatuses(void);
+
+/* Cleanup */
+void free_ast(ast_node *root);
+void free_ast_list(ast_nodelist *list);

File src/interpreter.c

 
 	fifo_push(MAX_SAVED_EXITSTATUSES, exitstatuses, exitstatus_head, exit_status);
 	set_exitstatuses();
-	/* Free AST */
-	/* Free tokens */
+	free_ast(root);
 	return;
 }
 
 	int cmdstats, argc = 0, execerr;
 	pid_t cmdpid;
 	char *argv[MAX_TOKEN_LENGTH];
-	ast_nodelist *arg, *arg_list = command->children;
+	ast_nodelist *arg;
 
 	/* Build the values to pass to execvp */
 	argv[argc] = interpret_value(command);
 	++argc;
 
-	arg = arg_list->node->children;
-	
-	while (arg != NULL) {
-		if (arg->node->type == VARIABLE) {
-			argv[argc] = interpret_variable(arg->node);
-		} else {
-			argv[argc] = interpret_value(arg->node);
+	if (command->children != NULL && command->children->node->type == ARGLIST) {
+		arg = command->children->node->children;
+
+		while (arg != NULL) {
+			if (arg->node->type == VARIABLE) {
+				argv[argc] = interpret_variable(arg->node);
+			} else {
+				argv[argc] = interpret_value(arg->node);
+			}
+			++argc;
+			arg = arg->next;
 		}
-		++argc;
-		arg = arg->next;
 	}
 
 	/* Null-terminate the list as per execvp's documentation. */
 	return;
 }
 
+void free_ast(ast_node *root) {
+	if (root != NULL) {
+		if (root->children == NULL) {
+			free(root);
+		} else {
+			free_ast_list(root->children);
+		}
+	}
+}
+
+void free_ast_list(ast_nodelist *list) {
+	ast_nodelist *next;
+	while (list != NULL) {
+		next = list->next;
+		free_ast(list->node);
+		free(list);
+		list = next;
+	}
+}
+