1/*
2 * cattcl.c --
3 *    A crude version of cat used in the build to concatenate Tcl source
4 * files into a library.
5 *-----------------------------------------------------------------------------
6 * Copyright 1996-1999 Karl Lehenbauer and Mark Diekhans.
7 *
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted, provided
10 * that the above copyright notice appear in all copies.  Karl Lehenbauer and
11 * Mark Diekhans make no representations about the suitability of this
12 * software for any purpose.  It is provided "as is" without express or
13 * implied warranty.
14 *-----------------------------------------------------------------------------
15 * $Id: cattcl.c,v 1.1 2001/10/24 23:31:50 hobbs Exp $
16 *-----------------------------------------------------------------------------
17 */
18
19#include <stdio.h>
20#include <windows.h>
21
22
23/*-----------------------------------------------------------------------------
24 * TclX_SplitWinCmdLine --
25 *   Parse the window command line into arguments.
26 *
27 * Parameters:
28 *   o argcPtr (O) - Count of arguments is returned here.
29 *   o argvPtr (O) - Argument vector is returned here.
30 * Notes:
31 *   This code taken from the Tcl file tclAppInit.c: Copyright (c) 1996 by
32 * Sun Microsystems, Inc.
33 *-----------------------------------------------------------------------------
34 */
35void
36TclX_SplitWinCmdLine (argcPtr, argvPtr)
37    int    *argcPtr;
38    char ***argvPtr;
39{
40    char   *args = GetCommandLine ();
41    char **argvlist, *p;
42    int size, i;
43
44    /*
45     * Precompute an overly pessimistic guess at the number of arguments
46     * in the command line by counting non-space spans.
47     */
48    for (size = 2, p = args; *p != '\0'; p++) {
49        if (isspace (*p)) {
50            size++;
51            while (isspace (*p)) {
52                p++;
53            }
54            if (*p == '\0') {
55                break;
56            }
57        }
58    }
59    argvlist = (char **) malloc ((unsigned) (size * sizeof (char *)));
60    *argvPtr = argvlist;
61
62    /*
63     * Parse the Windows command line string.  If an argument begins with a
64     * double quote, then spaces are considered part of the argument until the
65     * next double quote.  The argument terminates at the second quote.  Note
66     * that this is different from the usual Unix semantics.
67     */
68    for (i = 0, p = args; *p != '\0'; i++) {
69        while (isspace (*p)) {
70            p++;
71        }
72        if (*p == '\0') {
73            break;
74        }
75        if (*p == '"') {
76            p++;
77            (*argvPtr) [i] = p;
78            while ((*p != '\0') && (*p != '"')) {
79                p++;
80            }
81        } else {
82            (*argvPtr) [i] = p;
83            while (*p != '\0' && !isspace(*p)) {
84                p++;
85            }
86        }
87        if (*p != '\0') {
88            *p = '\0';
89            p++;
90        }
91    }
92    (*argvPtr) [i] = NULL;
93    *argcPtr = i;
94}
95
96/*
97 * Concatenate a bunch of files.
98 */
99int
100main (int    argc,
101      char **argv)
102{
103    FILE *fh;
104    int idx, c;
105
106    TclX_SplitWinCmdLine (&argc, &argv);
107
108
109    for (idx = 1; idx < argc; idx++) {
110        fh = fopen (argv [idx], "r");
111        if (fh == NULL) {
112            fprintf (stderr, "error opening \"%s\": %s\n",
113                     argv [idx], strerror (errno));
114            exit (1);
115        }
116        while ((c = fgetc (fh)) != EOF) {
117            if (fputc (c, stdout) == EOF) {
118                fprintf (stderr, "error writing stdout: %s\n",
119                         strerror (errno));
120                exit (1);
121            }
122        }
123        if (ferror (fh)) {
124            fprintf (stderr, "error reading \"%s\": %s\n",
125                     argv [idx], strerror (errno));
126            exit (1);
127        }
128        fclose (fh);
129    }
130    return 0;
131}
132
133
134