//returns the integer exponent for the previous number from stdin int exponent(){ char expo[3]; char c = peekchar(); int isNegative = 0; int index = 0, num = 0; if(c == '-' || c == '+'){ if(c == '-') isNegative = 1; c = getchar(); } //skip leading zeros while(peekchar() == '0') getchar(); while(((c = peekchar()) == '-' || c == '+' || CHARCLASS[c] == NUMERIC) && index < 3){ c = getchar(); expo[index] = c; num = num * 10 + (c - '0'); index++; } if(isNegative) num *= -1; return num; }
// Purpose: Compresses a string read from input using a hashtable H to store // recurring words // Pre: The Hashtable H must be empty, with no pre-set elements // Otherwise the hashtable may contain keys/values that do not match // up with the text read from input // The First line of input must contain an integer value >=1 for the size of the hash table // Input should not contain any digits after the first line, and should not // include words with punctuation inside of them (such as can't and it's) // Post: Prints the compressed version of the read string to standard output void text_compress(struct hashtable *H){ char inch = peekchar(); int wordctr = 0; //Continues to read until the EOF is found while ( inch != EOF ) { if (inch == '\n') { //If the gotten character is \n, print \n printf("\n"); inch = getchar(); } else if (isspace(inch) || ispunct(inch)) { //If its a space or punctuation, printf("%c", inch); //then just print the char again inch = getchar(); } else { //Otherwise first read the string from input char *newword = str_read(); //Then if the word is already in the hashtable, if (htContains(H, newword)) { //print the according value then free the read word printf("%d", htGetValue(H, newword)); free(newword); } else { //Otherwise add the value to the hashtable htAddValue(H, newword, wordctr); //And print it printf("%s", newword); wordctr++; free(newword); } } inch = peekchar(); } }
/* Skip blanks and whitespace. Expand this function to skip comments too. */ void skipblanks () { int c; while ((c = peekchar()) != EOF && (c == ' ' || c == '\n' || c == '\t' || c == '{' || (c == '(' && peek2char() == '*'))){ // { } comments if((c = peekchar()) == '{'){ while((c = peekchar()) != '}'){ getchar(); } } // (* *) comments int loop = 1; if((c = peekchar()) == '(' && (c = peek2char()) == '*'){ getchar(); // ( while(loop){ getchar(); // * if(peekchar() == '*' && peek2char() == ')'){ //exit getchar(); //grab *, then stop looping loop = 0; } } } getchar(); // will grab last ) and } } }
/* * Enter visual mode */ void vop(void) { register int c; #if 0 char atube[TUBESIZE + LBSIZE]; #endif ttymode f; /* mjm: was register */ if (!CA && UP == NOSTR) { if (initev) { toopen: merror("[Using open mode]"); putNFL(); oop(); return; } error("Visual needs addressible cursor or upline capability"); } if (OS && !EO) { if (initev) goto toopen; error("Can't use visual on a terminal which overstrikes"); } if (!CL) { if (initev) goto toopen; error("Visual requires clear screen capability"); } if (NS && !SF) { if (initev) goto toopen; error("Visual requires scrolling"); } ovbeg(); bastate = VISUAL; c = 0; if (any(peekchar(), "+-^.")) c = ex_getchar(); pastwh(); vsetsiz(isdigit(peekchar()) ? getnum() : value(WINDOW)); setwind(); ex_newline(); vok(atube); if (!inglobal) savevis(); Outchar = vputchar; vmoving = 0; f = ostart(); if (initev == 0) { vcontext(dot, c); vnline(NOSTR); } vmain(); Command = "visual"; ovend(f); }
/* Skip whitespace and comments. Moves the stream pointer * to right before the next valid character. */ void skipblanks () { int c, d; boolean done = FALSE; while ((c = peekchar()) != EOF && (c == ' ' || c == '\n' || c == '\t' || c == 13 || c == '(' || c == '{')) { /* Comment is of the form {}; parse through it. */ if (c == '{') { c = getchar(); done = FALSE; while (!done) { c = getchar(); if (c == EOF) { EOFFLG = 1; done = TRUE; } if (c == '}') { done = TRUE; } } } /* Comment is of the form (**). Check the next character to confirm * that this is actually a comment before parsing through it. */ if (c == '(' && (d = peek2char()) == '*') { getchar(); getchar(); done = FALSE; while (!done) { c = getchar(); if (c == EOF) { EOFFLG = 1; done = TRUE; } if (c == '*' && (d = peekchar()) == ')') { getchar(); done = TRUE; } } } else if (c == '(') { /* Not a comment. Break. */ break; } getchar(); } }
/* Get identifiers and reserved words */ TOKEN identifier (TOKEN tok) { int c, cclass, i; int counter = 0; boolean found = FALSE; while ((c = peekchar()) != EOF && counter < 15) { cclass = CHARCLASS[c]; if (cclass != ALPHA && cclass != NUMERIC) { break; // should throw an error } getchar(); tok->tokenval.tokenstring[counter] = c; /* Token may be longer than 15 characters; if it is, parse * through the rest of it. */ if (counter == 14) { c = peekchar(); cclass = CHARCLASS[c]; while ((cclass == ALPHA || cclass == NUMERIC) && c != EOF) { // in loop - add "c != EOF" check? getchar(); c = peekchar(); cclass = CHARCLASS[c]; } // add EOF check here? } counter++; } tok->tokenval.tokenstring[counter] = '\0'; /* If it's not a RESERVED (determined in the below for loop), then it's an IDENTIFIERTOK. */ tok->tokentype = IDENTIFIERTOK; /* Check if the token is a reserved word. Use FOUND to avoid performing unnecessary checks. */ i = 0; while (i < reserved_size && !found) { if ((strcmp(tok->tokenval.tokenstring, reserved[i])) == 0) { found = TRUE; tok->tokenval.which = i; tok->tokentype = RESERVED; } i++; } return tok; }
static /*err*/int parse_integer(parser_t * parser, int c) { int err; int value = c - '0'; for (;;) { c = peekchar(&parser->buffer); if ('0' <= c && c <= '9') { c = nextchar(&parser->buffer); if (value > INT_MAX/10) { print_error(parser, "integer value too large\n"); return EOVERFLOW; } value *= 10; if (value > INT_MAX - (c - '0')) { print_error(parser, "integer value too large\n"); return EOVERFLOW; } value += c - '0'; } else { break; } } err = matchinteger_parser(parser, value); return err; }
void run_test() { small_uint_t c; printf(&debug, "\nRunning external loop test.\n"); printf(&debug, "(press <Enter> to stop, `C' to clear counters)\n\n"); run_test_flag = 1; for (;;) { /* Print results. */ printf(&debug, "- out %ld - in %ld - errors lost=%ld -\r", eth.netif.out_packets, eth.netif.in_packets, eth.netif.in_discards); /* Break on any keyboard input. */ if (peekchar (&debug) >= 0) { c = getchar(&debug); if (c == 'C' || c == 'c') { /* Clear the line. */ printf(&debug, "\r\33[K"); eth.netif.out_packets = 0; eth.netif.in_packets = 0; eth.netif.in_discards = 0; continue; } if (c == 'D' || c == 'd') { eth_debug(ð, &debug); continue; } break; } timer_delay(&timer, 100); } run_test_flag = 0; puts(&debug, "\nDone.\n"); }
void hello (void *data) { int c = '!'; for (;;) { int n; putchar (&uart, '\r'); for (n=0; n<79; n++) { int k; for (k=0; k<50; k++) { putchar (&uart, c); putchar (&uart, '\b'); } putchar (&uart, c); if (peekchar (&uart) >= 0) { getchar (&uart); puts (&uart, "\nTesting UART, press any key to continue..."); getchar (&uart); break; } } putchar (&uart, '\n'); c++; if (c > '~') c = '!'; } }
int readExpression(void) { int e1; /* value of first sub-expression */ int e2; /* value of second sub-expression */ int c; int op; /* operation '+' or '*' */ c = peekchar(); if (c == '(') { c = getchar(); e1 = readExpression(); op = getchar(); e2 = readExpression(); c = getchar(); /* this had better be ')' */ if (c != ')') return EXPRESSION_ERROR; /* else */ switch(op) { case '*': return e1*e2; break; case '+': return e1+e2; break; default: return EXPRESSION_ERROR; break; } } else if (isdigit(c)) { return readNumber(); } else { return EXPRESSION_ERROR; } }
TOKEN special (TOKEN tok){ int c = peekchar(); int index = 0, i = 0; char string[3]; //special guaranteed to be 1 character long if(c != ':' && c != '<' && c != '>' && c != '.'){ c = getchar(); string[0] = c; string[1] = 0; } //special may be longer than 1 character else{ c = getchar(); int cc = peekchar(); //printf("c = %c, cc = %c\n", c, cc); if(cc != '>' && cc != '=' && cc != '.'){ string[0] = c; string[1] = 0; } else{ cc = getchar(); string[0] = c; string[1] = cc; string[2] = 0; } } for(; i < sizeof(operator)/sizeof(char*); i++){ if(strcmp(operator[i], string) == 0){ strcpy(tok->stringval, string); tok->tokentype = OPERATOR; index = i; } } i = 0; for(; i < sizeof(delim)/sizeof(char*); i++){ if(strcmp(delim[i], string) == 0){ strcpy(tok->stringval, string); tok->tokentype = DELIMITER; index = i; } } tok->datatype = STRINGTYPE; tok->whichval = index; }
static void skip_white(void) { while(1) { char c = peekchar(); if ( c != ' ' && c != '\n' && c != '\r') break; getchar(); } }
/* read_nat: Read a natural number, starting with the digit c */ int read_nat( int c ) { int ans = c - '0'; while( isdigit( peekchar() ) ) { c = getc(input); ans = ans * 10 + c - '0'; } return ans; }
// Purpose: Decompresses text read from standard input to readable text // Pre: Each integer value in input must correspond to a valid word // ie. Each integer in input must be less than // the number of words before the integer // The first token of input cannot be an integer // Post: The readable, decompressed version of the text read from input // is printed out to standard output void text_decompress() { //Creates a new array of pointer to chars to hold the strings char **strdict = malloc(sizeof(char*)); strdict[0] = NULL; char inch = peekchar(); int wordctr = 0; //Loops while the current character being worked on is not EOF while ( inch != EOF ) { if (inch == '\n') { //If the gotten character is \n, print \n printf("\n"); inch = getchar(); } else if (isspace(inch) || ispunct(inch)) { //If its a space or punctuation, printf("%c", inch); //then just print the char again inch = getchar(); } else if ( isalpha(inch) ) { //If the character is an alpha char char *newword = str_read(); //Read the full word strdict[wordctr] = newword; //Add it to the dictionary wordctr++; //Increment the number of words //Increase the memory size of strdict by one (since wordctr is increased by one) strdict = realloc(strdict, sizeof(char*) * (wordctr+1)); printf("%s", newword); //Print the word } else { char *newword = str_read(); //Otherwise the word is a digit printf("%s", strdict[atoi(newword)]); //Pass the converted integer version free(newword); //Of the read word and print the } //string located at that index, then free the word inch = peekchar(); //peeks at the next character for usage in the next lop } //Frees the new array for (int i=0; i<wordctr; i++) { free(strdict[i]); } free(strdict); }
// Purpose: Reads a string from input // Pre: True // Post: Returns a reference to a new malloced C-style string read from input char *str_read() { int size = 1; // size of the array int next_index = 0; // where the next number goes char *str = malloc(size * sizeof(char)); while (isalnum(peekchar()) && !(isspace(peekchar()))) { if (next_index == size) { size *= 2; str = realloc(str, size*sizeof(char)); if (str == NULL) { abort(); // error } } str[next_index] = getchar(); next_index++; } if (next_index == size) str = realloc(str, (size+1)*sizeof(char)); str[next_index] = '\0'; return str; }
static int skip_space(void) { char c; while(1) { c = peekchar(); if ( c != ' ') break; getchar(); } return c == '\n' || c == '\r'; }
Value *lreadlist() { Value *car, *cdr; if (peekchar() == ')') { getchar(); // eat ) return LISP_NIL; } car = lread(); cdr = lreadlist(); return mkpair(car, cdr); }
/* This encodes stdin to stdout using the quoted-printable content transfer encoding described in RFC 2045. */ void encode(void) { /* The longest any character can expand to is three characters plus '\0'. */ char buffer[4]; int c; int column, length; column = 0; while ((c = getchar()) != EOF) { if (c == '\n') { printf("\r\n"); column = 0; continue; } else if (c == ' ' || c == '\t') { if (peekchar() != '\n') sprintf(buffer, "%c", c); else sprintf(buffer, "=%02X", c); } else if (isprint(c) && c != '=') { sprintf(buffer, "%c", c); } else { sprintf(buffer, "=%02X", c); } length = strlen(buffer); if (column + length > MAXCOLUMN) { printf("=\r\n"); column = 0; } else if (column + length == MAXCOLUMN) { if (peekchar() != '\n') { printf("=\r\n"); column = 0; } } printf("%s", buffer); column += length; } return; }
void zop(char hadpr) { register int c, lines, op; zhadpr = hadpr; notempty(); znoclear = 0; zweight = 0; switch(c = op = ex_getchar()) { case '^': zweight = 1; case '-': case '+': while (peekchar() == op) { ex_getchar(); zweight++; } case '=': case '.': c = ex_getchar(); break; case EOF: znoclear++; break; default: op = 0; break; } if (digit(c)) { lines = c - '0'; for(;;) { c = ex_getchar(); if (!digit(c)) break; lines *= 10; lines += c - '0'; } if (lines < value(WINDOW)) znoclear++; if (op == '=') lines += 2; } else lines = op == EOF ? value(SCROLL) : value(WINDOW); if (c != EOF) { ungetchar(c); ex_newline(); } addr1 = addr2; setdot(); zop2(lines, op); }
void console_task (void *data) { int c, display_count = 0; tcp_socket_t *s; for (;;) { if (peekchar (&debug) < 0) { timer_delay (&timer, 50); if (++display_count == 10) { display_refresh (); display_count = 0; } continue; } c = getchar (&debug); switch (c) { case '\n': case '\r': putchar (&debug, '\n'); printf (&debug, "Transmit: %ld packets, %ld collisions, %ld errors\n", eth.netif.out_packets, eth.netif.out_collisions, eth.netif.out_errors); printf (&debug, "Receive: %ld packets, %ld errors, %ld lost\n", eth.netif.in_packets, eth.netif.in_errors, eth.netif.in_discards); printf (&debug, "Interrupts: %ln\n", eth.intr); printf (&debug, "Free memory: %u bytes\n", mem_available (&pool)); k5600bg1_debug (ð, &debug); puts (&debug, "Local address Port Peer address Port State\n"); for (s=ip.tcp_sockets; s; s=s->next) print_tcp_socket (&debug, s); for (s=ip.tcp_closing_sockets; s; s=s->next) print_tcp_socket (&debug, s); for (s=ip.tcp_listen_sockets; s; s=s->next) print_tcp_socket (&debug, s); if (user_socket) print_socket_data (&debug, user_socket); putchar (&debug, '\n'); break; case 't' & 037: task_print (&debug, 0); task_print (&debug, (task_t*) stack_console); // task_print (&debug, (task_t*) stack_poll); task_print (&debug, (task_t*) stack_tcp); task_print (&debug, (task_t*) eth.stack); task_print (&debug, (task_t*) ip.stack); putchar (&debug, '\n'); break; } } }
/* Get identifiers and reserved words */ TOKEN identifier (TOKEN tok){ char string[16]; char c = peekchar(); //peek first character of identifier int index = 0; while(CHARCLASS[c] == ALPHA || CHARCLASS[c] == NUMERIC){ c = getchar(); if(index < 15){ string[index] = c; index++; } c = peekchar(); } string[index] = 0; string[15] = 0; //oversized identifier special condition strcpy(tok->stringval, string); tok->tokentype = IDENTIFIERTOK; tok->datatype = STRINGTYPE; //now determine if reserved /// int i = 1; for(; i < sizeof(resprnt)/sizeof(char*); i++){ if(strcmp(resprnt[i], string) == 0){ tok->tokentype = RESERVED; tok->datatype = STRINGTYPE; //is this right for reserved??? CHECK!!! tok->whichval = i; } } //determine if and, or, not .... i = 0; for(; i < sizeof(operator)/sizeof(char*); i++){ if(strcmp(operator[i], string) == 0){ tok->tokentype = OPERATOR; tok->datatype = STRINGTYPE; tok->whichval = i; } } }
int readNumber(void) { int accumulator; /* the number so far */ int c; /* next character */ accumulator = 0; while ((c = peekchar()) != EOF && isdigit(c)) { c = getchar(); /* consume it */ accumulator *= 10; /* shift previous digits over */ accumulator += (c - '0'); /* add decimal value of new digit */ } return accumulator; }
/* Reads the special character(s) starting from the current stream * pointer location. Stores it in the argument TOKEN's tokenstring[] * var. Returns the TOKEN given in the argument. */ TOKEN special (TOKEN tok) { int c, cclass, i; int counter = 0; int result = 0; boolean found = FALSE; while ((c = peekchar()) != EOF && (cclass = CHARCLASS[c]) == SPECIAL) { getchar(); tok->tokenval.tokenstring[counter] = c; counter++; /* These are the only symbols that can be combined with another to form a new symbol. */ if (c != ':' && c != '<' && c != '>' && c != '.') { break; } } tok->tokenval.tokenstring[counter] = '\0'; /* Check whether or not token is an operator. Use FOUND to avoid performing unnecessary checks. */ i = 0; while (i < operators_size && !found) { if ((strcmp(tok->tokenval.tokenstring, operators[i])) == 0) { found = TRUE; tok->tokenval.which = i; // PLUS == 261, BIAS == 260 tok->tokentype = OPERATOR; } i++; } /* If token is not an operator, check to see if it's a delimiter. * Use FOUND to avoid performing unnecessary checks. */ if (!found) { i = 0; while (i < delimiters_size && !found) { if ((strcmp(tok->tokenval.tokenstring, delimiters[i])) == 0) { found = TRUE; tok->tokenval.which = i; // COMMA == 280, BIAS == 279 tok->tokentype = DELIMITER; } i++; } } if (!found) { // ?????????????????????? } return tok; }
static uint16_t get_hex(void) { uint16_t v=0; while(1) { char c = peekchar(); if (c >= '0' && c <= '9') { v = (v<<4) | (c-'0'); } else if (c >= 'a' && c <= 'f') { v = (v<<4) | (c-('a'-10)); } else if (c >= 'A' && c <= 'F') { v = (v<<4) | (c-('A'-10)); } else { return v; } getchar(); } }
void word_test (unsigned addr) { unsigned i, n; printf (&uart, "\nTesting address %08X... ", addr); for (i=0; ; ++i) { for (n=1; n; n<<=1) { word_check (addr, n); word_check (addr, ~n); } putchar (&uart, "|/-\\" [i&3]); putchar (&uart, '\b'); if (peekchar (&uart) >= 0) { getchar (&uart); break; } } puts (&uart, "done.\n"); }
/* * Ex allows you to say * delete 5 * to delete 5 lines, etc. * Such nonsense is implemented by setcount. */ setcount() { register int cnt; pastwh(); if (!isdigit(peekchar())) { setdot(); return; } addr1 = addr2; setdot(); cnt = getnum(); if (cnt <= 0) error("Bad count|Nonzero count required"); addr2 += cnt - 1; if (addr2 > dol) addr2 = dol; nonzero(); }
static char * collect_bareword(state_t *s) { /* XXX make this not static: */ char out[100]; char *pos = out; char c; for (;;) { c = peekchar(s); if (islower(c)) { *pos++ = popchar(s); } else { /* * We're done... */ *pos = '\0'; break; } } return (strdup(out)); }
TOKEN getstring (TOKEN tok){ //assuming no empty strings, no s := '' char string[16]; int index = 0, loop = 1; int c; getchar(); //grab first apostrophe while(loop){ c = getchar(); //first character of string, assuming at least length 1 //escaped apostrophe if(peekchar() == '\''){ if(peek2char() != '\''){ getchar(); //if not at max string if(index < 15){ string[index] = c; string[index+1] = 0; //not escaped, null terminate string } loop = 0; } else{ //escaped, grab one apostrophe getchar(); string[index] = c; index++; } } else{ //normal character //if not at max string if(index < 15){ string[index] = c; index++; } } string[15] = 0; //null terminate, special condition where string is too large } strcpy(tok->stringval, string); tok->tokentype = STRINGTOK; tok->datatype = STRINGTYPE; }
void poll_eth (void *data) { buf_t *p; unsigned long rx_packets, rx_bytes; rx_packets = 0; rx_bytes = 0; for (;;) { puts (&debug, "\rWaiting... "); mdelay (10); if (peekchar (&debug) >= 0) command (getchar (&debug)); p = eth.netif.interface->input (ð.netif); if (p) { ++rx_packets; rx_bytes += p->tot_len; printf (&debug, "Packet #%lu:\n", rx_packets); buf_print_ethernet (p); buf_free (p); } } }
/* read_id: Read a symbol, starting with the character c */ char *read_id( int c ) { char buf[100], *newstr; buf[0] = c; for( int i = 1; i < 100; i++ ) { c = peekchar(); if( isalpha(c) || isdigit(c) ) { buf[i] = getc(input); } else { buf[i] = '\0'; newstr = malloc( strlen(buf) + 1 ); check_mem(newstr); strcpy( newstr, buf ); return newstr; } } error: return NULL; }