xref: /illumos-gate/usr/src/cmd/adbgen/common/adbgen3.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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 /*
28  * Post-process adb script.
29  * All we do is collapse repeated formats into number*format.
30  * E.g. XXX is collapsed to 3X.
31  */
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 
37 int
38 main()
39 {
40 	int c, quote, paren, savec, count, dispcmd;
41 
42 	savec = count = 0;
43 	quote = 0;	/* not in quoted string */
44 	paren = 0;	/* not in parenthesized string */
45 	dispcmd = 0;	/* not in display command */
46 	while ((c = getchar()) != EOF) {
47 		if (c == '"') {
48 			quote = !quote;
49 		} else if (c == '(') {
50 			paren++;
51 		} else if (c == ')') {
52 			paren--;
53 		} else if (c == '/' || c == '?') {
54 			dispcmd = 1;
55 		} else if (c == '\n') {
56 			dispcmd = 0;
57 		}
58 		if (c == savec) {
59 			count++;
60 			continue;
61 		}
62 		if (savec) {
63 			if (count > 1) {
64 				printf("%d", count);
65 			}
66 			putchar(savec);
67 			savec = 0;
68 		}
69 		if (quote == 0 && paren == 0 && dispcmd &&
70 		    strchr("KJFXOQDUfYpPxoqdubcC+IaAtrn-", c)) {
71 			savec = c;
72 			count = 1;
73 		} else {
74 			putchar(c);
75 		}
76 	}
77 	if (savec)
78 		putchar(savec);
79 	return (0);
80 }
81