xref: /titanic_44/usr/src/cmd/adbgen/common/adbgen4.c (revision a5f69788de7ac07553de47f7fec8c05a9a94c105)
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 scripts to move increment and decrement around.
31  * The reason is that at the start of each +/ line, adb prints out
32  * the current location.  If the line then increments or decrements
33  * dot before printing the user may be confused.  So we move the
34  * increment or decrement to the preceeding line.
35  */
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <ctype.h>
41 
42 #define	BUFSIZE 1024	/* gross enough to never be exceeded (we hope) */
43 
44 char buf1[BUFSIZE], buf2[BUFSIZE];
45 
46 static char *scanpastnum(char *);
47 static int goodstart(char *);
48 
49 int
50 main()
51 {
52 	char *cur, *last, *cp1, *cp2, *ep, *t;
53 	int len;
54 
55 	gets(buf1);
56 	last = buf1;
57 	cur = buf2;
58 	while (gets(cur) != NULL) {
59 		if (goodstart(cur) && goodstart(last)) {
60 			/*
61 			 * Scan cur for initial increment.
62 			 * Ignore quoted strings, tabbing, adb newlines.
63 			 */
64 			cp1 = cur + 2;
65 			while (*cp1) {
66 				if (*cp1 == '"') {
67 					/* scan past quoted string */
68 					while (*++cp1 && *cp1 != '"')
69 						;
70 					cp1++;
71 					continue;
72 				}
73 				if (*cp1 >= '0' && *cp1 <= '9') {
74 					cp2 = scanpastnum(cp1);
75 				} else {
76 					cp2 = cp1;
77 				}
78 				if (*cp2 == 't' || *cp2 == 'n' ||
79 				    *cp2 == ' ') {
80 					/* ok to skip over this one */
81 					cp1 = cp2 + 1;
82 					continue;
83 				} else {
84 					break;
85 				}
86 			}
87 			/*
88 			 * Cp1 now points at the first non-quoted string and
89 			 * non adb tab specification.
90 			 * Now determine if it's an increment or decrement.
91 			 */
92 			cp2 = scanpastnum(cp1);
93 			if (*cp2 == '+' || *cp2 == '-') {
94 				/*
95 				 * This is what we were hoping to find.
96 				 * Move increment or decrement into last.
97 				 */
98 				cp2++;
99 				ep = last + strlen(last);
100 				len = cp2  - cp1;
101 				strncpy(ep, cp1, len);
102 				ep[len] = '\0';
103 				/*
104 				 * Remove it from cur.
105 				 */
106 				strcpy(cp1, cp2);
107 			}
108 		}
109 		/*
110 		 * Prepare for next iteration.
111 		 */
112 		puts(last);
113 		t = cur;
114 		cur = last;
115 		last = t;
116 	}
117 	puts(last);
118 	return (0);
119 }
120 
121 /*
122  * Cp points at a digit.
123  * Return pointer to next char that isn't a digit.
124  */
125 static char *
126 scanpastnum(char *cp1)
127 {
128 	char *cp2;
129 
130 	for (cp2 = cp1; isdigit(*cp2); cp2++)
131 		;
132 	return (cp2);
133 }
134 
135 /*
136  * Check whether a line has a good starting string.
137  * We need ./ or +/ at the beginning to even think
138  * of doing this motion stuff.
139  */
140 static int
141 goodstart(char *cp)
142 {
143 	if (*cp == '.' || *cp == '+') {
144 		if (*++cp == '/')
145 			return (1);
146 	}
147 	return (0);
148 }
149