home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 143_01 / batch.c < prev    next >
Text File  |  1985-11-14  |  7KB  |  293 lines

  1. /*
  2. %CC1 $1.C -X -E5000
  3. %CLINK $1 DIO WILDEXP -S
  4. %DELETE $1.CRL 
  5. */
  6. /*********************************************************************
  7. *                               BATCH                                *
  8. **********************************************************************
  9. *                  COPYRIGHT 1983 EUGENE H. MALLORY                  *
  10. *********************************************************************/
  11. #include "BDSCIO.H"
  12. #ifndef C80
  13. #include "DIO.H"
  14. #endif
  15.  
  16. #define BCOUNT 120
  17.  
  18. char string[MAXLINE];
  19. char fname[MAXLINE];
  20. char c;
  21. int fd1,fd2;
  22. char fcb1[BUFSIZ],fcb2[BUFSIZ];
  23. char barray[BCOUNT][MAXLINE];
  24. char buffer[128];
  25. int bctr,flag;
  26. int str_comp();
  27.  
  28. main(argc,argv)
  29. char **argv;
  30. int argc;
  31.  
  32. BEGIN
  33.     int i,j,menuflag;
  34.     char *fvector[3];
  35.     char **fvectp;
  36.     char *disk;
  37.     int diskno,vctr,len;
  38.     
  39.     dioinit(&argc,argv);
  40.     
  41.     if (DIOIN)
  42.     BEGIN
  43.         bctr = 0;
  44.         while (!getstring(string))
  45.         BEGIN
  46.             len = strlen(string);
  47.             string[len-1] = 0;
  48.             linecopy(barray[bctr++],string,argv,argc,0); 
  49.             if (bctr == BCOUNT)
  50.             error("BATCH: Batch file too big.");
  51.         END
  52.         goto writesub;
  53.     END
  54.  
  55.  
  56.     if (argc >= 2 && argv[1][0] == '/')
  57.     BEGIN
  58.         bctr = 0;
  59.         typef("*");
  60.         while (gets(string))
  61.         BEGIN
  62.             if (string[0] == 0) goto writesub;
  63.             linecopy(barray[bctr++],string,argv,argc,1); 
  64.             if (bctr == BCOUNT)
  65.             error("BATCH: Batch file too big.");
  66.             typef("*");
  67.         END
  68.         goto writesub;
  69.     END
  70.     
  71.     if (argc < 2)
  72.     BEGIN
  73.         disk = "ABCDEFGHIJKLM";
  74.         vctr = 3;
  75.         diskno = bdos(25,0);
  76.         if (!diskno) vctr = 2;
  77.         fvector[0] = "DUMMY";
  78.         fvector[1] = "?:*.BAT";
  79.         fvector[1][0] = disk[diskno];
  80.         fvector[2] = "A:*.BAT";
  81.         fvectp = fvector;
  82.         wildexp(&vctr,&fvectp);
  83. #ifdef C80
  84.         qsort(&fvectp[1],vctr-1,2,str_comp); 
  85. #else
  86.         qsort(&fvectp[1],vctr-1,2,&str_comp); 
  87. #endif
  88.         fvectp++;
  89. filemenu:
  90.         i = menu(vctr-1,fvectp);
  91.         if (i == -1) goto exitbatch;
  92.         strcpy(fname,fvectp[i]);
  93.         fd1 = fopen(fname,fcb1);
  94.         if (fd1 == ERROR)
  95.         error("BATCH: Unable to open %s",fname);
  96.     END
  97.     else
  98.     BEGIN
  99.         strcpy(fname,argv[1]);
  100.         strcat(fname,".BAT");
  101.         fd1 = fopen(fname,fcb1);
  102.         if (fd1==ERROR) 
  103.         BEGIN
  104.             strcpy(fname,"a:");
  105.             strcat(fname,argv[1]);
  106.             strcat(fname,".BAT");
  107.             fd1 = fopen(fname,fcb1);
  108.         END
  109.         if (fd1==ERROR) 
  110.         BEGIN
  111.             dioflush();
  112.             error("BATCH: Unable to find: %s. \N",fname);
  113.         END
  114.     END
  115.     
  116.     if (!fgets(string,fcb1))
  117.     BEGIN
  118.         dioflush();
  119.         error("BATCH: Batch file %s empty.",fname);
  120.     END
  121.     
  122.     bctr = 0;
  123.     do
  124.     BEGIN
  125.         len = strlen(string);
  126.         string[len-1] = 0;
  127.         linecopy(barray[bctr++],string,argv,argc,1); 
  128.         if (bctr == BCOUNT)
  129.         error("BATCH: Batch file too big.");
  130.     END
  131.     while (fgets(string,fcb1));
  132.     
  133. /*    SUBSTITUTED ARGUMENTS ARE NOW IN BARRAY */    
  134.  
  135. writesub:    
  136.     fd2 = open("A:$$$.SUB",READWRITE);
  137.     if (fd2 == -1)
  138.     BEGIN
  139.         fd2 = creat("A:$$$.SUB");
  140.     END
  141.     else
  142.     BEGIN
  143.         do
  144.         BEGIN
  145. #ifdef C80
  146.             flag = seek(fd2,0,2);    /* goto end of file */
  147.             /* This had to be done since C80 read only 
  148.             indicates when beyond EOF and it does so by 
  149.             returning an error flag. This accomplishes 
  150.             the same thing but isn't very elegant. Sorry. */
  151. #else
  152.             flag = read(fd2,buffer,1);
  153. #endif
  154.             if (flag == -1)
  155.             error("BATCH: Unable to append to A:$$$.SUB.");
  156.         END
  157.         while (flag);
  158.     END
  159.     
  160.     /* Now $$.sub should be opened correctly */
  161.     
  162.     while (bctr--)
  163.     BEGIN
  164.         for (i=0;i<128;i++) buffer[i] = 0x1a;
  165.         strcpy(&buffer[1],barray[bctr]);
  166.         buffer[0] = strlen(barray[bctr]);
  167.         i = write(fd2,buffer,1);
  168.         if (i != 1)
  169.         error("BATCH: Unable to write A:$$$.SUB.");
  170.     END
  171.     close(fd2);
  172.     
  173. exitbatch:
  174.     dioflush();
  175.     bios(1);
  176. END
  177.  
  178. menu(icounter,strings)
  179. char **strings;
  180. int icounter;
  181. BEGIN
  182. int max,bias,i,j;
  183. char *prompt;
  184.     prompt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  185.     bias = 0;
  186. loop:
  187.     typef("\n\014");
  188.     if ((icounter-bias) > 26) max = 26;
  189.     else max = icounter-bias;
  190.     for (i=0;i<(max+1)/2;i++)
  191.     BEGIN
  192.         typef("%c) %s",prompt[i],strings[i+bias]);
  193.         if (i+bias+(max+1)/2 < icounter)
  194.         BEGIN
  195.             for (j=0;j<=37-strlen(strings[i+bias]);j++) typef(" ");
  196.             typef("%c) %s\n",
  197.             prompt[i+(max+1)/2],
  198.             strings[i+bias+(max+1)/2]);
  199.         END
  200.         else typef("\n");
  201.     END
  202.     typef("\n\n");
  203.     if (icounter > bias+26)
  204.     typef("More selections are available.\n");
  205.     typef("Type selection, ESC to exit, CR for more, - to backup :");
  206.     c = bdos(1,0);
  207.     typef("\n\014");
  208.     if (c == '\033') return -1;
  209.     if (c == '\r' && (icounter > bias+26)) bias += 26;
  210.     else if (c == '\r' && (icounter <= bias+26)) bias = 0;
  211.     if (c == '-') bias -= 26;
  212.     if (bias < 0) bias = 0;
  213.     if (!isalpha(c)) goto loop;
  214.     for (i=0;toupper(c)!=prompt[i];i++);
  215.     if (i>=max) goto loop;
  216.     if (i+bias >= icounter) goto loop;
  217.     return i+bias;
  218. END
  219.  
  220. int str_comp(s,t)
  221. char **s, **t;
  222. BEGIN
  223.     char *s1, *t1;
  224.     int i;
  225.     s1 = *s;
  226.     t1 = *t;
  227.     for (i=0;i<MAXLINE;i++)
  228.         BEGIN
  229.         if (t1[i] != s1[i]) 
  230.             return s1[i] - t1[i];
  231.         if (s1[i] == '\0')  return 0;
  232.         END
  233.     return 0;
  234. END
  235.  
  236. linecopy(dest,source,argv,argc,bias)
  237. char *dest,*source,**argv;
  238. int argc,bias;
  239. BEGIN
  240. int i;
  241. char c,tempstr[MAXLINE];
  242.     if (*source == '*') *source = ';';
  243.     while (c = *source++)    
  244.     BEGIN
  245.         if (c == '$' && isdigit(*source))
  246.         BEGIN
  247.             i = *source-'0'+bias;
  248.             if (i < argc)
  249.             BEGIN
  250.                 strcpy(dest,argv[i]);
  251.                 dest += strlen(argv[i]);
  252.             END
  253.             source++;
  254.         END
  255.         else if (c == '$' && *source == '*')
  256.         BEGIN
  257.             for (i=1+bias;i<argc;i++)
  258.             BEGIN
  259.                 if (i < argc)
  260.                 BEGIN
  261.                     strcpy(dest,argv[i]);
  262.                     strcat(dest," ");
  263.                     dest += strlen(argv[i])+1;
  264.                 END
  265.             END
  266.             source++;
  267.         END
  268.         else if (c == '$' && *source == '#')
  269.         BEGIN
  270.             sprintf(tempstr,"%d",argc-bias);
  271.             strcpy(dest,tempstr);
  272.             dest += strlen(tempstr);
  273.             source++;
  274.         END
  275.         else if (c == '$' && *source == '$')
  276.         BEGIN
  277.             source++;
  278.             *dest++ = c;
  279.         END
  280.         else if (c == '^' && isalpha(*source))
  281.         BEGIN
  282.             *dest++ = toupper(*source)-0x40;
  283.             source++;
  284.         END 
  285.         else
  286.         BEGIN
  287.             *dest++ = c;
  288.         END
  289.     END
  290.     *dest = 0;
  291. END
  292.  
  293.