1*c54f35caSApple OSS Distributions#!/usr/bin/awk -f 2*c54f35caSApple OSS Distributions 3*c54f35caSApple OSS Distributions# Usage: foo <template> <file> 4*c54f35caSApple OSS Distributions# Searches through file for instances of 'kern_return_t $FOO' 5*c54f35caSApple OSS Distributions# where $FOO is an line in the template file 6*c54f35caSApple OSS Distributions# and prepends the first line in the template file. 7*c54f35caSApple OSS Distributions 8*c54f35caSApple OSS Distributions# Example template format: 9*c54f35caSApple OSS Distributions# %{ 10*c54f35caSApple OSS Distributions# __WATCHOS_PROHIBITED 11*c54f35caSApple OSS Distributions# %} 12*c54f35caSApple OSS Distributions# act_get_state 13*c54f35caSApple OSS Distributions# thread_get_state 14*c54f35caSApple OSS Distributions# 15*c54f35caSApple OSS Distributions 16*c54f35caSApple OSS Distributions# BEGIN { print ARGV[1]; print ARGV[2] } 17*c54f35caSApple OSS Distributions 18*c54f35caSApple OSS Distributions# In the first file, build array of lines 19*c54f35caSApple OSS DistributionsNR==FNR && /^ *$/ { 20*c54f35caSApple OSS Distributions next 21*c54f35caSApple OSS Distributions} 22*c54f35caSApple OSS DistributionsNR==FNR && /^#/ { 23*c54f35caSApple OSS Distributions next 24*c54f35caSApple OSS Distributions} 25*c54f35caSApple OSS DistributionsNR==FNR && /%{/ { 26*c54f35caSApple OSS Distributions parse_prefix = 1 27*c54f35caSApple OSS Distributions prefix = "" 28*c54f35caSApple OSS Distributions next 29*c54f35caSApple OSS Distributions} 30*c54f35caSApple OSS DistributionsNR==FNR && /^%}/ { 31*c54f35caSApple OSS Distributions parse_prefix = 0 32*c54f35caSApple OSS Distributions next 33*c54f35caSApple OSS Distributions} 34*c54f35caSApple OSS DistributionsNR==FNR { 35*c54f35caSApple OSS Distributions if (parse_prefix && length(prefix)) { 36*c54f35caSApple OSS Distributions prefix = sprintf("%s\n%s", prefix, $0) 37*c54f35caSApple OSS Distributions } else if (parse_prefix) { 38*c54f35caSApple OSS Distributions prefix = $0 39*c54f35caSApple OSS Distributions } else if (length(templates[$0])) { 40*c54f35caSApple OSS Distributions templates[$0] = sprintf("%s\n%s", templates[$0], prefix) 41*c54f35caSApple OSS Distributions } else { 42*c54f35caSApple OSS Distributions templates[$0] = prefix 43*c54f35caSApple OSS Distributions } 44*c54f35caSApple OSS Distributions next 45*c54f35caSApple OSS Distributions} 46*c54f35caSApple OSS Distributions 47*c54f35caSApple OSS Distributions# In the second file, match kern_return_t <template> 48*c54f35caSApple OSS Distributions# at the beginning of the line 49*c54f35caSApple OSS Distributions# print the prefix line if found 50*c54f35caSApple OSS Distributions 51*c54f35caSApple OSS Distributions/^kern_return_t/ { 52*c54f35caSApple OSS Distributions# print "match" 53*c54f35caSApple OSS Distributions if ($2 in templates) { 54*c54f35caSApple OSS Distributions print templates[$2] 55*c54f35caSApple OSS Distributions } 56*c54f35caSApple OSS Distributions} 57*c54f35caSApple OSS Distributions 58*c54f35caSApple OSS Distributions# Pass through everything in the second file 59*c54f35caSApple OSS Distributions{ print } 60