main.c (24360) | main.c (28066) |
---|---|
1/*- 2 * Copyright (c) 1992 Diomidis Spinellis. 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Diomidis Spinellis of Imperial College, University of London. 8 * --- 22 unchanged lines hidden (view full) --- 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38#ifndef lint | 1/*- 2 * Copyright (c) 1992 Diomidis Spinellis. 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Diomidis Spinellis of Imperial College, University of London. 8 * --- 22 unchanged lines hidden (view full) --- 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38#ifndef lint |
39static char copyright[] = | 39static const char copyright[] = |
40"@(#) Copyright (c) 1992, 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42#endif /* not lint */ 43 44#ifndef lint | 40"@(#) Copyright (c) 1992, 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42#endif /* not lint */ 43 44#ifndef lint |
45#if 0 |
|
45static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94"; | 46static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94"; |
47#endif 48static const char rcsid[] = 49 "$Id$"; |
|
46#endif /* not lint */ 47 48#include <sys/types.h> 49 | 50#endif /* not lint */ 51 52#include <sys/types.h> 53 |
54#include <err.h> |
|
50#include <errno.h> 51#include <fcntl.h> 52#include <locale.h> 53#include <regex.h> 54#include <stddef.h> 55#include <stdio.h> 56#include <stdlib.h> 57#include <string.h> --- 38 unchanged lines hidden (view full) --- 96 * units, but span across input files. 97 */ 98char *fname; /* File name. */ 99u_long linenum; 100int lastline; /* TRUE on the last line of the last file */ 101 102static void add_compunit __P((enum e_cut, char *)); 103static void add_file __P((char *)); | 55#include <errno.h> 56#include <fcntl.h> 57#include <locale.h> 58#include <regex.h> 59#include <stddef.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <string.h> --- 38 unchanged lines hidden (view full) --- 101 * units, but span across input files. 102 */ 103char *fname; /* File name. */ 104u_long linenum; 105int lastline; /* TRUE on the last line of the last file */ 106 107static void add_compunit __P((enum e_cut, char *)); 108static void add_file __P((char *)); |
109static void usage __P((void)); |
|
104 105int 106main(argc, argv) 107 int argc; 108 char *argv[]; 109{ 110 int c, fflag; 111 --- 13 unchanged lines hidden (view full) --- 125 fflag = 1; 126 add_compunit(CU_FILE, optarg); 127 break; 128 case 'n': 129 nflag = 1; 130 break; 131 default: 132 case '?': | 110 111int 112main(argc, argv) 113 int argc; 114 char *argv[]; 115{ 116 int c, fflag; 117 --- 13 unchanged lines hidden (view full) --- 131 fflag = 1; 132 add_compunit(CU_FILE, optarg); 133 break; 134 case 'n': 135 nflag = 1; 136 break; 137 default: 138 case '?': |
133 (void)fprintf(stderr, 134"usage:\tsed script [-an] [file ...]\n\tsed [-an] [-e script] ... [-f script_file] ... [file ...]\n"); 135 exit(1); | 139 usage(); |
136 } 137 argc -= optind; 138 argv += optind; 139 140 /* First usage case; script is the first arg */ 141 if (!eflag && !fflag && *argv) { 142 add_compunit(CU_STRING, *argv); 143 argv++; --- 5 unchanged lines hidden (view full) --- 149 if (*argv) 150 for (; *argv; argv++) 151 add_file(*argv); 152 else 153 add_file(NULL); 154 process(); 155 cfclose(prog, NULL); 156 if (fclose(stdout)) | 140 } 141 argc -= optind; 142 argv += optind; 143 144 /* First usage case; script is the first arg */ 145 if (!eflag && !fflag && *argv) { 146 add_compunit(CU_STRING, *argv); 147 argv++; --- 5 unchanged lines hidden (view full) --- 153 if (*argv) 154 for (; *argv; argv++) 155 add_file(*argv); 156 else 157 add_file(NULL); 158 process(); 159 cfclose(prog, NULL); 160 if (fclose(stdout)) |
157 err(FATAL, "stdout: %s", strerror(errno)); | 161 err(1, "stdout"); |
158 exit (0); 159} 160 | 162 exit (0); 163} 164 |
165static void 166usage() 167{ 168 (void)fprintf(stderr, "%s\n%s\n", 169 "usage: sed script [-an] [file ...]", 170 " sed [-an] [-e script] ... [-f script_file] ... [file ...]"); 171 exit(1); 172} 173 |
|
161/* 162 * Like fgets, but go through the chain of compilation units chaining them 163 * together. Empty strings and files are ignored. 164 */ 165char * 166cu_fgets(buf, n) 167 char *buf; 168 int n; --- 8 unchanged lines hidden (view full) --- 177 switch (state) { 178 case ST_EOF: 179 if (script == NULL) 180 return (NULL); 181 linenum = 0; 182 switch (script->type) { 183 case CU_FILE: 184 if ((f = fopen(script->s, "r")) == NULL) | 174/* 175 * Like fgets, but go through the chain of compilation units chaining them 176 * together. Empty strings and files are ignored. 177 */ 178char * 179cu_fgets(buf, n) 180 char *buf; 181 int n; --- 8 unchanged lines hidden (view full) --- 190 switch (state) { 191 case ST_EOF: 192 if (script == NULL) 193 return (NULL); 194 linenum = 0; 195 switch (script->type) { 196 case CU_FILE: 197 if ((f = fopen(script->s, "r")) == NULL) |
185 err(FATAL, 186 "%s: %s", script->s, strerror(errno)); | 198 err(1, "%s", script->s); |
187 fname = script->s; 188 state = ST_FILE; 189 goto again; 190 case CU_STRING: 191 if ((snprintf(string_ident, 192 sizeof(string_ident), "\"%s\"", script->s)) >= 193 sizeof(string_ident) - 1) 194 (void)strcpy(string_ident + --- 43 unchanged lines hidden (view full) --- 238 linenum++; 239 return (buf); 240 default: 241 *p++ = *s++; 242 } 243 } 244 } 245 /* NOTREACHED */ | 199 fname = script->s; 200 state = ST_FILE; 201 goto again; 202 case CU_STRING: 203 if ((snprintf(string_ident, 204 sizeof(string_ident), "\"%s\"", script->s)) >= 205 sizeof(string_ident) - 1) 206 (void)strcpy(string_ident + --- 43 unchanged lines hidden (view full) --- 250 linenum++; 251 return (buf); 252 default: 253 *p++ = *s++; 254 } 255 } 256 } 257 /* NOTREACHED */ |
258 return (NULL); |
|
246} 247 248/* 249 * Like fgets, but go through the list of files chaining them together. 250 * Set len to the length of the line. 251 */ 252int 253mf_fgets(sp, spflag) --- 13 unchanged lines hidden (view full) --- 267 return (0); 268 } 269 if (files->fname == NULL) { 270 f = stdin; 271 fname = "stdin"; 272 } else { 273 fname = files->fname; 274 if ((f = fopen(fname, "r")) == NULL) | 259} 260 261/* 262 * Like fgets, but go through the list of files chaining them together. 263 * Set len to the length of the line. 264 */ 265int 266mf_fgets(sp, spflag) --- 13 unchanged lines hidden (view full) --- 280 return (0); 281 } 282 if (files->fname == NULL) { 283 f = stdin; 284 fname = "stdin"; 285 } else { 286 fname = files->fname; 287 if ((f = fopen(fname, "r")) == NULL) |
275 err(FATAL, "%s: %s", 276 fname, strerror(errno)); | 288 err(1, "%s", fname); |
277 } 278 if ((c = getc(f)) != EOF) { 279 (void)ungetc(c, f); 280 break; 281 } 282 (void)fclose(f); 283 files = files->next; 284 } --- 5 unchanged lines hidden (view full) --- 290 291 /* 292 * Use fgetln so that we can handle essentially infinite input data. 293 * Can't use the pointer into the stdio buffer as the process space 294 * because the ungetc() can cause it to move. 295 */ 296 p = fgetln(f, &len); 297 if (ferror(f)) | 289 } 290 if ((c = getc(f)) != EOF) { 291 (void)ungetc(c, f); 292 break; 293 } 294 (void)fclose(f); 295 files = files->next; 296 } --- 5 unchanged lines hidden (view full) --- 302 303 /* 304 * Use fgetln so that we can handle essentially infinite input data. 305 * Can't use the pointer into the stdio buffer as the process space 306 * because the ungetc() can cause it to move. 307 */ 308 p = fgetln(f, &len); 309 if (ferror(f)) |
298 err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO)); | 310 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); |
299 cspace(sp, p, len, spflag); 300 301 linenum++; 302 /* Advance to next non-empty file */ 303 while ((c = getc(f)) == EOF) { 304 (void)fclose(f); 305 files = files->next; 306 if (files == NULL) { 307 lastline = 1; 308 return (1); 309 } 310 if (files->fname == NULL) { 311 f = stdin; 312 fname = "stdin"; 313 } else { 314 fname = files->fname; 315 if ((f = fopen(fname, "r")) == NULL) | 311 cspace(sp, p, len, spflag); 312 313 linenum++; 314 /* Advance to next non-empty file */ 315 while ((c = getc(f)) == EOF) { 316 (void)fclose(f); 317 files = files->next; 318 if (files == NULL) { 319 lastline = 1; 320 return (1); 321 } 322 if (files->fname == NULL) { 323 f = stdin; 324 fname = "stdin"; 325 } else { 326 fname = files->fname; 327 if ((f = fopen(fname, "r")) == NULL) |
316 err(FATAL, "%s: %s", fname, strerror(errno)); | 328 err(1, "%s", fname); |
317 } 318 } 319 (void)ungetc(c, f); 320 return (1); 321} 322 323/* 324 * Add a compilation unit to the linked list --- 31 unchanged lines hidden --- | 329 } 330 } 331 (void)ungetc(c, f); 332 return (1); 333} 334 335/* 336 * Add a compilation unit to the linked list --- 31 unchanged lines hidden --- |