r3275 - alfs-POC/src

jhuntwork at linuxfromscratch.org jhuntwork at linuxfromscratch.org
Sat Dec 9 17:47:51 PST 2006


Author: jhuntwork
Date: 2006-12-09 18:47:51 -0700 (Sat, 09 Dec 2006)
New Revision: 3275

Removed:
   alfs-POC/src/parser.c
Modified:
   alfs-POC/src/parser.cpp
Log:
Commands now fully parsed and currently placed in scriptlets.

Deleted: alfs-POC/src/parser.c
===================================================================
--- alfs-POC/src/parser.c	2006-12-09 21:55:45 UTC (rev 3274)
+++ alfs-POC/src/parser.c	2006-12-10 01:47:51 UTC (rev 3275)
@@ -1,256 +0,0 @@
-/*
-   Parser for the LFS Book. Grabs only executable commands.
-   Writtem by Jeremy Huntwork, 20051229
-*/
-   
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#define MAXLINE 4096
-#define CMDSDIR "commands"
-
-
-int parse_file(char *filename);
-
-int close_tag(char *line);
-
-int close_comment(char *line);
-
-int output_commands(char *dirname, char *filename);
-
-char commands[1024];
-int multi, comment, count=0;
-
-int main (int argc, char **argv)
-{
-  /* Check that we have an input file */
-  if (argc != 2){
-	printf("Usage: %s [FILE]\n", argv[0]);
-	return(-1);
-  }
-
-  /* We've been given a file, let's start parsing */
-  if ((parse_file(argv[1])) == -1)
-	return(-1);
-
-  return(0);
-
-}
-
-int parse_file(char *filename) {
-
-  FILE *fp;
-  char buf[MAXLINE+1];
-  char *href, *url, *loc, *loc2, *parent;
-  char wd[512], path[512];
-  int len;
-
-  /* Get the path to the parent directory of filename;
-     start by getting the last instance of a '/' */
-  if ((loc = strrchr(filename, '/')) != NULL) {
-
-  	/* Get the number position of the last '/' */
-	len = loc-filename;
-
-	/* Copy the path up to the last '/' to a new buffer */
-	strncpy(path, filename, len);
-
-	/* Add a terminating character to the end of the string */
-	path[len] = '\0';
-	
-	/* Shorten the filename to be just the name of the file,
-	   not the full path */
-	filename = loc+1;
-
-	/* Change directories to the path */
-	if ((chdir(path)) == -1)
-		perror(path);
-
-  }
-
-  printf("Parsing file: `%s\'\n", filename);
-
-  /* Open the file for reading */
-  fp = fopen(filename, "r");
-
-  /* Make sure we have a readable file */
-  if (fp == NULL){
-	perror(filename);
-	return(-1);
-  }
-
-  /* Add this file to the tally. */
-  count++;
-  printf("Files parsed: %d\n", count);
-
-  /* Set the current working directory, in case we leave it,
-     and so we can get the name of the parent directory for
-     the file we're parsing */
-  getcwd(wd, 256);
-
-  /* Get the name of the parent directory only */
-  if ((loc2 = strrchr(wd, '/')) == NULL) {
-	perror(loc2);
-	return(-1);
-  }
-  parent = loc2+1;
-  
-  /* Read one line at a time from the opened file
-     until the end of the file is reached */
-  while (!feof(fp)) {
-
-	/* Put the current line into a buffer */
-	fgets(buf, MAXLINE, fp);
-
-	/* First let's check if we're continuing from a previous line.
-	   That is, we either found an opening <screen> AND <userinput> and we
-	   want to make sure we get all the stuff up to the closing tags,
-	   or we found a comment. */
-
-	if (comment == 1) {
-		close_comment(buf);
-		continue;
-	}
-
-	if (multi == 1) {
-
-		/* Append the line to the current commands string */
-		strcat(commands, buf);
-
-		/* And see if we now have the closing tag */
-		if ((close_tag(buf)) == -1)
-			continue;
-		output_commands(parent, filename);
-		continue;
-	}
-
-	/* Seems we're not continuing from a previous line;
-	   so check to see if this line is a comment. */
-	if ((strstr(buf, "<!--")) != NULL)
-	{
-		close_comment(buf);
-		continue;
-	}
-	/* Now let's see if we have <screen> AND <userinput> tags
-	   which is the marker of a command to be run */
-	if ((strstr(buf, "<screen>")) != NULL)
-	{
-	  /* Now let's make sure it's not marked as a role="nodump" */
-	  if ((strstr(buf, "nodump")) != NULL)
-		continue;
-
-	  if ((strstr(buf, "<userinput>")) != NULL) {
-		/* OK, we've got a command we want; Copy it to the commands
-		   buffer. */
-		strcpy(commands, buf);
-
-		/* Now check to see if the closing tag is on the same line */
-		if ((close_tag(buf)) == -1)
-			continue;
-		output_commands(parent, filename);
-		continue;
-	  }
-	}
-
-	/* No userinput tag, how about a 'xi:include'?
-	   If there is a match, look for href */
-	if ((strstr(buf, "xi:include")) != NULL) {
-
-		/* Make sure it's not just grabbing a link to some text
-		   in a future chapter.
-		   FIXME: Find a better way, in case it's ever decided
-		   later to use this tag to also pull in commands. */
-		if ((strstr(buf, "xpointer")) != NULL)
-			continue;
-
-		/* If there is an href, we'll follow it,
-		   but first we have to get the URL :-) */
-		href = strstr(buf, "href");
-		if (href != NULL) {			
-
-			/* Initiate the strtok function - find
-			   the first occurence of the delimiter,
-			   i.e., the first quote */
-			url = strtok(href,"\"");
-
-		  	/* Now grab the first quote-delimited string,
-			   which should invariably be the ... in href="..." */
-			url = strtok(NULL, "\"");
-
-			/* We have the URL to the file, so let's parse it */
-			if ((parse_file(url)) == -1) {
-				perror(url);
-				return(-1);
-			}
-
-			/* We're back from parsing the included file; we need
-			   to change back to our old dir before moving on to
-			   the next line of the original file */
-			if ((chdir(wd)) == -1)
-				perror(wd);
-		}		
-	}
-  }
-
-  fclose(fp);
-  
-  return(0);
-}
-
-int close_tag(char *line) {
-/* Find a closing </userinput> tag on the provided string
-   and set the multi int accordingly */
-
-  if ((strstr(line, "</userinput>")) == NULL) {
-	multi=1;
-	return(-1);
-  }
-  multi=0;
-  return(0);
-}
-
-int close_comment(char *line) {
-  if ((strstr(line, "-->")) == NULL) {
-	comment=1;
-	return(-1);
-  }
-  comment=0;
-  return(0);
-}
-
-int output_commands(char *dirname, char *filename) {
-
-  char *loc;
-  char fullpath[512], strippedfile[256], num[4];
-  int len;
-  
-  if (count < 100)
-	sprintf(num, "0%d", count);
-  else
-        sprintf(num, "%d", count); 
-
-  /* Strip the filename of '.xml' */
-  loc = strchr(filename, '.');
-  len = loc-filename;
-  strncpy(strippedfile, filename, len);
-  strippedfile[len] = '\0';
-
-  /* Append each part of the path to a long string */
-  strcpy(fullpath, CMDSDIR);
-  strcat(fullpath, "/");
-  strcat(fullpath, dirname);
-  strcat(fullpath, "/");
-  strcat(fullpath, num);
-  strcat(fullpath, "-");
-  strcat(fullpath, strippedfile);
-
-  printf("PATH OF FILE TO WRITE: %s\n", fullpath);
-
-  /* mkdir(CMDSDIR, 0755); */
-
-  printf("---COMMANDS---\n%s\n", commands);
-
-  return(0);
-
-}

