This file is indexed.

/usr/bin/icmstart is in icmake 7.21.01-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/icmake -qt /tmp/icmstart

string g_confpath;
string g_home;
string g_userHome;
string g_skelpath = "/usr/share/icmake";
string g_program;
string g_defaultcommand = "";
string g_defaultcommandArg;
string g_destpath;
string g_cwd;
list   g_mkdir;

int g_confirminstall = 0;
int g_skeletons = 1;
int g_replace = 0;
int g_debug = 0;
int g_askreplace = 1;

void usage()
{
    printf("Usage: ", g_program, " [Options] dir [program|library]\n"
        "Where:\n"
        "   Options:\n"
        "     -c confpath: Use the configuration files (icmstart.rc, "
                                                                "AUTHOR,\n"
        "                  VERSION, YEARS found in `confpath' rather than\n"
        "                  if found in $HOME/.icmake or /etc/icmake\n"
        "     -d: debug: do not execute any commands, but show commands\n"
        "         that would have been executed\n"
        "     -I: do NOT install any files.\n"
        "     -r: replace existing files/directories\n"
        "     -s skelpath: Read the skeleton information from the directory\n"
        "                  `skelpath' rather than /usr/share/icmake\n"
        "   dir: the directory to install the files into\n"
        "   program, library: command passed by default to the icmbuild "
                                                                    "script\n"
        "\n");

    exit(1);
}

void die(string s)
{
    printf(g_program, ": ", s, "\n");
    exit(1);
}

int isFileOrDir(string s)
{
    return (int)stat(P_NOCHECK, s)[0] & (S_IFREG | S_IFDIR);
}

int isFile(string s)
{
    return (int)stat(P_NOCHECK, s)[0] & S_IFREG;
}

void syscall(string command)
{
    if (g_debug)
        printf(command, "\n");
    else
        system(command);
}

void md(string dir)
{
    int idx;
    for (idx = 0; idx < sizeof(g_mkdir); ++idx)
    {
        if (g_mkdir[idx] == dir)
            return;
    }
    syscall("mkdir -p " + dir);    
    g_mkdir += (list)dir;
}

string absPath(string arg)
{
    if (arg[0] != "/")
        arg = g_cwd + arg;
    if (arg[strlen(arg) - 1] != "/")
        arg += "/";
    return arg;
}

void arguments(int argc, list argv)
{
    int cmdidx = 1;
    string arg;
    list icm;
    int length;

    icm = getenv("ICM");                // ICM environment var defined?

    if ((int)icm[0] == 1)                
        g_skelpath = icm[1];            // then re-assign skelpath

    while (cmdidx < argc)
    {
        arg = argv[cmdidx];

        if (arg[0] != "-")              // no (more) options
            break;

        if (arg[1] == "c")              // -c: re-assign g_confpath
        {
            arg = substr(arg, 2, 999);  // get all beyond -c

            if (arg == "")              // or get then next argument
            {
                if (cmdidx == argc)
                    die("-c lacks configuration file specification");

                arg = argv[++cmdidx];
            }
            g_confpath = absPath(arg);  // reassign confpath
        }
        else if (arg[1] == "d")         // -d: debug
            g_debug = 1;
        else if (arg[1] == "I")         // -I: no skeletons
            g_skeletons = 0;
        else if (arg[1] == "r")         // -r: replace existing file(s)
        {
            g_askreplace = 0;
            g_replace = 1;
        }
        else if (arg[1] == "s")         // -s: re-assign g_skelpath
        {
            arg = substr(arg, 2, 999);  // get all beyond -s

            if (arg == "")              // or get then next argument
            {
                if (cmdidx == argc)
                    die("-s lacks skeleton dir specification");

                arg = argv[++cmdidx];
            }
            g_skelpath = arg;           // reassign skelpath
        }
        else
            printf("[Warning] ignoring unkown option ", arg, "\n");

        ++cmdidx;
    }

    g_skelpath = absPath(g_skelpath);
    g_destpath = argv[cmdidx];
    if (g_destpath[strlen(g_destpath) - 1] != "/")
        g_destpath += "/";
    md(g_destpath);                       // install the target dir

    if (!g_debug)
        g_destpath = chdir(g_destpath);

    if (++cmdidx < argc)
    {
        g_defaultcommandArg = argv[cmdidx];
        if 
        (
            g_defaultcommandArg == "program" 
            || 
            g_defaultcommandArg == "library"
        )
            g_defaultcommand = 
                        "#define DEFCOM \"" + g_defaultcommandArg + "\"\n";
        else
            printf("Ignored initial command `", g_defaultcommandArg, 
                                                        "' for icmbuild\n");
    }
}

