Home | History | Annotate | Download | only in diskomizer
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #pragma ident	"@(#)prompt.c	1.7	09/05/26 SMI"
     28 
     29 #include <diskomizer/log.h>
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <strings.h>
     33 #include "args.h"
     34 #include "prompt.h"
     35 #include "errors.h"
     36 
     37 
     38 static void
     39 remove_newline(char *str)
     40 {
     41 	int len;
     42 	len = strlen(str);
     43 
     44 	if (str[--len] == '\n')
     45 		str[len] = NULL;
     46 }
     47 static void
     48 append(char **str, char *line)
     49 {
     50 	int len;
     51 	char *tmp;
     52 	if (*str == NULL) {
     53 		len = strlen(line) + 1;
     54 	} else {
     55 		len = strlen(line) + strlen(*str) + 2;
     56 	}
     57 
     58 	if ((tmp = (char *)realloc(*str, len)) == NULL) {
     59 		REALLOC_ERROR(*str, len);
     60 		exit(1);
     61 	}
     62 	if (*str != NULL) {
     63 		(void) strcat(tmp, " ");
     64 	} else {
     65 		*tmp = NULL;
     66 	}
     67 	(void) strcat(tmp, line);
     68 	remove_newline(tmp);
     69 	*str = tmp;
     70 }
     71 static char *
     72 getline(FILE *tty, const char *prompt)
     73 {
     74 	static char buf[1024];
     75 	char *x;
     76 	FILE *ttyin;
     77 	(void) fprintf(tty, prompt);
     78 	(void) fflush(tty);
     79 	if ((ttyin = fopen("/dev/tty", "r")) == NULL)
     80 		return (0);
     81 
     82 	x = fgets(&buf[0], sizeof (buf), ttyin);
     83 	(void) fclose(ttyin);
     84 
     85 	return (x);
     86 }
     87 
     88 int
     89 prompt(void)
     90 {
     91 	FILE *tty;
     92 	char *res = NULL;
     93 	char *line;
     94 
     95 	if ((tty = fopen("/dev/tty", "w")) == NULL)
     96 		return (0);
     97 	(void) fprintf(tty,
     98 	    "Enter devices to test, one device per line, end with a ^D\n");
     99 	(void) fflush(tty);
    100 	while ((line = getline(tty, "Device: ")) != NULL) {
    101 		append(&res, line);
    102 	}
    103 	opts.device = res;
    104 	if ((line = getline(tty,
    105 	    "Would you like to save this config Y/N?:")) != NULL) {
    106 		if (*line == 'Y' || *line == 'y') {
    107 			do {
    108 				line = getline(tty,
    109 				    "Enter file name to save into:");
    110 
    111 				if (line == NULL)
    112 					exit(1);
    113 
    114 				remove_newline(line);
    115 			} while (!save_options(line));
    116 		}
    117 	}
    118 
    119 	(void) fclose(tty);
    120 	return (1);
    121 }
    122 
    123 #ifdef MAIN_PROMPT
    124 void
    125 main(int argc, char **argv)
    126 {
    127 
    128 	prompt();
    129 	printf("opts.device = %s\n", opts.device);
    130 }
    131 #endif
    132