File: nonblocking_push.c

package info (click to toggle)
re2c 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 50,052 kB
  • sloc: cpp: 32,477; ml: 8,279; sh: 5,265; makefile: 968; haskell: 612; python: 428; ansic: 227; javascript: 111; java: 3
file content (638 lines) | stat: -rw-r--r-- 10,586 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* Generated by re2c */
// re2c $INPUT -o $OUTPUT -fi
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include <assert.h>

#define YYMAXFILL 19

static const size_t SIZE = 4096;

struct input_t {
  char buf[SIZE + YYMAXFILL];
  char *lim;
  char *cur;
  char *tok;
  char *mark;
  int state;
  unsigned yyaccept;
  char yych;
  FILE *file;

  input_t(FILE * file)
    : buf()
    , lim(buf + SIZE)
    , cur(lim)
    , tok(lim)
    , mark(lim)
    , state(0)
    , yyaccept(0)
    , yych(0)
    , file(file)
  {}

  bool fill()
  {
    const size_t free = (tok - buf) + SIZE - (lim - buf);
    if (free < 1) return false;
    const size_t prefix = (tok - buf);

    memmove(buf, tok, buf - tok + SIZE);
    lim -= prefix;
    cur -= prefix;
    tok -= prefix;
    mark -= prefix;
    size_t to_read = SIZE - (lim - buf);
    printf("> Reading input, can take up to %lu bytes\n", to_read);
    size_t bytes_read = fread(lim, 1, to_read, file);
    lim += bytes_read;
    lim[0] = 0;

    // quick make a copy of buffer with newlines replaced w/ _
    char b[40];
    snprintf(b, 40, "%s", buf);
    for(int i = 0; i < 40; i++) {
      if ('\n' == b[i]) { b[i] = '_'; }
    }
    printf("> Read %lu bytes from input, current buffer: >%.40s<\n", bytes_read, b);

    return true;
  }
};

enum status_t {
  OK,
  FAIL,
  NEED_MORE_INPUT,
  WHITESPACE,
  WORD,
  THING
};
const char * STATUSES[] = {
  "OK",
  "FAIL",
  "NEED_MORE_INPUT",
  "WHITESPACE",
  "WORD",
  "THING"
};

#define YYGETSTATE()  in.state
#define YYSETSTATE(s) in.state = s
#define YYFILL() do { \
  printf("< Returning for more input\n"); \
  return NEED_MORE_INPUT; \
} while (0)

static status_t lex(input_t &in)
{
switch (YYGETSTATE()) {
	case 0:
		if (in.lim <= in.cur) goto yy28;
		goto yyFillLabel0;
	case 1:
		if (in.lim <= in.cur) goto yy4;
		goto yyFillLabel1;
	case 2:
		if (in.lim <= in.cur) goto yy7;
		goto yyFillLabel2;
	case 3:
		if (in.lim <= in.cur) goto yy7;
		goto yyFillLabel3;
	case 4:
		if (in.lim <= in.cur) goto yy7;
		goto yyFillLabel4;
	case 5:
		if (in.lim <= in.cur) goto yy7;
		goto yyFillLabel5;
	case 6:
		if (in.lim <= in.cur) goto yy7;
		goto yyFillLabel6;
	case 7:
		if (in.lim <= in.cur) goto yy7;
		goto yyFillLabel7;
	case 8:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel8;
	case 9:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel9;
	case 10:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel10;
	case 11:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel11;
	case 12:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel12;
	case 13:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel13;
	case 14:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel14;
	case 15:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel15;
	case 16:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel16;
	case 17:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel17;
	case 18:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel18;
	case 19:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel19;
	case 20:
		if (in.lim <= in.cur) goto yy14;
		goto yyFillLabel20;
	default: goto yy0;
}


yy0:
yyFillLabel0:
	in.yych = *in.cur;
	switch (in.yych) {
		case '\n':
		case ' ': goto yy3;
		case 'A':
		case 'B':
		case 'C':
		case 'D':
		case 'E':
		case 'F':
		case 'G':
		case 'H':
		case 'I':
		case 'J':
		case 'K':
		case 'L':
		case 'M':
		case 'N':
		case 'O':
		case 'P':
		case 'Q':
		case 'R':
		case 'S':
		case 'U':
		case 'V':
		case 'W':
		case 'X':
		case 'Y':
		case 'Z':
		case 'a':
		case 'b':
		case 'c':
		case 'd':
		case 'e':
		case 'f':
		case 'g':
		case 'h':
		case 'i':
		case 'j':
		case 'k':
		case 'l':
		case 'm':
		case 'n':
		case 'o':
		case 'p':
		case 'q':
		case 'r':
		case 's':
		case 't':
		case 'u':
		case 'v':
		case 'w':
		case 'x':
		case 'y':
		case 'z': goto yy5;
		case 'T': goto yy8;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(0);
				YYFILL();
			}
			goto yy2;
	}
