1 /*
2  *  mutt_vc_query - vCard query utility for mutt
3  *  Copyright (C) 2003  Andrew Hsu
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU General Public License as
7  *  published by the Free Software Foundation; either version 2 of
8  *  the License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  *  02111-1307  USA
19  *
20  *  $Id: main.c,v 1.6 2003/05/19 07:44:50 ahsu Rel $
21  */
22 
23 #include <limits.h>
24 #include "result.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #else
33 #define PACKAGE_STRING "mutt_vc_query"
34 #endif
35 
36 #define DEFAULT_HOME_ROLO_DIR ".rolo"
37 #define DEFAULT_FILENAME "contacts.vcf"
38 
39 static void set_defaults ();
40 static void process_command_line_args (int argc, char *const *argv);
41 static void display_usage (const char *prog_name);
42 static void display_version ();
43 static void display_license ();
44 
45 char *query_string = NULL;
46 char datafile[PATH_MAX];
47 char *misc_field = NULL;
48 int sort_by = 0;
49 
50 /***************************************************************************
51     Sets the default settings.
52  */
53 
54 static void
set_defaults()55 set_defaults ()
56 {
57   char *home = NULL;
58 
59   home = getenv ("HOME");
60   strcpy (datafile, home);
61   strncat (datafile, "/", 1);
62   strncat (datafile, DEFAULT_HOME_ROLO_DIR, strlen (DEFAULT_HOME_ROLO_DIR));
63   strncat (datafile, "/", 1);
64   strncat (datafile, DEFAULT_FILENAME, strlen (DEFAULT_FILENAME));
65 
66   sort_by = SORT_RESULTS_BY_NAME;
67 }
68 
69 /***************************************************************************
70     Parses the command-line arguments.
71  */
72 
73 static void
process_command_line_args(int argc,char * const * argv)74 process_command_line_args (int argc, char *const *argv)
75 {
76   int ch = -1;
77   char *progname = NULL;
78 
79   progname = argv[0];
80 
81   while (-1 != (ch = getopt (argc, argv, "f:met:vVh")))
82     {
83       switch (ch)
84         {
85         case 'f':
86           strcpy (datafile, optarg);
87           break;
88         case 'm':
89           sort_by = SORT_RESULTS_BY_MISC;
90           break;
91         case 'e':
92           sort_by = SORT_RESULTS_BY_EMAIL;
93           break;
94         case 't':
95           misc_field = strdup (optarg);
96           break;
97         case 'v':
98           display_version ();
99           exit (0);
100           break;
101         case 'V':
102           display_license ();
103           exit (0);
104           break;
105         case 'h':
106         case '?':
107         default:
108           display_usage (progname);
109           exit (0);
110         }
111     }
112 
113   argc -= optind;
114   argv += optind;
115 
116   if (1 != argc)
117     {
118       fprintf (stderr, "Invalid number of arguments.\n");
119       display_usage (progname);
120       exit (1);
121     }
122 
123   query_string = argv[0];
124 }
125 
126 /***************************************************************************
127     Outputs a one-line version statement.
128  */
129 
130 static void
display_version()131 display_version ()
132 {
133   printf ("%s\n", PACKAGE_STRING);
134 }
135 
136 /***************************************************************************
137     Outputs the software license.
138  */
139 
140 static void
display_license()141 display_license ()
142 {
143   printf ("mutt_vc_query - vCard query utility for mutt\n");
144   printf ("Copyright (C) 2003  Andrew Hsu\n");
145   printf ("\n");
146   printf ("This program is free software;");
147   printf (" you can redistribute it and/or modify\n");
148   printf ("it under the terms of the");
149   printf (" GNU General Public License as published by\n");
150   printf ("the Free Software Foundation;");
151   printf (" either version 2 of the License, or\n");
152   printf ("(at your option) any later version.\n");
153   printf ("\n");
154   printf ("This program is distributed");
155   printf (" in the hope that it will be useful,\n");
156   printf ("but WITHOUT ANY WARRANTY;");
157   printf (" without even the implied warranty of\n");
158   printf ("MERCHANTABILITY or FITNESS FOR A PARTICULAR");
159   printf (" PURPOSE.  See the\n");
160   printf ("GNU General Public License for more details.\n");
161   printf ("\n");
162   printf ("You should have received a copy of");
163   printf (" the GNU General Public License\n");
164   printf ("along with this program;");
165   printf (" if not, write to the Free Software\n");
166   printf ("Foundation, Inc., 59 Temple Place, Suite 330,");
167   printf (" Boston, MA  02111-1307  USA\n");
168 }
169 
170 /***************************************************************************
171     Ouputs how to use the program.
172  */
173 
174 static void
display_usage(const char * prog_name)175 display_usage (const char *prog_name)
176 {
177   printf ("usage: %s [-e|-m] [-t <type>] [-f <file>] query_string\n",
178           prog_name);
179   printf ("       %s -v\n", prog_name);
180   printf ("       %s -V\n", prog_name);
181   printf ("       %s -h\n", prog_name);
182   printf ("options:\n");
183   printf ("  -e            sort results by email\n");
184   printf ("  -m            sort results by misc\n");
185   printf ("  -f <file>     specify a data file to use\n");
186   printf ("  -t <type>     the type to use for the misc field\n");
187   printf ("  -v            display version\n");
188   printf ("  -V            display copyright and license\n");
189   printf ("  -h            this help message\n");
190 }
191 
192 /***************************************************************************
193     The main function.
194  */
195 
196 int
main(int argc,char * argv[])197 main (int argc, char *argv[])
198 {
199   query_result *results = NULL;
200   FILE *fp = NULL;
201   int searched = 0;
202   int rc = 0;
203   int ret_val = 0;
204 
205   set_defaults ();
206   process_command_line_args (argc, argv);
207   results = create_query_result ();
208 
209   fp = fopen (datafile, "r");
210   if (NULL == fp)
211     {
212       fprintf (stderr, "Unable to open data file: %s\n", datafile);
213       exit (1);
214     }
215 
216   printf ("Searching database ... ");
217   get_results (fp, query_string, misc_field, &searched, results, &rc);
218 
219   if (0 == rc)
220     {
221       /* the query produced no results */
222       printf ("no matches found.\n");
223       ret_val = 1;
224     }
225   else
226     {
227       /* success in finding matches for the query */
228       printf ("%i entries ... ", searched);
229       printf ("%i matching.\n", rc);
230       sort_results (results, rc, sort_by);
231       print_results (results);
232       ret_val = 0;
233     }
234 
235   return ret_val;
236 }
237