"Fossies" - the Fresh Open Source Software Archive

Member "jpilot-2_0_1/jpilot-sync.c" (3 Apr 2021, 8922 Bytes) of package /linux/privat/jpilot-2_0_1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "jpilot-sync.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.8.2_vs_2_0_1.

    1 /*******************************************************************************
    2  * jpilot-sync.c
    3  * A module of J-Pilot http://jpilot.org
    4  *
    5  * Copyright (C) 1999-2014 by Judd Montgomery
    6  *
    7  * This program is free software; you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by
    9  * the Free Software Foundation; version 2 of the License.
   10  *
   11  * This program is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program; if not, write to the Free Software
   18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   19  ******************************************************************************/
   20 
   21 /********************************* Includes ***********************************/
   22 #include "config.h"
   23 #include <stdlib.h>
   24 #include <string.h>
   25 #include <stdio.h>
   26 #include <signal.h>
   27 #ifdef HAVE_LOCALE_H
   28 #  include <locale.h>
   29 #endif
   30 
   31 #include "i18n.h"
   32 #include "utils.h"
   33 #include "log.h"
   34 #include "prefs.h"
   35 #include "sync.h"
   36 #include "plugins.h"
   37 #include "otherconv.h"
   38 
   39 /******************************* Global vars **********************************/
   40 int pipe_to_parent, pipe_from_parent;
   41 pid_t glob_child_pid;
   42 unsigned char skip_plugins;
   43 
   44 /* Start Hack */
   45 /* FIXME: The following is a hack.  
   46  * The variables below are global variables in jpilot.c which are unused in
   47  * this code but must be instantiated for the code to compile.  
   48  * The same is true of the functions which are only used in GUI mode. */
   49 pid_t jpilot_master_pid = -1;
   50 int *glob_date_label;
   51 GtkWidget *glob_dialog;
   52 gint glob_date_timer_tag;
   53 
   54 void output_to_pane(const char *str) { return; }
   55 void cb_app_button(GtkWidget *widget, gpointer data) { return; }
   56 void cb_cancel_sync(GtkWidget *widget, unsigned int flags) { return; }
   57 /* End Hack */
   58 
   59 /****************************** Main Code *************************************/
   60 static void fprint_jps_usage_string(FILE *out)
   61 {
   62    fprintf(out, "%s-sync [ -v || -h || [-d] [-P] [-b] [-l] [-p port] ]\n", EPN);
   63    fprintf(out, _(" J-Pilot preferences are read to get sync info such as port, rate, number of backups, etc.\n"));
   64    fprintf(out, _(" -v display version and compile options\n"));
   65    fprintf(out, _(" -h display help text\n"));
   66    fprintf(out, _(" -d display debug info to stdout\n"));
   67    fprintf(out, _(" -P skip loading plugins\n"));
   68    fprintf(out, _(" -b sync, and then do a backup\n"));
   69    fprintf(out, _(" -l loop, otherwise sync once and exit\n"));
   70    fprintf(out, _(" -p {port} use this port to sync on instead of default\n"));
   71 }
   72 
   73 static void sig_handler(int sig)
   74 {
   75 #ifdef ENABLE_PLUGINS
   76    struct plugin_s *plugin;
   77    GList *plugin_list, *temp_list;
   78 #endif
   79 
   80    jp_logf(JP_LOG_DEBUG, "caught signal %d\n", sig);
   81 
   82 #ifdef ENABLE_PLUGINS
   83    plugin_list = get_plugin_list();
   84 
   85    for (temp_list = plugin_list; temp_list; temp_list = temp_list->next) {
   86       plugin = (struct plugin_s *)temp_list->data;
   87       if (plugin) {
   88          if (plugin->plugin_exit_cleanup) {
   89             jp_logf(JP_LOG_DEBUG, "calling plugin_exit_cleanup\n");
   90             plugin->plugin_exit_cleanup();
   91          }
   92       }
   93    }
   94 #endif
   95 
   96    exit(0);
   97 }
   98 
   99 int main(int argc, char *argv[])
  100 {
  101    int flags;
  102    int r, i;
  103    int loop;
  104    char port[MAX_PREF_LEN];
  105 #ifdef ENABLE_PLUGINS
  106    struct plugin_s *plugin;
  107    GList *plugin_list, *temp_list;
  108    jp_startup_info info;
  109 #endif
  110 
  111    /* enable internationalization(i18n) before printing any output */
  112 #if defined(ENABLE_NLS)
  113 #  ifdef HAVE_LOCALE_H
  114       setlocale(LC_ALL, "");
  115 #  endif
  116    bindtextdomain(EPN, LOCALEDIR);
  117    textdomain(EPN);
  118 #endif
  119 
  120    port[0]='\0';
  121    glob_child_pid=0;
  122    loop=0;
  123    skip_plugins=0;
  124 
  125    flags = SYNC_NO_FORK;
  126 
  127    /* Read preferences from jpilot.rc file */
  128    pref_init();
  129    pref_read_rc_file();
  130    if (otherconv_init()) {
  131       printf("Error: could not set encoding\n");
  132       exit(1);
  133    }
  134 
  135    pipe_from_parent=STDIN_FILENO;
  136    pipe_to_parent=STDOUT_FILENO;
  137    glob_log_stdout_mask = JP_LOG_INFO | JP_LOG_WARN | JP_LOG_FATAL |
  138                           JP_LOG_STDOUT | JP_LOG_GUI;
  139 
  140    /* Parse command line options */
  141    for (i=1; i<argc; i++) {
  142       if (!strncasecmp(argv[i], "-v", 2)) {
  143          char options[1024];
  144          get_compile_options(options, sizeof(options));
  145          printf("\n%s\n", options);
  146          exit(0);
  147       }
  148       if ( (!strncmp(argv[i], "-h", 2)) || 
  149            (!strncasecmp(argv[1], "-?", 2))) {
  150          fprint_jps_usage_string(stderr);
  151          exit(0);
  152       }
  153       if (!strncasecmp(argv[i], "-d", 2)) {
  154          glob_log_stdout_mask = 0xFFFF;
  155          glob_log_file_mask = 0xFFFF;
  156          jp_logf(JP_LOG_DEBUG, "Debug messages on.\n");
  157       }
  158       if (!strncmp(argv[i], "-b", 2)) {
  159          flags |= SYNC_FULL_BACKUP;
  160       }
  161       if (!strncmp(argv[i], "-l", 2)) {
  162          loop=1;
  163       }
  164       if (!strncmp(argv[i], "-P", 2)) {
  165          skip_plugins = 1;
  166          flags |= SYNC_NO_PLUGINS;
  167          jp_logf(JP_LOG_INFO, _("Not loading plugins.\n"));
  168       }
  169       if (!strncmp(argv[i], "-p", 2)) {
  170          i++;
  171          if (i<argc) {
  172             g_strlcpy(port, argv[i], MAX_PREF_LEN);
  173             /* preference is not saved, so this is not persistent */
  174             set_pref(PREF_PORT, 0, port, FALSE);
  175          }
  176       }
  177    }
  178 
  179 #ifdef ENABLE_PLUGINS
  180    if (!skip_plugins) {
  181       load_plugins();
  182       plugin_list = get_plugin_list();
  183    } else {
  184       plugin_list = NULL;
  185    }
  186 
  187    for (temp_list = plugin_list; temp_list; temp_list = temp_list->next) {
  188       plugin = (struct plugin_s *)temp_list->data;
  189       jp_logf(JP_LOG_DEBUG, "plugin: [%s] was loaded\n", plugin->name);
  190       if (plugin) {
  191          if (plugin->plugin_startup) {
  192             info.base_dir = strdup(BASE_DIR);
  193             jp_logf(JP_LOG_DEBUG, "calling plugin_startup for [%s]\n", plugin->name);
  194             plugin->plugin_startup(&info);
  195             if (info.base_dir) {
  196                free(info.base_dir);
  197             }
  198          }
  199       }
  200    }
  201 #endif
  202 
  203    /* After plugin startups are called we want to make sure that cleanups
  204     * will be called */
  205    signal(SIGHUP, sig_handler);
  206    signal(SIGINT, sig_handler);
  207    signal(SIGTERM, sig_handler);
  208 
  209    do {
  210       r = setup_sync(flags);
  211       switch (r) {
  212        case 0:
  213          /* sync successful */
  214          break;
  215        case SYNC_ERROR_BIND:
  216          printf("\n");
  217          printf(_("Error: connecting to port %s\n"), port);
  218          break;
  219        case SYNC_ERROR_LISTEN:
  220          printf("\n");
  221          printf(_("Error: pi_listen\n"));
  222          break;
  223        case SYNC_ERROR_OPEN_CONDUIT:
  224          printf("\n");
  225          printf(_("Error: opening conduit to handheld\n"));
  226          break;
  227        case SYNC_ERROR_PI_ACCEPT:
  228          printf("\n");
  229          printf(_("Error: pi_accept\n"));
  230          break;
  231        case SYNC_ERROR_NOT_SAME_USER:
  232          printf("\n");
  233          printf(_("Error: "));
  234          printf(_("This handheld does not have the same user name.\n"));
  235          printf(_("as the one that was synced the last time.\n"));
  236 
  237          printf(_("Syncing with different handhelds to the same directory can destroy data.\n"));
  238 #ifdef ENABLE_PROMETHEON
  239          printf(_(" COPILOT_HOME"));
  240 #else
  241          printf(_(" JPILOT_HOME"));
  242 #endif
  243          printf(_(" environment variable can be used to sync different handhelds,\n"));
  244          printf(_(" to different directories for the same UNIX user name.\n"));
  245          break;
  246        case SYNC_ERROR_NOT_SAME_USERID:
  247          printf("\n");
  248          printf(_("This handheld does not have the same user ID.\n"));
  249          printf(_("as the one that was synced the last time.\n"));
  250          printf(_(" Syncing with different handhelds to the same directory can destroy data.\n"));
  251 #ifdef ENABLE_PROMETHEON
  252          printf(_(" COPILOT_HOME"));
  253 #else
  254          printf(_(" JPILOT_HOME"));
  255 #endif
  256          printf(_(" environment variable can be used to sync different handhelds,\n"));
  257          printf(_(" to different directories for the same UNIX user name.\n"));
  258          break;
  259        case SYNC_ERROR_NULL_USERID:
  260          printf("\n");
  261          printf(_("Error: "));
  262          printf(_("This handheld has a NULL user ID.\n"));
  263          printf(_("Every handheld must have a unique user ID in order to sync properly.\n"));
  264          printf(_("If the handheld has been hard reset, \n"));
  265          printf(_("   use restore from within "EPN" to restore it.\n"));
  266          printf(_("Otherwise, to add a new user name and ID\n"));
  267          printf(_("   use \"install-user %s name numeric_id\"\n"), port);
  268          break;
  269        default:
  270          printf("\n");
  271          printf(_("Error: sync returned error %d\n"), r);
  272       }
  273       sleep(1);
  274    } while (loop);
  275 
  276    otherconv_free();
  277 
  278    return r;
  279 }
  280