File: main.c

package info (click to toggle)
gpr 0.12deb1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,568 kB
  • ctags: 250
  • sloc: sh: 6,138; ansic: 3,684; makefile: 86; sed: 16
file content (329 lines) | stat: -rw-r--r-- 8,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * GPR
 * Copyright (C) 2000 CompuMetric Labs, Inc.
 *
 * For more information contact:
 *        Thomas Hubbell
 *        CompuMetric Labs, Inc.
 *        5920 Grelot Road, Suite C-2
 *        Mobile, AL 36609
 * 
 * Voice: (334) 342-2220
 *   Fax: (334) 343-2261
 * Email: thubbell@compumetric.com
 *   Web: http://www.compumetric.com
 *
 * This program 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
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *    
 *        File: Main.c
 * 
 * Description: Main program
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "unistd.h"
#include <stdlib.h>
#include <string.h>

#ifdef USE_GNOME
#include <gnome.h>
#else
#include <gtk/gtk.h>
#include <popt.h>
#endif

#include <gettext.h>
#include <sys/stat.h>

#include "interface.h"
#include "support.h"
#include "tab_categories.h"

/* popt variables and structs */
static char *printer = NULL;
static char *jobname = NULL;
static int remove_job = FALSE;
       int debug_on = FALSE;
static int no_run = FALSE;
static int ppdfilt = 1;



char *cmdname = "lpr -P";

/*
  MLP: This is the "tab categories" pointer. Need
  a better way to access it than a global. 
*/
ptrTab *globalTabList = NULL;

