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

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

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