home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 595.lha / MountStuff / EStartup / ETemplate.c < prev    next >
C/C++ Source or Header  |  1990-10-17  |  6KB  |  284 lines

  1.  
  2. #include <exec/types.h>
  3. #include <ctype.h>
  4. #include "hh:template.h"
  5.  
  6. /******** Definition of KeyArgV(TEMPLATE) */
  7.  
  8. /* If "TEMPLATE" EXISTS {
  9.       Return <TEMPLATE>   ( EMPTY IF NOT FILLED )
  10.    Else 
  11.       Return NULL
  12.    EndIf
  13. */
  14.  
  15. /******** Definition of DefaultKeyArgV(TEMPLATE,DEFAULT) */
  16.  
  17. /* If "TEMPLATE" EXISTS
  18.       If "TEMPLATE" NON EMPTY
  19.          Return <TEMPLATE>
  20.       Else
  21.          Return KeyArgV(DEFAULT)  ( SEE DEFINITION ABOVE )
  22.       EndIf
  23.    Else
  24.       Return NULL
  25.    EndIf
  26. */
  27.  
  28.  
  29. typedef struct PLATEARGV {
  30.        TEXT *pa_ArgV;
  31.        BOOL pa_Used;
  32. } PLATEARGV;
  33.  
  34. static PLATEARGV PlateArgV[50]; /* 50 Arguments Are A Lot */
  35. static LONG InitialCount, FinalCount = 0; /* ReleaseTemplate() Needs This */
  36.  
  37.  static BOOL
  38. StrULCmp(str1,str2)
  39.  TEXT *str1, *str2;
  40. {
  41.  BOOL same;
  42.  UBYTE byte;
  43.  
  44.  do {
  45.     byte = ((!!isalpha(*str1))) | ((!!isalpha(*str2))<<1) |
  46.            ((!!isupper(*str1))<<2) | ((!!isupper(*str2))<<3);
  47.     if (!(byte&0x03)) {
  48.        same = *str1==*str2;
  49.     } else if ((byte&0x03)!=0x03) {
  50.        same = FALSE;
  51.     } else {
  52.        switch (byte) {
  53.          case 0x03 :
  54.          case 0x0f :
  55.               same = *str1==*str2;
  56.               break;
  57.          case 0x07 :
  58.               same = *str1==_toupper(*str2);
  59.               break;
  60.          case 0x0b :
  61.               same = _toupper(*str1)==*str2;
  62.               break;
  63.        }
  64.     }
  65.     str1++;
  66.     str2++;
  67.  } while (same && *str1);
  68.  
  69.  return (!(same && !*str2)); /* Returning FALSE when same */
  70. }
  71.  
  72.  TEXT *
  73. KeyArgV(key)
  74.  TEXT *key;
  75. {
  76.  LONG i = 0;
  77.  
  78.  while (!(Template[i].tp_Flags&EOTP)) {
  79.        if (!StrULCmp(Template[i].tp_Key,key)) {
  80.           return(Template[i].tp_ArgV);
  81.        } else {
  82.           i++;
  83.        }
  84.  }
  85.  
  86.  return(NULL);
  87. }
  88.  
  89.  
  90.  TEXT *
  91. DefaultKeyArgV(key,defkey)
  92.  TEXT *key, *defkey;
  93. {
  94.  TEXT *argv;
  95.  
  96.  return((argv = KeyArgV(key)) ? ((*argv)?argv:KeyArgV(defkey)) : NULL);
  97. }
  98.  
  99.  
  100.  static VOID
  101. PressTemplate()
  102. {
  103.  LONG i = 0;
  104.  BOOL eop = Template[0].tp_Flags&EOTP;
  105.  
  106.  while (!eop) {
  107.        printf("%s",Template[i].tp_Key);
  108.        if (Template[i].tp_Flags&A_FLAG) {
  109.           printf("/a");
  110.        }
  111.        if (Template[i].tp_Flags&K_FLAG) {
  112.           printf("/k");
  113.        }
  114.        if (Template[i].tp_Flags&S_FLAG) {
  115.           printf("/s");
  116.        }
  117.        if (!(eop=Template[++i].tp_Flags&EOTP)) {
  118.           printf(",");
  119.        }
  120.  }
  121.  
  122. }
  123.  
  124.  static LONG
  125. GetMoreArgVs(pa)
  126.  PLATEARGV pa[];
  127. {
  128.  LONG i =0;
  129.  BOOL more = FALSE;
  130.  
  131.  do {
  132.     PressTemplate();
  133.     printf(": ");
  134.     do {
  135.        pa[i].pa_ArgV = (TEXT *)AllocMem(50);
  136.        pa[i].pa_Used = FALSE;
  137.        scanf("%s",pa[i++].pa_ArgV);
  138.     } while (getchar()==' ');
  139.     if ((more = !StrULCmp(pa[i-1].pa_ArgV,"?"))) {
  140.        FreeMem(pa[--i].pa_ArgV,50);
  141.     }
  142.  } while (more);
  143.  
  144.  return(i);
  145. }
  146.  
  147.  
  148.  VOID
  149. ReleaseTemplate()
  150. {
  151.  while (InitialCount<FinalCount) {
  152.        FreeMem(PlateArgV[InitialCount++].pa_ArgV,50);
  153.  }
  154. }
  155.  
  156.  
  157.  BOOL
  158. FetchTemplate(ac,av)
  159.  LONG ac;
  160.  TEXT *av[];
  161. {
  162.  LONG mc = ac -1;
  163.  LONG ArgVs, Plates = 0;
  164.  LONG avn, pln;
  165.  BOOL NoError, Match;
  166.  
  167.  pln = 0;
  168.  while (!(Template[pln].tp_Flags&EOTP)) {
  169.        if (Template[pln].tp_UserVar) {
  170.           *Template[pln].tp_UserVar = "";
  171.        }
  172.        Template[pln].tp_ArgV = ""; /* Make Every Entry Empty */ 
  173.        pln++;
  174.  }
  175.  
  176.  while (ac>1) {
  177.        PlateArgV[ac -2].pa_ArgV = av[ac -1];
  178.        PlateArgV[ac -2].pa_Used = FALSE;
  179.        ac--;
  180.  }
  181.  
  182.  InitialCount = mc;
  183.  
  184.  if (mc && !StrULCmp(PlateArgV[mc -1].pa_ArgV,"?")) {
  185.     InitialCount--;
  186.     mc += GetMoreArgVs(&PlateArgV[mc -1]) -1;
  187.  }
  188.  
  189.  FinalCount = mc;
  190.  
  191.  while (!(Template[Plates].tp_Flags&EOTP)) {
  192.        Plates++;
  193.  }
  194.  
  195.  ArgVs = FinalCount;
  196.  NoError = TRUE;
  197.  
  198.  while (Plates && ArgVs && NoError) {
  199.        /* Get First Free ArgV */
  200.        avn = 0;
  201.        while (PlateArgV[avn].pa_Used) {
  202.              avn++;
  203.        }
  204.  
  205.        /* Seek Into Plate For Identical KeyWord */
  206.        pln = 0;
  207.        while (!(Template[pln].tp_Flags&EOTP) &&
  208.               (Template[pln].tp_Flags&FETCHED ||
  209.               StrULCmp(Template[pln].tp_Key,PlateArgV[avn].pa_ArgV))) {
  210.              pln++;
  211.        }
  212.  
  213.        /* If Not Found, Try First Not /K/S */
  214.        if (Template[pln].tp_Flags&EOTP) {
  215.           Match = FALSE;
  216.           pln = 0;
  217.           while (!(Template[pln].tp_Flags&EOTP) &&
  218.                  Template[pln].tp_Flags&(K_FLAG|S_FLAG|FETCHED)) {
  219.                 pln++;
  220.           }
  221.        } else {
  222.           Match = TRUE;
  223.        }
  224.  
  225.        /* If Found, Fill It, Else Error */
  226.        if (Template[pln].tp_Flags&EOTP) {
  227.           NoError = FALSE;
  228.        } else {
  229.           Template[pln].tp_Flags |= FETCHED;
  230.           PlateArgV[avn].pa_Used = TRUE;
  231.           ArgVs--;
  232.           Plates--;
  233.  
  234.           if (Template[pln].tp_Flags&S_FLAG || !Match) {
  235.  
  236.              Template[pln].tp_ArgV = PlateArgV[avn].pa_ArgV;
  237.              if (Template[pln].tp_UserVar) {
  238.                 *(Template[pln].tp_UserVar) = PlateArgV[avn].pa_ArgV;
  239.              }
  240.  
  241.           } else { /* KeyWord, So Use Next ArgV */
  242.  
  243.              if (ArgVs) {
  244.                 PlateArgV[++avn].pa_Used = TRUE;
  245.                 ArgVs--;
  246.          
  247.                 Template[pln].tp_ArgV = PlateArgV[avn].pa_ArgV;
  248.                 if (Template[pln].tp_UserVar) {
  249.                    *(Template[pln].tp_UserVar) = PlateArgV[avn].pa_ArgV;
  250.                 }
  251.  
  252.              } else {
  253.                 NoError = FALSE;
  254.              }
  255.           }
  256.   
  257.        }
  258.  }
  259.  
  260.  if (ArgVs) {
  261.     NoError = FALSE;
  262.  } else if (Plates) {
  263.     pln = 0;
  264.     while (NoError && !(Template[pln].tp_Flags&EOTP)) {
  265.           if (!(Template[pln].tp_Flags&FETCHED) &&
  266.               Template[pln].tp_Flags&A_FLAG) {
  267.              NoError = FALSE;
  268.           } else {
  269.              pln++;
  270.           }
  271.     }
  272.  }
  273.  
  274.  if (!NoError) {
  275.     printf("Args no good for key \"");
  276.     PressTemplate();
  277.     printf("\"\n");
  278.  }
  279.  
  280.  return(NoError);
  281. }
  282.  
  283.  
  284.