OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [sash/] [sash.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * Copyright (c) 1993 by David I. Bell
3
 * Permission is granted to use, distribute, or modify this source,
4
 * provided that this copyright notice remains intact.
5
 *
6
 * Stand-alone shell for system maintainance for Linux.
7
 * This program should NOT be built using shared libraries.
8
 *
9
 * 1.1.1,       hacked to re-allow cmd line invocation of script file
10
 *              Pat Adamo, padamo@unix.asb.com
11
 */
12
 
13
#include "sash.h"
14
 
15
#ifndef CMD_HELP
16
#define CMD_HELP
17
#endif
18
#undef INTERNAL_PATH_EXPANSION
19
#define FAVOUR_EXTERNAL_COMMANDS
20
 
21
#include <stdlib.h>
22
#include <signal.h>
23
#include <errno.h>
24
#include <unistd.h>
25
#include <sys/stat.h>
26
#include <sys/time.h>
27
#include <sys/wait.h>
28
 
29
#ifndef EMBED
30
#define __signal(x,y,z) do {} while (0)
31
#endif
32
 
33
static char version[] = "1.1.1";
34
 
35
extern int intflag;
36
 
37
extern void do_test();
38
 
39
typedef struct {
40
        char    name[10];
41
        char    usage[30];
42
        void    (*func)();
43
        int     minargs;
44
        int     maxargs;
45
} CMDTAB;
46
 
47
 
48
CMDTAB  cmdtab[] = {
49
/*
50
        "alias",        "[name [command]]",     do_alias,
51
        1,              MAXARGS,
52
*/
53
        "cd",           "[dirname]",            do_cd,
54
        1,              2,
55
 
56
        "sleep",                "seconds",              do_sleep,
57
        1,              2,
58
 
59
        "chgrp",        "gid filename ...",     do_chgrp,
60
        3,              MAXARGS,
61
 
62
        "chmod",        "mode filename ...",    do_chmod,
63
        3,              MAXARGS,
64
 
65
        "chown",        "uid filename ...",     do_chown,
66
        3,              MAXARGS,
67
 
68
        "cmp",          "filename1 filename2",  do_cmp,
69
        3,              3,
70
 
71
        "cp",           "srcname ... destname", do_cp,
72
        3,              MAXARGS,
73
 
74
/*
75
        "dd",           "if=name of=name [bs=n] [count=n] [skip=n] [seek=n]", do_dd,
76
        3,              MAXARGS,
77
*/
78
        "df",           "[file-system]",        do_df,
79
        1,              2,
80
 
81
        "echo", "[args] ...",                   do_echo,
82
        1,              MAXARGS,
83
 
84
/*
85
        "ed",           "[filename]",           do_ed,
86
        1,              2,
87
*/
88
 
89
        "exec",         "filename [args]",      do_exec,
90
        2,              MAXARGS,
91
 
92
        "exit",         "",                     do_exit,
93
        1,              1,
94
 
95
        "free",         "",                     do_free,
96
        1,              1,
97
 
98
/*
99
        "-grep",        "[-in] word filename ...",      do_grep,
100
        3,              MAXARGS,
101
*/
102
 
103
#ifdef CMD_HELP
104
        "help",         "",                     do_help,
105
        1,              MAXARGS,
106
#endif
107
 
108
        "hexdump",      "[-s] filename",        do_hexdump,
109
        1,              3,
110
 
111
        "hostname",     "[hostname]",           do_hostname,
112
        1,              2,
113
 
114
        "kill",         "[-sig] pid ...",       do_kill,
115
        2,              MAXARGS,
116
 
117
        "ln",           "[-s] srcname ... destname",    do_ln,
118
        3,              MAXARGS,
119
 
120
        "ls",           "[-lidC] filename ...", do_ls,
121
        1,              MAXARGS,
122
 
123
        "mkdir",        "dirname ...",          do_mkdir,
124
        2,              MAXARGS,
125
 
126
        "mknod",        "filename type major minor",    do_mknod,
127
        5,              5,
128
 
129
        "more", "filename ...",         do_more,
130
        2,              MAXARGS,
131
 
132
        "mount",        "[-t type] devname dirname",    do_mount,
133
        3,              MAXARGS,
134
 
135
        "mv",           "srcname ... destname", do_mv,
136
        3,              MAXARGS,
137
 
138
        "printenv",     "[name]",               do_printenv,
139
        1,              2,
140
 
141
        "pwd",          "",                     do_pwd,
142
        1,              1,
143
 
144
        "pid",          "",                     do_pid,
145
        1,              1,
146
 
147
        "quit",         "",                     do_exit,
148
        1,              1,
149
 
150
        "rm",           "filename ...",         do_rm,
151
        2,              MAXARGS,
152
 
153
        "rmdir",        "dirname ...",          do_rmdir,
154
        2,              MAXARGS,
155
 
156
        "setenv",       "name value",           do_setenv,
157
        3,              3,
158
 
159
        "source",       "filename",             do_source,
160
        2,              2,
161
 
162
        "sync", "",                     do_sync,
163
        1,              1,
164
 
165
/*      "time", "",                     do_time,
166
        1,              1,
167
*/
168
/*
169
        "tar",          "[xtv]f devname filename ...",  do_tar,
170
        2,              MAXARGS,
171
*/
172
        "touch",        "filename ...",         do_touch,
173
        2,              MAXARGS,
174
 
175
        "umask",        "[mask]",               do_umask,
176
        1,              2,
177
 
178
        "umount",       "filename",             do_umount,
179
        2,              2,
180
 
181
/*
182
        "unalias",      "name",                 do_unalias,
183
        2,              2,
184
*/
185
        "ps",           "",                     do_ps,
186
        1,              MAXARGS,
187
 
188
/*      "reboot",       "",                     do_reboot,
189
        1,              MAXARGS,
190
*/
191
        "cat",          "filename ...",         do_cat,
192
        2,              MAXARGS,
193
 
194
        "date",         "date [MMDDhhmm[YYYY]]",        do_date,
195
        1,              2,
196
 
197
        NULL,           0,                       0,
198
        0,               0
199
};
200
 
201
 
202
typedef struct {
203
        char    *name;
204
        char    *value;
205
} ALIAS;
206
 
207
 
208
static  ALIAS   *aliastable;
209
static  int     aliascount;
210
 
211
static  FILE    *sourcefiles[MAXSOURCE];
212
static  int     sourcecount;
213
 
214
volatile static BOOL    intcrlf = TRUE;
215
 
216
 
217
static  void    catchint();
218
static  void    catchquit();
219
static  void    catchchild();
220
static  void    readfile();
221
static  void    command();
222
static  void    runcmd();
223
static  void    showprompt();
224
static  BOOL    trybuiltin();
225
static  BOOL    command_in_path();
226
static  ALIAS   *findalias();
227
 
228
extern char ** environ;
229
 
230
/*
231
char text1[] = "Text";
232
char * text2 = text1;
233
char ** text3 = &text2;
234
*/
235
 
236 708 simons
int main(argc, argv, env)
237 199 simons
        char    **argv;
238
        char    *env[];
239
{
240
        char    *cp;
241
/*      char    buf[PATHLEN];*/
242
        int dofile = 0;
243
 
244
        if ((argc > 1) && !strcmp(argv[1], "-c")) {
245
                /* We aren't that fancy a shell */
246
                exit(0);
247
        }
248
 
249
        //;'pa990523 +
250
        if ((argc > 1) && strcmp(argv[1], "-t"))
251
                {
252
                dofile++;
253
                printf("Shell invoked to run file: %s\n",argv[1]);
254
                }
255
        else
256
                printf("\nSash command shell (version %s)\n", version);
257
        fflush(stdout);
258
 
259
        signal(SIGINT, catchint);
260
        signal(SIGQUIT, catchquit);
261
        __signal(SIGCHLD, catchchild, SA_RESTART);
262
 
263
        if (getenv("PATH") == NULL)
264
                putenv("PATH=/bin:/usr/bin:/etc");
265
 
266
/*      cp = getenv("HOME");
267
        if (cp) {
268
                strcpy(buf, cp);
269
                strcat(buf, "/");
270
                strcat(buf, ".aliasrc");
271
 
272
                if ((access(buf, 0) == 0) || (errno != ENOENT))
273
                        readfile(buf);
274
        }
275
*/
276
        //;'pa990523 -1/+
277
        //readfile(NULL);
278
        if (dofile)
279
                {
280
                //open the file for reading!
281
                readfile(argv[1]);
282
                }
283 854 simons
//         else
284
//              {
285 199 simons
                readfile(NULL); //no arguments!
286 854 simons
//              } //end if arguments supplied
287 199 simons
        exit(0);
288
}
289
 
290
char    buf[CMDLEN];
291
 
292
 
293
/*
294
 * Read commands from the specified file.
295
 * A null name pointer indicates to read from stdin.
296
 */
297
static void
298
readfile(name)
299
        char    *name;
300
{
301
        FILE    *fp;
302
        int     cc;
303
        BOOL    ttyflag;
304
        char    *ptr;
305
 
306
        if (sourcecount >= MAXSOURCE) {
307
                fprintf(stderr, "Too many source files\n");
308
                return;
309
        }
310
 
311
        fp = stdin;
312
        if (name) {
313
                fp = fopen(name, "r");
314
                if (fp == NULL) {
315
                        perror(name);
316
                        return;
317
                }
318
        }
319
        sourcefiles[sourcecount++] = fp;
320
 
321
        ttyflag = isatty(fileno(fp));
322
 
323
        while (TRUE) {
324
                fflush(stdout);
325
                //;'pa990523 -1/+1
326
                //if (1)
327
                if (fp == stdin) //using terminal, so show prompt
328
                        showprompt();
329
 
330
                if (intflag && !ttyflag && (fp != stdin)) {
331
                        fclose(fp);
332
                        sourcecount--;
333
                        return;
334
                }
335
 
336
                if (fgets(buf, CMDLEN - 1, fp) == NULL) {
337
                        if (ferror(fp) && (errno == EINTR)) {
338
                                clearerr(fp);
339
                                continue;
340
                        }
341
                        break;
342
                }
343
 
344
                cc = strlen(buf);
345
                if (buf[cc - 1] == '\n')
346
                        cc--;
347
 
348
                while ((cc > 0) && isblank(buf[cc - 1]))
349
                        cc--;
350
                buf[cc] = '\0';
351
                //;'pa990523 +
352
                if (fp != stdin)
353
                        {
354
                        //taking commands from file - echo
355
                        printf("Command: %s\n",buf);
356
                        } //end if (fp != stdin)
357
 
358
                /* remove leading spaces and look for a '#' */
359
                ptr = &buf[0];
360
                while (*ptr == ' ') {
361
                        ptr++;
362
                }
363
                if (*ptr != '#')
364
                        command(buf);
365
        }
366
 
367
 
368
 
369
        if (ferror(fp)) {
370
                perror("Reading command line");
371
                if (fp == stdin)
372
                        exit(1);
373
        }
374
 
375
        clearerr(fp);
376
        if (fp != stdin)
377
                {//;'pa990523 added braces and printf
378
                fclose(fp);
379 854 simons
//              printf("Execution Finished, Exiting\n");
380 199 simons
                } //end if (fp != stdin)
381
 
382
        sourcecount--;
383
}
384
 
385
 
386
/*
387
 * Parse and execute one null-terminated command line string.
388
 * This breaks the command line up into words, checks to see if the
389
 * command is an alias, and expands wildcards.
390
 */
391
static void
392
command(cmd)
393
        char    *cmd;
394
{
395
        ALIAS   *alias;
396
        char    **argv;
397
        int     argc;
398
        int     bg;
399
        char   *c;
400
 
401
        intflag = FALSE;
402
 
403
        freechunks();
404
 
405
        while (isblank(*cmd))
406
                cmd++;
407
 
408
        if (c = strchr(cmd, '&')) {
409
                *c = '\0';
410
                bg = 1;
411
        } else
412
                bg = 0;
413
 
414
        if ((cmd = expandenvvar(cmd)) == NULL)
415
                return;
416
 
417
        if ((*cmd == '\0') || !makeargs(cmd, &argc, &argv))
418
                return;
419
 
420
        /*
421
         * Search for the command in the alias table.
422
         * If it is found, then replace the command name with
423
         * the alias, and append any other arguments to it.
424
         */
425
        alias = findalias(argv[0]);
426
        if (alias) {
427
                cmd = buf;
428
                strcpy(cmd, alias->value);
429
 
430
                while (--argc > 0) {
431
                        strcat(cmd, " ");
432
                        strcat(cmd, *++argv);
433
                }
434
 
435
                if (!makeargs(cmd, &argc, &argv))
436
                        return;
437
        }
438
 
439
        /*
440
         * Now look for the command in the builtin table, and execute
441
         * the command if found.
442
         */
443
#ifdef FAVOUR_EXTERNAL_COMMANDS
444
        if (!command_in_path(argv[0]))
445
#endif
446
        if (trybuiltin(argc, argv))
447
                return;
448
 
449
        /*
450
         * Not found, run the program along the PATH list.
451
         */
452
        runcmd(cmd, bg, argc, argv);
453
}
454
 
455
 
456
#ifdef FAVOUR_EXTERNAL_COMMANDS
457
/*
458
 * return true if we find this command in our
459
 * path.
460
 */
461
static BOOL
462
command_in_path(char *cmd)
463
{
464
        struct stat     stat_buf;
465
 
466
        if (strchr(cmd, '/') == 0) {
467
                char    * path;
468
                static char     path_copy[PATHLEN];
469
 
470
                /* Search path for binary */
471
                for (path = getenv("PATH"); path && *path; ) {
472
                        char * p2;
473
 
474
                        strcpy(path_copy, path);
475
                        if (p2 = strchr(path_copy, ':')) {
476
                                *p2 = '\0';
477
                        }
478
 
479
                        if (strlen(path_copy))
480
                                strcat(path_copy, "/");
481
                        strcat(path_copy, cmd);
482
 
483
                        if (!stat(path_copy, &stat_buf) && (stat_buf.st_mode & 0111))
484
                                return(TRUE);
485
 
486
                        p2 = strchr(path, ':');
487
                        if (p2)
488
                                path = p2 + 1;
489
                        else
490
                                path = 0;
491
                }
492
        } else if (!stat(cmd, &stat_buf) && (stat_buf.st_mode & 0111))
493
                return(TRUE);
494
        return(FALSE);
495
}
496
#endif /* FAVOUR_EXTERNAL_COMMANDS */
497
 
498
 
499
/*
500
 * Try to execute a built-in command.
501
 * Returns TRUE if the command is a built in, whether or not the
502
 * command succeeds.  Returns FALSE if this is not a built-in command.
503
 */
504
static BOOL
505
trybuiltin(argc, argv)
506
        char    **argv;
507
{
508
        CMDTAB  *cmdptr;
509
        int     oac;
510
        int     newargc;
511
        int     matches;
512
        int     i;
513
        char    *newargv[MAXARGS];
514
        char    *nametable[MAXARGS];
515
 
516
        cmdptr = cmdtab - 1;
517
        do {
518
                cmdptr++;
519
                if (cmdptr->name[0] == 0)
520
                        return FALSE;
521
 
522
        } while (strcmp(argv[0], cmdptr->name));
523
 
524
        /*
525
         * Give a usage string if the number of arguments is too large
526
         * or too small.
527
         */
528
        if ((argc < cmdptr->minargs) || (argc > cmdptr->maxargs)) {
529
                fprintf(stderr, "usage: %s %s\n",
530
                        cmdptr->name, cmdptr->usage);
531
                fflush(stderr);
532
 
533
                return TRUE;
534
        }
535
 
536
        /*
537
         * Check here for several special commands which do not
538
         * have wildcarding done for them.
539
         */
540
 
541
/*        if (cmdptr->func == do_prompt) {
542
                (*cmdptr->func)(argc, argv);
543
                return TRUE;
544
        }
545
*/
546
 
547
        /*
548
         * Now for each command argument, see if it is a wildcard, and if
549
         * so, replace the argument with the list of matching filenames.
550
         */
551
        newargv[0] = argv[0];
552
        newargc = 1;
553
        oac = 0;
554
 
555
        while (++oac < argc) {
556
                matches = expandwildcards(argv[oac], MAXARGS, nametable);
557
                if (matches < 0)
558
                        return TRUE;
559
 
560
                if ((newargc + matches) >= MAXARGS) {
561
                        fprintf(stderr, "Too many arguments\n");
562
                        return TRUE;
563
                }
564
 
565
                if (matches == 0)
566
                        newargv[newargc++] = argv[oac];
567
 
568
                for (i = 0; i < matches; i++)
569
                        newargv[newargc++] = nametable[i];
570
        }
571
 
572
        (*cmdptr->func)(newargc, newargv);
573
 
574
        return TRUE;
575
}
576
 
577
 
578
/*
579
 * Execute the specified command.
580
 */
581
static void
582
runcmd(cmd, bg, argc, argv)
583
        char    *cmd;
584
        int     bg;
585
        int     argc;
586
        char    **argv;
587
{
588
        register char * cp;
589
        BOOL            magic;
590
        int             pid;
591
        int             status;
592
        int oac;
593
        int newargc;
594
        int matches;
595
        int i;
596
        char    *newargv[MAXARGS];
597
        char    *nametable[MAXARGS];
598
 
599
        newargv[0] = argv[0];
600
 
601
#ifdef INTERNAL_PATH_EXPANSION
602
        if (strchr(argv[0], '/') == 0) {
603
                char    * path;
604
                struct stat     stat_buf;
605
                static char     path_copy[PATHLEN];
606
 
607
                /* Search path for binary */
608
                for (path = getenv("PATH"); path && *path; ) {
609
                        char * p2;
610
                        strncpy(path_copy, path, sizeof(path_copy - 1));
611
                        if (p2 = strchr(path_copy, ':')) {
612
                                *p2 = '\0';
613
                        }
614
 
615
                        if (strlen(path_copy))
616
                                strncat(path_copy, "/", sizeof(path_copy));
617
                        strncat(path_copy, argv[0], sizeof(path_copy));
618
 
619
                        if (!stat(path_copy, &stat_buf) && (stat_buf.st_mode & 0111)) {
620
                                newargv[0] = path_copy;
621
                                break;
622
                        }
623
 
624
                        p2 = strchr(path, ':');
625
                        if (p2)
626
                                path = p2 + 1;
627
                        else
628
                                path = 0;
629
                }
630
        }
631
#endif
632
 
633
        /*
634
         * Now for each command argument, see if it is a wildcard, and if
635
         * so, replace the argument with the list of matching filenames.
636
         */
637
        newargc = 1;
638
        oac = 0;
639
 
640
        while (++oac < argc) {
641
                matches = expandwildcards(argv[oac], MAXARGS, nametable);
642
                if (matches < 0)
643
                        return;
644
 
645
                if ((newargc + matches) >= MAXARGS) {
646
                        fprintf(stderr, "Too many arguments\n");
647
                        return;
648
                }
649
 
650
                if (matches == 0)
651
                        newargv[newargc++] = argv[oac];
652
 
653
                for (i = 0; i < matches; i++)
654
                        newargv[newargc++] = nametable[i];
655
        }
656
 
657
        newargv[newargc] = 0;
658
 
659
        magic = FALSE;
660
 
661
        /*
662
        for (cp = cmd; *cp; cp++) {
663
                if ((*cp >= 'a') && (*cp <= 'z'))
664
                        continue;
665
                if ((*cp >= 'A') && (*cp <= 'Z'))
666
                        continue;
667
                if (isdecimal(*cp))
668
                        continue;
669
                if (isblank(*cp))
670
                        continue;
671
 
672
                if ((*cp == '.') || (*cp == '/') || (*cp == '-') ||
673
                        (*cp == '+') || (*cp == '=') || (*cp == '_') ||
674
                        (*cp == ':') || (*cp == ','))
675
                                continue;
676
 
677
                magic = TRUE;
678
        }
679
        */
680
 
681
        if (magic) {
682
                printf("%s: no such file or directory\n", cmd);
683
                system(cmd);
684
                return;
685
        }
686
 
687 708 simons
        if (!bg) {
688 199 simons
                signal(SIGCHLD, SIG_DFL);
689 708 simons
        }
690 199 simons
 
691
        /*
692
         * No magic characters in the expanded command, so do the fork and
693
         * exec ourself.  If this fails with ENOEXEC, then run the
694
         * shell anyway since it might be a shell script.
695
         */
696
        if (!(pid = vfork())) {
697
                int     ci;
698
 
699
                /*
700
                 * We are the child, so run the program.
701
                 * First close any extra file descriptors we have opened.
702
                 * be sure not to modify any globals after the vfork !
703
                 */
704
 
705
                for (ci = 0; ci < sourcecount; ci++)
706
                        if (sourcefiles[ci] != stdin)
707
                                close(fileno(sourcefiles[ci]));
708
 
709
                signal(SIGINT, SIG_DFL);
710
                signal(SIGQUIT, SIG_DFL);
711
                signal(SIGCHLD, SIG_DFL);
712
 
713
                execvp(newargv[0], newargv);
714
 
715
                printf("%s: %s\n", newargv[0], (errno == ENOENT) ? "Bad command or file name" : strerror(errno));
716
 
717
                _exit(0);
718
        }
719
 
720
        if (pid < 0) {
721
                __signal(SIGCHLD, catchchild, SA_RESTART);
722
                perror("vfork failed");
723
                return;
724
        }
725
 
726
        if (bg) {
727
                printf("[%d]\n", pid);
728
                return;
729
        }
730
 
731
        if (pid) {
732
                int cpid;
733
                status = 0;
734
                intcrlf = FALSE;
735
 
736
                for (;;) {
737
                        cpid = wait4(pid, &status, 0, 0);
738
                        if ((cpid < 0) && (errno == EINTR))
739
                                continue;
740
                        if (cpid < 0)
741
                                break;
742
                        if (cpid != pid) {
743
                                fprintf(stderr, "sh %d: child %d died\n", getpid(), cpid);
744
                                continue;
745
                        }
746
                }
747
 
748
                __signal(SIGCHLD, catchchild, SA_RESTART);
749
 
750
                intcrlf = TRUE;
751
                if ((status & 0xff) == 0)
752
                        return;
753
 
754
                fprintf(stderr, "pid %d: %s (signal %d)\n", pid,
755
                        (status & 0x80) ? "core dumped" : "killed",
756
                        status & 0x7f);
757
                fflush(stderr);
758
 
759
                return;
760
        }
761
 
762
        perror(argv[0]);
763
        exit(1);
764
}
765
 
766
#ifdef CMD_HELP
767
void
768
do_help(argc, argv)
769
        char    **argv;
770
{
771
        CMDTAB  *cmdptr;
772
 
773
        for (cmdptr = cmdtab; cmdptr->name && cmdptr->name[0]; cmdptr++)
774
                printf("%-10s %s\n", cmdptr->name, cmdptr->usage);
775
}
776
#endif /* CMD_HELP */
777
 
778
#ifdef CMD_ALIAS
779
void
780
do_alias(argc, argv)
781
        char    **argv;
782
{
783
        char    *name;
784
        char    *value;
785
        ALIAS   *alias;
786
        int     count;
787
        char    buf[CMDLEN];
788
 
789
        if (argc < 2) {
790
                count = aliascount;
791
                for (alias = aliastable; count-- > 0; alias++)
792
                        printf("%s\t%s\n", alias->name, alias->value);
793
                return;
794
        }
795
 
796
        name = argv[1];
797
        if (argc == 2) {
798
                alias = findalias(name);
799
                if (alias)
800
                        printf("%s\n", alias->value);
801
                else
802
                        fprintf(stderr, "Alias \"%s\" is not defined\n", name);
803
                return;
804
        }
805
 
806
        if (strcmp(name, "alias") == 0) {
807
                fprintf(stderr, "Cannot alias \"alias\"\n");
808
                return;
809
        }
810
 
811
        if (!makestring(argc - 2, argv + 2, buf, CMDLEN))
812
                return;
813
 
814
        value = malloc(strlen(buf) + 1);
815
 
816
        if (value == NULL) {
817
                fprintf(stderr, "No memory for alias value\n");
818
                return;
819
        }
820
 
821
        strcpy(value, buf);
822
 
823
        alias = findalias(name);
824
        if (alias) {
825
                free(alias->value);
826
                alias->value = value;
827
                return;
828
        }
829
 
830
        if ((aliascount % ALIASALLOC) == 0) {
831
                count = aliascount + ALIASALLOC;
832
 
833
                if (aliastable)
834
                        alias = (ALIAS *) realloc(aliastable,
835
                                sizeof(ALIAS *) * count);
836
                else
837
                        alias = (ALIAS *) malloc(sizeof(ALIAS *) * count);
838
 
839
                if (alias == NULL) {
840
                        free(value);
841
                        fprintf(stderr, "No memory for alias table\n");
842
                        return;
843
                }
844
 
845
                aliastable = alias;
846
        }
847
 
848
        alias = &aliastable[aliascount];
849
 
850
        alias->name = malloc(strlen(name) + 1);
851
 
852
        if (alias->name == NULL) {
853
                free(value);
854
                fprintf(stderr, "No memory for alias name\n");
855
                return;
856
        }
857
 
858
        strcpy(alias->name, name);
859
        alias->value = value;
860
        aliascount++;
861
}
862
#endif /* CMD_ALIAS */
863
 
864
/*
865
 * Look up an alias name, and return a pointer to it.
866
 * Returns NULL if the name does not exist.
867
 */
868
static ALIAS *
869
findalias(name)
870
        char    *name;
871
{
872
        ALIAS   *alias;
873
        int     count;
874
 
875
        count = aliascount;
876
        for (alias = aliastable; count-- > 0; alias++) {
877
                if (strcmp(name, alias->name) == 0)
878
                        return alias;
879
        }
880
 
881
        return NULL;
882
}
883
 
884
 
885
void
886
do_source(argc, argv)
887
        char    **argv;
888
{
889
        readfile(argv[1]);
890
}
891
 
892
/*void
893
do_cd(argc, argv)
894
        char    **argv;
895
{
896
        char    *name;
897
 
898
        name = argv[1];
899
 
900
        if (chdir(name))
901
                perror("Unable to chdir to %s");
902
 
903
}*/
904
 
905
void
906
do_pid(argc, argv)
907
{
908
        printf("%d\n", getpid());
909
}
910
 
911
void
912
do_exec(argc, argv)
913
        char    **argv;
914
{
915
        char    *name;
916
 
917
        name = argv[1];
918
 
919
        if (access(name, 4)) {
920
                perror(name);
921
                return;
922
        }
923
 
924
        while (--sourcecount >= 0) {
925
                if (sourcefiles[sourcecount] != stdin)
926
                        fclose(sourcefiles[sourcecount]);
927
        }
928
 
929
        argv[0] = name;
930
        argv[1] = NULL;
931
 
932
        execv(name, argv);
933
 
934
        perror(name);
935
        exit(1);
936
}
937
 
938
/*void
939
do_exit(argc, argv)
940
        char    **argv;
941
{
942
        if (argc>1)
943
                exit(atoi(argv[1]));
944
        else
945
                exit(0);
946
}*/
947
 
948
 
949
#ifdef CMD_ALIAS
950
void
951
do_unalias(argc, argv)
952
        char    **argv;
953
{
954
        ALIAS   *alias;
955
 
956
        while (--argc > 0) {
957
                alias = findalias(*++argv);
958
                if (alias == NULL)
959
                        continue;
960
 
961
                free(alias->name);
962
                free(alias->value);
963
                aliascount--;
964
                alias->name = aliastable[aliascount].name;
965
                alias->value = aliastable[aliascount].value;
966
        }
967
}
968
#endif /* CMD_ALIAS */
969
 
970
/*
971
 * Display the prompt string.
972
 */
973
static void
974
showprompt()
975
{
976
        char    *cp;
977
        //;'pa990523 changed from 6...
978
        char buf[60];
979
 
980
#if 0
981
        sprintf(buf, "%d", getpid());
982
        write(STDOUT, buf, strlen(buf));
983
#endif
984
 
985
        *buf = '\0';
986
        getcwd(buf, sizeof(buf) - 1);
987
        strncat(buf, "> ", sizeof(buf) - 1);
988
        buf[sizeof(buf) - 1] = '\0';
989
        write(STDOUT, buf, strlen(buf));
990
}
991
 
992
 
993
static void
994
catchint()
995
{
996
        signal(SIGINT, catchint);
997
 
998
        intflag = TRUE;
999
 
1000
        if (intcrlf)
1001
                write(STDOUT, "\n", 1);
1002
}
1003
 
1004
 
1005
static void
1006
catchquit()
1007
{
1008
        signal(SIGQUIT, catchquit);
1009
 
1010
        intflag = TRUE;
1011
 
1012
        if (intcrlf)
1013
                write(STDOUT, "\n", 1);
1014
}
1015
 
1016
static void
1017
catchchild()
1018
{
1019
        char buf[40];
1020
        pid_t pid;
1021
        int status;
1022
 
1023
        /*signal(SIGCHLD, catchchild);*/ /* Unneeded */
1024
 
1025
        pid = wait4(-1, &status, WUNTRACED, 0);
1026
        if (WIFSTOPPED(status))
1027
                sprintf(buf, "sh %d: Child %d stopped\n", getpid(), pid);
1028
        else
1029
                sprintf(buf, "sh %d: Child %d died\n", getpid(), pid);
1030
 
1031
        intflag = TRUE;
1032
 
1033
        if (intcrlf)
1034
                write(STDOUT, "\n", 1);
1035
 
1036
        write(STDOUT, buf, strlen(buf));
1037
}
1038
 
1039
/* END CODE */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.