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