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

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

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