forked from mug896/macro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m4_diverting.html
869 lines (532 loc) · 35.4 KB
/
m4_diverting.html
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
<!DOCTYPE HTML>
<html lang="ko" >
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-161001174-6"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-161001174-6');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Diverting | Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="./styles/website.css">
<link rel="next" href="./m4_input_control.html" />
<link rel="prev" href="./m4_conditionals_and_loops.html" />
</head>
<body>
<div class="book"
data-level="3.5"
data-chapter-title="Diverting"
data-filepath="m4_diverting.md"
data-basepath="."
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="bug_reports.html">
<a href="./bug_reports.html">
<i class="fa fa-check"></i>
<b>1.</b>
Bug reports
</a>
</li>
<li class="chapter " data-level="2" data-path="cpp.html">
<a href="./cpp.html">
<i class="fa fa-check"></i>
<b>2.</b>
CPP
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="macros.html">
<a href="./macros.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Macros
</a>
<ul class="articles">
<li class="chapter " data-level="2.1.1" data-path="function-like_macros.html">
<a href="./function-like_macros.html">
<i class="fa fa-check"></i>
<b>2.1.1.</b>
Function-like Macros
</a>
</li>
<li class="chapter " data-level="2.1.2" data-path="one_two_hash.html">
<a href="./one_two_hash.html">
<i class="fa fa-check"></i>
<b>2.1.2.</b>
# , ##
</a>
</li>
<li class="chapter " data-level="2.1.3" data-path="variadic_macros.html">
<a href="./variadic_macros.html">
<i class="fa fa-check"></i>
<b>2.1.3.</b>
Variadic Macros
</a>
</li>
<li class="chapter " data-level="2.1.4" data-path="argument_prescan.html">
<a href="./argument_prescan.html">
<i class="fa fa-check"></i>
<b>2.1.4.</b>
Argument Prescan
</a>
</li>
<li class="chapter " data-level="2.1.5" data-path="predefined_macros.html">
<a href="./predefined_macros.html">
<i class="fa fa-check"></i>
<b>2.1.5.</b>
Predefined Macros
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2.2" data-path="conditionals.html">
<a href="./conditionals.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Conditionals
</a>
</li>
<li class="chapter " data-level="2.3" data-path="diagnostics.html">
<a href="./diagnostics.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Diagnostics
</a>
</li>
<li class="chapter " data-level="2.4" data-path="line_control.html">
<a href="./line_control.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Line Control
</a>
</li>
<li class="chapter " data-level="2.5" data-path="pragmas.html">
<a href="./pragmas.html">
<i class="fa fa-check"></i>
<b>2.5.</b>
Pragmas
</a>
</li>
<li class="chapter " data-level="2.6" data-path="command_line_options.html">
<a href="./command_line_options.html">
<i class="fa fa-check"></i>
<b>2.6.</b>
Command-line options
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="m4.html">
<a href="./m4.html">
<i class="fa fa-check"></i>
<b>3.</b>
M4
</a>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="m4_quotes.html">
<a href="./m4_quotes.html">
<i class="fa fa-check"></i>
<b>3.1.</b>
Quotes
</a>
</li>
<li class="chapter " data-level="3.2" data-path="m4_positional_parameters.html">
<a href="./m4_positional_parameters.html">
<i class="fa fa-check"></i>
<b>3.2.</b>
Positional Parameters
</a>
</li>
<li class="chapter " data-level="3.3" data-path="m4_local_variables.html">
<a href="./m4_local_variables.html">
<i class="fa fa-check"></i>
<b>3.3.</b>
Local Variables
</a>
</li>
<li class="chapter " data-level="3.4" data-path="m4_conditionals_and_loops.html">
<a href="./m4_conditionals_and_loops.html">
<i class="fa fa-check"></i>
<b>3.4.</b>
Conditionals and Loops
</a>
</li>
<li class="chapter active" data-level="3.5" data-path="m4_diverting.html">
<a href="./m4_diverting.html">
<i class="fa fa-check"></i>
<b>3.5.</b>
Diverting
</a>
</li>
<li class="chapter " data-level="3.6" data-path="m4_input_control.html">
<a href="./m4_input_control.html">
<i class="fa fa-check"></i>
<b>3.6.</b>
Input Control
</a>
</li>
<li class="chapter " data-level="3.7" data-path="m4_file_inclusion.html">
<a href="./m4_file_inclusion.html">
<i class="fa fa-check"></i>
<b>3.7.</b>
File Inclusion
</a>
</li>
<li class="chapter " data-level="3.8" data-path="m4_builtin_macros.html">
<a href="./m4_builtin_macros.html">
<i class="fa fa-check"></i>
<b>3.8.</b>
Built-in Macros
</a>
<ul class="articles">
<li class="chapter " data-level="3.8.1" data-path="m4_arithmetic.html">
<a href="./m4_arithmetic.html">
<i class="fa fa-check"></i>
<b>3.8.1.</b>
Arithmatic
</a>
</li>
<li class="chapter " data-level="3.8.2" data-path="m4_text_handling.html">
<a href="./m4_text_handling.html">
<i class="fa fa-check"></i>
<b>3.8.2.</b>
Text Handling
</a>
</li>
<li class="chapter " data-level="3.8.3" data-path="m4_shell_commands.html">
<a href="./m4_shell_commands.html">
<i class="fa fa-check"></i>
<b>3.8.3.</b>
Shell Commands
</a>
</li>
<li class="chapter " data-level="3.8.4" data-path="m4_miscellaneous.html">
<a href="./m4_miscellaneous.html">
<i class="fa fa-check"></i>
<b>3.8.4.</b>
Miscellaneous
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.9" data-path="m4_debugging.html">
<a href="./m4_debugging.html">
<i class="fa fa-check"></i>
<b>3.9.</b>
Debugging
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Introduction</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="diverting">Diverting</h1>
<p>매크로 치환 외에 특정 영역을 나중 or 먼저 출력하거나 또는 출력에서 제외하는 등
출력 조절이 필요할 때가 있습니다.
이와 같은 기능을 제공하는 것이 diverting and undiverting 입니다.</p>
<h2 id="divert">divert</h2>
<blockquote>
<p><strong>divert</strong><br><strong>divert</strong> ( <em>Number</em> ) </p>
</blockquote>
<p>M4 는 언제든지 출력을 임시 영역에 저장했다가 필요할 때 출력할 수 있습니다.
임시 영역은 양의 정수로 나타내는데 <code>0</code> 은 디폴트로 normal output stream 을 나타냅니다.
그러니까 <code>divert(3)</code> 으로 임시 영역 <code>3</code> 에 출력을 저장한 후에
<code>divert(0)</code> 을 하면 다시 normal output stream 으로 돌아와 출력되는것입니다.
저장한 내용을 나중에 직접 undivert 를 이용해 출력하지 않으면 자동으로 마지막에
undivert 되어 출력됩니다. 이때 출력은 번호순서에 따라 출력됩니다.</p>
<blockquote>
<p><code>()</code> 없는 <code>divert</code> 는 <code>divert(0)</code> 와 같습니다.</p>
</blockquote>
<pre><code class="lang-bash">divert(-<span class="hljs-number">1</span>) send to /dev/null
divert(<span class="hljs-number">0</span>) send to STDOUT
divert(<span class="hljs-number">1</span>) send to buffer <span class="hljs-comment">#1</span>
divert(<span class="hljs-number">2</span>) send to buffer <span class="hljs-comment">#2</span>
divert(<span class="hljs-number">3</span>) send to buffer <span class="hljs-comment">#3</span>
. . .
</code></pre>
<h4 id="아래는-활용예-입니다">아래는 활용예 입니다.</h4>
<ul>
<li><strong>divert</strong> : <code>divert(1)</code> 에 의해 AAA, BBB 라인이 임시 영역 <code>1</code> 에 저장되고
라인 222 는 <code>divert(0)</code> 에 의해 normal output stream 으로 출력되므로
111, 222 라인이 먼저 출력되고 나중에 AAA, BBB 라인이 출력됩니다.</li>
</ul>
<ul>
<li><strong>order</strong> : 라인 BBB 는 <code>divert(2)</code> 에 해당하고 라인 AAA 는 <code>divert(3)</code> 에 해당하므로
출력시 BBB, AAA 라인 순으로 출력됩니다.</li>
</ul>
<ul>
<li><strong>append</strong> : 같은 divert 번호를 사용하면 내용이 append 됩니다.
<code>divert(4)</code> 가 두 군데로 분리되어 있지만 내용이 append 되어 함께 출력됩니다.</li>
</ul>
<ul>
<li><strong>discard</strong> : 출력을 저장할 때 사용할 수 있는 번호는 양의 정수이므로
음수는 출력을 discard 할때 사용됩니다.
AAA, BBB 라인이 discard 되어 출력되지 않는 것을 볼 수 있습니다.
이때 출력만 discard 되는 것이지 내용중 매크로는 정상적으로 실행되므로
foo 매크로가 정의되어 100 이 출력됩니다.</li>
</ul>
<pre><code># divert # order # append # discard
$ m4 <<\@ $ m4 <<\@ $ m4 <<\@ $ m4 <<\@
111111111 111111111 divert(4)dnl 111111111
divert(1)dnl divert(3)dnl AAAAAAAAA divert(-1)dnl
AAAAAAAAA AAAAAAAAA divert(0)dnl AAAAAAAAA
BBBBBBBBB divert(2)dnl 111111111 define(foo, 100)
divert(0)dnl BBBBBBBBB divert(4)dnl BBBBBBBBB
222222222 divert(0)dnl BBBBBBBBB divert(0)dnl
@ 222222222 divert(0)dnl 222222222
@ 222222222 foo
111111111 @ @
222222222 111111111
AAAAAAAAA 222222222 111111111 111111111
BBBBBBBBB BBBBBBBBB 222222222 222222222
AAAAAAAAA AAAAAAAAA 100
BBBBBBBBB
</code></pre><p>다음과 같이 <code>divert(-1)</code> 를 활용하면 comment 와 매크로를 define 할 때 생성되는 trailing newline 들이 출력에서 제외됩니다.
마지막 forloop 매크로 확장 결과를 보면 공백 라인이 하나도 생성되지 않는 것을 볼 수 있습니다.</p>
<pre><code>$ m4 <<\@
divert(`-1')
# forloop(var, from, to, stmt) - improved version:
# works even if VAR is not a strict macro name
# performs sanity check that FROM is larger than TO
# allows complex numerical expressions in TO and FROM
define(`forloop', `ifelse(eval(`($2) <= ($3)'), `1',
`pushdef(`$1')_$0(`$1', eval(`$2'),
eval(`$3'), `$4')popdef(`$1')')')
define(`_forloop',
`define(`$1', `$2')$4`'ifelse(`$2', `$3', `',
`$0(`$1', incr(`$2'), `$3', `$4')')')
divert`'dnl # divert`'dnl 은 divert(0)dnl 과 같은것임
forloop(`x', 1, 5, `[x] ')
@
[1] [2] [3] [4] [5]
</code></pre><h2 id="undivert">undivert</h2>
<blockquote>
<p><strong>undivert</strong><br><strong>undivert</strong> ( <em>Number . . .</em> )<br><strong>undivert</strong> ( <em>File . . .</em> ) </p>
</blockquote>
<p>undivert 는 divert 로 저장해놓은 영역을 현재 divert 영역으로 출력할 때 사용합니다.
그러니까 <code>divert(3)</code> 으로 저장해놓은 영역을 normal output stream 으로 출력하려면
먼저 <code>divert(0)</code> 을 한 후에 <code>undivert(3)</code> 해야 하는 것입니다.
( 물론 현재 divert(0) 이면 먼저 divert(0) 할 필요는 없습니다 ).
또한 <code>divert(3)</code> 영역을 <code>divert(2)</code> 영역으로 출력할 수도 있는데
이때도 먼저 <code>divert(2)</code> 로 변경한 후에 <code>undivert(3)</code> 하면 됩니다.</p>
<p>번호는 여러 개를 입력할 수 있고 입력된 순서에 따라서 출력됩니다.
<code>()</code> 없이 <code>undivert</code> 하면 현재 저장되어 있는 모든 divert 영역을 출력합니다.
한번 undivert 하면 해당 영역은 empty 가 되므로 다시 출력할 수 없습니다.</p>
<blockquote>
<p>undivert 시 매크로 확장은 발생하지 않습니다.</p>
</blockquote>
<pre><code class="lang-sh">undivert(<span class="hljs-number">1</span>) dump buffer <span class="hljs-comment">#1 to the current data stream</span>
undivert(<span class="hljs-number">2</span>) dump buffer <span class="hljs-comment">#2 to the current data stream</span>
undivert(<span class="hljs-number">3</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>) dump buffer <span class="hljs-comment">#3, #1, #2 to the current data stream</span>
undivert dumps all buffers <span class="hljs-keyword">in</span> numeric order
</code></pre>
<pre><code>$ m4 <<\@ $ m4 <<\@ $ m4 <<\@ $ m4 <<\@
111111111 111111111 111111111 111111111
divert(3)dnl divert(3)dnl divert(3)dnl divert(3)dnl
AAAAAAAAA AAAAAAAAA AAAAAAAAA AAAAAAAAA
divert(2)dnl divert(2)dnl divert(2)dnl divert(2)dnl
BBBBBBBBB BBBBBBBBB BBBBBBBBB BBBBBBBBB
divert(1)dnl divert(1)dnl undivert(3)dnl divert(1)dnl
CCCCCCCCC CCCCCCCCC divert(1)dnl CCCCCCCCC
divert(0)dnl divert(0)dnl CCCCCCCCC divert(0)dnl
222222222 undivert(3,1,2)dnl divert(0)dnl undivert`'dnl
@ 222222222 undivert(2,1)dnl 222222222
@ 222222222 @
111111111 @
222222222 111111111 111111111
CCCCCCCCC AAAAAAAAA 111111111 CCCCCCCCC
BBBBBBBBB CCCCCCCCC BBBBBBBBB BBBBBBBBB
AAAAAAAAA BBBBBBBBB AAAAAAAAA AAAAAAAAA
222222222 CCCCCCCCC 222222222
222222222
</code></pre><h4 id="divert-영역-삭제">divert 영역 삭제</h4>
<p>저장해놓은 divert 영역을 삭제하려면 먼저 <code>divert(-1)</code> 로 변경한 후에 <code>undivert</code> 하면 됩니다.</p>
<blockquote>
<p><code>divert(0)</code> 영역은 삭제되지 않습니다.</p>
</blockquote>
<pre><code>$ m4 <<\@ $ m4 <<\@ $ m4 <<\@ $ m4 <<\@
111111111 111111111 111111111 111111111
divert(3)dnl divert(3)dnl divert(3)dnl divert(3)dnl
AAAAAAAAA AAAAAAAAA AAAAAAAAA AAAAAAAAA
divert(2)dnl divert(2)dnl divert(2)dnl divert(2)dnl
BBBBBBBBB BBBBBBBBB BBBBBBBBB BBBBBBBBB
divert(1)dnl divert(1)dnl divert(1)dnl divert(-1)dnl
CCCCCCCCC CCCCCCCCC CCCCCCCCC undivert(3)
divert(0)dnl divert(0)dnl divert(0)dnl divert(1)dnl
222222222 222222222 222222222 CCCCCCCCC
@ divert(-1)dnl divert(-1)dnl divert(0)dnl
undivert undivert(3,2) 222222222
111111111 @ @ @
222222222
CCCCCCCCC 111111111 111111111 111111111
BBBBBBBBB 222222222 222222222 222222222
AAAAAAAAA CCCCCCCCC CCCCCCCCC
BBBBBBBBB
</code></pre><h4 id="활용-예제-">활용 예제 )</h4>
<pre><code>$ m4 <<\EOF
divert(-1)
m4 has multiple output queues that can be manipulated with the
`divert' macro. Valid queues range from 0 to 10, inclusive, with
the default queue being 0. As an extension, GNU m4 supports more
diversions, limited only by integer type size.
Calling the `divert' macro with an invalid queue causes text to be
discarded until another call. Note that even while output is being
discarded, quotes around `divert' and other macros are needed to
prevent expansion.
# Macros aren't expanded within comments, meaning that keywords such
# as divert and other built-ins may be used without consequence.
# HTML utility macro:
define(`H2_COUNT', 0)
# The H2_COUNT macro is redefined every time the H2 macro is used:
define(`H2',
`define(`H2_COUNT', incr(H2_COUNT))<h2>H2_COUNT. $1</h2>')
divert(1)dnl
dnl
dnl The dnl macro causes m4 to discard the rest of the line, thus
dnl preventing unwanted blank lines from appearing in the output.
dnl
H2(First Section)
H2(Second Section)
H2(Conclusion)
dnl
divert(0)dnl
dnl
<HTML>
undivert(1)dnl One of the queues is being pushed to output.
</HTML>
EOF
--------------------------
<HTML>
<h2>1. First Section</h2>
<h2>2. Second Section</h2>
<h2>3. Conclusion</h2>
</HTML>
</code></pre><p><a name="undivert-include"></a></p>
<h4 id="undivert-를-이용한-파일-include">undivert 를 이용한 파일 include</h4>
<p>undivert 를 이용해 파일도 include 할 수 있습니다.
기본적으로 undivert 시에는 매크로 확장이 안되므로 문자 그대로 파일 내용이 include 됩니다.</p>
<pre><code>undivert(`foo.txt') dump file `foo.txt' to the current data stream
undivert(`foo.txt',`bar.txt') dump file `foo.txt', `bar.txt' . . .
</code></pre><pre><code>$ cat /usr/share/doc/m4/examples/incl.m4 # include 에 사용되는 파일
Include file start
foo
Include file end
---------------------------------------
$ m4 <<\@ -I /usr/share/doc/m4/examples
define(`foo', `hello include')dnl
include(`incl.m4')dnl # include 매크로
@
Include file start
hello include # foo 매크로가 확장된다.
Include file end
---------------------------------------
$ m4 <<\@ -I /usr/share/doc/m4/examples
define(`foo', `hello include')dnl
undivert(`incl.m4')dnl # undivert 매크로
@
Include file start
foo # undivert 는 매크로 확장이 안된다.
Include file end
</code></pre><p>만약에 파일이 존재하지 않거나, 읽을 수 없거나, 디렉토리 이거나 할 경우는
STDERR 로 오류 메시지가 출력되고 확장 결과는 void 가 됩니다.
하지만 include 매크로와 달리 종료 상태 값은 <code>0</code> 이 됩니다.</p>
<pre><code>$ m4 <<\@
111111111
undivert(`not_exist.m4')dnl
222222222
@
111111111
m4:stdin:2: cannot undivert `not_exist.m4': No such file or directory
222222222
$ echo $? # 종료 상태 값은 0 이된다.
0
</code></pre><h2 id="divnum">divnum</h2>
<p><code>divnum</code> built-in 매크로는 현재 divert 영역 번호를 반환합니다.</p>
<pre><code>$ m4 <<\@
Initial divnum
divert(1)dnl
Diversion one: divnum
divert(2)dnl
Diversion two: divnum
divert(3)dnl
Diversion three: divnum
@
Initial 0
Diversion one: 1
Diversion two: 2
Diversion three: 3
</code></pre><h3 id="cleardivert-사용자-정의-매크로">cleardivert 사용자 정의 매크로</h3>
<blockquote>
<p><strong>cleardivert</strong><br><strong>cleardivert</strong> ( <em>Number . . .</em> ) </p>
</blockquote>
<p>divert 영역을 삭제할 때 다음과 같이 매크로를 정의해 사용하면 편리합니다.
<code>cleardivert</code> 는 전체 divert 영역을 삭제하고
<code>Number</code> 인수를 추가하면 해당 영역만 삭제됩니다.</p>
<pre><code>define(`cleardivert',
`pushdef(`tmp', divnum)divert(-1)ifelse($#, 0,
`undivert`'', `undivert($@)')divert(tmp)popdef(`tmp')')
</code></pre>
</section>
</div>
</div>
</div>
<a href="./m4_conditionals_and_loops.html" class="navigation navigation-prev " aria-label="Previous page: Conditionals and Loops"><i class="fa fa-angle-left"></i></a>
<a href="./m4_input_control.html" class="navigation navigation-next " aria-label="Next page: Input Control"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
<script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>