home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------*/
- /* */
- /* Module Name: AMAXIN.C */
- /* Program Name: AMAX */
- /* Revision: 2.xx */
- /* Purpose: Input Routines Module */
- /* Programmer: Alan D. Bryant */
- /* */
- /* Copyright (C) 1988, 89, 90, 92 Alan D. Bryant, All Rights Reserved. */
- /* */
- /* NOTICE: This source code is copyrighted material. You are granted a */
- /* limited license to use and distribute the code. The complete text of */
- /* the license can be found in the document LICENSE.DOC which accompanies */
- /* this source code, or can be obtained directly from the author. */
- /* */
- /* Inquiries regarding this package should be directed to: */
- /* */
- /* AMAX */
- /* Alan D. Bryant */
- /* P. O. Box 101612 */
- /* Denver, CO 80250 */
- /* USA */
- /* */
- /*---------------------------------------------------------------------------*/
-
- #include <dos.h>
- #include <ctype.h>
- #include <bios.h>
- #include <stdio.h>
- #include "amax.h"
-
-
- extern char disptime;
-
- void getln(char *, unsigned int);
- unsigned char menu_select(char *);
- unsigned char agetch();
- int fgetln(unsigned char *, unsigned int, unsigned char [3], FILE *);
-
-
- void getln(char *line, unsigned int length)
- {
- int x;
- unsigned int y;
- unsigned char ch;
- unsigned char out[100] = "";
- unsigned char mode = 0;
-
- char exit = 0x00;
-
- if (length >= 500) {
- length -= 500;
- mode = 1;
- }
-
- x = -1;
-
- vreverse();
-
- if (strlen(line) > 0) {
- output(line);
- x = strlen(line) - 1;
- }
-
-
- while (! exit) {
- ch = 0x00;
- while (! ch) {
- ch = agetch();
- }
-
- /* if an escape, forget it and return */
-
- if (ch == 0x1B) {
- line[0] = 0x00;
- output("\r\n");
- vnormal();
- return;
- }
-
- /* if it's a control code, get the next character */
-
- if (ch < 0x08) continue;
- if (ch > 0x08 && ch < 0x0D) continue;
- if (ch > 0x0D && ch < 0x1B) continue;
-
- /* not an escape, so go ahead and print the character */
-
- ++x;
- sprintf(out, "%c", ch);
- output(out);
- line[x] = ch;
-
- /* if a backspace, handle it (thanks) */
-
- if (ch == 0x08) {
- if (x == 0) {
- vnormal();
- output(" ");
- vreverse();
- }
- if (x != 0) {
- --x;
- }
- --x;
- if (! direct) output(" \x08");
- else {
- output(" ");
- gotoxy(wherex() - 1, wherey());
- }
- }
-
- /* if a carriage return, output a linefeed, end in a null, return */
-
- if (ch == 0x0D) {
- output("\x0A");
- line[x] = 0x00;
- if (mode && x > 0) {
- line[x] = 0x0D;
- line[x+1] = 0x0A;
- line[x+2] = 0x00;
- }
- vnormal();
- return;
- }
-
- /* if we're at the end of the line, beep like mad and */
- /* stop the user from entering anything else but a */
- /* backspace or carriage return (thanks) */
-
- if (x == length) {
- if (! direct) output("\x08 \x08");
- else {
- gotoxy(wherex() - 1, wherey());
- output(" ");
- gotoxy(wherex() - 1, wherey());
- }
- --x;
- }
-
- /* if space is typed within 10 chars from end, then wrap */
- /* but put a null at the end of the line first */
- /* do this only if in text mode (mode == 1) */
-
- if (mode) {
- if (x > (length - 10) && ch == ' ') {
- line[x] = 0x00;
- output("\r\n");
- vnormal();
- return;
- }
- }
-
- /* Charlie: the previous 5 lines are all there is to the */
- /* line wrap code...whooppee, huh? <grin> */
-
- }
- line[x] = 0x00;
- vnormal();
- return;
- }
-
-
-
- unsigned char menu_select(char *possible)
- {
- unsigned char ch = 0xFF;
- unsigned char choices[25];
- extern char series[80];
- extern unsigned char series_pos;
-
- if (strlen(series)) {
- if (series_pos <= strlen(series)) {
- ch = series[series_pos];
- ++series_pos;
- if (ch == '=') ch = '\r';
- return ch;
- }
- }
-
- strcpy(choices, possible);
-
- while (! strchr(choices, ch)) {
- if (disptime) display_time(1);
- while (! kbhit()) {
- if (disptime) display_time(0);
- }
- ch = agetch();
- if (ch > 96 && ch < 123) ch -= 32;
- }
- return ch;
- }
-
-
- unsigned char agetch()
- {
- extern char extkey;
- extern char series[80];
- extern unsigned char series_pos;
-
- unsigned int key;
- unsigned char ch;
- char wait = 1;
-
- if (strlen(series)) {
- if (series_pos <= strlen(series)) {
- ch = series[series_pos];
- ++series_pos;
- if (ch == '=') ch = '\r';
- return ch;
- }
- }
-
- if (extkey) {
- while (wait) {
-
- /* wait for keypress */
-
- while (bioskey(1) == 0);
-
- /* get keypress */
-
- key = bioskey(0);
-
- if (key & 0xFF) {
- return key;
- }
-
- else {
- if (key == 0x4800) return 0x18; /* up arrow */
- if (key == 0x5000) return 0x19; /* down arrow */
- if (key == 0x4900) return 0x1E; /* page up */
- if (key == 0x5100) return 0x1F; /* page down */
- }
- }
- }
- else {
- ch = getch();
- return ch;
- }
- return 0x00;
- }
-
-
- #define LF 0x0A
- #define CR 0x0D
- #define EOD 0x1A
- #define LFH 0x8A
- #define CRH 0x8D
-
- int fgetln(unsigned char *buffer, unsigned int limit, unsigned char eolbfr[3], FILE *stream)
- {
- unsigned int x;
- unsigned int y = 0;
- int ch;
-
- for (x = 0; x < limit; x++) {
- buffer[x] = 0x00;
- }
-
- strcpy(eolbfr, "\0x00\0x00\0x00");
-
- for (x = 0; x < limit - 1; x++) {
- ch = fgetc(stream);
- if (ch == EOD || ch == EOF) return EOF;
- if (ch == LF || ch == CR || ch == LFH || ch == CRH) {
- eolbfr[y] = ch;
- ++y;
- ch = fgetc(stream);
- if (ch == LF || ch == CR || ch == LFH || ch == CRH) {
- eolbfr[y] = ch;
- return 0x01;
- }
- else {
- ungetc(ch, stream);
- return 0x01;
- }
- }
- else buffer[x] = ch;
- }
- buffer[limit] = 0x00;
- return 0x01;
- }
-
-
-
-
-