blob: 619a0ec4ff9770e7258d26ec681386add6550a9b [file] [log] [blame]
yu.dongc33b3072024-08-21 23:14:49 -07001#!/usr/bin/perl
2#
3# Copyright Statement:
4# --------------------
5# This software is protected by Copyright and the information contained
6# herein is confidential. The software may not be copied and the information
7# contained herein may not be used or disclosed except with the written
8# permission of MediaTek Inc. (C) 2005
9#
10# BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
11# THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
12# RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
13# AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
14# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
15# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
16# NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
17# SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
18# SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
19# THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
20# NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
21# SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
22#
23# BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
24# LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
25# AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
26# OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
27# MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
28#
29# THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
30# WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
31# LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
32# RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
33# THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
34#
35#
36#*****************************************************************************
37#*****************************************************************************
38##*
39##* Filename:
40##* ---------
41##* cat.pl
42##*
43##* Project:
44##* --------
45##* Maui_Software
46##*
47##* Description:
48##* ------------
49##* This script concatenate files and print on the standard output
50##
51##* Author:
52##* -------
53##* kk Lin-Wang (mtk04222)
54##*
55##*============================================================================
56use strict;
57use warnings;
58
59my @target = "";
60my $column = -1;
61my $binmode = -1;
62# parse parameters
63foreach my $arg (@ARGV)
64{
65 if ($arg =~ /^\-cut=(\d+)\b/i)
66 {
67 print "$1\n";
68 $column = $1;
69 ($column < 0) && &Usage;
70 }
71 elsif ($arg =~ /^-binary\b/i)
72 {
73 $binmode = 1;
74 }
75 else
76 {
77 push(@target,$arg);
78 }
79}
80($#target < 0) && &Usage;
81
82# replace backslash with slash
83foreach my $arg (@target)
84{
85 $arg =~ s/\\/\//g;
86}
87
88# print each file content
89my @fs = glob("@target");
90&printAllContent(@fs) if($column == -1);
91&printCutContent(@fs) if($column != -1);
92
93
94#******************************************************************************
95## FUNCTION
96## printAllContent
97##******************************************************************************
98sub printAllContent
99{
100 foreach my $file (@_)
101 {
102 open FH, $file or die "[cat.pl][Error:] Failed to open [$file]";
103 if($binmode eq 1)
104 {
105 binmode FH;
106 binmode STDOUT;
107 }
108 print while(<FH>);
109 close FH;
110 }
111}
112
113
114#******************************************************************************
115## FUNCTION
116## printCutContent
117##******************************************************************************
118sub printCutContent
119{
120 my @line = ();
121 foreach my $file (@_)
122 {
123 open FH, $file or die "[cat.pl][Error:] Failed to open [$file]";
124 while(<FH>)
125 {
126 @line = split(' ',$_);
127 next if($#line < $column);
128 if($line[$column] ne "")
129 {
130 print $line[$column];
131 print "\n";
132 }
133 }
134 close FH;
135 }
136}
137
138sub Usage
139{
140 warn <<"_END_OF_USAGE";
141Usage:
142 cat.pl [source[file1, files2]]
143Example:
144 perl cat.pl file1.c file2.c
145 perl cat.pl *.cpp *.c
146 perl cat.pl -cut=1 in.txt
147 perl cat.pl -binary foo.bin
148Description:
149 This script concatenate files and print on the standard output.
150
151 -cut=number only output the [#number] column in each line.
152 -binary read/write as binary mode
153 [ex1]:
154 in.txt:
155 string0 string1 string2
156 dog cat frog
157 command:
158 perl cat.pl -cut=1 in.txt
159 output:
160 string1
161 cat
162
163_END_OF_USAGE
164 exit 1;
165}