getpar.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* $NetBSD: getpar.c,v 1.12 2004/01/27 20:30:31 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. 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
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)getpar.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: getpar.c,v 1.12 2004/01/27 20:30:31 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "getpar.h"
  42. #include "trek.h"
  43. static int testterm(void);
  44. /**
  45. ** get integer parameter
  46. **/
  47. int
  48. getintpar(s)
  49. const char *s;
  50. {
  51. int i;
  52. int n;
  53. while (1)
  54. {
  55. if (testnl() && s)
  56. printf("%s: ", s);
  57. i = scanf("%d", &n);
  58. if (i < 0)
  59. exit(1);
  60. if (i > 0 && testterm())
  61. return (n);
  62. printf("invalid input; please enter an integer\n");
  63. skiptonl(0);
  64. }
  65. }
  66. /**
  67. ** get floating parameter
  68. **/
  69. double getfltpar(s)
  70. const char *s;
  71. {
  72. int i;
  73. double d;
  74. while (1)
  75. {
  76. if (testnl() && s)
  77. printf("%s: ", s);
  78. i = scanf("%lf", &d);
  79. if (i < 0)
  80. exit(1);
  81. if (i > 0 && testterm())
  82. return (d);
  83. printf("invalid input; please enter a double\n");
  84. skiptonl(0);
  85. }
  86. }
  87. /**
  88. ** get yes/no parameter
  89. **/
  90. const struct cvntab Yntab[] =
  91. {
  92. { "y", "es", (cmdfun)1, 1 },
  93. { "n", "o", (cmdfun)0, 0 },
  94. { NULL, NULL, NULL, 0 }
  95. };
  96. int
  97. getynpar(s)
  98. const char *s;
  99. {
  100. const struct cvntab *r;
  101. r = getcodpar(s, Yntab);
  102. return r->value2;
  103. }
  104. /**
  105. ** get coded parameter
  106. **/
  107. const struct cvntab *getcodpar(s, tab)
  108. const char *s;
  109. const struct cvntab tab[];
  110. {
  111. char input[100];
  112. const struct cvntab *r;
  113. int flag;
  114. const char *p, *q;
  115. int c;
  116. int f;
  117. flag = 0;
  118. while (1)
  119. {
  120. flag |= (f = testnl());
  121. if (flag)
  122. printf("%s: ", s);
  123. if (f)
  124. cgetc(0); /* throw out the newline */
  125. scanf("%*[ \t;]");
  126. if ((c = scanf("%99[^ \t;\n]", input)) < 0)
  127. exit(1);
  128. if (c == 0)
  129. continue;
  130. flag = 1;
  131. /* if command list, print four per line */
  132. if (input[0] == '?' && input[1] == 0)
  133. {
  134. c = 4;
  135. for (r = tab; r->abrev; r++)
  136. {
  137. strcpy(input, r->abrev);
  138. strcat(input, r->full);
  139. printf("%14.14s", input);
  140. if (--c > 0)
  141. continue;
  142. c = 4;
  143. printf("\n");
  144. }
  145. if (c != 4)
  146. printf("\n");
  147. continue;
  148. }
  149. /* search for in table */
  150. for (r = tab; r->abrev; r++)
  151. {
  152. p = input;
  153. for (q = r->abrev; *q; q++)
  154. if (*p++ != *q)
  155. break;
  156. if (!*q)
  157. {
  158. for (q = r->full; *p && *q; q++, p++)
  159. if (*p != *q)
  160. break;
  161. if (!*p || !*q)
  162. break;
  163. }
  164. }
  165. /* check for not found */
  166. if (!r->abrev)
  167. {
  168. printf("invalid input; ? for valid inputs\n");
  169. skiptonl(0);
  170. }
  171. else
  172. return (r);
  173. }
  174. }
  175. /**
  176. ** get string parameter
  177. **/
  178. void
  179. getstrpar(s, r, l, t)
  180. const char *s;
  181. char *r;
  182. int l;
  183. const char *t;
  184. {
  185. int i;
  186. char format[20];
  187. int f;
  188. if (t == 0)
  189. t = " \t\n;";
  190. (void)sprintf(format, "%%%d[^%s]", l, t);
  191. while (1)
  192. {
  193. if ((f = testnl()) && s)
  194. printf("%s: ", s);
  195. if (f)
  196. cgetc(0);
  197. scanf("%*[\t ;]");
  198. i = scanf(format, r);
  199. if (i < 0)
  200. exit(1);
  201. if (i != 0)
  202. return;
  203. }
  204. }
  205. /**
  206. ** test if newline is next valid character
  207. **/
  208. int
  209. testnl()
  210. {
  211. char c;
  212. while ((c = cgetc(0)) != '\n')
  213. if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
  214. (c >= 'A' && c <= 'Z') ||
  215. (c >= 'a' && c <= 'z') || c == '-')
  216. {
  217. ungetc(c, stdin);
  218. return(0);
  219. }
  220. ungetc(c, stdin);
  221. return (1);
  222. }
  223. /**
  224. ** scan for newline
  225. **/
  226. void
  227. skiptonl(c)
  228. int c;
  229. {
  230. while (c != '\n')
  231. if (!(c = cgetc(0)))
  232. return;
  233. ungetc('\n', stdin);
  234. return;
  235. }
  236. /**
  237. ** test for valid terminator
  238. **/
  239. static int
  240. testterm()
  241. {
  242. char c;
  243. if (!(c = cgetc(0)))
  244. return (1);
  245. if (c == '.')
  246. return (0);
  247. if (c == '\n' || c == ';')
  248. ungetc(c, stdin);
  249. return (1);
  250. }
  251. /*
  252. ** TEST FOR SPECIFIED DELIMITER
  253. **
  254. ** The standard input is scanned for the parameter. If found,
  255. ** it is thrown away and non-zero is returned. If not found,
  256. ** zero is returned.
  257. */
  258. int
  259. readdelim(d)
  260. char d;
  261. {
  262. char c;
  263. while ((c = cgetc(0)) != '\0')
  264. {
  265. if (c == d)
  266. return (1);
  267. if (c == ' ')
  268. continue;
  269. ungetc(c, stdin);
  270. break;
  271. }
  272. return (0);
  273. }