[Initng-svn] r1944 - initng/tools
svn at initng.thinktux.net
svn at initng.thinktux.net
Sun Nov 6 14:16:44 CET 2005
Author: jimmy
Date: Sun Nov 6 14:16:43 2005
New Revision: 1944
Modified:
initng/tools/install_service.c
Log:
New install_service.c from SaTaN0rX, cant be worse that the current one, applying.
Modified: initng/tools/install_service.c
==============================================================================
--- initng/tools/install_service.c (original)
+++ initng/tools/install_service.c Sun Nov 6 14:16:43 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__)
@@ -137,7 +137,7 @@
}
return NULL;
}
-
+#if 0
static int keyword_in_row(const char *key, const char *row)
{
char *tmp;
@@ -148,29 +148,30 @@
if(tmp) return(TRUE);
return(FALSE);
}
-
+#endif
int main(int argc, char **argv)
{
int i = 1;
- char *in_buf = NULL;
- char *out_buf = NULL;
- char *pt = NULL;
- off_t fsize = 0;
- int f_in;
- int f_out;
- int g_e = 0;
- int print_row = 1;
- int exec_row = 0;
- char exec_buffer[1024];
- char *data;
- const char *distro = NULL;
- int distro_len;
- const char *in = NULL;
- const char *out = 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 IF_BLOCK 1
+#define EXEC_BLOCK 2
+ int in_block = 0;
+ int if_block_processed = 0;
+ int if_block_print_out = 0;
-
- (void) argc;
+ int distro_len = -1;
+
/* check cmdline for set distribution */
while (argv[i])
@@ -186,13 +187,13 @@
else if (strcmp(argv[i], "--in") == 0
|| strcmp(argv[i], "-i") == 0)
{
- in = argv[i + 1];
+ infile = argv[i + 1];
i++;
}
else if (strcmp(argv[i], "--out") == 0
|| strcmp(argv[i], "-o") == 0)
{
- out = argv[i + 1];
+ outfile = argv[i + 1];
i++;
}
}
@@ -209,213 +210,140 @@
}
distro_len = strlen(distro);
fprintf(stderr, "Distribution set: \"%s\"\n", distro);
-
- if (!in)
+
+ /* open input for writing */
+ if (!infile)
{
- fprintf(stderr, "Have to set input file with -i\n");
+ fprintf(stderr, "You have to set input file with -i\n");
exit(1);
}
-
-
- f_in = open(in, O_RDONLY);
- if (!f_in)
- {
- fprintf(stderr, "Unable to open input file %s\n", in);
- return (1);
+ if (!(in = fopen(infile, "r"))){
+ fprintf(stderr, "could not open input file!\n");
+ exit(1);
}
- /* get in size, so we not how mutch to alloc */
- fsize = lseek(f_in, 0, SEEK_END);
- lseek(f_in, 0, SEEK_SET);
-
- /* allocate in and out buf, we hope twice as much as we are now should be enough. */
- in_buf = calloc((fsize * 2) + 1, sizeof(char));
- out_buf = calloc((fsize * 2) + 11, sizeof(char));
-
- /* zero outbuf */
- out_buf[0] = '\0';
-
- /* read input */
- read(f_in, in_buf, fsize);
-
- /* close input */
- close(f_in);
-
- /*fprintf(stderr,"File (%s): \"%s\"\n\n\n", in, in_buf); */
-
- /* set a temporary pointer */
- pt = in_buf;
-
- /* go tru every row */
- while (pt[0])
+ /* open output for writing */
+ if (!outfile)
{
- int row_len = 0;
-
- /* calculate row lenght */
- while (pt[row_len] != '\n' && pt[row_len])
- row_len++;
- if (pt[row_len] == '\n')
- row_len++;
-
- /* if we are gonna handle this output */
- if (pt[0] == '#')
+ if (strstr(infile, ".ii"))
{
- /* don't lissen on this commands without print_row on */
- if (print_row)
- {
- if (strncmp("#exec", pt, 5) == 0)
- {
- D_(" #exec block begins.\n");
- exec_row = 1;
- exec_buffer[0] = '\0';
- goto next;
- }
- else if (strncmp("#endexec", pt, 8) == 0)
- {
- D_(" #exec block ends.\n");
- exec_row = 2;
- goto next;
- }
- }
-
-
- /* this is a if block, if found distro in row set print_row = 1 */
- if (strncmp("#ifd", pt, 4) == 0)
- {
- D_(" #ifd block : ");
+ outfile = strndup(infile, strlen(infile) - 1);
+ } 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(keyword_in_row(distro, pt + 5))
- print_row = 1;
- else
- print_row = 0;
-
- g_e = 0;
-
-#ifdef DEBUG
- if (print_row == 1)
- printf("Printing..\n");
- else
- printf("Quiet..\n");
-#endif
- goto next;
- }
-
-
- else if (strncmp("#elsed", pt, 6) == 0)
- {
- D_(" #elsed block :");
- if(pt[5]=='\n' || pt[6]=='\n')
- {
- if(g_e==1)
- {
- print_row=0;
- goto next;
- }
+ /* main loop */
+ while (1)
+ {
+ fgets(line, LINE_LEN, 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)
- if (print_row==1)
- print_row=0;
- else if (print_row==0)
- print_row=1;
- goto next;
+ /* either in_block != 0 or line started with '#' */
+ if (! in_block) {
+ if (MATCH("#exec", line)){
+ D_("found #exec \n");
+ exec_buffer[0] = '\0';
+ in_block = EXEC_BLOCK;
+ 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 */
+ D_("actual distro %s matches line %s", distro, line);
+ if_block_print_out = 1;
+ if_block_processed = 1;
}
-
- g_e = 1;
- if(keyword_in_row(distro, pt + 6))
- {
- print_row = 1;
- } else {
- print_row = 0;
+ 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 ??? */
}
-
-#ifdef DEBUG
- if (print_row == 1)
- printf("Printing..\n");
- else
- printf("Quiet..\n");
-#endif
- goto next;
- }
-
- else if (strncmp("#endd", pt, 5) == 0)
- {
- D_("#endd block, all prints.\n");
- /* print as usual */
- print_row = 1;
- goto next;
- }
- }
-
- /* if exec_row is 1, it is feching data */
- if (exec_row == 1)
- {
- strncat(exec_buffer, pt, row_len);
- /*print_row = 0; */
- goto next;
- }
-
- /* if exec_row is 2, it is completed fetching data, and its executed */
- if (exec_row == 2)
- {
- exec_row = 0;
- /*printf("Do_exec\n"); */
- data = do_exec(exec_buffer, strlen(exec_buffer));
- D_("exec data is : \"%s\"\n", data);
- if (data)
- {
- strncat(out_buf, data, strlen(data));
- free(data);
- }
- }
-
- /* else we youst forwards - calculate row lenght */
- if (print_row)
- strncat(out_buf, pt, row_len);
-
- next:
- /* go to end */
- while (pt[0] != '\n' && pt[0])
- pt++;
- /* go to next row */
- if (pt[0] == '\n')
- pt++;
- }
-
- /* free input buffer */
- free(in_buf);
-
- /* open output for writing */
- if (out)
- {
- f_out = open(out, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- /* write output */
- write(f_out, out_buf, strlen(out_buf));
- close(f_out);
- }
- else
- {
- /* if its a .ii file */
- if (strstr(in, ".ii"))
- {
- char *out_2 = NULL;
-
- /* take away the last i */
- out_2 = strndup(in, strlen(in) - 1);
- f_out = open(out_2, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- /* write output */
- write(f_out, out_buf, strlen(out_buf));
- close(f_out);
- }
- else
- {
-
- fprintf(stderr, "out with -o not set, defaulting to stdout.\n");
- printf("%s", out_buf);
-
- }
+ /* 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))
+ in_block = 0;
+ continue;
+ } else {
+ /* in if block that should be printed => print it */
+ fprintf(out, "%s", line);
+ continue;
+ }
+
+ } else {
+ /* if block that should not be printed: check for elsed, endd */
+ 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) {
+ /*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 {
+ /* 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;
+ }
+ }
}
-
- /* free output buffer */
- free(out_buf);
-
+ fclose(in);
+ fclose(out);
exit(0);
}
More information about the Initng-svn
mailing list