summaryrefslogtreecommitdiffstats
path: root/src/altparser.cpp
blob: 29718bfc50f561af9e5cafa545c54ddf3d24267d (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/***************************************************************************
 *   Copyright (C) 2004 by Juanjo Álvarez                                  *
 *   juanjux@yahoo.es                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02111-1307, USA.          *
 ***************************************************************************/

#include "altparser.h"
#include <tqfileinfo.h>
#include <tqfile.h>
#include <tqdir.h>
#include <tqstringlist.h>

#include <unistd.h>
#include <string.h>
#include <errno.h>

Alternative::Alternative(Item *parentarg) : m_parent(parentarg)
{
    m_priority = 1;
    m_altSlaves = new TQStringList;
}

// Copy constructor
Alternative::Alternative(const Alternative &alt) :
    m_altPath(alt.getPath()),
    m_priority(alt.getPriority()),
    m_parent(alt.getParent())
{
    m_altSlaves = new TQStringList( *(alt.m_altSlaves) );
}

Alternative::~Alternative()
{
    if(m_altSlaves)delete m_altSlaves;
}

Alternative& Alternative::operator=(const Alternative &alt)
{
    if(this != &alt)
    {
        if(m_altSlaves)delete m_altSlaves;
        m_altPath = alt.getPath();
        m_priority = alt.getPriority();
        m_parent = alt.getParent();
        m_altSlaves = new TQStringList( *(alt.m_altSlaves) );
    }
    return (*this);
}

void Alternative::setSlaves(TQStringList *slaves)
{
    if(m_altSlaves) delete m_altSlaves;
    m_altSlaves = slaves;
}

bool Alternative::isSelected() const
{
    if(m_parent->isBroken()) return false;
    TQFileInfo file("/etc/alternatives/"+m_parent->getName());
    if(!file.isSymLink()) return false;
    if(file.readLink() == m_altPath) return 1;
    return 0;
}


bool Alternative::isBroken() const
{
    return !TQFile::exists(m_altPath);
}

bool Alternative::select()
{
    if(isSelected()) return true;
    if(isBroken())
    {
        m_selectError = TQString("Broken alternative: Unexisting path %1").arg(m_altPath);
        return false;
    }

    // Remove the current link:
    TQString parentPath = TQString("/etc/alternatives/%1").arg(m_parent->getName());
    TQFile origlink(parentPath);
    if(!origlink.remove())
    {
        m_selectError = TQString("Could not delete alternative link %1: %2").arg(parentPath).arg(origlink.errorString());
        return false;
    }

    // Then we do the main link:
    if(symlink(m_altPath.ascii(), parentPath.ascii()) == -1)
    {
        m_selectError = TQString(strerror(errno));
        return false;
    }

    // And finally the slaves
    SlaveList *parslaves = m_parent->getSlaves();
    parslaves->setAutoDelete(1);
    if(parslaves->count() == 0 || m_altSlaves->count() == 0) return true;
    int count = 0;
    TQStringList::iterator sl;
    Slave *parsl;
    for( sl = m_altSlaves->begin(); sl != m_altSlaves->end(); ++sl)
    {
        parsl = parslaves->at(count);
        TQString parstr = TQString("/etc/alternatives/%1").arg(parsl->slname);
        TQFile parlink(parstr);
        if(!parlink.remove())
        {
            m_selectError = TQString("Could not delete slave alternative link %1: %2").arg(parstr).arg(parlink.errorString());
            return false;
        }
        if(symlink( (*sl).ascii(), parstr.ascii()) == -1)
        {
            m_selectError = TQString(strerror(errno));
            return false;
        }
        ++count;
    }
    return true;
}


//*************************************** Item

Item::Item()
{
    m_mode = "auto";
    m_itemSlaves = new SlaveList;
    m_itemAlts = new AltsPtrList;
    m_itemSlaves->setAutoDelete(1);
    m_itemAlts->setAutoDelete(1);
}

// Deep copy
Item::Item(const Item &item) :
    m_name(item.m_name),
    m_mode(item.m_mode),
    m_path(item.m_path)
{
    m_itemSlaves = new SlaveList;
    m_itemAlts = new AltsPtrList;
    m_itemSlaves->setAutoDelete(1);
    m_itemAlts->setAutoDelete(1);
    Slave *slave;
    Slave *slavecopy;
    for(slave = item.m_itemSlaves->first(); slave; slave = item.m_itemSlaves->next())
    {
        slavecopy = new Slave;
        slavecopy->slname = slave->slname;
        slavecopy->slpath = slave->slpath;
        m_itemSlaves->append(slavecopy);
    }

    Alternative *alt;
    Alternative *altcopy;
    for(alt = item.m_itemAlts->first(); alt; alt = item.m_itemAlts->next())
    {
        // The Alternative class already has a deep copy constructor:
        altcopy = new Alternative( (*alt) );
        m_itemAlts->append(altcopy);
    }
}

Item& Item::operator=(const Item &item)
{
    if(this != &item)
    {
        if(m_itemSlaves)delete m_itemSlaves;
        if(m_itemAlts)delete m_itemAlts;
        m_name = item.m_name;
        m_mode = item.m_mode;
        m_path = item.m_path;
        m_itemSlaves = new SlaveList;
        m_itemAlts = new AltsPtrList;
        m_itemSlaves->setAutoDelete(1);
        m_itemAlts->setAutoDelete(1);
        Slave *slave;
        Slave *slavecopy;
        for(slave = item.m_itemSlaves->first(); slave; slave = item.m_itemSlaves->next())
        {
            slavecopy = new Slave;
            slavecopy->slname = slave->slname;
            slavecopy->slpath = slave->slpath;
            m_itemSlaves->append(slavecopy);
        }

        Alternative *alt;
        Alternative *altcopy;
        for(alt = item.m_itemAlts->first(); alt; alt = item.m_itemAlts->next())
        {
            altcopy = new Alternative( (*alt) );
            m_itemAlts->append(altcopy);
        }
    }
    return (*this);
}



Item::~Item()
{
    if(m_itemSlaves)delete m_itemSlaves;
    if(m_itemAlts)delete m_itemAlts;
}

Alternative *Item::getSelected() const
{
    Alternative *a;
    for(a = m_itemAlts->first(); a; a = m_itemAlts->next())
    {
        if(a->isSelected())
        {
            return a;
            break;
        }
    }
    return NULL;
}

void Item::setSlaves(SlaveList *slaves)
{
    if(this->m_itemSlaves)delete this->m_itemSlaves;
    this->m_itemSlaves = slaves;
}

void Item::addSlave(const TQString &namearg, const TQString &patharg)
{
    Slave *s = new Slave;
    s->slname = namearg;
    s->slpath = patharg;
    m_itemSlaves->append(s);
}

void Item::delSlave(const TQString &namearg)
{
    TQPtrListIterator<Slave> it(*m_itemSlaves);

    Slave *s;
    while( (s = it.current()) != 0)
    {
        ++it;
        if(s->slname == namearg)
        {
            m_itemSlaves->remove(s);
            break;
        }
    }
}
void Item::delSlaveByPath(const TQString &patharg)
{
    TQPtrListIterator<Slave> it(*m_itemSlaves);

    Slave *s;
    while( (s = it.current()) != 0)
    {
        ++it;
        if(s->slpath == patharg)
        {
            m_itemSlaves->remove(s);
            break;
        }
    }
}

Alternative *Item::getAlternative(const TQString &altpath)
{
    Alternative *a;
    for(a = m_itemAlts->first(); a; a = m_itemAlts->next())
    {
        if(a->getPath() == altpath)
        {
            return a;
            break;
        }
    }
    return NULL;
}

void Item::setAlternatives(AltsPtrList &alts)
{
    if(this->m_itemAlts)delete this->m_itemAlts;
    this->m_itemAlts = &alts;
}

void Item::delAlternativeByPath(const TQString &patharg)
{
    TQPtrListIterator<Alternative> it(*m_itemAlts);

    Alternative *a;
    while( (a = it.current()) != 0)
    {
        ++it;
        if(a->getPath() == patharg)
        {
            m_itemAlts->remove(a);
            break;
        }
    }
}

void Item::delAlternativeByPriority(int priorityarg)
{
    TQPtrListIterator<Alternative> it(*m_itemAlts);

    Alternative *a;
    while( (a = it.current()) != 0)
    {
        ++it;
        if(a->getPriority() == priorityarg)
        {
            m_itemAlts->remove(a);
            break;
        }
    }
}

bool Item::isBroken() const
{
    return !TQFile::exists(m_path);
}

/********************** AltFIlesManager ************/

AltFilesManager::AltFilesManager(const TQString &altdirarg) :
    m_altdir(altdirarg)
{
    m_itemlist = new ItemPtrList;
    m_itemlist->setAutoDelete(1);
    m_parseOk = true;
    m_errorMsg = "";
    if(!parseAltFiles(m_errorMsg))
    {
        m_parseOk = false;
    }
    //debugPrintAlts();
}

AltFilesManager::~AltFilesManager()
{
    //delete m_itemlist;
    /*
    Item *item;
    for(item = m_itemlist->first(); item; item = m_itemlist->next())
    {
        delete item;
    }

    delete m_itemlist;
    */
}

Item* AltFilesManager::getItem(const TQString &name) const
{
    TQPtrListIterator<Item> it(*m_itemlist);
    Item *i;
    while( (i = it.current()) !=  0)
    {
        ++it;
        if(i->getName() == name)
        {
            return i;
            break;
        }
    }
    return NULL;
}

bool AltFilesManager::parseAltFiles(TQString &errorstr)
{
    TQDir d(m_altdir);
    TQStringList fileList = d.entryList();
    TQStringList lines;
    TQFile altFile;
    TQString line, tmp;
    int nslaves;
    unsigned int index, slavesend;

    for( TQStringList::Iterator it = fileList.begin(); it != fileList.end(); ++it)
    {
        Item *item = new Item;
        if(*it == "." || *it == "..") {
            delete item;
            continue;
        }

        item->setName(*it);
        altFile.setName(m_altdir+"/"+*it);

        if(!altFile.open( IO_ReadOnly ))
        {
            errorstr = altFile.errorString();
            delete item;
            return false;
        }

        lines.clear();
        while ( !altFile.atEnd() )
        {
            if(!altFile.readLine(line, 9999))
            {
                errorstr = altFile.errorString();
                delete item;
                return false;
            }
            lines.append(line);
        }

        line = lines[0];
        tmp = line.left(line.length()-1);
        item->setMode(tmp);

        line = lines[1];
        tmp = line.left(line.length()-1);
        item->setPath(tmp);

        index = 2;
        line = lines[index];
        nslaves = 0;
        SlaveList *slaves = new SlaveList;
        slaves->setAutoDelete(1);

        while(line != "\n")
        {
            tmp = line.left(line.length()-1);
            Slave *slave = new Slave;
            nslaves++;
            slave->slname = tmp;

            line = lines[++index];
            tmp = line.left(line.length()-1);
            slave->slpath = tmp;

            slaves->append(slave);
            line = lines[++index];
        }

        item->setSlaves(slaves);

        ++index;
        while(index < lines.count()-1)
        {
            line = lines[index];
            Alternative *a = new Alternative(item);
            tmp = line.left(line.length()-1);
            a->setPath(tmp);

            if(line=="\n") {
                //File end (with a \n)
                delete a;
                break;
            }

            if(++index == lines.count())
            {
                item->addAlternative(a);
                break;
            }


            line = lines[index];
            tmp = line.left(line.length()-1);
            a->setPriority(tmp.toInt());

            if(++index == lines.count())
            {
                item->addAlternative(a);
                break;
            }

            line = lines[index];
            if(line != "\n" and nslaves > 0)
            {
                slavesend = index+nslaves;
                while(index < slavesend)
                {
                    line = lines[index];
                    tmp = line.left(line.length()-1);
                    a->addSlave(tmp);
                    ++index;
                }
            }
			else
			{
				if (nslaves >0)
				{
					++index;
				}
			}
            item->addAlternative(a);
        }
        m_itemlist->append(item);
        altFile.close();
    }

    return true;
}

//FIXME: This must be in a son of qptrlist!
/*
int AltFilesManager::compareItems(Item i1, Item i2)
{
    return i1.getPath().compare(i2.getPath());
}
*/

/*
void AltFilesManager::debugPrintAlts() const
{
    printf("----------------------------------\n");
    Item *item;
    for(item = m_itemlist->first(); item; item = m_itemlist->next())
    {
        printf("\nItem: %s\n", item->getName().ascii());
        printf("\tMode: %s\n", item->getMode().ascii());
        printf("\tPath: %s\n", item->getPath().ascii());
        if(item->getSlaves()->count() == 0)
            printf("\tNo slaves\n");
        else
        {
            Slave *slave;
            SlaveList *slaves = item->getSlaves();
            for(slave = slaves->first(); slave; slave = slaves->next())
            {
                printf("\tSlave name: %s\n", slave->name.ascii());
                printf("\tSlave path: %s\n", slave->path.ascii());
            }
        }
        printf("\tAlternatives:\n");
        if(item->getAlternatives()->count() == 0)
            printf("\t\tNO ALTERNATIVES!");
        else
        {
            Alternative *a;
            AltsPtrList *alts = item->getAlternatives();
            for(a = alts->first(); a; a = alts->next())
            {
                printf("\t\tPath: %s\n", a->getPath().ascii());
                printf("\t\tPriority: %d\n", a->getPriority());
                printf("\t\tSlaves:\n");
                if(a->getSlaves()->count() == 0)
                    printf("\t\t\tNo slaves\n");
                else
                {
                    TQStringList altslaves = *(a->getSlaves());
                    TQStringList::iterator sl;
                    for( sl = altslaves.begin(); sl != altslaves.end(); ++sl)
                    {
                        printf("\t\t\t%s\n", (*sl).ascii());
                    }
                }
            }
        }
    }
}
*/
/*
// ************************************** Test
int main(int argc, char **argv)
{
    AltFilesManager a("/var/lib/rpm/alternatives");
    if(!a.parsingOk())
        printf("ERROR PARSING ALT FILES: %s\n", a.getErrorMsg().ascii());
    else
        printf("\nOK, Finished parsing\n");

    Item *item= a.getItem("vi");
    if(item == NULL) return 0;
    printf("Nombre item: %s\n", item->getName().ascii());
    printf("Path item: %s\n", item->getPath().ascii());
    Alternative *alt = item->getSelected();
    if(alt == NULL) return 0;
    printf("Selected alt: %s\n", alt->getPath().ascii());

    Alternative *vimminimal = item->getAlternative("/bin/vim-minimal");
    if(vimminimal == NULL) { printf("NULL!\n"); return 0; }
    printf("Not selected alt: %s\n", vimminimal->getPath().ascii());

    printf("Selecting vim-minimal instead of vim-enhanced as vi\n");
    if(!vimminimal->select())
    {
        printf("ERROR: %s\n", vimminimal->getSelectError().ascii());
    }

    printf("Now selecting vim-enhanced...\n");
    Alternative *vimen = item->getAlternative("/usr/bin/vim-enhanced");
    if(vimen == NULL) { printf("NULL!\n"); return 0; }
    if(!vimen->select())
    {
        printf("ERROR: %s\n", alt->getSelectError().ascii());
    }
    return 0;
  }
*/