int replace(string target)
{
    string ret;

    while (g_askreplace)
    {
        printf("`", target, "' exists.\n"
               "Replace [?akNy] ? ");
        ret = getch();

        if (ret == "a")
        {
            g_replace = 1;
            g_askreplace = 0;
            break;
        }

        if (ret == "k")
        {
            g_askreplace = 0;
            break;
        }

        if (ret == "y")
            return 1;

        if (ret == "n" || ret == "\n")
            return 0;

        printf("Press `a' to replace all,\n"
               "      `k' to keep all current versions\n"
               "      `n' to keep current version (default)\n"
               "      `?' shows this help\n");
    }
    return g_replace;
}

int install(string target)
{
    return (int)(!exists(target) || replace(target));
}

int install_dest(string source, string dest)
{
    int idx;

    if (!isFileOrDir(source))
        die("Can't find `" + source + "'\n");

    if (dest == "")
        dest = change_path(source, g_destpath);
    else    
        dest = g_destpath + dest;

    idx = strlen(dest) - 1;

    if (dest[idx] == "/")
        dest = substr(dest, 0, idx);

    if (install(dest))
    {
        md(get_path(dest));
        syscall("cp -rd " + source + " " + dest);
        return 1;
    }
    return 0;
}

    // a line in the conffile may be organized as follows (except for empty
    // lines and comment lines starting at # in column 0)
    // name is either a file or a directory; directories are copied
    // completely. 
    //  name                - skelpath/name is installed as destpath/name
    //  ./path/name         - $cwd/path/name is installed as destpath/name
    //  /path/name          - /path/name is installed as destpath/name
    // a second entry may be dest (path OK): destpath/dest will be the
    // destination. E.g.:
    //  name  one/two       - skelpath/name is installed as destpath/one/two
    // All lines may start with a P (+space) or L (+space).
    // A source at a P-line is not installed when using 'icmstart xxx library'
    // A source at an L-line is not installed when using 'icmstart xxx
    // program' If omitted the source is installed unconditionally.
    // Following P or L (+space) an optional ? (+ space) may be specificed 
    // in which case the installation must be confirmed by the user
int install_file(string source, string dest)
{
    int idx;
    string rawDest;
    
    if (source[0] == ".")
        source = g_cwd + source;
    else
    {
        idx = strfind(source, "/");

        if (idx == -1)
            source = g_skelpath + source;
        else if (idx > 0)
            source = g_cwd + source;
    }
    return install_dest(source, dest);
}

string find(string path, string file)
{
    string ret;

    ret = path + "/" + file;
    if (exists(ret))
        return ret;

    return "";
}

string findFile(string file)
{
    string ret;

    if (g_confpath != "")                   // locate file in -c path
        ret = find(g_confpath, file);

    if (ret == "")                          // not found, locate in $HOME
        ret = find(g_home, file);

    if (ret == "")                          // not found, locate in CONFDIR
        ret = find("/etc/icmake", file);

    return ret;
}

void install_conf(string file, string default)
{
    string ret;
    ret = findFile(file);

    if (g_skeletons)
    {
        if (ret != "")
            system("cat " + ret + " >> " + g_destpath + "VERSION");
        else
            fprintf(g_destpath + "VERSION", 
                        "#define ", file, " \"", default, "\"\n");
    }
}    

