This commit was manufactured by cvs2svn to create tag 'd2x-0_1_2'.
[btb/d2x.git] / misc / parsarg.c
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-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #pragma off (unreferenced)
15 static char rcsid[] = "$Id: parsarg.c,v 1.1.1.1 2001-01-19 03:30:14 bradleyb Exp $";
16 #pragma on (unreferenced)
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <dos.h>
22
23 #include "parsarg.h"
24
25 #define ARGBUF_SIZE 500
26 #define MAX_ARGS 100
27
28 char arg_buf[ARGBUF_SIZE];
29 char *ab_ptr=arg_buf;
30
31 void perror_exit(int n,char *s)
32 {
33         perror(s);
34         exit(n);
35 }
36
37 void verror(char *s,void *first_arg_ptr)
38 {
39         fprintf(stderr,"Error: ");
40         vfprintf(stderr,s,first_arg_ptr);
41         fprintf(stderr,"\n");
42 }
43
44 void error_exit(int ret_code,char *s,...)
45 {
46         verror(s,((char *) &s)+sizeof(s));      /* addr of next parm on stack */
47         exit(ret_code);
48 }
49
50 void parse_args(int argc,char **argv,void (*handler_func)(char *arg),int flags)
51 {
52
53         for (;argc--;argv++) {
54                 if (**argv=='@') {                      /* read args from file */
55                         char *arg_ptrs[MAX_ARGS];
56                         int arg_count;
57                         FILE *argfile;
58                         int len;
59                         char *p=ab_ptr,c;
60
61                         if ((argfile=fopen(*argv+1,"rt"))==0) perror_exit(10,*argv+1);
62                         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");
63                         fclose(argfile);
64                         ab_ptr[len++]=0;                /* write terminating null */
65
66                         /* remove comments */
67
68                         while ((p=strchr(ab_ptr,';'))!=NULL) {
69                                 char *p2=strchr(p,'\n');
70
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
82                         while (!ab_ptr[len-1]) len--;   /* back up over terminating nulls */
83                         p=ab_ptr;
84
85
86                         for (arg_count=0;p<ab_ptr+len;) {
87                                 while (p<ab_ptr+len && ((c=*p)==' ' || c=='\t' || c=='\n')) p++;
88                                 if (p<ab_ptr+len) {     /* found parm? */
89                                         arg_ptrs[arg_count++]=p;
90                                         if (arg_count>=MAX_ARGS) error_exit(10,"Too many args");
91                                         while (p<ab_ptr+len && !((c=*p)==' ' || c=='\t' || c=='\n')) p++;
92                                         *p++=0;
93                                 }
94                         }
95                         ab_ptr+=len;
96                         parse_args(arg_count,arg_ptrs,handler_func,flags);
97                 }
98                 else
99                         if (flags&PA_EXPAND && (**argv != '-')) {
100                                 struct find_t ffblk;
101                                 char drive[_MAX_DRIVE],dir[_MAX_DIR];
102                                 char filename[_MAX_DRIVE+_MAX_DIR+13],*nptr;
103                                 int done;
104
105                                 _splitpath(*argv,drive,dir,NULL,NULL);          //get path
106                                 strcpy(filename,drive);
107                                 strcat(filename,dir);
108                                 nptr = filename + strlen(filename);                     //point at name part
109
110                                 done = _dos_findfirst(*argv,0,&ffblk);
111
112                                 if (done) handler_func(*argv);
113                         
114                                 else while (!done) {
115
116                                         strcpy(nptr,ffblk.name);        //copy name after path
117
118                                         handler_func(filename);
119                         
120                                         done = _dos_findnext(&ffblk);
121                         
122                                 }
123         
124                         }
125                         else
126                                 handler_func(*argv);
127
128         }
129
130 }
131
132