Annotation of MiNT/src/console.c, revision 1.1.1.5

1.1       root        1: /*
                      2: 
1.1.1.3   root        3: Copyright 1990,1991,1992 Eric R. Smith.
                      4: 
1.1.1.4   root        5: Copyright 1993,1994 Atari Corporation.
                      6: 
1.1.1.3   root        7: All rights reserved.
1.1       root        8: 
                      9: */
                     10: 
                     11: 
                     12: 
                     13: /* MiNT routines for doing console I/O */
                     14: 
                     15: 
                     16: 
                     17: #include "mint.h"
                     18: 
                     19: 
                     20: 
                     21: /*
                     22: 
                     23:  * These routines are what Cconout, Cauxout, etc. ultimately call.
                     24: 
                     25:  * They take an integer argument which is the user's file handle,
                     26: 
                     27:  * and do the translation to a file ptr (and return an appropriate error
                     28: 
                     29:  * message if necessary.
                     30: 
                     31:  * "mode" may be RAW, COOKED, ECHO, or COOKED|ECHO.
                     32: 
1.1.1.2   root       33:  * note that the user may not call them directly!
1.1       root       34: 
                     35:  */
                     36: 
                     37: 
                     38: 
                     39: long
                     40: 
1.1.1.2   root       41: file_instat(f)
1.1       root       42: 
1.1.1.2   root       43:        FILEPTR *f;
1.1       root       44: 
                     45: {
                     46: 
                     47:        long r;
                     48: 
                     49: 
                     50: 
1.1.1.2   root       51:        if (!f) {
1.1       root       52: 
                     53:                return EIHNDL;
                     54: 
                     55:        }
                     56: 
                     57:        r = 1;          /* default is to assume input waiting (e.g. TOS files)*/
                     58: 
1.1.1.5 ! root       59:        if (is_terminal(f))
        !            60: 
        !            61:                (void)tty_ioctl(f, FIONREAD, &r);
        !            62: 
        !            63:        else
        !            64: 
        !            65:                (void)(*f->dev->ioctl)(f, FIONREAD, &r);
1.1       root       66: 
                     67:        return r;
                     68: 
                     69: }
                     70: 
                     71: 
                     72: 
                     73: long
                     74: 
1.1.1.2   root       75: file_outstat(f)
1.1       root       76: 
1.1.1.2   root       77:        FILEPTR *f;
1.1       root       78: 
                     79: {
                     80: 
                     81:        long r;
                     82: 
                     83: 
                     84: 
1.1.1.2   root       85:        if (!f) {
1.1       root       86: 
                     87:                return EIHNDL;
                     88: 
                     89:        }
                     90: 
                     91:        r = 1;          /* default is to assume output OK (e.g. TOS files) */
                     92: 
1.1.1.5 ! root       93:        if (is_terminal(f))
        !            94: 
        !            95:                (void)tty_ioctl(f, FIONWRITE, &r);
        !            96: 
        !            97:        else
        !            98: 
        !            99:                (void)(*f->dev->ioctl)(f, FIONWRITE, &r);
1.1       root      100: 
                    101:        return r;
                    102: 
                    103: }
                    104: 
                    105: 
                    106: 
                    107: long
                    108: 
1.1.1.2   root      109: file_getchar(f, mode)
1.1       root      110: 
1.1.1.2   root      111:        FILEPTR *f;
1.1       root      112: 
1.1.1.2   root      113:        int mode;
1.1       root      114: 
1.1.1.2   root      115: {
1.1       root      116: 
                    117:        char c;
                    118: 
                    119:        long r;
                    120: 
                    121: 
                    122: 
1.1.1.2   root      123:        if (!f) {
1.1       root      124: 
                    125:                return EIHNDL;
                    126: 
                    127:        }
                    128: 
                    129:        if (is_terminal(f)) {
                    130: 
                    131:                return tty_getchar(f, mode);
                    132: 
                    133:        }
                    134: 
                    135:        r = (*f->dev->read)(f, &c, 1L);
                    136: 
                    137:        if (r != 1)
                    138: 
                    139:                return MiNTEOF;
                    140: 
                    141:        else
                    142: 
                    143:                return ((long)c) & 0xff;
                    144: 
                    145: }
                    146: 
                    147: 
                    148: 
                    149: long
                    150: 
