generated from configure.in
[swftools.git] / lib / args.h
1 /* args.h
2    Routines to simplify argument handling
3
4    Part of the swftools package.
5
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
7
8    This file is distributed under the GPL, see file COPYING for details */
9
10 #ifndef __args_h__
11 #define __args_h__
12
13 extern int args_callback_option(char*,char*);
14 extern int args_callback_longoption(char*,char*);
15 extern int args_callback_command(char*,char*);
16 extern void args_callback_usage(char*name);
17
18 int argn;
19 char**argv;
20
21 void processargs(int argn2,char**argv2)
22 {
23     int t;
24     if(argn2==1)
25     {
26         args_callback_usage(argv2[0]);
27         exit(1);
28     }
29     for(t=1;t<argn2;t++)
30     {
31         char*next;
32         if(t<argn2-1) next=argv2[t+1];
33         else        next=0;
34
35         if(argv2[t][0]=='-')
36         {
37             if(argv2[t][1]=='-')
38             {
39                 if(!strcmp(&argv2[t][2],"help")) 
40                 {
41                     args_callback_usage(argv2[0]);
42                     exit(1);
43                 }
44                 t+=args_callback_longoption(&argv2[t][2],next);
45             }
46             else
47             {
48                 if(strchr("?h",argv2[t][1]))
49                 {
50                     args_callback_usage(argv2[0]);
51                     exit(1);
52                 }
53                 t+=args_callback_option(&argv2[t][1],next);
54             }
55         }
56         else
57         {
58             t+=args_callback_command(argv2[t],next);
59         }
60     }
61 }
62
63 struct options_t
64 {
65     char*shortoption;
66     char*longoption;
67 };
68
69 int args_long2shortoption(struct options_t*options, char*name, char*val)
70 {
71     char*equal = strchr(name,'=');
72     if (equal) {
73         *equal = 0;
74         equal++;
75     }
76     while(options->shortoption) {
77         if(!strcmp(options->longoption, name)) {
78                 char*tmp = (char*)malloc(strlen(options->shortoption)
79                         +(equal?strlen(equal)+2:2));
80                 strcpy(tmp, options->shortoption);
81                 if(equal) {
82                     //strcpy(&tmp[strlen(tmp)], equal);
83                     int ret = args_callback_option(tmp, equal);
84                     return 0;
85                 }
86                 return args_callback_option(tmp,val);
87         }
88         options++;
89     }
90     fprintf(stderr, "Unknown option: --%s\n", name);
91     exit(1);
92 }
93
94 #endif //__args_h__