home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / utils sync / JetSync 1.0 / jetsync-1.0.exe / jetsync-1.0 / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-25  |  8.9 KB  |  378 lines

  1. #include <tk.h>
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <sys/wait.h>
  9.  
  10. #include "hotsync.h"
  11. #include "synchronize.h"
  12. #include "Random.h"
  13. #include "User.h"
  14. #include "defs.h"
  15. #include "assert.h"
  16. #include "proc.h"
  17.  
  18. #include "Comm.h"
  19.  
  20. /* Tcl/Tk reference variables*/
  21. Tk_Window mainw;
  22. Tcl_Interp *g_interp;
  23.  
  24. char home_dir[256]=""; /* inited at startup */
  25. char username[256]=""; /* inited at startup */
  26.  
  27.  
  28. /* daemon */
  29. int child_pid=-1; /* daemon usage */
  30. int daemon_flag = 0; /* 0 - always, 1 - only when jetsync, 2 - never */
  31. int write_daemon_line();
  32. int check_daemon();
  33.  
  34.  
  35. /* tcl/tk <-> C bindings */
  36. void create_commands(Tcl_Interp *interp);
  37.  
  38.  
  39.  
  40. /* quit and signal binds - also checks if a daemon is running and if it should
  41.    be killed or not */
  42. void __quit()
  43. {
  44. #ifdef __DEBUG
  45.   printf("Bailing out!\n");
  46. #endif
  47.  
  48.   if (!child_pid) { /* child exits */
  49.     child_pid = -1;
  50.     write_daemon_line();
  51.   }
  52.  
  53.   check_daemon();
  54.   if (daemon_flag != 0 && child_pid != -1) {
  55.     kill(child_pid, SIGINT);
  56.     child_pid = -1;
  57.     write_daemon_line();
  58.   }
  59.  
  60.   exit(0);
  61. }
  62.  
  63.  
  64. /* get current user - used in Tcl/Tk */
  65. /* Arguments: JETHOME directory as string */
  66. /* Returns: last user that used JetSync */
  67. __TCL__ int whoami(ClientData clientData, Tcl_Interp *interp, 
  68.                    int argc, char **argv)
  69. {
  70.   FILE *fp;
  71.   char line[80], path[256];
  72.  
  73. /* whoami receives one param stating JETHOME directory */
  74.   memset(home_dir, 0, 256);
  75.   strcpy(home_dir, argv[1]);
  76.  
  77.   sprintf(path,"%s/%s",home_dir,JS_PREFS);
  78.   fp = fopen(path,"r");
  79.   WARN(fp)
  80.  
  81.   while (!feof(fp)) {  
  82.     fgets(line, 80, fp);
  83.     
  84.     if (feof(fp)) break;
  85.   
  86.     strtok(line,":");
  87.     if (!line) {
  88.       printf("File \"conf/prefs\" in your installation directory is corrupt.\n");
  89.       return -1;
  90.     }
  91.   
  92.     if (!strcmp(line,"USER")) {
  93.       strcpy(interp->result, strtok(NULL,":"));
  94.       interp->result[strlen(interp->result)-1] = 0; /* remove '\n' */
  95.       break;
  96.     }
  97.   }
  98.   fclose(fp);
  99.  
  100.   return TCL_OK;
  101. }
  102.  
  103.  
  104. /* changes the current user */
  105. /* Arguments: argv[1] contains the new username */
  106. /* Returns: nothing */
  107. __TCL__ int tcl_change_user(ClientData clientData, Tcl_Interp *interp, 
  108.                             int argc, char **argv)
  109. {
  110.   change_user(argv[1]);
  111.   return TCL_OK;
  112. }
  113.  
  114.  
  115. /* Tcl/Tk call to quit program */
  116. __TCL__ int quit(ClientData clientData, Tcl_Interp *interp, 
  117.                  int argc, char **argv)
  118. {
  119.   __quit();
  120.   return TCL_OK;
  121. }
  122.  
  123.  
  124. /* trapping of the SIGINT signal */
  125. void *ctrlc(int i)
  126. {
  127.   __quit();
  128.   return 0;
  129. }
  130.  
  131.  
  132. /* auxiliary function to DoTrace */
  133. char *__xdtrace(ClientData clientData, Tcl_Interp *interp,        
  134.                 char *name1, char *name2, int flags)
  135. {
  136. #ifdef __DEBUG
  137.   printf("TRACE::%s ACCESS TO %s=%s\n",
  138.           flags & TCL_TRACE_READS ? "READ" : "WRITE",
  139.           name1,Tcl_GetVar(interp,name1,flags));
  140. #endif
  141.  
  142.   return NULL;
  143. }
  144.  
  145. /* handle tracing of a variable */
  146. __TCL__ int DoTrace(ClientData clientData, Tcl_Interp *interp, 
  147.                     int argc, char **argv)
  148. {
  149. #ifdef __DEBUG
  150.   printf("Tracing variable \"%s\"...\n", argv[1]);
  151. #endif
  152.   Tcl_TraceVar(interp, argv[1], 
  153.                TCL_TRACE_WRITES | TCL_GLOBAL_ONLY | TCL_TRACE_READS,
  154.                __xdtrace, clientData);
  155.   return 0;
  156. }
  157.  
  158.  
  159. /* get daemon preferences from global prefs file */
  160. /* Arguments: none */
  161. /* Returns: ignored */
  162. int check_daemon()
  163. {
  164.   FILE *fp;
  165.   char line[80], path[256];
  166.  
  167.   sprintf(path,"%s/%s",home_dir,JS_PREFS);
  168.   fp = fopen(path,"r");
  169.  
  170.   if (!fp) {
  171.     daemon_flag = 2; /* don't launch daemon */
  172.     return -1;
  173.   }
  174.  
  175.   while (!feof(fp)) {  
  176.     fgets(line, 80, fp);
  177.     
  178.     if (feof(fp)) break;
  179.   
  180.     strtok(line,":");
  181.   
  182.     if (!strcmp(line,"DAEMON")) {
  183.       daemon_flag = atoi(strtok(NULL,":"));
  184.       child_pid = atoi(strtok(NULL,":"));
  185.       break;
  186.     }
  187.   }
  188.  
  189.   fclose(fp);
  190.   return 0;
  191. }
  192.  
  193.  
  194. /* writes the appropriate values of the daemon interface to disk */
  195. /* Arguments: none */
  196. /* Returns: always 0 */
  197. int write_daemon_line()
  198. {
  199.   FILE *fp, *fpw;
  200.   char line[80], path[256], path2[256];
  201.  
  202.   sprintf(path,"%s/%s",home_dir,JS_PREFS);
  203.   fp = fopen(path,"r"); 
  204.   WARN(fp)
  205.     
  206.   sprintf(path2,"%s/%s.tmp",home_dir,JS_PREFS);
  207.   fpw = fopen(path2,"w");
  208.   WARN(fpw)
  209.   
  210.   memset((char *)line, 0, 80);    
  211.   fgets(line, 80, fp);                /* preserve 1st line (PCID) */
  212.   fputs(line, fpw);
  213.   memset((char *)line, 0, 80);    
  214.   fgets(line, 80, fp);                /* preserve 2nd line (USER) */
  215.   fputs(line, fpw);
  216.   memset((char *)line, 0, 80);    
  217.   fgets(line, 80, fp);                /* preserve 3rd line (MODEM) */
  218.   fputs(line, fpw);
  219.   memset((char *)line, 0, 80);    
  220.   fgets(line, 80, fp);                /* preserve 4th line (MODEMSPEED) */
  221.   fputs(line, fpw);
  222.   memset((char *)line, 0, 80);    
  223.   fgets(line, 80, fp);                /* preserve 5th line (MODEMTYPE) */
  224.   fputs(line, fpw);
  225.   memset((char *)line, 0, 80);    
  226.   fgets(line, 80, fp);                /* preserve 6th line (MODEMSTR) */
  227.   fputs(line, fpw);
  228.   memset((char *)line, 0, 80);    
  229.   fgets(line, 80, fp);                /* preserve 7th line (LOCAL) */
  230.   fputs(line, fpw);
  231.   memset((char *)line, 0, 80);    
  232.   fgets(line, 80, fp);                /* preserve 8th line (LOCALSPEED) */
  233.   fputs(line, fpw);
  234.   memset((char *)line, 0, 80);    
  235.   fgets(line, 80, fp);
  236. /* write new 9th line (DAEMON) */
  237.   fprintf(fpw,"DAEMON:%d:%d\n", daemon_flag, child_pid);
  238.   
  239.   fclose(fp);
  240.   fclose(fpw);  
  241.   rename(path2, path);
  242.  
  243.   return 0;
  244. }
  245.  
  246.  
  247.  
  248. /* background daemon that listens to synch requests sent via pilot */
  249. /* Arguments: none */
  250. /* Returns: always 0 unless an error occurs */
  251. int run_daemon()
  252. {
  253. /* check if daemon is to be present */
  254.   if (daemon_flag == 2) return 0;
  255.   if (child_pid != -1) {
  256. #ifdef __DEBUG
  257.     printf("Daemon is already running with PID %d\n", child_pid);
  258. #endif
  259.     return 0;
  260.   }
  261.  
  262.   if (!(child_pid=fork())) {
  263.     g_interp = Tcl_CreateInterp();
  264.     if (Tk_Init(g_interp) == TCL_ERROR) return TCL_ERROR;
  265.  
  266.     whoami(NULL, g_interp, 0, 0);
  267.     strcpy(username, g_interp->result);
  268.     printf("Daemon running in background...\n");
  269.     while (1) {
  270.       FILE *fp = fopen(getenv("PILOTPORT"),"r+b");
  271.       unsigned char c = fgetc(fp);
  272.       ungetc(c, fp);
  273.       fclose(fp);
  274.       printf("Synch signal received!\n");
  275.  
  276.       if (check_processes() < 0) {
  277.         int sd;
  278.         char path[256], logbuffer[1024]="";
  279.         struct PilotUser U;
  280.  
  281.         printf("Cannot start syncronization since there are JetSync "
  282.                "applications still running!\nPlease close all applications "
  283.                "first.\n");
  284.         js_Start(&sd, &U, username, path);
  285.         strcpy(logbuffer, "Cannot start syncronization since there are "
  286.                           "JetSync applications still running!\n");
  287.         js_CloseComm(sd, &U, logbuffer);
  288.  
  289.         continue;
  290.       }
  291.  
  292.       HOTSYNCMANAGER();
  293.     }
  294.   }
  295.   else
  296.     write_daemon_line();
  297.   return 0;
  298. }
  299.  
  300.  
  301.  
  302. /* program entry point */
  303. /* Arguments: ignored */
  304. /* Returns: always 0 unless an error occurs */
  305. int main(int argc, char **argv)
  306. {
  307.     signal(SIGINT,(void *)ctrlc);
  308.     signal(SIGTERM,(void *)ctrlc);
  309.  
  310. /* check if daemon should be launched and launch it */
  311.     check_daemon();
  312.     run_daemon();
  313.  
  314. /* enter Tcl/Tk main loop */
  315.     Tk_Main(argc, argv, Tcl_AppInit);
  316.  
  317.     return 0;
  318. }
  319.  
  320. /*
  321.  * Tcl_AppInit
  322.  *    This procedure performs application-specific initialization.
  323.  *    Most applications, especially those that incorporate additional
  324.  *    packages, will have their own version of this procedure.
  325.  *
  326.  * Results:
  327.  *    Returns a standard Tcl completion code, and leaves an error
  328.  *    message in interp->result if an error occurs.
  329.  *
  330.  */
  331.  
  332. int Tcl_AppInit(Tcl_Interp *interp)
  333. {
  334.   if (Tcl_Init(interp) == TCL_ERROR) return TCL_ERROR;
  335.   if (Tk_Init(interp) == TCL_ERROR) return TCL_ERROR;
  336.  
  337.   mainw = Tk_MainWindow(interp);
  338.   if (mainw == NULL) {
  339.     return TCL_ERROR;
  340.   }
  341.  
  342.   /*
  343.    * Call Tcl_CreateCommand for application-specific commands, if
  344.    * they weren't already created by the init procedures called above.
  345.    */
  346.  
  347.  
  348. /* hotsync.c */
  349.   Tcl_CreateCommand(interp,"initHotSyncPrefs",initHotSyncPrefs,0,0);
  350.   Tcl_CreateCommand(interp,"getHotSyncPrefs",getHotSyncPrefs,0,0);
  351.   Tcl_CreateCommand(interp,"setHotSyncPrefs",setHotSyncPrefs,0,0);
  352.  
  353. /* synchronize.c */
  354.   Tcl_CreateCommand(interp,"HOTSYNCMANAGER",HOTSYNCMANAGER,(ClientData *)NULL,
  355.                     (Tcl_CmdDeleteProc *) NULL);
  356.   Tcl_CreateCommand(interp,"DoTrace",DoTrace,(ClientData *)NULL,
  357.                     (Tcl_CmdDeleteProc *) NULL);
  358.  
  359. /* main.c */
  360.   Tcl_CreateCommand(interp,"whoami",whoami,(ClientData *)NULL,
  361.                     (Tcl_CmdDeleteProc *) NULL);
  362.   Tcl_CreateCommand(interp,"tcl_change_user",tcl_change_user,(ClientData *)NULL,
  363.                     (Tcl_CmdDeleteProc *) NULL);
  364.   Tcl_CreateCommand(interp,"quit",quit,(ClientData *)NULL,
  365.                     (Tcl_CmdDeleteProc *) NULL);
  366.  
  367.  
  368.   Tcl_SetVar(interp,"tcl_RcFileName","~/.wishrc",TCL_GLOBAL_ONLY);
  369.  
  370. /*
  371.   g_interp goal: keep track of the interp on a global pointer
  372.   usage: in libs and conduit code
  373. */
  374.   g_interp = interp;
  375.  
  376.   return TCL_OK;
  377. }
  378.