1.1.1.2   root      151: file_putchar(f, c, mode)
1.1       root      152: 
1.1.1.2   root      153:        FILEPTR *f;
1.1       root      154: 
                    155:        long c;
                    156: 
                    157:        int mode;
                    158: 
                    159: {
                    160: 
                    161:        char ch;
                    162: 
                    163: 
                    164: 
1.1.1.2   root      165:        if (!f) {
1.1       root      166: 
                    167:                return EIHNDL;
                    168: 
                    169:        }
                    170: 
                    171:        if (is_terminal(f)) {
                    172: 
                    173:                return tty_putchar(f, c & 0x7fffffffL, mode);
                    174: 
                    175:        }
                    176: 
                    177:        ch = c & 0x00ff;
                    178: 
                    179:        return (*f->dev->write)(f, &ch, 1L);
                    180: 
                    181: }
                    182: 
                    183: 
                    184: 
                    185: /*
                    186: 
                    187:  * OK, here are the GEMDOS console I/O routines
                    188: 
                    189:  */
                    190: 
                    191: 
                    192: 
1.1.1.2   root      193: long ARGS_ON_STACK
1.1       root      194: 
                    195: c_conin()
                    196: 
                    197: {
                    198: 
1.1.1.2   root      199:        return file_getchar(curproc->handle[0], COOKED|ECHO);
1.1       root      200: 
                    201: }
                    202: 
                    203: 
                    204: 
1.1.1.2   root      205: long ARGS_ON_STACK
1.1       root      206: 
                    207: c_conout(c)
                    208: 
                    209:        int c;
                    210: 
                    211: {
                    212: 
1.1.1.2   root      213:        return file_putchar(curproc->handle[1], (long)c, COOKED);
1.1       root      214: 
                    215: }
                    216: 
                    217: 
                    218: 
1.1.1.2   root      219: long ARGS_ON_STACK
1.1       root      220: 
                    221: c_auxin()
                    222: 
                    223: {
                    224: 
1.1.1.2   root      225:        return file_getchar(curproc->handle[2], RAW);
1.1       root      226: 
                    227: }
                    228: 
                    229: 
                    230: 
1.1.1.2   root      231: long ARGS_ON_STACK
1.1       root      232: 
                    233: c_auxout(c)
                    234: 
                    235:        int c;
                    236: 
                    237: {
                    238: 
1.1.1.2   root      239:        return file_putchar(curproc->handle[2], (long)c, RAW);
1.1       root      240: 
                    241: }
                    242: 
                    243: 
                    244: 
1.1.1.2   root      245: long ARGS_ON_STACK
1.1       root      246: 
                    247: c_prnout(c)
                    248: 
                    249:        int c;
                    250: 
                    251: {
                    252: 
1.1.1.2   root      253:        return file_putchar(curproc->handle[3], (long)c, RAW);
1.1       root      254: 
                    255: }
                    256: 
                    257: 
                    258: 
1.1.1.2   root      259: long ARGS_ON_STACK
1.1       root      260: 
                    261: c_rawio(c)
                    262: 
                    263:        int c;
                    264: 
                    265: {
                    266: 
                    267:        long r;
                    268: 
1.1.1.2   root      269:        PROC *p = curproc;
                    270: 
1.1       root      271: 
                    272: 
                    273:        if (c == 0x00ff) {
                    274: 
1.1.1.2   root      275:                if (!file_instat(p->handle[0]))
1.1       root      276: 
                    277:                        return 0;
                    278: 
1.1.1.2   root      279:                r = file_getchar(p->handle[0], RAW);
1.1       root      280: 
                    281:                if (r <= 0)
                    282: 
                    283:                        return 0;
                    284: 
                    285:                return r;
                    286: 
                    287:        }
                    288: 
                    289:        else
                    290: 
1.1.1.2   root      291:                return file_putchar(p->handle[1], (long)c, RAW);
1.1       root      292: 
                    293: }
                    294: 
                    295: 
                    296: 