void install_version()
{
    string str;
    str = g_destpath + "VERSION";

    if (install(str))
    {
        system(P_NOCHECK, "rm -f " + g_destpath + "VERSION");
        install_conf("AUTHOR",  "");
        install_conf("VERSION", "0.00.00");

        str = "date '+%Y'";
        str = `str`[0];
        install_conf("YEARS", substr(str, 0, strlen(str) - 1));
    }
}

void install_std()
{
    install_version();
                                        // install a default command if 
                                        // provided as last arg.
    if (install_file("icmconf", "") && g_defaultcommand != "")
        fprintf(g_destpath + "icmconf", g_defaultcommand);
}

int confirminstall(string source, string dest)
{
    if (g_confirminstall)
    {
        if (source == dest)
            printf("Install `", dest, "' [yN] ? ");
        else
            printf("Install `", source, "' as `", dest, "' [yN] ? ");
        return getch() == "y";
    }

    return 1;
}

string check_home(string source)
{
    if (source[0] != "~")
        return source;

    return g_userHome + substr(source, 1, strlen(source));
}

list shift(list source)     // shift away the first element
{
    list ret;
    int idx;

    for (idx = 1; idx != sizeof(source); ++idx)
        ret += (list)source[idx];

    return ret;
}

    
    // a line in the conffile may be organized as follows (name is either
    //  a file or a directory; directories are copied completely):
    //  name        - name is located in skelpath and installed at destpath
    //  name  dest  - name is located in skelpath and installed at destpath/name
    //                if name is a directory then the destination will be
    //                destpath/dest/name. dest may also be /dest
    //  path/name   - relative or absolute path's name will be installed at
    //                destpath. Relative is relative to the startup directory
    //  path/name dest - relative or absolute path's name will be installed
    //                at destpath/dest. dest may also be /dest
    // All lines may start with a P (+space) or L (+space).
    // A source at a P-line is not installed when using 'icmstart xxx library'
    // A source at an L-line is not installed when using 'icmstart xxx
    // program' If omitted the source is installed unconditionally.
    // Following P or L (+space) an optional ? (+ space) may be specificed 
    // in which case the installation must be confirmed by the user

void install_line(string confline)
{
    int idx;
    list fields;
    string source;
    string dest;
    string plFlag;
    string install;

    fields = strtok(confline, " \t\n");
    source = fields[0];

                                            // empty line or comment
    if (sizeof(fields) == 0 || source[0] == "#")
        return;

    install = source;
    if (install == "P" || install == "L")
    {
        fields = shift(fields);             // rm the P/L-flag
        source = fields[0];
    }
    else
        install = "";

    g_confirminstall = source == "?";       // need confirmation ?

    if (g_confirminstall)
    {
        fields = shift(fields);             // rm the ?-mark
        source = fields[0];
    }

    if 
    (
        g_defaultcommandArg == "library" && install == "P"
        ||
        g_defaultcommandArg == "program" && install == "L"
    )
        return;                             // no P with libs, no L with progs


    if (sizeof(fields) > 1)
        dest = fields[1];
    else
        dest = source;

    source = check_home(source);
    dest   = check_home(dest);

    if (!g_replace && !g_askreplace && exists(g_destpath + dest))
        return;

    if (!confirminstall(source, dest))
            return;

    install_file(source, dest);
}

void install_rc()
{
    list line;
    string conffile;

    conffile = findFile("icmstart.rc");

    if (!isFile(conffile))
        die("Can't find configuration file `icmstart.rc'");
    
    while (1)
    {
        line = fgets(conffile, (int)line[1]);
        if (!line)
            break;
        install_line(line[0]);
    }
}

int main(int argc, list argv)
{
    echo(OFF);

    g_program = get_base(argv[0]);
    g_userHome = getenv("HOME")[1];
    g_home = g_userHome + "/.icmake";

    if (argc == 1)
        usage();

    g_cwd = chdir("");

    arguments(argc, argv);

    install_std();              // install CLASSES VERSION and icmconf
    install_rc();               // install .rc file elements

    printf("Done. Don't forget to inspect the #defines in\n"
            "'", g_destpath, "icmconf'\n");

    return 0;
}