home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 November / PCWorld_2004-11_cd.bin / software / temacd / poweroff / pwroff30.exe / src / commandline.c < prev    next >
C/C++ Source or Header  |  2003-08-07  |  18KB  |  606 lines

  1. #include "poweroff.h"
  2.  
  3. void Usage(char *errstr,...)
  4. {
  5.   char str[10000],tmpstr[100];
  6.   va_list param;
  7.  
  8.   Log("Usage start");
  9.   va_start(param,errstr);
  10.   vsprintf(tmpstr,errstr,param);
  11.   va_end(param);
  12.  
  13.   sprintf(str,"Error: %s\n",tmpstr);
  14.   strcat(str,
  15.         "Usage: poweroff action [options]\n\
  16. Actions: logoff, reboot, shutdown, poweroff, standby, hibernate, lock, wol, monitor_off, monitor_on or no_action\n\
  17. Options:\n\
  18.   -warn\t\t: Show a warning message before doing the action\n\
  19.   -warntime s\t: Show the warning message for s seconds\n\
  20.   -playsound\t: Play a sound when the warning is displayed\n\
  21.   -soundfile \"f\"\t: Specifies the .WAV file that must be played\n\
  22.   -msg \"m\"\t: Specifies the warning message\n\
  23.   -force\t\t: Force applications to close\n\
  24.   -wait s\t\t: Wait s seconds before checking the time\n\
  25.   -remote m\t: Do the action on the remote machine m (name or ip)\n\
  26.   -remschedule\t: Schedule on the remote machine\n\
  27.   -use_nt\t\t: Use the NT protocol to shutdown/reboot a remote machine\n\
  28.   -port p\t\t: Use port p to connect to the remote machine\n\
  29.   -username u\t: User NT username u to connect to the remote machine\n\
  30.   -password p\t: Use password p to connect to the remote machine\n\
  31.   -run\t\t: Run a program before doing the action\n\
  32.   -program \"p\"\t: Runs program p before doing the action\n\
  33.   -directory \"d\"\t: Start the program in directory d\n\
  34.   -notray\t\t: Do not show poweroff icon in the system tray\n\
  35.   -nocancel\t: Do not allow the user to cancel the action from the tray or in the warning dialog\n\
  36.   -immediate\t: Perform the action immediately (default)\n\
  37.   -scheduled\t: Perform the action in a scheduled manner\n\
  38.   -time hh:mm\t: Perform the action at the specified time\n\
  39.   -date dd/mm/yyyy\t: Perform the action on the specified date\n\
  40.   -days [d1,d2,...]\t: Perform the action on the spcified days of the week (m,t,w,th,s,su)\n\
  41.   -weeks [1,..,5]\t: Perform the action on the spcified weeks (default=all weeks)\n\
  42.   -day d\t\t: Perform the action on the specified day of the month (1-31)\n\
  43.   -seconds n\t: Perform the action after n seconds\n\
  44.   -process p\t: Perform the action when a process named p does not exist anymore\n\
  45.   -ip ip\t\t: Specifies the ip adress for WOL\n\
  46.   -subnet s\t: Specifies the subnet mask for WOL\n\
  47.   -mac m\t\t: Specifies the mac address for WOL\n\
  48.   -allow_remote\t: Allow remote control\n\
  49. ");
  50.   strcat(str,
  51. "  -remote_port p\t: Listen on port p for commands\n\
  52.   -remote_pswd p\t: Set a password that clients must give for remote control\n\
  53.   -simulate\t: Simulate all actions, do not perform them\n\
  54.   -minimize\t: Start minimized in the system tray\n\
  55.   -quiet\t\t: Don't display error messages\n\
  56.   -save_settings\t: Saves current settings to the registry\n\
  57.   -remove_settings\t: Remove the settings from the registry\n\
  58.   -save_svc_set\t: Save current settings to the service\n\
  59.   -remove_svc_set\t: Removes the service settings\n\
  60.   -create_service\t: Creates the poweroff service, including the settings\n\
  61.   -remove_service\t: Removes the poweroff service, including the settings\n\
  62.   -debug\t\t: Start poweroff in debug mode\n\
  63. ");
  64.   Error(NULL,NULL,str);
  65.   Log("Usage end");
  66.   exit(0);
  67. }
  68.  
  69. void CreateNewArgv(char ***argv,int *argc,char *commandline,int start_pos,int end_pos)
  70. {
  71.   *argv=(char**)realloc(*argv,(*argc+1)*sizeof(char*));
  72.   (*argv)[*argc]=malloc(end_pos-start_pos+1);
  73.   memcpy((*argv)[*argc],&commandline[start_pos],end_pos-start_pos);
  74.   (*argv)[*argc][end_pos-start_pos]=0;
  75.   (*argc)++;
  76. }
  77.  
  78. void ConvertCommandLine(char *commandline,int *argc,char ***argv)
  79. {
  80.   int lastpos=0,x;
  81.   char quote=0;
  82.  
  83.   *argv=NULL;
  84.   *argc=0;
  85.   for (x=0;x<=(int)strlen(commandline);x++)
  86.   {
  87.     if (!quote && (commandline[x]==' ' || commandline[x]=='\t' || commandline[x]==0))
  88.     {
  89.       if (lastpos!=x)
  90.         CreateNewArgv(argv,argc,commandline,lastpos,x);
  91.       lastpos=x+1;
  92.     }
  93.     else if ((commandline[x]=='"' || commandline[x]=='\'') && (!quote || commandline[x]==quote))
  94.     {
  95.       if (!quote)
  96.       {
  97.         quote=commandline[x];
  98.       }
  99.       else
  100.       {
  101.         CreateNewArgv(argv,argc,commandline,lastpos,x);
  102.         quote=0;
  103.       }
  104.       lastpos=x+1;
  105.     }
  106.   }
  107.   *argv=(char**)realloc(*argv,(*argc+1)*sizeof(char*));
  108.   (*argv)[*argc]=NULL;
  109. }
  110.  
  111. int ParseCommandLine(char *commandline,PowerSettings *ps)
  112. {
  113.   int argc,x,args_found=0;
  114.   char **argv;
  115.   CommandLineAction action=NOTHING;
  116.  
  117.   Log("ParseCommandLine start");
  118.   ConvertCommandLine(commandline,&argc,&argv);
  119.   strcpy(ps->executable,argv[0]);
  120.   for (x=1;x<argc;x++)
  121.   {
  122.     args_found++;
  123.     if (argv[x][0]=='-')
  124.         {
  125.             if (!strcmp(argv[x],"-force"))
  126.             {
  127.                 ps->options.force=1;
  128.             }
  129.             else if (!strcmp(argv[x],"-warn"))
  130.             {
  131.                 ps->options.warning=1;
  132.             }
  133.             else if (!strcmp(argv[x],"-quiet"))
  134.             {
  135.                 ps->quiet=1;
  136.             }
  137.             else if (!strcmp(argv[x],"-playsound"))
  138.             {
  139.                 ps->warning.play_sound=1;
  140.             }
  141.       else if (!strcmp(argv[x],"-soundfile"))
  142.       {
  143.         if (x<argc-1)
  144.         {
  145.           x++;
  146.           strcpy(ps->warning.sound_file,argv[x]);
  147.         }
  148.         else
  149.           Usage("Missing argument for -soundfile");
  150.       }
  151.             else if (!strcmp(argv[x],"-service"))
  152.             {
  153.                 ps->run_as_service=1;
  154.         args_found--;
  155.             }
  156.       else if (!strcmp(argv[x],"-warntime"))
  157.       {
  158.         if (x<argc-1)
  159.         {
  160.           x++;
  161.           ps->warning.seconds=atoi(argv[x]);
  162.         }
  163.         else
  164.           Usage("Missing argument for -warntime");
  165.       }
  166.       else if (!strcmp(argv[x],"-wait"))
  167.       {
  168.         if (x<argc-1)
  169.         {
  170.           x++;
  171.           ps->schedule.wait=atoi(argv[x]);
  172.         }
  173.         else
  174.           Usage("Missing argument for -wait");
  175.       }
  176.       else if (!strcmp(argv[x],"-msg"))
  177.       {
  178.         if (x<argc-1)
  179.         {
  180.           x++;
  181.           strcpy(ps->warning.message,argv[x]);
  182.           ReplaceString(ps->warning.message,"\\n","\r\n");
  183.           ReplaceString(ps->warning.message,"\\\r\n","\\n");
  184.         }
  185.         else
  186.           Usage("Missing argument for -msg");
  187.       }
  188.       else if (!strcmp(argv[x],"-remote"))
  189.       {
  190.         if (x<argc-1)
  191.         {
  192.           x++;
  193.           strcpy(ps->remote.computer_name,argv[x]);
  194.           ps->who=REMOTE_COMPUTER;
  195.         }
  196.         else
  197.           Usage("Missing argument for -remote");
  198.       }
  199.             else if (!strcmp(argv[x],"-remschedule"))
  200.             {
  201.                 ps->remote.schedule_remote=1;
  202.             }
  203.             else if (!strcmp(argv[x],"-use_nt"))
  204.             {
  205.                 ps->remote.use_nt=1;
  206.             }
  207.       else if (!strcmp(argv[x],"-port"))
  208.       {
  209.         if (x<argc-1)
  210.         {
  211.           x++;
  212.           ps->remote.port=atoi(argv[x]);
  213.           if (ps->remote.port<=0)
  214.             ps->remote.port=LISTEN_PORT;
  215.         }
  216.         else
  217.           Usage("Missing argument for -port");
  218.       }
  219.       else if (!strcmp(argv[x],"-password"))
  220.       {
  221.         if (x<argc-1)
  222.         {
  223.           x++;
  224.           strcpy(ps->remote.password,argv[x]);
  225.         }
  226.         else
  227.           Usage("Missing argument for -password");
  228.       }
  229.       else if (!strcmp(argv[x],"-username"))
  230.       {
  231.         if (x<argc-1)
  232.         {
  233.           x++;
  234.           strcpy(ps->remote.username,argv[x]);
  235.           ps->remote.current_credentials=0;
  236.         }
  237.         else
  238.           Usage("Missing argument for -username");
  239.       }
  240.             else if (!strcmp(argv[x],"-run"))
  241.             {
  242.                 ps->options.run_program=1;
  243.             }
  244.       else if (!strcmp(argv[x],"-program"))
  245.       {
  246.         if (x<argc-1)
  247.         {
  248.           x++;
  249.           strcpy(ps->program.program,argv[x]);
  250.         }
  251.         else
  252.           Usage("Missing argument for -program");
  253.       }
  254.       else if (!strcmp(argv[x],"-directory"))
  255.       {
  256.         if (x<argc-1)
  257.         {
  258.           x++;
  259.           strcpy(ps->program.directory,argv[x]);
  260.         }
  261.         else
  262.           Usage("Missing argument for -directory");
  263.       }
  264.             else if (!strcmp(argv[x],"-notray"))
  265.             {
  266.                 ps->options.in_tray=0;
  267.             }
  268.             else if (!strcmp(argv[x],"-nocancel"))
  269.             {
  270.                 ps->options.allow_cancel=0;
  271.             }
  272.             else if (!strcmp(argv[x],"-immediate"))
  273.             {
  274.                 ps->when=IMMEDIATE;
  275.             }
  276.             else if (!strcmp(argv[x],"-scheduled"))
  277.             {
  278.                 ps->when=SCHEDULED;
  279.             }
  280.             else if (!strcmp(argv[x],"-allow_remote"))
  281.             {
  282.                 ps->options.allow_remote_control=1;
  283.             }
  284.       else if (!strcmp(argv[x],"-remote_port"))
  285.       {
  286.         if (x<argc-1)
  287.         {
  288.           x++;
  289.           ps->remote_control.port=atoi(argv[x]);
  290.           if (ps->remote_control.port<=0)
  291.             ps->remote_control.port=LISTEN_PORT;
  292.         }
  293.         else
  294.           Usage("Missing argument for -remote_port");
  295.       }
  296.       else if (!strcmp(argv[x],"-remote_pswd"))
  297.       {
  298.         if (x<argc-1)
  299.         {
  300.           x++;
  301.           strcpy(ps->remote_control.password,EncodePassword(argv[x]));
  302.         }
  303.         else
  304.           Usage("Missing argument for -remote_pswd");
  305.       }
  306.             else if (!strcmp(argv[x],"-process"))
  307.             {
  308.                 ps->when=PROCESS;
  309.         if (x<argc-1)
  310.         {
  311.           x++;
  312.           strcpy(ps->process.process,argv[x]);
  313.         }
  314.         else
  315.           Usage("Missing argument for -process");
  316.             }
  317.             else if (!strcmp(argv[x],"-simulate"))
  318.             {
  319.                 ps->simulate=1;
  320.         args_found--;
  321.             }
  322.             else if (!strcmp(argv[x],"-minimize"))
  323.             {
  324.                 ps->start_minimized=1;
  325.         args_found--;
  326.             }
  327.       else if (!strcmp(argv[x],"-time"))
  328.       {
  329.         if (x<argc-1)
  330.         {
  331.           int hours,minutes;
  332.  
  333.           x++;
  334.           if (sscanf(argv[x],"%d:%d",&hours,&minutes)!=2)
  335.             Usage("Invalid time format for -time (%s)",argv[x]);
  336.           else
  337.           {
  338.             GetLocalTime(&ps->schedule.time);
  339.             ps->schedule.time.wHour=hours;
  340.             ps->schedule.time.wMinute=minutes;
  341.             if (ps->schedule.date.wYear==0)
  342.               GetLocalTime(&ps->schedule.date);
  343.           }
  344.         }
  345.         else
  346.           Usage("Missing argument for -time");
  347.       }
  348.       else if (!strcmp(argv[x],"-date"))
  349.       {
  350.         if (x<argc-1)
  351.         {
  352.           int days,months,years;
  353.  
  354.           x++;
  355.           if (sscanf(argv[x],"%d/%d/%d",&days,&months,&years)!=3)
  356.             Usage("Invalid format for -date (%s)",argv[x]);
  357.           else
  358.           {
  359.             GetLocalTime(&ps->schedule.date);
  360.             ps->schedule.date.wDay=days;
  361.             ps->schedule.date.wMonth=months;
  362.             ps->schedule.date.wYear=years;
  363.             ps->schedule.schedule=FIXED_DAY;
  364.           }
  365.         }
  366.         else
  367.           Usage("Missing argument for -date");
  368.       }
  369.       else if (!strcmp(argv[x],"-days"))
  370.       {
  371.         if (x<argc-1)
  372.         {
  373.           char days[10][10];
  374.           int n,y;
  375.  
  376.           x++;
  377.           ps->schedule.monday=0;
  378.           ps->schedule.tuesday=0;
  379.           ps->schedule.wednesday=0;
  380.           ps->schedule.thursday=0;
  381.           ps->schedule.friday=0;
  382.           ps->schedule.saturday=0;
  383.           ps->schedule.sunday=0;
  384.           n=sscanf(argv[x],"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%s",days[0],days[1],days[2],days[3],days[4],days[5],days[6]);
  385.           for (y=0;y<n;y++)
  386.           {
  387.             if (!strcmp(days[y],"m"))
  388.               ps->schedule.monday=1;
  389.             else if (!strcmp(days[y],"t"))
  390.               ps->schedule.tuesday=1;
  391.             else if (!strcmp(days[y],"w"))
  392.               ps->schedule.wednesday=1;
  393.             else if (!strcmp(days[y],"th"))
  394.               ps->schedule.thursday=1;
  395.             else if (!strcmp(days[y],"f"))
  396.               ps->schedule.friday=1;
  397.             else if (!strcmp(days[y],"s"))
  398.               ps->schedule.saturday=1;
  399.             else if (!strcmp(days[y],"su"))
  400.               ps->schedule.sunday=1;
  401.             else
  402.               Usage("Invalid day specified for -day (%s)",argv[x]);
  403.             ps->schedule.schedule=DAILY;
  404.           }
  405.         }
  406.         else
  407.           Usage("Missing argument for -days");
  408.       }
  409.       else if (!strcmp(argv[x],"-weeks"))
  410.       {
  411.         if (x<argc-1)
  412.         {
  413.           char weeks[10][10];
  414.           int n,y;
  415.  
  416.           x++;
  417.           ps->schedule.week1=0;
  418.           ps->schedule.week2=0;
  419.           ps->schedule.week3=0;
  420.           ps->schedule.week4=0;
  421.           ps->schedule.week5=0;
  422.           n=sscanf(argv[x],"%[^,],%[^,],%[^,],%[^,],%s",weeks[0],weeks[1],weeks[2],weeks[3],weeks[4]);
  423.           for (y=0;y<n;y++)
  424.           {
  425.             if (!strcmp(weeks[y],"1"))
  426.               ps->schedule.week1=1;
  427.             else if (!strcmp(weeks[y],"2"))
  428.               ps->schedule.week2=1;
  429.             else if (!strcmp(weeks[y],"3"))
  430.               ps->schedule.week3=1;
  431.             else if (!strcmp(weeks[y],"4"))
  432.               ps->schedule.week4=1;
  433.             else if (!strcmp(weeks[y],"5"))
  434.               ps->schedule.week5=1;
  435.             else
  436.               Usage("Invalid week number specified for -week (%s)",argv[x]);
  437.             ps->schedule.schedule=DAILY;
  438.           }
  439.         }
  440.         else
  441.           Usage("Missing argument for -weeks");
  442.       }
  443.       else if (!strcmp(argv[x],"-day"))
  444.       {
  445.         if (x<argc-1)
  446.         {
  447.           x++;
  448.           ps->schedule.day=atoi(argv[x]);
  449.           ps->schedule.schedule=DAY_OF_MONTH;
  450.         }
  451.         else
  452.           Usage("Missing argument for -day");
  453.       }
  454.       else if (!strcmp(argv[x],"-seconds"))
  455.       {
  456.         if (x<argc-1)
  457.         {
  458.           x++;
  459.           ps->schedule.seconds=atoi(argv[x]);
  460.           ps->schedule.schedule=AFTER_X_SECONDS;
  461.         }
  462.         else
  463.           Usage("Missing argument for -seconds");
  464.       }
  465.       else if (!strcmp(argv[x],"-ip"))
  466.       {
  467.         if (x<argc-1)
  468.         {
  469.           x++;
  470.           strcpy(ps->remote.ip_address,argv[x]);
  471.         }
  472.         else
  473.           Usage("Missing argument for -ip");
  474.       }
  475.       else if (!strcmp(argv[x],"-subnet"))
  476.       {
  477.         if (x<argc-1)
  478.         {
  479.           x++;
  480.           strcpy(ps->remote.subnet_mask,argv[x]);
  481.         }
  482.         else
  483.           Usage("Missing argument for -subnet");
  484.       }
  485.       else if (!strcmp(argv[x],"-mac"))
  486.       {
  487.         if (x<argc-1)
  488.         {
  489.           x++;
  490.           strcpy(ps->remote.mac_address,argv[x]);
  491.         }
  492.         else
  493.           Usage("Missing argument for -mac");
  494.       }
  495.       else if (!strcmp(argv[x],"-save_settings"))
  496.       {
  497.         action=SAVE_SETTINGS;
  498.       }
  499.       else if (!strcmp(argv[x],"-remove_settings"))
  500.       {
  501.         action=REMOVE_SETTINGS;
  502.       }
  503.       else if (!strcmp(argv[x],"-save_svc_set"))
  504.       {
  505.         action=SAVE_SERVICE_SETTINGS;
  506.       }
  507.       else if (!strcmp(argv[x],"-remove_svc_set"))
  508.       {
  509.         action=REMOVE_SERVICE_SETTINGS;
  510.       }
  511.       else if (!strcmp(argv[x],"-create_service"))
  512.       {
  513.         action=CREATE_SERVICE;
  514.       }
  515.       else if (!strcmp(argv[x],"-remove_service"))
  516.       {
  517.         action=REMOVE_SERVICE;
  518.       }
  519.       else if (!strcmp(argv[x],"-debug"))
  520.       {
  521.         debug=1;
  522.         args_found--;
  523.       }
  524.       else
  525.         Usage("Invalid option specified (%s)",argv[x]);
  526.     }
  527.     else
  528.     {
  529.       if (!_stricmp(argv[x],"logoff"))
  530.         ps->action=LOGOFF;
  531.       else if (!_stricmp(argv[x],"reboot"))
  532.         ps->action=REBOOT;
  533.       else if (!_stricmp(argv[x],"shutdown"))
  534.         ps->action=SHUTDOWN;
  535.       else if (!_stricmp(argv[x],"poweroff"))
  536.         ps->action=POWEROFF;
  537.       else if (!_stricmp(argv[x],"standby"))
  538.         ps->action=STANDBY;
  539.       else if (!_stricmp(argv[x],"hibernate"))
  540.         ps->action=HIBERNATE;
  541.       else if (!_stricmp(argv[x],"lock"))
  542.         ps->action=LOCK;
  543.       else if (!_stricmp(argv[x],"monitor_off"))
  544.         ps->action=MONITOR_OFF;
  545.       else if (!_stricmp(argv[x],"monitor_on"))
  546.         ps->action=MONITOR_ON;
  547.       else if (!_stricmp(argv[x],"no_action"))
  548.         ps->action=NO_ACTION;
  549.       else if (!_stricmp(argv[x],"wol"))
  550.       {
  551.         ps->action=WAKE_ON_LAN;
  552.         ps->who=REMOTE_COMPUTER;
  553.       }
  554.       else
  555.         Usage("Invalid action specified (%s)",argv[x]);
  556.       ps->interactive=0;
  557.     }
  558.   }
  559.   switch (action)
  560.   {
  561.     case SAVE_SETTINGS:
  562.       SaveSettings(HKEY_CURRENT_USER,"SOFTWARE\\Poweroff",ps);
  563.       exit(0);
  564.       break;
  565.     case REMOVE_SETTINGS:
  566.       if (!DeleteRegistryKey(HKEY_CURRENT_USER,"SOFTWARE\\Poweroff"))
  567.       {
  568.         DisplayLastError(ps,NULL);
  569.       }
  570.       exit(0);
  571.       break;
  572.     case SAVE_SERVICE_SETTINGS:
  573.       SaveSettings(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\Poweroff\\Parameters",ps);
  574.       if (ps->windowsversion.dwPlatformId!=VER_PLATFORM_WIN32_WINDOWS)
  575.       {
  576.         if (IsPoweroffServiceRunning(NULL,ps))
  577.         {
  578.           StopPoweroffService(NULL,ps);
  579.           StartPoweroffService(NULL,ps);
  580.         }
  581.       }
  582.       exit(0);
  583.       break;
  584.     case REMOVE_SERVICE_SETTINGS:
  585.       if (!DeleteRegistryKey(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\Poweroff\\Parameters"))
  586.       {
  587.         DisplayLastError(ps,NULL);
  588.       }
  589.       exit(0);
  590.       break;
  591.     case CREATE_SERVICE:
  592.       CreatePoweroffService(NULL,ps);
  593.       exit(0);
  594.       break;
  595.     case REMOVE_SERVICE:
  596.       if (IsPoweroffServiceRunning(NULL,ps))
  597.         StopPoweroffService(NULL,ps);
  598.       RemovePoweroffService(NULL,ps);
  599.       exit(0);
  600.       break;
  601.   }
  602.   if (args_found>0)
  603.     return 1;
  604.   Log("ParseCommandLine end");
  605.   return 0;
  606. }