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 */
33 #include <sys/types.h>
38 /* external functions */
45 extern int optind
, opterr
;
47 /* static functions */
49 static void extract();
50 static void replace();
51 static void yankstr();
52 static void badformat();
54 static int getachar();
57 /* static variables */
59 static int eflg
; /* find strings in source file(s) */
60 static int dflg
; /* use replaced string a second argument */
61 static int rflg
; /* replace strings by function calls */
62 static int errflg
; /* syntax error on command line */
63 static char *Fname
; /* name of source file */
64 static int Lineno
; /* line number in source file */
65 static int Posno
; /* character position within line */
66 static int flag
; /* sets when newline is encountered */
67 static jmp_buf to_eof
;
71 main(int argc
, char *argv
[])
75 while ((ch
= getopt(argc
, argv
, "erd")) != -1)
98 if (optind
== argc
|| errflg
)
101 for (; optind
< argc
; optind
++)
102 extract(argv
[optind
]);
104 if (optind
+1 != argc
)
106 replace(argv
[optind
]);
115 if (freopen(name
, "r", stdin
) == NULL
) {
117 stderr
, "exstr: ERROR: couldn't open file '%s'\n", name
);
124 if (setjmp(to_eof
) != 0)
138 } while (isspace(ch
));
141 while (getachar() != '\n');
147 while ((ch
= getachar()) != '\'')
158 if (level
== 0 && ch
== '*')
160 else if (level
== 1 && ch
== '/')
176 register char *dp
= dbuf
;
181 saved_lineno
= Lineno
;
182 while ((cc
= getachar()) != '"') {
194 prstr(dbuf
, saved_lineno
, saved_posno
);
198 prstr(cp
, lineno
, posno
)
202 (void) fprintf(stdout
, "%s:%d:%d:::%s\n", Fname
, lineno
, posno
,
205 (void) fprintf(stdout
, "%s:%s\n", Fname
, cp
);
212 (void) fprintf(stderr
, "usage: exstr [-e] files\n");
213 (void) fprintf(stderr
, "or : exstr -r [-d] file\n");
241 char linebuf
[BUFSIZ
];
243 int curlineno
; /* line number in strings file */
244 int curposno
; /* character position in string file */
246 int curmsgno
; /* message number in strings file */
247 int wrong_msg
; /* invalid message number */
248 int cont_str
= 0; /* string continues in the next line */
250 char repbuf
[BUFSIZ
], *repbufp
;
251 char curline
[BUFSIZ
];
253 /* keeps track of character position within input file */
255 /* keeps track of character position within output buffer */
258 FILE *fi
; /* input source file pointer */
263 /* open input C source file */
264 if ((fi
= fopen(name
, "r")) == NULL
) {
265 (void) fprintf(stderr
,
266 "exstr: ERROR: couldn't open file '%s'\n", name
);
271 (void) fprintf(stdout
, "extern char *gettxt();\n");
273 /* process file containing the list of strings */
274 while (fgets(repbuf
, sizeof (repbuf
), stdin
) != NULL
) {
278 /* save a copy of the current line */
279 (void) strcpy(curline
, repbuf
);
281 /* take apart the input string */
282 repbufp
= strchr(repbuf
, ':');
286 /* verify that string belongs to the input C source file */
287 if (strcmp(repbuf
, name
) != 0)
289 repstr
= strchr(repbufp
, ':');
293 curlineno
= atoi(repbufp
);
294 if (curlineno
< savelineno
) {
295 (void) fprintf(stderr
,
296 "exstr: ERROR: stdin: line out of order\n");
297 (void) fprintf(stderr
, "%s", curline
);
300 savelineno
= curlineno
;
302 repstr
= strchr(repbufp
, ':');
305 repstr
[strlen(repstr
) - 1 ] = '\0';
307 curposno
= atoi(repbufp
);
309 repstr
= strchr(repbufp
, ':');
314 if (strlen(msgfile
) > (size_t)14 || *msgfile
== '\0') {
315 (void) fprintf(stderr
,
316 "exstr: ERROR: stdin: invalid message file name "
318 (void) fprintf(stderr
, "%s", curline
);
322 repstr
= strchr(repbufp
, ':');
328 if (!isdigit(*cp
++)) {
332 if (*repbufp
== '\0' || wrong_msg
) {
333 (void) fprintf(stderr
, "exstr: ERROR: stdin: invalid "
334 "message number '%s'\n", repbufp
);
335 (void) fprintf(stderr
, "%s", curline
);
338 curmsgno
= atoi(repbufp
);
340 /* move up to this line */
341 while (Lineno
!= curlineno
) {
342 if (outp
!= outbuf
) {
346 (void) fputs(outbuf
, stdout
);
347 } else if (*linebuf
!= '\0')
348 (void) fputs(linebuf
, stdout
);
352 sizeof (linebuf
), fi
) == NULL
) {
353 (void) fprintf(stderr
, "read error\n");
359 if (Posno
> curposno
) {
360 (void) fprintf(stderr
,
361 "Bad input record line number %d\n", Lineno
);
364 while (Posno
!= curposno
) {
369 (void) fprintf(stderr
, "exstr: ERROR: cannot replace "
370 "string '%s' in line (%d) of file (%s)\n", repstr
,
374 /* check if string continues in next line */
375 while (inp
[strlen(inp
)-2] == '\\' &&
376 inp
[strlen(inp
)-1] == '\n') {
378 sizeof (linebuf
), fi
) == NULL
) {
379 (void) fprintf(stderr
, "exstr: ERROR: read "
380 "error in file (%s)\n", Fname
);
388 while (*cp
!= '\0' && *cp
++ != '"')
391 (void) fprintf(stderr
, "exstr: ERROR: cannot "
392 "replace string '%s' in line (%d) of file "
393 "(%s)\n", repstr
, Lineno
, Fname
);
397 Posno
= cp
- linebuf
;
400 outp
+= snprintf(outp
, BUFSIZ
- (outp
- outbuf
),
401 "gettxt(\"%s:%d\", \"%s\")", msgfile
, curmsgno
,
404 outp
+= snprintf(outp
, BUFSIZ
- (outp
- outbuf
),
405 "gettxt(\"%s:%d\", \"\")", msgfile
, curmsgno
);
407 inp
+= strlen(repstr
)+2;
408 Posno
+= strlen(repstr
)+2;
413 if (outp
!= outbuf
) {
417 (void) fputs(outbuf
, stdout
);
419 while (fgets(linebuf
, sizeof (linebuf
), fi
) != NULL
)
420 (void) fputs(linebuf
, stdout
);
429 (void) fprintf(stderr
, "exstr: ERROR: stdin: Badly formatted "
430 "replacement string\n%s", line
);