source: trunk/tools/neercs/old/mygetopt.c @ 1642

Last change on this file since 1642 was 1642, checked in by sam, 8 years ago

neercs: import files from the (unfinished) old neercs code.

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  This program is free software. It comes without any warranty, to
8 *  the extent permitted by applicable law. You can redistribute it
9 *  and/or modify it under the terms of the Do What The Fuck You Want
10 *  To Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  mygetopt.c: getopt_long reimplementation
16 */
17
18#include "config.h"
19
20#include <stdio.h>
21#include <string.h>
22
23#include "caca_types.h"
24
25#include "mygetopt.h"
26
27int myoptind = 1;
28char *myoptarg = NULL;
29
30/* XXX: this getopt_long implementation should not be trusted for other
31   applications without any serious peer reviewing. It “just works” with
32   zzuf but may fail miserably in other programs. */
33int mygetopt(int argc, char *const _argv[], const char *optstring,
34             const struct myoption *longopts, int *longindex)
35{
36    char **argv = (char **)(uintptr_t) _argv;
37    char *flag;
38    int i;
39
40    if (myoptind >= argc)
41        return -1;
42
43    flag = argv[myoptind];
44
45    if (flag[0] == '-' && flag[1] != '-')
46    {
47        char *tmp;
48        int ret = flag[1];
49
50        if (ret == '\0')
51            return -1;
52
53        tmp = strchr(optstring, ret);
54        if (!tmp || ret == ':')
55            return '?';
56
57        myoptind++;
58        if (tmp[1] == ':')
59        {
60            if (flag[2] != '\0')
61                myoptarg = flag + 2;
62            else if (myoptind >= argc)
63            {
64                if (tmp[2] != ':')
65                {
66                    fprintf(stderr, "%s: `%s' needs an argument\n", argv[0],
67                            flag);
68                    return -2;
69                }
70            }
71            else
72                myoptarg = argv[myoptind++];
73            return ret;
74        }
75
76        if (flag[2] != '\0')
77        {
78            flag[1] = '-';
79            myoptind--;
80            argv[myoptind]++;
81        }
82
83        return ret;
84    }
85
86    if (flag[0] == '-' && flag[1] == '-')
87    {
88        if (flag[2] == '\0')
89            return -1;
90
91        for (i = 0; longopts[i].name; i++)
92        {
93            size_t l = strlen(longopts[i].name);
94
95            if (strncmp(flag + 2, longopts[i].name, l))
96                continue;
97
98            switch (flag[2 + l])
99            {
100            case '=':
101                if (!longopts[i].has_arg)
102                    goto bad_opt;
103                if (longindex)
104                    *longindex = i;
105                myoptind++;
106                myoptarg = flag + 2 + l + 1;
107                return longopts[i].val;
108            case '\0':
109                if (longindex)
110                    *longindex = i;
111                myoptind++;
112                if (longopts[i].has_arg)
113                    myoptarg = argv[myoptind++];
114                return longopts[i].val;
115            default:
116                break;
117            }
118        }
119      bad_opt:
120        fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag);
121        return '?';
122    }
123
124    return -1;
125}
Note: See TracBrowser for help on using the repository browser.