1.1.1.2   root      297: long ARGS_ON_STACK
1.1       root      298: 
                    299: c_rawcin()
                    300: 
                    301: {
                    302: 
1.1.1.2   root      303:        return file_getchar(curproc->handle[0], RAW);
1.1       root      304: 
                    305: }
                    306: 
                    307: 
                    308: 
1.1.1.2   root      309: long ARGS_ON_STACK
1.1       root      310: 
                    311: c_necin()
                    312: 
                    313: {
                    314: 
1.1.1.2   root      315:        return file_getchar(curproc->handle[0],COOKED|NOECHO);
1.1       root      316: 
                    317: }
                    318: 
                    319: 
                    320: 
1.1.1.2   root      321: long ARGS_ON_STACK
1.1       root      322: 
                    323: c_conws(str)
                    324: 
                    325:        const char *str;
                    326: 
                    327: {
                    328: 
                    329:        const char *p = str;
                    330: 
                    331:        long cnt = 0;
                    332: 
                    333: 
                    334: 
                    335:        while (*p++) cnt++;
                    336: 
                    337:        return f_write(1, cnt, str);
                    338: 
                    339: }
                    340: 
                    341: 
                    342: 
1.1.1.2   root      343: long ARGS_ON_STACK
1.1       root      344: 
                    345: c_conrs(buf)
                    346: 
                    347:        char *buf;
                    348: 
                    349: {
                    350: 
                    351:        long size, r;
                    352: 
                    353:        char *s;
                    354: 
                    355: 
                    356: 
                    357:        size = ((long)*buf) & 0xff;
                    358: 
                    359:        r = f_read(0, size, buf+2);
                    360: 
                    361:        if (r < 0) {
                    362: 
                    363:                buf[1] = 0;
                    364: 
                    365:                return r;
                    366: 
                    367:        }
                    368: 
                    369: /* if reading from a file, stop at first CR or LF encountered */
                    370: 
                    371:        s = buf+2;
                    372: 
                    373:        size = 0;
                    374: 
                    375:        while(r-- > 0) {
                    376: 
                    377:                if (*s == '\r' || *s == '\n')
                    378: 
                    379:                        break;
                    380: 
                    381:                s++; size++;
                    382: 
                    383:        }
                    384: 
                    385:        buf[1] = (char)size;
                    386: 
                    387:        return 0;
                    388: 
                    389: }
                    390: 
                    391: 
                    392: 
1.1.1.2   root      393: long ARGS_ON_STACK
1.1       root      394: 
                    395: c_conis()
                    396: 
                    397: {
                    398: 
1.1.1.2   root      399:        return -(!!file_instat(curproc->handle[0]));
1.1       root      400: 
                    401: }
                    402: 
                    403: 
                    404: 
1.1.1.2   root      405: long ARGS_ON_STACK
1.1       root      406: 
                    407: c_conos()
                    408: 
                    409: {
                    410: 
1.1.1.2   root      411:        return -(!!file_outstat(curproc->handle[1]));
1.1       root      412: 
                    413: }
                    414: 
                    415: 
                    416: 
1.1.1.2   root      417: long ARGS_ON_STACK
1.1       root      418: 
                    419: c_prnos()
                    420: 
                    421: {
                    422: 
1.1.1.2   root      423:        return -(!!file_outstat(curproc->handle[3]));
1.1       root      424: 
                    425: }
                    426: 
                    427: 
                    428: 
1.1.1.2   root      429: long ARGS_ON_STACK
1.1       root      430: 
                    431: c_auxis()
                    432: 
                    433: {
                    434: 
1.1.1.2   root      435:        return -(!!file_instat(curproc->handle[2]));
1.1       root      436: 
                    437: }
                    438: 
                    439: 
                    440: 
