[BACK]Return to fep_com.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / fep

Annotation of OpenXM_contrib2/fep/fep_com.c, Revision 1.1

1.1     ! noro        1: /*     Copyright (c) 1987, 1988 by Software Research Associates, Inc.  */
        !             2:
        !             3: #ifndef lint
        !             4: static char rcsid[]=
        !             5: "$Header: fep_com.c,v 4.9 91/05/29 14:30:43 utashiro Exp $ (SRA)";
        !             6: #endif /* lint */
        !             7:
        !             8: #include <stdio.h>
        !             9: #include <sys/ioctl.h>
        !            10: #ifdef TERMIOS
        !            11: #include <termios.h>
        !            12: #if defined(__linux__) || defined(__CYGWIN__)
        !            13: #ifndef _POSIX_VDISABLE
        !            14: #define _POSIX_VDISABLE '\0'
        !            15: #endif
        !            16: #endif
        !            17: #else
        !            18: #include <sgtty.h>
        !            19: #endif
        !            20: #include <ctype.h>
        !            21: #include <sys/param.h>
        !            22: #include <sys/file.h>
        !            23: #include <sys/stat.h>
        !            24: #include <sys/ioctl.h>
        !            25: #include "fep_defs.h"
        !            26: #include "fep_glob.h"
        !            27: #include "fep_funcs.h"
        !            28:
        !            29: int    tty_fix_bell = OFF;     /* ring bell if the tty mode is changed */
        !            30:
        !            31: typedef struct {
        !            32:        int cur_line;
        !            33:        int max_line;
        !            34: } MORE;
        !            35: MORE *create_more();
        !            36:
        !            37: /*
        !            38:  * Check command line if it call built-in function or not and execute it
        !            39:  */
        !            40: executeBuiltInFunction (comline, more)
        !            41:     char *comline, **more;
        !            42: {
        !            43:     register FunctionTableEnt *ftp;
        !            44:     char linebuf[MAXCMDLEN], *line;
        !            45:     char *search_string();
        !            46:     int argc;
        !            47:
        !            48:     /*
        !            49:      * Skip white space.
        !            50:      */
        !            51:     while (isspace (*comline))
        !            52:        comline++;
        !            53:
        !            54:     line = linebuf;
        !            55:     strcpy (line, comline);
        !            56:
        !            57:     if (more) {
        !            58:        if (*more = search_string (comline, ";")) {
        !            59:            *(line + (*more - comline)) = '\0';
        !            60:            do {
        !            61:                *more += 1;
        !            62:            } while (**more == ';');
        !            63:        }
        !            64:        else {
        !            65:            *more = (char*) 0;
        !            66:        }
        !            67:     }
        !            68:
        !            69:     /*
        !            70:      * Skip comment and blank line
        !            71:      */
        !            72:     if (*line == '#' || *line == '\0')
        !            73:        return (IGNORED);
        !            74:
        !            75:     /*
        !            76:      * All built-in command should be prefixed by 'fep-'
        !            77:      */
        !            78:     if (strncmp (line, "fep-", 4) != 0)
        !            79:        return (NOT_PROCESSED);
        !            80:
        !            81:     for (ftp = BuiltinFuncTable; ftp->name; ftp++) {
        !            82:        if (is_same_command (line, ftp->name)) {
        !            83:            if (debug)
        !            84:                showArgs (line);
        !            85:            if (
        !            86:                condition()
        !            87:                /* control structure should be processed on any condition */
        !            88:                || strncmp (line + 4, "if", 2) == 0
        !            89:                || strncmp (line + 4, "endif", 5) == 0
        !            90:                || strncmp (line + 4, "elseif", 6) == 0
        !            91:                || strncmp (line + 4, "else", 4) == 0
        !            92:            )
        !            93:                (*ftp->func)(line);
        !            94:            return (PROCESSED);
        !            95:        }
        !            96:     }
        !            97:     return (NOT_PROCESSED);
        !            98: }
        !            99:
        !           100: is_same_command (a, b)
        !           101:     register char *a, *b;
        !           102: {
        !           103:
        !           104:     while (*a && *b && *a == *b)
        !           105:        ++a, ++b;
        !           106:     if ((*a == '\0' || isspace ((unsigned char)*a)) && (*b == '\0' || isspace ((unsigned char)*b)))
        !           107:        return 1;
        !           108:     else
        !           109:        return 0;
        !           110: }
        !           111:
        !           112: /*
        !           113:  * Process 'fep-if' and 'fep-elseif'
        !           114:  */
        !           115: fep_if (comline)
        !           116:     char *comline;
        !           117: {
        !           118:     char *argv[MAXARGS];
        !           119:     int argc;
        !           120:     char *err;
        !           121:     int cond;
        !           122:     int i;
        !           123:
        !           124:     argc = mkargv (comline, argv, MAXARGS);
        !           125:
        !           126:     if (argc != 2 && argc != 4) {
        !           127:        printf ("%s: Illegal number of arguments\n", argv[0]);
        !           128:        return;
        !           129:     }
        !           130:
        !           131:     /*
        !           132:      * In case of only one argument,
        !           133:      * treat as true if the variable is set.
        !           134:      */
        !           135:     if (argc == 2) {
        !           136:        char *cp;
        !           137:
        !           138:        if (argv[1][0] == '$')
        !           139:            cp = &argv[1][1];
        !           140:        else
        !           141:            cp = &argv[1][0];
        !           142:
        !           143:        cond = look_var (cp) ? 1 : 0;
        !           144:     }
        !           145:     else {
        !           146:        int value;
        !           147:
        !           148:        /*
        !           149:         * Substitute variable prefixed by '$' mark.
        !           150:         */
        !           151:        for (i = 0; i < argc; i++) {
        !           152:            if (argv[i][0] == '$') {
        !           153:                char *v;
        !           154:
        !           155:                if (v = look_var (&argv[i][1]))
        !           156:                    argv[i] = v;
        !           157:                else
        !           158:                    argv[i] = "";
        !           159:            }
        !           160:        }
        !           161:
        !           162:        if (debug) {
        !           163:            int i;
        !           164:            char **argp;
        !           165:
        !           166:            for (i = 0, argp = argv; *argp; argp++, i++)
        !           167:                printf ("argv[%d] = \"%s\"\n", i, *argp);
        !           168:        }
        !           169:
        !           170:         /*
        !           171:         * Check operator.
        !           172:         */
        !           173:        if (eq (argv[2], "==") || eq (argv[2], "="))
        !           174:            value = 1;
        !           175:        else if (eq (argv[2], "!="))
        !           176:            value = 0;
        !           177:        else {
        !           178:            printf ("%s: Unknown opperator \"%s\"", argv[0], argv[2]);
        !           179:            return;
        !           180:        }
        !           181:
        !           182:        if (eq (argv[1], argv[3]))
        !           183:            cond = value;
        !           184:        else
        !           185:            cond = !value;
        !           186:     }
        !           187:
        !           188:     if (eq (argv[0], "fep-elseif"))
        !           189:        err = change_condition (cond);
        !           190:     else
        !           191:        err = push_condition (cond);
        !           192:
        !           193:     if (err)
        !           194:        printf ("%s: %s", argv[0], err);
        !           195:     else if (debug)
        !           196:        printf ("%s: %s\n", comline, cond ? "TRUE" : "FALSE");
        !           197:
        !           198:     return;
        !           199: }
        !           200:
        !           201: fep_else ()
        !           202: {
        !           203:     char *err;
        !           204:
        !           205:     if (err = change_condition (1))
        !           206:        printf ("fep-else: %s", err);
        !           207:
        !           208:     return;
        !           209: }
        !           210:
        !           211: fep_endif ()
        !           212: {
        !           213:     char *err;
        !           214:
        !           215:     if (err = pop_condition ())
        !           216:        printf ("fep-endif: %s", err);
        !           217:
        !           218:     return;
        !           219: }
        !           220:
        !           221: bind_to_key (comline)
        !           222:     char *comline;
        !           223: {
        !           224:     register FunctionTableEnt *fnte;
        !           225:     char *argv[MAXARGS];
        !           226:     int argc;
        !           227:
        !           228:     argc = mkargv (comline, argv, MAXARGS);
        !           229:
        !           230:     /*
        !           231:      * Something error occured. Print message and return
        !           232:      */
        !           233:     if (argc < 0) {
        !           234:        printf ("%s\n", argv[0]);
        !           235:        return;
        !           236:     }
        !           237:
        !           238:     /*
        !           239:      * Number of arguments should be three.
        !           240:      */
        !           241:     if (argc != 3) {
        !           242:        printf ("Invalid number of arguments\n");
        !           243:        return;
        !           244:     }
        !           245:
        !           246:     /*
        !           247:      * Find the function name from function name table.
        !           248:      */
        !           249:     for (fnte = FunctionNameTable; fnte->func; fnte++) {
        !           250:        if (strcmp (fnte->name, argv[1]) == 0) {
        !           251:            bind_key (curFuncTab, fnte->func, argv[2], fep_abort);
        !           252:            break;
        !           253:        }
        !           254:     }
        !           255:
        !           256:     /*
        !           257:      * Couldn't find such a function
        !           258:      */
        !           259:     if (fnte->func == NULL)
        !           260:        printf ("%s: no such built-in command\n", argv[1]);
        !           261: }
        !           262:
        !           263: alias(comline)
        !           264:     char *comline;
        !           265: {
        !           266:     char *argv[MAXARGS];
        !           267:     int argc;
        !           268:
        !           269:     argc = mkargv (comline, argv, MAXARGS);
        !           270:
        !           271:     switch (argc) {
        !           272:        case 1:
        !           273:            show_aliaslist (NULL);
        !           274:            break;
        !           275:
        !           276:        case 2:
        !           277:            show_aliaslist (argv[1]);
        !           278:            break;
        !           279:
        !           280:        case 3:
        !           281:            set_alias (argv[1], argv[2]);
        !           282:            break;
        !           283:
        !           284:        default:
        !           285:            printf ("%s: Illegal number of arguments.\n", argv[0]);
        !           286:     }
        !           287:     return;
        !           288: }
        !           289:
        !           290: unalias (comline)
        !           291:     char *comline;
        !           292: {
        !           293:     char *argv[MAXARGS];
        !           294:     int argc;
        !           295:     int i;
        !           296:
        !           297:     argc = mkargv (comline, argv, MAXARGS);
        !           298:
        !           299:     for (i=1; i<argc; i++)
        !           300:        unset_alias (argv[i]);
        !           301:
        !           302:     return;
        !           303: }
        !           304:
        !           305: set (comline)
        !           306:     char *comline;
        !           307: {
        !           308:     char line[MAXCMDLEN];
        !           309:     char *cp, *index();
        !           310:     char *argv[MAXARGS];
        !           311:     int argc;
        !           312:
        !           313:     /*
        !           314:      * If there is '=' character in command line, change it to space.
        !           315:      */
        !           316:     strcpy (line, comline);
        !           317:     if (cp = index (line, '='))
        !           318:        *cp = ' ';
        !           319:
        !           320:     argc = mkargv (line, argv, MAXARGS);
        !           321:
        !           322:     switch (argc) {
        !           323:
        !           324:        /* set */
        !           325:        case 1:
        !           326:            show_varlist ();
        !           327:            return;
        !           328:
        !           329:        /* set var */
        !           330:        case 2:
        !           331:            set_var (argv[1], "");
        !           332:            return;
        !           333:
        !           334:        /* set var = val */
        !           335:        case 3:
        !           336:            set_var (argv[1], argv[2]);
        !           337:            break;
        !           338:
        !           339:        default:
        !           340:            printf ("Invalid number of arguments\n");
        !           341:            return;
        !           342:     }
        !           343: }
        !           344:
        !           345: unset(comline)
        !           346:     char *comline;
        !           347: {
        !           348:     char **vp;
        !           349:     char *argv[MAXARGS];
        !           350:     int argc;
        !           351:
        !           352:     argc = mkargv (comline, argv, MAXARGS);
        !           353:
        !           354:     if (argc < 2) {
        !           355:        printf ("Invalid number of arguments\n");
        !           356:        return;
        !           357:     }
        !           358:
        !           359:     for (vp = &argv[1]; *vp; vp++)
        !           360:        unset_var (*vp);
        !           361: }
        !           362:
        !           363: extern int Transparency;
        !           364: extern int Through;
        !           365: #ifdef TERMIOS
        !           366: #define ttystruct termios
        !           367: #elif defined(TIOCSETN)
        !           368: #define ttystruct sgttyb
        !           369: #endif
        !           370: struct ttystruct master_ttymode;               /* master tty mode */
        !           371: struct ttystruct slave_ttymode;                /* slave tty mode */
        !           372: extern int master, slave;
        !           373: extern char slave_tty[];
        !           374:
        !           375: /*
        !           376:  * Toggle transparency switch.
        !           377:  * If variable Transparency is ON, fep doesn't care about input.
        !           378:  * If OFF, line editing will be done by fep.
        !           379:  * But this Transparency is set automaticaly by getcharacter() routine,
        !           380:  * if the variable auto-tty-fix is ON.
        !           381:  */
        !           382: toggle_through()
        !           383: {
        !           384:     int r;
        !           385:     int slave_fd;
        !           386: #ifdef TERMIOS
        !           387:     struct termios s;
        !           388: #else
        !           389:     struct sgttyb s;
        !           390: #endif
        !           391:
        !           392:     if (Through == OFF) {
        !           393:
        !           394:        slave_fd = open (slave_tty, O_WRONLY);
        !           395:        if (slave_fd < 0) {
        !           396:            perror (slave_tty);
        !           397:            return;
        !           398:        }
        !           399:
        !           400: #ifdef TERMIOS
        !           401:        r = tcgetattr(slave_fd, &s);
        !           402: #else
        !           403:        r = ioctl (slave_fd, TIOCGETP, (char *) &s);
        !           404: #endif
        !           405:        if (r < 0) {
        !           406:            perror (slave_tty);
        !           407:            (void) close (slave_fd);
        !           408:            return;
        !           409:        }
        !           410:
        !           411: #ifdef TERMIOS
        !           412:        s.c_lflag &= ~(ICANON);
        !           413:        s.c_cc[VMIN] = 1;
        !           414:        s.c_cc[VTIME] = 0;
        !           415:        r = tcsetattr(0, TCSANOW, &s);
        !           416: #else
        !           417:        s.sg_flags |= CBREAK;
        !           418:        r = ioctl (0, TIOCSETN, (char *) & s);
        !           419: #endif
        !           420:        if (r < 0) {
        !           421:            perror (slave_tty);
        !           422:            (void) close (slave_fd);
        !           423:        }
        !           424:        (void) close (slave_fd);
        !           425:     }
        !           426:     else
        !           427: #ifdef TERMIOS
        !           428:        r = tcsetattr(0, TCSANOW, & master_ttymode);
        !           429: #else
        !           430:        r = ioctl (0, TIOCSETN, (char *) & master_ttymode);
        !           431: #endif
        !           432:
        !           433:     if (r < 0) {
        !           434:        printf ("Can't change pty mode.\n");
        !           435:        return;
        !           436:     }
        !           437:
        !           438:     Through = !Through;
        !           439: }
        !           440:
        !           441: /*
        !           442:  * Check tty mode of slave tty and fix stdout tty mode
        !           443:  */
        !           444: fix_transparency()
        !           445: {
        !           446:     int r;
        !           447: #ifdef TERMIOS
        !           448:     struct termios s;
        !           449: #else
        !           450:     struct sgttyb s;
        !           451: #endif
        !           452:
        !           453:     if (Through)
        !           454:        return;
        !           455:
        !           456:     if (slave < 0)
        !           457:        return;
        !           458:
        !           459: #ifdef TERMIOS
        !           460:     r = tcgetattr(slave, &s);
        !           461:     s.c_iflag |= ICRNL;
        !           462:     s.c_oflag |= ONLCR;
        !           463: #else
        !           464:     r = ioctl (slave, TIOCGETP, (char *) &s);
        !           465:     /*
        !           466:      * slave CRMOD is off, but master should be.
        !           467:      */
        !           468:     s.sg_flags |= CRMOD;
        !           469: #endif
        !           470:     if (r < 0) {
        !           471:        perror (slave_tty);
        !           472:        return;
        !           473:     }
        !           474:
        !           475:     /*
        !           476:      * If find slave tty mode is cbreak or raw, fix tty mode of stdout to
        !           477:      * same mode as slave and set Transparency ON.
        !           478:      */
        !           479:
        !           480: #ifdef TERMIOS
        !           481:     if ((s.c_lflag & ICANON) == 0)
        !           482: #else
        !           483:     if (s.sg_flags & (CBREAK|RAW))
        !           484: #endif
        !           485:     {
        !           486:        if (Transparency == OFF) {
        !           487: #ifdef TERMIOS
        !           488:            r = tcsetattr(0, TCSANOW, & s);
        !           489: #else
        !           490:            r = ioctl (0, TIOCSETN, (char *) & s);
        !           491: #endif
        !           492:            if (r < 0) {
        !           493:                perror ("stdout");
        !           494:                return;
        !           495:            }
        !           496:            if (tty_fix_bell) errorBell ();
        !           497:            Transparency = ON;
        !           498:        }
        !           499:     }
        !           500:     else {
        !           501:        if (Transparency == ON) {
        !           502: #ifdef TERMIOS
        !           503:            r = tcsetattr(0, TCSANOW, & master_ttymode);
        !           504: #else
        !           505:            r = ioctl (0, TIOCSETN, (char *) & master_ttymode);
        !           506: #endif
        !           507:            if (r < 0) {
        !           508:                perror ("stdout");
        !           509:                return;
        !           510:            }
        !           511:            if (tty_fix_bell) errorBell ();
        !           512:            Transparency = OFF;
        !           513:        }
        !           514:     }
        !           515:
        !           516:     if (r < 0) {
        !           517:        printf ("Can't change pty mode.\n");
        !           518:        return;
        !           519:     }
        !           520: }
        !           521:
        !           522: putch (c)
        !           523:     int c;
        !           524: {
        !           525:     putchar (c);
        !           526:     fflush (stdout);
        !           527: }
        !           528:
        !           529: /*
        !           530: int crt, sline;
        !           531: */
        !           532:
        !           533: show_bindings ()
        !           534: {
        !           535:     MORE *m;
        !           536:
        !           537:     m = create_more(lines);
        !           538:     if (!m) {
        !           539:        errorBell ();
        !           540:        return (0);
        !           541:     }
        !           542:
        !           543:     clear_edit_line ();
        !           544:     (void) showBindingTbl (m, curFuncTab, "");
        !           545:     recover_edit_line (1);
        !           546:
        !           547:     destroy_more(m);
        !           548:     return (0);
        !           549: }
        !           550:
        !           551: showBindingTbl (m, ft, prefix)
        !           552:     MORE *m;
        !           553:     FUNC ft[];
        !           554:     char *prefix;
        !           555: {
        !           556:     register FunctionTableEnt *fnte;
        !           557:     register int i;
        !           558:
        !           559:     for (i = 0; i < 128; i++) {
        !           560:        if (ft[i] == self_insert || ft[i] == fep_abort)
        !           561:            continue;
        !           562:
        !           563:        /*
        !           564:         * If the pointer to function has indirect flag print "indirect".
        !           565:         */
        !           566:        if (isIndirect(ft[i])) {
        !           567:            char pf[64];
        !           568:
        !           569:            sprintf (pf, "%s%s%c-",
        !           570:                prefix, (i == 0 || isctlchar(i)) ? "^" : "", unctl(i));
        !           571:            if (showBindingTbl (m, maskIndirect(ft[i]), pf) == 0)
        !           572:                break;
        !           573:            continue;
        !           574:        }
        !           575:
        !           576:        /*
        !           577:         * Search function name table
        !           578:         */
        !           579:        for (fnte = FunctionNameTable; fnte->func; fnte++) {
        !           580:            if (ft[i] == fnte->func) {
        !           581:
        !           582:                if (!more(m))
        !           583:                    return (0);
        !           584:
        !           585:                /*
        !           586:                 * Show binding
        !           587:                 */
        !           588:                printf ("%s%s%c\t%s\n",
        !           589:                        prefix,
        !           590:                        i == 0 || isctlchar(i) ? "^" : "",
        !           591:                        unctl(i),
        !           592:                        fnte->name
        !           593:                        );
        !           594:                break;
        !           595:            }
        !           596:        }
        !           597:
        !           598:        /*
        !           599:         * Can't find such a function
        !           600:         */
        !           601:        if (fnte->func == NULL)
        !           602:            printf (
        !           603:                "%s%c\tunknown function (0x%x)\n",
        !           604:                i == 0 || isctlchar(i) ? "^" : "",
        !           605:                unctl(i),
        !           606:                ft[i]
        !           607:            );
        !           608:     }
        !           609:     return (1);
        !           610: }
        !           611:
        !           612: show_help ()
        !           613: {
        !           614:     MORE *m;
        !           615:
        !           616:     m = create_more(lines);
        !           617:     if (m == 0) {
        !           618:        errorBell ();
        !           619:        return;
        !           620:     }
        !           621:
        !           622:     clear_edit_line ();
        !           623:
        !           624:     more(m)                            &&
        !           625:     (printf ("Functions:\n") || 1)     &&
        !           626:     showTable (m, FunctionNameTable)   &&
        !           627:     more(m)                            &&
        !           628:     (printf ("Commands:\n") || 1)      &&
        !           629:     showTable (m, BuiltinFuncTable)    &&
        !           630:     more(m)                            &&
        !           631:     (printf ("Variables:\n") || 1)     &&
        !           632:     showVariables (m);
        !           633:
        !           634:     recover_edit_line (1);
        !           635: }
        !           636:
        !           637: showTable (m, fnte)
        !           638:     MORE *m;
        !           639:     FunctionTableEnt *fnte;
        !           640: {
        !           641:     int i;
        !           642:
        !           643:     /*
        !           644:      * Search function name table
        !           645:      */
        !           646:     for (; fnte->func; fnte++) {
        !           647:        if (!more(m))
        !           648:            return (0);
        !           649:        printf ("\t%-30s %s\n", fnte->name, fnte->help);
        !           650:     }
        !           651:
        !           652:     return (1);
        !           653: }
        !           654:
        !           655: showVariables (m)
        !           656:     MORE *m;
        !           657: {
        !           658:     extern VAR default_set_vars[], default_unset_vars[];
        !           659:     VAR *vp;
        !           660:
        !           661:     for (vp = default_set_vars; vp->v_name; ++vp) {
        !           662:        if (!vp->v_help)
        !           663:            continue;
        !           664:        if (!more(m))
        !           665:            return (0);
        !           666:        printf ("\t%-30s %s\n", vp->v_name, vp->v_help);
        !           667:     }
        !           668:
        !           669:     for (vp = default_unset_vars; vp->v_name; ++vp) {
        !           670:        if (!vp->v_help)
        !           671:            continue;
        !           672:        if (!more(m))
        !           673:            return (0);
        !           674:        printf ("\t%-30s %s\n", vp->v_name, vp->v_help);
        !           675:     }
        !           676:     return (1);
        !           677: }
        !           678:
        !           679: MORE *create_more(maxline)
        !           680:     int maxline;
        !           681: {
        !           682:     MORE *mp;
        !           683:
        !           684:     mp = (MORE *) malloc (sizeof (MORE));
        !           685:
        !           686:     if (mp == 0)
        !           687:        return ((MORE*)0);
        !           688:     else {
        !           689:        mp->cur_line = 0;
        !           690:        mp->max_line = maxline;
        !           691:        return (mp);
        !           692:     }
        !           693: }
        !           694:
        !           695: destroy_more(mp)
        !           696:     MORE *mp;
        !           697: {
        !           698:     if (mp)
        !           699:        free (mp);
        !           700: }
        !           701:
        !           702: more (mp)
        !           703:     MORE *mp;
        !           704: {
        !           705:
        !           706:     /*
        !           707:      * Print more message
        !           708:      */
        !           709: #   define PUTMORE     printf (  "--More--");
        !           710: #   define DELMORE     printf ("\r        \r");
        !           711:     if (mp->max_line && ++mp->cur_line >= mp->max_line) {
        !           712:
        !           713:        PUTMORE;
        !           714:        fflush (stdout);
        !           715:
        !           716:        switch (getcharacter()) {
        !           717:        case '\n': case '\r': case 'j':
        !           718:            --mp->cur_line;
        !           719:            break;
        !           720:
        !           721:        case 'd': case ctrl('D'):
        !           722:            mp->cur_line /= 2;
        !           723:            break;
        !           724:
        !           725:        case 'q': case 'Q': case ctrl('C'):
        !           726:            DELMORE;
        !           727:            return (0);
        !           728:
        !           729:        default:
        !           730:            mp->cur_line = 1;
        !           731:            break;
        !           732:        }
        !           733:        DELMORE;
        !           734:     }
        !           735:
        !           736:     if (mp->cur_line == 1 && look_var ("clear-repaint") && term_clear)
        !           737:        tputs (term_clear, 1, putch);
        !           738:
        !           739:     return (1);
        !           740: }
        !           741:
        !           742: /*
        !           743:  * Change directory
        !           744:  */
        !           745: fep_chdir (line)
        !           746: char *line;
        !           747: {
        !           748:     char *argv[MAXARGS];
        !           749:     int argc;
        !           750:
        !           751:     switch (mkargv (line, argv, MAXARGS)) {
        !           752:
        !           753:        /*
        !           754:         * Change directory with no arguments cause to chdir to home directory
        !           755:         */
        !           756:         case 1: {
        !           757:            char *home, *getenv();
        !           758:
        !           759:            if (home = getenv ("HOME"))
        !           760:                argv[1] = home;
        !           761:            else {
        !           762:                printf ("Where is your home directory?\n");
        !           763:                return;
        !           764:            }
        !           765:            break;
        !           766:        }
        !           767:
        !           768:        /*
        !           769:         * Change directory command with argument
        !           770:         */
        !           771:        case 2:
        !           772:            break;
        !           773:
        !           774:        /*
        !           775:         * Something error occured in mkargv.
        !           776:         */
        !           777:        case -1:
        !           778:            printf ("%s\n", argv[0]);
        !           779:            return;
        !           780:
        !           781:        default:
        !           782:            printf ("fep-chdir: Invalid number of arguments.\n");
        !           783:            return;
        !           784:     }
        !           785:
        !           786:     /*
        !           787:      * Chane directory.
        !           788:      * Keep in mind that end process still in old directory
        !           789:      */
        !           790:     if (chdir (argv[1]) < 0) {
        !           791:        perror (argv[1]);
        !           792:        return;
        !           793:     }
        !           794: }
        !           795:
        !           796: fep_pwd (line)
        !           797:     char *line;
        !           798: {
        !           799:     char cwd[MAXPATHLEN];
        !           800:
        !           801:     (void) getcwd (cwd, sizeof(cwd));
        !           802:     printf ("%s\n", cwd);
        !           803: }
        !           804:
        !           805: fep_echo (comline)
        !           806:     char *comline;
        !           807: {
        !           808:     char *argv[MAXARGS];
        !           809:     int argc;
        !           810:     char **argp;
        !           811:     int putnewline = 1, first;
        !           812:
        !           813:     argc = mkargv (comline, argv, MAXARGS);
        !           814:
        !           815:     argp = &argv[1];
        !           816:     if (*argp && strcmp (*argp, "-n") == 0) {
        !           817:        putnewline = 0;
        !           818:        ++argp;
        !           819:     }
        !           820:
        !           821:     for (first = 1; *argp; argp++) {
        !           822:        char *cp;
        !           823:
        !           824:        /*
        !           825:         * Put space
        !           826:         */
        !           827:        if (! first)
        !           828:            printf ("%s", " ");
        !           829:
        !           830:        /*
        !           831:         * Print argument
        !           832:         */
        !           833:        if (**argp == '$' && (cp = look_var (*argp + 1)))
        !           834:            printf ("%s", cp);
        !           835:        else
        !           836:            printf ("%s", *argp);
        !           837:
        !           838:        first = 0;
        !           839:     }
        !           840:
        !           841:     if (putnewline)
        !           842:        printf ("%c", '\n');
        !           843: }
        !           844:
        !           845: fep_command (comline)
        !           846:     char *comline;
        !           847: {
        !           848:     char *argv[MAXARGS];
        !           849:     int argc;
        !           850:     int i;
        !           851:     char **argp;
        !           852:     char buf[256];
        !           853:
        !           854:     argc = mkargv (comline, argv, MAXARGS);
        !           855:
        !           856:     if (argc == 1) {
        !           857:        printf ("Invalid number of arguments.\n");
        !           858:        return;
        !           859:     }
        !           860:
        !           861:     strcpy (buf, "");
        !           862:     for (i=1; i<argc; i++) {
        !           863:        strcat (buf, argv[i]);
        !           864:        strcat (buf, " ");
        !           865:     }
        !           866:
        !           867:     invoke_command (buf);
        !           868: }
        !           869:
        !           870: fep_source (comline)
        !           871:     char *comline;
        !           872: {
        !           873:     FILE *fp;
        !           874:     static char *argv[MAXARGS];
        !           875:     char file [MAXPATHLEN];
        !           876:     int argc;
        !           877:
        !           878:     argc = mkargv (comline, argv, MAXARGS);
        !           879:
        !           880:     if (argc != 2) {
        !           881:        printf ("Invalid number of arguments.\n");
        !           882:        return;
        !           883:     }
        !           884:
        !           885:     strcpy (file, argv[1]);
        !           886:     source_file (file);
        !           887:
        !           888:     return;
        !           889: }
        !           890:
        !           891: sourceRcFile ()
        !           892: {
        !           893:     char *home, filename[64], *getenv();
        !           894:     char line[256];
        !           895:     struct stat stb_home, stb_cur;
        !           896:
        !           897:     if (!(home = getenv ("HOME")))
        !           898:        return;
        !           899:
        !           900:     strcpy (filename, home);
        !           901:     strcat (filename, "/.feprc");
        !           902:
        !           903:     /*
        !           904:      * Source .feprc in home directory.
        !           905:      */
        !           906:     stb_home.st_ino = 0;
        !           907:     if (stat (filename, &stb_home) >= 0 && (stb_home.st_mode&S_IREAD))
        !           908:        source_file (filename);
        !           909:
        !           910:     /*
        !           911:      * Source .feprc in current directory.
        !           912:      */
        !           913:     if ((stat (".feprc", &stb_cur) >= 0 && stb_cur.st_ino != stb_home.st_ino))
        !           914:        source_file (".feprc");
        !           915:
        !           916:     return;
        !           917: }
        !           918:
        !           919: source_file (file)
        !           920:     char *file;
        !           921: {
        !           922:     FILE *fp;
        !           923:     char line[512], line2[512];
        !           924:     int i = 0;
        !           925:
        !           926:     if ((fp = fopen (file, "r")) == NULL) {
        !           927:        perror (file);
        !           928:        return;
        !           929:     }
        !           930:
        !           931:     while (fgets (line, 256, fp)) {
        !           932:        i++;
        !           933:        if (executeBuiltInFunction (line, 0) == NOT_PROCESSED) {
        !           934:            char *cp = line;
        !           935:
        !           936:            while (isspace (*cp))
        !           937:                cp++;
        !           938:            strcpy (line2, "fep-");
        !           939:            strcat (line2, cp);
        !           940:            if (executeBuiltInFunction (line2, 0) == NOT_PROCESSED) {
        !           941:                printf ("\"%s\", line %d: cannot be executed\n", file, i);
        !           942:                printf (">>> %s", line);
        !           943:            }
        !           944:        }
        !           945:     }
        !           946:
        !           947:     fclose (fp);
        !           948:     return;
        !           949:
        !           950: }
        !           951:
        !           952: #define MAX_IF_NEST 10
        !           953:
        !           954: #define CONDITION_MASK 0x00ff
        !           955: #define HAS_BEEN_TRUE  0x0100
        !           956:
        !           957: int condition_stack [MAX_IF_NEST] = {1};
        !           958: int current_if_stack = 0;
        !           959:
        !           960: condition ()
        !           961: {
        !           962:     int cond = 1, i;
        !           963:
        !           964:     if (current_if_stack == 0)
        !           965:        return (1);
        !           966:
        !           967:     for (i = 1; i <= current_if_stack; i++)
        !           968:        cond &= condition_stack [i];
        !           969:
        !           970:     return (cond & CONDITION_MASK);
        !           971: }
        !           972:
        !           973: char *
        !           974: change_condition (cond)
        !           975:     int cond;
        !           976: {
        !           977:     if (debug)
        !           978:        printf ("old=0x%x, new=0x%x\n",
        !           979:            condition_stack [current_if_stack], cond);
        !           980:     if (current_if_stack > 0) {
        !           981:        if (condition_stack [current_if_stack] & HAS_BEEN_TRUE)
        !           982:            cond = 0;
        !           983:        else if (cond != 0)
        !           984:            condition_stack [current_if_stack] |= HAS_BEEN_TRUE;
        !           985:
        !           986:        condition_stack [current_if_stack] &= ~CONDITION_MASK;
        !           987:        condition_stack [current_if_stack] |= cond;
        !           988:        return ((char *)0);
        !           989:     }
        !           990:     else
        !           991:        return ("Not in if close\n");
        !           992: }
        !           993:
        !           994: char *
        !           995: push_condition (cond)
        !           996:     int cond;
        !           997: {
        !           998:     if (current_if_stack < MAX_IF_NEST){
        !           999:        ++current_if_stack;
        !          1000:        condition_stack [current_if_stack] = cond;
        !          1001:        if (cond == 1)
        !          1002:            condition_stack [current_if_stack] |= HAS_BEEN_TRUE;
        !          1003:        return ((char*)0);
        !          1004:     }
        !          1005:     else
        !          1006:        return ("If stack over flow\n");
        !          1007: }
        !          1008:
        !          1009: char *
        !          1010: pop_condition ()
        !          1011: {
        !          1012:
        !          1013:     if (current_if_stack > 0) {
        !          1014:        --current_if_stack;
        !          1015:        return ((char*)0);
        !          1016:     }
        !          1017:     else
        !          1018:        return ("No more if stack\n");
        !          1019: }
        !          1020:
        !          1021: invoke_shell ()
        !          1022: {
        !          1023:     char *shell = "/bin/sh";
        !          1024:
        !          1025:     if (look_var ("shell"))
        !          1026:        shell = look_var ("shell");
        !          1027:
        !          1028:     invoke_command (shell);
        !          1029: }
        !          1030:
        !          1031: invoke_command (cmd)
        !          1032:     char *cmd;
        !          1033: {
        !          1034:     int catchsig();
        !          1035:     int (*func)();
        !          1036:
        !          1037:     clear_edit_line ();
        !          1038:     if (look_var ("verbose"))
        !          1039:        printf ("Invoke %s\n", cmd);
        !          1040:     fflush (stdout);
        !          1041:     recover_tty();
        !          1042:     recover_signal ();
        !          1043:     system (cmd);
        !          1044:     fix_signal ();
        !          1045:     fix_tty();
        !          1046:     if (look_var ("verbose"))
        !          1047:        printf ("Return to fep\n");
        !          1048:     recover_edit_line (1);
        !          1049: }
        !          1050:
        !          1051: FILE *redirect_fp = NULL;
        !          1052: int redirect_line = 0;
        !          1053:
        !          1054: fep_read_from_file (comline)
        !          1055:     char *comline;
        !          1056: {
        !          1057:     FILE *fp;
        !          1058:     static char *argv[MAXARGS];
        !          1059:     char file [MAXPATHLEN];
        !          1060:     int argc;
        !          1061:
        !          1062:     argc = mkargv (comline, argv, MAXARGS);
        !          1063:
        !          1064:     if (argc != 2) {
        !          1065:        printf ("Invalid number of arguments.\n");
        !          1066:        return;
        !          1067:     }
        !          1068:
        !          1069:     if (redirect_fp) {
        !          1070:        fclose (redirect_fp);
        !          1071:        redirect_fp = NULL;
        !          1072:     }
        !          1073:
        !          1074:     redirect_fp = fopen (argv[1], "r");
        !          1075:
        !          1076:     if (redirect_fp = fopen (argv[1], "r")) {
        !          1077:        if (look_var ("verbose"))
        !          1078:            printf ("Input redirected from %s\n", argv[1]);
        !          1079:        errorBell ();
        !          1080:        redirect_line = 0;
        !          1081:     }
        !          1082:     else
        !          1083:        perror (argv[0]);
        !          1084:
        !          1085:     return;
        !          1086: }
        !          1087:
        !          1088: /*
        !          1089:  * Process ID of the process redirecting from.
        !          1090:  */
        !          1091: int redirect_pid = 0;
        !          1092:
        !          1093: fep_read_from_command (comline)
        !          1094:     char *comline;
        !          1095: {
        !          1096:     static char *argv[MAXARGS];
        !          1097:     char buf[256];
        !          1098:     int argc;
        !          1099:     int i;
        !          1100:     FILE *popen();
        !          1101:
        !          1102:     argc = mkargv (comline, argv, MAXARGS);
        !          1103:
        !          1104:     if (argc == 1) {
        !          1105:        printf ("Invalid number of arguments.\n");
        !          1106:        return;
        !          1107:     }
        !          1108:
        !          1109:     if (redirect_fp) {
        !          1110:        fclose (redirect_fp);
        !          1111:        redirect_fp = NULL;
        !          1112:     }
        !          1113:
        !          1114:     strcpy (buf, "");
        !          1115:     for (i=1; i<argc; i++) {
        !          1116:        strcat (buf, argv[i]);
        !          1117:        strcat (buf, " ");
        !          1118:     }
        !          1119:
        !          1120:     if (redirect_fp = popen (buf, "r")) {
        !          1121:        if (look_var ("verbose"))
        !          1122:            printf ("Input redirected from %s\n", argv[1]);
        !          1123:        errorBell ();
        !          1124:        redirect_line = 0;
        !          1125:     }
        !          1126:     else
        !          1127:        perror (argv[0]);
        !          1128:
        !          1129:     return;
        !          1130: }
        !          1131:
        !          1132: char script_file[128];
        !          1133:
        !          1134: fep_start_script (comline)
        !          1135:     char *comline;
        !          1136: {
        !          1137:     char *name;
        !          1138:
        !          1139:     /*
        !          1140:      * Caution!! If editstatus is EDITING, comline argument is never passed.
        !          1141:      */
        !          1142:     if (editstatus == NOTEDITING) {
        !          1143:        int argc;
        !          1144:        static char *argv[MAXARGS];
        !          1145:        char buf[256];
        !          1146:
        !          1147:        argc = mkargv (comline, argv, MAXARGS);
        !          1148:        switch (argc) {
        !          1149:
        !          1150:            case 1:
        !          1151:                name = look_var ("script-file");
        !          1152:                break;
        !          1153:
        !          1154:            case 2:
        !          1155:                name = argv[1];
        !          1156:                break;
        !          1157:
        !          1158:            default:
        !          1159:                printf ("%s: Illegal number of arguments.\n", argv[0]);
        !          1160:        }
        !          1161:     }
        !          1162:     else
        !          1163:        name = look_var ("script-file");
        !          1164:
        !          1165:     /*
        !          1166:      * If script is running alread, reatun.
        !          1167:      */
        !          1168:     if (script_fp) {
        !          1169:        clear_edit_line ();
        !          1170:        errorBell ();
        !          1171:        printf ("script is already running.\n");
        !          1172:        recover_edit_line (1);
        !          1173:        return;
        !          1174:     }
        !          1175:
        !          1176:     if (!name) {
        !          1177:        clear_edit_line ();
        !          1178:        printf ("script-file is not set.\n");
        !          1179:        recover_edit_line (1);
        !          1180:        return;
        !          1181:     }
        !          1182:
        !          1183:     if ((script_fp = fopen (name, "a")) == NULL) {
        !          1184:        clear_edit_line ();
        !          1185:        perror (name);
        !          1186:        recover_edit_line (1);
        !          1187:        return;
        !          1188:     }
        !          1189:
        !          1190:     strncpy (script_file, name, sizeof (script_file));
        !          1191:
        !          1192:     clear_edit_line ();
        !          1193:     printf ("script start (file=\"%s\").\n", script_file);
        !          1194:     recover_edit_line (1);
        !          1195: }
        !          1196:
        !          1197: fep_end_script ()
        !          1198: {
        !          1199:     if (!script_fp) {
        !          1200:        clear_edit_line ();
        !          1201:        errorBell ();
        !          1202:        printf ("script is not started.\n");
        !          1203:        return;
        !          1204:     }
        !          1205:
        !          1206:     fclose (script_fp);
        !          1207:     script_fp = NULL;
        !          1208:
        !          1209:     clear_edit_line ();
        !          1210:     printf ("script end (file=\"%s\").\n", script_file);
        !          1211:     recover_edit_line (1);
        !          1212:
        !          1213:     return;
        !          1214: }
        !          1215:
        !          1216: fep_repaint(comline)
        !          1217:     char *comline;
        !          1218: {
        !          1219:     int i;
        !          1220:     int line;
        !          1221:     CHAR ch;
        !          1222:     char *crt_hight;
        !          1223:     BUFFER *bp = output_buffer;
        !          1224:
        !          1225:     /*
        !          1226:      * Caution!! If editstatus is EDITING, comline argument is never passed.
        !          1227:      */
        !          1228:     if (editstatus == NOTEDITING && comline) {
        !          1229:        int argc;
        !          1230:        static char *argv[MAXARGS];
        !          1231:        char buf[256];
        !          1232:
        !          1233:        argc = mkargv (comline, argv, MAXARGS);
        !          1234:        switch (argc) {
        !          1235:
        !          1236:            case 1:
        !          1237:                crt_hight = look_var ("crt");
        !          1238:                break;
        !          1239:
        !          1240:            case 2:
        !          1241:                crt_hight = argv[1];
        !          1242:                break;
        !          1243:
        !          1244:            default:
        !          1245:                printf ("%s: Illegal number of arguments.\n", argv[0]);
        !          1246:        }
        !          1247:     }
        !          1248:     else
        !          1249:        crt_hight = look_var ("crt");
        !          1250:
        !          1251:     line = atoi (crt_hight);
        !          1252:
        !          1253:     clear_edit_line ();
        !          1254:
        !          1255:     ch = buf_char (bp, -1);
        !          1256:     for (i = -1; ; --i) {
        !          1257:        if ((ch = buf_char(bp, i)) == '\n')
        !          1258:            --line;
        !          1259:        if (ch == (CHAR)-1 || line <= 0)
        !          1260:            break;
        !          1261:     }
        !          1262:     i += 1;
        !          1263:
        !          1264:     if (look_var("clear-repaint") && term_clear)
        !          1265:        tputs (term_clear, 1, putch);
        !          1266:
        !          1267:     for (; i < 0; i++)
        !          1268:        putchar (buf_char(bp, i));
        !          1269:
        !          1270:     /*
        !          1271:      * In this case, prompt should not be printed.
        !          1272:      * Saving prompt is ugly solution, but...
        !          1273:      */
        !          1274:     recover_edit_line (0);
        !          1275:     fflush (stdout);
        !          1276: }
        !          1277:
        !          1278: view_buffer (comline)
        !          1279:     char *comline;
        !          1280: {
        !          1281:     BUFFER *bp = output_buffer;
        !          1282:     MORE *m;
        !          1283:     int maxchar = buf_count (bp);
        !          1284:     int i;
        !          1285:     char c;
        !          1286:
        !          1287:     /*
        !          1288:      * Skip first line
        !          1289:      */
        !          1290:     for (i = 0; (c = buf_char (bp, i)) != (char)-1 && c != '\n'; i++)
        !          1291:        ;
        !          1292:     i += 1;
        !          1293:
        !          1294:     if (c == -1)
        !          1295:        return (0);
        !          1296:
        !          1297:     if (!(m = create_more (lines)))
        !          1298:        return (0);
        !          1299:
        !          1300:     clear_edit_line ();
        !          1301:     for (; i < maxchar; i++) {
        !          1302:        if (!more(m))
        !          1303:            break;
        !          1304:        while ((c = buf_char (bp, i)) != (char)-1 && c != '\n') {
        !          1305:            putchar (c);
        !          1306:            i++;
        !          1307:        }
        !          1308:        if (c == '\n')
        !          1309:            putchar ('\n');
        !          1310:     }
        !          1311:
        !          1312:     /*
        !          1313:      * If all output is shown, prompt not to be shown.
        !          1314:      */
        !          1315:     if (i < maxchar)
        !          1316:        recover_edit_line (1);
        !          1317:     else
        !          1318:        recover_edit_line (0);
        !          1319:
        !          1320:     destroy_more(m);
        !          1321:     return (0);
        !          1322: }
        !          1323:
        !          1324: #ifdef STAT
        !          1325:
        !          1326: #include "fep_stat.h"
        !          1327:
        !          1328: long stat_obyte = 0;
        !          1329: long stat_ibyte = 0;
        !          1330: long stat_rerror = 0;
        !          1331: long stat_werror = 0;
        !          1332: long stat_nselect = 0;
        !          1333:
        !          1334: struct statistics stat_info[] = {
        !          1335:     "Command Output",  &stat_obyte,
        !          1336:     "Command Input ",  &stat_ibyte,
        !          1337:     "Read error    ",  &stat_rerror,
        !          1338:     "Write error   ",  &stat_werror,
        !          1339:     "Select count  ",  &stat_nselect,
        !          1340:     NULL, NULL
        !          1341: };
        !          1342:
        !          1343: fep_showstat ()
        !          1344: {
        !          1345:     struct statistics *sp = stat_info;
        !          1346:     BUFFER *bp = output_buffer;
        !          1347:
        !          1348:     printf ("I/O and system calls:\n");
        !          1349:     for (sp = stat_info; sp->info_name; sp++) {
        !          1350:        printf ("\t%s: %d\n", sp->info_name, *(sp->info_valp));
        !          1351:     }
        !          1352:
        !          1353:     printf ("\nI/O Buffer:\n");
        !          1354:     printf ("\tMax  : %d\n", bp->b_max);
        !          1355:     printf ("\tHiWtr: %d\n", bp->b_hiwater);
        !          1356:     printf ("\tCount: %d\n", bp->b_count);
        !          1357:     printf ("\tNext : %d\n", bp->b_next);
        !          1358:
        !          1359: }
        !          1360: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>