char * get_dpistr_argument(const char *prompt) { int c; for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == ' ' || c == '\t') { /* skip blanks and tabs */ } else if (c == '\n') { printf("%s", prompt); } else if (c == '"' || c == '\'') { return get_string(c); } else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == '-' || c == '/' || c == '.' || c == ':')) { my_ungetch(c); return get_string(' '); } else { my_ungetch(c); return NULL; } } return NULL; }
int my_getop(char s[]) { int c, i; while(((s[0] = c = my_getch()) == ' ' )|| (c == '\t')); s[1] = '\0'; i = 0; if((!isdigit(c)) && (c != '.') && (c != '-')) { return c; } if(c == '-') { if(isdigit(c = my_getch()) || (c = '.')) { s[++i] = c; } else if(c != EOF) { my_ungetch(c); return '-'; } } if(isdigit(c)) { while(isdigit((s[++i]) = c = my_getch())); } if(c == '.') { while(isdigit((s[++i]) = c = my_getch())); } s[i] = '\0'; if(c!= EOF) { my_ungetch(c); } return NUMB; }
int get_number_argument(const char *prompt, long *number) { int c; int result = 0; for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == ' ' || c == '\t') { /* skip blanks and tabs */ } else if (c == '\n') { printf("%s", prompt); } else if ('0' <= c && c <= '9') { my_ungetch(c); result = get_number(number); break; } else { my_ungetch(c); *number = 0; break; } } return result; }
/* getop: get next operator or numeric operand */ int getop(char s[]) { int i, c, d; while ((s[0] = c = my_getch()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-' && c != '+') { if (c >= 'a' && c <= 'z') { /* Handle stack commands */ while (isalpha(s[++i] = my_getch())) ; s[i] = '\0'; if (!strcmp(s, "print")) return PRINT; else if (!strcmp(s, "dup")) return DUP; else if (!strcmp(s, "swap")) return SWAP; else if (!strcmp(s, "clear")) return CLEAR; else if (!strcmp(s, "sin")) return SIN; else if (!strcmp(s, "cos")) return COS; else if (!strcmp(s, "exp")) return EXP; else if (!strcmp(s, "pow")) return POW; else return c; /* Unknown command */ } else { return c; /* Handle operators */ } } /* peek at next character to make sure it is a number */ if (c == '-' || c == '+') { d = my_getch(); my_ungetch(d); if (!isdigit(d)) return c; /* not a number */ } if (!isdigit(c) && c != '.' && c != '-' && c != '+') i = 0; if (isdigit(c) || c == '-' || c == '+') /* collect integer part */ while (isdigit(s[++i] = c = my_getch())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = my_getch())) ; s[i] = '\0'; if (c != EOF) my_ungetch(c); return NUMBER; }
void unget(s) { int d = strlen(s); int l; for(l = d + 1; l >= 0; l--) { my_ungetch(s[l]); } }
int get_okay(const char *prompt, int default_value) { int c; flush_to_newline(0); printf("%s", prompt); for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == ' ' || c == '\t') { /* skip blanks and tabs */ } else if (c == '\n') { my_ungetch(c); return default_value; } else if (c == 'y' || c == 'Y') { return 1; } else if (c == 'n' || c == 'N') { return 0; } else { flush_to_newline(0); printf("%s", prompt); } } return -1; }
char * get_string(int eos) { char buf[DPISTRLEN+1]; char *s, *limit; int c; memset(buf, 0, sizeof(buf)); limit = buf + sizeof(buf); c = my_getch(); for (s = buf;; c = my_getch()) { if (c <= 0 || c == eos || (eos == ' ' && c == '\t')) { *s = 0; break; } else if (c == '\n') { *s = 0; my_ungetch(c); break; } else { *s++ = c; if (s >= limit) return NULL; } } return strdup(buf); }
int getint(int *pn) { int c, sign, d ; while (isspace(c = my_getch())) ; if ((!isdigit(c)) && (c != EOF) && (c != '+') && (c != '-')) { my_ungetch(c); return 0; } sign = (c == '-') ? -1 : 1; if (c == '+' || c == '-') d = c; if(!isdigit(c = my_getch())) { if(c != EOF) my_ungetch(c); my_ungetch(d); return d; } for (*pn = 0; isdigit(c); c = my_getch()) *pn = 10 * *pn + (c - '0'); *pn *= sign; if (c != EOF) my_ungetch(c); return c; }
int get_partition_modifier(void) { int c, result; result = 0; c = my_getch(); if (c == 'p' || c == 'P') result = 1; else if (c > 0) my_ungetch(c); return result; }
void flush_to_newline(int keep_newline) { int c; for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == '\n') { if (keep_newline) my_ungetch(c); break; } else { /* skip */ } } return; }
int get_number(long *number) { long value; int c; value = 0; while ((c = my_getch())) { if (c >= '0' && c <= '9') { value = value * 10 + (c - '0'); } else if (c == ' ' || c == '\t' || c == '\n') { my_ungetch(c); *number = value; return 1; } else { return 0; } } return 0; }
unsigned long get_multiplier(long divisor) { unsigned long result, extra; int c; c = my_getch(); extra = 1; if (c <= 0 || divisor <= 0) { result = 0; } else if (c == 't' || c == 'T') { result = 1024 * 1024; extra = 1024 * 1024; } else if (c == 'g' || c == 'G') { result = 1024 * 1024 * 1024; } else if (c == 'm' || c == 'M') { result = 1024 * 1024; } else if (c == 'k' || c == 'K') { result = 1024; } else { my_ungetch(c); result = 1; } if (result > 1) { if (extra > 1) { result /= divisor; if (result >= 4096) result = 0; /* overflow -> 20bits + >12bits */ else result *= extra; } else if (result >= divisor) { result /= divisor; } else { result = 1; } } return result; }
// For getword: int getword(char *word, int lim) { int c, getch(void); void my_ungetch(int); char *w = word; while (isspace(c = getch())) ; if (c != EOF) *w++ = c; if (!isalpha(c)) { *w = '\0'; return c; } for ( ; --lim > 0; w++) if (!isalnum(*w = getch())) { my_ungetch(*w); break; } *w = '\0'; return word[0]; }