~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/script-fu/script-fu-server.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* The GIMP -- an image manipulation program
 
1
/* GIMP - The GNU Image Manipulation Program
2
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or modify
18
18
 
19
19
#include "config.h"
20
20
 
21
 
#include <glib.h>               /* For G_OS_WIN32 */
22
 
 
23
 
#include <stdarg.h>
24
21
#include <stdlib.h>
25
 
#include <stdio.h>
26
22
#include <string.h>
 
23
#include <errno.h>
 
24
#include <time.h>
 
25
 
 
26
#ifdef HAVE_UNISTD_H
27
27
#include <unistd.h>
28
 
#include <time.h>
 
28
#endif
 
29
 
 
30
#ifdef HAVE_SYS_TIME_H
29
31
#include <sys/time.h>
 
32
#endif
 
33
 
30
34
#include <sys/types.h>
31
35
 
 
36
#include <glib.h>
 
37
 
32
38
#ifdef G_OS_WIN32
33
39
#include <winsock2.h>
 
40
#include <libgimpbase/gimpwin32-io.h>
34
41
#else
35
42
#include <sys/socket.h>
36
43
#ifdef HAVE_SYS_SELECT_H
39
46
#include <netinet/in.h>
40
47
#include <arpa/inet.h>
41
48
#include <netdb.h>
42
 
#include <errno.h>
43
49
#endif
44
50
 
45
 
#include <gtk/gtk.h>
 
51
#include <glib/gstdio.h>
46
52
 
47
53
#include "libgimp/gimp.h"
48
54
#include "libgimp/gimpui.h"
49
55
 
50
56
#include "script-fu-intl.h"
51
57
 
52
 
#include "siod-wrapper.h"
 
58
#include "scheme-wrapper.h"
53
59
#include "script-fu-server.h"
54
60
 
55
61
#ifdef G_OS_WIN32
128
134
 */
129
135
 
130
136
static void      server_start       (gint         port,
131
 
                                     const gchar *logfile);
 
137
                                     const gchar *logfile);
132
138
static gboolean  execute_command    (SFCommand   *cmd);
133
139
static gint      read_from_client   (gint         filedes);
134
140
static gint      make_socket        (guint        port);
135
141
static void      server_log         (const gchar *format,
136
 
                                     ...) G_GNUC_PRINTF (1, 2);
 
142
                                     ...) G_GNUC_PRINTF (1, 2);
137
143
static void      server_quit        (void);
138
144
 
139
145
static gboolean  server_interface   (void);
140
146
static void      response_callback  (GtkWidget   *widget,
141
147
                                     gint         response_id,
142
 
                                     gpointer     data);
 
148
                                     gpointer     data);
143
149
static void      print_socket_api_error (const gchar *api_name);
144
150
 
145
151
/*
183
189
 
184
190
void
185
191
script_fu_server_run (const gchar      *name,
186
 
                      gint              nparams,
187
 
                      const GimpParam  *params,
188
 
                      gint             *nreturn_vals,
189
 
                      GimpParam       **return_vals)
 
192
                      gint              nparams,
 
193
                      const GimpParam  *params,
 
194
                      gint             *nreturn_vals,
 
195
                      GimpParam       **return_vals)
190
196
{
191
197
  static GimpParam   values[1];
192
198
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
198
204
    {
199
205
    case GIMP_RUN_INTERACTIVE:
200
206
      if (server_interface ())
201
 
        {
202
 
          server_mode = TRUE;
 
207
        {
 
208
          server_mode = TRUE;
203
209
 
204
 
          /*  Start the server  */
205
 
          server_start (sint.port, sint.logfile);
206
 
        }
 
210
          /*  Start the server  */
 
211
          server_start (sint.port, sint.logfile);
 
212
        }
207
213
      break;
208
214
 
209
215
    case GIMP_RUN_NONINTERACTIVE:
216
222
 
217
223
    case GIMP_RUN_WITH_LAST_VALS:
218
224
      status = GIMP_PDB_CALLING_ERROR;
219
 
      g_warning ("Script-Fu server does handle \"GIMP_RUN_WITH_LAST_VALS\"");
 
225
      g_warning ("Script-Fu server does not handle \"GIMP_RUN_WITH_LAST_VALS\"");
220
226
 
221
227
    default:
222
228
      break;
330
336
}
331
337
 
332
338
static void
 