Modified: alfs-POC/src/parser.cpp
===================================================================
--- alfs-POC/src/parser.cpp	2006-12-09 21:55:45 UTC (rev 3274)
+++ alfs-POC/src/parser.cpp	2006-12-10 01:47:51 UTC (rev 3275)
@@ -1,6 +1,8 @@
 #include <iostream>
 #include <fstream>
 #include <string>
+#include <sstream>
+#include <sys/stat.h>
 using namespace std;
 
 int parse_file(string filename);
@@ -10,7 +12,11 @@
 string swap_ent(string ent_value);
 string find_ent(string ent_name);
 string parsed_ent_files;
+string parsed_files;
+char rootwd[256];
 
+int parsecount = 0;
+
 struct entities {
   string name;
   string value;
@@ -34,11 +40,20 @@
   add_entity("amp \"&\"");
   add_entity("quot \"\"\"");
 
+  /* Find our root directory - the one we ran the command from */
+  getcwd(rootwd, 256);
+  cout << "Working directory: " << rootwd << endl;
+
+  mkdir("commands", 0755);
+  cout << "Created directory: commands" << endl;
+
   /* Now parse the file */
   string fn = argv[1];
   if ((parse_file(fn)) == -1)
         return(-1);
 
+  cout << "Commands parsed." << endl;
+
   return(0);
 
 }
