1 /* fixwrites -- convert Pascal write/writeln's into fprintf's or putc's.
2 Originally by Tim Morgan, October 10, 1987. */
4 #include <w2c/config.h>
5 #include <kpathsea/c-pathmx.h>
7 char buf
[BUFSIZ
], filename
[PATH_MAX
], args
[100];
8 char *file
, *argp
, *as
, *cmd
;
12 /* Replace the last (should be only) newline in S with a null. */
15 remove_newline (string s
)
17 char *temp
= strrchr (s
, '\n');
20 fprintf (stderr
, "Lost newline somehow.\n");
21 /* This is so the convert script can delete the output file on error. */
31 insert_long (string cp
)
36 for (i
= 0; &buf
[i
] < cp
; ++i
)
38 strcpy (&tbuf
[i
], "(long)");
39 strcpy (&tbuf
[i
+ 6], cp
);
48 char temp
[BUFSIZ
], *tp
;
50 if (!fgets (temp
, BUFSIZ
, stdin
))
52 remove_newline (temp
);
55 for (tp
= temp
; *tp
== ' '; ++tp
)
63 do_blanks (int indent
)
67 for (i
= 0; i
< indent
/ 8; i
++)
70 for (i
= 0; i
< indent
; i
++)
75 /* Return true if we have a whole write/writeln statement. We determine
76 this by matching parens, ignoring those within strings. */
80 register int depth
= 0;
93 for (++cp
; cp
&& *cp
&& *cp
!= '"'; ++cp
)
99 if (*cp
== '\\') ++cp
;
109 /* Skips to the next , or ), skipping over balanced paren pairs. */
112 skip_balanced (string cp
)
114 register int depth
= 0;
116 while (depth
> 0 || (*cp
!= ',' && *cp
!= ')'))
133 /* Return true if c appears, except inside a quoted string */
136 bare (string cp
, char c
)
138 for (; *cp
&& *cp
!= c
; ++cp
)
142 ++cp
; /* skip over initial quotation mark */
143 while (*cp
&& *cp
!= '"')
144 { /* skip to closing double quote */
150 else if (*cp
== '\'')
152 ++cp
; /* skip to contained char */
154 ++cp
; /* if backslashed, it's double */
155 ++cp
; /* skip to closing single-quote mark */
162 /* xchr[...] is supposed to be replaced by Xchr(...) when characters
163 take more than a single octet each, as is the case in Aleph. Now
164 there are several occurrences of xchr[...[...]...], which are
165 translated into Xchr(...[...)...], and the compiler dies on syntax
166 errors. Ensures that it is the matching bracket that is replaced,
167 not the first one. */
170 advance_cp (char *cp
, int lefts
)
175 cp1
= strchr (cp
+ 1, ']');
176 cp2
= strchr (cp
+ 1, '[');
177 if (cp2
&& cp2
< cp1
)
178 return advance_cp (cp2
, lefts
+ 1);
181 return advance_cp (cp1
, lefts
- 1);
190 main (int argc
, string
*argv
)
193 int blanks_done
, indent
, i
;
194 const char *program_name
= "";
197 setmode(fileno(stdout
), _O_BINARY
);
199 for (i
= 1; i
< argc
; i
++)
201 if (STREQ(argv
[i
],"-t"))
204 program_name
= argv
[i
];
207 while (fgets (buf
, BUFSIZ
, stdin
))
209 remove_newline (buf
);
212 for (cp
= buf
; *cp
; ++cp
) ;
214 while (cp
!= buf
&& *--cp
== ' ') ;
221 while (*--cp
== ' ') ;
224 for (cp
= buf
, indent
= 0; *cp
== ' ' || *cp
== '\t'; ++cp
)
233 { /* All blanks, possibly with "{" */
243 while (*cp
== ' ' || *cp
== '\t')
256 if (strncmp (cp
, "read ( input", 12) == 0)
258 char variable_name
[20];
259 if (sscanf (cp
, "read ( input , %s )", variable_name
) != 1)
261 fprintf (stderr
, "sscanf failed\n");
264 printf ("%s = getint();\n", variable_name
);
268 if (strncmp (cp
, "lab", 3) == 0 && strchr (cp
, ':'))
274 while (*cp
++ != ':');
281 if (strncmp (cp
, "else write", 10) == 0)
305 if (strncmp (cp
, "write (", 7) && strncmp (cp
, "writeln (", 9))
307 /* if not a write/writeln, just copy it to stdout and continue */
312 while (!whole (buf
)) /* make sure we have whole stmt */
314 fgets (&buf
[strlen (buf
)], BUFSIZ
- strlen (buf
), stdin
);
315 remove_newline (buf
);
321 while (*(cp
+ 1) == ' ')
324 /* Some writes start with a variable, instead of a file. */
325 if (*(cp
+ 1) == '"' || *(cp
+ 1) == '\''
326 || strncmp (cp
+ 1, "buffer", 6) == 0
327 || strncmp (cp
+ 1, "xchr", 4) == 0
328 || strncmp (cp
+ 1, "k ,", 3) == 0
329 || strncmp (cp
+ 1, "s ,", 3) == 0
330 || strncmp (cp
+ 1, "dig", 3) == 0
331 || strncmp (cp
+ 1, "HEX", 3) == 0
332 || strncmp (cp
+ 1, "versionstring", 13) == 0
333 || strncmp (cp
+ 1, "kpathseaversionstring", 21) == 0
335 strcpy (filename
, "stdout");
339 while (*cp
!= ',' && *cp
!= ')')
345 printf ("putc ('\\n', %s);\n", filename
);
354 if (*cp
== '\'' || strncmp (cp
, "xchr", 4) == 0
355 || (strncmp (cp
,"HEX", 3) == 0
356 && (STREQ (program_name
, "ofm2opl")
357 || STREQ (program_name
, "opl2ofm")
358 || STREQ (program_name
, "ovp2ovf")
359 || STREQ (program_name
, "ovf2ovp")))
360 || strncmp (cp
, "ASCII04", 7) == 0
361 || strncmp (cp
, "ASCII1", 6) == 0
362 || strncmp (cp
, "ASCIIall", 8) == 0
363 || strncmp (cp
, "months", 6) == 0
364 || strncmp (cp
, "nameoffile", 10) == 0
365 || (strncmp (cp
, "buffer", 6) == 0
366 && (STREQ (program_name
, "vptovf")
367 || STREQ (program_name
, "pltotf")
368 || STREQ (program_name
, "ppltotf")
369 || STREQ (program_name
, "uppltotf")
370 || STREQ (program_name
, "ovp2ovf")
371 || STREQ (program_name
, "opl2ofm")))
372 || (((strncmp (cp
, "buf", 3) == 0
373 || strncmp (cp
, "xdig", 4) == 0
374 || strncmp (cp
, "xext", 4) == 0
375 || strncmp (cp
, "xhyf", 4) == 0)
376 && STREQ (program_name
, "patgen")))
381 if (tex
&& strncmp (cp
, "xchr", 4) == 0)
384 cp
= strchr (cp
, '[');
386 cp
= advance_cp(cp
,1);
389 else if (*cp
== '\'')
397 while (*++cp
!= '"') /* skip to end of string */
399 ++cp
; /* allow \" in string */
402 /* More kludges -- recognize some things as strings and use %s:
407 else if (strncmp (cp
, "versionstring", 13) == 0
408 || strncmp (cp
, "poolname", 8) == 0
409 || strncmp (cp
, "formatengine", 12) == 0
410 || strncmp (cp
, "dumpname", 8) == 0)
416 /* And yet another kludge, to handle stringcast (<whatever>) */
417 else if (strncmp (cp
, "stringcast", 10) == 0
418 || strncmp (cp
, "conststringcast", 15) == 0)
422 cp
= skip_balanced (cp
); /* Skip cast expression */
430 cp
= insert_long (cp
);
431 cp
= skip_balanced (cp
); /* It's a numeric expression */
433 while (*cp
!= ',' && *cp
!= ')')
435 while (*cp
== ',' || *cp
== ' ')
439 if (strncmp (cmd
, "writeln", 7) == 0)
446 if (strcmp (args
, "%c") == 0)
448 for (as
= argp
; *as
; ++as
) ;
449 while (*--as
!= ')') ;
451 printf ("putc (%s, %s);\n", argp
, filename
);
453 else if (strcmp (args
, "%c\\n") == 0)
455 for (as
= argp
; *as
; ++as
) ;
456 while (*--as
!= ')') ;
458 printf ("{ putc (%s, %s); ", argp
, filename
);
459 printf ("putc ( '\\n', %s); }\n", filename
);
461 else if (strcmp (args
, "%s") == 0)
462 printf ("Fputs (%s, %s\n", filename
, argp
);
464 printf ("fprintf (%s, \"%s\", %s\n", filename
, args
, argp
);