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