OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [dejagnu/] [example/] [calc/] [calc.c] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
#ifdef HAVE_STDLIB_H
2
#include <stdlib.h>
3
#endif
4
#include <ctype.h>
5
#include <stdio.h>
6
#include "calc.h"
7
 
8
static int words();
9
 
10
int main()
11
{
12
   char line[SIZE];
13
   int nword;
14
   char *words[NWORD];
15
 
16
   while(printf("calc: "), fflush(stdout), fgets(line,SIZE,stdin) != NULL) {
17
      if((nword = split(line,words,NWORD)) == 0) continue;
18
      if(strcmp(words[0],"add") == 0) {
19
         if(nword != 3) {
20
            printf("Usage: add #1 #2\n");
21
         } else {
22
            printf("%d",atoi(words[1]) + atoi(words[2]));
23
         }
24
      } else if(strcmp(words[0],"multiply") == 0) {
25
         if(nword != 3) {
26
            printf("Usage: multiply #1 #2\n");
27
         } else {
28
            int i1 = atoi(words[1]);
29
            if(i1 == 2) i1 = 3;         /* this is a bug */
30
            printf("%d",i1*atoi(words[2]));
31
         }
32
      } else if(strcmp(words[0],"quit") == 0) {
33
         break;
34
      } else if(strcmp(words[0],"version") == 0) {
35
         printf("Version: %s",VERSION);
36
      } else {
37
         printf("Unknown command: %s",words[0]);
38
      }
39
      printf("\n");
40
   }
41
 
42
   return(0);
43
}
44
 
45
int
46
split(line,words,nword)
47
char *line;
48
char **words;
49
int nword;                              /* number of elements in words */
50
{
51
   int i;
52
 
53
   while(isspace(*line)) line++;
54
   if(*line == '\0') return(0);
55
 
56
   for(i = 0;i < nword;i++) {
57
      words[i] = line;
58
      while(*line != '\0' && !isspace(*line)) line++;
59
      if(*line == '\0') break;
60
      *line++ = '\0';
61
      while(isspace(*line)) line++;
62
   }
63
 
64
   return(i);
65
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.