Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / cmd / ksh93 / bltins / getopts.c
1 /* $XConsortium: getopts.c /main/3 1995/11/01 16:28:23 rswiston $ */
2 /***************************************************************
3 *                                                              *
4 *                      AT&T - PROPRIETARY                      *
5 *                                                              *
6 *        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF        *
7 *                    AT&T BELL LABORATORIES                    *
8 *         AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN         *
9 *            ACCORDANCE WITH APPLICABLE AGREEMENTS             *
10 *                                                              *
11 *                Copyright (c) 1995 AT&T Corp.                 *
12 *              Unpublished & Not for Publication               *
13 *                     All Rights Reserved                      *
14 *                                                              *
15 *       The copyright notice above does not evidence any       *
16 *      actual or intended publication of such source code      *
17 *                                                              *
18 *               This software was created by the               *
19 *           Advanced Software Technology Department            *
20 *                    AT&T Bell Laboratories                    *
21 *                                                              *
22 *               For further information contact                *
23 *                    {research,attmail}!dgk                    *
24 *                                                              *
25 ***************************************************************/
26
27 /* : : generated by proto : : */
28
29 #if !defined(__PROTO__)
30 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
31 #if defined(__cplusplus)
32 #define __MANGLE__      "C"
33 #else
34 #define __MANGLE__
35 #endif
36 #define __STDARG__
37 #define __PROTO__(x)    x
38 #define __OTORP__(x)
39 #define __PARAM__(n,o)  n
40 #if !defined(__STDC__) && !defined(__cplusplus)
41 #if !defined(c_plusplus)
42 #define const
43 #endif
44 #define signed
45 #define void            int
46 #define volatile
47 #define __V_            char
48 #else
49 #define __V_            void
50 #endif
51 #else
52 #define __PROTO__(x)    ()
53 #define __OTORP__(x)    x
54 #define __PARAM__(n,o)  o
55 #define __MANGLE__
56 #define __V_            char
57 #define const
58 #define signed
59 #define void            int
60 #define volatile
61 #endif
62 #if defined(__cplusplus) || defined(c_plusplus)
63 #define __VARARG__      ...
64 #else
65 #define __VARARG__
66 #endif
67 #if defined(__STDARG__)
68 #define __VA_START__(p,a)       va_start(p,a)
69 #else
70 #define __VA_START__(p,a)       va_start(p)
71 #endif
72 #endif
73 #ifdef __osf__
74 #include        <machine/machlimits.h>
75 #endif
76 #include        "defs.h"
77 #include        "variables.h"
78 #include        <error.h>
79 #include        <nval.h>
80 #include        "builtins.h"
81
82 int     b_getopts __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
83         register char *options=error_info.context->id;
84         register Namval_t *np;
85         register int flag, mode, r=0;
86         static char value[2], key[2];
87         NOT_USED(extra);
88         while((flag = optget(argv,sh_optgetopts))) switch(flag)
89         {
90             case 'a':
91                 options = opt_arg;
92                 break;
93             case ':':
94                 error(2, opt_arg);
95                 break;
96             case '?':
97                 error(ERROR_usage(2), opt_arg);
98                 break;
99         }
100         argv += opt_index;
101         argc -= opt_index;
102         if(error_info.errors || argc<2)
103                 error(ERROR_usage(2),optusage((char*)0));
104         error_info.context->flags |= ERROR_SILENT;
105         error_info.id = options;
106         options = argv[0];
107         np = nv_open(argv[1],sh.var_tree,NV_NOASSIGN|NV_VARNAME);
108         if(argc>2)
109         {
110                 argv +=1;
111                 argc -=1;
112         }
113         else
114         {
115                 argv = sh.st.dolv;
116                 argc = sh.st.dolc;
117         }
118         opt_index = sh.st.optindex;
119         opt_char = sh.st.optchar;
120         if(mode= (*options==':'))
121                 options++;
122         switch(opt_index<=argc?(opt_num= LONG_MIN,flag=optget(argv,options)):0)
123         {
124             case '?':
125                 error(ERROR_usage(0),opt_arg);
126                 r = 2;
127                 *(options = value) = flag;
128                 break;
129             case ':':
130                 key[0] = opt_option[1];
131                 if(strmatch(opt_arg,"*unknown*"))
132                         flag = '?';
133                 if(mode)
134                         opt_arg = key;
135                 else
136                 {
137                         error(2, opt_arg);
138                         opt_arg = 0;
139                         flag = '?';
140                 }
141                 *(options = value) = flag;
142                 sh.st.opterror = 1;
143                 break;
144             case 0:
145                 if(sh.st.opterror)
146                 {
147                         char *com[2];
148                         com[0] = "-?";
149                         com[1] = 0;
150                         flag = opt_index;
151                         opt_index = 0;
152                         optget(com,options);
153                         opt_index = flag;
154                         if(!mode && strchr(options,' '))
155                                 error(ERROR_usage(0),optusage((char*)0));
156                 }
157                 opt_arg = 0;
158                 options = value;
159                 *options = '?';
160                 r=1;
161                 opt_char = 0;
162                 break;
163             default:
164                 options = opt_option + (*opt_option!='+');
165         }
166         error_info.context->flags &= ~ERROR_SILENT;
167         sh.st.optindex = opt_index;
168         sh.st.optchar = opt_char;
169         nv_putval(np, options, 0);
170         nv_close(np);
171         np = nv_search((char*)OPTARGNOD,sh.var_tree,NV_ADD|HASH_BUCKET|HASH_NOSCOPE);
172         if(opt_num != LONG_MIN)
173         {
174                 double d = opt_num;
175                 nv_putval(np, (char*)&d, NV_INTEGER|NV_RDONLY);
176         }
177         else
178                 nv_putval(np, opt_arg, NV_RDONLY);
179         return(r);
180 }
181