Tue Mar 6 23:40:47 2012 UTC ()
Change to use C89 concatenated strings.


(matt)
diff -r1.9 -r1.9.38.1 src/distrib/utils/more/main.c

cvs diff -r1.9 -r1.9.38.1 src/distrib/utils/more/main.c (switch to unified diff)

--- src/distrib/utils/more/main.c 2004/09/01 01:46:56 1.9
+++ src/distrib/utils/more/main.c 2012/03/06 23:40:47 1.9.38.1
@@ -1,361 +1,361 @@ @@ -1,361 +1,361 @@
1/* $NetBSD: main.c,v 1.9 2004/09/01 01:46:56 chs Exp $ */ 1/* main.c,v 1.9 2004/09/01 01:46:56 chs Exp */
2 2
3/* 3/*
4 * Copyright (c) 1988 Mark Nudelman 4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993 5 * Copyright (c) 1988, 1993
6 * Regents of the University of California. All rights reserved. 6 * Regents of the University of California. All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors 16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software 17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission. 18 * without specific prior written permission.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE. 30 * SUCH DAMAGE.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34#ifndef lint 34#ifndef lint
35__COPYRIGHT(\ 35__COPYRIGHT(
36"@(#) Copyright (c) 1988 Mark Nudelman.\n\ 36"@(#) Copyright (c) 1988 Mark Nudelman.\n"
37@(#) Copyright (c) 1988, 1993\n\ 37"@(#) Copyright (c) 1988, 1993\n"
38 Regents of the University of California. All rights reserved.\n"); 38" Regents of the University of California. All rights reserved.\n");
39#endif /* not lint */ 39#endif /* not lint */
40 40
41#ifndef lint 41#ifndef lint
42#if 0 42#if 0
43static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/7/93"; 43static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/7/93";
44#else 44#else
45__RCSID("$NetBSD: main.c,v 1.9 2004/09/01 01:46:56 chs Exp $"); 45__RCSID("main.c,v 1.9 2004/09/01 01:46:56 chs Exp");
46#endif 46#endif
47#endif /* not lint */ 47#endif /* not lint */
48 48
49/* 49/*
50 * Entry point, initialization, miscellaneous routines. 50 * Entry point, initialization, miscellaneous routines.
51 */ 51 */
52 52
53#include <sys/types.h> 53#include <sys/types.h>
54#include <sys/file.h> 54#include <sys/file.h>
55#include <stdio.h> 55#include <stdio.h>
56#include <string.h> 56#include <string.h>
57#include <errno.h> 57#include <errno.h>
58#include <stdlib.h> 58#include <stdlib.h>
59#include <unistd.h> 59#include <unistd.h>
60 60
61#include "less.h" 61#include "less.h"
62#include "extern.h" 62#include "extern.h"
63 63
64int ispipe; 64int ispipe;
65int new_file; 65int new_file;
66int is_tty; 66int is_tty;
67char *current_file, *previous_file, *current_name, *next_name; 67char *current_file, *previous_file, *current_name, *next_name;
68off_t prev_pos; 68off_t prev_pos;
69int any_display; 69int any_display;
70int scroll_lines; 70int scroll_lines;
71int ac; 71int ac;
72char **av; 72char **av;
73int curr_ac; 73int curr_ac;
74int quitting; 74int quitting;
75 75
76static void cat_file __P((void)); 76static void cat_file __P((void));
77/* 77/*
78 * Edit a new file. 78 * Edit a new file.
79 * Filename "-" means standard input. 79 * Filename "-" means standard input.
80 * No filename means the "current" file, from the command line. 80 * No filename means the "current" file, from the command line.
81 */ 81 */
82int 82int
83edit(filename) 83edit(filename)
84 char *filename; 84 char *filename;
85{ 85{
86 int f; 86 int f;
87 char *m; 87 char *m;
88 off_t initial_pos; 88 off_t initial_pos;
89 static int didpipe; 89 static int didpipe;
90 char message[100], *p; 90 char message[100], *p;
91 91
92 initial_pos = NULL_POSITION; 92 initial_pos = NULL_POSITION;
93 if (filename == NULL || *filename == '\0') { 93 if (filename == NULL || *filename == '\0') {
94 if (curr_ac >= ac) { 94 if (curr_ac >= ac) {
95 error("No current file"); 95 error("No current file");
96 return(0); 96 return(0);
97 } 97 }
98 filename = save(av[curr_ac]); 98 filename = save(av[curr_ac]);
99 } 99 }
100 else if (strcmp(filename, "#") == 0) { 100 else if (strcmp(filename, "#") == 0) {
101 if (*previous_file == '\0') { 101 if (*previous_file == '\0') {
102 error("no previous file"); 102 error("no previous file");
103 return(0); 103 return(0);
104 } 104 }
105 filename = save(previous_file); 105 filename = save(previous_file);
106 initial_pos = prev_pos; 106 initial_pos = prev_pos;
107 } else 107 } else
108 filename = save(filename); 108 filename = save(filename);
109 109
110 /* use standard input. */ 110 /* use standard input. */
111 if (!strcmp(filename, "-")) { 111 if (!strcmp(filename, "-")) {
112 if (didpipe) { 112 if (didpipe) {
113 error("Can view standard input only once"); 113 error("Can view standard input only once");
114 return(0); 114 return(0);
115 } 115 }
116 f = 0; 116 f = 0;
117 } 117 }
118 else if ((m = bad_file(filename, message, sizeof(message))) != NULL) { 118 else if ((m = bad_file(filename, message, sizeof(message))) != NULL) {
119 error(m); 119 error(m);
120 free(filename); 120 free(filename);
121 return(0); 121 return(0);
122 } 122 }
123 else if ((f = open(filename, O_RDONLY, 0)) < 0) { 123 else if ((f = open(filename, O_RDONLY, 0)) < 0) {
124 (void)snprintf(message, sizeof(message), "%s: %s", filename, 124 (void)snprintf(message, sizeof(message), "%s: %s", filename,
125 strerror(errno)); 125 strerror(errno));
126 error(message); 126 error(message);
127 free(filename); 127 free(filename);
128 return(0); 128 return(0);
129 } 129 }
130 130
131 if (isatty(f)) { 131 if (isatty(f)) {
132 /* 132 /*
133 * Not really necessary to call this an error, 133 * Not really necessary to call this an error,
134 * but if the control terminal (for commands) 134 * but if the control terminal (for commands)
135 * and the input file (for data) are the same, 135 * and the input file (for data) are the same,
136 * we get weird results at best. 136 * we get weird results at best.
137 */ 137 */
138 error("Can't take input from a terminal"); 138 error("Can't take input from a terminal");
139 if (f > 0) 139 if (f > 0)
140 (void)close(f); 140 (void)close(f);
141 (void)free(filename); 141 (void)free(filename);
142 return(0); 142 return(0);
143 } 143 }
144 144
145 /* 145 /*
146 * We are now committed to using the new file. 146 * We are now committed to using the new file.
147 * Close the current input file and set up to use the new one. 147 * Close the current input file and set up to use the new one.
148 */ 148 */
149 if (file > 0) 149 if (file > 0)
150 (void)close(file); 150 (void)close(file);
151 new_file = 1; 151 new_file = 1;
152 if (previous_file != NULL) 152 if (previous_file != NULL)
153 free(previous_file); 153 free(previous_file);
154 previous_file = current_file; 154 previous_file = current_file;
155 current_file = filename; 155 current_file = filename;
156 pos_clear(); 156 pos_clear();
157 prev_pos = position(TOP); 157 prev_pos = position(TOP);
158 ispipe = (f == 0); 158 ispipe = (f == 0);
159 if (ispipe) { 159 if (ispipe) {
160 didpipe = 1; 160 didpipe = 1;
161 current_name = "stdin"; 161 current_name = "stdin";
162 } else 162 } else
163 current_name = (p = rindex(filename, '/')) ? p + 1 : filename; 163 current_name = (p = rindex(filename, '/')) ? p + 1 : filename;
164 if (curr_ac >= ac) 164 if (curr_ac >= ac)
165 next_name = NULL; 165 next_name = NULL;
166 else 166 else
167 next_name = av[curr_ac + 1]; 167 next_name = av[curr_ac + 1];
168 file = f; 168 file = f;
169 ch_init(cbufs, 0); 169 ch_init(cbufs, 0);
170 init_mark(); 170 init_mark();
171 171
172 if (is_tty) { 172 if (is_tty) {
173 int no_display = !any_display; 173 int no_display = !any_display;
174 any_display = 1; 174 any_display = 1;
175 if (no_display && errmsgs > 0) { 175 if (no_display && errmsgs > 0) {
176 /* 176 /*
177 * We displayed some messages on error output 177 * We displayed some messages on error output
178 * (file descriptor 2; see error() function). 178 * (file descriptor 2; see error() function).
179 * Before erasing the screen contents, 179 * Before erasing the screen contents,
180 * display the file name and wait for a keystroke. 180 * display the file name and wait for a keystroke.
181 */ 181 */
182 error(filename); 182 error(filename);
183 } 183 }
184 /* 184 /*
185 * Indicate there is nothing displayed yet. 185 * Indicate there is nothing displayed yet.
186 */ 186 */
187 if (initial_pos != NULL_POSITION) 187 if (initial_pos != NULL_POSITION)
188 jump_loc(initial_pos); 188 jump_loc(initial_pos);
189 clr_linenum(); 189 clr_linenum();
190 } 190 }
191 return(1); 191 return(1);
192} 192}
193 193
194/* 194/*
195 * Edit the next file in the command line list. 195 * Edit the next file in the command line list.
196 */ 196 */
197void 197void
198next_file(n) 198next_file(n)
199 int n; 199 int n;
200{ 200{
201 if (curr_ac + n >= ac) { 201 if (curr_ac + n >= ac) {
202 if (quit_at_eof || position(TOP) == NULL_POSITION) 202 if (quit_at_eof || position(TOP) == NULL_POSITION)
203 quit(); 203 quit();
204 error("No (N-th) next file"); 204 error("No (N-th) next file");
205 } 205 }
206 else 206 else
207 (void)edit(av[curr_ac += n]); 207 (void)edit(av[curr_ac += n]);
208} 208}
209 209
210/* 210/*
211 * Edit the previous file in the command line list. 211 * Edit the previous file in the command line list.
212 */ 212 */
213void 213void
214prev_file(n) 214prev_file(n)
215 int n; 215 int n;
216{ 216{
217 if (curr_ac - n < 0) 217 if (curr_ac - n < 0)
218 error("No (N-th) previous file"); 218 error("No (N-th) previous file");
219 else 219 else
220 (void)edit(av[curr_ac -= n]); 220 (void)edit(av[curr_ac -= n]);
221} 221}
222 222
223/* 223/*
224 * copy a file directly to standard output; used if stdout is not a tty. 224 * copy a file directly to standard output; used if stdout is not a tty.
225 * the only processing is to squeeze multiple blank input lines. 225 * the only processing is to squeeze multiple blank input lines.
226 */ 226 */
227static void 227static void
228cat_file() 228cat_file()
229{ 229{
230 int c, empty; 230 int c, empty;
231 231
232 if (squeeze) { 232 if (squeeze) {
233 empty = 0; 233 empty = 0;
234 while ((c = ch_forw_get()) != EOI) 234 while ((c = ch_forw_get()) != EOI)
235 if (c != '\n') { 235 if (c != '\n') {
236 putchr(c); 236 putchr(c);
237 empty = 0; 237 empty = 0;
238 } 238 }
239 else if (empty < 2) { 239 else if (empty < 2) {
240 putchr(c); 240 putchr(c);
241 ++empty; 241 ++empty;
242 } 242 }
243 } 243 }
244 else while ((c = ch_forw_get()) != EOI) 244 else while ((c = ch_forw_get()) != EOI)
245 putchr(c); 245 putchr(c);
246 flush(); 246 flush();
247} 247}
248 248
249int 249int
250main(argc, argv) 250main(argc, argv)
251 int argc; 251 int argc;
252 char **argv; 252 char **argv;
253{ 253{
254 int envargc, argcnt; 254 int envargc, argcnt;
255 char *envargv[2]; 255 char *envargv[2];
256 256
257 /* 257 /*
258 * Process command line arguments and MORE environment arguments. 258 * Process command line arguments and MORE environment arguments.
259 * Command line arguments override environment arguments. 259 * Command line arguments override environment arguments.
260 */ 260 */
261 if ((envargv[1] = getenv("MORE")) != NULL) { 261 if ((envargv[1] = getenv("MORE")) != NULL) {
262 envargc = 2; 262 envargc = 2;
263 envargv[0] = "more"; 263 envargv[0] = "more";
264 envargv[2] = NULL; 264 envargv[2] = NULL;
265 (void)option(envargc, envargv); 265 (void)option(envargc, envargv);
266 } 266 }
267 argcnt = option(argc, argv); 267 argcnt = option(argc, argv);
268 argv += argcnt; 268 argv += argcnt;
269 argc -= argcnt; 269 argc -= argcnt;
270 270
271 /* 271 /*
272 * Set up list of files to be examined. 272 * Set up list of files to be examined.
273 */ 273 */
274 ac = argc; 274 ac = argc;
275 av = argv; 275 av = argv;
276 curr_ac = 0; 276 curr_ac = 0;
277 277
278 /* 278 /*
279 * Set up terminal, etc. 279 * Set up terminal, etc.
280 */ 280 */
281 is_tty = isatty(1); 281 is_tty = isatty(1);
282 if (!is_tty) { 282 if (!is_tty) {
283 /* 283 /*
284 * Output is not a tty. 284 * Output is not a tty.
285 * Just copy the input file(s) to output. 285 * Just copy the input file(s) to output.
286 */ 286 */
287 if (ac < 1) { 287 if (ac < 1) {
288 (void)edit("-"); 288 (void)edit("-");
289 cat_file(); 289 cat_file();
290 } else { 290 } else {
291 do { 291 do {
292 (void)edit((char *)NULL); 292 (void)edit((char *)NULL);
293 if (file >= 0) 293 if (file >= 0)
294 cat_file(); 294 cat_file();
295 } while (++curr_ac < ac); 295 } while (++curr_ac < ac);
296 } 296 }
297 exit(0); 297 exit(0);
298 } 298 }
299 299
300 raw_mode(1); 300 raw_mode(1);
301 get_term(); 301 get_term();
302 open_getchr(); 302 open_getchr();
303 init(); 303 init();
304 init_signals(1); 304 init_signals(1);
305 305
306 /* select the first file to examine. */ 306 /* select the first file to examine. */
307 if (ac < 1) 307 if (ac < 1)
308 (void)edit("-"); /* Standard input */ 308 (void)edit("-"); /* Standard input */
309 else { 309 else {
310 /* 310 /*
311 * Try all the files named as command arguments. 311 * Try all the files named as command arguments.
312 * We are simply looking for one which can be 312 * We are simply looking for one which can be
313 * opened without error. 313 * opened without error.
314 */ 314 */
315 do { 315 do {
316 (void)edit((char *)NULL); 316 (void)edit((char *)NULL);
317 } while (file < 0 && ++curr_ac < ac); 317 } while (file < 0 && ++curr_ac < ac);
318 } 318 }
319 319
320 if (file >= 0) 320 if (file >= 0)
321 commands(); 321 commands();
322 quit(); 322 quit();
323} 323}
324 324
325/* 325/*
326 * Copy a string to a "safe" place 326 * Copy a string to a "safe" place
327 * (that is, to a buffer allocated by malloc). 327 * (that is, to a buffer allocated by malloc).
328 */ 328 */
329char * 329char *
330save(s) 330save(s)
331 char *s; 331 char *s;
332{ 332{
333 char *p; 333 char *p;
334 334
335 p = strdup(s); 335 p = strdup(s);
336 if (p == NULL) 336 if (p == NULL)
337 { 337 {
338 error("cannot allocate memory"); 338 error("cannot allocate memory");
339 quit(); 339 quit();
340 } 340 }
341 return(p); 341 return(p);
342} 342}
343 343
344/* 344/*
345 * Exit the program. 345 * Exit the program.
346 */ 346 */
347void 347void
348quit() 348quit()
349{ 349{
350 /* 350 /*
351 * Put cursor at bottom left corner, clear the line, 351 * Put cursor at bottom left corner, clear the line,
352 * reset the terminal modes, and exit. 352 * reset the terminal modes, and exit.
353 */ 353 */
354 quitting = 1; 354 quitting = 1;
355 lower_left(); 355 lower_left();
356 clear_eol(); 356 clear_eol();
357 deinit(); 357 deinit();
358 flush(); 358 flush();
359 raw_mode(0); 359 raw_mode(0);
360 exit(0); 360 exit(0);
361} 361}