cvs commit: LFS/lfscmd/src help.c help.h lfscmd.c lfscmd.h string.c string.h file.c file.h main.c main.h stringfun.c stringfun.h xml.c xml.h

timothy at linuxfromscratch.org timothy at linuxfromscratch.org
Sun Mar 2 12:59:31 PST 2003


timothy     03/03/02 15:59:31

  Modified:    lfscmd   ChangeLog INSTALL README
               lfscmd/doc lfscmd.1
               lfscmd/src file.c file.h main.c
  Added:       lfscmd/src help.c help.h lfscmd.c lfscmd.h string.c string.h
  Removed:     lfscmd   Makefile.am autogen.sh configure.ac
               lfscmd/src main.h stringfun.c stringfun.h xml.c xml.h
  Log:
  Rewrote command-line parser and much
  of the XML code. Added option -q for
  performing queries. Removed automake
  files (they felt dirty).
  
  Revision  Changes    Path
  1.3       +6 -0      LFS/lfscmd/ChangeLog
  
  Index: ChangeLog
  ===================================================================
  RCS file: /home/cvsroot/LFS/lfscmd/ChangeLog,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ChangeLog	10 Feb 2003 01:36:08 -0000	1.2
  +++ ChangeLog	2 Mar 2003 20:59:30 -0000	1.3
  @@ -1,3 +1,9 @@
  +o [timothy] March 2nd
  +
  +  Rewrote command-line parser and much
  +  of the XML code. Added option -q for
  +  performing queries.
  +
   o [timothy] Feburary 19th
   
     Added sysvinit-style prefixes patch.
  
  
  
  1.2       +0 -1      LFS/lfscmd/INSTALL
  
  Index: INSTALL
  ===================================================================
  RCS file: /home/cvsroot/LFS/lfscmd/INSTALL,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- INSTALL	13 Dec 2002 13:35:22 -0000	1.1
  +++ INSTALL	2 Mar 2003 20:59:30 -0000	1.2
  @@ -1,5 +1,4 @@
   INSTALLATION
  -	./configure
   	make
   	make install
   
  
  
  
  1.2       +11 -8     LFS/lfscmd/README
  
  Index: README
  ===================================================================
  RCS file: /home/cvsroot/LFS/lfscmd/README,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- README	13 Dec 2002 13:35:22 -0000	1.1
  +++ README	2 Mar 2003 20:59:30 -0000	1.2
  @@ -1,16 +1,19 @@
   NAME
  -	LFSCMD 0.5 - Extract LFS/BLFS book commands.
  +	LFSCMD - Extract LFS/BLFS book commands.
  +
  +SYNOPSIS
  +	lfscmd [options] INDEX.XML
   
   DESCRIPTION
  -	lfscmd prints the screen commands from the LFS/BLFS book
  +	lfscmd  prints  the screen commands from the LFS/BLFS book
   	to either stdout or files.
   
  -SYNOPSIS
  -	lfscmd [-tfx] INDEX.XML
  -
   OPTIONS
  -	-t	Print titles (as comments).
  +	-f     Write commands to their own file.
  +
  +	-q query
  +              Output pages matching regex query.
   
  -	-f	Write commands to their own file.
  +	-t     Print titles (as comments).
   
  -	-x	Give executable permission to resulting files.
  +	-x     Give executable permission to the resulting  files.
  
  
  
  1.2       +9 -5      LFS/lfscmd/doc/lfscmd.1
  
  Index: lfscmd.1
  ===================================================================
  RCS file: /home/cvsroot/LFS/lfscmd/doc/lfscmd.1,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- lfscmd.1	13 Dec 2002 13:35:22 -0000	1.1
  +++ lfscmd.1	2 Mar 2003 20:59:30 -0000	1.2
  @@ -3,20 +3,24 @@
   .\"
   .TH LFSCMD 1
   .SH NAME
  -LFSCMD 0.5 \- Extract LFS/BLFS book commands.
  +LFSCMD \- Extract LFS/BLFS book commands.
   .SH SYNOPSIS
  -.B lfscmd [-tfx] INDEX.XML
  +.B lfscmd [options] INDEX.XML
   .SH DESCRIPTION
   .B lfscmd
   prints the screen commands from the LFS/BLFS book to either
   stdout or files.
   .SH OPTIONS
   .TP
  -.B -t
  -Print titles (as comments).
  -.TP
   .B -f
   Write commands to their own file.
  +.TP
  +.B
  +-q query
  +Output pages matching regex query.
  +.TP
  +.B -t
  +Print titles (as comments).
   .TP
   .B
   -x
  
  
  
  1.2       +8 -23     LFS/lfscmd/src/file.c
  
  Index: file.c
  ===================================================================
  RCS file: /home/cvsroot/LFS/lfscmd/src/file.c,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- file.c	13 Dec 2002 13:35:22 -0000	1.1
  +++ file.c	2 Mar 2003 20:59:30 -0000	1.2
  @@ -1,5 +1,5 @@
   /*
  - * Copyright (C) 2002 Free Software Foundation, Inc.
  + * Copyright (C) 2003 Timothy Bauscher <timothy at linuxfromscratch.org>
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
  @@ -15,33 +15,18 @@
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    *
  - * Timothy Bauscher <timothy at linuxfromscratch.org>
   */
   
   #include <stdio.h>
  -#include <unistd.h>
  -#include <string.h>
  -#include "file.h"
  -#include "xml.h"
  -#include "stringfun.h"
   
  -int readable (char *filename) {
  -	FILE *test;
  -	if ((test = fopen(filename, "r")) == NULL) {
  -		fprintf(stderr, "Cannot open %s for reading.\n", filename);
  -		return(0);
  -	}
  +#include "string.h"
  +#include "file.h"
   
  -	fclose(test);
  -	return(1);
  -}
  +FILE *write_file (const char *fname, const char *mode) {
  +    FILE *file = NULL;
   
  -FILE *openFile (char *filename) {
  -	FILE *output;
  -	if ((output = fopen(filename, "a")) == NULL) {
  -		fprintf(stderr, "ERROR: Cannot write to file: %s\n", filename);
  -		exit(1);
  -	}
  +    if ((file = fopen(fname, mode)) == NULL)
  +    error("Cannot access file: %s", fname);
   
  -	return(output);
  +    return(file);
   }
  
  
  
  1.2       +1 -2      LFS/lfscmd/src/file.h
  
  Index: file.h
  ===================================================================
  RCS file: /home/cvsroot/LFS/lfscmd/src/file.h,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- file.h	13 Dec 2002 13:35:22 -0000	1.1
  +++ file.h	2 Mar 2003 20:59:30 -0000	1.2
  @@ -1,7 +1,6 @@
   #ifndef FILE_H
   #define FILE_H
   
  -extern int readable (char *filename);
  -extern FILE *openFile (char *filename);
  +extern FILE *write_file (const char *fname, const char *mode);
   
   #endif
  
  
  
  1.2       +4 -51     LFS/lfscmd/src/main.c
  
  Index: main.c
  ===================================================================
  RCS file: /home/cvsroot/LFS/lfscmd/src/main.c,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- main.c	13 Dec 2002 13:35:22 -0000	1.1
  +++ main.c	2 Mar 2003 20:59:30 -0000	1.2
  @@ -1,5 +1,5 @@
   /*
  - * Copyright (C) 2002 Free Software Foundation, Inc.
  + * Copyright (C) 2003 Timothy Bauscher <timothy at linuxfromscratch.org>
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
  @@ -15,57 +15,10 @@
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    *
  - * Timothy Bauscher <timothy at linuxfromscratch.org>
   */
   
  -#include <stdio.h>
  -#include <unistd.h>
  -#include <ctype.h>
  -#include <stdlib.h>
  -#include <string.h>
  -#include "main.h"
  -#include "xml.h"
  -#include "file.h"
  +#include "lfscmd.h"
   
  -int main (int argc, char *argv[]) {
  -	int option=0;
  -	char *xmlfile;
  -
  -	opterr=0;
  -	while ((option=getopt(argc, argv, "xft-")) != -1) {
  -		switch (option) {
  -			case 'x':
  -				lfsopts.executable=1;
  -				break;
  -			case 'f':
  -				lfsopts.files=1;
  -				break;
  -			case 't':
  -				lfsopts.titles=1;
  -				break;
  -			case '-':
  -				usage(argv[0]);
  -				return(-1);
  -				break;
  -			case '?':
  -				usage(argv[0]);
  -				fprintf(stderr, "\nUnknown option: %c\n", optopt);
  -				return(-1);
  -		}
  -	}
  -
  -	if ((argc - optind) < 1) {
  -		usage(argv[0]);
  -		return(-1);
  -	}
  -
  -	xmlfile=argv[optind++];
  -	if (readable(xmlfile))
  -		return( parsedoc(xmlfile) );
  -
  -	return(-1);
  -}
  -
  -void usage (char *progname) {
  -	fprintf(stderr, "Usage: %s [-tfx] INDEX.XML\n\n-t  Print titles (as comments).\n-f  Write commands to their own file.\n-x  Give executable permission to the resulting files.\n", progname);
  +int main (int argc, char **argv) {
  +    return(lfscmd(argc, argv));
   }
  
  
  
  1.1                  LFS/lfscmd/src/help.c
  
  Index: help.c
  ===================================================================
  /*
   * Copyright (C) 2003 Timothy Bauscher <timothy at linuxfromscratch.org>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
  */
  
  #include <stdio.h>
  #include <stdlib.h>
  
  #include "help.h"
  
  void help (const char *prog)
  {
  	fprintf(stderr,
  "Usage: %s [options] index.xml\n\
  Options:\n\
    -f\n\
       Write commands to their own file.\n\
    -q query\n\
       Output pages matching regex query.\n\
    -t\n\
       Print titles (as comments).\n\
    -x\n\
       Make file output executable.\n",
  prog);
  
      exit(1);
  }
  
  
  
  1.1                  LFS/lfscmd/src/help.h
  
  Index: help.h
  ===================================================================
  #ifndef HELP_H
  #define HELP_H
  
  extern void help (const char *prog);
  
  #endif
  
  
  
  1.1                  LFS/lfscmd/src/lfscmd.c
  
  Index: lfscmd.c
  ===================================================================
  /*
   * Copyright (C) 2003 Timothy Bauscher <timothy at linuxfromscratch.org>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
  */
  
  #include <stdio.h>
  #include <regex.h>
  #include <sys/stat.h>
  #include <unistd.h>
  
  #include "help.h"
  #include "string.h"
  #include "file.h"
  #include "lfscmd.h"
  
  int lfscmd (int argc, char **argv) {
      xmlDocPtr doc;
      xmlNodePtr node;
      int c;
  
      /* Set defaults */
      lfs.exe     = 0;
      lfs.file    = 0;
      lfs.title   = 0;
      lfs.status  = 0;
      lfs.query   = NULL;
      lfs.xmlfile = NULL;
  
      while ((c = getopt (argc, argv, "fxtq:")) != -1)
      switch(c)
      {
         case 'f':
            lfs.file = 1;
            break;
         case 'x':
            lfs.exe = 1;
            break;
         case 't':
            lfs.title = 1;
            break;
         case 'q':
            lfs.query = optarg;
            break;
         case '?':
         default:
            help(argv[0]);
      }
  
      /* Get xmlfile */
      for (c=optind; c < argc; c++)
      lfs.xmlfile = argv[c];
  
      /* Validate arguments */
      if (NULL == lfs.xmlfile)
      help(argv[0]);
  
      /* Setup the xml parser */
      xmlSubstituteEntitiesDefault(1);
      doc = xmlParseFile(lfs.xmlfile);
      node = xmlDocGetRootElement(doc);
  
      /* Check that document is well-formed */
      if (NULL == doc)
      error("Document not well-formed");
  
      /* Check for an empty root element */
      if (NULL == node)
      error("Empty root element");
  
      return(lfscmd_parsexml(doc, node->children));
  }
  
  int lfscmd_parsexml (xmlDocPtr doc, xmlNodePtr node) {
      regex_t reg;
  
      /* Compile regex query */
      if (NULL != lfs.query) {
         if (regcomp(&reg, lfs.query, REG_NOSUB))
         error("Regex is ill-formed: %s", lfs.query);
      }
  
      while (NULL != node)
      {
          /* Record page title */
          if (string_comp("title", node->name)) {
             lfs.sect = xmlNodeListGetString(doc, node->children, 1);
             lfs.status = 1;
          }
  
          /* Determine filename by section id */
          else if (string_comp("part", node->name)
               || (string_len(node->name) >= 4
               && string_comp("sect", string_snip(node->name, 0, 4)))) {
  
               if (NULL != xmlGetProp(node, "id"))
               lfs.fname = xmlGetProp(node, "id");
          }
  
          /* Display screen commands */
          else if (string_comp("screen", node->name)
               && string_comp("userinput", node->children->name)) {
  
               /* Match title or section with query */
               if (NULL != lfs.query) {
                  if (! regexec(&reg, lfs.sect, 0, NULL, 0)
                     || ! regexec(&reg, lfs.fname, 0, NULL, 0))
  
                     lfscmd_parse_screen(doc, node->children);
               }
               else
                 lfscmd_parse_screen(doc, node->children);
          }
  
          /* Recursively traverse the tree */
          if (NULL != node->children)
          lfscmd_parsexml(doc, node->children);
  
          node = node->next;
      }
      return(0);
  }
  
  int lfscmd_parse_screen (xmlDocPtr doc, xmlNodePtr node) {
      FILE *output = stdout;
  
      if (NULL == lfs.fname)
         lfs.fname = "unknown";
      if (NULL == lfs.sect)
         lfs.sect = "unknown";
  
      /* Append output to file */
      if (1 == lfs.file)
      output = write_file(lfs.fname, "a");
  
      /* Output new page title */
      if (1 == lfs.status && 1 == lfs.title) {
         fprintf(output, "\n\n### %s: %s ###\n", lfs.fname, lfs.sect);
         lfs.status = 0;
      }
  
      /* Output screen commands and content */
      while (NULL != node)
      {
          /* Output content outside of "userinput" */
          if (string_comp("text", node->name)) {
              if (NULL != (lfs.cmd = node->content))
              fprintf(output, "%s", lfs.cmd);
          }
  
          /* Output commands */
          else if (string_comp("userinput", node->name)) {
  
              /* Strip double ampersands */
              if (NULL != (lfs.cmd = xmlNodeListGetString(doc, node->children, 1)))
              fprintf(output, "%s", string_strip(lfs.cmd, "&&"));
          }
  
          node = node->next;
      }
  
      /* Output trailing space */
      fprintf(output, "\n");
  
      /* Close file */
      if (1 == lfs.file) {
         fclose(output);
  
         /* Make file executable */
         if (1 == lfs.exe)
         chmod(lfs.fname, 00755);
      }
  
      return(1);
  }
  
  
  
  1.1                  LFS/lfscmd/src/lfscmd.h
  
  Index: lfscmd.h
  ===================================================================
  #ifndef LFSCMD_H
  #define LFSCMD_H
  
  #include <libxml/tree.h>
  
  struct LFSCMD {
      int exe, file, title;
      int status;
      char *query, *xmlfile;
      char *cmd, *fname, *sect;
  } lfs;
  
  extern int lfscmd (int argc, char **argv);
  extern int lfscmd_parsexml (xmlDocPtr doc, xmlNodePtr node);
  extern int lfscmd_parse_screen (xmlDocPtr doc, xmlNodePtr node);
  
  #endif
  
  
  
  1.1                  LFS/lfscmd/src/string.c
  
  Index: string.c
  ===================================================================
  /*
   * Copyright (C) 2003 Timothy Bauscher <timothy at linuxfromscratch.org>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
  */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdarg.h>
  #include <regex.h>
  
  #include "string.h"
  
  void error (const char *fmt, ...) {
      va_list args;
      char msg[512];
  
      va_start(args, fmt);
      vsnprintf(msg, sizeof msg, fmt, args);
      va_end(args);
  
      fprintf(stderr, "Error: %s\n", msg);
      free(msg);
  
      exit(1);
  }
  
  int string_cat (char *str1, const char *str2) {
      int c, d;
  
      c = string_len(str1);
      d = 0;
  
      while ('\0' != str2[d])
      str1[c++] = str2[d++];
  
      str1[c] = '\0';
      return(d);
  }
  
  int string_comp (const char *str1, const char *str2) {
      int len;
  
      len = string_len(str1);
      if (len != string_len(str2))
      return(0);
  
      for (; len > 0; len--) {
          if (str1[len] != str2[len])
  	return(0);
      }
  
      return(1);
  }
  
  int string_len (const char *string) {
      int c;
      for (c=0; '\0' != string[c]; c++);
      return(c);
  }
  
  char *string_snip (const char *string, const int start, const int end) {
      int c, d;
      char *str;
  
      str = malloc((end-start+1) * sizeof(char));
      d = 0;
  
      for (c=start; c < end; c++)
      str[d++] = string[c];
  
      str[d] = '\0';
      return(str);
  }
  
  char *string_strip (const char *string, const char *strip) {
      char *str, *tmp;
      int len_stp, len_str;
      int c, s;
  
      len_stp = string_len(strip);
      len_str = string_len(string);
      str = malloc((len_str + 1) * sizeof(char));
      c = 0;
  
      for (s=0; '\0' != string[s]; s++)
      {
         tmp = string_snip(string, s, s + len_stp);
  
         if (string_comp(strip, tmp))
            s += len_stp;
         else
            str[c++] = string[s];
  
         free(tmp);
      }
  
      str[c] = '\0';
      return(str);
  }
  
  
  
  1.1                  LFS/lfscmd/src/string.h
  
  Index: string.h
  ===================================================================
  #ifndef STRING_H
  #define STRING_H
  
  extern void error (const char *fmt, ...);
  extern int string_cat (char *str1, const char *str2);
  extern int string_comp (const char *str1, const char *str2);
  extern int string_len (const char *string);
  extern char *string_snip (const char *string, const int start, const int end);
  extern char *string_strip (const char *string, const char *strip);
  
  #endif
  
  
  
-- 
Unsubscribe: send email to listar at linuxfromscratch.org
and put 'unsubscribe lfs-book' in the subject header of the message



More information about the lfs-book mailing list