de rien
[parefeu.git] / rt.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <readline/readline.h>
5 #include <readline/history.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 char *stripwhite (char *string)
10 {
11         char *s, *t;
12
13         for (s = string; isspace (*s); s++)
14                 ;
15
16         if (*s == 0)
17                 return (s);
18
19         t = s + strlen (s) - 1;
20         while (t > s && isspace (*t))
21                 t--;
22         *++t = '\0';
23
24         return s;
25 }
26
27 static char test[4096];
28 void numbers(const char *input)
29 {
30         char *word, *brkt;
31         size_t i = 0;
32         FILE *f = fopen("toto.bin", "w");
33         char r;
34         int mode;
35
36         strncpy(test, input, 4096);
37
38         char *sep=":;,\t -";
39
40         for (word=strtok_r(test, sep, &brkt); word; word=strtok_r(NULL, sep, &brkt))
41         {
42
43                 if (
44                                 (strnstr(word, "0x", 2) == word) |
45                                 (strnstr(word, "0X", 2) == word) |
46                                 (strnstr(word, "X", 1) == word) |
47                                 (strnstr(word, "x", 1) == word)
48                    )
49                         mode = 16;
50                 else
51                         mode = 10;
52
53                 r=strtol(word, NULL, mode);
54                 printf("<%s> = %d", word, (int)r);
55                 fwrite(&r, 1, 1, f);
56         }
57
58         fclose(f);
59 }
60
61 int main()
62 {
63
64         int ex=0;
65         while(!ex) {
66                 char* line=readline("$ ");
67                 char* s;
68                 if(!line){
69                         break;
70                 }
71                 numbers(line);
72                 s=stripwhite(line);
73                 add_history (s);
74                 if(s){
75                         ex=!strcmp(line,"quit");
76                 }
77                 free(line);
78         }
79 }