[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.3

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

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