3 * Facility: m4 macro processor
11 * indx - find the index of second str in the
22 for (p
= s1
; *p
; p
++) {
23 for (t
= p
, m
= s2
; *m
&& *m
== *t
; m
++, t
++)
32 * putback - push character back onto input
41 error("m4: too many characters pushed back");
45 * pbstr - push string back onto input
46 * putback is replicated to improve
65 if ((bp
= zp
) == endpbb
)
66 error("m4: too many characters pushed back");
70 * pbnum - convert number to string, push back on input.
78 num
= (n
< 0) ? -n
: n
;
80 putback(num
% 10 + '0');
82 while ((num
/= 10) > 0);
84 if (n
< 0) putback('-');
88 * chrsave - put single char on string space
96 else ***/ if (ep
< endest
)
99 error("m4: string space overflow");
103 * getdiv - read in a diversion file, and
112 if (active
== outfile
[ind
])
113 error("m4: undivert: diversion still active.");
114 (void) fclose(outfile
[ind
]);
116 m4temp
[UNIQUE
] = ind
+ '0';
117 if ((dfil
= fopen(m4temp
, "r")) == NULL
)
118 error("m4: cannot undivert.");
120 while((c
= getc(dfil
)) != EOF
)
127 if (unlink(m4temp
) == -1)
129 error("m4: cannot unlink.");
133 * Very fatal error. Close all files
140 fprintf(stderr
,"%s\n",s
);
147 static char *msg
= "\ninterrupted.";
150 int s
; /* ANSI requires the parameter */
156 * killdiv - get rid of the diversion files
162 for (n
= 0; n
< MAXOUT
; n
++)
163 if (outfile
[n
] != NULL
) {
164 (void) fclose (outfile
[n
]);
165 m4temp
[UNIQUE
] = n
+ '0';
167 (void) remove (m4temp
);
169 (void) unlink (m4temp
);
175 * save a string somewhere..
185 p
= (char *) malloc(n
);
186 if (p
!= NULL
) (void) memcpy(p
, s
, n
);
191 fprintf(stderr
, "Usage: m4 [-Dname[=val]] [-Uname]\n");
197 * H. Spencer getopt - get option letter from argv
204 char *optarg
; /* Global argument pointer. */
205 int optind
= 0; /* Global argv index. */
207 static char *scan
= NULL
; /* Private scan pointer. */
210 getopt(argc
, argv
, optstring
)
216 register char *place
;
220 if (scan
== NULL
|| *scan
== '\0') {
224 if (optind
>= argc
|| argv
[optind
][0] != '-' || argv
[optind
][1] == '\0')
226 if (strcmp(argv
[optind
], "--")==0) {
231 scan
= argv
[optind
]+1;
236 place
= index(optstring
, c
);
238 if (place
== NULL
|| c
== ':') {
239 fprintf(stderr
, "%s: unknown option -%c\n", argv
[0], c
);
249 optarg
= argv
[optind
];
261 * This code uses Duff's Device (tm Tom Duff)
262 * to unroll the copying loop:
263 * while (count-- > 0)
267 #define COPYBYTE *to++ = *from++
269 void memcpy(to
, from
, count
)
270 register char *from
, *to
;
274 register int loops
= (count
+8-1) >> 3; /* div 8 round up */
276 switch (count
&(8-1)) { /* mod 8 */
286 } while (--loops
> 0);