UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2018 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 33 #include <cx/string.h> 34 #include <cx/utils.h> 35 36 #include <libidav/utils.h> 37 38 #include "assistant.h" 39 40 char* assistant_getcfg(const char *cfgname) { 41 cxmutstr line; 42 char *value = NULL; 43 while(!value) { 44 printf("%s: ", cfgname); 45 fflush(stdout); 46 line = util_readline(stdin); 47 if(line.length == 0 || !line.ptr) { 48 fprintf(stderr, "%s must be not empty\n", cfgname); 49 continue; 50 } 51 52 value = line.ptr; 53 break; 54 } 55 return value; 56 } 57 58 char* assistant_getoptcfg(const char *cfgname) { 59 cxmutstr line; 60 char *value = NULL; 61 while(!value) { 62 printf("%s (optional): ", cfgname); 63 fflush(stdout); 64 line = util_readline(stdin); 65 if(line.length == 0 || !line.ptr) { 66 if(line.ptr) { 67 free(line.ptr); 68 } 69 break; 70 } 71 72 value = line.ptr; 73 break; 74 } 75 return value; 76 } 77 78 char* assistant_gethiddenoptcfg(const char *cfgname) { 79 printf("%s (optional): ", cfgname); 80 fflush(stdout); 81 char *pw = util_password_input(""); 82 if(pw[0] == 0) { 83 free(pw); 84 pw = NULL; 85 } 86 return pw; 87 } 88 89 90 char* assistant_getdefcfg(const char *cfgname, const char *defval) { 91 cxmutstr line; 92 char *value = NULL; 93 while(!value) { 94 printf("%s (default: %s): ", cfgname, defval); 95 fflush(stdout); 96 line = util_readline(stdin); 97 if(line.length == 0 || !line.ptr) { 98 if(line.ptr) { 99 free(line.ptr); 100 } 101 break; 102 } 103 104 value = line.ptr; 105 break; 106 } 107 return value ? value : strdup(defval); 108 } 109