Changeset 1198 for trunk/src/kash/main.c


Ignore:
Timestamp:
Oct 6, 2007, 11:19:19 PM (18 years ago)
Author:
bird
Message:

moving globals into shinstance...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/main.c

    r884 r1198  
    8484/*int rootpid;
    8585int rootshell;*/
     86#ifdef unused_variables
    8687STATIC union node *curcmd;
    8788STATIC union node *prevcmd;
     89#endif
    8890
    8991STATIC void read_profile(struct shinstance *, const char *);
     
    9496extern void init_syntax(void);
    9597#endif
     98STATIC int usage(const char *argv0);
     99STATIC int version(const char *argv0);
    96100
    97101/*
     
    109113
    110114        /*
    111          * Global initializations. 
    112         */
     115         * Global initializations.
     116        */
    113117        setlocale(LC_ALL, "");
    114118#ifdef _MSC_VER
    115119        init_syntax();
    116120#endif
     121
     122        /*
     123         * Check for --version and --help.
     124     */
     125        if (argc > 1 && argv[1][0] == '-' && argv[1][1] == '-') {
     126                if (!strcmp(argv[1], "--help"))
     127                        return usage(argv[0]);
     128                if (!strcmp(argv[1], "--version"))
     129                        return version(argv[0]);
     130        }
     131
    117132        /*
    118133         * Create the root shell instance.
     
    123138        shthread_set_shell(psh);
    124139        return shell_main(psh, argc, argv);
    125 }       
    126 
    127 int 
     140}
     141
     142int
    128143shell_main(shinstance *psh, int argc, char **argv)
    129144{
     
    161176
    162177                if (psh->exception != EXSHELLPROC) {
    163                         if (state == 0 || psh->iflag == 0 || ! psh->rootshell)
     178                        if (state == 0 || iflag(psh) == 0 || ! psh->rootshell)
    164179                                exitshell(psh, psh->exitstatus);
    165180                }
     
    189204#ifdef DEBUG
    190205#if DEBUG == 2
    191         debug = 1;
     206        debug(psh) = 1;
    192207#endif
    193208        opentrace(psh);
     
    215230state3:
    216231        state = 4;
    217         if (psh->sflag == 0 || psh->minusc) {
     232        if (sflag(psh) == 0 || psh->minusc) {
    218233                static int sigs[] =  {
    219234                    SIGINT, SIGQUIT, SIGHUP,
     
    233248                evalstring(psh, psh->minusc, 0);
    234249
    235         if (psh->sflag || psh->minusc == NULL) {
     250        if (sflag(psh) || psh->minusc == NULL) {
    236251state4: /* XXX ??? - why isn't this before the "if" statement */
    237252                cmdloop(psh, 1);
     
    262277                        dotrap(psh);
    263278                inter = 0;
    264                 if (psh->iflag && top) {
     279                if (iflag(psh) && top) {
    265280                        inter = 1;
    266281                        showjobs(psh, psh->out2, SHOW_CHANGED);
     
    274289                                break;
    275290                        if (!stoppedjobs(psh)) {
    276                                 if (!psh->Iflag)
     291                                if (!Iflag(psh))
    277292                                        break;
    278293                                out2str(psh, "\nUse \"exit\" to leave shell.\n");
    279294                        }
    280295                        numeof++;
    281                 } else if (n != NULL && psh->nflag == 0) {
     296                } else if (n != NULL && nflag(psh) == 0) {
    282297                        psh->job_warning = (psh->job_warning == 2) ? 1 : 0;
    283298                        numeof = 0;
     
    314329                return;
    315330        /* -q turns off -x and -v just when executing init files */
    316         if (psh->qflag)  {
    317             if (psh->xflag)
    318                     psh->xflag = 0, xflag_set = 1;
    319             if (psh->vflag)
    320                     psh->vflag = 0, vflag_set = 1;
     331        if (qflag(psh))  {
     332            if (xflag(psh))
     333                    xflag(psh) = 0, xflag_set = 1;
     334            if (vflag(psh))
     335                    vflag(psh) = 0, vflag_set = 1;
    321336        }
    322337        cmdloop(psh, 0);
    323         if (psh->qflag)  {
     338        if (qflag(psh))  {
    324339            if (xflag_set)
    325                     psh->xflag = 1;
     340                    xflag(psh) = 1;
    326341            if (vflag_set)
    327                     psh->vflag = 1;
     342                    vflag(psh) = 1;
    328343        }
    329344        popfile(psh);
     
    414429                return 0;
    415430        if (argc > 1)
    416                 psh->exitstatus = number(argv[1]);
     431                psh->exitstatus = number(psh, argv[1]);
    417432        exitshell(psh, psh->exitstatus);
    418433        /* NOTREACHED */
     
    420435}
    421436
     437
     438STATIC const char *
     439strip_argv0(const char *argv0, size_t *lenp)
     440{
     441        const char *tmp;
     442
     443        /* skip the path */
     444        for (tmp = strpbrk(argv0, "\\/:"); tmp; tmp = strpbrk(argv0, "\\/:"))
     445                argv0 = tmp + 1;
     446
     447        /* find the end, ignoring extenions */
     448        tmp = strrchr(argv0, '.');
     449        if (!tmp)
     450                tmp = strchr(argv0, '\0');
     451        *lenp = tmp - argv0;
     452        return argv0;
     453}
     454
     455STATIC int
     456usage(const char *argv0)
     457{
     458        size_t len;
     459        strip_argv0(argv0, &len);
     460
     461        fprintf(stdout,
     462                        "usage: %.*s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
     463                    "               [+o option_name] [command_file [argument ...]]\n"
     464                    "   or: %.*s -c [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
     465                    "               [+o option_name] command_string [command_name [argument ...]]\n"
     466                    "   or: %.*s -s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
     467                    "               [+o option_name] [argument ...]\n"
     468                    "   or: %.*s --help\n"
     469                    "   or: %.*s --version\n",
     470                    len, argv0, len, argv0, len, argv0, len, argv0, len, argv0);
     471        return 0;
     472}
     473
     474STATIC int
     475version(const char *argv0)
     476{
     477        size_t len;
     478        strip_argv0(argv0, &len);
     479
     480        fprintf(stdout,
     481                        "%.*s - kBuild version %d.%d.%d\n",
     482                    len, argv0,
     483                    KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
     484        return 0;
     485}
     486
     487
    422488/*
    423489 * Local Variables:
Note: See TracChangeset for help on using the changeset viewer.