home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / mkmake.lha / mkmake.c < prev    next >
C/C++ Source or Header  |  1992-06-13  |  15KB  |  597 lines

  1. /* mkmake.c -- Martin Hohl -- 28.Dez.1991 -- All rights reserved */
  2. /********************************************************************/
  3. /* Name:  mkmake.c                                                  */
  4. /* Zweck: Makefile-Generator für MS-DOS und AmigaDOS        |    |  */
  5. /*                                                          |    |  */
  6. /* Autor: Martin Hohl                                       |\__/`  */
  7. /*        Robert-Leicht-Strasse 132                         |       */
  8. /*        D-7000 Stuttgart 80                               |       */
  9. /*        F.R.Germany                                               */
  10. /* Version:  0.2 vom 07.05.1992                                     */
  11. /* Compiler: AmigaDOS: SAS/C 5.10                                   */
  12. /* Letzte Modifikation:  07.05.1992                                 */
  13. /* Status: Freeware, (C)opyright by Martin Hohl                     */
  14. /********************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #ifdef __TURBOC__
  21. #include <dos.h>
  22. #include <dir.h>
  23. #define MSDOS
  24.  
  25. #define OBJEXT "OBJ"
  26. #define OBJEXTLC "obj"
  27. #define LIBRARIAN "tlib"
  28. #endif
  29.  
  30. #ifdef AMIGA
  31. #include <exec/types.h>
  32. #include <exec/memory.h>
  33. #include <libraries/dos.h>
  34. #include <libraries/dosextens.h>
  35.  
  36. #define OBJEXT "O"
  37. #define OBJEXTLC "o"
  38. #define LIBRARIAN "oml"
  39.  
  40. extern struct DosLibrary *DOSBase;
  41.  
  42. #define DosVersion (DOSBase->dl_lib.lib_Version)
  43.  
  44. #define NAMESIZE 128
  45. #endif
  46.  
  47. #ifdef AZTEC_C
  48. #include <functions.h>
  49. #endif
  50.  
  51. #ifdef LATTICE_50
  52. #include <proto/exec.h>
  53. #include <proto/dos.h>
  54.  
  55. /* these were unimplemented until dos 36.147 */
  56.  
  57. BOOL ParsePatternNoCase( UBYTE *pat, UBYTE *buf, long buflen );
  58. BOOL MatchPatternNoCase( UBYTE *pat, UBYTE *str );
  59.  
  60. #pragma libcall DOSBase ParsePatternNoCase 3C6 32103
  61. #pragma libcall DOSBase MatchPatternNoCase 3CC 2102
  62. #endif
  63.  
  64. #ifdef MSDOS
  65. #define NAMESIZE 14
  66. #endif
  67.  
  68. void AddToNameList(struct NameList **, char *);
  69. void FreeNameList(struct NameList **);
  70. void MakeExt(char *, char *, char *);
  71. void MakeBasename(char *, char *);
  72. void strlcpy(register char *, register char *);
  73. void mputn(FILE *, char *, unsigned);
  74. int iswild(char *);
  75. void scandir(char *, struct NameList **);
  76. int exists(char *);
  77. int tolower(int);
  78. #ifdef AMIGA
  79. int match(char *, char *);
  80. char *parsepath(char *, char **);
  81. void cleanup(UBYTE *, USHORT, struct FileInfoBlock *, BPTR);
  82. #endif
  83.  
  84. struct NameList {
  85.   struct NameList *Next;
  86.   char Name[NAMESIZE];
  87. };
  88.  
  89. /* Name in Liste einfuegen */
  90. void AddToNameList(list,name)
  91. struct NameList **list;
  92. char *name;
  93. {
  94.   register struct NameList *ptr;
  95.  
  96.   if ((ptr=(struct NameList *)malloc(sizeof(struct NameList)))==0L) {
  97.     printf("not enough memory for malloc()\n");
  98.     exit(-1);
  99.   };
  100.   strncpy(ptr->Name,name,NAMESIZE);
  101.   ptr->Next = *list;
  102.   *list=ptr;
  103. }
  104.  
  105. /* Liste komplett freigeben */
  106. void FreeNameList(ptr)
  107. struct NameList **ptr;
  108. {
  109.   register struct NameList *workptr, *nextptr;
  110.  
  111.   workptr = *ptr;
  112.   while (workptr!=0L) {
  113.     nextptr=workptr->Next;
  114.     free(workptr);
  115.     workptr=nextptr;
  116.   };
  117.   *ptr=0L;
  118. }
  119.  
  120. /* (Neue) Extension an Filenamen anhängen */
  121. void MakeExt(NewName,filename,newext)
  122. register char *NewName;
  123. register char *filename;
  124. register char *newext;
  125. {
  126.   while ((*filename != '.') && (*filename != '\0')) {
  127.     *NewName++ = *filename++;
  128.   };
  129.   *NewName++ = '.';
  130.   while (*newext != '\0') {
  131.     *NewName++ = *newext++;
  132.   };
  133.   *NewName = '\0';
  134. }
  135.  
  136. /* Base-Name aus Dateinamen erzeugen, Parameter-Reihenfolge wie bei strcpy */
  137. void MakeBasename(Basename,Filename)
  138. register char *Basename;
  139. register char *Filename;
  140. {
  141.   while ((*Filename != '.') && (*Filename != '\0')) {
  142.     *Basename++ = *Filename++;
  143.   };
  144.   *Basename = '\0';
  145. }
  146.  
  147. /* String-Kopieren mit Umwandlung in lowercase */
  148. void strlcpy(register char *dest, register char *src)
  149. {
  150.   char c;
  151.  
  152.   while (*src != '\0') {
  153.     c = tolower(*src);
  154.     *dest++ = c;
  155.     src++;
  156.   };
  157.   *dest = '\0';
  158. }
  159.  
  160. #ifdef TEST
  161. void mputn(FILE * fp, char *str, unsigned max)
  162. {
  163.   unsigned i;
  164.  
  165.   i=max;
  166.   while ((i != 0) && (*str != '\0')) {
  167.     putc(*str,fp);
  168.     str++;
  169.     i--;
  170.   };
  171.   while (i != 0) {
  172.     putc(' ',fp);
  173.     i--;
  174.   };
  175. }
  176. #endif
  177.  
  178. /*--------SYSTEM DEPENDENT FUNCTION SECTION---------------------*/
  179. /* Prüfen, ob Name ein Wildcard-Muster enthält */
  180. int iswild(char *Pattern)
  181. {
  182.   int rc;
  183. #ifdef MSDOS
  184.   rc = (strchr(Pattern,'*') != 0L) || (strchr(Pattern,'?') != 0L);
  185. #endif
  186. #ifdef AMIGA
  187.   rc = 0;
  188.   while (*Pattern != '\0') {
  189.     switch(*Pattern) {
  190.       case '#':
  191.       case '?':
  192.       case '|':
  193.       case '(':
  194.       case ')':
  195.       case '[':
  196.       case ']':
  197.         rc = ~0;
  198.         break;
  199.     };
  200.     Pattern++;
  201.   };
  202. #endif
  203.   return rc;
  204. }
  205.  
  206. #ifdef AMIGA
  207. /* Pfadname zerlegen */
  208. char *parsepath(char *pathname, char **Pattern)
  209. {
  210.   register char *s,*d, *Path;
  211.  
  212.   s = strrchr(pathname,'/');
  213.   if (s == 0L) s = strrchr(pathname,':');
  214.   if (s != 0L) {
  215.     Path = d = malloc((unsigned)(s-pathname)+1);
  216.     while ((*pathname != '\0') && (pathname != s)) {
  217.       *d++ = *pathname++;
  218.     };
  219.     if (pathname == s) *d++ = *pathname;
  220.     *d = '\0';
  221.     s++;
  222.     *Pattern = strdup(s);
  223.   }
  224.   else {
  225.     Path = malloc(1);
  226.     *Path = '\0';
  227.     *Pattern = strdup(pathname);
  228.   };
  229.   { register unsigned char *s;
  230.   s = *Pattern;
  231.   while (*s != '\0') {
  232.     if ((*s >='a') && (*s <= 'z')) *s &= 0xDF;
  233.     s++;
  234.   };
  235.   }
  236.   return Path;
  237. }
  238.  
  239. void cleanup(UBYTE *PBuf, USHORT PBufLen, struct FileInfoBlock *FIB, BPTR DirLock)
  240. {
  241.   if (PBuf != 0L) FreeMem(PBuf, PBufLen);
  242.   if (FIB != 0L) FreeMem(FIB, sizeof(struct FileInfoBlock));
  243.   if (DirLock != 0L) UnLock(DirLock);
  244. }
  245. #endif
  246.  
  247. /* scan the directory given by pathname */
  248. void scandir(char *pathname, struct NameList **rootptr)
  249. {
  250. #ifdef MSDOS
  251.   int attrmask,rc;
  252.   struct NameList *work;
  253.   struct ffblk *dirp;
  254.   if ((dirp = malloc(sizeof(struct ffblk))) == NULL) {
  255.     fputs("Cannot allocate memory\n",stderr);
  256.     exit(0);
  257.   };
  258.   memset(dirp,0,sizeof(struct ffblk));
  259.  
  260.   attrmask = FA_DIREC | FA_ARCH | FA_RDONLY;
  261.   rc = findfirst(pathname, dirp, attrmask);
  262.   while (rc == 0) {
  263.     if (!(dirp->ff_attrib & FA_DIREC)) { /* normale Datei */
  264.       AddToNameList(rootptr,dirp->ff_name);
  265.     }; /* if ... FA_DIREC */
  266.     rc = findnext(dirp);
  267.   }; /* while */
  268.   free(dirp);
  269. #endif
  270. #ifdef AMIGA
  271.   UBYTE *PBuf = 0L;
  272.   USHORT PBufLen = 0;
  273.   BPTR DirLock = 0L;
  274.   ULONG success;
  275.   struct FileInfoBlock *FIB = 0L;
  276.   char *Pattern, *Path;
  277.   char fullname[128];
  278.  
  279.   Path = parsepath(pathname,&Pattern);
  280.  
  281.   PBufLen = (strlen(Pattern)+1)*3;
  282.   if ((PBuf = AllocMem((ULONG)PBufLen, MEMF_PUBLIC | MEMF_CLEAR)) == 0L) {
  283.     fputs("not enough memory\n",stderr);
  284.     exit(0);
  285.   };
  286.  
  287.   if (DosVersion >= 37) {
  288.     int IsWild = ParsePatternNoCase(Pattern,PBuf,(ULONG)PBufLen);
  289.     if (IsWild==-1) {
  290.       fputs("Pech gehabt ! Puffer für ParsePattern war zu klein.\n",stderr);
  291.       cleanup(PBuf,PBufLen,FIB,DirLock);
  292.       exit(0);
  293.     };
  294.   };
  295.  
  296.   if ((FIB = AllocMem((ULONG)sizeof(struct FileInfoBlock),MEMF_PUBLIC)) == NULL) {
  297.     fputs("not enough memory\n",stderr);
  298.     cleanup(PBuf,PBufLen,FIB,DirLock);
  299.     exit(0);
  300.   };
  301.  
  302.   DirLock = Lock(Path,SHARED_LOCK);
  303.   if (DirLock==NULL) {
  304.     fputs("Cannot get a lock for ",stderr);
  305.     fputs(pathname,stderr);
  306.     putc('\n',stderr);
  307.     cleanup(PBuf,PBufLen,FIB,DirLock);
  308.     exit(0);
  309.   };
  310.  
  311.   if (success = Examine(DirLock, FIB)) {
  312.     if (DosVersion >= 37) {
  313.       while (success) {
  314.         if (FIB->fib_DirEntryType < 0 ) {
  315.           if (MatchPatternNoCase(PBuf,FIB->fib_FileName)) {
  316. #ifdef TEST
  317.             mputn(stderr,FIB->fib_FileName,40);
  318.             putc('\r',stderr);
  319.             fflush(stderr);
  320. #endif
  321.             if (*Path != '\0') {
  322.               strncpy(fullname,Path,128);
  323.               strcat(fullname,FIB->fib_FileName);
  324.               AddToNameList(rootptr, fullname);
  325.             }
  326.             else AddToNameList(rootptr, FIB->fib_FileName);
  327.           };
  328.         }; /* if */
  329.         success = ExNext(DirLock, FIB);
  330.       }; /* while */
  331.     }
  332.     else {
  333.       while (success) {
  334.         if (FIB->fib_DirEntryType < 0 ) {
  335.           if (match(FIB->fib_FileName,Pattern)) {
  336. #ifdef TEST
  337.             mputn(stderr,FIB->fib_FileName,40);
  338.             putc('\r',stderr);
  339.             fflush(stderr);
  340. #endif
  341.             if (*Path != '\0') {
  342.               strncpy(fullname,Path,128);
  343.               strcat(fullname,FIB->fib_FileName);
  344.               AddToNameList(rootptr, fullname);
  345.             }
  346.             else AddToNameList(rootptr, FIB->fib_FileName);
  347.           };
  348.         }; /* if */
  349.         success = ExNext(DirLock, FIB);
  350.       }; /* while */
  351.     }
  352.   }
  353.   else {
  354.     fputs("Cannot examine directory ",stderr);
  355.     fputs(pathname,stderr);
  356.     putc('\n',stderr);
  357.     cleanup(PBuf,PBufLen,FIB,DirLock);
  358.     exit(20);
  359.   };
  360.  
  361.   UnLock(DirLock);
  362.   DirLock = 0L;
  363.  
  364.   FreeMem(FIB, (ULONG)sizeof(struct FileInfoBlock));
  365.   FIB = 0L;
  366.   FreeMem(PBuf, (ULONG)PBufLen);
  367.   PBuf = 0L;
  368. #endif
  369. }
  370.  
  371. /* Test whether a file exists */
  372. int exists(char *filename)
  373. {
  374.   int rc;
  375.   rc = access(filename,0);
  376.   return(rc==0);
  377. }
  378.  
  379. /*---END OF SYSTEM DEPENDENT FUNCTION SECTION---------------------*/
  380.  
  381. /* Hauptprogramm */
  382. main(argc,argv)
  383. int argc;
  384. char *argv[];
  385. {
  386.   static char pathname[20][128];
  387.   int i;
  388.   static char objname[128];
  389.   static char objext[12];
  390.   static char mainfile[13];
  391.   static char filename[13];
  392.   static char libfile[13];
  393.  
  394.   int anzpn = 0;
  395.   struct NameList *Root, *work;
  396.   char allflag, librarian;
  397.   int l,lf;
  398.  
  399.   char mainflag, lowercase = 0;
  400.   register char *argstr;
  401.   char *allstr;
  402.  
  403.   if (argc<=1) {
  404.     fputs("Usage: ",stderr);
  405.     fputs(argv[0],stderr);
  406.     fputs(" [-o<objext>] [-m<mainfile>] [-aL] [-r<libfile>] fpattern\n",stderr);
  407.   };
  408.   strncpy(objext,OBJEXT,12);
  409.   strncpy(mainfile,"",13);
  410.   allflag = 0; librarian = 0;
  411.  
  412.   for (i=1; i<argc; i++) {
  413.     argstr = argv[i];
  414.     if (*argstr == '-') {
  415.       argstr++;
  416.       switch(*argstr) {
  417.         case 'o': strncpy(objext,++argstr,12); break;
  418.         case 'm': strncpy(mainfile,++argstr,12); break;
  419.         case 'a': allflag = !0; break;
  420.         case 'r': librarian = !0; strncpy(libfile,++argstr,12); break;
  421.         case 'L': 
  422.           lowercase = !0;
  423.           strlcpy(objext,objext);
  424.           break;
  425.         default: 
  426.           fputs("Unknown option -",stderr);
  427.           putc(*argstr,stderr);
  428.           fputs(" ignored\n",stderr);
  429.       };
  430.     }
  431.     else strncpy(pathname[anzpn++],argv[i],128);
  432.   };
  433.  
  434.   Root = 0L;
  435.   for (i=0; i<anzpn; i++) { 
  436.     if (iswild(pathname[i])) scandir(pathname[i],&Root);
  437.     else {
  438.       if (exists(pathname[i])) AddToNameList(&Root,pathname[i]);
  439.     }
  440.   }; /* for */
  441.  
  442.   if (allflag) {
  443.     putchar('\n');
  444.     if ((allstr = malloc(256)) == 0L) {
  445.       fputs("not enough memory for malloc\n",stderr);
  446.       exit(0);
  447.     };
  448.     strncpy(allstr,"SOURCES = ",256);
  449.     for (work=Root; work != 0L; work=work->Next) {
  450.       if (lowercase) strlcpy(filename,work->Name);
  451.       else strcpy(filename,work->Name);
  452.       l = strlen(allstr); 
  453.       lf = strlen(filename);
  454.       if (l+lf<78) {
  455.         strcat(allstr,filename);
  456.         l += lf;
  457.       }
  458.       else {
  459.         fputs(allstr,stdout);
  460.         fputs(" \\\n",stdout);
  461.         strncpy(allstr,"          ",256);
  462.         strcat(allstr,filename);
  463.       };        
  464.       if (work->Next != 0L) {
  465.         strcat(allstr," ");
  466.       }
  467.       else {
  468.         if (strcmp(allstr,"          ")!=0) {
  469.           fputs(allstr,stdout);
  470.           putchar('\n');
  471.         };
  472.         putchar('\n');
  473.       };
  474.     };
  475.     strncpy(allstr,"OBJ = ",256);
  476.     for (work=Root; work != 0L; work=work->Next) {
  477.       if (lowercase) strlcpy(filename,work->Name);
  478.       else strcpy(filename,work->Name);
  479.       if (lowercase) MakeExt(objname,filename,OBJEXTLC);
  480.       else MakeExt(objname,filename,OBJEXT);
  481.       l = strlen(allstr); 
  482.       lf = strlen(objname);
  483.       if (l+lf<78) {
  484.         strcat(allstr,objname);
  485.         l += lf;
  486.       }
  487.       else {
  488.         fputs(allstr,stdout);
  489.         fputs(" \\\n",stdout);
  490.         strncpy(allstr,"      ",256);
  491.         strcat(allstr,objname);
  492.       };        
  493.       if (work->Next != 0L) {
  494.         strcat(allstr," ");
  495.       }
  496.       else {
  497.         if (strcmp(allstr,"      ")!=0) {
  498.           fputs(allstr,stdout);
  499.           putchar('\n');
  500.         };
  501.         putchar('\n');
  502.       };
  503.     };
  504.     free(allstr); 
  505.   };
  506.   for (work=Root; work != 0L; work=work->Next) {
  507.     if (lowercase) strlcpy(filename,work->Name);
  508.     else strcpy(filename,work->Name);
  509.     if (stricmp(filename,mainfile)==0) mainflag = !0;
  510.     else mainflag = 0;
  511.     if (mainflag) {
  512. #ifdef MSDOS
  513.       if (lowercase) MakeExt(objname,filename,"exe");
  514.       else MakeExt(objname,filename,"EXE");
  515. #else
  516.       MakeBasename(objname,filename);
  517. #endif
  518.     }
  519.     else {
  520.       if (lowercase) MakeExt(objname,filename,objext);
  521.       else MakeExt(objname,filename,objext);
  522.     };
  523.     fputs(objname,stdout);
  524.     putchar(':');
  525.     putchar(' ');
  526.     fputs(filename,stdout);
  527.     putchar('\n');
  528.     fputs("\t$(CC) $(CFLAGS) ",stdout);
  529. #ifndef AMIGA
  530.     if (!mainflag) fputs("-c ",stdout);
  531. #endif
  532.     fputs(filename,stdout);
  533.     putchar('\n');
  534.     if (work->Next != 0L) putchar('\n'); 
  535.   }
  536.   if (librarian) {
  537.     putchar('\n');
  538.     if ((allstr = malloc(256)) == 0L) {
  539.       fputs("not enough memory for malloc\n",stderr);
  540.       exit(0);
  541.     };
  542.     if (libfile[0] == '\0') strcpy(libfile,"libtest.lib");
  543.     fputs(libfile,stdout);
  544.     fputs(": $(OBJ)\n",stdout);
  545. #ifdef MSDOS
  546.     strncpy(allstr,"\ttlib ",256);
  547. #endif
  548. #ifdef AMIGA
  549.     strncpy(allstr,"\toml -v ",256);
  550. #endif
  551.     strcat(allstr,libfile);
  552. #ifdef AMIGA
  553.     strcat(allstr," r");
  554. #endif
  555.     for (work=Root; work != 0L; work=work->Next) {
  556.       if (lowercase) strlcpy(filename,work->Name);
  557.       else strcpy(filename,work->Name);
  558.       if (lowercase) MakeExt(objname,filename,OBJEXTLC);
  559.       else MakeExt(objname,filename,OBJEXT);
  560.       l = strlen(allstr); 
  561.       lf = strlen(objname);
  562.       if (l+lf<100) {
  563. #ifdef MSDOS
  564.         strcat(allstr," +-");
  565.         strcat(allstr,objname);
  566. #endif
  567. #ifdef AMIGA
  568.         strcat(allstr," ");
  569.         strcat(allstr,objname);
  570. #endif
  571.         l += lf;
  572.       }
  573.       else {
  574.         fputs(allstr,stdout);
  575.         putchar('\n');
  576. #ifdef MSDOS
  577.         strncpy(allstr,"\ttlib ",256);
  578.         strcat(allstr,libfile);
  579.         strcat(allstr," -+");
  580.         strcat(allstr,objname);
  581. #endif
  582. #ifdef AMIGA
  583.         strncpy(allstr,"\toml -v ",256);
  584.         strcat(allstr,libfile);
  585.         strcat(allstr," r ");
  586.         strcat(allstr,objname);
  587. #endif
  588.       };        
  589.       if (work->Next == 0L) {
  590.         fputs(allstr,stdout);
  591.         putchar('\n');
  592.         putchar('\n');
  593.       };
  594.     };
  595.   };
  596. }
  597.