OSDN Git Service

Bumped version 2.4.4.
[chasen-legacy/chasen.git] / lib / getopt.c
1 /*
2  * Copyright (c) 2003 Nara Institute of Science and Technology
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name Nara Institute of Science and Technology may not be used to
15  *    endorse or promote products derived from this software without
16  *    specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Nara Institute of Science and Technology 
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
21  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE Nara Institute
22  * of Science and Technology BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $Id: getopt.c,v 1.1.1.1 2007/03/13 07:40:10 masayu-a Exp $
31  */
32
33 #include <stdio.h>
34 #include <string.h>
35
36 int Cha_optind = 0;
37 char *Cha_optarg;
38
39 int
40 cha_getopt(char **argv, char *optstring, FILE * fp)
41 {
42     static char *nextchar;
43     char *op, c;
44
45     /*
46      * initialization 
47      */
48     if (Cha_optind == 0) {
49         Cha_optind = 1;
50         nextchar = argv[1];
51     }
52     Cha_optarg = NULL;
53
54     if (nextchar == argv[Cha_optind]) {
55         /*
56          * no option 
57          */
58         if (nextchar == NULL || nextchar[0] != '-' || nextchar[1] == '\0')
59             return EOF;
60         /*
61          * '--' option 
62          */
63         if (*++nextchar == '-') {
64             nextchar = argv[++Cha_optind];
65             return EOF;
66         }
67     }
68
69     /*
70      * find out an option letter 
71      */
72     c = *nextchar++;
73     if ((op = strchr(optstring, c)) == NULL || c == ':') {
74         if (fp != NULL)
75             fprintf(fp, "%s: invalid option -- %c\n", argv[0], c);
76         c = '?';
77     }
78     /*
79      * option with an argument 
80      */
81     else if (op[1] == ':') {
82         /*
83          * next character 
84          */
85         if (*nextchar)
86             Cha_optarg = nextchar;
87         /*
88          * next argv 
89          */
90         else if (argv[Cha_optind + 1] != NULL)
91             Cha_optarg = argv[++Cha_optind];
92         /*
93          * no argument 
94          */
95         else {
96             if (fp != NULL)
97                 fprintf(fp, "%s: option requires an argument -- %c\n",
98                         argv[0], c);
99             c = '?';
100         }
101         nextchar = argv[++Cha_optind];
102     }
103
104     if (nextchar != NULL && *nextchar == '\0')
105         nextchar = argv[++Cha_optind];
106
107     return c;
108 }
109
110 /*
111  * chasen_getopt
112  */
113 int
114 cha_getopt_chasen(char **argv, FILE * fp)
115 {
116     return cha_getopt(argv, "i:sP:D:RabmpdvfecMo:F:L:l:jr:w:O:ChV", fp);
117 }
118
119
120
121 #ifdef TEST
122 int
123 main(int argc, char *argv[])
124 {
125     int c;
126
127     while (1) {
128         c = cha_getopt(argv, "abc:d:", stderr);
129         if (c == EOF)
130             break;
131         switch (c) {
132         case 'a':
133             printf("option a\n");
134             break;
135
136         case 'b':
137             printf("option b\n");
138             break;
139
140         case 'c':
141             printf("option c with value `%s'\n", Cha_optarg);
142             break;
143
144         case '?':
145             break;
146
147         default:
148             printf("?? getopt returned character code 0%o ??\n", c);
149         }
150     }
151
152     if (Cha_optind < argc) {
153         printf("non-option ARGV-elements: ");
154         while (Cha_optind < argc)
155             printf("%s ", argv[Cha_optind++]);
156         printf("\n");
157     }
158
159     exit(0);
160 }
161 #endif /* TEST */