xref: /titanic_50/usr/src/cmd/adbgen/common/adbgen3.c (revision 29949e866e40b95795203f3ee46f44a197c946e4)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Post-process adb script.
31  * All we do is collapse repeated formats into number*format.
32  * E.g. XXX is collapsed to 3X.
33  */
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 
39 int
40 main()
41 {
42 	int c, quote, paren, savec, count, dispcmd;
43 
44 	savec = count = 0;
45 	quote = 0;	/* not in quoted string */
46 	paren = 0;	/* not in parenthesized string */
47 	dispcmd = 0;	/* not in display command */
48 	while ((c = getchar()) != EOF) {
49 		if (c == '"') {
50 			quote = !quote;
51 		} else if (c == '(') {
52 			paren++;
53 		} else if (c == ')') {
54 			paren--;
55 		} else if (c == '/' || c == '?') {
56 			dispcmd = 1;
57 		} else if (c == '\n') {
58 			dispcmd = 0;
59 		}
60 		if (c == savec) {
61 			count++;
62 			continue;
63 		}
64 		if (savec) {
65 			if (count > 1) {
66 				printf("%d", count);
67 			}
68 			putchar(savec);
69 			savec = 0;
70 		}
71 		if (quote == 0 && paren == 0 && dispcmd &&
72 		    strchr("KJFXOQDUfYpPxoqdubcC+IaAtrn-", c)) {
73 			savec = c;
74 			count = 1;
75 		} else {
76 			putchar(c);
77 		}
78 	}
79 	if (savec)
80 		putchar(savec);
81 	return (0);
82 }
83