summaryrefslogtreecommitdiffstats
path: root/tdeinit/tdestartupconfig.cpp
blob: 373ac6787d0d32d0ad93136ce7cac732204b6436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/****************************************************************************

 Copyright (C) 2005 Lubos Lunak        <l.lunak@kde.org>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

****************************************************************************/

/*

This utility helps to have some configuration options available in starttde
without the need to launch anything linked to KDE libraries (which may need
some time to load).

The configuration options are written to $TDEHOME/share/config/startupconfigkeys,
one option per line, as <file> <group> <key> <default>. It is possible to
use ' for quoting multiword entries. Values of these options will be written
to $TDEHOME/share/config/startupconfig as a shell script that will set
the values to shell variables, named <file>_<group>_<key> (all spaces replaced
by underscores, everything lowercase). So e.g. line
"ksplashrc KSplash Theme Default" may result in "ksplashrc_ksplash_theme=Default".

In order to real a whole group it is possible to use <file> <[group]>, e.g.
"ksplashrc [KSplash]", which will set shell variables for all keys in the group.
It is not possible to specify default values, but since the configuration options
are processed in the order they are specified this can be solved by first
specifying a group and then all the entries that need default values.

When a tdeconf_update script is used to update such option, tdestartupconfig is run
before tdeconf_update and therefore cannot see the change in time. To avoid this
problem, together with the tdeconf_update script also the matching global config
file should be updated (any change, tdestartupconfig will see the timestamp change).

Note that the kdeglobals config file is not used as a depedendency for other config
files.

Since the checking is timestamp-based, config files that are frequently updated
should not be used.

Kstartupconfig works by storing every line from startupconfigkeys in file startupconfigfiles
followed by paths of all files that are relevant to the option. Non-existent files
have '!' prepended (for the case they'll be later created), the list of files is
terminated by line containing '*'. If the timestamps of all relevant files are older
than the timestamp of the startupconfigfile file, there's no need to update anything.
Otherwise tdedostartupconfig is launched to create or update all the necessary files
(which already requires loading KDE libraries, but this case should be rare).

*/

#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
    {
    char tdehome[ 1024 ];
    if( getenv( "TDEHOME" ))
        strlcpy( tdehome, getenv( "TDEHOME" ), 1024 );
    else if( getenv( "HOME" ))
        {
        strlcpy( tdehome, getenv( "HOME" ), 1024 );
        strlcat( tdehome, "/.trinity", 1024  );
        }
    else
        return 1;
    char filename[ 1024 ];
    strlcpy( filename, tdehome, 1024 );
    strlcat( filename, "/share/config/startupconfig", 1024 );
    if( access( filename, R_OK ) != 0 )
        {
        int ret = system( "tdedostartupconfig" );
        return WEXITSTATUS( ret );
        }
    strlcpy( filename, tdehome, 1024 );
    strlcat( filename, "/share/config/startupconfigfiles", 1024 );
    struct stat st;
    if( stat( filename, &st ) != 0 )
        {
        int ret = system( "tdedostartupconfig" );
        return WEXITSTATUS( ret );
        }
    time_t config_time = st.st_mtime;
    FILE* config = fopen( filename, "r" );
    if( config == NULL )
        {
        int ret = system( "tdedostartupconfig" );
        return WEXITSTATUS( ret );
        }
    strlcpy( filename, tdehome, 1024 );
    strlcat( filename, "/share/config/startupconfigkeys", 1024 );
    FILE* keys = fopen( filename, "r" );
    if( keys == NULL )
        {
        fclose( config );
        return 2;
        }
    bool need_update = true;
    for(;;)
        {
        char keyline[ 1024 ];
        if( fgets( keyline, 1023, keys ) == NULL )
            {
            need_update = false;
            break;
            }
        if( char* nl = strchr( keyline, '\n' ))
            *nl = '\0';
        char line[ 1024 ];
        if( fgets( line, 1023, config ) == NULL )
            break;
        if( char* nl = strchr( line, '\n' ))
            *nl = '\0';
        if( strcmp( keyline, line ) != 0 )
            break;
        bool ok = false;
        for(;;)
            {
            if( fgets( line, 1023, config ) == NULL )
                break;
            if( char* nl = strchr( line, '\n' ))
                *nl = '\0';
            if( *line == '\0' )
                break;
            if( *line == '*' )
                {
                ok = true;
                break;
                }
            if( *line == '!' )
                {
                if( access( line + 1, R_OK ) == 0 )
                    break; // file now exists -> update
                }
            else
                {
                struct stat st;
                if( stat( line, &st ) != 0 )
                    break;
                if( st.st_mtime > config_time )
                    break;
                }
            }
        if( !ok )
            break;
        }
    fclose( keys );
    fclose( config );
    if( need_update )
        {
        int ret = system( "tdedostartupconfig" );
        return WEXITSTATUS( ret );
        }
    return 0;
    }