re-worked ndis to do device detection from a win2k-style registry enum database,...
[reactos.git] / freeldr / tools / deptool.c
1 //
2 // deptool.c
3 // Copyright (C) 2002 by Brian Palmer <brianp@sginet.com>
4 //
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #define ERROR_SUCCESS 0
11 #define ERROR_NOTENOUGHPARAMS 1
12 #define ERROR_DEPENDFILENOTFOUND 2
13 #define ERROR_OUTOFMEMORY 3
14 #define ERROR_READERROR 4
15 #define ERROR_WRITEERROR 5
16
17 int main(int argc, char *argv[])
18 {
19 FILE* DependFile;
20 int DependFileSize;
21 char* DependFileData;
22 char* NewDependFileData;
23 int CurIdx;
24 int CurIdx2;
25 int RuleDependencySplit = 0;
26
27 // Make sure they passed enough command line parameters
28 if (argc < 2)
29 {
30 printf("Usage: deptool srcfile.d\n");
31 return ERROR_NOTENOUGHPARAMS;
32 }
33
34 // Try to open the dependency file
35 DependFile = fopen(argv[1], "r+t");
36 if (DependFile == NULL)
37 {
38 printf("deptool: No such dependency file: %s\n", argv[1]);
39 return ERROR_DEPENDFILENOTFOUND;
40 }
41
42 // Get the file size
43 fseek(DependFile, 0, SEEK_END);
44 DependFileSize = ftell(DependFile);
45 rewind(DependFile);
46
47 // Allocate memory
48 DependFileData = (char *)malloc(DependFileSize);
49 NewDependFileData = (char *)malloc(DependFileSize * 3);
50 if (!DependFileData || !NewDependFileData)
51 {
52 printf("deptool: Out of memory!\n");
53 fclose(DependFile);
54 return ERROR_OUTOFMEMORY;
55 }
56 memset(DependFileData, 0, DependFileSize);
57 memset(NewDependFileData, 0, DependFileSize * 3);
58
59 // Read in file data
60 fread(DependFileData, 1, DependFileSize, DependFile);
61 if (ferror(DependFile))
62 {
63 printf("deptool: Dependency file read error.\n");
64 fclose(DependFile);
65 return ERROR_READERROR;
66 }
67
68 // Loop through the dependency file data and
69 // insert the rule for the dependency file itself
70 for (CurIdx=0,CurIdx2=0; DependFileData[CurIdx]; CurIdx++,CurIdx2++)
71 {
72 // Find the first colon ':' in the file and insert
73 // the rule right before it
74 if (DependFileData[CurIdx] == ':')
75 {
76 NewDependFileData[CurIdx2] = ' ';
77 CurIdx2++;
78 strcat(&NewDependFileData[CurIdx2], argv[1]);
79 CurIdx2 += strlen(argv[1]);
80 NewDependFileData[CurIdx2] = ' ';
81 CurIdx2++;
82 strcat(NewDependFileData, &DependFileData[CurIdx]);
83 CurIdx2 += 2;
84 RuleDependencySplit = CurIdx + 2;
85 break;
86 }
87 else
88 {
89 NewDependFileData[CurIdx2] = DependFileData[CurIdx];
90 }
91 }
92
93 // Now loop through all the rule dependencies and
94 // turn them into rules themselves
95 strcat(NewDependFileData, "\n\n");
96 CurIdx = RuleDependencySplit;
97 CurIdx2 = strlen(NewDependFileData);
98 for (; DependFileData[CurIdx]; CurIdx++,CurIdx2++)
99 {
100 // If it's a line continuation char '\' then skip over it
101 if (DependFileData[CurIdx] == '\\')
102 {
103 CurIdx2--;
104 continue;
105 }
106
107 // If it's a new line char '\n' then insert a colon ':' to make it a rule
108 if (DependFileData[CurIdx] == '\n')
109 {
110 NewDependFileData[CurIdx2] = ':';
111 CurIdx2++;
112 }
113
114 NewDependFileData[CurIdx2] = DependFileData[CurIdx];
115 }
116
117 // Write out file data
118 rewind(DependFile);
119 fwrite(NewDependFileData, 1, strlen(NewDependFileData), DependFile);
120 if (ferror(DependFile))
121 {
122 printf("deptool: Dependency file write error.\n");
123 fclose(DependFile);
124 return ERROR_WRITEERROR;
125 }
126
127 fclose(DependFile);
128 return ERROR_SUCCESS;
129 }