4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <sys/types.h>
40 /* external functions */
47 extern int optind
, opterr
;
49 /* static functions */
51 static void extract();
52 static void replace();
53 static void yankstr();
54 static void badformat();
56 static int getachar();
59 /* static variables */
61 static int eflg
; /* find strings in source file(s) */
62 static int dflg
; /* use replaced string a second argument */
63 static int rflg
; /* replace strings by function calls */
64 static int errflg
; /* syntax error on command line */
65 static char *Fname
; /* name of source file */
66 static int Lineno
; /* line number in source file */
67 static int Posno
; /* character position within line */
68 static int flag
; /* sets when newline is encountered */
69 static jmp_buf to_eof
;
73 main(int argc
, char *argv
[])
77 while ((ch
= getopt(argc
, argv
, "erd")) != -1)
100 if (optind
== argc
|| errflg
)
103 for (; optind
< argc
; optind
++)
104 extract(argv
[optind
]);
106 if (optind
+1 != argc
)
108 replace(argv
[optind
]);
117 if (freopen(name
, "r", stdin
) == NULL
) {
119 stderr
, "exstr: ERROR: couldn't open file '%s'\n", name
);
126 if (setjmp(to_eof
) != 0)
140 } while (isspace(ch
));
143 while (getachar() != '\n');
149 while ((ch
= getachar()) != '\'')
160 if (level
== 0 && ch
== '*')
162 else if (level
== 1 && ch
== '/')
178 register char *dp
= dbuf
;
183 saved_lineno
= Lineno
;
184 while ((cc
= getachar()) != '"') {
196 prstr(dbuf
, saved_lineno
, saved_posno
);
200 prstr(cp
, lineno
, posno
)
204 (void) fprintf(stdout
, "%s:%d:%d:::%s\n", Fname
, lineno
, posno
,
207 (void) fprintf(stdout
, "%s:%s\n", Fname
, cp
);
214 (void) fprintf(stderr
, "usage: exstr [-e] files\n");
215 (void) fprintf(stderr
, "or : exstr -r [-d] file\n");
243 char linebuf
[BUFSIZ
];
245 int curlineno
; /* line number in strings file */
246 int curposno
; /* character position in string file */
248 int curmsgno
; /* message number in strings file */
249 int wrong_msg
; /* invalid message number */
250 int cont_str
= 0; /* string continues in the next line */
252 char repbuf
[BUFSIZ
], *repbufp
;
253 char curline
[BUFSIZ
];
255 /* keeps track of character position within input file */
257 /* keeps track of character position within output buffer */
260 FILE *fi
; /* input source file pointer */
265 /* open input C source file */
266 if ((fi
= fopen(name
, "r")) == (FILE *)NULL
) {
267 (void) fprintf(stderr
,
268 "exstr: ERROR: couldn't open file '%s'\n", name
);
273 (void) fprintf(stdout
, "extern char *gettxt();\n");
275 /* process file containing the list of strings */
276 while (fgets(repbuf
, sizeof (repbuf
), stdin
) != (char *)NULL
) {
280 /* save a copy of the current line */
281 (void) strcpy(curline
, repbuf
);
283 /* take apart the input string */
284 repbufp
= strchr(repbuf
, ':');
285 if (repbufp
== (char *)NULL
)
288 /* verify that string belongs to the input C source file */
289 if (strcmp(repbuf
, name
) != NULL
)
291 repstr
= strchr(repbufp
, ':');
292 if (repstr
== (char *)NULL
)
295 curlineno
= atoi(repbufp
);
296 if (curlineno
< savelineno
) {
297 (void) fprintf(stderr
,
298 "exstr: ERROR: stdin: line out of order\n");
299 (void) fprintf(stderr
, "%s", curline
);
302 savelineno
= curlineno
;
304 repstr
= strchr(repbufp
, ':');
305 if (repstr
== (char *)NULL
)
307 repstr
[strlen(repstr
) - 1 ] = '\0';
309 curposno
= atoi(repbufp
);
311 repstr
= strchr(repbufp
, ':');
312 if (repstr
== (char *)NULL
)
316 if (strlen(msgfile
) > (size_t)14 || *msgfile
== '\0') {
317 (void) fprintf(stderr
,
318 "exstr: ERROR: stdin: invalid message file name "
320 (void) fprintf(stderr
, "%s", curline
);
324 repstr
= strchr(repbufp
, ':');
325 if (repstr
== (char *)NULL
)
330 if (!isdigit(*cp
++)) {
334 if (*repbufp
== '\0' || wrong_msg
) {
335 (void) fprintf(stderr
, "exstr: ERROR: stdin: invalid "
336 "message number '%s'\n", repbufp
);
337 (void) fprintf(stderr
, "%s", curline
);
340 curmsgno
= atoi(repbufp
);
342 /* move up to this line */
343 while (Lineno
!= curlineno
) {
344 if (outp
!= outbuf
) {
348 (void) fputs(outbuf
, stdout
);
349 } else if (*linebuf
!= '\0')
350 (void) fputs(linebuf
, stdout
);
354 sizeof (linebuf
), fi
) == (char *)NULL
) {
355 (void) fprintf(stderr
, "read error\n");
361 if (Posno
> curposno
) {
362 (void) fprintf(stderr
,
363 "Bad input record line number %d\n", Lineno
);
366 while (Posno
!= curposno
) {
371 (void) fprintf(stderr
, "exstr: ERROR: cannot replace "
372 "string '%s' in line (%d) of file (%s)\n", repstr
,
376 /* check if string continues in next line */
377 while (inp
[strlen(inp
)-2] == '\\' &&
378 inp
[strlen(inp
)-1] == '\n') {
380 sizeof (linebuf
), fi
) == (char *)NULL
) {
381 (void) fprintf(stderr
, "exstr: ERROR: read "
382 "error in file (%s)\n", Fname
);
390 while (*cp
!= '\0' && *cp
++ != '"')
393 (void) fprintf(stderr
, "exstr: ERROR: cannot "
394 "replace string '%s' in line (%d) of file "
395 "(%s)\n", repstr
, Lineno
, Fname
);
399 Posno
= cp
- linebuf
;
402 outp
+= snprintf(outp
, BUFSIZ
- (outp
- outbuf
),
403 "gettxt(\"%s:%d\", \"%s\")", msgfile
, curmsgno
,
406 outp
+= snprintf(outp
, BUFSIZ
- (outp
- outbuf
),
407 "gettxt(\"%s:%d\", \"\")", msgfile
, curmsgno
);
409 inp
+= strlen(repstr
)+2;
410 Posno
+= strlen(repstr
)+2;
415 if (outp
!= outbuf
) {
419 (void) fputs(outbuf
, stdout
);
421 while (fgets(linebuf
, sizeof (linebuf
), fi
) != (char *)NULL
)
422 (void) fputs(linebuf
, stdout
);
431 (void) fprintf(stderr
, "exstr: ERROR: stdin: Badly formatted "
432 "replacement string\n%s", line
);