home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff284.lzh / ARPTools / src / Asn.c next >
C/C++ Source or Header  |  1989-11-27  |  5KB  |  207 lines

  1. /*
  2.           Asn - Perform logical device and environment var assigns
  3.                 from a list file.
  4.  
  5.           Original effort by Fabio Rossetti.
  6.  
  7.           (c) 1989 by Fabio Rossetti
  8.  
  9.       To compile under Lattice C v5.0x use:
  10.  
  11.         lc -O -v -cus asn
  12.         blink lib:cres.o asn.o to asn lib lib:a.lib lib:lc.lib sd nd
  13.  
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/libraries.h>
  19. #include <libraries/dos.h>
  20. #include <libraries/dosextens.h>
  21. #include <libraries/arpbase.h>
  22. #include <arpfunctions.h>
  23. #include <proto/exec.h>
  24.         
  25.  
  26. /* assign list file buffer */
  27. #define BUFSIZE 4096
  28. #define DEFAULT_FILE "DEVS:AssignList"
  29.  
  30. /* flag values */
  31. #define SPLIT_COMMENT 3        
  32. #define SPLIT_NAME 2
  33. #define OKAY 1
  34. #define ASSIGNEM 1
  35. #define DONTASSIGNEM 0
  36.  
  37. /* token identifiers */
  38. #define NAME 0
  39. #define VALUE 1
  40.  
  41. struct ArpBase *ArpBase;
  42. BPTR fh,FH;
  43. struct Process *Pr;
  44.  
  45. TEXT Token[2][256]; /* buffers to hold token names (devices, var names & values) */
  46. TEXT Env[108] = "ENV:"; /* buffer to hold filename of 1.3 ENV: variable */
  47.  
  48. /* Trick to keep code down to size */
  49. VOID MemCleanup()
  50. {
  51. }
  52. /* as above */
  53. VOID _main(Line)
  54. STRPTR Line;
  55.  
  56. {
  57.     
  58.     STRPTR argv;
  59.     LONG retcod;
  60.  
  61.     REGISTER TEXT *Fil,*Buf,*i,*EndOfBuf;
  62.     REGISTER LONG count,Pos=0;
  63.     struct {
  64.         unsigned StOfBuf : 2;
  65.         unsigned Do : 1;
  66.         unsigned TokenType : 1;
  67.         } St;
  68.  
  69.     St.StOfBuf=OKAY;
  70.     
  71.     Pr = (struct Process*)FindTask(NULL);
  72.     
  73.     if(!(ArpBase = (struct ArpBase*)OpenLibrary(ArpName,ArpVersion))){
  74.             Pr->pr_Result2=ERROR_INVALID_RESIDENT_LIBRARY;
  75.             exit(RETURN_FAIL);
  76.             }
  77.     
  78.     /* parse command line */
  79.  
  80.         argv =  NULL;
  81.  
  82.     while(*Line > ' ')
  83.         ++Line;
  84.  
  85.     /* don't need argc */
  86.     (VOID)GADS(++Line,
  87.         strlen(Line),
  88.         "Usage: Asn [AssignListFile]",
  89.         &argv,
  90.         "FILE");
  91.  
  92.  
  93.     /* use default file if no argument's given */
  94.     Fil = (argv == NULL) ? DEFAULT_FILE  : argv;
  95.  
  96.     if (!(fh=ArpOpen(Fil,MODE_OLDFILE))) 
  97.         {
  98.          retcod=IoErr();
  99.          Printf("Error: Couldn't open %s\n",Fil);
  100.          CloseLibrary((struct Library*)ArpBase);
  101.          Pr->pr_Result2=retcod;
  102.  
  103.         exit(RETURN_ERROR);
  104.         }
  105.  
  106.     if (!(Buf=(TEXT *)ArpAllocMem(BUFSIZE,MEMF_PUBLIC | MEMF_CLEAR)))
  107.         {
  108.         Puts("No memory!");
  109.         CloseLibrary((struct Library*)ArpBase);
  110.         Pr->pr_Result2=ERROR_NO_FREE_STORE; 
  111.         exit(RETURN_FAIL);
  112.         }
  113.  
  114.     while (count = Read(fh,Buf,BUFSIZE)) {
  115.     
  116.     /* point end of actually read data */
  117.     EndOfBuf = (TEXT *) Buf+count;
  118.  
  119.     /* scan file buffer */    
  120.     for (i=Buf;i < EndOfBuf; i++) {
  121.     
  122.     /* Check for ^C */
  123.     if (SetSignal(0,0) & SIGBREAKF_CTRL_C) {
  124.             Puts("***BREAK");
  125.             CloseLibrary((struct Library*)ArpBase);
  126.             exit(RETURN_WARN);
  127.             }
  128.         
  129.         /* skip comments */
  130.         if (((*i == '/') && (*(i+1) == '*')) ||
  131.                  St.StOfBuf == SPLIT_COMMENT) {        
  132.             while(!((i >= EndOfBuf) ||
  133.                 ((*i == '*') && (*(i+1) == '/')))) i++;
  134.             if (i >= EndOfBuf) St.StOfBuf = SPLIT_COMMENT;
  135.             if ((*i == '*') && (*(i+1) == '/')) {
  136.                     St.StOfBuf = OKAY;
  137.                     i+=2;
  138.                     }
  139.             }
  140.  
  141.         St.Do = DONTASSIGNEM;
  142.  
  143.         if (((*i >=33) && (*i <=126)) || (St.StOfBuf == SPLIT_NAME)) {
  144.             while(!((i>=EndOfBuf) || (*i <33) || (*i >126))) 
  145.                 Token[St.TokenType][Pos++] = *i++;
  146.             if (i>=EndOfBuf) St.StOfBuf = SPLIT_NAME;
  147.             else {
  148.                 St.StOfBuf = OKAY;
  149.                 if ((*i <33) || (*i >126)) {
  150.                 Token[St.TokenType][Pos] = '\0';
  151.                 if (St.TokenType==NAME) {
  152.                     St.TokenType = VALUE;
  153.                     St.Do = DONTASSIGNEM;
  154.                             }
  155.                 else {
  156.                     St.TokenType = NAME;
  157.                     St.Do = ASSIGNEM;
  158.                     }
  159.                 Pos = 0;
  160.                     }
  161.             
  162.                 }
  163.             }
  164.  
  165.         /* do device-var assignment */
  166.         if (St.Do == ASSIGNEM) {
  167.             /* ARP/MANX var */
  168.             if ((Toupper(Token[NAME][0]) == 'M') &&
  169.                  (Token[NAME][1] == '_')      &&
  170.                 (Token[NAME][2] != '\0')) {if
  171.                 ((!(Setenv(&(Token[NAME][2]),Token[VALUE])))) 
  172.                 Printf("Error: Unable to set ARP/MANX var %s as %s\n",
  173.                         &(Token[NAME][2]),
  174.                         Token[VALUE]); }    
  175.  
  176.             /* 1.3 environment var */
  177.             else if ((Toupper(Token[NAME][0]) == 'E') &&
  178.                      (Token[NAME][1] == '_')   &&
  179.                     (Token[NAME][2] != '\0') ) {
  180.             strcat(Env,&(Token[NAME][2]));
  181.             if (!(FH = ArpOpen(Env,MODE_NEWFILE)) || 
  182.                 !(Write(FH,Token[VALUE],
  183.                     strlen(Token[VALUE]))))
  184.                 Printf("Error: Unable to set environment var %s as %s\n",
  185.                         &(Token[NAME][2]),
  186.                         Token[VALUE]);
  187.                 /* reset var filename buffer */    
  188.                 Env[4] = '\0';
  189.                 }
  190.             /* logical device assign */    
  191.             else {
  192.                 /* strip colon in logical device name */
  193.                 Token[NAME][strlen(Token[NAME])-1] = '\0';
  194.                 if (!(Assign( Token[NAME],
  195.                   Token[VALUE]) == ASSIGN_OK)){
  196.                 Printf ("Error: Unable to assign %s: to %s\n",
  197.                         Token[NAME],
  198.                         Token[VALUE]);    
  199.                 }
  200.                 }
  201.             }            
  202.             }
  203.     }
  204.  
  205.      CloseLibrary((struct Library*)ArpBase);    
  206. }
  207.