339
server_progress_start (const gchar *message,
 
340
                       gboolean     cancelable,
 
341
                       gpointer     user_data)
 
342
{
 
343
  /* do nothing */
 
344
}
 
345
 
 
346
static void
 
347
server_progress_end (gpointer user_data)
 
348
{
 
349
  /* do nothing */
 
350
}
 
351
 
 
352
static void
 
353
server_progress_set_text (const gchar *message,
 
354
                          gpointer     user_data)
 
355
{
 
356
  /* do nothing */
 
357
}
 
358
 
 
359
static void
 
360
server_progress_set_value (gdouble   percentage,
 
361
                           gpointer  user_data)
 
362
{
 
363
  /* do nothing */
 
364
}
 
365
 
 
366
 
 
367
/*
 
368
 * Suppress progress popups by installing progress handlers that do nothing.
 
369
 */
 
370
static const gchar *
 
371
server_progress_install (void)
 
372
{
 
373
  GimpProgressVtable vtable = { 0, };
 
374
 
 
375
  vtable.start     = server_progress_start;
 
376
  vtable.end       = server_progress_end;
 
377
  vtable.set_text  = server_progress_set_text;
 
378
  vtable.set_value = server_progress_set_value;
 
379
 
 
380
  return gimp_progress_install_vtable (&vtable, NULL);
 
381
}
 
382
 
 
383
static void
 
384
server_progress_uninstall (const gchar *progress)
 
385
{
 
386
  gimp_progress_uninstall (progress);
 
387
}
 
388
 
 
389
static void
333
390
server_start (gint         port,
334
 
              const gchar *logfile)
 
391
              const gchar *logfile)
335
392
{
 
393
  const gchar *progress;
 
394
 
336
395
  /* First of all, create the socket and set it up to accept connections. */
337
396
  /* This may fail if there's a server running on this port already.      */
338
397
  server_sock = make_socket (port);
345
404
 
346
405
  /*  Setup up the server log file  */
347
406
  if (logfile && *logfile)
348
 
    server_log_file = fopen (logfile, "a");
 
407
    server_log_file = g_fopen (logfile, "a");
349
408
  else
350
409
    server_log_file = NULL;
351
410
 
356
415
  clients = g_hash_table_new_full (g_direct_hash, NULL,
357
416
                                   NULL, (GDestroyNotify) g_free);
358
417
 
359
 
  server_log ("Script-fu server initialized and listening...\n");
 
418
  progress = server_progress_install ();
 
419
 
 
420
  server_log ("Script-Fu server initialized and listening...\n");
360
421
 
361
422
  /*  Loop until the server is finished  */
362
423
  while (! script_fu_done)
364
425
      script_fu_server_listen (0);
365
426
 
366
427
      while (command_queue)
367
 
        {
 
428
        {
368
429
          SFCommand *cmd = (SFCommand *) command_queue->data;
369
430
 
370
 
          /*  Process the command  */
371
 
          execute_command (cmd);
372
 
 
373
 
          /*  Remove the command from the list  */
374
 
          command_queue = g_list_remove (command_queue, cmd);
375
 
          queue_length--;
376
 
 
377
 
          /*  Free the request  */
378
 
          g_free (cmd->command);
379
 
          g_free (cmd);
380
 
        }
 
431
          /*  Process the command  */
 
432
          execute_command (cmd);
 
433
 
 
434
          /*  Remove the command from the list  */
 
435
          command_queue = g_list_remove (command_queue, cmd);
 
436
          queue_length--;
 
437
 
 
438
          /*  Free the request  */
 
439
          g_free (cmd->command);
 
440
          g_free (cmd);
 
441
      }
381
442
    }
382
443
 
 
444
  server_progress_uninstall (progress);
 
445
 
383
446
  server_quit ();
384
447
}
385
448
 
398
461
  time (&clock1);
399
462
 
400
463
  /*  run the command  */
401
 
  if (siod_interpret_string (cmd->command) != 0)
 
464
  if (ts_interpret_string (cmd->command) != 0)