1.1.1.2   root      441: long ARGS_ON_STACK
1.1       root      442: 
                    443: c_auxos()
                    444: 
                    445: {
                    446: 
1.1.1.2   root      447:        return -(!!file_outstat(curproc->handle[2]));
                    448: 
                    449: }
                    450: 
                    451: 
                    452: 
                    453: /* Extended GEMDOS routines */
                    454: 
                    455: 
                    456: 
                    457: long ARGS_ON_STACK
                    458: 
                    459: f_instat(h)
                    460: 
                    461:        int h;
                    462: 
                    463: {
                    464: 
                    465:        PROC *proc;
                    466: 
                    467:        int fh = h;
                    468: 
                    469: 
                    470: 
                    471: #if O_GLOBAL
                    472: 
                    473:        if (fh >= 100) {
                    474: 
                    475:                proc = rootproc;
                    476: 
                    477:                fh -= 100;
                    478: 
                    479:        } else
                    480: 
                    481: #endif
                    482: 
                    483:                proc = curproc;
                    484: 
                    485: 
                    486: 
                    487:        if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
                    488: 
                    489:                DEBUG(("Finstat: bad handle %d", h));
                    490: 
                    491:                return EIHNDL;
                    492: 
                    493:        }
                    494: 
                    495:        return file_instat(proc->handle[fh]);
                    496: 
                    497: }
                    498: 
                    499: 
                    500: 
                    501: long ARGS_ON_STACK
                    502: 
                    503: f_outstat(h)
                    504: 
                    505:        int h;
                    506: 
                    507: {
                    508: 
                    509:        int fh = h;
                    510: 
                    511:        PROC *proc;
                    512: 
                    513: #if O_GLOBAL
                    514: 
                    515:        if (fh >= 100) {
                    516: 
                    517:                fh -= 100;
                    518: 
                    519:                proc = rootproc;
                    520: 
                    521:        } else
                    522: 
                    523: #endif
                    524: 
                    525:                proc = curproc;
                    526: 
                    527: 
                    528: 
                    529:        if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
                    530: 
                    531:                DEBUG(("Foutstat: bad handle %d", h));
                    532: 
                    533:                return EIHNDL;
                    534: 
                    535:        }
                    536: 
                    537:        return file_outstat(proc->handle[fh]);
                    538: 
                    539: }
                    540: 
                    541: 
                    542: 
                    543: long ARGS_ON_STACK
                    544: 
                    545: f_getchar(h, mode)
                    546: 
                    547:        int h, mode;
                    548: 
                    549: {
                    550: 
                    551:        int fh = h;
                    552: 
                    553:        PROC *proc;
                    554: 
                    555: 
                    556: 
                    557: #if O_GLOBAL
                    558: 
                    559:        if (fh >= 100) {
                    560: 
                    561:                fh -= 100;
                    562: 
                    563:                proc = rootproc;
                    564: 
                    565:        } else
                    566: 
                    567: #endif
                    568: 
                    569:                proc = curproc;
                    570: 
                    571:        if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
                    572: 
                    573:                DEBUG(("Fgetchar: bad handle %d", h));
                    574: 
                    575:                return EIHNDL;
                    576: 
                    577:        }
                    578: 
                    579:        return file_getchar(proc->handle[fh], mode);
                    580: 
                    581: }
                    582: 
                    583: 
                    584: 
                    585: long ARGS_ON_STACK
                    586: 
                    587: f_putchar(h, c, mode)
                    588: 
                    589:        int h;
                    590: 
                    591:        long c;
                    592: 
                    593:        int mode;
                    594: 
                    595: {
                    596: 
                    597:        int fh = h;
                    598: 
                    599:        PROC *proc;
                    600: 
                    601: 
                    602: 
                    603: #if O_GLOBAL
                    604: 
                    605:        if (fh >= 100) {
                    606: 
                    607:                fh -= 100;
                    608: 
                    609:                proc = rootproc;
                    610: 
                    611:        } else
                    612: 
                    613: #endif
                    614: 
                    615:                proc = curproc;
                    616: 
                    617: 
                    618: 
                    619:        if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
                    620: 
                    621:                DEBUG(("Fputchar: bad handle %d", h));
                    622: 
                    623:                return EIHNDL;
                    624: 
                    625:        }
                    626: 
                    627:        return file_putchar(proc->handle[fh], c, mode);
1.1       root      628: 
                    629: }
                    630: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.