Back to index

lightning-sunbird  0.9+nobinonly
opendir.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <sys/types.h>
00038 #include "ONEiiop.h"
00039 #include "rdf.h"
00040 
00041 #if !defined(WIN32) 
00042 extern "C" int gethostname(char *name, int namelen); 
00043 #endif
00044 
00045 void
00046 WriteClient (void* obj, char* buffer) {
00047   size_t len = strlen(buffer);
00048   WAIWriteClient((ServerSession_t)obj, (const unsigned char *) buffer, len);
00049 }
00050 
00051 void AnswerOpenDirQuery(WriteClientProc callBack, void* obj, char* query, char* cookie);
00052 void  AnswerSearchQuery (WriteClientProc callBack, void* obj, char *query, char* cookie) ;
00053 
00054 #define PREFIX "<html><body><a href=\"/\"><center><img src=\"http://directory.mozilla.org/img/opendir.gif\" width=396 height=79 border=\"0\"></center></a>"
00055 
00056 #define POSTFIX "</body><html>"
00057 
00058 long
00059 Run(ServerSession_t obj)
00060 {
00061   char* query = (char*) malloc(300);
00062   char* val;
00063   char* cookie;
00064   WAIgetRequestInfo(obj, "QUERY",  &query);  
00065   WAIgetCookie(obj, &cookie); 
00066 
00067   if (!query) return 0;
00068   printf("Query = %s. Cookie = %s\n", query, cookie);
00069   val = strchr(query, '=');
00070   if (!val) return 0;
00071   val++;
00072   *(val -1) = '\0';
00073   WAIStartResponse(obj);
00074   /*  WriteClient(obj, PREFIX); */
00075   
00076   if (strcmp(query, "browse") == 0) {
00077     AnswerOpenDirQuery(WriteClient, obj, val, cookie);
00078   } else if (strcmp(query, "search") == 0) {
00079     AnswerSearchQuery(WriteClient, obj, val, cookie);
00080   } else if (strcmp(query, "query") == 0) {
00081     char** ans = processRDFQuery(val);
00082     if (ans) {
00083       int n = 0;
00084       for (n = 0; ans[n] != 0; n++) {
00085         WriteClient(obj, ans[n]);
00086       }
00087     }
00088   }
00089   /*  WriteClient(obj, POSTFIX); */
00090   return 0;
00091 }
00092 
00093 
00094 #if defined(WIN32)
00095 int WINAPI WinMain(
00096     HINSTANCE hInstance,    // handle to current instance
00097     HINSTANCE hPrevInstance,       // handle to previous instance
00098     LPSTR lpCmdLine, // pointer to command line
00099     int nCmdShow     // show state of window
00100    )
00101 
00102 {
00103   int argc = 0;
00104   char **argv = NULL;
00105 #else
00106   int main(int argc, char **argv)
00107     {
00108 #endif
00109       char localhost[256];
00110       char *host;
00111       RDF_Resource u;
00112       int n = 2;
00113       IIOPWebAppService_t obj;
00114       
00115       //
00116        // Normally we expect to see a hostname:port as
00117              // our one and only argument.  If we are started by
00118              // the OAD we will see an extra argument which should
00119              // be ignored.
00120              // The recommended registration with the OAD should 
00121        // specify the hostname:port as an argument to the
00122              // object provider.
00123              //
00124              // So try to ferrit out the hostname:port
00125 #ifndef WIN32
00126              // Easier if we just have a normal main.
00127                                if (argc > 1){
00128                                  if (argc == 1)
00129                                    host = argv[1];
00130                                  else if (*argv[1] != '-')
00131                      host = argv[1];
00132 
00133 #else
00134        // Windows command line drops the program name
00135        // that appears as argv[0].
00136        // GetCommandLine can be used to retrieve the
00137        // whole thing.
00138         n = 1;
00139        if (lpCmdLine && *lpCmdLine){
00140               if (*lpCmdLine != '-'){
00141                      char *sep = strchr(lpCmdLine, ' ');
00142                      if (sep)
00143                             *sep = '\0';
00144                      host = lpCmdLine;
00145               }
00146 #endif
00147        }else{
00148               gethostname(localhost, sizeof(localhost));
00149               host = localhost;
00150        }
00151 
00152        //
00153        // Pass in argc/argv if we have 'em.
00154 #ifndef WIN32
00155        obj = WAIcreateWebAppService("OpenDir", Run, argc, argv);
00156 #else
00157        /* obj = WAIcreateWebAppService("OpenDir", Run, 0, 0); */
00158 #endif
00159 /*     WAIregisterService(obj, host); */
00160         RDF_Initialize();
00161         printf("RDF Initialized!\n");
00162         if (argc == 0) {
00163         char** ans;
00164         RDF_ReadFile("s1");
00165         RDF_ReadFile("s2");
00166               } else {
00167         while (n < argc) {
00168            char* name = argv[n++];
00169              printf("Reading %s\n", name);
00170              RDF_ReadFile(name);
00171        }
00172               }
00173         printf("done");
00174 
00175        WAIimplIsReady();
00176     
00177        return 0;
00178 }
00179  
00180