int main(int argc, char **argv)
{
  GtkWidget *gpr_main_window;	/* The main gpr window */
  GtkWidget *gpr_inst_options_window;	/* the installable options
					   window */
  ppd_struct *ppd;	/* local copy of the ppd structure */

  const char **args;	/* leftover command-line arguments */

  int i;		/* argument index */
  char tf_path[64];	/* temporary file pathname */
  int tfd = -1;		/* temporary file descriptor */
  FILE *tmpf = 0;	/* temporary file */
  char curr_line[200];	/* the current line of input from stdin */
  struct stat info;	/* file info, used to see if file is piped 
			   from an app */

  //Mennucc: in Debian, gpr may divert lpr to a script that calls gpr
  //   this command line avoids looping
  {
    struct stat buf;
    if ( 0==stat("/usr/bin/lpr.not.gpr", &buf ) ) {
      cmdname = "/usr/bin/lpr.not.gpr -P";
    }
  }
  // Mennucc: this seems to be an obvious error
  if (0==strcmp(argv[0], "lpr")) {
    if (getenv("DISPLAY") == NULL) {
      char **vars = malloc(sizeof(char *) * (argc + 1));
      vars[argc] = NULL;
      for (i = 0; i < argc; i++)
	vars[i] = argv[i];
      execvp("/usr/bin/lpr.not.gpr", vars);
      // exec failed
      perror("gpr");
      exit(1);
    }
  }


  ppd = malloc(sizeof(ppd_struct));
  memset(ppd, 0, sizeof(ppd_struct));


const struct poptOption options[] = {
  {
    "printer",'P',POPT_ARG_STRING,&printer,0,
    _("Specify a destination printer"),NULL
  },{
    "debug",'d',POPT_ARG_NONE,&debug_on,0,
    _("Enable debugging output"),NULL
  },{
    "jobname",'J',POPT_ARG_STRING,&jobname,0,
    _("lpr option: Job name to print on burst page"),NULL
  },{
    "remove",'r',POPT_ARG_NONE,&remove_job,0,
    _("lpr option: Remove the files upon completion of spooling"),NULL
  },{
    "norun",'n',POPT_ARG_NONE,&no_run,0,
    _("gpr option: Print the command line rather than executing the command"),
    NULL
  },{
    "ppdfilt",'p',POPT_ARG_INT,&ppdfilt,0,
    _("gpr option: Use ppdfilt: 0 = Never, 1 = Always, 2 = with non-default PPD"),
    NULL
  },POPT_AUTOHELP
  {
    NULL,'\0',0,NULL,0,NULL,NULL
  }
};

  //seems this is useless
 gtk_set_locale();

  
  //gnome_init("gpr", VERSION, argc, argv);
#ifdef USE_GNOME
  gnome_init_with_popt_table("gpr", VERSION, argc, argv, options, 0, &pctx);
#else
  if(! (gtk_init_check(&argc, &argv))) 
    { g_error(_("error initializing GTK")) ;     return 1; }
#endif


#ifdef ENABLE_NLS
  bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
#if GTK_MAJOR_VERSION >= 2
  /* Mennucc: from here on, all messages will be translated before
     they are passed to GTK, that is, Pango, so they must be UTF-8.
     Thanks to Thomas Huriaux (see http://bugs.debian.org/361260 ) */
  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
  /* note that this means that any translated  message that may be written to
     the terminal will be UTF-8 as well!
     Should use g_set_printerr_handler () and iconv to translate back...
  */
#warning "FIX UTF OUTPUT"
#endif
  textdomain(PACKAGE);
#endif

#if HAVE_POPT_H
  {
    poptContext pctx;	/* the popt context */
    pctx= poptGetContext("gpr",argc,argv,
			 options, //const struct poptOption * options,
			 0);//int flags);
    
    int val;  
    while( (val= poptGetNextOpt(pctx)) >=0) 
      printf("%d  ",val); 
    if(val < -1 )    { 
      g_error("%s: %s\n",
	      poptBadOption(pctx, POPT_BADOPTION_NOALIAS),
	      poptStrerror(val));
      //g_error(_("error %d parsing command line parameters"),val) ;
      return 1;}
    
    args = poptGetArgs(pctx);
  

  if (args != NULL) {
    i = 0;
    while (args[i] != NULL) {
      ppd->file_to_print = g_strdup(args[i]);
      if (no_run)    g_message(" printing '%s' ",args[i]);
      ++i;
    }
    /* ensures we have the correct path to the file on the
       command_line */
    if (!g_path_is_absolute(ppd->file_to_print)) {
      ppd->file_to_print =
	g_strconcat(g_get_current_dir(), "/",
		    ppd->file_to_print, NULL);
    }
    if(i>1) g_warning("gpr prints only one file at a time (sorry about that)");
  }
 poptFreeContext(pctx);
  }
#else
  if(argc>1) g_warning(" compile gpr with libpopt to be able to use command line parameters");
#endif

  if (no_run)
    ppd->debug_flag = 1;

  /* 
   * Very simple command-line option parsing routine
   * Will expand and make more robust as we add more options.
   */
  if (printer)
    ppd->printer_name = g_strdup(printer);
  else {
    printer = g_getenv("PRINTER");
    if (printer)
      ppd->printer_name = g_strdup(printer);
  }

  if (remove_job) {
    if (ppd->lpr_opts)
      ppd->lpr_opts = g_strconcat(ppd->lpr_opts, "-r ", NULL);
    else
      ppd->lpr_opts = g_strconcat("-r ", NULL);
  }

  if (debug_on)
    ppd->debug_flag = 1;

  ppd->ppdfilt = ppdfilt;

  /*
    if (spooler == 1)
    ppd->spooler = 1;
  */

  switch (get_spooler_type()) {
  case SPOOLER_UNKNOWN:
    /* assume standard LPR */
    ppd->spooler = 0;
    break;
  case SPOOLER_LPR:
    ppd->spooler = 0;
    break;
  case SPOOLER_LPRNG:
    ppd->spooler = 1;
    break;
  case SPOOLER_CUPS:
    ppd->spooler = 2;
    ppdfilt = 1;
    break;
  case SPOOLER_PPR:
    ppd->spooler = 3;
    cmdname = "ppr -d";
    ppdfilt = 1;
    break;
  case SPOOLER_PDQ:
    ppd->spooler = 4;
    cmdname = "pdq -P";
    ppdfilt = 1;
    break;
  case SPOOLER_FOOMATIC_DIRECT:
    ppd->spooler = 5;
    cmdname = "directomatic -P";
    ppdfilt = 1;
    break;
  }

  /* 
   * Just ignore this, it allows Corel WPO2000 to print, though
   */
  /* 
     if(jobname){ if(ppd->lpr_opts) ppd->lpr_opts =
     g_strconcat(ppd->lpr_opts,"-J",jobname," ",NULL); else ppd->lpr_opts 
     = g_strconcat("-J",jobname," ",NULL); } */


  /*-----------------------------------------------------------------
    MLP: Parse the tab categories file, at its expected location.
  -----------------------------------------------------------------*/
#if HAVE_XML
  globalTabList = tab_parse_xml_file ("/usr/share/gpr/tabs/categories.xml");
#endif

  /* 
   * check to see if file is being piped in from an app or from command-line
   * if so, copy to a temp file
   */
  fstat(0, &info);
  if (S_ISFIFO(info.st_mode) || S_ISREG(info.st_mode)) {
    strcpy(tf_path, "/tmp/gpr-XXXXXX");
    if ((tfd = mkstemp(tf_path)) >= 0) {
      ppd->file_to_print = g_strdup(tf_path);
      tmpf = fdopen(tfd, "w+");
      while (fgets(curr_line, sizeof(curr_line), stdin)
	     != NULL) {
	fputs(curr_line, tmpf);
      }
      fclose(tmpf);
    }
  }

  gpr_inst_options_window = create_gpr_inst_options_window(ppd);

  gpr_main_window = create_gpr_main_window(ppd, gpr_inst_options_window);
  gtk_widget_show(gpr_main_window);

  gtk_main();
 
  /*A Mennucc: clean up is called if and only if the program is really closed*/
  clean_up(NULL, ppd);
  
  free(ppd);
  ppd = NULL;

  return 0;
}