Updated PCI IDs to latest snapshot.
[tangerine.git] / tools / cpak / cpak.c
blob64322f1856ec0c9cd1273b3269295a0eb18dfa71
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
12 struct inclist {
13 struct inclist *next;
14 char *text;
17 FILE * fd, * fdo;
19 #define _read(stream,buff,count) fread(buff,1,count,stream)
21 int main ( int argc, char **argv )
23 int count;
24 int i, filecount, startarg;
25 char fbuf[50];
27 /* Starting sequence of a C comment: '\slash','\asterisk' */
28 char c_comment[3] = { 0x2f, 0x2a, 0x00 };
29 /* Starting sequence of a C++ comment: '\slash','\slash' */
30 char cpp_comment[3] = { 0x2f, 0x2f, 0x00 };
32 char ft[] = "zyx";
33 struct inclist first = { NULL, ft }, *current, *search;
35 char bracket;
36 char incname[50];
37 char filename[100];
38 char *outputdir;
40 if ( argc < 2 )
42 fprintf( stderr, "No input files given!\n" );
43 exit ( -1 );
46 if ( strcmp(argv[1],"-d") == 0 )
48 outputdir = argv[2];
49 startarg = 3;
51 else
53 outputdir = ".";
54 startarg = 1;
57 sprintf(filename, "%s/functions.c", outputdir);
58 fdo = fopen ( filename, "w" );
59 if ( fdo == NULL )
61 fprintf ( stderr, "Could not open functions.c out-file!\n" );
62 exit ( -1 );
64 fprintf ( fdo, "#include \"functions.h\"\n" );
66 printf ( "Collecting functions..." );
67 for ( filecount = startarg ; filecount < argc ; filecount++ )
69 /* printf ( "Opening %s\n", argv[filecount] ); */
70 strcpy ( filename, argv[filecount] );
71 strcat ( filename, ".c" );
72 fd = fopen ( filename, "rb" );
73 if ( fd == NULL )
75 fprintf ( stderr, "\n%s - No such file !\n", argv[filecount] );
76 exit(-1);
79 fprintf ( fdo, "#line 1 \"%s\"\n", filename );
80 count = _read ( fd, fbuf, 1 );
81 while ( count == 1 )
83 switch ( fbuf[0] )
85 case '/':
86 count = _read( fd, &fbuf[1], 1 );
87 if ( fbuf[count] == '*' )
89 /* This is a C comment, copy everything until
90 the end of the comment */
91 fprintf ( fdo, c_comment );
96 count = _read ( fd, fbuf, 1 );
97 putc ( fbuf[0], fdo );
98 } while ( count == 1 && fbuf[0] != '*' );
99 count = _read( fd, fbuf, 1 );
100 putc ( fbuf[0], fdo );
101 } while ( count == 1 && fbuf[0] != '/' );
103 else if ( fbuf[count] == '/' )
105 /* This is a C++ comment, copy everything until
106 the end of the current line */
107 fprintf ( fdo, cpp_comment );
110 count = _read( fd, fbuf, 1 );
111 putc ( fbuf[0], fdo );
112 } while ( count == 1 && fbuf[0] != '\n' );
114 else
116 if ( fseek ( fd, -count, SEEK_CUR ) != 0 )
118 perror ( "fseek" );
119 exit ( -1 );
121 putc ( fbuf[0], fdo );
123 break;
124 case '#':
125 count = _read ( fd, &fbuf[1], 8 );
126 fbuf[count+1] = 0;
127 if ( strcmp ( fbuf, "#include " ) == 0 )
129 _read ( fd, incname, 1 );
130 if ( incname[0] == '<' || incname[0] == 0x22 )
132 int found;
134 if ( incname[0] == '<' )
136 bracket = '>';
138 else
140 bracket = 0x22;
143 i = 0;
144 do {
145 i++;
146 _read ( fd, &incname[i], 1 );
147 } while ( incname[i] != bracket );
148 incname[i+1] = 0;
152 found = 0, search = &first;
153 search->next != NULL;
154 search = search->next
157 /* don't put <> includes after "" ones */
158 if ( bracket == '>' && search->next->text[0] != '<')
159 break;
161 if ( strcmp ( search->next->text, incname) == 0 )
163 found = 1;
164 break;
167 if ( !found )
169 current = malloc ( sizeof ( struct inclist ) );
170 current->next = search->next;
171 current->text = strdup ( incname );
172 search->next = current;
175 else
177 fprintf ( fdo, "#include %c", incname[0] );
180 else
182 if ( fseek ( fd, -count, SEEK_CUR ) != 0 )
184 perror ( "fseek" );
185 exit ( -1 );
187 putc ( fbuf[0], fdo );
189 if( count > 0 )
190 count = 1;
191 break;
192 default:
193 putc ( fbuf[0], fdo );
194 break;
196 count = _read ( fd, fbuf, 1 );
198 /* If the last character of the file is not a newline '\n'
199 then append a newline character to avoid faulty concatenation
200 of files */
201 if ( fbuf[0] != '\n' )
203 putc ( '\n', fdo );
205 fclose ( fd );
207 fclose ( fdo );
209 sprintf(filename, "%s/functions.h", outputdir);
210 fdo = fopen ( filename, "w" );
211 if ( fdo == NULL )
213 fprintf ( stderr, "Could not open functions.h out-file!\n" );
214 exit ( -1 );
217 search = first.next;
218 while ( search != NULL )
220 current = search;
221 fprintf ( fdo, "#include %s\n", current->text );
222 search = current->next;
223 free ( current->text );
224 free ( current );
226 fclose ( fdo );
228 printf ( "Done!\n" );
230 return ( 0 );