402
465
    {
403
466
      error = TRUE;
404
 
      response = siod_get_error_msg ();
 
467
      response = ts_get_error_msg ();
405
468
      response_len = strlen (response);
406
469
 
407
470
      server_log ("%s\n", response);
410
473
    {
411
474
      error = FALSE;
412
475
 
413
 
      response = siod_get_success_msg ();
 
476
      response = ts_get_success_msg ();
414
477
      response_len = strlen (response);
415
478
 
416
479
      time (&clock2);
417
480
      server_log ("Request #%d processed in %f seconds, finishing on %s",
418
 
                  cmd->request_no, difftime (clock2, clock1), ctime (&clock2));
 
481
                  cmd->request_no, difftime (clock2, clock1), ctime (&clock2));
419
482
    }
420
483
 
421
484
  buffer[MAGIC_BYTE]     = MAGIC;
427
490
  for (i = 0; i < RESPONSE_HEADER; i++)
428
491
    if (cmd->filedes > 0 && send (cmd->filedes, buffer + i, 1, 0) < 0)
429
492
      {
430
 
        /*  Write error  */
431
 
        print_socket_api_error ("send");
432
 
        return FALSE;
 
493
        /*  Write error  */
 
494
        print_socket_api_error ("send");
 
495
        return FALSE;
433
496
      }
434
497
 
435
498
  for (i = 0; i < response_len; i++)
436
499
    if (cmd->filedes > 0 && send (cmd->filedes, response + i, 1, 0) < 0)
437
500
      {
438
 
        /*  Write error  */
439
 
        print_socket_api_error ("send");
440
 
        return FALSE;
 
501
        /*  Write error  */
 
502
        print_socket_api_error ("send");
 
503
        return FALSE;
441
504
      }
442
505
 
443
506
  return FALSE;
518
581
  clientaddr = g_hash_table_lookup (clients, GINT_TO_POINTER (cmd->filedes));
519
582
  time (&clock);
520
583
  server_log ("Received request #%d from IP address %s: %s on %s,"
521
 
              "[Request queue length: %d]",
522
 
              cmd->request_no,
523
 
              clientaddr ? clientaddr : "<invalid>",
524
 
              cmd->command, ctime (&clock), queue_length);
 
584
              "[Request queue length: %d]",
 
585
              cmd->request_no,
 
586
                  clientaddr ? clientaddr : "<invalid>",
 
587
                      cmd->command, ctime (&clock), queue_length);
525
588
 
526
589
  return 0;
527
590
}
647
710
 
648
711
  dlg = gimp_dialog_new (_("Script-Fu Server Options"), "script-fu",
649
712
                         NULL, 0,
650
 
                         gimp_standard_help_func, "plug-in-script-fu-server",
651
 
 
652
 
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
653
 
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
654
 
 
655
 
                         NULL);
 
713
                         gimp_standard_help_func, "plug-in-script-fu-server",
 
714
 
 
715
                         GTK_STOCK_CANCEL,   GTK_RESPONSE_CANCEL,
 
716
                         _("_Start Server"), GTK_RESPONSE_OK,
 
717
 
 
718
                         NULL);
 
719
 
 
720
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dlg),
 
721
                                           GTK_RESPONSE_OK,
 
722
                                           GTK_RESPONSE_CANCEL,
 
723
                                           -1);
656
724
 
657
725
  g_signal_connect (dlg, "response",
658
726
                    G_CALLBACK (response_callback),
673
741
  sint.port_entry = gtk_entry_new ();
674
742
  gtk_entry_set_text (GTK_ENTRY (sint.port_entry), "10008");
675
743
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
676
 
                             _("Server Port:"), 0.0, 0.5,
677
 
                             sint.port_entry, 1, FALSE);
 
744
                             _("Server port:"), 0.0, 0.5,
 
745
                             sint.port_entry, 1, FALSE);
678
746
 
679
747
  /*  The server logfile  */
680
748
  sint.log_entry = gtk_entry_new ();
681
749
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
682
 
                             _("Server Logfile:"), 0.0, 0.5,
683
 
                             sint.log_entry, 1, FALSE);
 
750
                             _("Server logfile:"), 0.0, 0.5,
 
751
                             sint.log_entry, 1, FALSE);
684
752
 
685
753
  gtk_widget_show (table);
686
754
  gtk_widget_show (dlg);
879
947
      emsg = unk;
880
948
      break;
881
949
    }
882
 
  fprintf (stderr, "%s failed: %s\n", api_name, emsg);
 
950
 
 
951
  g_printerr ("%s failed: %s\n", api_name, emsg);
883
952
#else
884
953
  perror (api_name);
885
954
#endif