1c4099f13d7df7b11da1a4fc99f440df967c9921
3 /* linker: atoi.o fdprintf.o parser.o */
17 //#define BUFFER_SIZE 4096
18 #define BUFFER_SIZE 256
22 #define CEIL(x, y) (((x) + (y) - 1) / (y))
23 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
24 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
28 char *progname
= NULL
;
35 int fd
= ret
? stdfderr
: stdfdout
;
36 fdprintf (fd
, "usage: %s\n", progname
);
37 fdprintf (fd
, " -h : help message\n");
38 fdprintf (fd
, " -v : verbose level (%d)\n", verbose
);
45 int main (int argc
, char *argv
[])
47 char buffer
[BUFFER_SIZE
+ 1] = {0};
54 while (progname
[i
] != '\0') {
55 if ((progname
[i
] == '/') || (progname
[i
] == '\\')) {
63 /* argument processing */
66 char *arg
= *(++argv
);
68 PRINTERR ("%s: invalid option -- %s\n", progname
, arg
);
74 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
76 PRINTERR ("%s: missing verbose level\n", progname
);
83 return usage (c
!= 'h');
87 /* read from input stream */
89 while ((n
= read (stdfdin
, pt
, BUFFER_SIZE
- (pt
- buffer
))) != 0) {
90 VERBOSE (DEBUG
, PRINTOUT ("read %d bytes\n", n
));
92 if ((n
== 2) && (buffer
[0] == '.')) {
96 /* look for end of line */
97 for (i
= 0, j
= 0; i
< n
; i
++) {
98 if (buffer
[i
] == '\n') {
100 VERBOSE (DEBUG
, PRINTOUT ("line(%d): %s\n", j
, buffer
+ j
));
101 element_t
*element
= parser (buffer
, NULL
);
102 if (element
== (void *)(-1)) {
103 VERBOSE (WARNING
, PRINTOUT ("error while parsing: %s\n", buffer
));
105 print_element (element
, 0);
112 /* keep remainding */
114 for (i
= 0; i
< n
- j
; i
++) {
115 buffer
[i
] = buffer
[i
+ j
];
118 for (i
= n
- j
; i
< BUFFER_SIZE
; i
++) {
128 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
129 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
130 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
131 // test: echo "foo\nbar\nfoobar" | calc.exe -v3
133 /* vim: set ts=4 sw=4 et: */