source: trunk/tools/fastdep/fastdep.c@ 3137

Last change on this file since 3137 was 3137, checked in by bird, 25 years ago

Added check for stack overflow in depCheckCyclic.

File size: 77.1 KB
Line 
1/* $Id: fastdep.c,v 1.13 2000-03-17 10:42:22 bird Exp $
2 *
3 * Fast dependents. (Fast = Quick and Dirty!)
4 *
5 * Copyright (c) 1999-2000 knut st. osmundsen
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10
11/*******************************************************************************
12* Defined Constants And Macros *
13*******************************************************************************/
14#define INCL_DOSERRORS
15#define INCL_FILEMGR
16
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#include <os2.h>
22#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
25#include <direct.h>
26
27#include "avl.h"
28
29#ifndef INLINE
30# if defined(__IBMC__)
31# define INLINE _Inline
32# elif defined(__IBMCPP__)
33# define INLINE inline
34# else
35# error "unknown compiler - inline keyword unknown!"
36# endif
37#endif
38
39/*
40 * This following section is used while testing fastdep.
41 * stdio.h should be included; string.h never included.
42 */
43/*
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47*/
48
49#if 1
50#include <stdio.h>
51#else
52#include <string.h>
53#include <string.h>
54#endif
55
56/*
57 */ /* */ /*
58#include <string.h>
59 */
60#if 1
61# if 1
62 #if 0
63# include <string.h>
64 #else
65# if 1
66 #if 1
67 #if 0
68# include <string.h>
69 #else /* */ /*
70*/
71 # include <stdio.h>
72 #endif
73 #endif
74 #endif
75 #endif
76 #endif
77#endif
78
79/*******************************************************************************
80* Structures and Typedefs *
81*******************************************************************************/
82typedef struct _Options
83{
84 const char * pszInclude;
85 const char * pszExclude;
86 BOOL fExcludeAll;
87 const char * pszObjectExt;
88 const char * pszObjectDir;
89 BOOL fObjectDir; /* replace object directory? */
90 const char * pszRsrcExt;
91 BOOL fObjRule;
92 BOOL fNoObjectPath;
93 BOOL fSrcWhenObj;
94 BOOL fAppend; /* append to the output file, not overwrite it. */
95 BOOL fCheckCyclic; /* allways check for cylic dependency before inserting an dependent. */
96 BOOL fCacheSearchDirs; /* cache entire search dirs. */
97} OPTIONS, *POPTIONS;
98
99
100/*
101 * Language specific analysis functions type.
102 */
103typedef int ( _FNLANG) (const char *pszFilename, void *pvFile,
104 BOOL fHeader, POPTIONS pOptions);
105typedef _FNLANG *PFNLANG;
106
107
108/**
109 * This struct holds the static configuration of the util.
110 */
111typedef struct _ConfigEntry
112{
113 const char **papszExts; /* Pointer to an array of pointer to extentions for this handler. */
114 /* If NULL this is the last entry. */
115 int iFirstHdr; /* Index into the papszExts array of the first headerfile/copybook. */
116 /* Set it to the NULL element of the array if no headers for this extention. */
117 /* A non-header file may get a object rule. */
118 PFNLANG pfn; /* Pointer to handler function. */
119} CONFIGENTRY, *PCONFIGENTRY;
120
121
122/**
123 * Dependant Rule
124 */
125typedef struct _DepRule
126{
127 AVLNODECORE avlCore;
128 char * pszRule; /* Pointer to rule name */
129 int cDeps; /* Entries in the dependant array. */
130 char ** papszDep; /* Pointer to an array of pointers to dependants. */
131} DEPRULE, *PDEPRULE;
132
133
134/**
135 * Filename cache entry.
136 */
137#define FCACHEENTRY AVLNODECORE
138#define PFCACHEENTRY PAVLNODECORE
139
140
141/*******************************************************************************
142* Internal Functions *
143*******************************************************************************/
144static void syntax(void);
145static int makeDependent(const char *pszFilename, POPTIONS pOptions);
146
147int langC_CPP(const char *pszFilename, void *pvFile, BOOL fHeader, POPTIONS pOptions);
148int langAsm(const char *pszFilename, void *pvFile, BOOL fHeader, POPTIONS pOptions);
149int langRC(const char *pszFilename, void *pvFile, BOOL fHeader, POPTIONS pOptions);
150int langCOBOL(const char *pszFilename, void *pvFile, BOOL fHeader, POPTIONS pOptions);
151
152
153/* string operations */
154static int strnicmpwords(const char *pszS1, const char *pszS2, int cch);
155
156/* file operations */
157static char *fileNormalize(char *pszFilename);
158static char *fileNormalize2(const char *pszFilename, char *pszBuffer);
159 char *filePath(const char *pszFilename, char *pszBuffer);
160static char *filePathSlash(const char *pszFilename, char *pszBuffer);
161static char *filePathSlash2(const char *pszFilename, char *pszBuffer);
162static char *fileName(const char *pszFilename, char *pszBuffer);
163static char *fileNameNoExt(const char *pszFilename, char *pszBuffer);
164static char *fileExt(const char *pszFilename, char *pszBuffer);
165
166/* filecache operations */
167static BOOL filecacheAddFile(const char *pszFilename);
168static BOOL filecacheAddDir(const char *pszDir);
169INLINE BOOL filecacheFind(const char *pszFilename);
170INLINE BOOL filecacheIsDirCached(const char *pszDir);
171
172/* pathlist operations */
173static char *pathlistFindFile(const char *pszPathList, const char *pszFilename, char *pszBuffer, POPTIONS pOptions);
174
175/* word operations */
176static char *findEndOfWord(char *psz);
177#if 0 /* not used */
178static char *findStartOfWord(char *psz, const char *pszStart);
179#endif
180
181/* file helpers */
182static signed long fsize(FILE *phFile);
183
184/* text helpers */
185INLINE char *trim(char *psz);
186INLINE char *trimR(char *psz);
187
188/* textbuffer */
189static void *textbufferCreate(const char *pszFilename);
190static void textbufferDestroy(void *pvBuffer);
191static char *textbufferNextLine(void *pvBuffer, char *psz);
192static char *textbufferGetNextLine(void *pvBuffer, void **ppv, char *pszLineBuffer, int cchLineBuffer);
193
194/* depend workers */
195static BOOL depReadFile(const char *pszFilename, POPTIONS pOptions);
196static BOOL depWriteFile(const char *pszFilename);
197static void depRemoveAll(void);
198static void *depAddRule(const char *pszRulePath, const char *pszName, const char *pszExt);
199static BOOL depAddDepend(void *pvRule, const char *pszDep, BOOL fCheckCyclic);
200#if 0 /* not used */
201static BOOL depCleanFile(const char *pszFilename);
202#endif
203static BOOL depCheckCyclic(PDEPRULE pdepRule, const char *pszDep);
204
205
206/*******************************************************************************
207* Global Variables *
208*******************************************************************************/
209/*
210 * Pointer to the list of dependencies.
211 */
212static PDEPRULE pdepTree = NULL;
213
214
215/*
216 * Filecache - tree starts here.
217 */
218static PFCACHEENTRY pfcTree = NULL;
219static unsigned cfcNodes = 0;
220static PFCACHEENTRY pfcDirTree = NULL;
221
222
223/*
224 * Current directory stuff
225 */
226static char szCurDir[CCHMAXPATH];
227static int aiSlashes[CCHMAXPATH];
228static int cSlashes;
229
230
231/*
232 * Configuration stuff.
233 */
234static const char pszDefaultDepFile[] = ".depend";
235static const char *apszExtC_CPP[] = {"c", "sqc", "cpp", "h", "hpp", NULL};
236static const char *apszExtAsm[] = {"asm", "inc", NULL};
237static const char *apszExtRC[] = {"rc", "dlg", NULL};
238static const char *apszExtCOBOL[] = {"cbl", "cob", "sqb", NULL};
239static CONFIGENTRY aConfig[] =
240{
241 {
242 apszExtC_CPP,
243 3,
244 langC_CPP,
245 },
246
247 {
248 apszExtAsm,
249 1,
250 langAsm,
251 },
252
253 {
254 apszExtRC,
255 1,
256 langRC,
257 },
258
259 {
260 apszExtCOBOL,
261 3,
262 langCOBOL,
263 },
264
265 /* terminating entry */
266 {
267 NULL,
268 -1,
269 NULL
270 }
271};
272
273
274/**
275 * Main function.
276 * @returns 0 on success.
277 * -n count of failiures.
278 * @param
279 * @param
280 * @equiv
281 * @precond
282 * @methdesc
283 * @result
284 * @time
285 * @sketch
286 * @algo
287 * @remark
288 */
289int main(int argc, char **argv)
290{
291 int rc = 0;
292 int argi = 1;
293 int i;
294 const char *pszDepFile = pszDefaultDepFile;
295
296 static char szObjectDir[CCHMAXPATH];
297 static char szObjectExt[64] = "obj";
298 static char szRsrcExt[64] = "res";
299 static char szInclude[32768] = ";";
300 static char szExclude[32768] = ";";
301
302 OPTIONS options =
303 {
304 szInclude, /* pszInclude */
305 szExclude, /* pszExclude */
306 FALSE, /* fExcludeAll */
307 szObjectExt, /* pszObjectExt */
308 szObjectDir, /* pszObjectDir */
309 FALSE, /* fObjectDir */
310 szRsrcExt, /* pszRsrcExt */
311 TRUE, /* fObjRule */
312 FALSE, /* fNoObjectPath */
313 TRUE, /* fSrcWhenObj */
314 FALSE, /* fAppend */
315 TRUE, /* fCheckCyclic */
316 TRUE /* fCacheSearchDirs */
317 };
318
319 szObjectDir[0] = '\0';
320
321 if (argc == 1)
322 {
323 syntax();
324 return -87;
325 }
326
327 /*
328 * Initiate current directory stuff
329 */
330 if (_getcwd(szCurDir, sizeof(szCurDir)) == NULL)
331 {
332 fprintf(stderr, "fatal error: failed to get current directory\n");
333 return -88;
334 }
335 strlwr(szCurDir);
336 for (i = 0, cSlashes; szCurDir[i] != '\0'; i++)
337 {
338 if (szCurDir[i] == '/')
339 szCurDir[i] = '\\';
340 if (szCurDir[i] == '\\')
341 aiSlashes[cSlashes++] = i;
342 }
343 if (szCurDir[i-1] != '\\')
344 {
345 aiSlashes[cSlashes] = i;
346 szCurDir[i++] = '\\';
347 szCurDir[i] = '\0';
348 }
349
350 /*
351 * parse arguments
352 */
353 while (argi < argc)
354 {
355 if (argv[argi][0] == '-' || argv[argi][0] == '/')
356 {
357 /* parameters */
358 switch (argv[argi][1])
359 {
360 case 'A':
361 case 'a': /* Append to the output file */
362 options.fAppend = argv[argi][2] != '-';
363 break;
364
365 case 'D':
366 case 'd': /* "-d <filename>" */
367 {
368 const char *pszOld = pszDepFile;
369 if (argv[argi][2] != '\0')
370 pszDepFile = &argv[argi][2];
371 else
372 {
373 if (argi + 1 < argc)
374 pszDepFile = argv[++argi];
375 else
376 {
377 fprintf(stderr, "invalid parameter -d, filename missing!\n");
378 return -1;
379 }
380 }
381
382 /* if dependencies are generated we'll flush them to the old filename */
383 if (pdepTree != NULL && pszOld != pszDepFile)
384 {
385 if (!depWriteFile(pszOld))
386 fprintf(stderr, "error: failed to write (flush) dependencies.\n");
387 depRemoveAll();
388 }
389 break;
390 }
391
392 case 'C': /* forced directory cache 'ca' or cylic check 'cy'*/
393 case 'c':
394 if (argv[argi][2] == 'a' || argv[argi][2] == 'A')
395 options.fCacheSearchDirs = TRUE;
396 else if ((argv[argi][2] == 'y' || argv[argi][2] == 'Y'))
397 options.fCheckCyclic = argv[argi][3] != '-';
398 break;
399
400 case 'E': /* list of paths. If a file is found in one of these directories the */
401 case 'e': /* filename will be used without the directory path. */
402 /* Eall<[+]|-> ? */
403 if (strlen(&argv[argi][1]) <= 5 && strnicmp(&argv[argi][1], "Eall", 4) == 0)
404 {
405 options.fExcludeAll = argv[argi][5] != '-';
406 break;
407 }
408 /* path or path list */
409 if (strlen(argv[argi]) > 2)
410 strcat(szExclude, &argv[argi][2]);
411 else
412 {
413 strcat(szExclude, argv[argi+1]);
414 argi++;
415 }
416 if (szExclude[strlen(szExclude)-1] != ';')
417 strcat(szExclude, ";");
418 break;
419
420 case 'I': /* optional include path. This has precedence over the INCLUDE environment variable. */
421 case 'i':
422 if (strlen(argv[argi]) > 2)
423 strcat(szInclude, &argv[argi][2]);
424 else
425 {
426 strcat(szInclude, argv[argi+1]);
427 argi++;
428 }
429 if (szInclude[strlen(szInclude)-1] != ';')
430 strcat(szInclude, ";");
431 break;
432
433 case 'n': /* no object path , -N<[+]|-> */
434 case 'N':
435 if (strlen(argv[argi]) <= 1+1+1)
436 options.fNoObjectPath = argv[argi][2] != '-';
437 else
438 {
439 fprintf(stderr, "error: invalid parameter!, '%s'\n", argv[argi]);
440 return -1;
441 }
442 break;
443
444 case 'o': /* object base directory, Obj or Obr<[+]|-> */
445 case 'O':
446 if (strlen(&argv[argi][1]) <= 4 && strnicmp(&argv[argi][1], "Obr", 3) == 0)
447 {
448 options.fObjRule = argv[argi][4] != '-';
449 break;
450 }
451
452 if (strlen(&argv[argi][1]) >= 4 && strnicmp(&argv[argi][1], "Obj", 3) == 0)
453 {
454 if (strlen(argv[argi]) > 4)
455 strcpy(szObjectExt, argv[argi]+4);
456 else
457 {
458 strcpy(szObjectExt, argv[argi+1]);
459 argi++;
460 }
461 break;
462 }
463
464 /* path: -o or -o- */
465 options.fObjectDir = TRUE;
466 if (strlen(argv[argi]) > 2)
467 {
468 if (argv[argi][2] == '-') /* no object path */
469 szObjectDir[0] = '\0';
470 else
471 strcpy(szObjectDir, argv[argi]+2);
472 }
473 else
474 {
475 strcpy(szObjectDir, argv[argi+1]);
476 argi++;
477 }
478 if (szObjectDir[0] != '\0'
479 && szObjectDir[strlen(szObjectDir)-1] != '\\'
480 && szObjectDir[strlen(szObjectDir)-1] != '/'
481 )
482 strcat(szObjectDir, "\\");
483 break;
484
485 case 'r':
486 case 'R':
487 if (strlen(argv[argi]) > 2)
488 strcpy(szObjectExt, argv[argi]+2);
489 else
490 {
491 strcpy(szObjectExt, argv[argi+1]);
492 argi++;
493 }
494 break;
495
496 case 'h':
497 case 'H':
498 case '?':
499 syntax();
500 return 1;
501
502 default:
503 fprintf(stderr, "error: invalid parameter! '%s'\n", argv[argi]);
504 return -1;
505 }
506
507 }
508 else
509 { /* not a parameter! */
510 ULONG ulRc;
511 char achBuffer[4096];
512 PFILEFINDBUF3 pfindbuf3 = (PFILEFINDBUF3)(void*)&achBuffer[0];
513 HDIR hDir = HDIR_CREATE;
514 ULONG cFiles = ~0UL;
515 int i;
516
517 /*
518 * If append option is specified we'll have to read the existing dep file
519 * before starting adding new dependencies.
520 */
521 if (pdepTree == NULL && options.fAppend)
522 depReadFile(pszDepFile, &options);
523
524 /*
525 * Search for the files specified.
526 */
527 ulRc = DosFindFirst(argv[argi], &hDir,
528 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_ARCHIVED,
529 pfindbuf3, sizeof(achBuffer), &cFiles, FIL_STANDARD);
530 if (!options.fCacheSearchDirs)
531 options.fCacheSearchDirs = cFiles > 25;
532 while (ulRc == NO_ERROR)
533 {
534 for (i = 0;
535 i < cFiles;
536 i++, pfindbuf3 = (PFILEFINDBUF3)((int)pfindbuf3 + pfindbuf3->oNextEntryOffset)
537 )
538 {
539 char *psz;
540 char szSource[CCHMAXPATH];
541
542 /*
543 * Make full path.
544 */
545 if ((psz = strrchr(argv[argi], '\\')) || (psz = strrchr(argv[argi], '/')))
546 {
547 strncpy(szSource, argv[argi], psz - argv[argi] + 1);
548 szSource[psz - argv[argi] + 1] = '\0';
549 }
550 else
551 szSource[0] = '\0';
552 strcat(szSource, pfindbuf3->achName);
553
554 /*
555 * Analyse the file.
556 */
557 rc -= makeDependent(&szSource[0], &options);
558 }
559
560 /* next file */
561 cFiles = ~0UL;
562 pfindbuf3 = (PFILEFINDBUF3)(void*)&achBuffer[0];
563 ulRc = DosFindNext(hDir, pfindbuf3, sizeof(achBuffer), &cFiles);
564 }
565 DosFindClose(hDir);
566 }
567 /* next */
568 argi++;
569 }
570
571 /* Write the depend file! */
572 if (!depWriteFile(pszDepFile))
573 fprintf(stderr, "error: failed to write dependencies file!\n");
574 #if 0
575 printf("cfcNodes=%d\n", cfcNodes);
576 #endif
577
578 return rc;
579}
580
581
582/**
583 * Displays the syntax description for this util.
584 * @status completely implemented.
585 * @author knut st. osmundsen
586 */
587static void syntax(void)
588{
589 printf(
590 "FastDep v0.2\n"
591 "Quick and dirty dependency scanner. Creates a makefile readable depend file.\n"
592 "\n"
593 "Syntax: FastDep [-a<[+]|->] [-ca] [-cy<[+]|->] [-d <outputfn>]\n"
594 " [-e <excludepath>] [-eall<[+]|->] [-i <include>] [-n<[+]|->]\n"
595 " [-o <objdir>] [-obr<[+]|->] <files>\n"
596 "\n"
597 " -a<[+]|-> Append to the output file. Default: Overwrite.\n"
598 " -ca Force search directory caching.\n"
599 " Default: cache if more that 25 files are to be searched.\n"
600 " (more than 25 in the first file expression.)\n"
601 " -cy<[+]|-> Check for cylic dependencies. Default: -cy-\n"
602 " -d <outputfn> Output filename. Default: %s\n"
603 " -e excludepath Exclude paths. If a filename is found in any\n"
604 " of these paths only the filename is used, not\n"
605 " the path+filename (which is default) (don't work?).\n"
606 " -eall<[+]|-> Include and source filenames, paths or no paths.\n"
607 " -eall+: No path are added to the filename.\n"
608 " -eall-: The filename is appended the include path\n"
609 " was found in.\n"
610 " Default: eall-\n"
611 " -i <include> Additional include paths. INCLUDE is searched after this.\n"
612 " -n<[+]|-> No path for object files in the rules.\n"
613 " -o <objdir> Path were object files are placed. This path replaces the\n"
614 " entire filename path\n"
615 " -o- No object path\n"
616 " -obr<[+]|-> -obr+: Object rule.\n"
617 " -obr-: No object rule, rule for source filename is generated.\n"
618 " -obj[ ]<objext> Object extention. Default: obj\n"
619 " -r[ ]<rsrcext> Resource binary extention. Default: res\n"
620 " <files> Files to scan. Wildchars are allowed.\n"
621 "\n"
622 " copyright (c) 1999-2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)\n",
623 pszDefaultDepFile
624 );
625}
626
627
628/**
629 * Generates depend info on this file, these are stored internally
630 * and written to file later.
631 * @returns
632 * @param pszFilename Pointer to source filename.
633 * @param pOptions Pointer to options struct.
634 * @status completely implemented.
635 * @author knut st. osmundsen
636 */
637static int makeDependent(const char *pszFilename, POPTIONS pOptions)
638{
639 int rc = -1;
640 void * pvFile;
641
642 pvFile = textbufferCreate(pszFilename);
643 if (pvFile != NULL)
644 {
645 char szExt[CCHMAXPATH];
646 PCONFIGENTRY pCfg = &aConfig[0];
647 BOOL fHeader;
648
649 /*
650 * Find which filetype this is...
651 */
652 fileExt(pszFilename, szExt);
653 while (pCfg->papszExts != NULL)
654 {
655 const char **ppsz = pCfg->papszExts;
656 while (*ppsz != NULL && stricmp(*ppsz, szExt) != 0)
657 ppsz++;
658 if (*ppsz != NULL)
659 {
660 fHeader = &pCfg->papszExts[pCfg->iFirstHdr] <= ppsz;
661 break;
662 }
663 pCfg++;
664 }
665
666 /* Found? */
667 if (pCfg->papszExts != NULL)
668 rc = (*pCfg->pfn)(pszFilename, pvFile, fHeader, pOptions);
669 else
670 {
671 if (*fileName(pszFilename, szExt) != '.') /* these are 'hidden' files, like .cvsignore, let's ignore them. */
672 fprintf(stderr, "warning: '%s' has an unknown file type.\n", pszFilename);
673 rc = 0;
674 }
675
676 textbufferDestroy(pvFile);
677 }
678 else
679 fprintf(stderr, "failed to open '%s'\n", pszFilename);
680
681 return rc;
682}
683
684
685/**
686 * Generates depend info on this C or C++ file, these are stored internally
687 * and written to file later.
688 * @returns 0 on success.
689 * !0 on error.
690 * @param pszFilename Pointer to source filename.
691 * @param pvFile Pointer to file textbuffer.
692 * @param pOptions Pointer to options struct.
693 * @status completely implemented.
694 * @author knut st. osmundsen
695 */
696int langC_CPP(const char *pszFilename, void *pvFile, BOOL fHeader, POPTIONS pOptions)
697{
698 void * pvRule; /* Handle to the current rule. */
699 char szBuffer[4096]; /* Max line length is 4096... should not be a problem. */
700 int iLine; /* Linenumber. */
701 void * pv = NULL; /* An index used by textbufferGetNextLine. */
702 BOOL fComment; /* TRUE when within a multiline comment. */
703 /* FALSE when not within a multiline comment. */
704 int iIfStack; /* StackPointer. */
705 struct IfStackEntry
706 {
707 int fIncluded : 1; /* TRUE: include this code;
708 * FALSE: excluded */
709 int fIf : 1; /* TRUE: #if part of the expression.
710 * FALSE: #else part of the expression. */
711 int fSupported : 1; /* TRUE: supported if/else statement
712 * FALSE: unsupported all else[<something>] are ignored
713 * All code is included.
714 */
715 } achIfStack[256];
716
717
718 /**********************************/
719 /* Add the depend rule */
720 /**********************************/
721 if (pOptions->fObjRule && !fHeader)
722 {
723 if (pOptions->fNoObjectPath)
724 pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, pOptions->pszObjectExt);
725 else
726 pvRule = depAddRule(pOptions->fObjectDir ?
727 pOptions->pszObjectDir :
728 filePathSlash(pszFilename, szBuffer),
729 fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
730 pOptions->pszObjectExt);
731
732 if (pOptions->fSrcWhenObj && pvRule)
733 depAddDepend(pvRule,
734 pOptions->fExcludeAll ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer),
735 pOptions->fCheckCyclic);
736 }
737 else
738 pvRule = depAddRule(pOptions->fExcludeAll ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer), NULL, NULL);
739
740 /* duplicate rule? */
741 if (pvRule == NULL)
742 return 0;
743
744
745 /*******************/
746 /* find dependants */
747 /*******************/
748 /* Initiate the IF-stack, comment state and line number. */
749 iIfStack = 0;
750 achIfStack[iIfStack].fIf = TRUE;
751 achIfStack[iIfStack].fIncluded = TRUE;
752 achIfStack[iIfStack].fSupported = TRUE;
753 fComment = FALSE;
754 iLine = 0;
755 while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
756 {
757 /* search for #include */
758 register char *pszC;
759 int cbLen;
760 int i = 0;
761 iLine++;
762
763 /* skip blank chars */
764 cbLen = strlen(szBuffer);
765 while (i + 2 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
766 i++;
767
768 /* preprocessor statement? */
769 if (!fComment && szBuffer[i] == '#')
770 {
771 /*
772 * Preprocessor checks
773 * We known that we have a preprocessor statment (starting with an '#' * at szBuffer[i]).
774 * Depending on the word afterwards we'll take some different actions.
775 * So we'll start of by extracting that word and make a string swich on it.
776 * Note that there might be some blanks between the hash and the word.
777 */
778 int cchWord;
779 char * pszEndWord;
780 char * pszArgument;
781 i++; /* skip hash ('#') */
782 while (szBuffer[i] == '\t' || szBuffer[i] == ' ') /* skip blanks */
783 i++;
784 pszArgument = pszEndWord = findEndOfWord(&szBuffer[i]);
785 cchWord = pszEndWord - &szBuffer[i];
786
787 /*
788 * Find the argument by skipping the blanks.
789 */
790 while (*pszArgument == '\t' || *pszArgument == ' ') /* skip blanks */
791 pszArgument++;
792
793 /*
794 * string switch.
795 */
796 if (strncmp(&szBuffer[i], "include", cchWord) == 0)
797 {
798 /*
799 * #include
800 *
801 * Are we in a state where this file is to be included?
802 */
803 if (achIfStack[iIfStack].fIncluded)
804 {
805 char szFullname[CCHMAXPATH];
806 char *psz;
807 BOOL f = FALSE;
808 int j;
809
810 /* extract info between "" or <> */
811 while (i < cbLen && !(f = (szBuffer[i] == '"' || szBuffer[i] == '<')))
812 i++;
813 i++; /* skip '"' or '<' */
814
815 /* if invalid statement then continue with the next line! */
816 if (!f) continue;
817
818 /* find end */
819 j = f = 0;
820 while (i + j < cbLen && j < CCHMAXPATH &&
821 !(f = (szBuffer[i+j] == '"' || szBuffer[i+j] == '>')))
822 j++;
823
824 /* if invalid statement then continue with the next line! */
825 if (!f) continue;
826
827 /* copy filename */
828 strncpy(szFullname, &szBuffer[i], j);
829 szFullname[j] = '\0'; /* ensure terminatition. */
830
831 /* find include file! */
832 psz = pathlistFindFile(pOptions->pszInclude, szFullname, szBuffer, pOptions);
833 if (psz == NULL)
834 psz = pathlistFindFile(getenv("INCLUDE"), szFullname, szBuffer, pOptions);
835
836 /* did we find the include? */
837 if (psz != NULL)
838 {
839 char szBuffer2[CCHMAXPATH];
840 if (pOptions->fExcludeAll ||
841 pathlistFindFile(pOptions->pszExclude, szFullname, szBuffer2, pOptions) != NULL
842 )
843 depAddDepend(pvRule, szFullname, pOptions->fCheckCyclic);
844 else
845 depAddDepend(pvRule, szBuffer, pOptions->fCheckCyclic);
846 }
847 else
848 fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
849 pszFilename, iLine, szFullname);
850 }
851 }
852 else
853 /*
854 * #if
855 */
856 if (strncmp(&szBuffer[i], "if", cchWord) == 0)
857 { /* #if 0 and #if <1-9> are supported */
858 pszEndWord = findEndOfWord(pszArgument);
859 iIfStack++;
860 if ((pszEndWord - pszArgument) == 1
861 && *pszArgument >= '0' && *pszArgument <= '9')
862 {
863 if (*pszArgument != '0')
864 achIfStack[iIfStack].fIncluded = TRUE;
865 else
866 achIfStack[iIfStack].fIncluded = FALSE;
867 }
868 else
869 achIfStack[iIfStack].fSupported = FALSE;
870 achIfStack[iIfStack].fIncluded = TRUE;
871 achIfStack[iIfStack].fIf = TRUE;
872 }
873 else
874 /*
875 * #else
876 */
877 if (strncmp(&szBuffer[i], "else", cchWord) == 0)
878 {
879 if (achIfStack[iIfStack].fSupported)
880 {
881 if (achIfStack[iIfStack].fIncluded) /* ARG!! this'll prevent warning */
882 achIfStack[iIfStack].fIncluded = FALSE;
883 else
884 achIfStack[iIfStack].fIncluded = TRUE;
885 }
886 achIfStack[iIfStack].fIf = FALSE;
887 }
888 else
889 /*
890 * #endif
891 */
892 if (strncmp(&szBuffer[i], "endif", cchWord) == 0)
893 { /* Pop the if-stack. */
894 if (iIfStack > 0)
895 iIfStack--;
896 else
897 fprintf(stderr, "%s(%d): If-Stack underflow!\n", pszFilename, iLine);
898 }
899 /*
900 * general if<something> and elseif<something> implementations
901 */
902 else
903 if (strncmp(&szBuffer[i], "elseif", 6) == 0)
904 {
905 achIfStack[iIfStack].fSupported = FALSE;
906 achIfStack[iIfStack].fIncluded = TRUE;
907 }
908 else
909 if (strncmp(&szBuffer[i], "if", 2) == 0)
910 {
911 iIfStack++;
912 achIfStack[iIfStack].fIf = TRUE;
913 achIfStack[iIfStack].fSupported = FALSE;
914 achIfStack[iIfStack].fIncluded = TRUE;
915 }
916 /* The rest of them aren't implemented yet.
917 else if (strncmp(&szBuffer[i], "if") == 0)
918 {
919 }
920 */
921 }
922
923 /*
924 * Comment checks.
925 * -Start at first non-blank.
926 * -Loop thru the line since we might have more than one
927 * comment statement on a single line.
928 */
929 pszC = &szBuffer[i];
930 while (pszC != NULL && *pszC != '\0')
931 {
932 if (fComment)
933 pszC = strstr(pszC, "*/"); /* look for end comment mark. */
934 else
935 {
936 char *pszLC;
937 pszLC= strstr(pszC, "//"); /* look for single line comment mark. */
938 pszC = strstr(pszC, "/*"); /* look for start comment mark */
939 if (pszLC && pszLC < pszC) /* if there is an single line comment mark before the */
940 break; /* muliline comment mark we'll ignore the multiline mark. */
941 }
942
943 /* Comment mark found? */
944 if (pszC != NULL)
945 {
946 fComment = !fComment;
947 pszC += 2; /* skip comment mark */
948
949 /* debug */
950 /*
951 if (fComment)
952 fprintf(stderr, "starts at line %d\n", iLine);
953 else
954 fprintf(stderr, "ends at line %d\n", iLine);
955 */
956 }
957 }
958 } /*while*/
959
960 return 0;
961}
962
963
964/**
965 * Generates depend info on this file, these are stored internally
966 * and written to file later.
967 * @returns 0 on success.
968 * !0 on error.
969 * @param pszFilename Pointer to source filename.
970 * @param pvFile Pointer to file textbuffer.
971 * @param pOptions Pointer to options struct.
972 * @status completely implemented.
973 * @author knut st. osmundsen
974 */
975int langAsm(const char *pszFilename, void *pvFile, BOOL fHeader, POPTIONS pOptions)
976{
977 void * pvRule; /* Handle to the current rule. */
978 char szBuffer[4096]; /* Temporary buffer (max line lenght size...) */
979 int iLine; /* current line number */
980 void * pv = NULL; /* An index used by textbufferGetNextLine. */
981
982
983 /**********************************/
984 /* Add the depend rule */
985 /**********************************/
986 if (pOptions->fObjRule && !fHeader)
987 {
988 if (pOptions->fNoObjectPath)
989 pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, pOptions->pszObjectExt);
990 else
991 pvRule = depAddRule(pOptions->fObjectDir ?
992 pOptions->pszObjectDir :
993 filePathSlash(pszFilename, szBuffer),
994 fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
995 pOptions->pszObjectExt);
996
997 if (pOptions->fSrcWhenObj && pvRule)
998 depAddDepend(pvRule,
999 pOptions->fExcludeAll ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer),
1000 pOptions->fCheckCyclic);
1001 }
1002 else
1003 pvRule = depAddRule(pOptions->fExcludeAll ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer), NULL, NULL);
1004
1005 /* duplicate rule? */
1006 if (pvRule == NULL)
1007 return 0;
1008
1009
1010 /*******************/
1011 /* find dependants */
1012 /*******************/
1013 iLine = 0;
1014 while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
1015 {
1016 /* search for include */
1017 int cbLen;
1018 int i = 0;
1019 iLine++;
1020
1021 /* skip blank chars */
1022 cbLen = strlen(szBuffer);
1023 while (i + 9 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
1024 i++;
1025
1026 /* is this an include? */
1027 if (strnicmp(&szBuffer[i], "include", 7) == 0
1028 && (szBuffer[i + 7] == '\t' || szBuffer[i + 7] == ' ')
1029 )
1030 {
1031 char szFullname[CCHMAXPATH];
1032 char *psz;
1033 int j;
1034
1035 /* skip to first no blank char */
1036 i += 7;
1037 while (i < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
1038 i++;
1039
1040 /* comment check - if comment found, no filename was given. continue. */
1041 if (szBuffer[i] == ';') continue;
1042
1043 /* find end */
1044 j = 0;
1045 while (i + j < cbLen
1046 && j < CCHMAXPATH
1047 && szBuffer[i+j] != ' ' && szBuffer[i+j] != '\t' && szBuffer[i+j] != '\n'
1048 && szBuffer[i+j] != '\0' && szBuffer[i+j] != ';' && szBuffer[i+j] != '\r'
1049 )
1050 j++;
1051
1052 /* copy filename */
1053 strncpy(szFullname, &szBuffer[i], j);
1054 szFullname[j] = '\0'; /* ensure terminatition. */
1055
1056 /* find include file! */
1057 psz = pathlistFindFile(pOptions->pszInclude, szFullname, szBuffer, pOptions);
1058 if (psz == NULL)
1059 psz = pathlistFindFile(getenv("INCLUDE"), szFullname, szBuffer, pOptions);
1060
1061 /* Did we find the include? */
1062 if (psz != NULL)
1063 {
1064 char szBuffer2[CCHMAXPATH];
1065 if (pOptions->fExcludeAll ||
1066 pathlistFindFile(pOptions->pszExclude, szFullname, szBuffer2, pOptions) != NULL
1067 )
1068 depAddDepend(pvRule, szFullname, pOptions->fCheckCyclic);
1069 else
1070 depAddDepend(pvRule, szBuffer, pOptions->fCheckCyclic);
1071 }
1072 else
1073 fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
1074 pszFilename, iLine, szFullname);
1075 }
1076 } /*while*/
1077
1078 return 0;
1079}
1080
1081
1082/**
1083 * Generates depend info on this Resource file, these are stored internally
1084 * and written to file later.
1085 * @returns 0 on success.
1086 * !0 on error.
1087 * @param pszFilename Pointer to source filename.
1088 * @param pvFile Pointer to file textbuffer.
1089 * @param pOptions Pointer to options struct.
1090 * @status completely implemented.
1091 * @author knut st. osmundsen
1092 */
1093int langRC(const char *pszFilename, void *pvFile, BOOL fHeader, POPTIONS pOptions)
1094{
1095 void * pvRule; /* Handle to the current rule. */
1096 char szBuffer[4096]; /* Temporary buffer (max line lenght size...) */
1097 int iLine; /* current line number */
1098 void * pv = NULL; /* An index used by textbufferGetNextLine. */
1099
1100
1101 /**********************************/
1102 /* Add the depend rule */
1103 /**********************************/
1104 if (pOptions->fObjRule && !fHeader)
1105 {
1106 if (pOptions->fNoObjectPath)
1107 pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, pOptions->pszRsrcExt);
1108 else
1109 pvRule = depAddRule(pOptions->fObjectDir ?
1110 pOptions->pszObjectDir :
1111 filePathSlash(pszFilename, szBuffer),
1112 fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
1113 pOptions->pszRsrcExt);
1114
1115 if (pOptions->fSrcWhenObj && pvRule)
1116 depAddDepend(pvRule,
1117 pOptions->fExcludeAll ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer),
1118 pOptions->fCheckCyclic);
1119 }
1120 else
1121 pvRule = depAddRule(pOptions->fExcludeAll ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer), NULL, NULL);
1122
1123 /* duplicate rule? */
1124 if (pvRule == NULL)
1125 return 0;
1126
1127
1128 /*******************/
1129 /* find dependants */
1130 /*******************/
1131 iLine = 0;
1132 while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
1133 {
1134 /* search for #include */
1135 int cbLen;
1136 int i = 0;
1137 iLine++;
1138
1139 /* skip blank chars */
1140 cbLen = strlen(szBuffer);
1141 while (i + 9 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
1142 i++;
1143
1144 /* is this an include? */
1145 if ( strncmp(&szBuffer[i], "#include", 8) == 0
1146 || strncmp(&szBuffer[i], "RCINCLUDE", 9) == 0
1147 || strncmp(&szBuffer[i], "DLGINCLUDE", 10) == 0
1148 )
1149 {
1150 char szFullname[CCHMAXPATH];
1151 char *psz;
1152 BOOL f = FALSE;
1153 int j;
1154
1155 /* extract info between "" or <> */
1156 while (i < cbLen && !(f = (szBuffer[i] == '"' || szBuffer[i] == '<')))
1157 i++;
1158 i++; /* skip '"' or '<' */
1159
1160 /* if invalid statement then continue with the next line! */
1161 if (!f) continue;
1162
1163 /* find end */
1164 j = f = 0;
1165 while (i + j < cbLen && j < CCHMAXPATH &&
1166 !(f = (szBuffer[i+j] == '"' || szBuffer[i+j] == '>')))
1167 j++;
1168
1169 /* if invalid statement then continue with the next line! */
1170 if (!f) continue;
1171
1172 /* copy filename */
1173 strncpy(szFullname, &szBuffer[i], j);
1174 szFullname[j] = '\0'; /* ensure terminatition. */
1175
1176 /* find include file! */
1177 psz = pathlistFindFile(pOptions->pszInclude, szFullname, szBuffer, pOptions);
1178 if (psz == NULL)
1179 psz = pathlistFindFile(getenv("INCLUDE"), szFullname, szBuffer, pOptions);
1180
1181 /* did we find the include? */
1182 if (psz != NULL)
1183 {
1184 char szBuffer2[CCHMAXPATH];
1185 if (pOptions->fExcludeAll ||
1186 pathlistFindFile(pOptions->pszExclude, szFullname, szBuffer2, pOptions) != NULL
1187 )
1188 depAddDepend(pvRule, szFullname, pOptions->fCheckCyclic);
1189 else
1190 depAddDepend(pvRule, szBuffer, pOptions->fCheckCyclic);
1191 }
1192 else
1193 fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
1194 pszFilename, iLine, szFullname);
1195 }
1196 } /*while*/
1197
1198 return 0;
1199}
1200
1201
1202/**
1203 * Generates depend info on this COBOL file, these are stored internally
1204 * and written to file later.
1205 * @returns 0 on success.
1206 * !0 on error.
1207 * @param pszFilename Pointer to source filename.
1208 * @param pvFile Pointer to file textbuffer.
1209 * @param pOptions Pointer to options struct.
1210 * @status completely implemented.
1211 * @author knut st. osmundsen
1212 */
1213int langCOBOL(const char *pszFilename, void *pvFile, BOOL fHeader, POPTIONS pOptions)
1214{
1215 void * pvRule; /* Handle to the current rule. */
1216 char szBuffer[4096]; /* Temporary buffer (max line lenght size...) */
1217 int iLine; /* current line number */
1218 void * pv = NULL; /* An index used by textbufferGetNextLine. */
1219
1220
1221 /**********************************/
1222 /* Add the depend rule */
1223 /**********************************/
1224 if (pOptions->fObjRule && !fHeader)
1225 {
1226 if (pOptions->fNoObjectPath)
1227 pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, pOptions->pszObjectExt);
1228 else
1229 pvRule = depAddRule(pOptions->fObjectDir ?
1230 pOptions->pszObjectDir :
1231 filePathSlash(pszFilename, szBuffer),
1232 fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
1233 pOptions->pszObjectExt);
1234
1235 if (pOptions->fSrcWhenObj && pvRule)
1236 depAddDepend(pvRule,
1237 pOptions->fExcludeAll ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer),
1238 pOptions->fCheckCyclic);
1239 }
1240 else
1241 pvRule = depAddRule(pOptions->fExcludeAll ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer), NULL, NULL);
1242
1243 /* duplicate rule? */
1244 if (pvRule == NULL)
1245 return 0;
1246
1247
1248 /*******************/
1249 /* find dependants */
1250 /*******************/
1251 iLine = 0;
1252 while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
1253 {
1254 /* search for #include */
1255 int cbLen;
1256 int i = 0;
1257 int i1, i2;
1258 iLine++;
1259
1260 /* check for comment mark (column 7) */
1261 if (szBuffer[6] == '*')
1262 continue;
1263
1264 /* skip blank chars */
1265 cbLen = strlen(szBuffer);
1266 while (i + 9 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
1267 i++;
1268
1269 /* is this an include? */
1270 if ( (i1 = strnicmp(&szBuffer[i], "COPY", 4)) == 0
1271 || (i2 = strnicmpwords(&szBuffer[i], "EXEC SQL INCLUDE", 16)) == 0
1272 )
1273 {
1274 char szFullname[CCHMAXPATH];
1275 char *psz;
1276 int j;
1277
1278 /* skip statement */
1279 i += 4;
1280 if (i1 != 0)
1281 {
1282 int y = 2; /* skip two words */
1283 do
1284 {
1285 /* skip blanks */
1286 while (szBuffer[i] == ' ' || szBuffer[i] == '\t')
1287 i++;
1288 /* skip word */
1289 while (szBuffer[i] != ' ' && szBuffer[i] != '\t'
1290 && szBuffer[i] != '\0' && szBuffer[i] != '\n')
1291 i++;
1292 y--;
1293 } while (y > 0);
1294 }
1295
1296 /* check for blank */
1297 if (szBuffer[i] != ' ' && szBuffer[i] != '\t') /* no copybook specified... */
1298 continue;
1299
1300 /* skip blanks */
1301 while (szBuffer[i] == ' ' || szBuffer[i] == '\t')
1302 i++;
1303
1304 /* if invalid statement then continue with the next line! */
1305 if (szBuffer[i] == '\0' || szBuffer[i] == '\n')
1306 continue;
1307
1308 /* find end */
1309 j = 0;
1310 while (i + j < cbLen && j < CCHMAXPATH
1311 && szBuffer[i+j] != '.'
1312 && szBuffer[i+j] != ' ' && szBuffer[i+j] != '\t'
1313 && szBuffer[i+j] != '\0' && szBuffer[i+j] != '\n'
1314 )
1315 j++;
1316
1317 /* if invalid statement then continue with the next line! */
1318 if (szBuffer[i+j] != '.' && szBuffer[i+j] != ' ' && szBuffer[i] != '\t')
1319 continue;
1320
1321 /* copy filename */
1322 strncpy(szFullname, &szBuffer[i], j);
1323 szFullname[j] = '\0'; /* ensure terminatition. */
1324
1325 /* add extention .cpy - hardcoded for the moment. */
1326 strcat(szFullname, ".cpy");
1327
1328 /* find include file! */
1329 psz = pathlistFindFile(pOptions->pszInclude, szFullname, szBuffer, pOptions);
1330
1331 /* did we find the include? */
1332 if (psz != NULL)
1333 {
1334 char szBuffer2[CCHMAXPATH];
1335 if (pOptions->fExcludeAll ||
1336 pathlistFindFile(pOptions->pszExclude, szFullname, szBuffer2, pOptions) != NULL
1337 )
1338 depAddDepend(pvRule, szFullname, pOptions->fCheckCyclic);
1339 else
1340 depAddDepend(pvRule, szBuffer, pOptions->fCheckCyclic);
1341 }
1342 else
1343 fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
1344 pszFilename, iLine, szFullname);
1345 }
1346 } /*while*/
1347
1348 return 0;
1349}
1350
1351#define upcase(ch) \
1352 (ch >= 'a' && ch <= 'z' ? ch - ('a' - 'A') : ch)
1353
1354/**
1355 * Compares words. Multiple spaces are treates as on single blank i both string when comparing them.
1356 * @returns 0 equal. (same as strnicmp)
1357 * @param pszS1 String 1
1358 * @param pszS2 String 2
1359 * @param cch Length to compare (relative to string 1)
1360 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1361 */
1362static int strnicmpwords(const char *pszS1, const char *pszS2, int cch)
1363{
1364 do
1365 {
1366 while (cch > 0 && upcase(*pszS1) == upcase(*pszS2) && *pszS1 != ' ')
1367 pszS1++, pszS2++, cch--;
1368
1369 /* blank test and skipping */
1370 if (cch > 0 && *pszS1 == ' ' && *pszS2 == ' ')
1371 {
1372 while (cch > 0 && *pszS1 == ' ')
1373 pszS1++, cch--;
1374
1375 while (*pszS2 == ' ')
1376 pszS2++;
1377 }
1378 else
1379 break;
1380 } while (cch > 0);
1381
1382 return cch == 0 ? 0 : *pszS1 - *pszS2;
1383}
1384
1385
1386/**
1387 * Normalizes the path slashes for the filename. It will partially expand paths too.
1388 * @returns pszFilename
1389 * @param pszFilename Pointer to filename string. Not empty string!
1390 * Much space to play with.
1391 */
1392char *fileNormalize(char *pszFilename)
1393{
1394 char *psz = pszFilename;
1395
1396 /* correct slashes */
1397 while ((pszFilename = strchr(pszFilename, '//')) != NULL)
1398 *pszFilename++ = '\\';
1399
1400 /* expand path? */
1401 pszFilename = psz;
1402 if (pszFilename[1] != ':')
1403 { /* relative path */
1404 int iSlash;
1405 char szFile[CCHMAXPATH];
1406 char * psz = szFile;
1407
1408 strcpy(szFile, pszFilename);
1409 iSlash = *psz == '\\' ? 0 : cSlashes;
1410 while (*psz != '\0')
1411 {
1412 if (*psz == '.' && psz[1] == '.' && psz[2] == '\\')
1413 { /* up one directory */
1414 if (iSlash > 0)
1415 iSlash--;
1416 psz += 3;
1417 }
1418 else if (*psz == '.' && psz[1] == '\\')
1419 { /* no change */
1420 psz += 2;
1421 }
1422 else
1423 { /* completed expantion! */
1424 strncpy(pszFilename, szCurDir, aiSlashes[iSlash]+1);
1425 strcpy(pszFilename + aiSlashes[iSlash]+1, psz);
1426 break;
1427 }
1428 }
1429 }
1430 /* else: assume full path */
1431
1432 return psz;
1433}
1434
1435
1436/**
1437 * Normalizes the path slashes for the filename. It will partially expand paths too.
1438 * @returns pszFilename
1439 * @param pszFilename Pointer to filename string. Not empty string!
1440 * Much space to play with.
1441 * @param pszBuffer Pointer to output buffer.
1442 */
1443char *fileNormalize2(const char *pszFilename, char *pszBuffer)
1444{
1445 char *psz = pszBuffer;
1446
1447 /* expand path? */
1448 if (pszFilename[1] != ':')
1449 { /* relative path */
1450 int iSlash;
1451
1452 iSlash = *pszFilename == '\\' || *pszFilename == '/' ? 0 : cSlashes;
1453 while (*pszFilename != '\0')
1454 {
1455 if (*pszFilename == '.' && pszFilename[1] == '.' && (pszFilename[2] == '\\' || pszFilename[1] == '/'))
1456 { /* up one directory */
1457 if (iSlash > 0)
1458 iSlash--;
1459 pszFilename += 3;
1460 }
1461 else if (*pszFilename == '.' && (pszFilename[1] == '\\' || pszFilename[1] == '/'))
1462 { /* no change */
1463 pszFilename += 2;
1464 }
1465 else
1466 { /* completed expantion! */
1467 strncpy(pszBuffer, szCurDir, aiSlashes[iSlash]+1);
1468 strcpy(pszBuffer + aiSlashes[iSlash]+1, pszFilename);
1469 break;
1470 }
1471 }
1472 }
1473 /* else: assume full path */
1474
1475 /* correct slashes */
1476 while ((pszBuffer = strchr(pszBuffer, '//')) != NULL)
1477 *pszBuffer++ = '\\';
1478
1479 return psz;
1480}
1481
1482
1483/**
1484 * Copies the path part (excluding the slash) into pszBuffer and returns
1485 * a pointer to the buffer.
1486 * If no path is found "" is returned.
1487 * @returns Pointer to pszBuffer with path.
1488 * @param pszFilename Pointer to readonly filename.
1489 * @param pszBuffer Pointer to output Buffer.
1490 * @status completely implemented.
1491 * @author knut st. osmundsen
1492 */
1493char *filePath(const char *pszFilename, char *pszBuffer)
1494{
1495 char *psz = strrchr(pszFilename, '\\');
1496 if (psz == NULL)
1497 psz = strrchr(pszFilename, '/');
1498
1499 if (psz == NULL)
1500 *pszBuffer = '\0';
1501 else
1502 {
1503 strncpy(pszBuffer, pszFilename, psz - pszFilename - 1);
1504 pszBuffer[psz - pszFilename - 1] = '\0';
1505 }
1506
1507 return pszBuffer;
1508}
1509
1510
1511/**
1512 * Copies the path part including the slash into pszBuffer and returns
1513 * a pointer to the buffer.
1514 * If no path is found "" is returned.
1515 * @returns Pointer to pszBuffer with path.
1516 * @param pszFilename Pointer to readonly filename.
1517 * @param pszBuffer Pointer to output Buffer.
1518 * @status completely implemented.
1519 * @author knut st. osmundsen
1520 */
1521static char *filePathSlash(const char *pszFilename, char *pszBuffer)
1522{
1523 char *psz = strrchr(pszFilename, '\\');
1524 if (psz == NULL)
1525 psz = strrchr(pszFilename, '/');
1526
1527 if (psz == NULL)
1528 *pszBuffer = '\0';
1529 else
1530 {
1531 strncpy(pszBuffer, pszFilename, psz - pszFilename + 1);
1532 pszBuffer[psz - pszFilename + 1] = '\0';
1533 }
1534
1535 return pszBuffer;
1536}
1537
1538
1539/**
1540 * Copies the path part including the slash into pszBuffer and returns
1541 * a pointer to the buffer. If no path is found "" is returned.
1542 * The path is normalized to only use '\\'.
1543 * @returns Pointer to pszBuffer with path.
1544 * @param pszFilename Pointer to readonly filename.
1545 * @param pszBuffer Pointer to output Buffer.
1546 * @status completely implemented.
1547 * @author knut st. osmundsen
1548 */
1549static char *filePathSlash2(const char *pszFilename, char *pszBuffer)
1550{
1551 char *psz = strrchr(pszFilename, '\\');
1552 if (psz == NULL)
1553 psz = strrchr(pszFilename, '/');
1554
1555 if (psz == NULL)
1556 *pszBuffer = '\0';
1557 else
1558 {
1559 strncpy(pszBuffer, pszFilename, psz - pszFilename + 1);
1560 pszBuffer[psz - pszFilename + 1] = '\0';
1561
1562 /* normalize all '/' to '\\' */
1563 psz = pszBuffer;
1564 while ((psz = strchr(psz, '/')) != NULL)
1565 *psz++ = '\\';
1566 }
1567
1568 return pszBuffer;
1569}
1570
1571
1572/**
1573 * Copies the filename (with extention) into pszBuffer and returns
1574 * a pointer to the buffer.
1575 * @returns Pointer to pszBuffer with path.
1576 * @param pszFilename Pointer to readonly filename.
1577 * @param pszBuffer Pointer to output Buffer.
1578 * @status completely implemented.
1579 * @author knut st. osmundsen
1580 */
1581char *fileName(const char *pszFilename, char *pszBuffer)
1582{
1583 char *psz = strrchr(pszFilename, '\\');
1584 if (psz == NULL)
1585 psz = strrchr(pszFilename, '/');
1586
1587 strcpy(pszBuffer, psz == NULL ? pszFilename : psz + 1);
1588
1589 return pszBuffer;
1590}
1591
1592
1593/**
1594 * Copies the name part with out extention into pszBuffer and returns
1595 * a pointer to the buffer.
1596 * If no name is found "" is returned.
1597 * @returns Pointer to pszBuffer with path.
1598 * @param pszFilename Pointer to readonly filename.
1599 * @param pszBuffer Pointer to output Buffer.
1600 * @status completely implemented.
1601 * @author knut st. osmundsen
1602 */
1603char *fileNameNoExt(const char *pszFilename, char *pszBuffer)
1604{
1605 char *psz = strrchr(pszFilename, '\\');
1606 if (psz == NULL)
1607 psz = strrchr(pszFilename, '/');
1608
1609 strcpy(pszBuffer, psz == NULL ? pszFilename : psz + 1);
1610
1611 psz = strrchr(pszBuffer, '.');
1612 if (psz > pszBuffer) /* an extetion on it's own (.depend) is a filename not an extetion! */
1613 *psz = '\0';
1614
1615 return pszBuffer;
1616}
1617
1618
1619/**
1620 * Copies the extention part into pszBuffer and returns
1621 * a pointer to the buffer.
1622 * If no extention is found "" is returned.
1623 * The dot ('.') is not included!
1624 * @returns Pointer to pszBuffer with path.
1625 * @param pszFilename Pointer to readonly filename.
1626 * @param pszBuffer Pointer to output Buffer.
1627 * @status completely implemented.
1628 * @author knut st. osmundsen
1629 */
1630char *fileExt(const char *pszFilename, char *pszBuffer)
1631{
1632 char *psz = strrchr(pszFilename, '.');
1633 if (psz != NULL)
1634 {
1635 if (strchr(psz, '\\') != NULL || strchr(psz, '/') != NULL)
1636 *pszBuffer = '\0';
1637 else
1638 strcpy(pszBuffer, psz + 1);
1639 }
1640 else
1641 *pszBuffer = '\0';
1642
1643 return pszBuffer;
1644}
1645
1646
1647/**
1648 * Adds a file to the cache.
1649 * @returns Success indicator.
1650 * @param pszFilename Name of the file which is to be added. (with path!)
1651 */
1652static BOOL filecacheAddFile(const char *pszFilename)
1653{
1654 PFCACHEENTRY pfcNew;
1655
1656 /* allocate new block and fill in data */
1657 pfcNew = malloc(sizeof(FCACHEENTRY) + strlen(pszFilename) + 1);
1658 if (pfcNew == NULL)
1659 {
1660 fprintf(stderr, "error: out of memory! (line=%d)\n", __LINE__);
1661 return FALSE;
1662 }
1663 pfcNew->Key = (char*)(void*)pfcNew + sizeof(FCACHEENTRY);
1664 strcpy((char*)(unsigned)pfcNew->Key, pszFilename);
1665 if (!AVLInsert(&pfcTree, pfcNew))
1666 {
1667 free(pfcNew);
1668 return TRUE;
1669 }
1670 cfcNodes++;
1671
1672 return TRUE;
1673}
1674
1675
1676
1677/**
1678 * Adds a file to the cache.
1679 * @returns Success indicator.
1680 * @param pszDir Name of the path which is to be added. (with slash!)
1681 */
1682static BOOL filecacheAddDir(const char *pszDir)
1683{
1684 PFCACHEENTRY pfcNew;
1685 APIRET rc;
1686 char szDir[CCHMAXPATH];
1687 int cchDir;
1688 char achBuffer[16384];
1689 PFILEFINDBUF3 pfindbuf3 = (PFILEFINDBUF3)(void*)&achBuffer[0];
1690 HDIR hDir = HDIR_CREATE;
1691 ULONG cFiles = 0xFFFFFFF;
1692 int i;
1693
1694 /* Make path */
1695 filePathSlash2(pszDir, szDir);
1696 strlwr(szDir); /* Convert name to lower case to allow faster searchs! */
1697 cchDir = strlen(szDir);
1698
1699
1700 /* Add directory to pfcDirTree. */
1701 pfcNew = malloc(sizeof(FCACHEENTRY) + cchDir + 1);
1702 if (pfcNew == NULL)
1703 {
1704 fprintf(stderr, "error: out of memory! (line=%d)\n", __LINE__);
1705 DosFindClose(hDir);
1706 return FALSE;
1707 }
1708 pfcNew->Key = (char*)(void*)pfcNew + sizeof(FCACHEENTRY);
1709 strcpy((char*)(unsigned)pfcNew->Key, szDir);
1710 AVLInsert(&pfcDirTree, pfcNew);
1711
1712
1713 /* Start to search directory - all files */
1714 strcat(szDir + cchDir, "*");
1715 rc = DosFindFirst(szDir, &hDir, FILE_NORMAL,
1716 pfindbuf3, sizeof(achBuffer),
1717 &cFiles, FIL_STANDARD);
1718 while (rc == NO_ERROR)
1719 {
1720 for (i = 0;
1721 i < cFiles;
1722 i++, pfindbuf3 = (PFILEFINDBUF3)((int)pfindbuf3 + pfindbuf3->oNextEntryOffset)
1723 )
1724 {
1725 pfcNew = malloc(sizeof(FCACHEENTRY) + cchDir + pfindbuf3->cchName + 1);
1726 if (pfcNew == NULL)
1727 {
1728 fprintf(stderr, "error: out of memory! (line=%d)\n", __LINE__);
1729 DosFindClose(hDir);
1730 return FALSE;
1731 }
1732 pfcNew->Key = (char*)(void*)pfcNew + sizeof(FCACHEENTRY);
1733 strcpy((char*)(unsigned)pfcNew->Key, szDir);
1734 strcpy((char*)(unsigned)pfcNew->Key + cchDir, pfindbuf3->achName);
1735 strlwr((char*)(unsigned)pfcNew->Key + cchDir); /* Convert name to lower case to allow faster searchs! */
1736 if (!AVLInsert(&pfcTree, pfcNew))
1737 free(pfcNew);
1738 else
1739 cfcNodes++;
1740 }
1741
1742 /* next */
1743 cFiles = 0xFFFFFFF;
1744 pfindbuf3 = (PFILEFINDBUF3)(void*)&achBuffer[0];
1745 rc = DosFindNext(hDir, pfindbuf3, sizeof(achBuffer), &cFiles);
1746 }
1747
1748 DosFindClose(hDir);
1749
1750 return TRUE;
1751}
1752
1753
1754/**
1755 * Checks if pszFilename is exists in the cache.
1756 * @return TRUE if found. FALSE if not found.
1757 * @param pszFilename Name of the file to be found. (with path!)
1758 * This is in lower case!
1759 */
1760INLINE BOOL filecacheFind(const char *pszFilename)
1761{
1762 return AVLGet(&pfcTree, (AVLKEY)pszFilename) != NULL;
1763}
1764
1765
1766/**
1767 * Checks if pszFilename is exists in the cache.
1768 * @return TRUE if found. FALSE if not found.
1769 * @param pszFilename Name of the file to be found. (with path!)
1770 * This is in lower case!
1771 */
1772INLINE BOOL filecacheIsDirCached(const char *pszDir)
1773{
1774 return AVLGet(&pfcDirTree, (AVLKEY)pszDir) != NULL;
1775}
1776
1777
1778
1779/**
1780 * Finds a filename in a specified pathlist.
1781 * @returns Pointer to a filename consiting of the path part + the given filename.
1782 * (pointer into pszBuffer)
1783 * NULL if file is not found. ("" in buffer)
1784 * @param pszPathList Path list to search for filename.
1785 * @parma pszFilename Filename to find.
1786 * @parma pszBuffer Ouput Buffer.
1787 * @param pOptions Pointer to options struct.
1788 * @status completely implemented.
1789 * @author knut st. osmundsen
1790 * @remark need substantial optimizations. 95% of execution is spend here.
1791 */
1792static char *pathlistFindFile(const char *pszPathList, const char *pszFilename, char *pszBuffer, POPTIONS pOptions)
1793{
1794 const char *psz = pszPathList;
1795 const char *pszNext = NULL;
1796
1797 *pszBuffer = '\0';
1798
1799 if (pszPathList == NULL)
1800 return NULL;
1801
1802 while (*psz != '\0')
1803 {
1804 /* find end of this path */
1805 pszNext = strchr(psz, ';');
1806 if (pszNext == NULL)
1807 pszNext = psz + strlen(psz);
1808
1809 if (pszNext - psz > 0)
1810 {
1811 APIRET rc;
1812
1813 /* make search statment */
1814 strncpy(pszBuffer, psz, pszNext - psz);
1815 pszBuffer[pszNext - psz] = '\0';
1816 if (pszBuffer[pszNext - psz - 1] != '\\' && pszBuffer[pszNext - psz - 1] != '/')
1817 strcpy(&pszBuffer[pszNext - psz], "\\");
1818 strcat(pszBuffer, pszFilename);
1819 strlwr(pszBuffer); /* to speed up AVL tree search we'll lowercase all names. */
1820 fileNormalize(pszBuffer);
1821
1822 /*
1823 * Search for the file in this directory.
1824 * Search cache first
1825 */
1826 if (!filecacheFind(pszBuffer))
1827 {
1828 char szDir[CCHMAXPATH];
1829
1830 filePathSlash(pszBuffer, szDir);
1831 if (!filecacheIsDirCached(szDir))
1832 {
1833 /*
1834 * If caching of entire dirs are enabled, we'll
1835 * add the directory to the cache and search it.
1836 */
1837 if (pOptions->fCacheSearchDirs && filecacheAddDir(szDir))
1838 {
1839 if (filecacheFind(pszBuffer))
1840 return pszBuffer;
1841 }
1842 else
1843 {
1844 FILESTATUS3 fsts3;
1845
1846 /* ask the OS */
1847 rc = DosQueryPathInfo(pszBuffer, FIL_STANDARD, &fsts3, sizeof(fsts3));
1848 if (rc == NO_ERROR)
1849 { /* add file to cache. */
1850 filecacheAddFile(pszBuffer);
1851 return pszBuffer;
1852 }
1853 }
1854 }
1855 }
1856 else
1857 return pszBuffer;
1858 }
1859
1860 /* next */
1861 if (*pszNext != ';')
1862 break;
1863 psz = pszNext + 1;
1864 }
1865
1866 return NULL;
1867}
1868
1869
1870/**
1871 * Finds the first char after word.
1872 * @returns Pointer to the first char after word.
1873 * @param psz Where to start.
1874 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1875 */
1876static char *findEndOfWord(char *psz)
1877{
1878
1879 while (*psz != '\0' &&
1880 (
1881 (*psz >= 'A' && *psz <= 'Z') || (*psz >= 'a' && *psz <= 'z')
1882 ||
1883 (*psz >= '0' && *psz <= '9')
1884 ||
1885 *psz == '_'
1886 )
1887 )
1888 ++psz;
1889 return (char *)psz;
1890}
1891
1892#if 0 /* not used */
1893/**
1894 * Find the starting char of a word
1895 * @returns Pointer to first char in word.
1896 * @param psz Where to start.
1897 * @param pszStart Where to stop.
1898 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1899 */
1900static char *findStartOfWord(const char *psz, const char *pszStart)
1901{
1902 const char *pszR = psz;
1903 while (psz >= pszStart &&
1904 (
1905 (*psz >= 'A' && *psz <= 'Z')
1906 || (*psz >= 'a' && *psz <= 'z')
1907 || (*psz >= '0' && *psz <= '9')
1908 || *psz == '_'
1909 )
1910 )
1911 pszR = psz--;
1912 return (char*)pszR;
1913}
1914#endif
1915
1916/**
1917 * Find the size of a file.
1918 * @returns Size of file. -1 on error.
1919 * @param phFile File handle.
1920 */
1921static signed long fsize(FILE *phFile)
1922{
1923 int ipos;
1924 signed long cb;
1925
1926 if ((ipos = ftell(phFile)) < 0
1927 ||
1928 fseek(phFile, 0, SEEK_END) != 0
1929 ||
1930 (cb = ftell(phFile)) < 0
1931 ||
1932 fseek(phFile, ipos, SEEK_SET) != 0
1933 )
1934 cb = -1;
1935 return cb;
1936}
1937
1938
1939
1940/**
1941 * Trims a string, ie. removing spaces (and tabs) from both ends of the string.
1942 * @returns Pointer to first not space or tab char in the string.
1943 * @param psz Pointer to the string which is to be trimmed.
1944 * @status completely implmented.
1945 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1946 */
1947INLINE char *trim(char *psz)
1948{
1949 int i;
1950 if (psz == NULL)
1951 return NULL;
1952 while (*psz == ' ' || *psz == '\t')
1953 psz++;
1954 i = strlen(psz) - 1;
1955 while (i >= 0 && (psz[i] == ' ' || *psz == '\t'))
1956 i--;
1957 psz[i+1] = '\0';
1958 return psz;
1959}
1960
1961
1962/**
1963 * Right trims a string, ie. removing spaces (and tabs) from the end of the stri
1964 * @returns Pointer to the string passed in.
1965 * @param psz Pointer to the string which is to be right trimmed.
1966 * @status completely implmented.
1967 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1968 */
1969INLINE char *trimR(char *psz)
1970{
1971 int i;
1972 if (psz == NULL)
1973 return NULL;
1974 i = strlen(psz) - 1;
1975 while (i >= 0 && (psz[i] == ' ' || *psz == '\t'))
1976 i--;
1977 psz[i+1] = '\0';
1978 return psz;
1979}
1980
1981
1982/**
1983 * Creates a memory buffer for a text file.
1984 * @returns Pointer to file memoryblock. NULL on error.
1985 * @param pszFilename Pointer to filename string.
1986 */
1987static void *textbufferCreate(const char *pszFilename)
1988{
1989 void *pvFile = NULL;
1990 FILE *phFile;
1991
1992 phFile = fopen(pszFilename, "r");
1993 if (phFile != NULL)
1994 {
1995 signed long cbFile = fsize(phFile);
1996 if (cbFile > 0)
1997 {
1998 pvFile = malloc(cbFile + 1);
1999 if (pvFile != NULL)
2000 {
2001 memset(pvFile, 0, cbFile + 1);
2002 if (fread(pvFile, 1, cbFile, phFile) == 0)
2003 { /* failed! */
2004 free(pvFile);
2005 pvFile = NULL;
2006 }
2007 }
2008 else
2009 fprintf(stderr, "warning/error: failed to open file %s\n", pszFilename);
2010 }
2011 fclose(phFile);
2012 }
2013 return pvFile;
2014}
2015
2016
2017/**
2018 * Destroys a text textbuffer.
2019 * @param pvBuffer Buffer handle.
2020 */
2021static void textbufferDestroy(void *pvBuffer)
2022{
2023 free(pvBuffer);
2024}
2025
2026
2027/**
2028 * Gets the next line from an textbuffer.
2029 * @returns Pointer to the next line.
2030 * @param pvBuffer Buffer handle.
2031 * @param psz Pointer to current line.
2032 * NULL is passed in to get the first line.
2033 */
2034static char *textbufferNextLine(void *pvBuffer, register char *psz)
2035{
2036 register char ch;
2037
2038 /* if first line psz is NULL. */
2039 if (psz == NULL)
2040 return (char*)pvBuffer;
2041
2042 /* skip till end of file or end of line. */
2043 ch = *psz;
2044 while (ch != '\0' && ch != '\n' && ch != '\r')
2045 ch = *++psz;
2046
2047 /* skip line end */
2048 if (ch == '\n')
2049 psz++;
2050 if (*psz == '\r')
2051 psz++;
2052
2053 return psz;
2054}
2055
2056
2057/**
2058 * Gets the next line from an textbuffer.
2059 * (fgets for textbuffer)
2060 * @returns Pointer to pszOutBuffer. NULL when end of file.
2061 * @param pvBuffer Buffer handle.
2062 * @param ppv Pointer to a buffer index pointer. (holds the current buffer index)
2063 * Pointer to a null pointer is passed in to get the first line.
2064 * @param pszLineBuffer Output line buffer. (!= NULL)
2065 * @param cchLineBuffer Size of the output line buffer. (> 0)
2066 * @remark '\n' and '\r' are removed!
2067 */
2068static char *textbufferGetNextLine(void *pvBuffer, void **ppv, char *pszLineBuffer, int cchLineBuffer)
2069{
2070 char * pszLine = pszLineBuffer;
2071 char * psz = *(char**)(void*)ppv;
2072 register char ch;
2073
2074 /* first line? */
2075 if (psz == NULL)
2076 psz = pvBuffer;
2077
2078 /* Copy to end of the line or end of the linebuffer. */
2079 ch = *psz;
2080 cchLineBuffer--; /* reserve space for '\0' */
2081 while (cchLineBuffer > 0 && ch != '\0' && ch != '\n' && ch != '\r')
2082 {
2083 *pszLine++ = ch;
2084 ch = *++psz;
2085 }
2086 *pszLine = '\0';
2087
2088 /* skip line end */
2089 if (ch == '\n')
2090 ch = *++psz;
2091 if (ch == '\r')
2092 psz++;
2093
2094 /* check if position has changed - if unchanged it's the end of file! */
2095 if (*ppv == (void*)psz)
2096 pszLineBuffer = NULL;
2097
2098 /* store current position */
2099 *ppv = (void*)psz;
2100
2101 return pszLineBuffer;
2102}
2103
2104
2105/**
2106 * Appends a depend file to the internal file.
2107 */
2108static BOOL depReadFile(const char *pszFilename, POPTIONS pOptions)
2109{
2110 void *pvFile;
2111 char *pszNext;
2112 BOOL fMoreDeps = FALSE;
2113 void *pvRule = NULL;
2114
2115 /* read depend file */
2116 pvFile = textbufferCreate(pszFilename);
2117 if (pvFile == NULL)
2118 return FALSE;
2119
2120 /* parse the original depend file */
2121 pszNext = pvFile;
2122 while (*pszNext != '\0')
2123 {
2124 int i;
2125 int cch;
2126 char *psz;
2127
2128 /* get the next line. */
2129 psz = pszNext;
2130 pszNext = textbufferNextLine(pvFile, pszNext);
2131
2132 /*
2133 * Process the current line:
2134 * Start off by terminating the line.
2135 * Trim the line,
2136 * Skip empty lines.
2137 * If not looking for more deps Then
2138 * Check if new rule starts here.
2139 * Endif
2140 *
2141 * If more deps to last rule Then
2142 * Get dependant name.
2143 * Endif
2144 */
2145 i = -1;
2146 while (psz <= &pszNext[i] && pszNext[i] == '\n' || pszNext[i] == '\r')
2147 pszNext[i--] = '\0';
2148 trimR(psz);
2149 cch = strlen(psz);
2150 if (cch == 0)
2151 continue;
2152
2153 /* new rule? */
2154 if (!fMoreDeps && *psz != ' ' && *psz != '\t' && *psz != '\0')
2155 {
2156 i = 0;
2157 while (psz[i] != '\0')
2158 {
2159 if (psz[i] == ':'
2160 && (psz[i+1] == ' '
2161 || psz[i+1] == '\t'
2162 || psz[i+1] == '\0'
2163 || psz[i+1] == '\\'
2164 )
2165 )
2166 break;
2167 i++;
2168 }
2169
2170 if (psz[i] == ':')
2171 { /* new rule! */
2172 psz[i] = '\0';
2173 pvRule = depAddRule(trimR(psz), NULL, NULL);
2174 psz += i + 1;
2175 cch -= i + 1;
2176 fMoreDeps = TRUE;
2177 }
2178 }
2179
2180 /* more dependants */
2181 if (fMoreDeps)
2182 {
2183 if (cch > 0 && psz[cch-1] == '\\')
2184 {
2185 fMoreDeps = TRUE;
2186 psz[cch-1] = '\0';
2187 }
2188 else
2189 fMoreDeps = FALSE;
2190
2191 /* if not duplicate rule */
2192 if (pvRule != NULL)
2193 {
2194 psz = trim(psz);
2195 if (*psz != '\0')
2196 depAddDepend(pvRule, psz, pOptions->fCheckCyclic);
2197 }
2198 }
2199 } /* while */
2200
2201
2202 /* return succesfully */
2203 textbufferDestroy(pvFile);
2204 return TRUE;
2205}
2206
2207/**
2208 *
2209 * @returns Success indicator.
2210 * @params pszFilename Pointer to name of the output file.
2211 */
2212static BOOL depWriteFile(const char *pszFilename)
2213{
2214 FILE *phFile;
2215 phFile = fopen(pszFilename, "w");
2216 if (phFile != NULL)
2217 {
2218 AVLENUMDATA EnumData;
2219 PDEPRULE pdep;
2220 char szBuffer[4096];
2221 int iBuffer = 0;
2222 int cch;
2223
2224 pdep = (PDEPRULE)(void*)AVLBeginEnumTree((PPAVLNODECORE)(void*)&pdepTree, &EnumData, TRUE);
2225 while (pdep != NULL)
2226 {
2227 /* Write rule. Flush the buffer first if necessary. */
2228 cch = strlen(pdep->pszRule);
2229 if (iBuffer + cch + 2 >= sizeof(szBuffer))
2230 {
2231 fwrite(szBuffer, iBuffer, 1, phFile);
2232 iBuffer = 0;
2233 }
2234 strcpy(szBuffer + iBuffer, pdep->pszRule);
2235 iBuffer += cch;
2236 strcpy(szBuffer + iBuffer++, ":");
2237
2238 /* write rule dependants. */
2239 if (pdep->papszDep != NULL)
2240 {
2241 char **ppsz = pdep->papszDep;
2242 while (*ppsz != NULL)
2243 {
2244 /* flush buffer? */
2245 if (iBuffer + strlen(*ppsz) + 20 >= sizeof(szBuffer))
2246 {
2247 fwrite(szBuffer, iBuffer, 1, phFile);
2248 iBuffer = 0;
2249 }
2250 iBuffer += sprintf(szBuffer + iBuffer, " \\\n %s", *ppsz);
2251
2252 /* next dependant */
2253 ppsz++;
2254 }
2255 }
2256
2257 /* Add two new lines. Flush buffer first if necessary. */
2258 if (iBuffer + 2 >= sizeof(szBuffer))
2259 {
2260 fwrite(szBuffer, iBuffer, 1, phFile);
2261 iBuffer = 0;
2262 }
2263 strcpy(szBuffer + iBuffer, "\n\n");
2264 iBuffer += 2;
2265
2266 /* next rule */
2267 pdep = (PDEPRULE)(void*)AVLGetNextNode(&EnumData);
2268 }
2269
2270 /* flush buffer. */
2271 fwrite(szBuffer, iBuffer, 1, phFile);
2272
2273 fclose(phFile);
2274 return TRUE;
2275 }
2276
2277 return FALSE;
2278}
2279
2280
2281/**
2282 * Removes all nodes in the tree of dependencies. (pdepTree)
2283 */
2284static void depRemoveAll(void)
2285{
2286 AVLENUMDATA EnumData;
2287 PDEPRULE pdep;
2288
2289 pdep = (PDEPRULE)(void*)AVLBeginEnumTree((PPAVLNODECORE)(void*)&pdepTree, &EnumData, TRUE);
2290 while (pdep != NULL)
2291 {
2292 /* free this */
2293 if (pdep->papszDep != NULL)
2294 {
2295 char ** ppsz = pdep->papszDep;
2296 while (*ppsz != NULL)
2297 free(*ppsz++);
2298 free(pdep->papszDep);
2299 }
2300 free(pdep);
2301
2302 /* next */
2303 pdep = (PDEPRULE)(void*)AVLGetNextNode(&EnumData);
2304 }
2305 pdepTree = NULL;
2306}
2307
2308
2309/**
2310 * Adds a rule to the list of dependant rules.
2311 * @returns Rule handle. NULL if rule exists/error.
2312 * @param pszRulePath Pointer to rule text. Empty strings are banned!
2313 * This string might only contain the path of the rule. (with '\\')
2314 * @param pszName Name of the rule.
2315 * NULL if pszRulePath contains the entire rule.
2316 * @param pszExt Extention (without '.')
2317 * NULL if pszRulePath or pszRulePath and pszName contains the entire rule.
2318 */
2319static void *depAddRule(const char *pszRulePath, const char *pszName, const char *pszExt)
2320{
2321 char szRule[CCHMAXPATH*2];
2322 PDEPRULE pNew;
2323 int cch;
2324
2325 /* make rulename */
2326 strcpy(szRule, pszRulePath);
2327 cch = strlen(szRule);
2328 if (pszName != NULL)
2329 {
2330 strcpy(szRule + cch, pszName);
2331 cch += strlen(szRule + cch);
2332 }
2333 if (pszExt != NULL)
2334 {
2335 strcat(szRule + cch++, ".");
2336 strcat(szRule + cch, pszExt);
2337 cch += strlen(szRule + cch);
2338 }
2339
2340
2341 /*
2342 * Allocate a new rule structure and fill in data
2343 * Note. One block for both the DEPRULE and the pszRule string.
2344 */
2345 pNew = malloc(sizeof(DEPRULE) + cch + 1);
2346 if (pNew == NULL)
2347 {
2348 fprintf(stderr, "error: out of memory. (line=%d)\n", __LINE__);
2349 return NULL;
2350 }
2351 pNew->pszRule = (char*)(void*)(pNew + 1);
2352 strcpy(pNew->pszRule, szRule);
2353 pNew->cDeps = 0;
2354 pNew->papszDep = NULL;
2355 pNew->avlCore.Key = pNew->pszRule;
2356
2357 /* Insert rule */
2358 if (!AVLInsert((PPAVLNODECORE)(void*)&pdepTree, &pNew->avlCore))
2359 { /* rule existed - return NULL */
2360 free(pNew);
2361 return NULL;
2362 }
2363
2364 return pNew;
2365}
2366
2367
2368
2369/**
2370 * Adds a dependant to a rule.
2371 * @returns Successindicator. TRUE = success.
2372 * FALSE = cyclic or out of memory.
2373 * @param pvRule Rule handle.
2374 * @param pszDep Pointer to dependant name
2375 * @param fCheckCyclic When set we'll check that we're not creating an cyclic dependency.
2376 */
2377static BOOL depAddDepend(void *pvRule, const char *pszDep, BOOL fCheckCyclic)
2378{
2379 PDEPRULE pdep = (PDEPRULE)pvRule;
2380
2381 if (fCheckCyclic && depCheckCyclic(pdep, pszDep))
2382 {
2383 fprintf(stderr, "warning: Cylic dependancy caused us to ignore '%s' in rule '%s'.\n",
2384 pszDep, pdep->pszRule);
2385 return FALSE;
2386 }
2387
2388 /* allocate more array space */
2389 if (((pdep->cDeps) % 48) == 0)
2390 {
2391 pdep->papszDep = realloc(pdep->papszDep, sizeof(char*) * (pdep->cDeps + 50));
2392 if (pdep->papszDep == NULL)
2393 {
2394 pdep->cDeps = 0;
2395 fprintf(stderr, "error: out of memory, (line=%d)\n", __LINE__);
2396 return FALSE;
2397 }
2398 }
2399
2400 /* allocate string space and copy pszDep */
2401 if ((pdep->papszDep[pdep->cDeps] = malloc(strlen(pszDep) + 1)) == NULL)
2402 {
2403 fprintf(stderr, "error: out of memory, (line=%d)\n", __LINE__);
2404 return FALSE;
2405 }
2406 strcpy(pdep->papszDep[pdep->cDeps], pszDep);
2407
2408 /* terminate array and increment dep count */
2409 pdep->papszDep[++pdep->cDeps] = NULL;
2410
2411 /* successful! */
2412 return TRUE;
2413}
2414
2415
2416/**
2417 * Checks if adding this dependent will create a cylic dependency.
2418 * @returns TRUE: Cyclic.
2419 * FALSE: Non-cylic.
2420 * @param pdepRule Rule pszDep is to be inserted in.
2421 * @param pszDep Depend name.
2422 */
2423static BOOL depCheckCyclic(PDEPRULE pdepRule, const char *pszDep)
2424{
2425 #define DEPTH 32
2426 char * pszRule = pdepRule->pszRule;
2427 char ** appsz[DEPTH];
2428 PDEPRULE pdep;
2429 int i;
2430
2431 /* find rule for the dep. */
2432 if ((pdep = (PDEPRULE)(void*)AVLGet((PPAVLNODECORE)(void*)&pdepTree, pszDep)) == NULL
2433 || pdep->papszDep == NULL)
2434 return FALSE; /* no rule, or no dependents, not cyclic */
2435
2436 i = 0;
2437 appsz[0] = pdep->papszDep;
2438 while (i >= 0)
2439 {
2440 register char ** ppsz = appsz[i];
2441
2442 while (*ppsz != NULL)
2443 {
2444 /* check if equal to the main rule */
2445 if (strcmp(pszRule, *ppsz) == 0)
2446 return TRUE;
2447
2448 /* push onto stack (ppsz is incremented in this test!) */
2449 if ((pdep = (PDEPRULE)(void*)AVLGet((PPAVLNODECORE)(void*)&pdepTree, *ppsz++)) != NULL
2450 && pdep->papszDep != NULL)
2451 {
2452 if (i >= DEPTH)
2453 {
2454 fprintf(stderr, "error: too deap chain (%d). pszRule=%s pszDep=%s\n",
2455 i, pszRule, pszDep);
2456 return FALSE;
2457 }
2458 appsz[i++] = ppsz; /* save next */
2459 ppsz = pdep->papszDep; /* start processing new node */
2460 }
2461 }
2462
2463 /* pop stack */
2464 i--;
2465 }
2466
2467 return FALSE;
2468}
2469
2470
2471/*
2472 * Testing purpose.
2473 */
2474#include <os2.h>
2475#ifdef OLEMANN
2476#include "olemann.h"
2477#endif
2478
2479
2480
Note: See TracBrowser for help on using the repository browser.