yy2:
	++in.cur;
	YYSETSTATE(-1);
	{ printf("< Unexpected character >%c<\n", in.yych); return FAIL; }
yy3:
	++in.cur;
yyFillLabel1:
	in.yych = *in.cur;
	switch (in.yych) {
		case '\n':
		case ' ': goto yy3;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(1);
				YYFILL();
			}
			goto yy4;
	}
yy4:
	YYSETSTATE(-1);
	{ printf("< whitespace\n");                         return WHITESPACE; }
yy5:
	++in.cur;
yyFillLabel2:
	in.yych = *in.cur;
yy6:
	switch (in.yych) {
		case 'A':
		case 'B':
		case 'C':
		case 'D':
		case 'E':
		case 'F':
		case 'G':
		case 'H':
		case 'I':
		case 'J':
		case 'K':
		case 'L':
		case 'M':
		case 'N':
		case 'O':
		case 'P':
		case 'Q':
		case 'R':
		case 'S':
		case 'T':
		case 'U':
		case 'V':
		case 'W':
		case 'X':
		case 'Y':
		case 'Z':
		case 'a':
		case 'b':
		case 'c':
		case 'd':
		case 'e':
		case 'f':
		case 'g':
		case 'h':
		case 'i':
		case 'j':
		case 'k':
		case 'l':
		case 'm':
		case 'n':
		case 'o':
		case 'p':
		case 'q':
		case 'r':
		case 's':
		case 't':
		case 'u':
		case 'v':
		case 'w':
		case 'x':
		case 'y':
		case 'z': goto yy5;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(2);
				YYFILL();
			}
			goto yy7;
	}
yy7:
	YYSETSTATE(-1);
	{ printf("< word\n");                               return WORD; }
yy8:
	++in.cur;
yyFillLabel3:
	in.yych = *in.cur;
	switch (in.yych) {
		case 0x00:
			if (in.lim <= in.cur) {
				YYSETSTATE(3);
				YYFILL();
			}
			goto yy7;
		case 'H': goto yy9;
		default: goto yy6;
	}
yy9:
	++in.cur;
yyFillLabel4:
	in.yych = *in.cur;
	switch (in.yych) {
		case 0x00:
			if (in.lim <= in.cur) {
				YYSETSTATE(4);
				YYFILL();
			}
			goto yy7;
		case 'I': goto yy10;
		default: goto yy6;
	}
yy10:
	++in.cur;
yyFillLabel5:
	in.yych = *in.cur;
	switch (in.yych) {
		case 0x00:
			if (in.lim <= in.cur) {
				YYSETSTATE(5);
				YYFILL();
			}
			goto yy7;
		case 'N': goto yy11;
		default: goto yy6;
	}
yy11:
	++in.cur;
yyFillLabel6:
	in.yych = *in.cur;
	switch (in.yych) {
		case 0x00:
			if (in.lim <= in.cur) {
				YYSETSTATE(6);
				YYFILL();
			}
			goto yy7;
		case 'G': goto yy12;
		default: goto yy6;
	}
yy12:
	in.mark = ++in.cur;
yyFillLabel7:
	in.yych = *in.cur;
	switch (in.yych) {
		case 0x00:
			if (in.lim <= in.cur) {
				YYSETSTATE(7);
				YYFILL();
			}
			goto yy7;
		case '\n': goto yy13;
		default: goto yy6;
	}
