-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.c
1153 lines (1071 loc) · 27.9 KB
/
action.c
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* execute the action for a matched template */
/* $Id: action.c,v 1.15 2004/12/29 00:10:15 dngray Exp $ */
/*********************************************************************
This file is part of "gema", the general-purpose macro translator,
written by David N. Gray <dgray@acm.org> in 1994 and 1995.
You may do whatever you like with this, so long as you retain
an acknowledgment of the original source.
*********************************************************************/
/*
* $Log: action.c,v $
* Revision 1.15 2004/12/29 00:10:15 dngray
* Extend range of @radix by using strtoul instead of strtol.
* (Suggested by Alex Karahalios.)
*
* Revision 1.14 2004/09/18 22:57:05 dngray
* Allow MAX_DOMAINS to be larger than 255
* (merged changes contributed by Alex Karahalios).
*
* Revision 1.13 2003/12/01 18:58:16 gray
* Fix a flaw in @set-syntax that affects optimized Microsoft compile.
*
* Revision 1.12 2003/11/02 00:03:56 gray
* Add trace printout of domain call.
*
* Revision 1.11 2001/12/31 01:35:22 gray
* Flush stdout before invoking @shell command.
*
* Revision 1.10 2001/12/15 20:21:06 gray
* Clean up compiler warnings.
*
* Revision 1.9 1996/04/08 05:04:50 gray
* Fix @wrap to do nothing if the argument is empty, and to not wrap when the
* current column is less than the margin.
*
* Revision 1.8 1995/08/06 02:28:55 gray
* Ignore implicit "\I" when expanding template as "$0" so that it doesn't
* introduce spaces that were not present in the original text.
*
* Revision 1.7 1995/07/04 23:36:22 gray
* Use separate segment on Macintosh -- from David A. Mundie
*
* Revision 1.6 1995/06/12 02:58:25 gray
* Add OP_OUTCOL and OP_GET_SWITCH.
*
* Revision 1.5 1995/05/08 03:13:42 gray
* Add @expand-wild
*/
#ifdef MACOS
#pragma segment action
#endif
#include "pattern.h"
#include "util.h"
#include "var.h"
#include "patimp.h"
#include <ctype.h> /* for isalnum */
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stdlib.h> /* for system */
#include "main.h"
#ifndef MSDOS
#include <locale.h> /* for setlocale */
#endif
int wrap_column = 80;
char* wrap_indent = NULL;
static unsigned wrap_indent_length = 0;
static void
close_output(const char* pathname);
static boolean
put_var( COStream out, const char* name, boolean default_value ) {
size_t length;
const char* value;
value = get_var( name, default_value, &length );
if ( value != NULL ) {
cos_put_len(out, value, length);
return TRUE;
}
else return FALSE;
}
static void
put_number( COStream out, long number ) {
char buf[20];
sprintf(buf,"%ld", number);
cos_puts(out, buf);
}
#if defined(_QC) || defined(_MSC_VER) /* Microsoft C or Quick C */
#pragma check_stack(on)
#endif
static void
put_datime( COStream out, const time_t *t ) {
char* x = ctime(t);
int len = strlen(x);
cos_put_len(out, x, x[len-1] == '\n' ? len-1 : len);
}
static CIStream
function_operand( const unsigned char** asp, CIStream* args ) {
const unsigned char* ap;
ap = *asp;
if ( ap[2] == PT_SEPARATOR ) {
if ( ap[0] == PT_PUT_ARG ) {
/* special case when operand is $n and nothing else */
CIStream arg = args[ ap[1] - 1 ];
*asp = ap+3;
cis_rewind(arg);
return clone_input_stream( arg );
} /* end PUT_PUT_ARG */
if ( ap[0] == PT_VAR1 ) {
/* special case for variable to avoid copying the value */
char vname[2];
size_t len;
const char* value;
vname[0] = ap[1];
vname[1] = '\0';
value = get_var(vname, TRUE, &len);
if ( value != NULL ) {
*asp = ap+3;
return make_string_input_stream(value, len, FALSE);
}
}
}
if ( ap[0] == PT_OP ) {
if ( ap[1] == OP_READ ) {
CIStream path;
ap += 2;
path = function_operand( &ap, args );
if ( *ap == PT_SEPARATOR ) {
/* special case when operand is "@include{...}" and nothing else. */
const char* pathname;
CIStream istream;
pathname = cis_whole_string(path);
if ( strcmp(canonicalize_path(cos_pathname(output_stream)),
canonicalize_path(pathname))==0 ) {
/* Using same file for translation input and output --
actually read the backup file instead. */
if ( current_backup != NULL )
pathname = current_backup;
else input_error( input_stream, EXS_INPUT,
"input and output files same but no backup\n" );
}
else
close_output(pathname);
istream = open_input_file(pathname, binary );
if ( istream == NULL )
istream = make_string_input_stream("", 0, FALSE);
*asp = ap + 1;
cis_close(path);
return istream;
}
cis_close(path);
} /* end OP_READ */
#if 0 /* changed my mind; use @read{-} instead of of @stdin{} */
else if ( ap[1] == OP_STDIN && ap[2] == PT_SEPARATOR ) {
*asp = ap + 3;
return stdin_stream;
}
#endif
} /* end PT_OP */
{
/* else general case */
COStream outbuf;
outbuf = make_buffer_output_stream();
*asp = do_action( *asp, args, outbuf );
return convert_output_to_input( outbuf );
}
}
static long
numeric_operand( const unsigned char** asp, CIStream* args ) {
long result;
char* end;
const char* sp;
sp = (const char*) *asp;
result = strtol( sp, &end, 10 );
if ( *end == PT_SEPARATOR && end > sp ) {
/* short-cut for constant number */
*asp = (const unsigned char*)end+1;
}
else { /* general case */
CIStream arg;
char* string;
arg = function_operand( asp, args );
string = cis_whole_string(arg);
result = strtol( string, &end, 10 );
while ( isspace(*end) )
end++;
if ( *end != '\0' )
input_error( input_stream, EXS_NUM,
"Non-numeric operand: \"%.99s\"\n", string);
cis_close(arg);
}
return result;
}
struct outfile_struct {
COStream out;
struct outfile_struct * next;
};
static struct outfile_struct * outfiles = NULL;
static COStream
find_output_file(const char* pathname, boolean make) {
struct outfile_struct * os;
COStream altout;
for ( os = outfiles ; ; os = os->next ) {
if ( os == NULL ) {
if ( !make )
return NULL;
altout = open_output_file( pathname, binary );
if ( altout == NULL )
return make_buffer_output_stream();
os = allocate(sizeof(struct outfile_struct),MemoryStream);
os->next = outfiles;
os->out = altout;
outfiles = os;
break;
}
if ( strcmp(pathname, cos_pathname(os->out)) == 0 ) {
altout = os->out;
break;
}
}
return altout;
}
static void
close_output(const char* pathname) {
COStream outs = find_output_file(pathname, FALSE);
if ( outs != NULL ) {
struct outfile_struct * xx;
if ( outfiles->out == outs ) {
xx = outfiles;
outfiles = xx->next;
}
else {
struct outfile_struct * os;
for ( os = outfiles ; ; os = os->next ) {
xx = os->next;
if ( xx->out == outs ) {
os->next = xx->next;
break;
}
}
}
cos_close(outs);
free(xx);
}
}
void
merge_pathnames( COStream out, boolean just_dir,
const char* dpath, const char* npath, const char* tpath ) {
/* create a pathname by taking the directory from the first argument,
the name from the second, and the extension from the third. */
const char* dname;
const char* nname;
const char* ntype;
const char* p;
ntype = pathname_type(npath);
if ( is_absolute_pathname(npath) || dpath == NULL || dpath[0] == '\0' )
nname = npath;
else {
nname = pathname_name_and_type(npath);
if ( just_dir ) {
cos_puts(out,dpath);
if ( cos_prevch(out) != DirDelim )
cos_putch(out,DirDelim);
}
else {
dname = pathname_name_and_type(dpath);
for ( p = dpath ; p < dname ; p++ )
cos_putch(out,*p);
}
}
if ( ntype == NULL )
cos_puts(out,nname);
else
for ( p = nname ; p < ntype ; p++ )
cos_putch(out,*p);
p = NULL;
if ( tpath != NULL ) {
p = pathname_type(tpath);
if ( p == NULL && tpath[0] != '\0' && strchr(tpath,DirDelim)==NULL )
p = tpath;
}
if ( p == NULL )
p = ntype;
if ( p != NULL ) {
if ( cos_prevch(out) != '.' )
cos_putch(out,'.');
cos_puts(out, p);
}
}
static const unsigned char*
skip_action( const unsigned char* action) {
const unsigned char* as;
unsigned char ac;
as = action;
for ( ; ; ) {
ac = *as++;
switch (ac) {
case PT_END: return as-1;
case PT_SEPARATOR: return as;
case PT_PUT_ARG:
case PT_VAR1:
case PT_AUX:
case PT_QUOTE:
as++;
case PT_ONE_OPT:
case PT_LINE:
case PT_MATCHED_TEXT:
break;
case PT_DOMAIN:
#if MAX_DOMAINS < 256
as = skip_action( as+1 );
#else
as = skip_action( as+2 ); /* +2 since domains take up 2 bytes */
#endif
break;
case PT_OP: {
int n;
for ( n = fnnargs[*as++] ; n > 0 ; n-- ) {
assert ( *as != PT_END );
as = skip_action(as);
}
break;
}
#ifdef PT_ARG_DELIM
case PT_ARG_DELIM:
#endif
case PT_WORD_DELIM:
case PT_ID_DELIM:
case PT_SPACE:
case PT_SKIP_WHITE_SPACE:
break;
default:
assert( !is_operator(ac) );
break;
} /* end switch ac */
} /* end for */
} /* end skip_action */
static const unsigned char*
do_cmp( int cmp, const unsigned char* start, CIStream* args, COStream out) {
const unsigned char* as;
as = start;
if ( cmp >= 0 ) {
as = skip_action(as);
if ( cmp > 0 )
as = skip_action(as);
}
as = do_action( as, args, out );
if ( cmp <= 0 ) {
as = skip_action(as);
if ( cmp < 0 )
as = skip_action(as);
}
return as;
} /* end do_cmp */
const unsigned char*
do_action( const unsigned char* action, CIStream* args, COStream out) {
const unsigned char* as;
unsigned char ac;
int argn = 0;
as = action;
if ( as != NULL )
for ( ; ; ) {
ac = *as++;
switch (ac) {
case PT_END: return as-1;
case PT_SEPARATOR: return as;
case PT_PUT_ARG: {
CIStream arg = args[ (*as++) - 1 ];
cis_rewind(arg);
cos_copy_input_stream(out,arg);
break;
}
case PT_ONE_OPT:
cos_putch(out,arg_char);
break;
case PT_DOMAIN: {
CIStream inbuf;
Pattern save_rule = current_rule;
#if MAX_DOMAINS < 256
int domain = *as++ - 1;
#else
/* Get domain index as 14 bit little endian number */
int domain = ((unsigned char)*as++)&0x7f;
domain = ((((unsigned char)*as++)&0x7f)<<7) | domain;
#endif
if ( as[0] == PT_VAR1 ||
( as[0] == PT_OP &&
( as[1] == OP_VAR || as[1] == OP_VAR_DFLT ) ) ) {
/* for safety, copy the variable's value in case it is
changed during translation. */
COStream outbuf;
outbuf = make_buffer_output_stream();
as = do_action( as, args, outbuf );
inbuf = convert_output_to_input( outbuf );
}
else /* optimized operand access */
inbuf = function_operand( &as, args );
#ifdef TRACE
if ( trace_switch ) {
int n;
fprintf( stderr, "%12ld,%2d ",
cis_line(input_stream), cis_column(input_stream));
for ( n = trace_indent ; n > 0 ; n-- )
fputc(' ',stderr);
if ( cis_is_file(inbuf) ) {
const char* inpath = cis_pathname(inbuf);
if ( inpath == NULL )
inpath = "-";
fprintf( stderr, "@%s{@read{%s}}\n",
domains[domain]->name, inpath);
}
else
fprintf( stderr, "@%s{%.60s}\n",
domains[domain]->name, cis_whole_string(inbuf));
++trace_indent;
}
#endif
if ( !translate( inbuf, domains[domain], out, NULL ) &&
cis_is_file(inbuf) && exit_status < EXS_FAIL )
exit_status = EXS_FAIL;
#ifdef TRACE
if ( trace_switch ) {
--trace_indent;
}
#endif
current_rule = save_rule;
cis_close(inbuf);
break;
}
case PT_VAR1: {
char vname[2];
vname[0] = *as++;
vname[1] = '\0';
put_var(out, vname, FALSE);
break;
}
case PT_LINE:
cos_freshline(out);
break;
case PT_MATCHED_TEXT:
do_action( current_rule->pattern, args, out );
break;
case PT_SPECIAL_ARG:
#if MAX_DOMAINS >= 256 /* advance one more since 2 bytes for domain index */
case PT_RECUR:
#endif
as++;
case PT_REGEXP:
#if MAX_DOMAINS < 256
case PT_RECUR:
#endif
as++;
case PT_MATCH_ANY:
case PT_MATCH_ONE: {
/* these will be encountered only when replaying the template as $0 */
CIStream arg = args[ argn++ ];
cis_rewind(arg);
cos_copy_input_stream(out,arg);
break;
}
case PT_AUX:
as++;
break;
case PT_OP: {
CIStream inbuf = NULL;
enum Operators ac;
ac = (enum Operators)*as++;
switch(ac){
case OP_UNDEFINE:
case OP_DEFINE: {
inbuf = function_operand( &as, args );
read_patterns(inbuf, "", ac==OP_UNDEFINE);
break;
}
case OP_SUBST: {
int d;
CIStream arg;
Pattern save_rule = current_rule;
arg = function_operand( &as, args );
d = read_patterns(arg," temp ",FALSE);
inbuf = function_operand( &as, args );
translate ( inbuf, domains[d], out, NULL );
current_rule = save_rule;
delete_domain(d);
cis_close(arg);
break;
}
case OP_VAR: {
inbuf = function_operand( &as, args );
put_var(out, cis_whole_string(inbuf), FALSE );
break;
}
case OP_VAR_DFLT: {
inbuf = function_operand( &as, args ); /* variable name */
if ( put_var(out, cis_whole_string(inbuf), TRUE ) )
as = skip_action(as); /* skip default value */
else as = do_action( as, args, out ); /* output default */
break;
}
case OP_SET: {
CIStream name;
name = function_operand( &as, args );
inbuf = function_operand( &as, args );
set_var( cis_whole_string(name),
cis_whole_string(inbuf), cis_length(inbuf) );
cis_close(name);
break;
}
case OP_BIND: {
CIStream name;
name = function_operand( &as, args );
inbuf = function_operand( &as, args );
bind_var( cis_whole_string(name),
cis_whole_string(inbuf), cis_length(inbuf) );
cis_close(name);
break;
}
case OP_UNBIND: {
CIStream name;
name = function_operand( &as, args );
unbind_var( cis_whole_string(name) );
cis_close(name);
break;
}
case OP_APPEND: {
CIStream name;
name = function_operand( &as, args );
inbuf = function_operand( &as, args );
append_var( cis_whole_string(name),
cis_whole_string(inbuf), cis_length(inbuf) );
cis_close(name);
break;
}
case OP_INCR:
case OP_DECR: {
CIStream name;
name = function_operand( &as, args );
incr_var( cis_whole_string(name), ac==OP_DECR? -1 : 1 );
cis_close(name);
break;
}
case OP_GETENV:
case OP_GETENV_DEFAULT: {
CIStream dbuf = NULL;
char* value;
inbuf = function_operand( &as, args );
if ( ac == OP_GETENV_DEFAULT )
dbuf = function_operand( &as, args );
value = getenv(cis_whole_string(inbuf));
if ( value == NULL )
cos_copy_input_stream(out, dbuf);
else cos_puts(out, value);
cis_close(dbuf);
break;
}
case OP_ERR: {
static COStream err_stream = NULL;
if ( err_stream == NULL )
err_stream = make_file_output_stream(stderr,"stderr");
as = do_action( as, args, err_stream );
break;
}
case OP_OUT: {
as = do_action( as, args, output_stream );
break;
}
case OP_PATH:
case OP_FILE: {
const char* path = cis_pathname(input_stream);
if ( path != NULL ) {
if ( ac == OP_FILE )
path = pathname_name_and_type(path);
cos_puts(out, path);
}
break;
}
case OP_OUTFILE: {
const char* opath;
opath = cos_pathname(out);
if ( opath == NULL )
opath = cos_pathname(output_stream);
cos_puts(out, opath);
break;
}
case OP_LINE: {
put_number(out, cis_line(input_stream));
break;
}
case OP_COL: {
put_number(out, cis_column(input_stream));
break;
}
case OP_OUTCOL: {
put_number(out, cos_column(output_stream));
break;
}
case OP_HELP:
usage();
break;
case OP_VERSION:
cos_puts(out, Version);
break;
case OP_DATE:
case OP_TIME: {
time_t now;
struct tm* ts;
char tbuf [12];
now = time(NULL);
ts = localtime(&now);
if ( ac == OP_TIME )
sprintf(tbuf, "%02d:%02d:%02d",
ts->tm_hour, ts->tm_min, ts->tm_sec);
else sprintf(tbuf, "%02d/%02d/%d",
ts->tm_mon + 1, ts->tm_mday, 1900 + ts->tm_year);
cos_puts(out, tbuf);
break;
}
case OP_DATIME: {
time_t now;
now = time(NULL);
put_datime( out, &now );
break;
}
case OP_MODTIME: {
time_t mtime;
mtime = cis_mod_time(input_stream);
if ( mtime != 0 )
put_datime( out, &mtime );
break;
}
case OP_PROBE: {
inbuf = function_operand( &as, args );
cos_putch(out, probe_pathname(cis_whole_string(inbuf)));
break;
}
case OP_READ: {
const char* pathname;
CIStream in;
inbuf = function_operand( &as, args );
pathname = cis_whole_string(inbuf);
close_output(pathname);
in = open_input_file(pathname,binary);
cos_copy_input_stream(out, in);
cis_close(in);
break;
}
case OP_WRITE: {
COStream oldout;
const char* pathname;
oldout = output_stream;
inbuf = function_operand( &as, args );
pathname = cis_whole_string(inbuf);
output_stream = find_output_file(pathname,TRUE);
as = do_action( as, args, output_stream );
output_stream = oldout;
break;
}
case OP_CLOSE: {
inbuf = function_operand( &as, args );
close_output(cis_whole_string(inbuf));
break;
}
case OP_COMBINEPATH:
case OP_MERGEPATH: {
CIStream dir;
CIStream name;
CIStream typ;
dir = function_operand( &as, args );
name = function_operand( &as, args );
typ = function_operand( &as, args );
merge_pathnames( out, ac==OP_COMBINEPATH, cis_whole_string(dir),
cis_whole_string(name),
cis_whole_string(typ) );
cis_close(dir);
cis_close(name);
cis_close(typ);
break;
}
case OP_RELPATH: {
CIStream dir;
dir = function_operand( &as, args );
inbuf = function_operand( &as, args );
cos_puts( out, relative_pathname(cis_whole_string(dir),
cis_whole_string(inbuf)) );
cis_close(dir);
break;
}
case OP_EXP_WILD: {
inbuf = function_operand( &as, args );
expand_wildcard ( cis_whole_string(inbuf), out );
break;
}
case OP_ADD:
case OP_SUB:
case OP_MUL:
case OP_DIV:
case OP_MOD:
case OP_AND:
case OP_OR: {
long x,y,z;
x = numeric_operand( &as, args );
y = numeric_operand( &as, args );
switch(ac){
case OP_ADD: z = x + y; break;
case OP_SUB: z = x - y; break;
case OP_MUL: z = x * y; break;
case OP_DIV: z = x / y; break;
case OP_MOD: z = x % y; break;
case OP_AND: z = x & y; break;
case OP_OR: z = x | y; break;
default: /* can't happen; just to avoid compiler warning */
assert(FALSE);
z = 0;
break;
}
put_number(out,z);
break;
}
case OP_NOT:
put_number(out, ~ numeric_operand( &as, args ) );
break;
case OP_RADIX: {
int from, to;
unsigned long value;
char* string;
char* end;
const char* fmt;
char buf[24]; /* enough for 64 bits in octal */
from = (int)numeric_operand( &as, args );
to = (int)numeric_operand( &as, args );
inbuf = function_operand( &as, args );
string = cis_whole_string(inbuf);
value = strtoul( string, &end, from );
if ( *end != '\0' )
input_error ( input_stream, EXS_NUM,
"Invalid argument for radix %d conversion: \"%.99s\"\n",
from, string);
if ( to == 8 )
fmt = "%lo";
else if ( to == 16 )
fmt = "%lX";
else {
if ( to != 10 )
input_error ( input_stream, EXS_NUM,
"Unsupported radix: %d\n", to);
while ( isspace(string[0]) )
string++;
fmt = (string[0]=='-') ? "%ld" : "%lu";
}
sprintf(buf, fmt, value);
cos_puts(out, buf);
break;
}
case OP_STR_CMP:
case OP_STRI_CMP: { /* string comparison */
CIStream x = function_operand( &as, args );
CIStream y = function_operand( &as, args );
const char* xs = cis_whole_string(x);
const char* ys = cis_whole_string(y);
int cmp;
cmp = ac == OP_STRI_CMP ? stricmp(xs, ys) : strcmp(xs, ys);
cis_close(x); cis_close(y);
as = do_cmp( cmp, as, args, out);
break;
}
case OP_NUM_CMP: { /* numeric comparison */
long x = numeric_operand( &as, args );
long y = numeric_operand( &as, args );
int cmp;
if ( x < y )
cmp = -1;
else if ( x == y )
cmp = 0;
else cmp = 1;
as = do_cmp( cmp, as, args, out);
break;
}
case OP_LENGTH: {
inbuf = function_operand( &as, args );
put_number(out, cis_length(inbuf));
break;
}
case OP_TAB: {
int col;
col = (int)numeric_operand( &as, args );
cos_spaces(out, col - (int)cos_column(out));
break;
}
case OP_WRAP: {
unsigned length;
unsigned col;
inbuf = function_operand( &as, args );
length = cis_length(inbuf);
col = cos_column(out);
if ( ( ((int)(col + length)) > wrap_column &&
col > wrap_indent_length ) ||
( col <= 1 && length > 0 ) ) {
cos_freshline(out);
cos_puts(out, wrap_indent);
skip_whitespace(inbuf);
}
cos_copy_input_stream(out, inbuf);
break;
}
case OP_SET_WRAP: {
wrap_column = (int)numeric_operand( &as, args );
inbuf = function_operand( &as, args );
if ( wrap_indent != NULL )
free(wrap_indent);
wrap_indent_length = cis_length(inbuf);
wrap_indent = str_dup_len( cis_whole_string(inbuf),
wrap_indent_length );
break;
}
case OP_RIGHT:
case OP_LEFT:
case OP_CENTER: { /* justify value in fixed-length field */
int field_length, string_length, left_pad, right_pad;
field_length = (int)numeric_operand( &as, args );
inbuf = function_operand( &as, args );
string_length = cis_length(inbuf);
left_pad = field_length - string_length;
right_pad = 0;
if ( left_pad < 0 )
left_pad = 0;
if ( ac == OP_LEFT ) {
right_pad = left_pad;
left_pad = 0;
}
else if ( ac == OP_CENTER ) {
left_pad = left_pad / 2;
right_pad = field_length - string_length - left_pad;
}
cos_spaces(out, left_pad);
cos_copy_input_stream(out, inbuf);
cos_spaces(out, right_pad);
break;
}
case OP_FILL_RIGHT:
case OP_FILL_LEFT:
case OP_FILL_CENTER: { /* justify value in fixed-length field */
int field_length, string_length, left_pad, right_pad;
CIStream background;
int i;
background = function_operand( &as, args );
field_length = cis_length(background);
inbuf = function_operand( &as, args );
string_length = cis_length(inbuf);
left_pad = field_length - string_length;
right_pad = 0;
if ( left_pad < 0 )
left_pad = 0;
if ( ac == OP_FILL_LEFT ) {
right_pad = left_pad;
left_pad = 0;
}
else if ( ac == OP_FILL_CENTER ) {
left_pad = left_pad / 2;
right_pad = field_length - string_length - left_pad;
} else assert( ac == OP_FILL_RIGHT );
for ( i = left_pad ; i > 0 ; i-- )
cos_putch(out, cis_getch(background));
cos_copy_input_stream(out, inbuf);
if ( right_pad > 0 ) {
for ( i = string_length ; i > 0 ; i-- )
(void)cis_getch(background);
cos_copy_input_stream(out, background);
}
cis_close(background);
break;
}
case OP_SUBSTRING: {
int skip_length, result_length, string_length;
skip_length = (int)numeric_operand( &as, args );
result_length = (int)numeric_operand( &as, args );
inbuf = function_operand( &as, args );
string_length = cis_length(inbuf);
if ( skip_length <= string_length ) {
if ( skip_length < 0 )
skip_length = 0;
if ( (skip_length + result_length) > string_length )
result_length = string_length - skip_length;
cos_put_len(out, cis_whole_string(inbuf) + skip_length,
result_length);
}
break;
}
case OP_DOWNCASE:
case OP_UPCASE: {
int cc;
inbuf = function_operand( &as, args );
while ( (cc = cis_getch(inbuf)) != EOF )
cos_putch(out, ac==OP_DOWNCASE ? tolower(cc) : toupper(cc) );
break;
}
case OP_CHARINT:
inbuf = function_operand( &as, args );
put_number(out, cis_getch(inbuf));
break;
case OP_INTCHAR:
cos_putch(out, (char)numeric_operand( &as, args ));
break;
case OP_REVERSE: {
int len;
const char* start;
const char* ip;
inbuf = function_operand( &as, args );
len = cis_length(inbuf);
start = cis_whole_string(inbuf);
for ( ip = start+len-1 ; ip >= start ; ip-- )
cos_putch(out, *ip);
break;
}
case OP_SHELL: {
const char* command;
inbuf = function_operand( &as, args );
command = cis_whole_string(inbuf);
fflush(stdout);
if ( system( command ) < 0 ) {
input_error ( input_stream, EXS_SHELL,
"Failed shell command \"%.20s...\":\n", command );
perror("system");
}
break;
}
case OP_EXIT:
translation_status = Translate_Exited;
break;
case OP_FAIL:
translation_status = Translate_Failed;
break;
case OP_END_OR_FAIL:
/* ideally this should be testing whether the input stream
has been advanced, but that is not so easy. */