[Initng-svn] r1949 - initng/tools
svn at initng.thinktux.net
svn at initng.thinktux.net
Sun Nov 6 16:16:52 CET 2005
Author: SaTaN0r1
Date: Sun Nov 6 16:16:52 2005
New Revision: 1949
Modified:
initng/tools/install_service.c
Log:
Cleaned install_service a little bit.*) Better matching of the distros, eg #ifd gentoorx will not build on distro gentoo *) whitspaces in front of #ifd, #elsed, #exec, ... are ignored
*) whitspaces after an empty (eg. the last) #elsed are also ignored
*) now it checks how much byte are left in the exec_buffer
Modified: initng/tools/install_service.c
==============================================================================
--- initng/tools/install_service.c (original)
+++ initng/tools/install_service.c Sun Nov 6 16:16:52 2005
@@ -30,7 +30,7 @@
#include <sys/types.h>
#include <sys/wait.h>
-/*#define DEBUG */
+/* #define DEBUG */
#ifdef DEBUG
#define D_(fmt,...) printf(fmt, ## __VA_ARGS__)
@@ -38,6 +38,11 @@
#define D_(fmt,...)
#endif
+/*the string "blah" must be the FIRST parameter, but his improves perf a little bit */
+#define MATCH(PATTERN, LINE) (strncmp(PATTERN, skip_spaces(LINE), sizeof(PATTERN) - 1) == 0)
+#define LEN(STR) (sizeof(STR) - 1)
+
+
/* this program will parse all .i files in installation
setting distribution dependent values on install time */
@@ -138,35 +143,58 @@
return NULL;
}
-#if 0
-static int keyword_in_row(const char *key, const char *row)
-{
- char *tmp;
- char *st = strndup(row, strcspn(row, "\n\0;"));
+static inline const char* skip_spaces(const char* in) {
+ while (*in == ' ' || *in == '\t')
+ in++;
+ return in;
+}
- tmp = strstr(st, key);
- printf("checking for %s in %s\n", key, st);
- free(st);
- if (tmp)
- return (TRUE);
- return (FALSE);
+static inline int empty_elsed(const char* in) {
+ in = skip_spaces(in);
+ in += LEN("#elsed");
+ while (*in == ' ' || *in == '\t')
+ in++;
+ if (*in == '\n')
+ return 1;
+ else
+ return 0;
+}
+
+/* this verifies that the string "distro" is found in the "in" string
+ * and that the character before the string "distro" in "in" is either
+ * ' ' or '\t'
+ * the character after "distro" meight either be ' ', '\t' or '\n'
+ */
+static inline int match_distro(const char* in, const char* distro) {
+ const char* tmp;
+ int len = strlen(distro);
+ tmp = skip_spaces(in);
+ while (1) {
+ tmp = strstr(tmp, distro);
+ if (! tmp)
+ return 0;
+ if ((tmp[-1] != ' ' && tmp[-1] != '\t') || (tmp[len] != ' ' && tmp[len] != '\t' && tmp[len] != '\n'))
+ tmp++; /* this will make strstr not find the same part again */
+ else
+ return 1;
+ }
}
-#endif
int main(int argc, char **argv)
{
int i = 1;
-
- FILE *in;
- FILE *out = NULL;
-
- const char *distro = NULL;
- const char *infile = NULL;
- const char *outfile = NULL;
-
+
+ FILE* in;
+ FILE* out = NULL;
+
+ const char* distro = NULL;
+ const char* infile = NULL;
+ const char* outfile = NULL;
#define LINE_LEN 1000
char line[LINE_LEN];
- char exec_buffer[100000];
+#define EXEC_BUFFER_LEN 100000
+ char exec_buffer[EXEC_BUFFER_LEN];
+ int exec_buffer_free = EXEC_BUFFER_LEN - 1;
#define IF_BLOCK 1
#define EXEC_BLOCK 2
@@ -175,7 +203,7 @@
int if_block_print_out = 0;
int distro_len = -1;
-
+
/* check cmdline for set distribution */
while (argv[i])
@@ -214,17 +242,16 @@
}
distro_len = strlen(distro);
fprintf(stderr, "Distribution set: \"%s\"\n", distro);
-
+
/* open input for writing */
if (!infile)
{
fprintf(stderr, "You have to set input file with -i\n");
exit(1);
}
- if (!(in = fopen(infile, "r")))
- {
- fprintf(stderr, "could not open input file!\n");
- exit(1);
+ if (!(in = fopen(infile, "r"))){
+ fprintf(stderr, "could not open input file!\n");
+ exit(1);
}
/* open output for writing */
@@ -233,153 +260,122 @@
if (strstr(infile, ".ii"))
{
outfile = strndup(infile, strlen(infile) - 1);
- }
- else
- {
+ } else {
fprintf(stderr, "out with -o not set, defaulting to stdout.\n");
out = stdout;
- }
+ }
}
- if (!out && !(out = fopen(outfile, "w")))
- {
- fprintf(stderr, "could not open output file!\n");
- exit(1);
+ if (!out && !(out = fopen(outfile, "w"))){
+ fprintf(stderr, "could not open output file!\n");
+ exit(1);
}
/* main loop */
while (1)
{
fgets(line, LINE_LEN, in);
- if (feof(in))
+ if (feof(in))
break;
- if (!in_block && line[0] != '#')
- {
- fprintf(out, "%s", line);
- continue; /* read next line */
- }
-
- /*the string "balh" must be the FIRST parameter, but his improves perf a little bit */
-#define MATCH(PATTERN, LINE) (strncmp(PATTERN, LINE, sizeof(PATTERN) - 1) == 0)
-
- /* either in_block != 0 or line started with '#' */
- if (!in_block)
- {
- if (MATCH("#exec", line))
- {
- D_("found #exec \n");
- exec_buffer[0] = '\0';
+ if (! in_block && line[0] != '#') {
+ fprintf(out, "%s", line);
+ continue; /* read next line */
+ }
+
+ /* either in_block != 0 or line started with '#' */
+ if (! in_block) {
+ if (MATCH("#exec", line)){
+ D_("found #exec \n");
+ exec_buffer[0] = '\0';
+ exec_buffer_free = EXEC_BUFFER_LEN - 1;
in_block = EXEC_BLOCK;
- continue;
- }
- else if (MATCH("#ifd", line))
- {
+ continue;
+ } else if (MATCH("#ifd", line)) {
D_("found #ifd\n");
- in_block = IF_BLOCK;
- if_block_print_out = 0;
- if_block_processed = 0;
- if (strstr(line, distro))
- {
- /* TODO: better matchin than strstr */
+ in_block = IF_BLOCK;
+ if_block_print_out = 0;
+ if_block_processed = 0;
+ if (match_distro(line, distro)) {
D_("actual distro %s matches line %s", distro, line);
- if_block_print_out = 1;
- if_block_processed = 1;
- }
- continue;
- }
- else
- {
+ if_block_print_out = 1;
+ if_block_processed = 1;
+ }
+ continue;
+ } else {
/* line started with a '#', but we are not in_block, and the line was neither
- * #ifd nor #exec. so it's a comment.
- */
- if (MATCH("#elsed", line) ||
- MATCH("#endd", line) || MATCH("#endexec", line))
- {
- fprintf(stderr,
- "WARNING: found #elsed, #end or #endexec, but not in BLOCK!\n");
- /* exit here ??? */
- }
- /* this is a comment so print it */
- fprintf(out, "%s", line);
- continue; /* read next line */
- }
- }
+ * #ifd nor #exec. so it's a comment.
+ */
+ if (MATCH("#elsed", line) ||
+ MATCH("#endd", line) ||
+ MATCH("#endexec", line)){
+ fprintf(stderr, "WARNING: found #elsed, #end or #endexec, but not in BLOCK!\n");
+ /* exit here ??? */
+ }
+ /* this is a comment so print it */
+ fprintf(out, "%s", line);
+ continue; /* read next line */
+ }
+ }
/* ok, we are in a block */
- if (in_block == IF_BLOCK)
- {
- if (if_block_print_out)
- {
- /*if the block should be printed up to now */
- if (MATCH("#endd", line) || MATCH("#elsed", line))
- {
- /* elsed or endd, but this section should be printed => stop printing */
- if_block_print_out = 0;
- /* endd => in_block = 0; */
- if (MATCH("#endd", line))
+ if (in_block == IF_BLOCK) {
+ if (if_block_print_out){
+ /*if the block should be printed up to now */
+ if (MATCH("#endd", line) || MATCH("#elsed", line)) {
+ /* elsed or endd, but this section should be printed => stop printing */
+ if_block_print_out = 0;
+ /* endd => in_block = 0; */
+ if (MATCH("#endd", line)) {
+ D_("found #endd\n");
in_block = 0;
- continue;
- }
- else
- {
+ } else {
+ D_("found #elsed\n");
+ }
+ continue;
+ } else {
/* in if block that should be printed => print it */
fprintf(out, "%s", line);
- continue;
- }
-
- }
- else
- {
+ continue;
+ }
+
+ } else {
/* if block that should not be printed: check for elsed, endd */
- if (MATCH("#elsed", line))
- {
+ if (MATCH("#elsed", line)){
D_("found #elsed\n");
- /* now we need to check if there is some distro specified */
- if (strlen(line) == sizeof("#elsed\n") - 1)
- {
+ /* now we need to check if there is some distro specified */
+ if (empty_elsed(line)) {
/*no distro specified */
- if (!if_block_processed)
- {
- /* this if block has not yet been processed, so print this one */
- if_block_print_out = 1;
- }
- continue;
- }
- else
- { /* #elsed blahblah */
- if (strstr(line, distro))
- {
- /* TODO: better matching than strstr */
- if_block_print_out = 1;
- if_block_processed = 1;
- continue;
- }
- }
- }
- else if (MATCH("#endd", line))
- {
- in_block = 0;
- continue;
- }
- else
- continue;
- }
- }
- else
- {
+ if (!if_block_processed) {
+ /* this if block has not yet been processed, so print this one */
+ if_block_print_out = 1;
+ }
+ continue;
+ } else { /* #elsed blahblah */
+ if (match_distro(line, distro)) {
+ if_block_print_out = 1;
+ if_block_processed = 1;
+ continue;
+ }
+ }
+ } else if (MATCH("#endd", line)) {
+ D_("found #endd\n");
+ in_block = 0;
+ continue;
+ } else
+ continue;
+ }
+ } else {
/* in_block == EXEC_BLOCK */
- if (MATCH("#endexec", line))
- {
- printf("found #endexec\n");
- /* Do the exec shit */
- fprintf(out, "%s", do_exec(exec_buffer, strlen(exec_buffer)));
- in_block = 0;
- continue;
- }
- else
- {
- strcat(exec_buffer, line);
- continue;
- }
- }
+ if (MATCH("#endexec", line)) {
+ D_("found #endexec\n");
+ /* Do the exec shit*/
+ fprintf(out, "%s", do_exec(exec_buffer, strlen(exec_buffer)));
+ in_block = 0;
+ continue;
+ } else {
+ strncat(exec_buffer, line, exec_buffer_free);
+ exec_buffer_free -= strlen(line);
+ continue;
+ }
+ }
}
fclose(in);
fclose(out);
More information about the Initng-svn
mailing list