17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*23a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*23a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*23a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include "util.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * This is just like fgets, but recognizes that "\\n" signals a continuation
357c478bd9Sstevel@tonic-gate  * of a line
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate char *
getaline(line,maxlen,fp)38*23a1cceaSRoger A. Faulkner getaline(line, maxlen, fp)
397c478bd9Sstevel@tonic-gate 	char *line;
407c478bd9Sstevel@tonic-gate 	int maxlen;
417c478bd9Sstevel@tonic-gate 	FILE *fp;
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	register char *p;
447c478bd9Sstevel@tonic-gate 	register char *start;
457c478bd9Sstevel@tonic-gate 	int c;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	start = line;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate nextline:
507c478bd9Sstevel@tonic-gate 	if (fgets(start, maxlen, fp) == NULL) {
517c478bd9Sstevel@tonic-gate 		return (NULL);
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 	for (p = start; *p; p++) {
547c478bd9Sstevel@tonic-gate 		if (*p == '\n') {
557c478bd9Sstevel@tonic-gate 			if (p > start && *(p-1) == '\\') {
567c478bd9Sstevel@tonic-gate 				start = p - 1;
577c478bd9Sstevel@tonic-gate 				maxlen++;
587c478bd9Sstevel@tonic-gate 				goto nextline;
597c478bd9Sstevel@tonic-gate 			} else {
607c478bd9Sstevel@tonic-gate 				return (line);
617c478bd9Sstevel@tonic-gate 			}
627c478bd9Sstevel@tonic-gate 		}
637c478bd9Sstevel@tonic-gate 		maxlen--;
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/*
677c478bd9Sstevel@tonic-gate 	 * Input line is too long. Rest of the line needs to be discarded.
687c478bd9Sstevel@tonic-gate 	 * Reinsert the last char into the stream. This is done so that
697c478bd9Sstevel@tonic-gate 	 * in case the last char read is '\' and it is followed by a '\n'
707c478bd9Sstevel@tonic-gate 	 * then the next line too can be discarded.
717c478bd9Sstevel@tonic-gate 	 */
727c478bd9Sstevel@tonic-gate 	if (p > start)
737c478bd9Sstevel@tonic-gate 		(void) ungetc(*(p-1), fp);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/*
767c478bd9Sstevel@tonic-gate 	 * Discard the rest of the line
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	while ((c = getc(fp)) != EOF) {
797c478bd9Sstevel@tonic-gate 		if (c == '\n')
807c478bd9Sstevel@tonic-gate 			break;
817c478bd9Sstevel@tonic-gate 		else if (c == '\\') {
827c478bd9Sstevel@tonic-gate 			/*
837c478bd9Sstevel@tonic-gate 			 * Ignore the next character except EOF
847c478bd9Sstevel@tonic-gate 			 */
857c478bd9Sstevel@tonic-gate 			if (getc(fp) == EOF)
867c478bd9Sstevel@tonic-gate 				break;
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	maxlen = strlen(line) + 1;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Certain functions expects a newline in the buffer.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	if (maxlen >= 2)
967c478bd9Sstevel@tonic-gate 		line[maxlen - 2] = '\n';
977c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Following line too long - remaining chars "
987c478bd9Sstevel@tonic-gate 			"ignored\n--- %s", line);
997c478bd9Sstevel@tonic-gate 	return (line);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate void
fatal(message)1047c478bd9Sstevel@tonic-gate fatal(message)
1057c478bd9Sstevel@tonic-gate 	char *message;
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "fatal error: %s\n", message);
1087c478bd9Sstevel@tonic-gate 	exit(1);
1097c478bd9Sstevel@tonic-gate }
110