Commits

Steven! Ragnarök committed cfc4897 Merge

Merge in changes from Sean.

Comments (0)

Files changed (5)

examples/src/path_lookup_test.c

+#include <stdlib.h>
+#include <stdio.h>
+#include "path_lookup.h"
+#include <string.h>
+
+int main(int argc, char *argv[])
+{
+  char* path1 = strdup("/home/sean/Code/sh142"); // avoid a hard coded string. dynamically allocates memory with malloc
+  char* path2 = strdup("/home/sean/Code/sh142/src"); // avoid a hard coded string. dynamically allocates memory with malloc
+  setenv("DATA", "/home/sean/Dropbox/Lotus", 1);
+
+  char* result1 = searchPath(path1, "Gemfile.lock");
+  char* result2 = searchPath(path2, "fifo.c");
+  char* result3 = searchPath(path2, "fifo.h");
+  char* result4 = resolve_path("ls");
+  char* result5 = resolve_path("doesnotExist.cmpe");
+  char* result6 = resolve_data("mods.txt");
+  char* result7 = resolve_data("doesnotExist.cmpe");
+
+  printf("%s\n", result1);
+  printf("%s\n", result2);
+
+  if(result3 == NULL)
+    printf("fifo.h could not be found in: %s\n", path2);
+  else
+    printf("%s\n", result3);
+
+  printf("%s\n", result4);
+
+  if(result5 == NULL)
+    printf("doesnotExist.cmpe could not be found in: PATH\n");
+  else
+    printf("%s\n", result5);
+
+  printf("%s\n", result6);
+
+  if(result7 == NULL)
+    printf("doesnotExist.cmpe could not be found in: DATA\n");
+  else
+    printf("%s\n", result7);
+  
+  return 0;
+}
  * is returned with the contents of the old string up until the final newline.
  * Otherwise the original string is returned unmodified.
  */
-#include <string.h>
 
-char *chomp(char *str)
-{
-	if(str[strlen(str)] == '\n']) // if the last character in the string is a newline character
-	{
-		char* ptrToNewLine = strrchr(str);
-		*ptrToNewLine = '\0'; // replace the last newline with the null terminator character, thus truncating the string
-	}
-}
-
-char* chomp2(char* inputString) // this chomp actually follows steven's directions
-{
-	if(inputString[strlen(inputString)] == '\n') // detect the string that ends in a newline
-	{
-		char[] newString = new char[strlen(str)-1]; // allocate memory for a new string to length-1 of the last string
-		for(int x =0; x < strlen(inputString); x++)
-			newString[x] = *inputString[x]; // copy the string contents over
-		inputString = newString; // assign the pointer to the new value
-	}
-	return inputString; // return the inputString pointer, modified only if the original string ended in a newline
-}
+char *chomp(char* str);

report/chatlog.log

+Offline	
+(3) Le,Steven,me
+Le
+Steven
+	
+ 
+	 	
+  Actions ▼
+ You have invited Le to this chat.
+ This is now a group chat. Add another person.
+ Le Clair, Nicolas has joined.
+ me:  no one pushed anythign yet?
+ Sent at 11:31 PM on Friday
+ me:  is this thing on?
+ Sent at 11:34 PM on Friday
+ Steven Ragnarök has joined.
+ Steven:  hey
+so
+I pushed a very primitive tokenizer+parser+interpreter
+Nick, the questions that you asked in email
+can basically be answered by comparing what you have to tokenizer.c
+ me:  i think Nick is afk
+ Steven:  I also switched away from using a fixed-length array
+and am using a linked list
+He can read when he gets back
+I am stuck for time
+there's a short in my power cable
+ me:  well that's unfortuante
+ Steven:  and of course, everyone I'm with is Mac OS only
+yeah
+fucking seriously
+ me:  i still can't build =\
+trying to figure that out
+ Steven:  build at all
+ me:  i can build examples
+ Steven:  or build your path_lookup
+ me:  because there's no readline reference
+ Steven:  gotcha
+try pulling latest
+I may have fixed some stuff
+ me:  abort: crosses branches
+ Steven:  wat?
+ me:  sean@ubuntu:~/Code/sh142$ hg update
+abort: crosses branches (merge branches or use --clean to discard changes)
+ Steven:  what changes have you made and are they important?
+ me:  just changed a couple lines in one file
+i can re do it
+ Steven:  k
+ me:  is it hg update --clean?
+ Steven:  yeah
+at least, I think os
+ me:  nice, no more readline error
+ Steven:  there are three new header, source pairs
+ me:  this did pop up though:
+
+src/interpreter.c:23:25: error: request for member ‘token’ in something not a structure or union
+make: * [obj/interpreter.o] Error 1
+ Sent at 11:50 PM on Friday
+ Steven:  ah
+just fixed that
+I'm going to dump a bunch of information into the channel while I still have power
+you can either use it to try and add more code
+or use it to write the report
+but unless I can borrow a Thinkpad power cord tomorrow, I'm basically boned until I get home to my spare on Sunday evening
+There are three new source/header pairs
+tokenizer, parser, and interpreter
+ Sent at 11:53 PM on Friday
+ Steven:  the tokenizer separates on spaces only
+ Sent at 11:55 PM on Friday
+ Steven:  I'm thinking for simplicity's sake, we will require spaces between all tokens
+unless we have time to extend the tokenizer later
+the parser is the first "tricky" part of the code
+in include/sh142types.h I define some compound data structers.
+The simplest is a strlist, a linked list of strings.
+Then there is also the ast_node and ast_nodelist
+ast stands for Abstract Syntax Tree
+basically, the parser takes the stream of input tokens
+and looks at one at a time to figure out where the next token fits into the parse
+this technique is called recursive descent parsing
+It is the simplest type of parser that can still work for a whole shell
+ Sent at 11:59 PM on Friday
+ me:  how can I make one thing at a time again? I tried make path_lookup.o
+
+The readline error came back after pulling
+ Steven:  After being turned into an abstract syntax tree, the ast is sent to be interpreted.
+if you think of the parsing phase as bundling everything up
+interpretation is unpacking it and playing with it
+make obj/path_lookup.o
+ Sent at 12:02 AM on Saturday
+ Steven:  Right now it's pretty primitive
+it can only run plain jane commands
+I was in the process of adding Variable expansion when I noticed the charging light wasn't own
+*on
+ Sent at 12:04 AM on Saturday
+ Steven:  There's a description of the grammar that I wanted to parse in parser by tonight is in parser.h
+haha
+can't sentence properly
+feel free to start on that
+if you want
+I'll let you know if I find a cord
+ Sent at 12:07 AM on Saturday
+ me:  okay
+shouldn't be too hard to find oen
+one*
+don't forget to check craigslist
+ Sent at 12:09 AM on Saturday
+ 
+	
+
+	
+	
+	
+	
+	
+	
+	
+	
+	
+Pop-in
 		return str;
 	}
 }
