Commits

Steven! Ragnarök committed 2aac5b3

Add remote commands through a cool parser hack.

  • Participants
  • Parent commits 05bfb83

Comments (0)

Files changed (2)

  * Arg                ::= VariableExpression | Value
  * VariableExpression ::= [Arg]'$'Value[Arg]
  * VarAssign          ::= Value'='Arg
- * // Not Yet Implemented //
  * RedirectList       ::= Redirect [RedirectList]
  * Redirect           ::= StdinRedirect | StdoutRedirect | StderrRedirect
  * StdinRedirect      ::= '<' Arg
  * StdoutRedirect     ::= '>' Arg
  * StderrRedirect     ::= '2>' Arg
+ * // Not Yet Implemented //
+ * NetworkCommand     ::= NodeName':'Command
+ * NodeName           ::= Value
+ * DataVariable       ::= #D:Value
  * Chain              ::= AndedCommand | OrredCommand
  * AndedCommand       ::= '&&' Command
  * OrredCommand       ::= '||' Command
  * BackgroundKey      ::= '&'
- * NetworkCommand     ::= NodeName':'Command
  */
 
 strlist *tokens;
 ast_node *parse_value(void);
 ast_node *parse_redirect_list(void);
 ast_node *parse_redirect(void);
+ast_node *parse_network_command(int colonidx);
+
 
 /* Helper functions */
 void strip_head(void);
 int contains_eql(char *str);
+int contains_colon(char *str);
 int is_special_token(void);
 int is_redirect_token(void);
 void parser_error(void);
 #include "../include/parser.h"
 
 ast_node *parse() {
-	int eql;
+	int eql, colon;
 	ast_node *root;
 	if ((eql = contains_eql(tokens->str)) != -1) {
 		root =  parse_var_assign(eql);
+	} else if ((colon = contains_colon(tokens->str)) != -1) {
+		root = parse_network_command(colon);
 	} else {
 		root = parse_command();
 	}
 	return var_assign;
 }
 
+ast_node *parse_network_command(int colonidx) {
+	ast_node *network_command;
+	strlist *ssh_token, *hostname_token;
+	/* Insert new tokens for the ssh cmd and hostname. */
+	ssh_token = malloc(sizeof(strlist));
+	hostname_token = malloc(sizeof(strlist));
+	strncpy(ssh_token->str, "ssh", MAX_TOKEN_LENGTH);
+
+	/* Nullify the colon to split the string. Take the first half as hostname. */
+	tokens->str[colonidx] = '\0';
+	strncpy(hostname_token->str, tokens->str, MAX_TOKEN_LENGTH);
+
+	/* Copy the latter (command) portion of the original head of tokens to the
+	 * front of the string.
+	 */
+	strncpy(tokens->str, &tokens->str[colonidx+1], MAX_TOKEN_LENGTH - (colonidx + 1));
+	/* Set the ssh token to point to the hostname token and the hostname token
+	 * to point to the orginal command. Then make the ssh token the head of the
+	 * tokens linked list.
+	 */
+	ssh_token->next = hostname_token;
+	hostname_token->next = tokens;
+	tokens = ssh_token;
+	return parse_command();
+}
+
 /* Helper Functions */
 void strip_head() {
 	if (tokens == NULL) {
 	return -1;
 }
 
+/* Returns the index of the colon character within the string or
+ * -1 if none is present.
+ */
+int contains_colon(char *str) {
+	int i;
+	for (i = 0; i < strlen(str); i++) {
+		if (str[i] == ':') {
+			return i;
+		}
+	}
+	return -1;
+}
+
+
 int is_special_token() {
 	/* Piggyback off redirect helper. */
 	if (is_redirect_token()) {