2 * This file has been modified from the original CUT distribution.
5 * Copyright (c) 2001-2002 Samuel A. Falvo II, William D. Tanksley
6 * See CUT-LICENSE.TXT for details.
9 * Revision 1.4 2003/03/18 05:53:50 sfalvo
10 * ADD: cutgen.c: cut_exit() -- common exit point; returns proper error code
13 * FIX: cutgen.c: Factored all instances of exit() to invoke cut_exit()
14 * instead. This fixes the bug #703793.
16 * Revision 1.3 2003/03/13 04:27:54 sfalvo
17 * ADD: LICENSE.TXT -- zlib license
19 * ADD: README cut.h cutgen.c -- Changelog token for CVS
21 * FIX: test/bringup-failure -- reflects new usage for bringups and
22 * teardowns in CUT 2.2.
33 #define DO_NOT_PROCESS "..."
35 #define SEARCH_TOKEN_TEST "__CUT__"
36 #define SEARCH_TOKEN_BRINGUP "__CUT_BRINGUP__"
37 #define SEARCH_TOKEN_TAKEDOWN "__CUT_TAKEDOWN__"
39 #define MAX_SYMBOL_LENGTH 256 /* arbitrary */
40 #define MAX_LINE_LENGTH 1024 /* arbitrary */
42 #define SEARCH_TOKEN_TEST_LENGTH sizeof( SEARCH_TOKEN_TEST )-1
43 #define SEARCH_TOKEN_BRINGUP_LENGTH sizeof( SEARCH_TOKEN_BRINGUP )-1
45 #define terr(fmt, args...) do { fprintf(stderr, "\n"); twarn(fmt, ##args); exit(1); } while (0)
46 #define terrx(fmt, args...) do { fprintf(stderr, "\n"); twarnx(fmt, ##args); exit(1); } while (0)
48 typedef enum TestType
{
53 typedef struct TestItem
{
54 char name
[MAX_SYMBOL_LENGTH
];
55 struct TestItem
*next
;
58 typedef struct TestGroup
{
59 char name
[MAX_SYMBOL_LENGTH
];
60 struct TestItem
*tests
;
61 struct TestGroup
*next
;
66 TestGroup
*test_groups
= 0;
69 static int g_count
, g_ready
, g_index
; /* Used by filename globbing support for windows */
70 static char **g_wildcards
, g_fileName
[MAX_LINE_LENGTH
];
72 int NameAndTypeInTestList( char *name
, TestType type
)
77 for (group
= test_groups
; group
; group
= group
->next
) {
78 if (!strcmp(group
->name
, name
) && type
== TYPE_BRINGUP
) return 1;
79 for (item
= group
->tests
; item
; item
= item
->next
) {
80 if (!strcmp(item
->name
, name
)) return 1;
87 void AppendToTestList( char *name
, TestType type
)
92 if (type
== TYPE_BRINGUP
) {
93 struct TestGroup
*new;
95 new = malloc(sizeof(struct TestGroup
));
96 if (!new) terr("malloc");
98 new->next
= test_groups
;
99 strcpy(new->name
, name
);
103 if (!cur
) terrx("no current test group");
105 newt
= malloc(sizeof(struct TestItem
));
106 if (!newt
) terr("malloc");
108 newt
->next
= cur
->tests
;
109 strcpy(newt
->name
, name
);
114 void InsertNameAndTypeIntoTestList( char *name
, TestType type
)
116 if ( !NameAndTypeInTestList( name
, type
) )
117 AppendToTestList( name
, type
);
120 int CharacterIsDigit(char ch
)
122 return ( ( ch
>= '0') && ( ch
<= '9' ) );
125 int CharacterIsUppercase(char ch
)
127 return ( ( ch
>= 'A' ) && ( ch
<= 'Z' ) );
130 int CharacterIsLowercase(char ch
)
132 return ( ( ch
>= 'a' ) && ( ch
<= 'z' ) );
135 int CharacterIsAlphabetic(char ch
)
137 return CharacterIsUppercase(ch
) || CharacterIsLowercase(ch
) || ( ch
== '_' );
140 int CharacterIsAlphanumeric( char ch
)
142 return CharacterIsDigit(ch
) || CharacterIsAlphabetic(ch
);
145 void ProcessGenericFunction( char *line
, int position
,
146 TestType type
, int tokenDisplacement
)
148 char name
[MAX_SYMBOL_LENGTH
] = "";
149 int maxLength
= strlen( line
) - 1, offset
=0;
150 position
= position
+ tokenDisplacement
;
152 while ( CharacterIsAlphanumeric(line
[position
])
153 && (position
<maxLength
) && (offset
<MAX_SYMBOL_LENGTH
) )
155 name
[offset
++] = line
[position
++];
159 InsertNameAndTypeIntoTestList( name
, type
);
162 void ProcessBringupFunction( char *line
, int position
)
164 ProcessGenericFunction( line
, position
, TYPE_BRINGUP
, SEARCH_TOKEN_BRINGUP_LENGTH
);
167 void ProcessTestFunction( char *line
, int position
)
169 ProcessGenericFunction( line
, position
, TYPE_TEST
, SEARCH_TOKEN_TEST_LENGTH
);
173 int OffsetOfSubstring( char *line
, char *token
)
175 char *inset
= strstr(line
,token
);
177 if ( !inset
) return -1;
178 else return inset
- line
;
181 void CallIfSubstringFound( char *line
, char *token
, void (*function
)(char*,int) )
183 int index
= OffsetOfSubstring( line
, token
);
185 function( line
, index
);
188 void ProcessSourceFile( char *filename
)
191 char line
[MAX_LINE_LENGTH
];
193 if( strcmp( filename
, DO_NOT_PROCESS
) != 0 )
196 source
= fopen(filename
,"r");
198 while ( fgets(line
,MAX_LINE_LENGTH
,source
) )
200 CallIfSubstringFound( line
, SEARCH_TOKEN_BRINGUP
, ProcessBringupFunction
);
201 CallIfSubstringFound( line
, SEARCH_TOKEN_TEST
, ProcessTestFunction
);
208 void EmitExternDeclarationFor( char *name
, char *prefix
)
210 fprintf( outfile
, "extern void %s%s( void );\n", prefix
, name
);
213 void Emit(char *text
)
215 fprintf(outfile
, "%s\n", text
);
223 void ListExternalFunctions()
228 for (cur
= test_groups
; cur
; cur
= cur
->next
) {
229 EmitExternDeclarationFor(cur
->name
, SEARCH_TOKEN_BRINGUP
);
230 EmitExternDeclarationFor(cur
->name
, SEARCH_TOKEN_TAKEDOWN
);
231 for (item
= cur
->tests
; item
; item
= item
->next
) {
232 EmitExternDeclarationFor(item
->name
, SEARCH_TOKEN_TEST
);
238 void ListHeaderFiles(void)
241 "#include <string.h>\n"
242 "#include <stdlib.h>\n"
243 "#include <stdio.h>\n"
244 "#include <stdarg.h>\n"
245 "#include \"cut.h\"\n"
251 void EmitIndented(int indent
,char *format
, ...)
254 /* Print two spaces per level of indentation. */
255 fprintf( outfile
, "%*s", indent
*2, "" );
258 vfprintf( outfile
, format
, v
);
261 fprintf( outfile
, "\n" );
264 void EmitUnitTesterBody()
269 Emit( "int main( int argc, char *argv[] )\n{" );
270 Emit( " if ( argc == 1 )" );
271 Emit( " cut_init( -1 );" );
272 Emit( " else cut_init( atoi( argv[1] ) );" );
275 for (group
= test_groups
; group
; group
= group
->next
) {
276 for (test
= group
->tests
; test
; test
= test
->next
) {
277 EmitIndented(1, "cut_run(%s, %s);", group
->name
, test
->name
);
282 Emit( " cut_break_formatting();" );
283 Emit( " printf(\"Done.\\n\");" );
284 Emit( " return 0;\n}\n" );
289 Emit( "/* Automatically generated: DO NOT MODIFY. */" );
292 ListExternalFunctions();
294 EmitUnitTesterBody();
297 void FileName( char *base
)
299 strncpy( g_fileName
, base
, MAX_LINE_LENGTH
);
300 g_fileName
[ MAX_LINE_LENGTH
- 1 ] = 0;
303 int LoadArgument( void )
305 if ( g_index
>= g_count
)
308 FileName( g_wildcards
[g_index
] );
309 g_index
++; /* MUST come after FileName() call; bad code smell */
313 void StartArguments( int starting
, int count
, char *array
[] )
319 g_ready
= LoadArgument();
322 int NextArgument( void )
325 g_ready
= LoadArgument();
330 char *GetArgument( void )
338 void EstablishOutputFile( int argc
, char *argv
[] )
345 if( ( argv
[i
+1] != NULL
) && ( strcmp( argv
[i
], "-o" ) == 0 ) )
347 outfile
= fopen( argv
[i
+1], "wb+" );
348 if( outfile
== NULL
)
349 fprintf( stderr
, "ERROR: Can't open %s for writing.\n", argv
[i
+1] );
351 argv
[i
] = argv
[i
+1] = DO_NOT_PROCESS
;
362 int main( int argc
,char *argv
[] )
371 " %s [options] <input file> [<input file> [...]]\n"
374 " -o filename Specifies output file.\n"
377 " If -o is left unspecified, output defaults to stdout.\n",
383 EstablishOutputFile( argc
, argv
);
385 /* Skip the executable's name and the output filename. */
386 StartArguments(0,argc
,argv
);
388 /* Consume the rest of the arguments, one at a time. */
389 while ( NextArgument() )
391 filename
= GetArgument();
393 if( strcmp( filename
, DO_NOT_PROCESS
) )
395 fprintf( stderr
, " - parsing '%s'... ", filename
);
396 ProcessSourceFile( filename
);
397 fprintf( stderr
, "done.\n");
408 * vim: tabstop=3 shiftwidth=3 expandtab