+
+
+// #include <string.h>
+
+// char *chomp(char *str)
+// {
+// 	if(str[strlen(str)] == '\n']) // if the last character in the string is a newline character
+// 	{
+// 		char* ptrToNewLine = strrchr(str);
+// 		*ptrToNewLine = '\0'; // replace the last newline with the null terminator character, thus truncating the string
+// 	}
+// }
+
+// char* chomp2(char* inputString) // this chomp actually follows steven's directions
+// {
+// 	if(inputString[strlen(inputString)] == '\n') // detect the string that ends in a newline
+// 	{
+// 		char[] newString = new char[strlen(str)-1]; // allocate memory for a new string to length-1 of the last string
+// 		for(int x =0; x < strlen(inputString); x++)
+// 			newString[x] = *inputString[x]; // copy the string contents over
+// 		inputString = newString; // assign the pointer to the new value
+// 	}
+// 	return inputString; // return the inputString pointer, modified only if the original string ended in a newline
+// }

src/path_lookup.c

 #include <stdlib.h>
 #include <string.h>
 #include <dirent.h> // angle brackets signify library code
-#include "../include/path_lookup.h" // quotes signify your local code
+// #include "path_lookup.h" // quotes signify your local code
 #include <sys/types.h>
 #include <stdio.h>
 
 const char* delimToken = ":";
+char* qualifiedPath;
 
 //http://stackoverflow.com/questions/612097/how-can-i-get-a-list-of-files-in-a-directory-using-c-or-c
 char* searchPath(char* inputPath, char* inputExecutableName)
 {
-	DIR *dir;
-	struct dirent *ent;
-	dir = opendir (inputPath);
-	if (dir != NULL) 
-	{
-	  while ((ent = readdir (dir)) != NULL) /* print all the files and directories within directory */
+	DIR *directory = opendir(inputPath);
+	struct dirent *entry = NULL;
+	if (directory == NULL) // if path was invalid
+		return NULL; 
+	  while((entry = readdir(directory))) /* print all the files and directories within directory */
 	  {
-		if (strlen(ent->d_name) == strlen(inputPath) && !strcmp(ent->d_name, inputExecutableName))  // if the length of the current file and the name are the same
+		if (strcmp(entry->d_name, inputExecutableName) == 0)  // if the strings are equal
         {
-        	(void)closedir(dir); // close the file handle
-            return ent->d_name; // return the current file name
+        	closedir(directory); // close the file handle
+            qualifiedPath = (char*) malloc(strlen(inputPath) + strlen(inputExecutableName)); // leave one extra space for the /
+            strcat(qualifiedPath, inputPath);
+            strcat(qualifiedPath, "/");
+            strcat(qualifiedPath, inputExecutableName);           
+            return qualifiedPath; // return the fully qualified path to the program
         }
-	    	printf("%s\n", ent->d_name);
 	  }
-	  closedir(dir);
-	} 
+	  closedir(directory);
 	return NULL;
 }