-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_p4.cc
1594 lines (1356 loc) · 49.3 KB
/
php_p4.cc
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2001-2008, Perforce Software, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "clientapi.h"
#include "error.h"
#include "enviro.h"
#include "hostenv.h"
#include "i18napi.h"
#include "ident.h"
#include "strtable.h"
#include "undefdups.h"
extern "C"
{
#include "php.h"
}
#include "Zend/zend_exceptions.h"
#include "php_macros.h"
#include "php_p4_exception.h"
#include "php_p4_depotfile.h"
#include "php_p4_revision.h"
#include "php_p4_integration.h"
#include "specmgr.h"
#include "php_p4result.h"
#include "php_clientuser.h"
#include "php_clientapi.h"
#include "php_p4.h"
#include <iostream>
zend_class_entry *p4_ce;
static zend_object_handlers p4_object_handlers;
// convenience functions.
static zval p4php_create_revision_objects(zval *hash);
static zval p4php_create_integration_objects(zval *hash, int i);
static zval p4php_run_filelog(char *filespec, zval *this_ptr);
static zval p4php_create_p4_object(zend_class_entry *ce);
static bool p4php_client_is_tagged(zval *this_ptr);
static void p4php_fetch_spec(char *name, zval func, zval *args, INTERNAL_FUNCTION_PARAMETERS);
static void p4php_delete_spec(char *spectype, zval func, zval *args, INTERNAL_FUNCTION_PARAMETERS);
static void p4php_parse_or_format_spec(char *spectype, zval func, zval *args, INTERNAL_FUNCTION_PARAMETERS);
static void p4php_run_cmd(char *cmd, zval func, zval *args, INTERNAL_FUNCTION_PARAMETERS);
static void p4php_save_spec(char *spectype, zval func, zval *args, INTERNAL_FUNCTION_PARAMETERS);
// reusable for functions with no parameters
ZEND_BEGIN_ARG_INFO(__p4_no_args, 0)
ZEND_END_ARG_INFO()
// argument infos: magic functions:
ZEND_BEGIN_ARG_INFO(__p4_set_args, 0)
ZEND_ARG_INFO(0,property)
ZEND_ARG_INFO(0,value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_get_args, 0)
ZEND_ARG_INFO(0,property)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_isset_args, 0)
ZEND_ARG_INFO(0,isset_args)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_call_args, 0)
ZEND_ARG_INFO(0,call_arg_1)
ZEND_ARG_INFO(0,call_arg_2)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_unset_args, 0)
ZEND_ARG_INFO(0,unset_arg_1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_env_args, 0)
ZEND_ARG_INFO(0,var)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_format_spec_args, 0)
ZEND_ARG_INFO(0,spectype)
ZEND_ARG_ARRAY_INFO(0,dict,0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_parse_spec_args, 0)
ZEND_ARG_INFO(0,spectype)
ZEND_ARG_INFO(0,spec)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_run_args, 0)
ZEND_ARG_INFO(0,command)
ZEND_ARG_VARIADIC_INFO(0,mixed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_run_filelog_args, 0)
ZEND_ARG_INFO(0,fileSpec)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_run_login_args, 0)
ZEND_ARG_INFO(0,password)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_run_password_args, 0)
ZEND_ARG_INFO(0,oldpass)
ZEND_ARG_INFO(0,newpass)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_run_resolve_args, 0)
ZEND_ARG_INFO(0,resolver)
ZEND_ARG_ARRAY_INFO(0,args,0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_set_protocol_args, 0)
ZEND_ARG_INFO(0,key)
ZEND_ARG_INFO(0,value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_set_var_args, 0)
ZEND_ARG_INFO(0,key)
ZEND_ARG_INFO(0,value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(__p4_set_evar_args, 0)
ZEND_ARG_INFO(0,key)
ZEND_ARG_INFO(0,value)
ZEND_END_ARG_INFO()
// member function argument describtions:
/* Client API Properties */
static property_t p4_properties[] = {
/* Read / Write Properties */
{ "api_level", &PHPClientAPI::SetApiLevel, &PHPClientAPI::GetApiLevel, true },
{ "charset", &PHPClientAPI::SetCharset, &PHPClientAPI::GetCharset, true },
{ "client", &PHPClientAPI::SetClient, &PHPClientAPI::GetClient, true },
{ "cwd", &PHPClientAPI::SetCwd, &PHPClientAPI::GetCwd, true },
{ "exception_level",
&PHPClientAPI::SetExceptionLevel, &PHPClientAPI::GetExceptionLevel, true },
{ "handler", &PHPClientAPI::SetHandler, &PHPClientAPI::GetHandler, true },
{ "host", &PHPClientAPI::SetHost, &PHPClientAPI::GetHost, true },
{ "input", &PHPClientAPI::SetInput, &PHPClientAPI::GetInput, true },
{ "maxlocktime", &PHPClientAPI::SetMaxLockTime, &PHPClientAPI::GetMaxLockTime, true },
{ "maxresults", &PHPClientAPI::SetMaxResults, &PHPClientAPI::GetMaxResults, true },
{ "maxscanrows", &PHPClientAPI::SetMaxScanRows, &PHPClientAPI::GetMaxScanRows, true },
{ "password", &PHPClientAPI::SetPassword, &PHPClientAPI::GetPassword, true },
{ "port", &PHPClientAPI::SetPort, &PHPClientAPI::GetPort, true },
{ "prog", &PHPClientAPI::SetProg, &PHPClientAPI::GetProg, true },
{ "resolver", &PHPClientAPI::SetResolver, &PHPClientAPI::GetResolver, true },
{ "streams", &PHPClientAPI::SetStreams, &PHPClientAPI::GetStreams, true },
{ "tagged", &PHPClientAPI::SetTagged, &PHPClientAPI::GetTagged, true },
{ "ticket_file", &PHPClientAPI::SetTicketFile, &PHPClientAPI::GetTicketFile, true },
{ "user", &PHPClientAPI::SetUser, &PHPClientAPI::GetUser, true },
{ "version", &PHPClientAPI::SetVersion, &PHPClientAPI::GetVersion, true },
{ "expand_sequences",
&PHPClientAPI::SetExpandSequences, &PHPClientAPI::GetExpandSequences, true },
{ "loginsso", &PHPClientAPI::SetEnableSSO, &PHPClientAPI::GetEnableSSO, true },
{ "ssovars", NULL, &PHPClientAPI::GetSSOVars, true },
{ "ssopassresult",
&PHPClientAPI::SetSSOPassResult, &PHPClientAPI::GetSSOPassResult, true },
{ "ssofailresult",
&PHPClientAPI::SetSSOFailResult, &PHPClientAPI::GetSSOFailResult, true },
/* Read-Only Properties */
{ "errors", NULL, &PHPClientAPI::GetErrors, true },
{ "p4config_file", NULL, &PHPClientAPI::GetConfig, true },
{ "server_level", NULL, &PHPClientAPI::GetServerLevel, true },
{ "warnings", NULL, &PHPClientAPI::GetWarnings, true },
/* Sentinal */
{ NULL, NULL, NULL, false }
};
/* P4 Class Methods */
static zend_function_entry perforce_p4_functions[] = {
PHP_ME(P4, __construct, __p4_no_args, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(P4, __set, __p4_set_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, __get, __p4_get_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, __isset, __p4_isset_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, __call, __p4_call_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, __unset, __p4_unset_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, connect, __p4_no_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, connected, __p4_no_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, disconnect, __p4_no_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, env, __p4_env_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, format_spec, __p4_format_spec_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, identify, __p4_no_args, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(P4, parse_spec, __p4_parse_spec_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, run, __p4_run_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, run_filelog, __p4_run_filelog_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, run_login, __p4_run_login_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, run_password, __p4_run_password_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, run_resolve, __p4_run_resolve_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, run_submit, __p4_no_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, set_protocol, __p4_set_protocol_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, set_var, __p4_set_var_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, set_evar, __p4_set_evar_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, get_evar, __p4_env_args, ZEND_ACC_PUBLIC)
PHP_ME(P4, set_trace, __p4_no_args, ZEND_ACC_PUBLIC)
{ NULL, NULL, NULL }
};
/* free storage allocated for P4 class. */
void p4_object_free_storage(zend_object *object TSRMLS_DC)
{
p4_object *obj = php_p4_object_fetch_object(object);
delete obj->client;
zend_object_std_dtor(object TSRMLS_CC);
}
static void p4_object_destroy(zend_object *object TSRMLS_DC)
{
p4_object *obj = php_p4_object_fetch_object(object);
zend_objects_destroy_object(object);
}
/* create_object handler for P4 class. */
zend_object * p4_create_object(zend_class_entry *type TSRMLS_DC)
{
p4_object *obj = (p4_object *) ecalloc(1, sizeof(struct p4_object) + zend_object_properties_size(type));
zend_object_std_init(&obj->std, type TSRMLS_CC);
obj->std.handlers = &p4_object_handlers;
return &obj->std;
}
/* Register the P4 Class. */
void register_p4_class(INIT_FUNC_ARGS)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "P4", perforce_p4_functions);
p4_ce = zend_register_internal_class(&ce TSRMLS_CC);
p4_ce->create_object = p4_create_object;
memcpy(&p4_object_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
p4_object_handlers.clone_obj = NULL;
p4_object_handlers.offset = XtOffsetOf(struct p4_object, std);
p4_object_handlers.free_obj = p4_object_free_storage;
p4_object_handlers.dtor_obj = p4_object_destroy;
}
/* Fetch the PHPClientAPI instance used by this instance from the pool. */
PHPClientAPI *get_client_api(zval *this_ptr)
{
p4_object *obj = Z_P4_OBJ_P(this_ptr);
// protect against null client
if (obj->client == NULL) {
zend_error(E_ERROR, "Cannot get perforce client api instance");
}
return obj->client;
}
/* {{{ proto void P4::__construct()
Constructor: Initialize certain fields. */
PHP_METHOD(P4, __construct)
{
p4_object *obj = Z_P4_OBJ_P(getThis());
obj->client = new PHPClientAPI;
}
/* }}} */
/* {{{ proto mixed P4::__get(string name)
Magic method: __get. Reads properties from client API property list. */
PHP_METHOD(P4, __get)
{
char *name;
size_t name_len;
PHPClientAPI *client;
bool found = false;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"s",
&name, &name_len) == FAILURE) {
if ( PHP_PERFORCE_DEBUG_DATA )
std::cerr << "__get() returning null" << std::endl;
RETURN_NULL();
}
client = get_client_api(getThis());
/* look for an element in the properties table. if an accessor exists for
* the element, call it. */
for (property_t *ptr = p4_properties; ptr->property != NULL; ptr++) {
if (strcmp(name, ptr->property) == 0) {
found = true;
accessor_t accessor = ptr->accessor;
mutator_t mutator = ptr->mutator;
if (accessor == NULL) {
continue;
}
// read-only attributes are set by the server or environment
if (mutator != NULL && ptr->isset == false) {
RETURN_NULL();
}
(client->*accessor)(return_value);
}
}
/* if no element was found in the properties table, fall back to default
* php class behaviour, could be an undeclared field set at runtime. */
if (!found) {
zval rv;
#if ( PHP_VERSION_ID < 80000)
zval *rval = zend_read_property(p4_ce, getThis(), name, sizeof(name) - 1, 0, &rv);
#else
zval *rval = zend_read_property(p4_ce, Z_OBJ_P(getThis()), name, sizeof(name) - 1, 0, &rv);
#endif
ZVAL_DUP(return_value, rval);
return;
}
}
/* }}} */
/* {{{ proto void P4::__set(string name)
Magic method: __set. Reads properties from client API property list. */
PHP_METHOD(P4, __set)
{
char *name;
size_t name_len;
zval *value;
PHPClientAPI *client;
bool read_only = false;
bool found = false;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"sz",
&name, &name_len, &value) == FAILURE) {
RETURN_NULL();
}
client = get_client_api(getThis());
/* look for an element in the properties table. if a mutator method is
* found, call it. */
for (property_t *ptr = p4_properties; ptr->property != NULL; ptr++) {
if (strcmp(name, ptr->property) == 0) {
found = true;
mutator_t mutator = ptr->mutator;
if (mutator == NULL) {
read_only = true;
continue;
}
ptr->isset = true;
(client->*mutator)(value);
}
}
/* throw an exception if caller is trying to set a read-only attribute. */
if (read_only) {
StrBuf m;
m << "Attempted to set read-only attribute: ";
m.Append(name);
zend_throw_exception_ex(get_p4_exception_ce(),
0 TSRMLS_CC, m.Text());
}
/* fallback to default php behaviour if attribute is not found. */
if (!found) {
#if ( PHP_VERSION_ID < 80000)
zend_update_property(p4_ce, getThis(), name, sizeof(name) - 1, value TSRMLS_CC);
#else
zend_update_property(p4_ce, Z_OBJ_P(getThis()), name, sizeof(name) - 1, value TSRMLS_CC);
#endif
}
}
/* }}} */
/* {{{ proto bool P4::__isset(string name)
Magic method: __isset. */
PHP_METHOD(P4, __isset)
{
char *name;
size_t name_len;
bool isset;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"s",
&name, &name_len) == FAILURE) {
RETURN_NULL();
}
// default to false
isset = false;
for (property_t *ptr = p4_properties; ptr->property != NULL; ptr++) {
if (strcmp(name, ptr->property) == 0) {
isset = ptr->isset;
}
}
RETVAL_BOOL(isset);
}
/* }}} */
/* {{{ proto mixed P4::__call(string name, array arguments)
Magic method: __call. triggered when invoking inaccessible methods. */
PHP_METHOD(P4, __call)
{
char *name;
size_t name_len;
zval *args;
zval func;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"sz",
&name, &name_len, &args) == FAILURE) {
RETURN_NULL();
}
// run method name
ZVAL_STRING(&func, (char *)"run");
if (strncmp(name, "fetch_", sizeof("fetch_") - 1) == 0) {
name += strlen("fetch_");
p4php_fetch_spec(name, func, args, INTERNAL_FUNCTION_PARAM_PASSTHRU);
zval_dtor(&func);
return;
} else if (strncmp(name, "delete_", sizeof("delete_") - 1) == 0) {
name += strlen("delete_");
p4php_delete_spec(name, func, args, INTERNAL_FUNCTION_PARAM_PASSTHRU);
zval_dtor(&func);
return;
} else if (strncmp(name, "format_", sizeof("format_") - 1) == 0) {
name += strlen("format_");
// set function name
zval_dtor(&func);
ZVAL_STRING(&func, (char *)"format_spec");
p4php_parse_or_format_spec(name, func, args, INTERNAL_FUNCTION_PARAM_PASSTHRU);
zval_dtor(&func);
return;
} else if (strncmp(name, "parse_", sizeof("parse_") - 1) == 0) {
name += strlen("parse_");
// set function name
zval_dtor(&func);
ZVAL_STRING(&func, (char *)"parse_spec");
p4php_parse_or_format_spec(name, func, args, INTERNAL_FUNCTION_PARAM_PASSTHRU);
zval_dtor(&func);
return;
} else if (strncmp(name, "run_", sizeof("run_") - 1) == 0) {
name += strlen("run_");
p4php_run_cmd(name, func, args, INTERNAL_FUNCTION_PARAM_PASSTHRU);
zval_dtor(&func);
return;
} else if (strncmp(name, "save_", sizeof("save_") - 1) == 0) {
name += strlen("save_");
p4php_save_spec(name, func, args, INTERNAL_FUNCTION_PARAM_PASSTHRU);
zval_dtor(&func);
return;
} else {
// Method not found.
StrBuf msg;
msg << "Call to undefined method P4::";
msg.Append(name);
msg.Append("()");
php_error(E_ERROR, msg.Text(), 1);
RETURN_NULL();
}
}
/* }}} */
/* {{{ proto void P4::__unset(string name)
Invoked when unset is used on an inaccessible member. */
PHP_METHOD(P4, __unset)
{
char *name;
size_t name_len;
PHPClientAPI *client;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"s",
&name, &name_len) == FAILURE) {
RETURN_NULL();
}
for (property_t *ptr = p4_properties; ptr->property != NULL; ptr++) {
if (strcmp(name, ptr->property) == 0) {
zval val;
ptr->isset = false;
mutator_t mutator = ptr->mutator;
if (mutator == NULL) continue;
ZVAL_NULL(&val);
client = get_client_api(getThis());
(client->*mutator)(&val);
}
}
}
/* }}} */
/* {{{ proto bool P4::connect()
Connect to the perforce server. */
PHP_METHOD(P4, connect)
{
zval connected;
PHPClientAPI *client;
client = get_client_api(getThis());
connected = client->Connect();
if (Z_TYPE(connected) != IS_TRUE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool P4::connected()
Determine whether client is connected or not. */
PHP_METHOD(P4, connected)
{
PHPClientAPI *client = get_client_api(getThis());
RETURN_BOOL(client->Connected());
}
/* }}} */
/* {{{ proto void P4::disconnect()
Disconnect from Perforce server. */
PHP_METHOD(P4, disconnect)
{
PHPClientAPI *client = get_client_api(getThis());
client->Disconnect();
}
/* }}} */
/* {{{ proto string P4::env(string var)
Get a value from the environment following the Perforce conventions,
including honouring P4CONFIG files, etc. */
PHP_METHOD(P4, env)
{
char *var;
size_t var_len;
PHPClientAPI *client;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"s",
&var, &var_len) == FAILURE) {
RETURN_NULL();
}
client = get_client_api(getThis());
ZVAL_STRING(return_value, (char *)client->GetEnv(var));
}
/* }}} */
/* {{{ proto string P4::format_spec(string spectype, array dict)
* Converts fields in dict to a Perforce form (spec). */
PHP_METHOD(P4, format_spec)
{
const char *type;
size_t type_len;
zval *hash;
PHPClientAPI *client;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"sz",
&type, &type_len, &hash) == FAILURE) {
RETURN_NULL();
}
client = get_client_api(getThis());
// FormatSpec wraps estrdup, so don't duplicate from ZVAL_STRING
RETURN_STR(client->FormatSpec(type, hash));
}
/* }}} */
/* {{{ proto string P4::identify()
Print build information including P4-PHP version and P4API version. */
PHP_METHOD(P4, identify)
{
StrBuf s;
ident.GetMessage(&s);
RETVAL_STRING(s.Text());
}
/* }}} */
/* {{{ proto array P4::parse_spec(string spectype, string spec)
Parses a Perforce form (spec) into a array. */
PHP_METHOD(P4, parse_spec)
{
const char *type, *form;
size_t type_len, form_len;
PHPClientAPI *client;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"ss",
&type, &type_len, &form, &form_len) == FAILURE) {
RETURN_NULL();
}
client = get_client_api(getThis());
client->ParseSpec(type, form, return_value);
}
/* }}} */
/* {{{ proto mixed P4::run(string command, [, mixed ... ])
Run a Perforce command and return its results. */
PHP_METHOD(P4, run)
{
int argc = ZEND_NUM_ARGS();
zval *args;
StrBuf cmdString;
PHPClientAPI *client;
client = get_client_api(getThis());
args = (zval *) safe_emalloc(argc, sizeof(zval), 0);
if (argc == 0 || zend_get_parameters_array_ex(argc, args) == FAILURE) {
efree(args);
WRONG_PARAM_COUNT;
}
zend_string ** strArgs = (zend_string **) safe_emalloc(argc, sizeof(zend_string *), 0);
for (int i = 0 ; i < argc; i++) {
strArgs[i] = zval_get_string(&args[i]);
}
if (PHP_PERFORCE_DEBUG) {
cmdString << "\"p4";
for (int i = 0; i < argc; i++) {
switch (Z_TYPE(args[i])) {
case IS_STRING:
cmdString << " " << Z_STRVAL(args[i]);
break;
case IS_LONG:
cmdString << " " << Z_LVAL(args[i]);
break;
case IS_DOUBLE:
cmdString << " " << static_cast<P4INT64>(Z_DVAL(args[i]));
break;
case IS_TRUE:
cmdString << " " << "TRUE";
break;
case IS_FALSE:
cmdString << " " << "FALSE";
break;
case IS_REFERENCE:
cmdString << " REFERENCE";
break;
default:
cmdString << " [Non alpha-numeric arg]";
break;
}
}
cmdString << "\"";
std::cerr << "[P4] {php_run} " << cmdString.Text() << std::endl;
}
// run command with args.
client->Run(strArgs, argc, return_value);
efree(args);
for (int i = 0; i < argc; i++ ) {
zend_string_release(strArgs[i]);
}
efree(strArgs);
}
/* }}} */
void enumerate_how(zval *record, zval *integrations, zend_string *key)
{
zval* record_elem;
int counter = 0;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(record), record_elem)
{
zval* integration;
// fetch the actual P4_Integration instance out of the array we
// allocated earlier.
if ( (integration = zend_hash_index_find(Z_ARRVAL_P(integrations), counter)) == NULL ) {
php_error(E_WARNING, "Could not retrieve P4_Integration instance", 1);
continue;
}
#if ( PHP_VERSION_ID < 80000)
zend_update_property_ex(get_p4_integration_ce(), integration, key, record_elem);
#else
zend_update_property_ex(get_p4_integration_ce(), Z_OBJ_P(integration), key, record_elem);
#endif
counter++;
}
ZEND_HASH_FOREACH_END();
}
void
enumerate_revisions(zend_class_entry* ce, zval& p4_depotfile, zval *val, zval* revision, int i, zval* integrations)
{
// loop through each element of the hash representing this revision:
// {
// 'depotFile' => '//depot/foo',
// 'client' => [ 'client', 'client', 'client', 'client' ],
// 'how' => [ [ 'ignored' ], None, None, [ 'branch from' ] ]
// }
zval * values;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(val), values)
{
/*****************************************************************
* There are three types of values we will encounter in this loop:
*
* 1. Scalars - We only expect 'depotFile' which is assigned to the
* P4_DepotFile instance as it will be the same for
* each revision.
*
* 2. Arrays - Such as 'client', 'rev', 'action'. We will extract
* the element that corresponds to the revision we are
* currently working on (determined by the loop counter)
* and assign the element as a property on the revision.
*
* 3. Arrays of Arrays - Such as 'how', 'srev'. These indicate
* integrations on the specific revision. Like all arrays
* we will extract the element (which will be an array)
* that corresponds to the revision we are currently
* working with. If the value is NULL, we can assume that
* there are no integrations on the revision. If the value
* is an array, we must collect each of the elements,
* assigning them to the corresponding P4_Integration
* object for this revision.
******************************************************************/
// If we found a non-array entry, it's the depotFile - assign it
// and move on
if ( Z_TYPE_P(values) != IS_ARRAY ) {
#if ( PHP_VERSION_ID < 80000)
zend_update_property(ce, &p4_depotfile, ZEND_STRS("depotFile") - 1, values);
#else
zend_update_property(ce, Z_OBJ_P(&p4_depotfile), ZEND_STRS("depotFile") - 1, values);
#endif
continue;
}
HashTable *ht = Z_ARRVAL_P(values);
HashPosition pos = 0;
zend_string *key;
zend_ulong index;
int cur = zend_hash_get_current_key_ex(ht, &key, &index, &pos);
if ( cur == HASH_KEY_IS_LONG ) {
// Not quite sure what encountering an element with a numeric
// key would mean... probably best to just move along and pretend
// we didn't see it. *whistles*
continue;
}
// depotFile needs to be set on the P4_DepotFile instance
// if (strncmp(key, "depotFile", strlen("depotFile")) == 0) {
// zend_update_property(ce, p4_depotfile, key, key_len - 1,
// *values TSRMLS_CC);
// }
// if it's not an array, simply set the property on the
// P4_Revision object.
if ( Z_TYPE_P(values) != IS_ARRAY ) {
#if ( PHP_VERSION_ID < 80000)
zend_update_property_ex(get_p4_revision_ce(), revision, key, values);
#else
zend_update_property_ex(get_p4_revision_ce(), Z_OBJ_P(revision), key, values);
#endif
continue;
}
// extract the element from the values array that corresponds
// to the revision object we're dealing with currently
// if no element exists, act as though it is null and continue
zval* record;
if ( (record = zend_hash_index_find(Z_ARRVAL_P(values), i )) == NULL ) {
continue;
}
if ( Z_TYPE_P(record) != IS_ARRAY ) {
#if ( PHP_VERSION_ID < 80000)
zend_update_property_ex(get_p4_revision_ce(), revision, key, record);
#else
zend_update_property_ex(get_p4_revision_ce(), Z_OBJ_P(revision), key, record);
#endif
continue;
}
// at this point we're probably dealing with an integration
// record (e.g. 'how', 'srev', 'erev', ...). double check
if ( zend_string_equals_literal(key, "how") != 0
&& zend_string_equals_literal(key, "file") != 0
&& zend_string_equals_literal(key, "srev") != 0
&& zend_string_equals_literal(key, "erev") != 0 ) {
if ( PHP_PERFORCE_DEBUG )
std::cerr << "Unexpected element: " << key << std::endl;
continue;
}
// at this point, key, record will be something like:
// erev, Array( [0] => #1, [1] => #2 )
if ( integrations == NULL ) {
php_error(E_WARNING, "Error parsing integrations.", 1);
continue;
}
enumerate_how(record, integrations, key);
}
ZEND_HASH_FOREACH_END();
}
/* {{{ proto array P4::run_filelog(string fileSpec)
* Runs a p4 filelog and returns an array of P4_DepotFile objects. */
PHP_METHOD(P4, run_filelog)
{
char *filespec;
size_t filespec_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"s",
&filespec, &filespec_len) == FAILURE) {
RETURN_NULL();
}
// get raw 'filelog' results.
zval results = p4php_run_filelog(filespec, getThis());
if (Z_TYPE(results) == IS_NULL) {
// return an empty array.
array_init(return_value);
zval_dtor(&results);
return;
}
if (!p4php_client_is_tagged(getThis())) {
// if tagged output is disabled, our job is mostly done.
// just copy the value in results to return_value and free it.
RETVAL_ZVAL(&results, 1, 1);
return;
}
/*
* Filelog results are returned as an array of hashes. The elements of
* the hashes will be arrays in most cases or strings. We need to
* transform these hashes into P4_DepotFile instances. Each
* P4_DepotFile instance has a depotFile field and a revisions array.
* Each element in the revisions array is a P4_Revision instance. Each
* P4_Revision instance has a number of scalar fields and an array of
* P4_Integration instances.
*
* For example, the following:
* [ { rev: [2, 1], depotFile : '//depot/file.txt', action: ['edit', 'add'] } ]
*
* Would become the following:
* [ P4_DepotFile(
* depotFile = '//depot/file.txt',
* revisions = [
* P4_Revision( rev = 1, action = add ),
* P4_Revision( rev = 2, action = edit )
* ]
* )
* ]
*/
array_init(return_value);
// tagged results. results will be an array of hashes, each hash will be
// represented by one P4_DepotFile instance. Each P4_DepotFile instance
// can have one or more P4_Revision instances and each P4_Revision
// instance can have zero or more P4_Integration instances.
zval *val;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(results), val)
{
// elem should be an array of hashes which will be represented as
// a P4_DepotFile object:
//
// [
// {
// 'depotFile' => '//depot/bar',
// 'rev' => [4, 3, 2, 1],
// 'how' => [ [ 'ignored' ], None, None, [ 'branch from' ] ],
// ^ list of integrations in the first revision
// ...
// }
// ]
// bail if element is not a hash for some reason.
if (Z_TYPE_P(val) != IS_ARRAY) {
continue;
}
// Create a P4_DepotFile object to represent this hash.
zend_class_entry *ce = get_p4_depotfile_ce();
zval p4_depotfile = p4php_create_p4_object(ce);
// Create P4_Revision objects for this P4_DepotFile instance.
zval revisions = p4php_create_revision_objects(val);
// loop over each of the revisions in this P4_DepotFile instance
int nrevs = zend_hash_num_elements(Z_ARRVAL(revisions));
// in the above example, there are 4 revisions (one for each element
// in each of the array elements).
for (int i = 0; i < nrevs; i++) {
zval *revision;
// fetch the actual P4_Revision instance out of the array we
// allocated earlier.
if ((revision = zend_hash_index_find(Z_ARRVAL(revisions), i)) == NULL)
{
php_error(E_WARNING,
"Could not retrieve P4_Revision instance", 1);
continue;
}
// create P4_Integration objects for this revision.
zval integrations = p4php_create_integration_objects(val, i);
// loop through each element of the hash representing this revision:
// {
// 'depotFile' => '//depot/foo',
// 'client' => [ 'client', 'client', 'client', 'client' ],
// 'how' => [ [ 'ignored' ], None, None, [ 'branch from' ] ]
// }
enumerate_revisions(ce, p4_depotfile, val, revision, i, &integrations);
if (!Z_ISNULL(integrations)) {
#if ( PHP_VERSION_ID < 80000)
zend_update_property(get_p4_revision_ce(), revision,
ZEND_STRS("integrations")-1, &integrations TSRMLS_CC);
#else
zend_update_property(get_p4_revision_ce(), Z_OBJ_P(revision),
ZEND_STRS("integrations")-1, &integrations TSRMLS_CC);
#endif
Z_TRY_DELREF(integrations);
}
} /* for (int i = 0; i < nrevs; i++) */
for (int i = 0; i < nrevs; i++) {
zval *revision;
if ((revision = zend_hash_index_find(Z_ARRVAL(revisions), i)) == NULL)
{
php_error(E_WARNING, "Problem parsing revision output.", 1);
}
}
#if ( PHP_VERSION_ID < 80000)
zend_update_property(ce, &p4_depotfile, ZEND_STRS("revisions")-1, &revisions TSRMLS_CC);
#else
zend_update_property(ce, Z_OBJ_P(&p4_depotfile), ZEND_STRS("revisions")-1, &revisions TSRMLS_CC);
#endif
Z_TRY_DELREF(revisions);
add_next_index_zval(return_value, &p4_depotfile);
}
ZEND_HASH_FOREACH_END();
zval_dtor(&results);
}
/* }}} */
/* {{{ proto array P4::run_login(string password).
Runs p4 login using a password set by the user. */
PHP_METHOD(P4, run_login)
{
zval *object;
PHPClientAPI *client;
zval params[1];
zval param0;
zval *password;
zval func;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"z", &password) == FAILURE) {
RETURN_NULL();
}
ZVAL_STRING(&func, "run");
ZVAL_STRING(¶m0, "login");
object = getThis();
client = get_client_api(object);
client->SetInput(password);
params[0] = param0;
call_user_function(NULL, object, &func, return_value, 1, params TSRMLS_CC);
zval_dtor(&func);
zval_dtor(¶m0);
}
/* }}} */
/* {{{ proto array P4::run_password(string oldpass, string newpass).
A thin wrapper to make it easy to change your password. */
PHP_METHOD(P4, run_password)
{
zval *object;
zval *oldpass, *newpass;
zval params[1];
zval func, param0;