sdkcreationmw/sdkruntimes/NMIT/instreg/instreg.c
author rajpuroh
Mon, 08 Mar 2010 12:09:11 +0530
changeset 0 b26acd06ea60
permissions -rw-r--r--
First Contribution of SDK components
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     1
/*
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     2
* Copyright (c) 2003-2005 Nokia Corporation and/or its subsidiary(-ies).
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     3
* All rights reserved.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     4
* This component and the accompanying materials are made available
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     6
* which accompanies this distribution, and is available
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     8
*
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
     9
* Initial Contributors:
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    11
*
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    12
* Contributors:
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    13
*
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    14
* Description: 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    15
*
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    16
*/
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    17
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    18
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    19
#include "s_lib.h"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    20
#include "instreg.h"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    21
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    22
/* Exit codes */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    23
#define EXITCODE_SUCCESS    0   /* successful completion */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    24
#define EXITCODE_HELP       1   /* help printed, nothing done */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    25
#define EXITCODE_CMDLINE    2   /* command line parse error */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    26
#define EXITCODE_IOERR      3   /* cannot write installation registry */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    27
#define EXITCODE_NOMEM      4   /* out of memory (very unlikely) */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    28
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    29
/* Data structures */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    30
struct _MemContext {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    31
    const MemHook * next;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    32
};
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    33
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    34
typedef struct _AppContext {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    35
    Bool unregister;            /* true if we are unregistering the product */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    36
    StrBuf sb;                  /* temporary string buffer */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    37
    DOMNode* root;              /* root node of the DOM tree */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    38
    Str name;                   /* product name */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    39
    Str ver;                    /* product version, NULL if unknown */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    40
    Str loc;                    /* product location */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    41
    Str exe;                    /* product executable */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    42
    Str type;                   /* product type */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    43
    Str nameEsc;                /* XML-escaped product name */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    44
    Str typeEsc;                /* XML-escaped product type */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    45
    Str verEsc;                 /* XML-escaped version */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    46
    Str exeEsc;                 /* XML-escaped path */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    47
    Str locEsc;                 /* XML-escaped location */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    48
} AppContext;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    49
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    50
/* Tags in the installation registry file */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    51
#define REGISTRY_TAG    "registry"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    52
#define PRODUCT_TAG     "product"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    53
#define NAME_TAG        "name"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    54
#define VERSION_TAG     "version"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    55
#define EXECUTABLE_TAG  "executable"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    56
#define LOCATION_TAG    "location"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    57
#define TYPE_ATTR       "type"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    58
#define EMULATOR_TYPE   "emulator"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    59
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    60
#define ROOT_TAG        REGISTRY_TAG
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    61
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    62
/*
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    63
 * Some default values. Note that we are making an assumption that the
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    64
 * default values don't need to be XML-escaped.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    65
 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    66
#define DEFAULT_TYPE    EMULATOR_TYPE
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    67
#define DEFAULT_VERSION "1.0"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    68
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    69
/* Desctiption of the command line parameters */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    70
#define CMDLINE_PARAM   "PRODUCT"
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    71
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    72
/* Modifiable global data */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    73
static Char* pname = "instreg";
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    74
static Char regfile[] = "C:\\Nokia\\Registry\\installationRegistry.xml";
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    75
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    76
/* Constants */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    77
static const Str commonHeader = "\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    78
<?xml version=\"1.0\" standalone=\"yes\"?>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    79
\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    80
<!DOCTYPE "REGISTRY_TAG" [\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    81
<!ELEMENT "REGISTRY_TAG" ("PRODUCT_TAG")*>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    82
<!ELEMENT "PRODUCT_TAG" ("NAME_TAG", "VERSION_TAG", "EXECUTABLE_TAG", "\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    83
LOCATION_TAG")>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    84
<!ELEMENT "NAME_TAG" (#PCDATA)>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    85
<!ELEMENT "VERSION_TAG" (#PCDATA)>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    86
<!ELEMENT "EXECUTABLE_TAG" (#PCDATA)>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    87
<!ELEMENT "LOCATION_TAG" (#PCDATA)>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    88
<!ATTLIST "PRODUCT_TAG" "TYPE_ATTR" CDATA #IMPLIED>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    89
]>\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    90
\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    91
";
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    92
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    93
/* This template is used when we are creating a new file */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    94
static const Str newTemplate = "%s\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    95
<"REGISTRY_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    96
    <"PRODUCT_TAG" "TYPE_ATTR"=\"%s\">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    97
        <"NAME_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    98
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
    99
        </"NAME_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   100
        <"VERSION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   101
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   102
        </"VERSION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   103
        <"EXECUTABLE_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   104
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   105
        </"EXECUTABLE_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   106
        <"LOCATION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   107
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   108
        </"LOCATION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   109
    </"PRODUCT_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   110
</"REGISTRY_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   111
";
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   112
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   113
/* This template is used when we are updating the existing entry */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   114
static const Str updateTemplate = "\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   115
<"PRODUCT_TAG" "TYPE_ATTR"=\"%s\">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   116
        <"NAME_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   117
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   118
        </"NAME_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   119
        <"VERSION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   120
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   121
        </"VERSION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   122
        <"EXECUTABLE_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   123
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   124
        </"EXECUTABLE_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   125
        <"LOCATION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   126
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   127
        </"LOCATION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   128
    </"PRODUCT_TAG">\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   129
";
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   130
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   131
/* Template for appending a new entry to the existing file */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   132
static const Str appendTemplate = "\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   133
    <"PRODUCT_TAG" "TYPE_ATTR"=\"%s\">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   134
        <"NAME_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   135
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   136
        </"NAME_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   137
        <"VERSION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   138
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   139
        </"VERSION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   140
        <"EXECUTABLE_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   141
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   142
        </"EXECUTABLE_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   143
        <"LOCATION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   144
            %s\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   145
        </"LOCATION_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   146
    </"PRODUCT_TAG">\n\
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   147
";
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   148
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   149
/**
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   150
 * Memory hooks. If memory allocation fails, we exit the program with
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   151
 * EXITCODE_NOMEM status. Therefore, we don't need for check for memory
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   152
 * allocation failures in the code.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   153
 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   154
static Bool APP_MemInit(MemContext* ctx, const MemHook* next)
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   155
{
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   156
    ctx->next = next;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   157
    return True;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   158
}
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   159
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   160
static void* APP_MemAlloc(MemContext* ctx, size_t size)
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   161
{
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   162
    void* ptr = MEM_Alloc2(ctx->next, size);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   163
    if (!ptr) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   164
        TRACE_Error("Out of memory!\n");
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   165
        exit(EXITCODE_NOMEM);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   166
    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   167
    return ptr;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   168
}
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   169
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   170
static Bool APP_PrintTag(File* out, Str tag, const XMLAttr* a, StrBuf* sb)
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   171
{
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   172
    if (FILE_Puts(out, TEXT("<")) && FILE_Puts(out, tag)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   173
        int i;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   174
        int n = XML_AttrCount(a);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   175
        for (i=0; i<n; i++) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   176
            Str name = XML_AttrNameAt(a, i);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   177
            Str value = XML_AttrValueAt(a, i);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   178
            Str esc = XML_Escape(NULL, value);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   179
            if (!esc) esc = XML_Escape(sb, value);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   180
            if (FILE_Printf(out, TEXT(" %s=\"%s\""), name, esc) < 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   181
                return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   182
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   183
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   184
        return (FILE_Puts(out, TEXT(">")) > 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   185
    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   186
    return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   187
}
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   188
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   189
/**
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   190
 * Prints the DOM node to the specified stream.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   191
 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   192
static Bool APP_PrintDOM(File* out, DOMNode* node, StrBuf* sb)
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   193
{
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   194
    if (APP_PrintTag(out, DOM_TagName(node),DOM_Attr(node), sb)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   195
        if (DOM_ChunkCount(node) == 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   196
            Str charData = DOM_CharData(node);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   197
            if (charData && FILE_Printf(out, TEXT("%s"), charData) < 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   198
                return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   199
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   200
        } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   201
            DOMChunk* chunk = DOM_FirstChunk(node);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   202
            while (chunk) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   203
                DOMNode* child = DOM_ChunkNode(chunk);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   204
                Str text = DOM_ChunkText(chunk);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   205
                ASSERT(text || child);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   206
                if ((text && !FILE_Puts(out, text)) ||
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   207
                    (child && !APP_PrintDOM(out, child, sb))) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   208
                    return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   209
                }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   210
                chunk=DOM_NextChunk(chunk);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   211
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   212
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   213
        return (FILE_Printf(out, TEXT("</%s>"),DOM_TagName(node)) > 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   214
    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   215
    return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   216
}
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   217
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   218
/**
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   219
 * Matches the child tag of the product node against the expected value
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   220
 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   221
static Bool APP_MatchInfo(DOMNode* p, Str tag, Str expect, StrBuf* sb)
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   222
{
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   223
    if (expect) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   224
        DOMNode* node = DOM_FindFirst(p, tag);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   225
        if (node) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   226
            Str data = DOM_CharData(node);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   227
            if (data && StrStr(data, expect)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   228
                STRBUF_Copy(sb, data);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   229
                STRBUF_Trim(sb);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   230
                return STRBUF_EqualsTo(sb, expect);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   231
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   232
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   233
        /* no match */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   234
        return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   235
    } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   236
        /* nothing is expected, anything will do */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   237
        return True;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   238
    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   239
}
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   240
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   241
/**
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   242
 * Checks whether the node matches the product criteria specified on
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   243
 * the command line. This serves dual purpose. When registering the
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   244
 * product, this is how we find the existing entry to update. The
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   245
 * first entry that matches the criteria will be updated, all others
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   246
 * ignored. When unregistering the product, all matching entries are
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   247
 * removed.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   248
 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   249
static Bool APP_MatchProduct(DOMNode* product, AppContext* app)
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   250
{
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   251
    Bool match = False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   252
    if (product && StrCmp(DOM_TagName(product),PRODUCT_TAG) == 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   253
        Str type = XML_AttrValue(DOM_Attr(product),TYPE_ATTR);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   254
        if (!app->type || (type && StrCmp(type,app->type) == 0)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   255
            /* compare product information */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   256
            if (APP_MatchInfo(product, NAME_TAG,  app->name, &app->sb) &&
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   257
                APP_MatchInfo(product, VERSION_TAG,  app->ver, &app->sb) &&
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   258
                APP_MatchInfo(product, LOCATION_TAG,  app->loc, &app->sb) &&
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   259
                APP_MatchInfo(product, EXECUTABLE_TAG,  app->exe, &app->sb)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   260
                match = True;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   261
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   262
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   263
    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   264
    return match;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   265
}
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   266
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   267
/**
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   268
 * FILE_Save callback. If invoked from FILE_Save, this callback is actually
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   269
 * writing to a temporary file. The temporary file then gets renamed into the
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   270
 * actual output file. This guarantees that we either successfully update the
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   271
 * file, or don't change it at all. Note that we also invoke this callback 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   272
 * directly, to write to stdout.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   273
 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   274
static Bool APP_FileSaveCB(File* out, Str fname, void * ctx)
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   275
{
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   276
    AppContext* app = ctx;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   277
    Str tag = DOM_TagName(app->root);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   278
    if (FILE_Puts(out, commonHeader) &&
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   279
        APP_PrintTag(out, tag, DOM_Attr(app->root), &app->sb)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   280
        Bool updated = False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   281
        DOMChunk* chunk = DOM_FirstChunk(app->root);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   282
        while (chunk) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   283
            DOMNode* child = DOM_ChunkNode(chunk);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   284
            Str text = DOM_ChunkText(chunk);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   285
            if (child) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   286
                if (APP_MatchProduct(child, app)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   287
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   288
                    /* 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   289
                     * If unregistering, skip this entry. Also, when 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   290
                     * we find more that one matching entry when we are
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   291
                     * registering the product, we update the first one
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   292
                     * and remove the others.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   293
                     */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   294
                    if (!app->unregister && !updated) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   295
                        /* update the existing entry */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   296
                        if (!FILE_Puts(out, text) ||
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   297
                             FILE_Printf(out, updateTemplate, 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   298
                                        app->typeEsc, app->nameEsc, 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   299
                                        app->verEsc, app->exeEsc, 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   300
                                        app->locEsc) > 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   301
                            updated = True;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   302
                        } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   303
                            return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   304
                        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   305
                    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   306
                } else if (!FILE_Puts(out, text) ||
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   307
                           !APP_PrintDOM(out, child, &app->sb)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   308
                    return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   309
                }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   310
            } else if (text) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   311
                if (!FILE_Puts(out, text)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   312
                    return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   313
                }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   314
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   315
            chunk = DOM_NextChunk(chunk);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   316
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   317
        if (!updated) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   318
            if (DOM_ChunkCount(app->root) == 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   319
                Str charData = DOM_CharData(app->root);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   320
                if (charData && FILE_Printf(out, TEXT("%s"), charData) < 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   321
                    return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   322
                }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   323
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   324
            if (!app->unregister) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   325
                /* append a new entry */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   326
                if (FILE_Printf(out,appendTemplate, app->typeEsc, 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   327
                                app->nameEsc, app->verEsc, app->exeEsc,
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   328
                                app->locEsc) < 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   329
                    return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   330
                }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   331
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   332
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   333
        return (FILE_Printf(out, TEXT("</%s>\n"),tag) > 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   334
    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   335
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   336
    /* some kind of I/O error */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   337
    return False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   338
}
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   339
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   340
/**
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   341
 * The program entry point.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   342
 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   343
int main(int argc, char* argv[]) 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   344
{
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   345
    AppContext app;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   346
    Bool toStdout = False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   347
    Bool help = False;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   348
    Vector params;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   349
    CmdLine* c;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   350
    Str prog;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   351
    int status;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   352
    Char sysDirBuf[MAX_PATH +1];
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   353
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   354
    MemProc mp;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   355
    MemContext mc;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   356
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   357
    /* install memory hook so that we don't have to check for NULL */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   358
    MEM_InitModule();    
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   359
    memset(&mc, 0, sizeof(mc));
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   360
    memset(&mp, 0, sizeof(mp));
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   361
    mp.memInit = APP_MemInit;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   362
    mp.memAlloc = APP_MemAlloc;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   363
    MEM_Hook(&mp, &mc);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   364
    RANDOM_InitModule();    
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   365
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   366
    /* figure out the program name */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   367
    prog = FILE_FilePart(argv[0]);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   368
#ifdef _WIN32
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   369
    if (STRING_EndsWith(prog, ".exe")) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   370
        int n = StrLen(prog)-4;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   371
        pname = MEM_NewArray(Char, n+1);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   372
        StrnCpy(pname, prog, n);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   373
        pname[n] = 0;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   374
    } else 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   375
#endif /* _WIN32 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   376
    pname = STRING_Dup(prog);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   377
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   378
    /* 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   379
     * determine the drive where Windows is installed. That's the drive
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   380
     * where installationRegistry.xml file is located.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   381
     */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   382
#ifdef _WIN32
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   383
    if (GetSystemDirectory(sysDirBuf, COUNT(sysDirBuf))) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   384
        TRACE1("system drive is %c:\n",sysDirBuf[0]);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   385
        regfile[0] = sysDirBuf[0];
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   386
    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   387
#endif /* _WIN32 */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   388
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   389
    memset(&app, 0, sizeof(app));
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   390
    c = CMDLINE_Create(pname);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   391
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   392
#ifdef _CONSOLE
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   393
    CMDLINE_AddTrueOpt(c,'h',"help","print this help and exit",&help);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   394
    CMDLINE_AddTrueOpt(c,'c',"stdout","write updated installation registry "
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   395
        "to stdout",&toStdout);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   396
#endif /* _CONSOLE */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   397
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   398
    CMDLINE_SetParamName(CMDLINE_AddStrOpt(c,'e',"executable",
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   399
        "path to the executable (required to register)",&app.exe), "FILE");
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   400
    CMDLINE_SetParamName(CMDLINE_AddStrOpt(c,'v',"version",
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   401
        "product version (default is "DEFAULT_VERSION")",&app.ver),"VERSION");
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   402
    CMDLINE_SetParamName(CMDLINE_AddStrOpt(c,'l',"location",
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   403
        "product location (default is the executable folder)",&app.loc),"DIR");
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   404
    CMDLINE_SetParamName(CMDLINE_AddStrOpt(c,'t',"type",
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   405
        "product type (default is "DEFAULT_TYPE")",&app.type),"TYPE");
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   406
    CMDLINE_AddTrueOpt(c,'u',"unregister","unregister the product "
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   407
        "(default is to register)",&app.unregister);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   408
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   409
    VECTOR_Init(&params, 0, vectorEqualsString, NULL);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   410
    if (!CMDLINE_Parse(c,argv+1,argc-1,PARSE_NO_DUP,&params) || 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   411
        VECTOR_IsEmpty(&params) || help) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   412
        TRACE_Output(PRODUCT_NAME " Version %d.%d.%d\nCopyright (C) "
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   413
            PRODUCT_COPYRIGHT ". All rights reserved.\n\n", 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   414
            PRODUCT_VERSION_MAJOR, PRODUCT_VERSION_MINOR,
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   415
            PRODUCT_VERSION_MICRO);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   416
        CMDLINE_Usage(c, CMDLINE_PARAM, 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   417
        status = (help ? EXITCODE_HELP : EXITCODE_CMDLINE);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   418
    } else if (VECTOR_Size(&params) > 1) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   419
        TRACE_Error("%s: unexpected command line parameters\n", pname);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   420
        CMDLINE_Usage(c, CMDLINE_PARAM, 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   421
        status = EXITCODE_CMDLINE;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   422
    } else if (!app.exe && !app.unregister) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   423
        TRACE_Error("%s: product executable is required\n", pname);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   424
        CMDLINE_Usage(c, CMDLINE_PARAM, 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   425
        status = EXITCODE_CMDLINE;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   426
    } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   427
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   428
        StrBuf typeBuf;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   429
        StrBuf nameBuf;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   430
        StrBuf verBuf;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   431
        StrBuf exeBuf;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   432
        StrBuf locBuf;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   433
        Char* dir = NULL;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   434
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   435
        if (app.exe && !app.loc) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   436
            /* default location */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   437
            if (FILE_FilePart(app.exe) == app.exe) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   438
                int n = GetCurrentDirectory(0,NULL)+1;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   439
                dir = MEM_NewArray(Char,n);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   440
                dir[GetCurrentDirectory(n,dir)] = 0;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   441
            } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   442
                dir = FILE_DirName(app.exe, 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   443
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   444
            if (STRING_EndsWith(dir, FILE_SEPARATOR)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   445
                dir[StrLen(dir)-1] = 0;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   446
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   447
            app.loc = dir;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   448
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   449
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   450
        status = EXITCODE_SUCCESS;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   451
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   452
        /* XML escape the strings */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   453
        STRBUF_Init(&typeBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   454
        STRBUF_Init(&nameBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   455
        STRBUF_Init(&verBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   456
        STRBUF_Init(&exeBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   457
        STRBUF_Init(&locBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   458
        STRBUF_Init(&app.sb);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   459
        app.name = VECTOR_Get(&params, 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   460
        app.nameEsc = XML_Escape(&nameBuf, app.name);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   461
        if (app.exe) app.exeEsc = XML_Escape(&exeBuf, app.exe);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   462
        if (app.loc) app.locEsc = XML_Escape(&locBuf, app.loc);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   463
        
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   464
        if (app.type) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   465
            app.typeEsc = XML_Escape(&typeBuf, app.type);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   466
        } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   467
            /* we know there's nothing to escape here */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   468
            app.typeEsc = DEFAULT_TYPE;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   469
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   470
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   471
        if (app.ver) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   472
            app.verEsc = XML_Escape(&verBuf, app.ver);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   473
        } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   474
            /* we know there's nothing to escape here */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   475
            app.verEsc = DEFAULT_VERSION;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   476
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   477
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   478
        app.root = DOM_Load(regfile);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   479
        if (app.root && StrCmp(DOM_TagName(app.root),ROOT_TAG) == 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   480
            if (toStdout) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   481
                File* out = FILE_AttachToFile(stdout,"stdout");
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   482
                APP_FileSaveCB(out, FILE_Name(out), &app);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   483
                FILE_Close(out);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   484
            } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   485
                if (!FILE_Save(regfile, APP_FileSaveCB, &app, NULL)) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   486
                    status = EXITCODE_IOERR;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   487
                }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   488
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   489
        } else if (!app.unregister) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   490
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   491
            /*
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   492
             * either the file does not exist, or else it exists but 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   493
             * contains some garbage (root tag does not match the 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   494
             * expectation). Create a new file, unless the user has
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   495
             * given us -u (unregister) option in which case we have
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   496
             * nothing to do.
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   497
             */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   498
            if (toStdout) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   499
                printf(newTemplate, commonHeader, app.typeEsc, app.nameEsc,
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   500
                    app.verEsc, app.exeEsc, app.locEsc);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   501
            } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   502
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   503
                /* create a new file */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   504
                File* f = FILE_Open(regfile, WRITE_TEXT_MODE, NULL);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   505
                if (!f) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   506
                    /* perhaps, the directory does not exist? */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   507
                    Char* regdir = FILE_DirName(regfile, 0);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   508
                    FILE_MkDir(regdir);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   509
                    MEM_Free(regdir);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   510
                }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   511
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   512
                /* try again */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   513
                f = FILE_Open(regfile, WRITE_TEXT_MODE, NULL);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   514
                if (f) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   515
                    if (FILE_Printf(f, newTemplate, commonHeader, 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   516
                                    app.typeEsc, app.nameEsc, 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   517
                                    app.verEsc, app.exeEsc, 
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   518
                                    app.locEsc) < 0) {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   519
                        status = EXITCODE_IOERR;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   520
                    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   521
                    FILE_Close(f);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   522
                } else {
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   523
                    status = EXITCODE_IOERR;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   524
                }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   525
            }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   526
        }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   527
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   528
        STRBUF_Destroy(&app.sb);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   529
        DOM_Delete(app.root);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   530
        STRBUF_Destroy(&typeBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   531
        STRBUF_Destroy(&nameBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   532
        STRBUF_Destroy(&verBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   533
        STRBUF_Destroy(&exeBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   534
        STRBUF_Destroy(&locBuf);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   535
        MEM_Free(dir);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   536
    }
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   537
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   538
    /* Cleanup */
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   539
    MEM_Free(pname);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   540
    VECTOR_Destroy(&params);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   541
    CMDLINE_Delete(c);
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   542
    RANDOM_Shutdown();
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   543
    MEM_Shutdown();
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   544
    return status;
b26acd06ea60 First Contribution of SDK components
rajpuroh
parents:
diff changeset
   545
}