permit multiline comments and strings in macros
[bpt/coccinelle.git] / tools / distributed / spatch_linux.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #define MAX 9
7
8 #ifndef HOME
9 #define HOME "/home/julia/coccinelle/tools/distributed/"
10 #endif
11
12 void do_child(int id, unsigned int argc, char **argv, int max,
13 char *script) {
14 int i;
15 char **new_args = malloc(sizeof(char*) * (argc + 5));
16 char string1[50],string2[50];
17 for(i=1; i!=argc; i++) {
18 new_args[i+4] = argv[i];
19 }
20 new_args[i+4] = NULL;
21 new_args[0] = "nothing";
22 new_args[1] = new_args[5]; // cocci file must be first
23 new_args[2] = "-index";
24 sprintf(string1, "%d", id);
25 new_args[3] = string1; // processor number must be third
26 new_args[4] = "-max";
27 sprintf(string2, "%d", max);
28 new_args[5] = string2;
29 execvp(script,new_args);
30 printf("tried to execute %s\n",HOME "spatch_linux_script");
31 perror("exec failure");
32 _exit(0);
33 }
34
35 void cleanup(char **argv) {
36 char **new_args = malloc(sizeof(char*) * 3);
37 new_args[0] = "nothing";
38 new_args[1] = argv[1];
39 new_args[2] = NULL;
40 printf ("doing cleanup on %s\n",argv[1]);
41 execvp(HOME "cleanup",new_args);
42 }
43
44 int main(unsigned int argc, char **argv) {
45 int i, start=0, max;
46 char script[150];
47 // interpret the arguments
48 max = MAX;
49 if (!strcmp(argv[1],"-processes")) {max = atoi(argv[2]); start = 2;}
50 if (!strcmp(argv[1],"-script")) {
51 strcpy(script,HOME);
52 strcat(script,argv[2]);
53 start = 2;
54 } else strcpy(script,HOME "spatch_linux_script");
55 if (!strcmp(argv[1],"--help")) {
56 printf("spatch_linux [-processes n] foo.cocci ...\n");
57 exit (0);
58 }
59
60 // run the child processes
61 int pid;
62 for(i=0;i!=max;i++) {
63 if (!(pid=fork())) {
64 // child
65 do_child(i,argc-start,&argv[start],max,script);
66 }
67 else if (pid > 0) {
68 // printf("Child born: %d\n", pid);
69 }
70 else
71 printf("*** forking error ***\n");
72 }
73 int status;
74 for(i=0;i!=max;i++) {
75 pid = wait(&status);
76 // printf("Child dead: %d -- %d\n", pid,status);
77 }
78 cleanup(&argv[start]);
79 }