Debian Codegraph @

generator/pamtris/input.c

raw
1 /*=============================================================================
2 input.c
3 ===============================================================================
4 Input handling functions
5 =============================================================================*/
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <ctype.h>
11 #include <assert.h>
12
13 #include "netpbm/mallocvar.h"
14 #include "netpbm/pm.h"
15 #include "netpbm/nstring.h"
16
17 #include "limits_pamtris.h"
18 #include "framebuffer.h"
19 #include "triangle.h"
20
21 #include "input.h"
22
23 #define DRAW_MODE_TRIANGLES 1
24 #define DRAW_MODE_STRIP 2
25 #define DRAW_MODE_FAN 3
26
27 #define CMD_SET_MODE "mode"
28 #define CMD_SET_ATTRIBS "attribs"
29 #define CMD_VERTEX "vertex"
30 #define CMD_PRINT "print"
31 #define CMD_CLEAR "clear"
32 #define CMD_RESET "reset"
33 #define CMD_QUIT "quit"
34
35 #define ARG_TRIANGLES "triangles"
36 #define ARG_STRIP "strip"
37 #define ARG_FAN "fan"
38 #define ARG_IMAGE "image"
39 #define ARG_DEPTH "depth"
40
41
42 typedef struct {
43 Xy v_xy;
44 /* X- and Y-coordinates of the vertices for the current triangle.
45 */
46 Attribs v_attribs;
47 /* Vertex attributes for the current triangle. Includes the
48 Z-coordinates.
49 */
50 int32_t curr_attribs[MAX_NUM_ATTRIBS];
51 /* Attributes that will be assigned to the next vertex. Does not
52 include the Z-coordinate.
53 */
54 uint8_t next;
55 /* Index of the next vertex to be read. */
56 bool draw;
57 /* If true, draws a new triangle upon reading a new vertex. */
58
59 uint8_t mode;
60 /* Drawing mode. */
61
62 bool initialized;
63 } state_info;
64
65
66
67 static void
68 clearAttribs(state_info * const si,
69 int32_t const maxval,
70 int16_t const num_attribs) {
71
72 unsigned int i;
73
74 for (i = 0; i < num_attribs; ++i) {
75 si->curr_attribs[i] = maxval;
76 }
77 }
78
79
80
81 void
82 input_init(Input * const inputP) {
83
84 inputP->buffer = NULL;
85 inputP->length = 0;
86 inputP->number = 1;
87 }
88
89
90
91 void
92 input_term(Input * const inputP) {
93
94 if (inputP->buffer)
95 free(inputP->buffer);
96 }
97
98
99
100 typedef struct {
101 /*----------------------------------------------------------------------------
102 Value of a whitespace-delimited input symbol.
103 -----------------------------------------------------------------------------*/
104 bool null;
105 /* This is a null token. 'begin' and 'end' are meaningless */
106 char * begin;
107 /* pointer to first character of token, in some unowned storage */
108 char * end;
109 /* pointer to one position past last character of token */
110 } Token;
111
112
113
114 static Token
115 nextToken(char * const startPos) {
116 /*----------------------------------------------------------------------------
117 The token after the one that starts at 'startPos'.
118
119 Tokens are separated by any amount of whitespace.
120
121 Iff there is no next token, return a null token.
122 -----------------------------------------------------------------------------*/
123 Token retval;
124 char * p;
125
126 for (p = startPos; *p && isspace(*p); ++p);
127
128 if (*p) {
129 retval.null = false;
130 retval.begin = p;
131
132 for (; *p && !isspace(*p); ++p);
133
134 retval.end = p;
135 } else
136 retval.null = true;
137
138 return retval;
139 }
140
141
142
143 static Token
144 tokenAfter(Token const currentToken) {
145
146 if (currentToken.null)
147 return currentToken;
148 else
149 return nextToken(currentToken.end);
150 }
151
152
153
154 static bool
155 tokenIsPrefix(Token const token,
156 const char * const target) {
157 /*----------------------------------------------------------------------------
158 The value of token 'token' is a prefix of the NUL-terminated string
159 'target'.
160 -----------------------------------------------------------------------------*/
161 if (token.null)
162 return false;
163 else {
164 unsigned int charsMatchedCt;
165 const char * p;
166
167 for (p = token.begin, charsMatchedCt = 0;
168 p != token.end && target[charsMatchedCt] != '\0'; ++p) {
169
170 if (*p == target[charsMatchedCt])
171 ++charsMatchedCt;
172 else
173 break;
174 }
175
176 return (*p == '\0' || isspace(*p));
177 }
178 }
179
180
181
182 static void
183 initState(state_info * const siP) {
184
185 siP->next = 0;
186 siP->draw = false;
187 siP->mode = DRAW_MODE_TRIANGLES;
188 }
189
190
191
192 static void
193 makeLowercase(Token const t) {
194
195 char * p;
196
197 for (p = t.begin; p != t.end; ++p)
198 *p = tolower(*p);
199 }
200
201
202
203 static void
204 removeComments(char * const str) {
205
206 char * p;
207
208 for (p = &str[0]; *p; ++p) {
209 if (*p == '#') {
210 *p = '\0';
211
212 break;
213 }
214 }
215 }
216
217
218
219 static void
220 processMode(Token const firstArgToken,
221 state_info * const stateP,
222 bool * const excessArgumentsP,
223 const char ** const errorP) {
224
225 if (firstArgToken.null)
226 pm_asprintf(errorP, "syntax error: no drawing mode argument");
227 else {
228 makeLowercase(firstArgToken);
229
230 switch (*firstArgToken.begin) {
231 case 't':
232 if (!tokenIsPrefix(firstArgToken, ARG_TRIANGLES))
233 pm_asprintf(errorP, "unrecognized drawing mode");
234 else {
235 stateP->mode = DRAW_MODE_TRIANGLES;
236 stateP->draw = false;
237 stateP->next = 0;
238
239 *errorP = NULL;
240 }
241 break;
242 case 's':
243 if (!tokenIsPrefix(firstArgToken, ARG_STRIP))
244 pm_asprintf(errorP, "unrecognized drawing mode");
245 else {
246 stateP->mode = DRAW_MODE_STRIP;
247 stateP->draw = false;
248 stateP->next = 0;
249
250 *errorP = NULL;
251 }
252 break;
253 case 'f':
254 if (!tokenIsPrefix(firstArgToken, ARG_FAN))
255 pm_asprintf(errorP, "unrecognized drawing mode");
256 else {
257 stateP->mode = DRAW_MODE_FAN;
258 stateP->draw = false;
259 stateP->next = 0;
260
261 *errorP = NULL;
262 }
263 break;
264 default:
265 pm_asprintf(errorP, "unrecognized drawing mode");
266 }
267 if (!*errorP)
268 *excessArgumentsP = !tokenAfter(firstArgToken).null;
269 }
270 }
271
272
273
274 static void
275 processAttrib(Token const firstArgToken,
276 state_info * const stateP,
277 framebuffer_info * const fbiP,
278 bool * const excessArgumentsP,
279 long int * const iArgs,
280 const char ** const errorP) {
281
282 Token nt;
283 unsigned int i;
284
285 for (i = 0, *errorP = NULL, nt = firstArgToken;
286 i < fbiP->num_attribs && !*errorP;
287 ++i, nt = tokenAfter(nt)) {
288
289 char * strtolEnd;
290
291 if (nt.null)
292 pm_asprintf(errorP, "syntax error: only %u arguments", i);
293 else {
294 iArgs[i] = strtol(nt.begin, &strtolEnd, 10);
295
296 if (strtolEnd != nt.end)
297 pm_asprintf(errorP, "syntax error: argument not numeric");
298 else {
299 if (iArgs[i] < 0 || iArgs[i] > fbiP->maxval)
300 pm_asprintf(errorP, "argument(s) out of bounds");
301 }
302 }
303 }
304
305 if (!*errorP) {
306 unsigned int i;
307
308 for (i = 0; i < fbiP->num_attribs; ++i)
309 stateP->curr_attribs[i] = iArgs[i];
310
311 *excessArgumentsP = !tokenAfter(nt).null;
312 }
313 }
314
315
316
317 static void
318 processVertex(Token const firstArgToken,
319 state_info * const stateP,
320 struct boundary_info * const biP,
321 framebuffer_info * const fbiP,
322 bool * const excessArgumentsP,
323 long int * const iArgs,
324 const char ** const errorP) {
325
326 Token nt;
327 unsigned int i;
328
329 for (i = 0, *errorP = NULL, nt = firstArgToken;
330 i < 4 && !*errorP;
331 ++i, nt = tokenAfter(nt)) {
332
333 if (nt.null) {
334 if (i != 3)
335 pm_asprintf(errorP, "syntax error");
336 else
337 iArgs[i] = 1;
338 } else {
339 char * strtolEnd;
340
341 iArgs[i] = strtol(nt.begin, &strtolEnd, 10);
342
343 if (strtolEnd != nt.end)
344 pm_asprintf(errorP, "syntax error");
345 }
346
347 if (!*errorP) {
348 if (i < 3) {
349 if (iArgs[i] < MIN_COORD || iArgs[i] > MAX_COORD)
350 pm_asprintf(errorP, "coordinates out of bounds");
351 } else {
352 if (iArgs[i] < MIN_INPUT_W || iArgs[i] > MAX_INPUT_W)
353 pm_asprintf(errorP,
354 "perspective correction factor (w) "
355 "out of bounds");
356 }
357 }
358 }
359
360 if (!*errorP) {
361 unsigned int i;
362
363 for (i = 0; i < fbiP->num_attribs; ++i) {
364 stateP->v_attribs._[stateP->next][i] = stateP->curr_attribs[i];
365 }
366
367 stateP->v_attribs._[stateP->next][fbiP->num_attribs + 0] =
368 iArgs[2];
369 stateP->v_attribs._[stateP->next][fbiP->num_attribs + 1] =
370 iArgs[3];
371
372 stateP->v_xy._[stateP->next][0] = iArgs[0];
373 stateP->v_xy._[stateP->next][1] = iArgs[1];
374
375 ++stateP->next;
376
377 if (!stateP->draw) {
378 if (stateP->next == 3)
379 stateP->draw = true;
380 }
381
382 if (stateP->draw)
383 draw_triangle(stateP->v_xy, stateP->v_attribs, biP, fbiP);
384
385 if (stateP->next == 3) {
386 switch(stateP->mode) {
387 case DRAW_MODE_FAN:
388 stateP->next = 1;
389 break;
390 case DRAW_MODE_TRIANGLES:
391 stateP->draw = false;
392 stateP->next = 0;
393 break;
394 case DRAW_MODE_STRIP:
395 stateP->next = 0;
396 break;
397 default:
398 stateP->next = 0;
399 }
400 }
401 *excessArgumentsP = !nt.null;
402 }
403 }
404
405
406
407 static void
408 processPrint(Token const firstArgToken,
409 framebuffer_info * const fbiP,
410 bool * const excessArgumentsP,
411 const char ** const errorP) {
412
413 print_framebuffer(fbiP);
414
415 *excessArgumentsP = !firstArgToken.null;
416
417 *errorP = NULL;
418 }
419
420
421
422
423 static void
424 processExcl(Token const firstArgToken,
425 framebuffer_info * const fbiP,
426 bool * const excessArgumentsP,
427 const char ** const errorP) {
428
429
430 print_framebuffer(fbiP);
431
432 *excessArgumentsP = !firstArgToken.null;
433
434 *errorP = NULL;
435 }
436
437
438
439 static void
440 processClear(Token const firstArgToken,
441 framebuffer_info * const fbiP,
442 bool * const excessArgumentsP,
443 const char ** const errorP) {
444
445 if (firstArgToken.null) {
446 clear_framebuffer(true, true, fbiP);
447 *excessArgumentsP = false;
448 *errorP = NULL;
449 } else {
450 makeLowercase(firstArgToken);
451
452 switch(*firstArgToken.begin) {
453 case 'i':
454 if (!tokenIsPrefix(firstArgToken, "image"))
455 pm_asprintf(errorP, "unrecognized argument");
456 else {
457 clear_framebuffer(true, false, fbiP);
458 *errorP = NULL;
459 }
460 break;
461 case 'd':
462 if (!tokenIsPrefix(firstArgToken, "depth"))
463 pm_asprintf(errorP, "unrecognized argument");
464 else {
465 clear_framebuffer(false, true, fbiP);
466 *errorP = NULL;
467 }
468 break;
469 case 'z':
470 if (firstArgToken.end - firstArgToken.begin > 1)
471 pm_asprintf(errorP, "unrecognized argument");
472 else {
473 clear_framebuffer(false, true, fbiP);
474 *errorP = NULL;
475 }
476 break;
477 default:
478 pm_asprintf(errorP, "unrecognized argument");
479 }
480 *excessArgumentsP = !tokenAfter(firstArgToken).null;
481 }
482 }
483
484
485
486 static void
487 processReset(Token const firstArgToken,
488 state_info * const stateP,
489 framebuffer_info * const fbiP,
490 bool * const excessArgumentsP,
491 long int * const iArgs,
492 const char ** const errorP) {
493
494 Token nt;
495 unsigned int i;
496
497 for (i = 0, *errorP = NULL, nt = firstArgToken;
498 i < 2 && !*errorP;
499 ++i, nt = tokenAfter(nt)) {
500
501 if (nt.null)
502 pm_asprintf(errorP, "syntax error: only %u arguments", i);
503 else {
504 char * strtolEnd;
505
506 iArgs[i] = strtol(nt.begin, &strtolEnd, 10);
507
508 if (nt.end != strtolEnd) {
509 pm_asprintf(errorP, "syntax error: non-numeric junk "
510 "in argument %u", i);
511 }
512 }
513 }
514
515 if (!*errorP) {
516 if (iArgs[0] < 1 || iArgs[0] > PAM_OVERALL_MAXVAL)
517 pm_asprintf(errorP, "invalid new maxval");
518 else {
519 if (iArgs[1] < 1 || iArgs[1] > MAX_NUM_ATTRIBS)
520 pm_asprintf(errorP, "invalid new number of generic vertex "
521 "attributes");
522 else {
523 if (!nt.null) {
524 if (!set_tupletype(nt.begin,
525 fbiP->outpam.tuple_type)) {
526 pm_message(
527 "warning: could not set new tuple type; "
528 "using a null string");
529 set_tupletype(NULL, fbiP->outpam.tuple_type);
530 }
531 nt = tokenAfter(nt);
532 } else
533 set_tupletype(NULL, fbiP->outpam.tuple_type);
534
535 if (!realloc_image_buffer(iArgs[0], iArgs[1], fbiP)) {
536 pm_error("Unable to allocate memory for "
537 "image buffer");
538 }
539
540 stateP->next = 0;
541 stateP->draw = false;
542
543 clearAttribs(stateP, fbiP->maxval, fbiP->num_attribs);
544 }
545 *excessArgumentsP = !nt.null;
546 }
547 }
548 }
549
550
551
552 static void
553 processQuit(Token const firstArgToken,
554 bool * const excessArgumentsP,
555 const char ** const errorP) {
556
557 *excessArgumentsP = !firstArgToken.null;
558
559 *errorP = NULL;
560 }
561
562
563
564 static void
565 processCommandToken(Token const cmdToken,
566 state_info * const stateP,
567 struct boundary_info * const biP,
568 framebuffer_info * const fbiP,
569 bool * const noMoreCommandsP,
570 bool * const excessArgumentsP,
571 const char ** const errorP) {
572 /*----------------------------------------------------------------------------
573 Process command whose command token is 'commandToken'.
574
575 We may modify the storage pointed to by 'cmdToken', converting upper case
576 to lower case.
577 -----------------------------------------------------------------------------*/
578 long int iArgs[MAX_NUM_ATTRIBS];
579 /* For storing potential integer arguments. */
580 bool noMoreCommands;
581 const char * error;
582 /* Description of problem with the command; NULL if no problem.
583 */
584 bool excessArguments;
585 /* The command line has extra tokens beyond the end of a valid
586 command.
587
588 This is meaningful only when 'error' is null.
589 */
590
591 makeLowercase(cmdToken);
592
593 noMoreCommands = false; /* initial assumption */
594
595 if (tokenIsPrefix(cmdToken, CMD_SET_MODE)) {
596 processMode(tokenAfter(cmdToken), stateP,
597 &excessArguments, &error);
598 } else if (tokenIsPrefix(cmdToken, CMD_SET_ATTRIBS)) {
599 processAttrib(tokenAfter(cmdToken), stateP, fbiP,
600 &excessArguments, iArgs, &error);
601 } else if (tokenIsPrefix(cmdToken, CMD_VERTEX)) {
602 processVertex(tokenAfter(cmdToken), stateP, biP, fbiP,
603 &excessArguments, iArgs, &error);
604 } else if (tokenIsPrefix(cmdToken, CMD_PRINT)) {
605 processPrint(tokenAfter(cmdToken), fbiP,
606 &excessArguments, &error);
607 } else if (*cmdToken.begin == '!' && cmdToken.end == cmdToken.begin + 1) {
608 processExcl(tokenAfter(cmdToken), fbiP,
609 &excessArguments, &error);
610 } else if (tokenIsPrefix(cmdToken, CMD_CLEAR) ||
611 (*cmdToken.begin == '*' &&
612 cmdToken.end == cmdToken.begin + 1)) {
613 processClear(tokenAfter(cmdToken), fbiP,
614 &excessArguments, &error);
615 } else if (tokenIsPrefix(cmdToken, CMD_RESET)) {
616 processReset(tokenAfter(cmdToken), stateP, fbiP,
617 &excessArguments, iArgs, &error);
618 } else if (tokenIsPrefix(cmdToken, CMD_QUIT)) {
619 processQuit(tokenAfter(cmdToken),
620 &excessArguments, &error);
621 noMoreCommands = true;
622 } else
623 pm_asprintf(&error, "unrecognized command");
624
625 if (!noMoreCommands) {
626 if (error) {
627 *errorP = error;
628 } else {
629 *errorP = NULL;
630 *excessArgumentsP = excessArguments;
631 }
632 }
633
634 *noMoreCommandsP = noMoreCommands;
635 }
636
637
638
639 void
640 input_process_next_command(Input * const inputP,
641 struct boundary_info * const biP,
642 framebuffer_info * const fbiP,
643 bool * const noMoreCommandsP) {
644 /*----------------------------------------------------------------------------
645 Doesn't necessarily process a command, just the next line of input, which
646 may be empty.
647
648 Return *noMoreCommandsP true iff the next command is a quit command or
649 there is no next command.
650
651 We may modify the input buffer, converting upper case to lower.
652 -----------------------------------------------------------------------------*/
653 static state_info state;
654
655 Token cmdToken;
656
657 if (!state.initialized) {
658 initState(&state);
659 clearAttribs(&state, fbiP->maxval, fbiP->num_attribs);
660
661 state.initialized = true;
662 }
663
664 {
665 int eof;
666 size_t lineLen;
667
668 pm_getline(stdin, &inputP->buffer, &inputP->length, &eof, &lineLen);
669
670 if (eof) {
671 *noMoreCommandsP = true;
672 return;
673 }
674 }
675
676 removeComments(inputP->buffer);
677
678 cmdToken = nextToken(inputP->buffer);
679
680 if (cmdToken.null)
681 *noMoreCommandsP = false;
682 else {
683 const char * error;
684 bool excessArguments;
685
686 processCommandToken(cmdToken, &state, biP, fbiP,
687 noMoreCommandsP, &excessArguments, &error);
688
689 if (!*noMoreCommandsP) {
690 if (error) {
691 pm_errormsg("Error in line %u: %s",
692 (unsigned)inputP->number, error);
693 pm_strfree(error);
694 } else if (excessArguments) {
695 pm_message("warning: ignoring excess arguments: line %u",
696 (unsigned)inputP->number);
697 }
698 }
699 }
700 ++inputP->number;
701 }
702
703