home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / comm / cyberpager-1.5.lha / CyberPager / source / library / aliases.c < prev    next >
C/C++ Source or Header  |  1994-02-07  |  4KB  |  168 lines

  1.  /*
  2.   * read the aliases file looking for a particular alias.  returns a struct
  3.   * describing that alias if found or NULL if not found (or an error occurs).
  4.   * format of lines in the alias file are:
  5.   * 
  6.   * ; indicates start of a comment
  7.   * 
  8.   * <alias> <service name> <pin> <max message> <max page>
  9.   */
  10.  
  11. #include "memory.h"
  12.  
  13. #define TEMPLATE "NAME/A,SERVICE/A,PIN/A,MAXMSG/N/A,MAXPAGE/N/A"
  14.   enum templateArgs {
  15.       ARG_NAME,
  16.       ARG_SERVICE,
  17.       ARG_PIN,
  18.       ARG_MAXMSG,
  19.       ARG_MAXPAGE,
  20.       ARG_sizeof
  21.   };
  22.  
  23. #define INPUT_BUFFER_SIZE (1024)
  24.  
  25. PagerAlias_t *__saveds __asm FindPagerAlias(register __a0 PagerHandle_t * ph, register __a1 STRPTR aliasName)
  26. {
  27.     PrivatePagerAlias_t *pals;
  28.     PagerAlias_t *als;
  29.     ULONG line = 0;
  30.     STRPTR inputBuffer, ptr;
  31.     struct RDArgs *ArgsPtr, *MyArgs;
  32.     char *ArgArray[ARG_sizeof];
  33.     BOOL leave;
  34.     BPTR fh;
  35.  
  36.     ObtainSemaphore(&ph->ph_Sema);
  37.  
  38.     /* first look to see if we have already read this alias in */
  39.  
  40.     for (pals = (PrivatePagerAlias_t *) ph->ph_AliasList.mlh_Head;
  41.          pals->pals_Node.mln_Succ;
  42.          pals = (PrivatePagerAlias_t *) pals->pals_Node.mln_Succ)
  43.         if (stricmp(aliasName, pals->pals_Alias.als_Name) == 0) {
  44.             pals->pals_UseCount++;
  45.             ReleaseSemaphore(&ph->ph_Sema);
  46.             return &pals->pals_Alias;
  47.         }
  48.  
  49.     /*
  50.      * oh, well.  need to read the aliases file to find information on
  51.      * this aliases
  52.      */
  53.  
  54.     als = NULL;
  55.  
  56.     if (inputBuffer = MyAllocVec(INPUT_BUFFER_SIZE)) {
  57.         /* try and open the aliases file */
  58.  
  59.         if (fh = Open("pager:aliases", MODE_OLDFILE)) {
  60.             /* loop reading lines looking for the alias desired */
  61.  
  62.             leave = FALSE;
  63.  
  64.             while (leave == FALSE && FGets(fh, inputBuffer, INPUT_BUFFER_SIZE - 1)) {
  65.                 line++;
  66.  
  67.                 /*
  68.                  * check to make sure the line isn't all
  69.                  * blank
  70.                  */
  71.  
  72.                 ptr = inputBuffer;
  73.                 while (isspace(*ptr))
  74.                     ptr++;
  75.  
  76.                 if (!*ptr)
  77.                     continue;
  78.  
  79.                 /*
  80.                  * check for comment character at beginning
  81.                  * of line
  82.                  */
  83.  
  84.                 if (*ptr == ';')
  85.                     continue;
  86.  
  87.                 /* setup to call ReadArgs() to parse the line */
  88.  
  89.                 if (MyArgs = (struct RDArgs *)AllocDosObject(DOS_RDARGS, TAG_DONE)) {
  90.                     MyArgs->RDA_Flags |= RDAF_NOPROMPT;
  91.                     MyArgs->RDA_Source.CS_Buffer = inputBuffer;
  92.                     MyArgs->RDA_Source.CS_Length = strlen(inputBuffer);
  93.                     MyArgs->RDA_Source.CS_CurChr = 0L;
  94.  
  95.                     memset((char *)ArgArray, 0, sizeof(ArgArray));
  96.                     if (ArgsPtr = ReadArgs(TEMPLATE, (LONG *)&ArgArray, MyArgs)) {
  97.                         if (stricmp(aliasName, ArgArray[ARG_NAME]) == 0) {
  98.  
  99.                             /*
  100.                              * we've found the
  101.                              * alias so allocate
  102.                              * and fill in a
  103.                              * alias structure
  104.                              */
  105.                             leave = TRUE;
  106.  
  107.                             if (pals = (PrivatePagerAlias_t *) MyAllocVec(sizeof(PrivatePagerAlias_t) + strlen(ArgArray[ARG_NAME]) + strlen(ArgArray[ARG_SERVICE]) + strlen(ArgArray[ARG_PIN]) + 3)) {
  108.                                 pals->pals_Handle = ph;
  109.                                 pals->pals_UseCount = 1;
  110.                                 AddTail((struct List *)&ph->ph_AliasList, (struct Node *)&pals->pals_Node);
  111.  
  112.                                 als = &pals->pals_Alias;
  113.  
  114.                                 als->als_Name = (STRPTR)&als[1];
  115.                                 als->als_Service = als->als_Name + strlen(ArgArray[ARG_NAME]) + 1;
  116.                                 als->als_PIN = als->als_Service + strlen(ArgArray[ARG_SERVICE]) + 1;
  117.  
  118.                                 strcpy(als->als_Name, ArgArray[ARG_NAME]);
  119.                                 strcpy(als->als_Service, ArgArray[ARG_SERVICE]);
  120.                                 strcpy(als->als_PIN, ArgArray[ARG_PIN]);
  121.                                 als->als_MaxMessageLen = *((LONG *)ArgArray[ARG_MAXMSG]);
  122.                                 als->als_MaxPageLen = *((LONG *)ArgArray[ARG_MAXPAGE]);
  123.                             }
  124.                         }
  125.  
  126.                         FreeArgs(ArgsPtr);
  127.                     }
  128.                     else {
  129.                         Fault(IoErr(), NULL, inputBuffer, INPUT_BUFFER_SIZE - 1);
  130.                         ULog(ph, -1, "Error in aliases file - line %ld: %s", line, inputBuffer);
  131.                         leave = TRUE;
  132.                     }
  133.  
  134.                     FreeDosObject(DOS_RDARGS, MyArgs);
  135.                 }
  136.             }
  137.  
  138.             Close(fh);
  139.         }
  140.         else
  141.             ULog(ph, -1, "Couldn't open aliases file");
  142.  
  143.         MyFreeVec(inputBuffer);
  144.     }
  145.  
  146.     ReleaseSemaphore(&ph->ph_Sema);
  147.  
  148.     return als;
  149. }
  150.  
  151. void __saveds __asm FreePagerAlias(register __a0 PagerAlias_t * als)
  152. {
  153.     PrivatePagerAlias_t *pals;
  154.  
  155.     if (als) {
  156.         pals = (PrivatePagerAlias_t *) ((size_t) als - offsetof(PrivatePagerAlias_t, pals_Alias));
  157.  
  158.         ObtainSemaphore(&pals->pals_Handle->ph_Sema);
  159.  
  160.         if (--pals->pals_UseCount == 0) {
  161.             Remove((struct Node *)&pals->pals_Node);
  162.             MyFreeVec(pals);
  163.         }
  164.  
  165.         ReleaseSemaphore(&pals->pals_Handle->ph_Sema);
  166.     }
  167. }
  168.