Skip to content

Commit b1f3a91

Browse files
committed
@- Added CLI (command line intrerface) sapi based on a cut-down version
@ of the CGI sapi which is more suited for writing shell scripts. Some of @ the differences are: it prints no HTTP headers, displays plain text @ error messages, etc. (Edin) Added CLI (command line intrerface) sapi. # Let's see if a build expert can make this compile along # side some other sapi.
1 parent 1788410 commit b1f3a91

6 files changed

Lines changed: 830 additions & 0 deletions

File tree

‎sapi/cli/CREDITS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CLI
2+
Rasmus Lerdorf, Stig Bakken, Edin Kadribasic

‎sapi/cli/Makefile.in‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
LTLIBRARY_NAME = libsapi.la
3+
LTLIBRARY_SOURCES = php_cli.c getopt.c
4+
5+
include $(top_srcdir)/build/ltlib.mk

‎sapi/cli/config.m4‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
dnl
2+
dnl $Id$
3+
dnl
4+
5+
AC_MSG_CHECKING(for CLI build)
6+
AC_ARG_WITH(cli,
7+
[ --with-cli Build PHP as CLI application],[
8+
if test "$withval" = "yes"; then
9+
CLIPATH=/usr/local
10+
else
11+
CLIPATH=$withval
12+
fi
13+
PHP_SAPI=cli
14+
PHP_PROGRAM=php
15+
INSTALL_IT="\$(INSTALL) -m 0755 $SAPI_PROGRAM \$(bindir)/$PHP_PROGRAM"
16+
RESULT=yes
17+
PHP_SUBST(EXT_PROGRAM_LDADD)
18+
],[
19+
RESULT=no
20+
])
21+
AC_MSG_RESULT($RESULT)

‎sapi/cli/getopt.c‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/* Borrowed from Apache NT Port */
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <assert.h>
6+
#include <stdlib.h>
7+
#include "php_getopt.h"
8+
#define OPTERRCOLON (1)
9+
#define OPTERRNF (2)
10+
#define OPTERRARG (3)
11+
12+
13+
char *ap_php_optarg;
14+
int ap_php_optind = 1;
15+
static int ap_php_opterr = 1;
16+
static int ap_php_optopt;
17+
18+
static int
19+
ap_php_optiserr(int argc, char * const *argv, int oint, const char *optstr,
20+
int optchr, int err)
21+
{
22+
if (ap_php_opterr)
23+
{
24+
fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
25+
switch(err)
26+
{
27+
case OPTERRCOLON:
28+
fprintf(stderr, ": in flags\n");
29+
break;
30+
case OPTERRNF:
31+
fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
32+
break;
33+
case OPTERRARG:
34+
fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
35+
break;
36+
default:
37+
fprintf(stderr, "unknown\n");
38+
break;
39+
}
40+
}
41+
ap_php_optopt = argv[oint][optchr];
42+
return('?');
43+
}
44+
45+
int ap_php_getopt(int argc, char* const *argv, const char *optstr)
46+
{
47+
static int optchr = 0;
48+
static int dash = 0; /* have already seen the - */
49+
50+
char *cp;
51+
52+
if (ap_php_optind >= argc)
53+
return(EOF);
54+
if (!dash && (argv[ap_php_optind][0] != '-'))
55+
return(EOF);
56+
if (!dash && (argv[ap_php_optind][0] == '-') && !argv[ap_php_optind][1])
57+
{
58+
/*
59+
* use to specify stdin. Need to let pgm process this and
60+
* the following args
61+
*/
62+
return(EOF);
63+
}
64+
if ((argv[ap_php_optind][0] == '-') && (argv[ap_php_optind][1] == '-'))
65+
{
66+
/* -- indicates end of args */
67+
ap_php_optind++;
68+
return(EOF);
69+
}
70+
if (!dash)
71+
{
72+
assert((argv[ap_php_optind][0] == '-') && argv[ap_php_optind][1]);
73+
dash = 1;
74+
optchr = 1;
75+
}
76+
77+
/* Check if the guy tries to do a -: kind of flag */
78+
assert(dash);
79+
if (argv[ap_php_optind][optchr] == ':')
80+
{
81+
dash = 0;
82+
ap_php_optind++;
83+
return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRCOLON));
84+
}
85+
if (!(cp = strchr(optstr, argv[ap_php_optind][optchr])))
86+
{
87+
int errind = ap_php_optind;
88+
int errchr = optchr;
89+
90+
if (!argv[ap_php_optind][optchr+1])
91+
{
92+
dash = 0;
93+
ap_php_optind++;
94+
}
95+
else
96+
optchr++;
97+
return(ap_php_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF));
98+
}
99+
if (cp[1] == ':')
100+
{
101+
/* Check for cases where the value of the argument
102+
is in the form -<arg> <val> or in the form -<arg><val> */
103+
dash = 0;
104+
if(!argv[ap_php_optind][2]) {
105+
ap_php_optind++;
106+
if (ap_php_optind == argc)
107+
return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRARG));
108+
ap_php_optarg = argv[ap_php_optind++];
109+
}
110+
else
111+
{
112+
ap_php_optarg = &argv[ap_php_optind][2];
113+
ap_php_optind++;
114+
}
115+
return(*cp);
116+
}
117+
else
118+
{
119+
if (!argv[ap_php_optind][optchr+1])
120+
{
121+
dash = 0;
122+
ap_php_optind++;
123+
}
124+
else
125+
optchr++;
126+
return(*cp);
127+
}
128+
assert(0);
129+
return(0);
130+
}
131+
132+
#ifdef TESTGETOPT
133+
int
134+
main (int argc, char **argv)
135+
{
136+
int c;
137+
extern char *ap_php_optarg;
138+
extern int ap_php_optind;
139+
int aflg = 0;
140+
int bflg = 0;
141+
int errflg = 0;
142+
char *ofile = NULL;
143+
144+
while ((c = ap_php_getopt(argc, argv, "abo:")) != EOF)
145+
switch (c) {
146+
case 'a':
147+
if (bflg)
148+
errflg++;
149+
else
150+
aflg++;
151+
break;
152+
case 'b':
153+
if (aflg)
154+
errflg++;
155+
else
156+
bflg++;
157+
break;
158+
case 'o':
159+
ofile = ap_php_optarg;
160+
(void)printf("ofile = %s\n", ofile);
161+
break;
162+
case '?':
163+
errflg++;
164+
}
165+
if (errflg) {
166+
(void)fprintf(stderr,
167+
"usage: cmd [-a|-b] [-o <filename>] files...\n");
168+
exit (2);
169+
}
170+
for ( ; ap_php_optind < argc; ap_php_optind++)
171+
(void)printf("%s\n", argv[ap_php_optind]);
172+
return 0;
173+
}
174+
175+
#endif /* TESTGETOPT */

0 commit comments

Comments
 (0)