| 1 | /*
|
|---|
| 2 | * Fast dependands. (Fase = Quick and Dirty!)
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (c) 1999 knut st. osmundsen
|
|---|
| 5 | *
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #define INCL_DOSERRORS
|
|---|
| 9 | #define INCL_FILEMGR
|
|---|
| 10 | #include <os2.h>
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 | #include <string.h>
|
|---|
| 14 |
|
|---|
| 15 | /*@Global***********************************************************************
|
|---|
| 16 | * Global Variables *
|
|---|
| 17 | *******************************************************************************/
|
|---|
| 18 | static const char pszDefaultDepFile[] = ".depend";
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | /*@IntFunc**********************************************************************
|
|---|
| 22 | * Internal Functions *
|
|---|
| 23 | *******************************************************************************/
|
|---|
| 24 | static int makeDependent(FILE *phDep, const char *pszSource, const char *pszInclude, const char *pszObjectDir);
|
|---|
| 25 | static int getFullIncludename(char *pszFilename, const char *pszInclude);
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Main function.
|
|---|
| 30 | * @returns 0 on success.
|
|---|
| 31 | * -n count of failiures.
|
|---|
| 32 | * @param
|
|---|
| 33 | * @param
|
|---|
| 34 | * @equiv
|
|---|
| 35 | * @precond
|
|---|
| 36 | * @methdesc
|
|---|
| 37 | * @result
|
|---|
| 38 | * @time
|
|---|
| 39 | * @sketch
|
|---|
| 40 | * @algo
|
|---|
| 41 | * @remark
|
|---|
| 42 | */
|
|---|
| 43 | int main(int argc, char **argv)
|
|---|
| 44 | {
|
|---|
| 45 | FILE *phDep;
|
|---|
| 46 | int rc = 0;
|
|---|
| 47 | int argi = 1;
|
|---|
| 48 | const char *pszDepFile = pszDefaultDepFile;
|
|---|
| 49 | char szObjectDir[CCHMAXPATH] = {0};
|
|---|
| 50 | static char szInclude[32768] = ";";
|
|---|
| 51 |
|
|---|
| 52 | /* look for depend filename option "-d <filename>" */
|
|---|
| 53 | if (argc >= 3 && strcmp(argv[1], "-d") == 0)
|
|---|
| 54 | {
|
|---|
| 55 | pszDepFile = argv[2];
|
|---|
| 56 | argi = 3;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | phDep = fopen(pszDepFile, "w");
|
|---|
| 60 | if (phDep != NULL)
|
|---|
| 61 | {
|
|---|
| 62 | while (argi < argc)
|
|---|
| 63 | {
|
|---|
| 64 | ULONG ulRc;
|
|---|
| 65 | FILEFINDBUF3 filebuf;
|
|---|
| 66 | HDIR hDir = HDIR_CREATE;
|
|---|
| 67 | ULONG ulFound = 1;
|
|---|
| 68 |
|
|---|
| 69 | if (argv[argi][0] == '-' || argv[argi][0] == '/')
|
|---|
| 70 | {
|
|---|
| 71 | /* parameters */
|
|---|
| 72 | switch (argv[argi][1])
|
|---|
| 73 | {
|
|---|
| 74 | case 'I': /* optional include path. This has precedence over the INCLUDE environment variable. */
|
|---|
| 75 | case 'i':
|
|---|
| 76 | if (strlen(argv[argi]) > 2)
|
|---|
| 77 | strcat(szInclude, &argv[argi][2]);
|
|---|
| 78 | else
|
|---|
| 79 | {
|
|---|
| 80 | strcat(szInclude, argv[argi+1]);
|
|---|
| 81 | argi++;
|
|---|
| 82 | }
|
|---|
| 83 | if (szInclude[strlen(szInclude)-1] != ';')
|
|---|
| 84 | strcat(szInclude, ";");
|
|---|
| 85 | break;
|
|---|
| 86 |
|
|---|
| 87 | case 'o': /* object base directory */
|
|---|
| 88 | case 'O':
|
|---|
| 89 | strcpy(szObjectDir, argv[argi]+2);
|
|---|
| 90 | break;
|
|---|
| 91 |
|
|---|
| 92 | default:
|
|---|
| 93 | fprintf(stderr, "error: invalid parameter! '%s'\n", argv[argi]);
|
|---|
| 94 | return -1;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | }
|
|---|
| 98 | else
|
|---|
| 99 | {
|
|---|
| 100 | /* not a parameter! */
|
|---|
| 101 | ulRc = DosFindFirst(argv[argi], &hDir,
|
|---|
| 102 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_ARCHIVED,
|
|---|
| 103 | &filebuf, sizeof(FILEFINDBUF3), &ulFound, FIL_STANDARD);
|
|---|
| 104 | while (rc == NO_ERROR && ulFound == 1)
|
|---|
| 105 | {
|
|---|
| 106 | char *psz;
|
|---|
| 107 | char szSource[CCHMAXPATH];
|
|---|
| 108 | char szObjectDirTmp[CCHMAXPATH];
|
|---|
| 109 |
|
|---|
| 110 | if ((psz = strrchr(argv[argi], '\\')) || (psz = strrchr(argv[argi], '/')))
|
|---|
| 111 | {
|
|---|
| 112 | strncpy(szSource, argv[argi], psz - argv[argi] + 1);
|
|---|
| 113 | szSource[psz - argv[argi] + 1] = '\0';
|
|---|
| 114 | if (szObjectDir[0] == '\0')
|
|---|
| 115 | {
|
|---|
| 116 | strncpy(szObjectDirTmp, argv[argi], psz - argv[argi] + 1);
|
|---|
| 117 | szObjectDirTmp[psz - argv[argi] + 1] = '\0';
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | else
|
|---|
| 121 | szSource[0] = '\0';
|
|---|
| 122 | if (szObjectDir[0] != '\0')
|
|---|
| 123 | {
|
|---|
| 124 | int i;
|
|---|
| 125 | strcpy(szObjectDirTmp, szObjectDir);
|
|---|
| 126 | i = strlen(szObjectDirTmp);
|
|---|
| 127 | if (szObjectDirTmp[i - 1] == '\\' || szObjectDirTmp[i - 1] == '/')
|
|---|
| 128 | szObjectDirTmp[i - 1] = '\0';
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | strcat(szSource, filebuf.achName);
|
|---|
| 132 | rc -= makeDependent(phDep, &szSource[0], &szInclude[0], &szObjectDirTmp[0]);
|
|---|
| 133 | ulRc = DosFindNext(hDir, &filebuf, sizeof(filebuf), &ulFound);
|
|---|
| 134 |
|
|---|
| 135 | }
|
|---|
| 136 | DosFindClose(hDir);
|
|---|
| 137 | }
|
|---|
| 138 | /* next */
|
|---|
| 139 | argi++;
|
|---|
| 140 | }
|
|---|
| 141 | } else
|
|---|
| 142 | {
|
|---|
| 143 | fprintf(stderr, "error opening outputfile '%s'.\n", pszDepFile);
|
|---|
| 144 | rc = 1;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | return rc;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * Generates depend info on this file, and fwrites it to phDep.
|
|---|
| 154 | * @returns
|
|---|
| 155 | * @param phDep Pointer to file struct for outfile.
|
|---|
| 156 | * @param pszSource Name of source file.
|
|---|
| 157 | * @param pszIncldue Pointer to additional include path.
|
|---|
| 158 | * @param pszObjectDir Pointer to object directory.
|
|---|
| 159 | */
|
|---|
| 160 | static int makeDependent(FILE *phDep, const char *pszSource, const char *pszInclude, const char *pszObjectDir)
|
|---|
| 161 | {
|
|---|
| 162 | FILE *phFile;
|
|---|
| 163 |
|
|---|
| 164 | phFile = fopen(pszSource, "r");
|
|---|
| 165 | if (phFile != NULL)
|
|---|
| 166 | {
|
|---|
| 167 | char szBuffer[4096]; /* max line lenght */
|
|---|
| 168 | int k = strlen(pszSource) - 1;
|
|---|
| 169 | int l;
|
|---|
| 170 | /**********************************/
|
|---|
| 171 | /* print file name to depend file */
|
|---|
| 172 | /**********************************/
|
|---|
| 173 | while (pszSource[k] != '.' && k >= 0) k--;
|
|---|
| 174 | l = k;
|
|---|
| 175 | while (pszSource[l] != '\\' && pszSource[l] != '/' && l >= 0)
|
|---|
| 176 | l--;
|
|---|
| 177 | if (stricmp(&pszSource[k], ".c") == 0
|
|---|
| 178 | || stricmp(&pszSource[k], ".cpp") == 0
|
|---|
| 179 | || stricmp(&pszSource[k], ".asm") == 0
|
|---|
| 180 | )
|
|---|
| 181 | fprintf(phDep, "%s%.*s.obj:%*s %s",
|
|---|
| 182 | pszObjectDir,
|
|---|
| 183 | k - l, &pszSource[l],
|
|---|
| 184 | (k - l + strlen(pszObjectDir)) + 4 > 20 ? 0 : 20 - (k - l + strlen(pszObjectDir)) - 4, "",
|
|---|
| 185 | pszSource);
|
|---|
| 186 | else if (stricmp(&pszSource[k], ".rc") == 0)
|
|---|
| 187 | fprintf(phDep, "%s%.*s.res:%*s %s",
|
|---|
| 188 | pszObjectDir,
|
|---|
| 189 | k - l, &pszSource[l],
|
|---|
| 190 | (k - l + strlen(pszObjectDir)) + 4 > 20 ? 0 : 20 - (k - l + strlen(pszObjectDir)) - 4, "",
|
|---|
| 191 | pszSource);
|
|---|
| 192 | else
|
|---|
| 193 | fprintf(phDep, "%s:%-*s", pszSource, strlen(pszSource) > 20 ? 0 : 20 - strlen(pszSource), "");
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 | /*******************/
|
|---|
| 197 | /* find dependants */
|
|---|
| 198 | /*******************/
|
|---|
| 199 | while (!feof(phFile))
|
|---|
| 200 | {
|
|---|
| 201 | if (fgets(szBuffer, sizeof(szBuffer), phFile) != NULL)
|
|---|
| 202 | {
|
|---|
| 203 | /* search for #include */
|
|---|
| 204 | int cbLen;
|
|---|
| 205 | int i = 0;
|
|---|
| 206 | int f = 0;
|
|---|
| 207 |
|
|---|
| 208 | cbLen = strlen(szBuffer);
|
|---|
| 209 | while (i + 9 < cbLen
|
|---|
| 210 | && !(f = (strncmp(&szBuffer[i], "#include", 8) == 0
|
|---|
| 211 | || strncmp(&szBuffer[i], "RCINCLUDE", 9) == 0)
|
|---|
| 212 | )
|
|---|
| 213 | /*&& (szBuffer[i] == ' ' || szBuffer[i] == '\t') */
|
|---|
| 214 | )
|
|---|
| 215 | i++;
|
|---|
| 216 |
|
|---|
| 217 | /* found */
|
|---|
| 218 | if (f)
|
|---|
| 219 | {
|
|---|
| 220 | /* extract info between "" or <> */
|
|---|
| 221 | f = 0;
|
|---|
| 222 | while (i < cbLen && !(f = (szBuffer[i] == '"' || szBuffer[i] == '<')))
|
|---|
| 223 | i++;
|
|---|
| 224 | i++; /* skipp '"' or '<' */
|
|---|
| 225 | if (f)
|
|---|
| 226 | {
|
|---|
| 227 | int j;
|
|---|
| 228 | /* find end */
|
|---|
| 229 | j = f = 0;
|
|---|
| 230 | while (i + j < cbLen && j < 260 && !(f = (szBuffer[i+j] == '"' || szBuffer[i+j] == '>')))
|
|---|
| 231 | j++;
|
|---|
| 232 |
|
|---|
| 233 | if (f)
|
|---|
| 234 | {
|
|---|
| 235 | char szFullname[261];
|
|---|
| 236 |
|
|---|
| 237 | /* find include file path */
|
|---|
| 238 | strncpy(&szFullname[1], &szBuffer[i], j);
|
|---|
| 239 | szFullname[j+1] = '\0'; /* ensure terminatition. */
|
|---|
| 240 | if (getFullIncludename(&szFullname[1], pszInclude) == 0)
|
|---|
| 241 | {
|
|---|
| 242 | szFullname[0] = ' '; /* blank char at begining */
|
|---|
| 243 | if (fwrite(&szFullname[0], strlen(szFullname), 1, phDep) != 1)
|
|---|
| 244 | fprintf(stderr, "fwrite failed - loc 2\n");
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | /*
|
|---|
| 252 | else
|
|---|
| 253 | break;
|
|---|
| 254 | */
|
|---|
| 255 | } /*while*/
|
|---|
| 256 | fputs("\n", phDep);
|
|---|
| 257 | fclose(phFile);
|
|---|
| 258 | } else
|
|---|
| 259 | return -1;
|
|---|
| 260 |
|
|---|
| 261 | return 0;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| 268 | * Gets the fullpath include-filename.
|
|---|
| 269 | * @returns 0 on success, -1 on error.
|
|---|
| 270 | * @param pszFilename Input: Pointer to filename to be found, and buffer for output.
|
|---|
| 271 | * Ouput: Buffer now contains fullpath include-filename.
|
|---|
| 272 | * @param pszInclude Additional includepath.
|
|---|
| 273 | */
|
|---|
| 274 | static int getFullIncludename(char *pszFilename, const char *pszInclude)
|
|---|
| 275 | {
|
|---|
| 276 | const char *pszEnvInclude;
|
|---|
| 277 | const char *psz;
|
|---|
| 278 |
|
|---|
| 279 | pszEnvInclude = getenv("INCLUDE");
|
|---|
| 280 | if ((pszEnvInclude == NULL && (pszInclude == NULL || strlen(pszInclude) == 0)) || strlen(pszFilename) == 0)
|
|---|
| 281 | return -1;
|
|---|
| 282 |
|
|---|
| 283 | psz = "";
|
|---|
| 284 | while (psz != NULL && psz != '\0')
|
|---|
| 285 | {
|
|---|
| 286 | const char *pszNext;
|
|---|
| 287 | int cbLen;
|
|---|
| 288 | char szFileTmpIn[260];
|
|---|
| 289 | FILEFINDBUF3 filebuf;
|
|---|
| 290 | ULONG ulRc;
|
|---|
| 291 | HDIR hDir = HDIR_CREATE;
|
|---|
| 292 | ULONG ulFound = 1;
|
|---|
| 293 |
|
|---|
| 294 | /* get addr of next ';' or '\0' */
|
|---|
| 295 | pszNext = strchr(psz, ';');
|
|---|
| 296 | if (pszNext == NULL)
|
|---|
| 297 | pszNext = psz + strlen(psz);
|
|---|
| 298 |
|
|---|
| 299 | /* add a '\\' and the pszFilename string to the include path part. */
|
|---|
| 300 | cbLen = (int)pszNext - (int)psz;
|
|---|
| 301 | if (cbLen > 0)
|
|---|
| 302 | {
|
|---|
| 303 | strncpy(szFileTmpIn, psz, (int)pszNext - (int)psz);
|
|---|
| 304 | if (szFileTmpIn[cbLen - 1] != '\\' && szFileTmpIn[cbLen - 1] != '/')
|
|---|
| 305 | szFileTmpIn[cbLen++] = '\\';
|
|---|
| 306 | }
|
|---|
| 307 | strcpy(&szFileTmpIn[cbLen], pszFilename);
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 | /**************************/
|
|---|
| 311 | /* check if file is found */
|
|---|
| 312 | /**************************/
|
|---|
| 313 | ulRc = DosFindFirst(&szFileTmpIn[0], &hDir,
|
|---|
| 314 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_ARCHIVED,
|
|---|
| 315 | &filebuf, sizeof(FILEFINDBUF3), &ulFound, FIL_STANDARD);
|
|---|
| 316 | if (ulRc == NO_ERROR && ulFound == 1)
|
|---|
| 317 | {
|
|---|
| 318 | strcpy(pszFilename, szFileTmpIn);
|
|---|
| 319 | DosFindClose(hDir);
|
|---|
| 320 | return 0;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /* next */
|
|---|
| 324 | if (*pszNext == ';' && pszNext[1] != '\0')
|
|---|
| 325 | psz = pszNext + 1;
|
|---|
| 326 | else
|
|---|
| 327 | {
|
|---|
| 328 | psz = pszInclude;
|
|---|
| 329 | pszInclude = NULL;
|
|---|
| 330 |
|
|---|
| 331 | if (psz == NULL)
|
|---|
| 332 | {
|
|---|
| 333 | psz = pszEnvInclude;
|
|---|
| 334 | pszEnvInclude = NULL;
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | return -1;
|
|---|
| 340 | }
|
|---|