remove OSF1 support
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / cmd / ksh93 / bltins / misc.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: misc.c /main/3 1995/11/01 16:28:50 rswiston $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF        *
29 *                    AT&T BELL LABORATORIES                    *
30 *         AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN         *
31 *            ACCORDANCE WITH APPLICABLE AGREEMENTS             *
32 *                                                              *
33 *                Copyright (c) 1995 AT&T Corp.                 *
34 *              Unpublished & Not for Publication               *
35 *                     All Rights Reserved                      *
36 *                                                              *
37 *       The copyright notice above does not evidence any       *
38 *      actual or intended publication of such source code      *
39 *                                                              *
40 *               This software was created by the               *
41 *           Advanced Software Technology Department            *
42 *                    AT&T Bell Laboratories                    *
43 *                                                              *
44 *               For further information contact                *
45 *                    {research,attmail}!dgk                    *
46 *                                                              *
47 ***************************************************************/
48
49 /* : : generated by proto : : */
50
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__      "C"
55 #else
56 #define __MANGLE__
57 #endif
58 #define __STDARG__
59 #define __PROTO__(x)    x
60 #define __OTORP__(x)
61 #define __PARAM__(n,o)  n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
64 #define const
65 #endif
66 #define signed
67 #define void            int
68 #define volatile
69 #define __V_            char
70 #else
71 #define __V_            void
72 #endif
73 #else
74 #define __PROTO__(x)    ()
75 #define __OTORP__(x)    x
76 #define __PARAM__(n,o)  o
77 #define __MANGLE__
78 #define __V_            char
79 #define const
80 #define signed
81 #define void            int
82 #define volatile
83 #endif
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__      ...
86 #else
87 #define __VARARG__
88 #endif
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a)       va_start(p,a)
91 #else
92 #define __VA_START__(p,a)       va_start(p)
93 #endif
94 #endif
95 #include        "defs.h"
96 #include        "variables.h"
97 #include        "shnodes.h"
98 #include        "path.h"
99 #include        "io.h"
100 #include        "name.h"
101 #include        "history.h"
102 #include        "builtins.h"
103 #include        "jobs.h"
104
105 #define DOTMAX  32      /* maximum level of . nesting */
106
107 static int      clear;
108 static void     noexport __PROTO__((Namval_t*));
109 static char     *arg0;
110
111 int    b_exec __PARAM__((int argc,char *argv[], __V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[]; __V_ *extra;){
112         register int n;
113         sh.st.ioset = 0;
114         clear = 0;
115         NOT_USED(extra);
116         while (n = optget(argv, sh_optexec)) switch (n)
117         {
118             case 'a':
119                 arg0 = opt_arg;
120                 argc = 0;
121                 break;
122             case 'c':
123                 clear=1;
124                 break;
125             case ':':
126                 error(2, opt_arg);
127                 break;
128             case '?':
129                 error(ERROR_usage(0), opt_arg);
130                 return(2);
131         }
132         argv += opt_index;
133         if(error_info.errors)
134                 error(ERROR_usage(2),optusage((char*)0));
135         if(*argv)
136                 b_login(argc,argv,0);
137         return(0);
138 }
139
140 static void     noexport __PARAM__((register Namval_t* np), (np)) __OTORP__(register Namval_t* np;){
141         nv_offattr(np,NV_EXPORT);
142 }
143
144 int    b_login __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
145         const char *pname;
146         struct checkpt *pp = (struct checkpt*)sh.jmplist;
147         NOT_USED(extra);
148         if(sh_isoption(SH_RESTRICTED))
149                 error(ERROR_exit(1),e_restricted,argv[0]);
150         else
151         {
152                 register struct argnod *arg=sh.envlist;
153                 register Namval_t* np;
154                 register char *cp;
155                 if(sh.subshell)
156                         sh_subfork();
157                 if(clear)
158                         nv_scan(sh.var_tree,noexport,NV_EXPORT,NV_EXPORT);
159                 while(arg)
160                 {
161                         if((cp=strchr(arg->argval,'=')) &&
162                                 (*cp=0,np=nv_search(arg->argval,sh.var_tree,0)))
163                                 nv_onattr(np,NV_EXPORT);
164                         if(cp)
165                                 *cp = '=';
166                         arg=arg->argnxt.ap;
167                 }
168                 pname = argv[0];
169                 if(argc==0)
170                         argv[0] = arg0;
171 #ifdef JOBS
172                 if(job_close() < 0)
173                         return(1);
174 #endif /* JOBS */
175                 /* force bad exec to terminate shell */
176                 pp->mode = SH_JMPEXIT;
177                 sh_sigreset(2);
178                 sh_freeup();
179                 path_exec(pname,argv,NIL(struct argnod*));
180                 sh_done(0);
181         }
182         return(1);
183 }
184
185 int    b_let __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
186         register int r;
187         register char *arg;
188         NOT_USED(argc);
189         NOT_USED(extra);
190         while (r = optget(argv,sh_optlet)) switch (r)
191         {
192             case ':':
193                 error(2, opt_arg);
194                 break;
195             case '?':
196                 error(ERROR_usage(2), opt_arg);
197                 break;
198         }
199         argv += opt_index;
200         if(error_info.errors || !*argv)
201                 error(ERROR_usage(2),optusage((char*)0));
202         while(arg= *argv++)
203                 r = !sh_arith(arg);
204         return(r);
205 }
206
207 int    b_eval __PARAM__((int argc,char *argv[], __V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[]; __V_ *extra;){
208         register int r;
209         NOT_USED(argc);
210         NOT_USED(extra);
211         while (r = optget(argv,sh_opteval)) switch (r)
212         {
213             case ':':
214                 error(2, opt_arg);
215                 break;
216             case '?':
217                 error(ERROR_usage(0), opt_arg);
218                 return(2);
219         }
220         if(error_info.errors)
221                 error(ERROR_usage(2),optusage((char*)0));
222         argv += opt_index;
223         if(*argv && **argv)
224         {
225                 sh_offstate(SH_MONITOR);
226                 sh_eval(sh_sfeval(argv),0);
227         }
228         return(sh.exitval);
229 }
230
231 int    b_dot_cmd __PARAM__((register int n,char *argv[],__V_* extra), (n, argv, extra)) __OTORP__(register int n;char *argv[];__V_* extra;){
232         register char *script;
233         register Namval_t *np;
234         register int jmpval;
235         int     fd;
236         struct dolnod   *argsave=0, *saveargfor;
237         char **saveargv;
238         struct checkpt buff;
239         Sfio_t *iop=0;
240         NOT_USED(extra);
241         while (n = optget(argv,sh_optdot)) switch (n)
242         {
243             case ':':
244                 error(2, opt_arg);
245                 break;
246             case '?':
247                 error(ERROR_usage(0), opt_arg);
248                 return(2);
249         }
250         argv += opt_index;
251         script = *argv;
252         if(error_info.errors || !script)
253                 error(ERROR_usage(2),optusage((char*)0));
254         if(!(np=sh.posix_fun))
255         {
256                 /* check for KornShell style function first */
257                 np = nv_search(script,sh.fun_tree,0);
258                 if(np && is_afunction(np) && !nv_isattr(np,NV_FPOSIX))
259                 {
260                         if(!np->nvalue.ip)
261                         {
262                                 path_search(script,NIL(char*),0);
263                                 if(np->nvalue.ip)
264                                 {
265                                         if(nv_isattr(np,NV_FPOSIX))
266                                                 np = 0;
267                                 }
268                                 else
269                                         error(ERROR_exit(1),e_found,script);
270                         }
271                 }
272                 else
273                         np = 0;
274                 if(!np  && (fd=path_open(script,path_get(script))) < 0)
275                         error(ERROR_system(1),e_open,script);
276         }
277         sh.posix_fun = 0;
278         if(sh.dot_depth++ > DOTMAX)
279                 error(ERROR_exit(1),e_toodeep,script);
280         if(np || argv[1])
281         {
282                 n = sh.st.dolc;
283                 saveargv = sh.st.dolv;
284                 argsave = sh_argnew(argv,&saveargfor);
285         }
286         sh_pushcontext(&buff,SH_JMPDOT);
287         jmpval = sigsetjmp(buff.buff,0);
288         if(jmpval == 0)
289         {
290                 if(np)
291                         sh_exec((union anynode*)(nv_funtree(np)),sh_isstate(SH_ERREXIT));
292                 else
293                 {
294                         char buff[IOBSIZE+1];
295                         iop = sfnew(NIL(Sfio_t*),buff,IOBSIZE,fd,SF_READ);
296                         sh_eval(iop,0);
297                 }
298         }
299         sh_popcontext(&buff);
300         sh.dot_depth--;
301         if((np || argv[1]) && jmpval!=SH_JMPSCRIPT)
302         {
303                 sh_argreset(argsave,saveargfor);
304                 sh.st.dolc = n;
305                 sh.st.dolv = saveargv;
306         }
307         if(sh.exitval > SH_EXITSIG)
308                 sh_fault(sh.exitval&SH_EXITMASK);
309         if(jmpval && jmpval!=SH_JMPFUN)
310                 siglongjmp(*sh.jmplist,jmpval);
311         return(sh.exitval);
312 }
313
314 /*
315  * null, true  command
316  */
317 int    b_true __PARAM__((int argc,register char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;register char *argv[];__V_ *extra;){
318         NOT_USED(argc);
319         NOT_USED(argv[0]);
320         NOT_USED(extra);
321         return(0);
322 }
323
324 /*
325  * false  command
326  */
327 int    b_false __PARAM__((int argc,register char *argv[], __V_ *extra), (argc, argv, extra)) __OTORP__(int argc;register char *argv[]; __V_ *extra;){
328         NOT_USED(argc);
329         NOT_USED(argv[0]);
330         NOT_USED(extra);
331         return(1);
332 }
333
334 int    b_shift __PARAM__((register int n, register char *argv[], __V_ *extra), (n, argv, extra)) __OTORP__(register int n; register char *argv[]; __V_ *extra;){
335         register char *arg;
336         NOT_USED(extra);
337         while((n = optget(argv,sh_optcflow))) switch(n)
338         {
339                 case ':':
340                         error(2, opt_arg);
341                         break;
342                 case '?':
343                         error(ERROR_usage(0), opt_arg);
344                         return(2);
345         }
346         if(error_info.errors)
347                 error(ERROR_usage(2),optusage((char*)0));
348         argv += opt_index;
349         n = ((arg= *argv)?(int)sh_arith(arg):1);
350         if(n<0 || sh.st.dolc<n)
351                 error(ERROR_exit(1),e_number,arg);
352         else
353         {
354                 sh.st.dolv += n;
355                 sh.st.dolc -= n;
356         }
357         return(0);
358 }
359
360 int    b_wait __PARAM__((register int n,register char *argv[],__V_ *extra), (n, argv, extra)) __OTORP__(register int n;register char *argv[];__V_ *extra;){
361         NOT_USED(extra);
362         while((n = optget(argv,sh_optjoblist))) switch(n)
363         {
364                 case ':':
365                         error(2, opt_arg);
366                         break;
367                 case '?':
368                         error(ERROR_usage(2), opt_arg);
369                         break;
370         }
371         if(error_info.errors)
372                 error(ERROR_usage(2),optusage((char*)0));
373         argv += opt_index;
374         job_bwait(argv);
375         return(sh.exitval);
376 }
377
378 #ifdef JOBS
379 int    b_bg_fg __PARAM__((register int n,register char *argv[],__V_ *extra), (n, argv, extra)) __OTORP__(register int n;register char *argv[];__V_ *extra;){
380         register int flag = **argv;
381         NOT_USED(extra);
382         while((n = optget(argv,sh_optjoblist))) switch(n)
383         {
384             case ':':
385                 error(2, opt_arg);
386                 break;
387             case '?':
388                 error(ERROR_usage(2), opt_arg);
389                 break;
390         }
391         if(error_info.errors)
392                 error(ERROR_usage(2),optusage((char*)0));
393         argv += opt_index;
394         if(!sh_isoption(SH_MONITOR) || !job.jobcontrol)
395         {
396                 if(sh_isstate(SH_INTERACTIVE))
397                         error(ERROR_exit(1),e_no_jctl);
398                 return(1);
399         }
400         if(flag=='d' && *argv==0)
401                 argv = (char**)0;
402         if(job_walk(sfstdout,job_switch,flag,argv))
403                 error(ERROR_exit(1),e_no_job);
404         return(sh.exitval);
405 }
406
407 int    b_jobs __PARAM__((register int n,char *argv[],__V_ *extra), (n, argv, extra)) __OTORP__(register int n;char *argv[];__V_ *extra;){
408         register int flag = 0;
409         NOT_USED(extra);
410         while((n = optget(argv,sh_optjobs))) switch(n)
411         {
412             case 'l':
413                 flag = JOB_LFLAG;
414                 break;
415             case 'n':
416                 flag = JOB_NFLAG;
417                 break;
418             case 'p':
419                 flag = JOB_PFLAG;
420                 break;
421             case ':':
422                 error(2, opt_arg);
423                 break;
424             case '?':
425                 error(ERROR_usage(2), opt_arg);
426                 break;
427         }
428         argv += opt_index;
429         if(error_info.errors)
430                 error(ERROR_usage(2),optusage((char*)0));
431         if(*argv==0)
432                 argv = (char**)0;
433         if(job_walk(sfstdout,job_list,flag,argv))
434                 error(ERROR_exit(1),e_no_job);
435         job_wait((pid_t)0);
436         return(sh.exitval);
437 }
438 #endif
439
440 #ifdef _cmd_universe
441 /*
442  * There are several universe styles that are masked by the getuniv(),
443  * setuniv() calls.
444  */
445 int     b_universe __PARAM__((int argc, char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc; char *argv[];__V_ *extra;){
446         register char *arg;
447         register int n;
448         NOT_USED(extra);
449         while((n = optget(argv,sh_optuniverse))) switch(n)
450         {
451             case ':':
452                 error(2, opt_arg);
453                 break;
454             case '?':
455                 error(ERROR_usage(2), opt_arg);
456                 break;
457         }
458         argv += opt_index;
459         argc -= opt_index;
460         if(error_info.errors || argc>1)
461                 error(ERROR_usage(2),optusage((char*)0));
462         if(arg = argv[0])
463         {
464                 if(!astconf("_AST_UNIVERSE",0,arg))
465                         error(ERROR_exit(1), e_badname,arg);
466         }
467         else
468         {
469                 if(!(arg=astconf("_AST_UNIVERSE",0,0)))
470                         error(ERROR_exit(1),e_nouniverse);
471                 else
472                         sfputr(sfstdout,arg,'\n');
473         }
474         return(0);
475 }
476 #endif /* cmd_universe */
477
478 #ifdef SHOPT_FS_3D
479     int b_vpath_map __PARAM__((register int argc,char *argv[], __V_ *extra), (argc, argv, extra)) __OTORP__(register int argc;char *argv[]; __V_ *extra;){
480         register int flag, n;
481         register const char *optstr; 
482         register char *vend; 
483         NOT_USED(extra);
484         if(argv[0][1]=='p')
485         {
486                 optstr = sh_optvpath;
487                 flag = FS3D_VIEW;
488         }
489         else
490         {
491                 optstr = sh_optvmap;
492                 flag = FS3D_VERSION;
493         }
494         while(n = optget(argv, optstr)) switch(n)
495         {
496             case ':':
497                 error(2, opt_arg);
498                 break;
499             case '?':
500                 error(ERROR_usage(2), opt_arg);
501                 break;
502         }
503         if(error_info.errors)
504                 error(ERROR_usage(2),optusage((char*)0));
505         argv += opt_index;
506         argc -= opt_index;
507         switch(argc)
508         {
509             case 0:
510             case 1:
511                 flag |= FS3D_GET;
512                 if((n = mount(*argv,(char*)0,flag,0)) >= 0)
513                 {
514                         vend = stakalloc(++n);
515                         n = mount(*argv,vend,flag|FS3D_SIZE(n),0);
516                 }
517                 if(n < 0)
518                         goto failed;
519                 if(argc==1)
520                 {
521                         sfprintf(sfstdout,"%s\n",vend);
522                         break;
523                 }
524                 n = 0;
525                 while(flag = *vend++)
526                 {
527                         if(flag==' ')
528                         {
529                                 flag  = e_sptbnl[n+1];
530                                 n = !n;
531                         }
532                         sfputc(sfstdout,flag);
533                 }
534                 if(n)
535                         sfputc(sfstdout,'\n');
536                 break;
537              default:
538                 if((argc&1))
539                         error(ERROR_usage(2),optusage((char*)0));
540                 /*FALLTHROUGH*/
541              case 2:
542                 if(!sh.lim.fs3d)
543                         goto failed;
544                 if(sh.subshell)
545                         sh_subfork();
546                 for(n=0;n<argc;n+=2)
547                 {
548                         if(mount(argv[n+1],argv[n],flag,0)<0)
549                                 goto failed;
550                 }
551         }
552         return(0);
553 failed:
554         if(argc>1)
555                 error(ERROR_exit(1),e_cantset,flag==2?e_mapping:e_versions);
556         else
557                 error(ERROR_exit(1),e_cantget,flag==2?e_mapping:e_versions);
558         return(1);
559     }
560 #endif /* SHOPT_FS_3D */
561