yy13:
	++in.cur;
yyFillLabel8:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'W': goto yy15;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(8);
				YYFILL();
			}
			goto yy14;
	}
yy14:
	in.cur = in.mark;
	goto yy7;
yy15:
	++in.cur;
yyFillLabel9:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'I': goto yy16;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(9);
				YYFILL();
			}
			goto yy14;
	}
yy16:
	++in.cur;
yyFillLabel10:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'T': goto yy17;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(10);
				YYFILL();
			}
			goto yy14;
	}
yy17:
	++in.cur;
yyFillLabel11:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'H': goto yy18;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(11);
				YYFILL();
			}
			goto yy14;
	}
yy18:
	++in.cur;
yyFillLabel12:
	in.yych = *in.cur;
	switch (in.yych) {
		case '\n': goto yy19;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(12);
				YYFILL();
			}
			goto yy14;
	}
yy19:
	++in.cur;
yyFillLabel13:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'N': goto yy20;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(13);
				YYFILL();
			}
			goto yy14;
	}
yy20:
	++in.cur;
yyFillLabel14:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'E': goto yy21;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(14);
				YYFILL();
			}
			goto yy14;
	}
yy21:
	++in.cur;
yyFillLabel15:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'W': goto yy22;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(15);
				YYFILL();
			}
			goto yy14;
	}
yy22:
	++in.cur;
yyFillLabel16:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'L': goto yy23;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(16);
				YYFILL();
			}
			goto yy14;
	}
yy23:
	++in.cur;
yyFillLabel17:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'I': goto yy24;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(17);
				YYFILL();
			}
			goto yy14;
	}
yy24:
	++in.cur;
yyFillLabel18:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'N': goto yy25;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(18);
				YYFILL();
			}
			goto yy14;
	}
yy25:
	++in.cur;
yyFillLabel19:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'E': goto yy26;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(19);
				YYFILL();
			}
			goto yy14;
	}
yy26:
	++in.cur;
yyFillLabel20:
	in.yych = *in.cur;
	switch (in.yych) {
		case 'S': goto yy27;
		default:
			if (in.lim <= in.cur) {
				YYSETSTATE(20);
				YYFILL();
			}
			goto yy14;
	}
yy27:
	++in.cur;
	YYSETSTATE(-1);
	{ printf("< Thing w/ newlines\n");                  return THING; }
yy28:
	YYSETSTATE(-1);
	{ printf("< EOF\n");                                return OK; }

}

int main()
{
  int fds[2];
  pipe(fds);
  fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | O_NONBLOCK);
  FILE * write = fdopen(fds[1], "w");
  FILE * read = fdopen(fds[0], "r");
  input_t in(read);

  const char * packets[] = {
    "THING\n",
    "WITH\n",
    "NEWLINES\n",
    "H", "E", "L", "O", "\n",
    "HELO\n",
    "THING\nWITH\n",
    "NEWLINES"
  };
  size_t num_packets = sizeof(packets) / sizeof(char *);
  size_t current_packet = 0;

  enum status_t result = NEED_MORE_INPUT;

  while (OK != result) {
    switch (result) {
      case NEED_MORE_INPUT:
        if (current_packet == num_packets) {
          printf("Not enough input\n");
          goto end;
        }

        fwrite(packets[current_packet], strlen(packets[current_packet]), 1, write);
        fflush(write);
        current_packet++;
        printf("Packet %lu / %lu written\n", current_packet, num_packets);

        if (current_packet == num_packets) {
          printf("%lu / %lu packets sent, Closing down communication channel\n",
            current_packet, num_packets);
          fclose(write);
          write = NULL;
        }

        in.fill();
        break;

      case FAIL:
        goto end;

      default:
        // careful, need to reset state (re2c forgets to do it)
        YYSETSTATE(0);
        break;
    }

    result = lex(in);
    printf("Received response from lexer: %s\n", STATUSES[result]);
  }

end:

  // cleanup
  fclose(read);
  if (write) {
    fclose(write);
  }

  return result == OK ? 0 : 1;
}

#undef YYGETSTATE
#undef YYSETSTATE
#undef YYFILL