Debian Codegraph @

util/dialtest.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 /*
27 * Program for interactively using dial string rules.
28 *
29 * Usage: dialtest [-v] [-a areacode] [-c countrycode]
30 * [-i internationalprefix] [-l longdistanceprefix] dial-rules-file
31 */
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include "DialRules.h"
35 #include "Sys.h"
36
37 extern void fxFatal(const char* va_alist ...);
38
39 static const char* appName;
40 static bool quiet = false;
41
42 static void
43 usage()
44 {
45 fxFatal(_("usage: %s"
46 " [-v]"
47 " [-a area-code]"
48 " [-c country-code]"
49 " [-i international-prefix]"
50 " [-l long-distance-prefix]"
51 " dialrules")
52 , appName
53 );
54 }
55
56 static int
57 prompt()
58 {
59 if (! quiet)
60 printf(_("ready> ")); fflush(stdout);
61 return (1);
62 }
63
64 int
65 main(int argc, char* argv[])
66 {
67 const char* areaCode = "415";
68 const char* countryCode = "1";
69 const char* internationalPrefix = "011";
70 const char* longDistancePrefix = "1";
71 bool verbose = false;
72 extern int optind;
73 extern char* optarg;
74 int c;
75
76 appName = argv[0];
77 while ((c = Sys::getopt(argc, argv, "a:c:i:l:qv")) != -1)
78 switch (c) {
79 case 'a':
80 areaCode = optarg;
81 break;
82 case 'c':
83 countryCode = optarg;
84 break;
85 case 'i':
86 internationalPrefix = optarg;
87 break;
88 case 'l':
89 longDistancePrefix = optarg;
90 break;
91 case 'q':
92 quiet = true;
93 break;
94 case 'v':
95 verbose = true;
96 break;
97 case '?':
98 usage();
99 /*NOTREACHED*/
100 }
101 if (argc - optind != 1)
102 usage();
103 DialStringRules rules(argv[optind]);
104 rules.setVerbose(!quiet);
105 rules.def("AreaCode", areaCode);
106 rules.def("CountryCode", countryCode);
107 rules.def("InternationalPrefix", internationalPrefix);
108 rules.def("LongDistancePrefix", longDistancePrefix);
109 if (!rules.parse())
110 fxFatal(_("%s: Problem parsing rules in %s"), appName, argv[optind]);
111 char line[1024];
112 while (prompt() && fgets(line, sizeof (line), stdin)) {
113 char* cp = strchr(line, '\n');
114 if (cp)
115 *cp = '\0';
116 if (verbose)
117 printf(_("input = \"%s\"\n"), line);
118 if ((cp = strchr(line, '('))) {
119 char* ep = strrchr(cp, ')');
120 if (ep)
121 *ep = '\0';
122 fxStr set(line, cp-line);
123 fxStr result = rules.applyRules(set, cp+1);
124 if (quiet)
125 printf("%s\n", (const char*) result);
126 else
127 printf("%s(%s) = \"%s\"\n", (const char*) set, cp+1, (const char*) result);
128 } else {
129 fxStr c = rules.canonicalNumber(line);
130 fxStr d = rules.dialString(line);
131 fxStr n = rules.displayNumber(line);
132 printf(_("canonical = \"%s\"\n"), (const char*) c);
133 printf(_("dial-string = \"%s\"\n"), (const char*) d);
134 printf(_("display = \"%s\"\n"), (const char *) n);
135 }
136 }
137 return (0);
138 }