xref: /netbsd-src/sys/fs/nfs/nfs2netbsd.sh (revision e63709eb768ae522e98d55346ff031ce49a42a74)
1#!/bin/sh
2# nfs2netbsd - arrange bits of (FreeBSD's) newnfs code for importing
3# usage: nfs2netbsd FREEBSDSYSDIR
4#
5# Caution: unlike most of the *2netbsd scripts in the tree, this copies
6# from another dir (which should be the sys/ dir from a FreeBSD checkout)
7# rather than operating on a tree already in the current directory.
8#
9# The current directory should be empty.
10#
11# $NetBSD: nfs2netbsd.sh,v 1.1 2013/09/30 07:23:37 dholland Exp $
12
13FTOP="$1"
14
15if [ $# != 1 ]; then
16    echo "$0: usage: $0 freebsdsysdir" 1>&2
17    exit 1
18fi
19
20if [ ! -d "$FTOP" ]; then
21    echo "$0: $FTOP: not found" 1>&2
22    exit 1
23fi
24
25############################################################
26# 1. Get the list of files.
27
28# Note that we don't (for now anyway) take rpc/* and xdr/*.
29
30FILES=$(egrep -w 'nfscl|nfsd' "$FTOP"/conf/files | awk '{ print $1 }' |\
31	sed '/^rpc\//d;/^xdr\//d')
32
33DIRS=$(echo "$FILES" | sed 's,/[^/*]*$,,' | sort -u)
34
35MOREFILES=$(cd "$FTOP" && find $DIRS -name '*.h' -print)
36
37FILES="$FILES $MOREFILES"
38
39############################################################
40# 2. Create the directories to copy into.
41
42ALLDIRS=$(echo "$DIRS" | awk -F/ '
43    {
44	path = sep = "";
45	for (i=1;i<=NF;i++) {
46	    path = path sep $i;
47	    sep = "/";
48	    print path;
49	}
50    }
51' | sort -u)
52
53for D in $ALLDIRS; do
54    echo "MKDIR   $D"
55    mkdir "$D" || exit 1
56done
57
58############################################################
59# 3. Copy the files.
60
61# In the course of copying, strip the dollar-signs from FreeBSD RCS
62# tags and add a NetBSD tag.
63
64for F in $FILES; do
65    echo "COPY    $F"
66    awk < "$FTOP"/"$F" '
67	function detag() {
68	    gsub("\\$", "", $0);
69	}
70	function commentout() {
71	    $0 = "/* " $0 " */";
72	}
73	BEGIN {
74	    first = 1;
75	}
76
77	# there are a handful of netbsd __RCSID()s in the input
78	/__RCSID(.*NetBSD:.*)/ {
79	    detag();
80	    commentout();
81	    print;
82	    first = 0;
83	    next;
84	}
85	/__FBSDID(.*FreeBSD:.*)/ {
86	    detag();
87	    commentout();
88	    print;
89	    printf "__RCSID(\"%sNetBSD%s\");\n", "$", "$";
90	    first = 0;
91	    next;
92	}
93	/\$NetBSD.*\$/ {
94	    detag();
95	    print;
96	    first = 0;
97	    next;
98	}
99	/\$FreeBSD.*\$/ {
100	    orig = $0;
101	    detag();
102	    print;
103	    sub("FreeBSD:.*\\$", "NetBSD$", orig);
104	    print orig;
105	    first = 0;
106	    next;
107	}
108	first {
109	    printf "/*\t%sNetBSD%s\t*/\n", "$", "$";
110	    print;
111	    first = 0;
112	    next;
113	}
114	{ print; }
115    ' "name=$F" > "$F"
116done
117
118# If you need to diff the files against the freebsd tree for some
119# reason, e.g. because you needed to debug the awk script above,
120# uncomment this for testing.
121#exit 3
122
123############################################################
124# 4. Move the files around the way we want them.
125
126# Be sure to reflect changes in this section into section 5.
127
128
129# If these fail, it means the script needs to be updated...
130mv nfs/nfsproto.h nfs/oldnfsproto.h || exit 1
131mv nfs/xdr_subs.h nfs/old_xdr_subs.h || exit 1
132
133# Make sure nothing in nfs/ and fs/nfs/ overlaps as we're going
134# to merge those dirs.
135
136BAD=$( (
137	(cd nfs && ls)
138	(cd fs/nfs && ls)
139    ) | sort | uniq -d)
140if [ x"$BAD" != x ]; then
141    echo "$0: The following files exist in both nfs/ and fs/nfs/:" 1>&2
142    echo "$BAD" 1>&2
143    echo "$0: Please add logic to fix this before continuing." 1>&2
144    exit 1
145fi
146
147# Now rearrange the dirs.
148
149mkdir fs/nfs/common || exit 1
150mv nfs/*.[ch] fs/nfs/common/ || exit 1
151mv fs/nfs/*.[ch] fs/nfs/common/ || exit 1
152mv fs/nfsserver fs/nfs/server || exit 1
153mv fs/nfsclient fs/nfs/client || exit 1
154mv nlm fs/nfs/nlm || exit 1
155
156rmdir nfs || exit 1
157
158############################################################
159# 5. Prepare a skeleton files.newnfs.
160
161# This helps make sure that freebsd changes in the file list
162# propagate.
163
164echo 'GEN     fs/nfs/files.newnfs'
165
166egrep -w 'nfscl|nfsd' "$FTOP"/conf/files |\
167	sed '/^rpc\//d;/^xdr\//d' | sed '
168    s,^fs/nfs/,fs/nfs/common/,
169    s,^fs/nfsclient/,fs/nfs/client/,
170    s,^fs/nfsserver/,fs/nfs/server/,
171    s,^nfs/,fs/nfs/common/,
172    s,^nlm/,fs/nfs/nlm/,
173' | sort | awk '
174    BEGIN {
175	# fbsd -> nbsd translation table for files.* tokens
176
177	# old nfs implementation
178	transtoken["nfsserver"] = "false";
179	transtoken["nfsclient"] = "false";
180
181	# new nfs implementation
182	transtoken["nfscl"] = "new_nfsclient";
183	transtoken["nfsd"] = "new_nfsserver";
184	transtoken["nfslockd"] = "new_nfslockd";
185	transtoken["nfs_root"] = "new_nfs_boot";
186	transtoken["bootp"] = "new_nfs_boot_bootp";
187
188	# other stuff
189	transtoken["inet"] = "true";
190    }
191    {
192	file = $1;
193	expr = "";
194	havetoken = 0;
195	for (i=2;i<=NF;i++) {
196	    if ($i == "optional") {
197		continue;
198	    }
199	    if ($i == "|") {
200		havetoken = 0;
201	    }
202	    else if (havetoken) {
203		expr = expr " &";
204		havetoken = 0;
205	    }
206	    else {
207		havetoken = 1;
208	    }
209	    t = $i;
210	    if (transtoken[t]) {
211		t = transtoken[t];
212	    }
213	    expr = expr " " t;
214	    seentokens[t] = 1;
215	}
216	gsub("false \\& [a-zA-Z0-9_]+ \\| ", "", expr);
217	gsub("false \\| ", "", expr);
218	gsub(" \\& true", "", expr);
219	files[++nfiles] = file;
220	exprs[file] = expr;
221    }
222
223    END {
224	# This output is not meant to be perfect; it is meant as a
225	# starting point.
226
227	printf "#\t%sNetBSD%s\n", "$", "$";
228	printf "\n";
229
230	printf "deffs NEW_NFSCLIENT\n";
231
232	sep = "defflag opt_newnfs.h\t\t\t";
233	for (t in seentokens) {
234	    if (t == "true" || t == "false" || t == "|" || t == "&") {
235		continue;
236	    }
237	    if (t == "new_nfsclient") {
238		continue;
239	    }
240	    printf "%s%s\n", sep, toupper(t);
241	    sep = "\t\t\t\t\t";
242	}
243	printf "\n";
244
245	for (i=1;i<=nfiles;i++) {
246	    printf "file\t%s", files[i];
247	    ntabs = 4 - int(length(files[i])/8);
248	    if (ntabs < 1) {
249		ntabs = 1;
250	    }
251	    for (j=0; j<ntabs; j++) {
252		printf "\t";
253	    }
254	    printf "%s\n", exprs[files[i]];
255	}
256    }
257' > fs/nfs/files.newnfs
258
259############################################################
260# 6. done
261
262mv fs/nfs/* . || exit 1
263rmdir fs/nfs fs || exit 1
264
265echo "Now do:"
266echo "   cvs -d cvs.netbsd.org:/cvsroot import src/sys/fs/nfs FREEBSD FREEBSD-NNNNNN"
267echo "where NNNNNN is the subversion version number."
268