2 * Copyright (C) 2003 Timothy Bauscher <timothy@linuxfromscratch.org>
17 struct LFSCMD options
;
19 int lfscmd (int argc
, char **argv
)
31 options
.strip_amp
= 0;
33 //options.xmlfile = NULL;
35 while ((c
= getopt (argc
, argv
, "fxteq:a")) != -1)
51 options
.query
= optarg
;
54 options
.strip_amp
= 1;
61 /* Get xml file path */
62 xmlfile
= argv
[optind
];
64 /* Validate arguments */
66 && NULL
== (xmlfile
=locate_book("LFSCMD_BOOK")))
69 /* Setup the xml parser */
70 xmlSubstituteEntitiesDefault(1);
71 doc
= xmlParseFile(xmlfile
);
72 node
= xmlDocGetRootElement(doc
);
74 /* Check that the document is well-formed */
75 if (NULL
== doc
) error("Document not well-formed");
76 /* Check for an empty root element */
77 if (NULL
== node
) error("Empty root element");
79 return(lfscmd_parsexml("unknown", "unknown", doc
, node
->children
));
82 int lfscmd_parsexml (char* sect
, char* fname
, xmlDocPtr doc
, xmlNodePtr node
)
85 FILE *output
= stdout
;
88 /* Compile regex query */
89 if (NULL
!= options
.query
)
91 if (regcomp(®
, options
.query
, REG_NOSUB
))
92 error("Regex is ill-formed: %s", options
.query
);
97 /* Record page title */
98 if (string_comp("title", node
->name
))
100 sect
= xmlNodeListGetString(doc
, node
->children
, 1);
103 /* Determine filename by section id */
104 else if (string_comp("part", node
->name
)
105 || (string_len(node
->name
) >= 4
106 && string_comp("sect", string_snip(node
->name
, 0, 4))))
108 if (NULL
!= xmlGetProp(node
, "id"))
109 fname
= xmlGetProp(node
, "id");
112 /* Display screen commands */
113 else if (string_comp("screen", node
->name
)
114 && string_comp("userinput", node
->children
->name
))
116 /* Match title or section with query */
117 if (NULL
!= options
.query
)
119 if (!regexec(®
, sect
, 0, NULL
, 0)
120 || !regexec(®
, fname
, 0, NULL
, 0))
122 /* Append output to file */
124 output
=write_file(fname
, "a");
126 /* Output new page title */
127 if (options
.title
&& !title_shown
)
129 fprintf(output
, "\n\n### %s: %s ###\n", fname
, sect
);
132 lfscmd_parse_screen(output
, doc
, node
->children
);
137 /* Append output to file */
139 output
=write_file(fname
, "a");
141 /* Output new page title */
142 if (options
.title
&& !title_shown
)
144 fprintf(output
, "\n\n### %s: %s ###\n", fname
, sect
);
147 lfscmd_parse_screen(output
, doc
, node
->children
);
154 /* Make file executable */
160 /* Recursively traverse the tree */
161 if (NULL
!= node
->children
)
162 lfscmd_parsexml(sect
, fname
, doc
, node
->children
);
169 static void output_cmd(FILE* output
, char* cmd
)
172 system(options
.strip_amp
? string_strip(cmd
, " &&") : cmd
);
174 fprintf(output
, "%s", options
.strip_amp
? string_strip(cmd
, " &&") : cmd
);
177 int lfscmd_parse_command(FILE* output
, xmlDocPtr doc
, xmlNodePtr node
)
183 /* Output content outside of "command" */
184 if (string_comp("text", node
->name
))
186 if (NULL
!= (cmd
= node
->content
))
188 output_cmd(output
, cmd
);
192 if (NULL
!= (cmd
= xmlNodeListGetString(doc
, node
->children
, 1)))
194 output_cmd(output
, cmd
);
202 int lfscmd_parse_userinput(FILE* output
, xmlDocPtr doc
, xmlNodePtr node
)
206 /* Output userinput commands and content */
209 /* Properly deal with BLFS commands of the form <screen><userinput><command> */
210 if (string_comp("command", node
->name
))
212 lfscmd_parse_command(output
, doc
, node
->children
);
214 else if (NULL
!= (cmd
= xmlNodeListGetString(doc
, node
, 1)))
216 output_cmd(output
, cmd
);
225 int lfscmd_parse_screen (FILE* output
, xmlDocPtr doc
, xmlNodePtr node
)
229 /* Output screen commands and content */
232 /* Output content outside of "userinput" */
233 if (string_comp("text", node
->name
))
235 if (NULL
!= (cmd
= node
->content
))
237 output_cmd(output
, cmd
);
241 /* Output commands */
242 else if (string_comp("userinput", node
->name
))
244 lfscmd_parse_userinput(output
, doc
, node
->children
);
250 /* Output trailing space */
251 fprintf(output
, "\n");