📄 conf.c
字号:
/* This file is part of GNUnet. (C) 2005, 2006 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNUnet; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*//* * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> * Released under the terms of the GNU GPL v2.0. *//** * @file text/conf.c * @brief GNUnet Setup * @author Roman Zippel * @author Nils Durner * @author Christian Grothoff * * TODO: * - support editing of string inputs... */#include "platform.h"#include "gnunet_setup_lib.h"#include "conf.h"#include <termios.h>static charrd (){ size_t ret; char c; ret = fread (&c, 1, 1, stdin); if (ret == 1) return c; return 'q'; /* quit */}/** * printf with indentation */static voidiprintf (int indent, const char *format, ...){ int i; va_list va; for (i = 0; i < indent; i++) printf (" "); va_start (va, format); vfprintf (stdout, format, va); va_end (va); fflush (stdout);}static char *getValueAsString (GNUNET_GNS_TreeNodeKindAndType type, GNUNET_GNS_Value * val){ char buf[92]; switch (type & GNUNET_GNS_TYPE_MASK) { case GNUNET_GNS_TYPE_BOOLEAN: if (val->Boolean.val) return GNUNET_strdup (_("yes")); return GNUNET_strdup (_("no")); case GNUNET_GNS_TYPE_STRING: case GNUNET_GNS_TYPE_SINGLE_CHOICE: case GNUNET_GNS_TYPE_MULTIPLE_CHOICE: return GNUNET_strdup (val->String.val); case GNUNET_GNS_TYPE_DOUBLE: GNUNET_snprintf (buf, 92, "%f", val->Double.val); return GNUNET_strdup (buf); case GNUNET_GNS_TYPE_UINT64: GNUNET_snprintf (buf, 92, "%llu", val->UInt64.val); return GNUNET_strdup (buf); } return GNUNET_strdup ("Internal error.");}static voidprintChoice (int indent, GNUNET_GNS_TreeNodeKindAndType type, GNUNET_GNS_Value * val){ int i; char defLet; switch (type & GNUNET_GNS_TYPE_MASK) { case GNUNET_GNS_TYPE_BOOLEAN: iprintf (indent, _("\tEnter yes (%s), no (%s) or help (%s): "), val->Boolean.def ? "Y" : "y", val->Boolean.def ? "n" : "N", "d", "?"); break; case GNUNET_GNS_TYPE_STRING: case GNUNET_GNS_TYPE_MULTIPLE_CHOICE: i = 0; defLet = '\0'; if (val->String.legalRange[0] != NULL) iprintf (indent, _("\tPossible choices:\n")); while (val->String.legalRange[i] != NULL) { iprintf (indent, "\t %s\n", val->String.legalRange[i]); i++; } iprintf (indent, _ ("\tUse single space prefix to avoid conflicts with hotkeys!\n")); iprintf (indent, _("\tEnter string (type '%s' for default value `%s'): "), "d", val->String.def); break; case GNUNET_GNS_TYPE_SINGLE_CHOICE: i = 0; defLet = '\0'; while (val->String.legalRange[i] != NULL) { iprintf (indent, "\t (%c) %s\n", (i < 10) ? '0' + i : 'a' + i - 10, val->String.legalRange[i]); if (0 == strcmp (val->String.legalRange[i], val->String.def)) defLet = (i < 10) ? '0' + i : 'a' + i - 10; i++; } GNUNET_GE_ASSERT (NULL, defLet != '\0'); iprintf (indent, "\n\t (?) Help\n"); iprintf (indent, _("\t Enter choice (default is %c): "), defLet); break; case GNUNET_GNS_TYPE_DOUBLE: iprintf (indent, _("\tEnter floating point (type '%s' for default value %f): "), "d", val->Double.def); break; case GNUNET_GNS_TYPE_UINT64: iprintf (indent, _ ("\tEnter unsigned integer in interval [%llu,%llu] (type '%s' for default value %llu): "), val->UInt64.min, val->UInt64.max, "d", val->UInt64.def); break; default: return; }}/** * @return GNUNET_OK on success, GNUNET_NO to display help, GNUNET_SYSERR to abort */static intreadValue (GNUNET_GNS_TreeNodeKindAndType type, GNUNET_GNS_Value * val){ int c; char buf[1024]; int i; int j; unsigned long long l; switch (type & GNUNET_GNS_TYPE_MASK) { case GNUNET_GNS_TYPE_BOOLEAN: while (1) { c = rd (); switch (c) { case '\n': printf ("\n"); return GNUNET_YES; /* skip */ case 'y': case 'Y': val->Boolean.val = 1; printf (_("Yes\n")); return GNUNET_YES; case 'n': case 'N': val->Boolean.val = 0; printf (_("No\n")); return GNUNET_YES; case '?': printf (_("Help\n")); return GNUNET_NO; case 'q': printf (_("Abort\n")); return GNUNET_SYSERR; default: break; } } break; case GNUNET_GNS_TYPE_STRING: case GNUNET_GNS_TYPE_MULTIPLE_CHOICE: i = 0; while (1) { buf[i] = rd (); if (buf[i] == 'q') { printf (_("Abort\n")); return GNUNET_SYSERR; }#if 0 if (buf[i] == '\b') { if (i > 0) { printf ("\b"); /* this does not work */ i--; } continue; }#endif if ((buf[i] == 'd') && (i == 0)) { printf ("%s\n", val->String.def); GNUNET_free (val->String.val); val->String.val = GNUNET_strdup (val->String.def); return GNUNET_YES; } if ((buf[i] == '?') && (i == 0)) { printf (_("Help\n")); return GNUNET_NO; } if ((buf[i] == '\n') && (i == 0)) { printf ("%s\n", val->String.val); return GNUNET_YES; /* keep */ } if (buf[i] != '\n') { if (i < 1023) { printf ("%c", buf[i]); fflush (stdout); i++; } continue; } break; } GNUNET_free (val->String.val); val->String.val = GNUNET_strdup (buf[0] == ' ' ? &buf[1] : buf); printf ("\n"); return GNUNET_OK; case GNUNET_GNS_TYPE_SINGLE_CHOICE: while (1) { c = rd (); if (c == '?') { printf (_("Help\n")); return GNUNET_NO; } if (c == '\n') { printf ("%s\n", val->String.val); return GNUNET_YES; } if (c == 'q') { printf (_("Abort\n")); return GNUNET_SYSERR; } i = -1; if ((c >= '0') && (c <= '9')) i = c - '0'; else if ((c >= 'a') && (c <= 'z')) i = c - 'a' + 10; else continue; /* invalid entry */ for (j = 0; j <= i; j++) if (val->String.legalRange[j] == NULL) { i = -1; break; } if (i == -1) continue; /* invalid entry */ GNUNET_free (val->String.val); val->String.val = GNUNET_strdup (val->String.legalRange[i]); printf ("%s\n", val->String.val); return GNUNET_OK; } /* unreachable */ case GNUNET_GNS_TYPE_DOUBLE: i = 0; while (1) { buf[i] = rd (); if (buf[i] == 'q') { printf (_("Abort\n")); return GNUNET_SYSERR; }#if 0 if (buf[i] == '\b')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -