PARSARG.C 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. /*
  14. * $Source: f:/miner/source/misc/rcs/parsarg.c $
  15. * $Revision: 1.1 $
  16. * $Author: matt $
  17. * $Date: 1993/09/09 17:32:03 $
  18. *
  19. * Source for parse_args() function. See header file (parsarg.h) for info
  20. *
  21. * $Log: parsarg.c $
  22. * Revision 1.1 1993/09/09 17:32:03 matt
  23. * Initial revision
  24. *
  25. *
  26. */
  27. #pragma off (unreferenced)
  28. static char rcsid[] = "$Id: parsarg.c 1.1 1993/09/09 17:32:03 matt Exp $";
  29. #pragma on (unreferenced)
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <dos.h>
  34. #include "parsarg.h"
  35. #define ARGBUF_SIZE 500
  36. #define MAX_ARGS 100
  37. char arg_buf[ARGBUF_SIZE];
  38. char *ab_ptr=arg_buf;
  39. void perror_exit(int n,char *s)
  40. {
  41. perror(s);
  42. exit(n);
  43. }
  44. void verror(char *s,void *first_arg_ptr)
  45. {
  46. fprintf(stderr,"Error: ");
  47. vfprintf(stderr,s,first_arg_ptr);
  48. fprintf(stderr,"\n");
  49. }
  50. void error_exit(int ret_code,char *s,...)
  51. {
  52. verror(s,((char *) &s)+sizeof(s)); /* addr of next parm on stack */
  53. exit(ret_code);
  54. }
  55. void parse_args(int argc,char **argv,void (*handler_func)(char *arg),int flags)
  56. {
  57. for (;argc--;argv++) {
  58. if (**argv=='@') { /* read args from file */
  59. char *arg_ptrs[MAX_ARGS];
  60. int arg_count;
  61. FILE *argfile;
  62. int len;
  63. char *p=ab_ptr,c;
  64. if ((argfile=fopen(*argv+1,"rt"))==0) perror_exit(10,*argv+1);
  65. if ((len=fread(ab_ptr,1,ARGBUF_SIZE-((int) (ab_ptr-arg_buf)),argfile))==ARGBUF_SIZE-(ab_ptr-arg_buf)) error_exit(20,"Argument buffer not big enough\n");
  66. fclose(argfile);
  67. ab_ptr[len++]=0; /* write terminating null */
  68. /* remove comments */
  69. while ((p=strchr(ab_ptr,';'))!=NULL) {
  70. char *p2=strchr(p,'\n');
  71. if (p2) { /* found cr */
  72. strcpy(p,p2); /* copy over comment */
  73. len = strlen(ab_ptr);
  74. }
  75. else { /* no cr, end of string */
  76. *p=0;
  77. len = (int) (p-ab_ptr);
  78. }
  79. }
  80. ab_ptr[len]=0; /* write terminating null */
  81. while (!ab_ptr[len-1]) len--; /* back up over terminating nulls */
  82. p=ab_ptr;
  83. for (arg_count=0;p<ab_ptr+len;) {
  84. while (p<ab_ptr+len && ((c=*p)==' ' || c=='\t' || c=='\n')) p++;
  85. if (p<ab_ptr+len) { /* found parm? */
  86. arg_ptrs[arg_count++]=p;
  87. if (arg_count>=MAX_ARGS) error_exit(10,"Too many args");
  88. while (p<ab_ptr+len && !((c=*p)==' ' || c=='\t' || c=='\n')) p++;
  89. *p++=0;
  90. }
  91. }
  92. ab_ptr+=len;
  93. parse_args(arg_count,arg_ptrs,handler_func,flags);
  94. }
  95. else
  96. if (flags&PA_EXPAND && (**argv != '-')) {
  97. struct find_t ffblk;
  98. char drive[_MAX_DRIVE],dir[_MAX_DIR];
  99. char filename[_MAX_DRIVE+_MAX_DIR+13],*nptr;
  100. int done;
  101. _splitpath(*argv,drive,dir,NULL,NULL); //get path
  102. strcpy(filename,drive);
  103. strcat(filename,dir);
  104. nptr = filename + strlen(filename); //point at name part
  105. done = _dos_findfirst(*argv,0,&ffblk);
  106. if (done) handler_func(*argv);
  107. else while (!done) {
  108. strcpy(nptr,ffblk.name); //copy name after path
  109. handler_func(filename);
  110. done = _dos_findnext(&ffblk);
  111. }
  112. }
  113. else
  114. handler_func(*argv);
  115. }
  116. }