home *** CD-ROM | disk | FTP | other *** search
/ PC Open 48 / pcopen48.iso / Internet / HtTrack / DATA1.CAB / Sources / src / htsalias.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-21  |  14.6 KB  |  479 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  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. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. This project has been developed by Xavier Roche and Yann Philippot,
  29. from the company Serianet at Caen, France (http://www.serianet.com)
  30.  
  31. Please visit our Website: http://www.httrack.com
  32. */
  33.  
  34.  
  35. /* ------------------------------------------------------------ */
  36. /* File: htsalias.c subroutines:                                */
  37. /*       alias for command-line options and config files        */
  38. /* Author: Xavier Roche                                         */
  39. /* ------------------------------------------------------------ */
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include "htsalias.h"
  45. #include "htsglobal.h"
  46. void linput(FILE* fp,char* s,int max);
  47. void hts_lowcase(char* s);
  48.  
  49. #define _NOT_NULL(a) ( (a!=NULL) ? (a) : "" )
  50. #define is_realspace(c) (strchr(" \x0d\x0a\x09",(c))!=NULL)
  51.  
  52. // COPY OF cmdl_ins in htsmain.c
  53. // Insert a command in the argc/argv
  54. #define cmdl_ins(token,argc,argv,buff,ptr) \
  55.   { \
  56.   int i; \
  57.   for(i=argc;i>0;i--)\
  58.   argv[i]=argv[i-1];\
  59.   } \
  60.   argv[0]=(buff+ptr); \
  61.   strcpy(argv[0],token); \
  62.   ptr += (strlen(argv[0])+1); \
  63.   argc++
  64. // END OF COPY OF cmdl_ins in htsmain.c
  65.  
  66.  
  67. /*
  68.   Aliases for command-line and config file definitions
  69.   These definitions can be used:
  70.   in command line:
  71.   --sockets=8       --cache=0
  72.   --sockets 8       --cache off
  73.                     --nocache
  74.   -c8               -C0
  75.   in config file:
  76.   sockets=8         cache=0
  77.   set sockets 8     cache off
  78.  
  79. */
  80. /*
  81.   single : no options
  82.   param  : this option allows a number parameter (1, for example) and can be mixed with other options (R1C1c8)
  83.   param1 : this option must be alone, and needs one distinct parameter (-P <path>)
  84.   param0 : this option must be alone, but the parameter should be put together (+*.gif)
  85. */
  86. const char hts_optalias[][4][64] = {
  87.   {"connections","-c","param","number of simultaneous connections allowed"},
  88.   {"cache","-C","param","number of retries for non-fatal errors"},
  89.  
  90.   
  91.   /*   {"","","",""}, */
  92.   {"path","-O","param1","output path"},
  93.   {"mirror","-w","single",""},
  94.   {"mirror-wizard","-W","single",""},
  95.   {"get-files","-g","single",""},
  96.   {"quiet","-q","single",""},
  97.   {"mirrorlinks","-Y","single",""},
  98.   {"proxy","-P","param1","proxy name:port"},
  99.   {"httpproxy-ftp","-%f","param",""},
  100.   {"recurse-levels","-r","param",""},
  101.   {"max-files","-m","param",""},
  102.   {"max-size","-M","param",""},
  103.   {"max-time","-E","param",""},
  104.   {"max-rate","-A","param",""},
  105.   {"max-pause","-G","param",""},
  106.   {"sockets","-c","param","number of simultaneous connections allowed"},
  107.   {"timeout","-T","",""},
  108.   {"retries","-R","param","number of retries for non-fatal errors"},
  109.   {"min-rate","-J","param",""},
  110.   {"host-control","-H","param",""},
  111.   {"extended-parsing","-%P","param",""},
  112.   {"near","-n","single",""},
  113.   {"test","-t","single",""},
  114.   {"list","-%L","param1",""},
  115.   {"structure","-N","param",""}, {"user-structure","-N","param1",""},
  116.   {"long-names","-L","param",""},
  117.   {"keep-links","-K","param",""},
  118.   {"replace-external","-x","single",""},
  119.   {"generate-errors","-o","single",""},
  120.   {"purge-old","-X","param",""},
  121.   {"cookies","-b","param",""},
  122.   {"check-type","-u","param",""},
  123.   {"parse-java","-j","param",""},
  124.   {"robots","-s","param",""},
  125.   {"http-10","-%h","single",""},
  126.   {"tolerant","-%B","single",""},
  127.   {"user-agent","-F","param1","user-agent identify"},
  128.   {"footer","-%F","param1",""},
  129.   {"cache","-C","param",""},
  130.   {"store-all-in-cache","-k","single",""},
  131.   {"do-not-recatch","-%n","single",""},
  132.   {"do-not-log","-Q","single",""},
  133.   {"extra-log","-z","single",""},
  134.   {"debug-log","-Z","single",""},
  135.   {"verbose","-v","single",""},
  136.   {"file-log","-f","single",""},
  137.   {"single-log","-f2","single",""},
  138.   {"index","-I","param",""},
  139.   {"priority","-p","param",""},
  140.   {"debug-headers","-%H","single",""},
  141.   {"userdef-cmd","-V","param1",""},
  142.   {"structure","-N","param1","user-defined structure"},
  143.   {"usercommand","-V","param1","user-defined command"},
  144.   {"display","-%v","single","show files transfered and other funny realtime information"},
  145.   /* */
  146.  
  147.  
  148.   
  149.   /* STANDARD ALIASES */
  150.   {"spider","-p0C0I0t","single",""},
  151.   {"testsite","-p0C0I0t","single",""},
  152.   {"testlinks","-r1p0C0I0t","single",""}, {"test","-r1p0C0I0t","single",""}, {"bookmark","-r1p0C0I0t","single",""},
  153.   {"mirror","-w","single",""},
  154.   {"testscan","-p0C0I0Q","single",""}, {"scan","-p0C0I0Q","single",""}, {"check","-p0C0I0Q","single",""},
  155.   {"skeleton","-p1","single",""},
  156.   {"get","-qg","single",""},
  157.   {"update","-iC2","single",""},
  158.   {"continue","-iC1","single",""}, {"restart","-iC1","single",""},
  159.   {"sucker","-r99","single",""},
  160.   {"help","-h","single",""}, {"documentation","-h","single",""}, {"doc","-h","single",""},
  161.   {"wide","-c32","single",""},
  162.   {"tiny","-c1","single",""},
  163.   {"ultrawide","-c48","single",""},
  164.   {"http10","-%h","single",""},
  165.   {"filelist","-%L","single",""}, {"list","-%L","single",""},
  166.   /* END OF ALIASES */
  167.  
  168.   /* Filters */
  169.   {"allow","+","param0","allow filter"},
  170.   {"deny","-","param0","deny filter"},
  171.   /* */
  172.  
  173.   /* URLs */
  174.   {"add","","param0","add URLs"},
  175.   /* */
  176.  
  177.   /* Specific */
  178.   {"user","-%U","param1","output path"},
  179.   /* */
  180.  
  181.   /* Internal */
  182.   {"catchurl","--catchurl","single","catch complex URL through proxy"},
  183.   {"updatehttrack","--updatehttrack","single","update HTTrack Website Copier"},
  184.   {"clean","--clean","single","clean up log files and cache"},
  185.   {"tide","--clean","single","clean up log files and cache"},
  186.   /* */
  187.  
  188.   {"","","",""}
  189. };
  190.  
  191.  
  192. /* 
  193.   Check for alias in command-line 
  194.   argc,argv     as in main()
  195.   n_arg         argument position
  196.   return_argv   a char[2][] where to put result
  197.   return_error  buffer in case of syntax error
  198.  
  199.   return value: number of arguments treated (0 if error)
  200. */
  201. int optalias_check(int argc,const char * const * argv,int n_arg,
  202.                    int* return_argc,char** return_argv,
  203.                    char* return_error) {
  204.   return_error[0]='\0';
  205.   *return_argc=1;
  206.   if (argv[n_arg][0]=='-')
  207.   if (argv[n_arg][1]=='-') {
  208.     char command[1000];
  209.     char param[1000];
  210.     char addcommand[256];
  211.     /* */
  212.     char* position;
  213.     int need_param=1;
  214.     //int return_param=0;
  215.     int pos;
  216.     command[0]=param[0]=addcommand[0]='\0';
  217.  
  218.     /* --sockets=8 */
  219.     if ( (position=strchr(argv[n_arg],'=')) ) {
  220.       /* Copy command */
  221.       strncat(command,argv[n_arg]+2,(int) (position - (argv[n_arg]+2)) );
  222.       /* Copy parameter */
  223.       strcpy(param,position+1);
  224.     }
  225.     /* --nocache */
  226.     else if (strncmp(argv[n_arg]+2,"no",2)==0) {
  227.       strcpy(command,argv[n_arg]+4);
  228.       strcpy(param,"0");
  229.     }
  230.     /* --sockets 8 */
  231.     else {
  232.       if (strncmp(argv[n_arg]+2,"wide-",5)==0) {
  233.         strcpy(addcommand,"c32");
  234.         strcpy(command,strchr(argv[n_arg]+2,'-')+1);
  235.       } else if (strncmp(argv[n_arg]+2,"tiny-",5)==0) {
  236.         strcpy(addcommand,"c1");
  237.         strcpy(command,strchr(argv[n_arg]+2,'-')+1);
  238.       } else
  239.         strcpy(command,argv[n_arg]+2);
  240.       need_param=2;
  241.     }
  242.  
  243.     /* Now solve the alias */
  244.     pos=optalias_find(command);
  245.     if (pos>=0) {
  246.       /* Copy real name */
  247.       strcpy(command,hts_optalias[pos][1]);
  248.       /* With parameters? */
  249.       if (strncmp(hts_optalias[pos][2],"param",5)==0) {
  250.         /* Copy parameters? */
  251.         if (need_param == 2) {
  252.           if ((n_arg+1>=argc) || (argv[n_arg+1][0]=='-')) {  /* no supplemental parameter */
  253.             sprintf(return_error,
  254.               "Syntax error:\n\tOption %s needs to be followed by a parameter: %s <param>\n\t%s\n",
  255.               command,command,_NOT_NULL(optalias_help(command)));
  256.             return 0;
  257.           }
  258.           strcpy(param,argv[n_arg+1]);
  259.           need_param=2;
  260.         }
  261.       } else
  262.         need_param=1;
  263.  
  264.       /* Final result */
  265.  
  266.       /* Must be alone (-P /tmp) */
  267.       if (strcmp(hts_optalias[pos][2],"param1")==0) {
  268.         strcpy(return_argv[0],command);
  269.         strcpy(return_argv[1],param);
  270.         *return_argc=2;     /* 2 parameters returned */
  271.       } 
  272.       /* Alone with parameter (+*.gif) */
  273.       else if (strcmp(hts_optalias[pos][2],"param0")==0) {
  274.         /* Command */
  275.         strcpy(return_argv[0],command);
  276.         strcat(return_argv[0],param);
  277.       }
  278.       /* Together (-c8) */
  279.       else {
  280.         /* Command */
  281.         strcpy(return_argv[0],command);
  282.         /* Parameters accepted */
  283.         if (strncmp(hts_optalias[pos][2],"param",5)==0) {
  284.           /* --cache=off or --index=on */
  285.           if (strcmp(param,"off")==0)
  286.             strcat(return_argv[0],"0");
  287.           else if (strcmp(param,"on")==0) {
  288.             // on is the default
  289.             // strcat(return_argv[0],"1");
  290.           } else
  291.             strcat(return_argv[0],param);
  292.         }
  293.         *return_argc=1;     /* 1 parameter returned */
  294.       }
  295.     } else {
  296.       sprintf(return_error,"Unknown option: %s\n",command);
  297.       return 0;
  298.     }
  299.     return need_param;
  300.   }
  301.  
  302.   /* Check -P <path> */
  303.   {
  304.     int pos;
  305.     if ((pos=optreal_find(argv[n_arg]))>=0) {
  306.       if ( (strcmp(hts_optalias[pos][2],"param1")==0) || (strcmp(hts_optalias[pos][2],"param0")==0)) {
  307.         if ((n_arg+1>=argc) || (argv[n_arg+1][0]=='-')) {  /* no supplemental parameter */
  308.           sprintf(return_error,
  309.             "Syntax error:\n\tOption %s needs to be followed by a parameter: %s <param>\n\t%s\n",
  310.             argv[n_arg],argv[n_arg],_NOT_NULL(optalias_help(argv[n_arg])));
  311.           return 0;
  312.         }
  313.         /* Copy parameters */
  314.         strcpy(return_argv[0],argv[n_arg]);
  315.         strcpy(return_argv[1],argv[n_arg+1]);
  316.         /* And return */
  317.         *return_argc=2;     /* 2 parameters returned */
  318.         return 2;           /* 2 parameters used */
  319.       }
  320.     }
  321.   }
  322.   
  323.   /* Copy and return other unknown option */
  324.   strcpy(return_argv[0],argv[n_arg]);
  325.   return 1;
  326. }
  327.  
  328. /* Finds the <token> option alias and returns the index, or -1 if failed */
  329. int optalias_find(const char* token) {
  330.   if (token[0] != '\0') {
  331.     int i=0;
  332.     while(hts_optalias[i][0][0] != '\0') {
  333.       if (strcmp(token,hts_optalias[i][0])==0) {
  334.         return i;
  335.       }
  336.       i++;
  337.     }
  338.   }
  339.   return -1;
  340. }
  341.  
  342. /* Finds the <token> real option and returns the index, or -1 if failed */
  343. int optreal_find(const char* token) {
  344.   if (token[0] != '\0') {
  345.     int i=0;
  346.     while(hts_optalias[i][0][0] != '\0') {
  347.       if (strcmp(token,hts_optalias[i][1])==0) {
  348.         return i;
  349.       }
  350.       i++;
  351.     }
  352.   }
  353.   return -1;
  354. }
  355.  
  356. const char* optreal_value(int p) {
  357.   return hts_optalias[p][1];
  358. }
  359. const char* optalias_value(int p) {
  360.   return hts_optalias[p][0];
  361. }
  362. const char* opttype_value(int p) {
  363.   return hts_optalias[p][2];
  364. }
  365. const char* opthelp_value(int p) {
  366.   return hts_optalias[p][3];
  367. }
  368.  
  369. /* Help for option <token>, empty if not available, or NULL if unknown <token> */
  370. const char* optalias_help(const char* token) {
  371.   int pos=optalias_find(token);
  372.   if (pos>=0)
  373.     return hts_optalias[pos][3];
  374.   else
  375.     return NULL;
  376. }
  377.  
  378. /* Include a file to the current command line */
  379. /* example:
  380.   set sockets 8
  381.   index on
  382.   allow *.gif
  383.   deny ad.*
  384. */
  385. int optinclude_file(const char* name,
  386.                     int* argc,char** argv,char* x_argvblk,int* x_ptr) {
  387.   FILE* fp;
  388.   fp=fopen(name,"rb");
  389.   if (fp) {
  390.     char line[256];
  391.     int insert_after=1;       /* first, insert after program filename */
  392.     while(!feof(fp)) {
  393.       char *a,*b;
  394.       int result;
  395.       
  396.       /* read line */
  397.       linput(fp,line,250);
  398.       hts_lowcase(line);
  399.       if (strnotempty(line)) {
  400.         /* no comment line: # // ; */
  401.         if (strchr("#/;",line[0])==NULL) {
  402.           /* right trim */
  403.           a=line+strlen(line)-1;
  404.           while(is_realspace(*a)) *(a--) = '\0';
  405.           /* jump "set " and spaces */
  406.           a=line;
  407.           while(is_realspace(*a)) a++;
  408.           if (strncmp(a,"set",3)==0) {
  409.             if (is_realspace(*(a+3))) {
  410.               a+=4;
  411.             }
  412.           }
  413.           while(is_realspace(*a)) a++;
  414.           /* delete = ("sockets=8") */
  415.           if ( (b=strchr(a,'=')) )
  416.             *b=' ';
  417.           
  418.           /* isolate option and parameter */
  419.           b=a;
  420.           while( (!is_realspace(*b)) && (*b) ) b++;
  421.           if (*b) {
  422.             *b='\0';
  423.             b++;
  424.           }
  425.           /* a is now the option, b the parameter */
  426.           
  427.           {
  428.             int return_argc;
  429.             char return_error[256];
  430.             char  _tmp_argv[4][HTS_CDLMAXSIZE];
  431.             char*  tmp_argv[4];
  432.             tmp_argv[0]=_tmp_argv[0]; tmp_argv[1]=_tmp_argv[1]; tmp_argv[2]=_tmp_argv[2]; tmp_argv[3]=_tmp_argv[3];
  433.             strcpy(tmp_argv[0],"--");
  434.             strcat(tmp_argv[0],a);
  435.             strcpy(tmp_argv[1],b);
  436.             
  437.             result=optalias_check(2,(const char * const *)tmp_argv,0,
  438.               &return_argc,(tmp_argv+2),
  439.               return_error);
  440.             if (!result) {
  441.               printf("%s\n",return_error);
  442.             } else {
  443.               int insert_after_argc;
  444.               /* Insert parameters BUT so that they can be in the same order */
  445.               /* temporary argc: Number of parameters after minus insert_after_argc */
  446.               insert_after_argc=(*argc)-insert_after;
  447.               cmdl_ins((tmp_argv[2]),insert_after_argc,(argv+insert_after),x_argvblk,(*x_ptr));
  448.               *argc=insert_after_argc+insert_after;
  449.               insert_after++;
  450.               /* Second one */
  451.               if (return_argc>1) {
  452.                 insert_after_argc=(*argc)-insert_after;
  453.                 cmdl_ins((tmp_argv[3]),insert_after_argc,(argv+insert_after),x_argvblk,(*x_ptr));
  454.                 *argc=insert_after_argc+insert_after;
  455.                 insert_after++;
  456.               }
  457.               /* increment to nbr of used parameters */
  458.               /* insert_after+=result; */
  459.             }
  460.           }
  461.         }
  462.         
  463.       }
  464.     }
  465.     fclose(fp);
  466.     return 1;
  467.   }
  468.   return 0;
  469. }
  470.  
  471. /* Get home directory, empty if failed */
  472. char* hts_gethome(void) {
  473.   char* home = getenv( "HOME" );
  474.   if (home)
  475.     return home;
  476.   else
  477.     return ".";
  478. }
  479.