@@ -46,10 +61,11 @@
 int parse_file(string filename){
 
   int loc, len, i, multi = 0, comment = 0, screen = 0, userinput = 0;
-  string fn, parent, curline, path, buf, multibuf, parsebuf, url;
-  const char *dir, *file;
+  string fn, sn, parent, curline, path, buf, multibuf, parsebuf, url;
+  const char *dir, *file, *scpt;
   char wd[256];
   ifstream fp;
+  ofstream script;
   
   len = filename.length();
 
@@ -65,13 +81,13 @@
   }
  
   /* Open the file for parsing. */ 
-  // cout << "Parsing file: " << fn << endl;
   file = fn.c_str();
   fp.open(file);
   if (!fp) {
     perror(file);
     return(-1);
   }
+  parsecount++;
   /* Set the current working directory, in case we leave it,
      and so we can get the name of the parent directory for
      the file we're parsing */
@@ -80,6 +96,17 @@
   if ((loc = path.find_last_of("/")) != -1)
       parent = string(path, 0, loc);
 
+  /* Set up the output script name */
+  std::ostringstream stream;
+  stream << parsecount;
+  sn.append(rootwd);
+  sn.append("/commands/");
+  if (parsecount < 100)
+     sn.append("0");
+  sn.append(stream.str());
+  sn.append("-" + fn + ".sh");
+  scpt = sn.c_str();
+
   /* Read one line at a time from the opened file
      until the end of the file is reached */
   while (fp.good()) {
@@ -108,7 +135,13 @@
 	  // '<' not found in the string
 	    if ((screen == 1) && (userinput == 1)) {
 		buf = swap_ent(buf);
-	  	cout << buf << endl;
+		if ((parsed_files.find(fn)) != string::npos) {
+	  	  parsed_files.append(fn);	
+	  	  parsed_files.append(":");
+		}
+		script.open(scpt, ios::app);
+		script << buf << endl;
+		script.close();
 	    }
 	    buf.erase();
 	    break;
@@ -134,7 +167,6 @@
 		loc = parsebuf.find_first_not_of("<");
 		len = parsebuf.find_last_not_of(">");
 	        parsebuf = string(parsebuf, loc, (len-loc)+1);
-	        //cout << "Parsed tag is: " << parsebuf << endl;
 	
 		// Do we have an entity?
 		if ((parsebuf.find("!ENTITY")) != string::npos) {
@@ -148,6 +180,7 @@
 			perror(url.c_str());
 			return(-1);
 	              } else {
+			parsed_ent_files.append(":");
 			parsed_ent_files.append(url);
 		      }
 		      if ((chdir(wd)) == -1)
@@ -165,7 +198,6 @@
 		  if ((parsebuf.find("xpointer")) == string::npos) {
 		    url = string(parsebuf, parsebuf.find("href="), parsebuf.length()-parsebuf.find("href=")+1);
 		    url = string(url, url.find_first_of("\"")+1, url.find_last_of("\"")-url.find_first_of("\"")-1);
-		    //cout << "Got an included file! URL is: " << url << endl;
 		    //parse the file
 		    if ((parse_file(url.c_str())) == -1) {
                       perror(url.c_str());
@@ -186,10 +218,9 @@
 		if ((parsebuf.find("userinput")) != string::npos)
 			userinput = 1;
 
-		if ((parsebuf.find("/userinput")) != string::npos) {
+		if ((parsebuf.find("/userinput")) != string::npos)
 			userinput = 0;
-			cout << endl;
-		}
+
 		if ((parsebuf.find("/screen")) != string::npos)
 			screen = 0;
 
@@ -205,20 +236,16 @@
 
 	  default :
 	  // '<' found, but not the first character in the string.
-	  //  if ((string(buf,0,i).find_first_not_of(" ")) != string::npos)
-	  //    cout << (string(buf, 0, i)) << endl;
             if ((screen == 1) && (userinput == 1)) {
                 parsebuf = string(buf, 0, i);
                 parsebuf = swap_ent(parsebuf);
-                cout << parsebuf;
+                script.open(scpt, ios::app);
+                script << parsebuf << endl;
+                script.close();
             }
-	    buf = string(buf, i, buf.length()-i);
-
-	    
+	    buf = string(buf, i, buf.length()-i);   
 	}
-	
     }
-   // cout << "Current line: " << curline << endl;
   }
 
   fp.close();
@@ -256,8 +283,6 @@
       list = list->next;
     list->next = temp;
   }
-  // cout << "Added entity: " << ent_name << "=\"" << ent_value << "\"" << endl;
-
   return(0);
 }
 




More information about the alfs-log mailing list